changeset: 17:510dfb51a408
tag: tip
user: pancake <pancake_AT_nopcode.org>
date: Mon Apr 26 00:13:21 2010 +0200
files: Makefile config.def.h gi_sdl.c swk.c swk.h test.c
description:
added config.h to configure colors, font size and keys
implied simplification in gi_sdl
diff -r 704afc9e5950 -r 510dfb51a408 Makefile
--- a/Makefile Thu Apr 22 15:01:14 2010 +0200
+++ b/Makefile Mon Apr 26 00:13:21 2010 +0200
@@ -14,7 +14,10 @@
all: static test
-test: test.o libswk.a
+config.h:
+ cp config.def.h config.h
+
+test: config.h test.o libswk.a
${CC} test.o -o test libswk.a ${GI_LIBS}
clean:
@@ -30,7 +33,9 @@
static: libswk.a
-libswk.a: swk.o ${GI_OBJS}
+swk.o: config.h
+
+libswk.a: config.h swk.o ${GI_OBJS}
rm -f libswk.a
ar qcvf libswk.a swk.o ${GI_OBJS}
echo CFLAGS+=-I${PREFIX}/include > swk.mk
diff -r 704afc9e5950 -r 510dfb51a408 config.def.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/config.def.h Mon Apr 26 00:13:21 2010 +0200
@@ -0,0 +1,30 @@
+/* See LICENSE file for copyright and license details. */
+
+/* appearance */
+#define FONTSIZE 14
+// SDL
+#define SWK_COLOR(r,g,b) 0x##r,0x##g,0x##b
+// X11
+//#define SWK_COLOR(r,g,b) r##g##b
+
+#define HICOLOR SWK_COLOR(0,66,ff)
+#define FGCOLOR SWK_COLOR(ff,ff,ff)
+#define BGCOLOR SWK_COLOR(00,00,00)
+#define TFCOLOR SWK_COLOR(cc,cc,cc)
+
+/* key bindings */
+static SwkKeyBind keys[] = {
+ { Ctrl, 'j', swk_focus_next },
+ { Ctrl, 'k', swk_focus_prev },
+ { Ctrl, 8 , swk_focus_first },
+ { Ctrl, 9 , swk_focus_prev },
+ { 0 , 9 , swk_focus_next },
+ { Ctrl, 10 , swk_focus_next },
+ { Ctrl, 11 , swk_focus_prev },
+ { Ctrl, 12 , swk_focus_activate },
+ { 0 , KUp, swk_focus_prev },
+ { 0 , KDown, swk_focus_next },
+ { 0 , 13 , swk_focus_activate },
+ { Ctrl, 12 , swk_focus_activate },
+ { 0 }
+};
diff -r 704afc9e5950 -r 510dfb51a408 gi_sdl.c
--- a/gi_sdl.c Thu Apr 22 15:01:14 2010 +0200
+++ b/gi_sdl.c Mon Apr 26 00:13:21 2010 +0200
@@ -2,17 +2,22 @@
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include "swk.h"
+#include "config.h"
+//#define SWK_COLOR(r,g,b) 0x##r,0x##g,0x##b
+
+#if 0
#define HICOLOR 0x00,0x66,0xff
#define FGCOLOR 0xff,0xff,0xff
#define BGCOLOR 0x00,0x00,0x00
#define TFCOLOR 0xcc,0xcc,0xcc
+#endif
+
+/* --- */
#define FONTNAME "Inconsolata.otf"
-#define FONTSIZE 14
#define FS FONTSIZE
#define BPP 32
#define SDLFLAGS SDL_DOUBLEBUF|SDL_RESIZABLE
-/* --- */
static Uint32 pal[ColorLast];
static SDL_Color fontcolor = { TFCOLOR };
@@ -181,7 +186,6 @@
int i;
x1 *= FS; y1 *= FS;
x2 *= FS; y2 *= FS;
-
if(x2==0) for(i=0;i<y2;i++) putpixel(x1, y1+i, pal[color]);
else
if(y2==0) for(i=0;i<x2;i++) putpixel(x1+i, y1, pal[color]);
diff -r 704afc9e5950 -r 510dfb51a408 swk.c
--- a/swk.c Thu Apr 22 15:01:14 2010 +0200
+++ b/swk.c Mon Apr 26 00:13:21 2010 +0200
@@ -3,6 +3,7 @@
#include <string.h>
#include <stdlib.h>
#include "swk.h"
+#include "config.h"
// move into SwkWindow* ?
static int running = 0;
@@ -10,7 +11,8 @@
int
swk_init(SwkWindow *w) {
w->_e.win = w;
- swk_focus_first(w);
+ if (w->box == NULL)
+ swk_focus_first(w);
if(w->r.w == 0 || w->r.h == 0) {
w->r.w = 640;
w->r.h = 480;
@@ -88,6 +90,12 @@
return swk_gi_has_event(w);
}
+void
+swk_focus_activate(SwkWindow *w) {
+ w->_e.box = w->box;
+ w->_e.type = EClick;
+}
+
SwkEvent *
swk_next_event(SwkWindow *w) {
if(running)
@@ -99,45 +107,19 @@
void
swk_handle_event(SwkEvent *e) {
+ int i;
SwkBox *b;
switch(e->type) {
case EKey:
- // TODO: ^F ullscreen? handle ^Y and ^P to copypasta box->text
- // ^A focus first widget, ^E focus last widget ?
- if(e->data.key.modmask == 2) {
- switch(e->data.key.keycode) {
- case 8:
- swk_focus_first(e->win);
- break;
- case 10:
- swk_focus_next(e->win);
- break;
- case 11:
- swk_focus_prev(e->win);
- break;
- case 12:
- e->type = EClick;
+ for(i=0; keys[i].cb; i++) {
+ if (e->data.key.modmask == keys[i].modmask
+ && e->data.key.keycode == keys[i].keycode) {
+ keys[i].cb(e->win);
break;
}
- } else
- switch(e->data.key.keycode) {
- case KUp:
- swk_focus_prev(e->win);
- break;
- case KDown:
- swk_focus_next(e->win);
- break;
- case 9: // TAB
- if(e->data.key.modmask)
- swk_focus_prev(e->win);
- else swk_focus_next(e->win);
- swk_update(e->win);
- break;
- case 13: // ENTER
- e->box = e->win->box;
- e->type = EClick;
- break;
- case 27: // ESC
+ }
+ /* XXX: this must be implemented in app? */
+ if (e->data.key.keycode==27) {
e->box = e->win->box;
e->type = EQuit;
swk_exit();
diff -r 704afc9e5950 -r 510dfb51a408 swk.h
--- a/swk.h Thu Apr 22 15:01:14 2010 +0200
+++ b/swk.h Mon Apr 26 00:13:21 2010 +0200
@@ -9,7 +9,10 @@
typedef enum { KUp=0xe0, KDown=0xe1, KLeft=0xe2, KRight=0xe3 } SwkKeyCode;
typedef struct SwkBox SwkBox;
+typedef struct SwkEvent SwkEvent;
typedef struct SwkWindow SwkWindow;
+typedef void (*SwkEventCallback)(SwkEvent *e);
+typedef void (*SwkKeyCallback)(SwkWindow *w);
typedef struct {
int x;
@@ -30,11 +33,17 @@
} Click;
typedef struct {
+ int modmask;
int keycode;
- int modmask;
} Key;
typedef struct {
+ int modmask;
+ int keycode;
+ SwkKeyCallback cb;
+} SwkKeyBind;
+
+struct SwkEvent {
SwkEventType type;
SwkBox *box;
SwkWindow *win;
@@ -45,9 +54,7 @@
Rect expose;
int rows;
} data;
-} SwkEvent;
-
-typedef void (*SwkEventCallback)(SwkEvent *e);
+};
struct SwkBox {
Rect r;
@@ -77,6 +84,7 @@
void swk_focus_first(SwkWindow *w);
void swk_focus_next(SwkWindow *w);
void swk_focus_prev(SwkWindow *w);
+void swk_focus_activate(SwkWindow *w);
void swk_button(SwkEvent *e);
void swk_label(SwkEvent *e);
diff -r 704afc9e5950 -r 510dfb51a408 test.c
--- a/test.c Thu Apr 22 15:01:14 2010 +0200
+++ b/test.c Mon Apr 26 00:13:21 2010 +0200
@@ -38,7 +38,8 @@
main() {
SwkWindow w = {
.title="Hello World",
- .boxes=helloworld
+ .boxes=helloworld,
+ .box=helloworld+10
};
if(!swk_init(&w))
return 1;
Received on Sun Apr 25 2010 - 22:19:50 UTC
This archive was generated by hypermail 2.2.0 : Sun Apr 25 2010 - 22:24:04 UTC