swk

static widget kit
git clone git://git.suckless.org/swk
Log | Files | Refs | README | LICENSE

commit b8b4fc7a82b093b2f9dae1ea19dbc74d5747adde
parent f28565e74c46df6dd482e6beb835fc09992d3542
Author: pancake <pancake@nopcode.org>
Date:   Sun, 22 Aug 2010 07:54:34 +0200

honor config.h colors and fix window boundaries in gi_x11
use libdraw as dependency for gi_x11
and other minor cosmetic changes
Diffstat:
Makefile | 2+-
TODO | 2++
config.def.h | 18+++++++++++-------
gi_x11.c | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
swk.c | 6++++--
swk.h | 2+-
t/Makefile | 8++++----
t/calc.c | 4++--
t/test.c | 2+-
9 files changed, 85 insertions(+), 44 deletions(-)

diff --git a/Makefile b/Makefile @@ -9,7 +9,7 @@ ifeq (${GI},sdl) GI_LIBS=-lSDL -lSDL_ttf -lSDL_image else ifeq (${GI},x11) -GI_LIBS=-lX11 +GI_LIBS=-lX11 -ldraw endif endif diff --git a/TODO b/TODO @@ -1,5 +1,7 @@ TODO ==== + * Add thresold to detect click and pan + * implement a top bar to mark selected column * support multiline text widget * support for user-defined input handler (when input widget is activated, run xterm -e vim str > text) diff --git a/config.def.h b/config.def.h @@ -8,26 +8,28 @@ #define WINWIDTH 640 #define WINHEIGHT 480 #define TOUCHSCREEN 0 -// SDL +#ifdef FG +#define SWK_COLOR(r,g,b) 0x##r##g##b +#else #define SWK_COLOR(r,g,b) 0x##r,0x##g,0x##b -// X11 -//#define SWK_COLOR(r,g,b) r##g##b +#endif -#define HICOLOR SWK_COLOR(0,66,ff) +#define HICOLOR SWK_COLOR(00,66,ff) #define BGCOLOR SWK_COLOR(20,20,20) #define FGCOLOR SWK_COLOR(e0,e0,e0) #define TFCOLOR SWK_COLOR(cc,cc,cc) /* key bindings */ static SwkKeyBind keys[] = { + { 0, '\n', swk_focus_activate}, { Ctrl, 'j', swk_focus_next }, { Ctrl, 'k', swk_focus_prev }, - { Ctrl, 'h' , swk_column_move_left }, - { Ctrl, 'l', swk_column_move_right }, //{ Ctrl, 8 , swk_focus_first }, //{ Ctrl, 9 , swk_focus_prev }, { Ctrl, 8 , swk_column_move_left }, - { Ctrl, 12 , swk_column_move_right }, + { Ctrl, 12 , swk_column_move_right }, + { Ctrl, 'h' , swk_column_move_left }, + { Ctrl, 'l', swk_column_move_right }, { 0 , 9 , swk_focus_next }, { Ctrl, 10 , swk_focus_next }, { Ctrl, 11 , swk_focus_prev }, @@ -38,6 +40,8 @@ static SwkKeyBind keys[] = { { Ctrl, 12 , swk_focus_activate }, { Ctrl|Shift, 10, swk_scroll_down }, { Ctrl|Shift, 11, swk_scroll_up }, + { Ctrl|Shift, 'J', swk_scroll_up }, + { Ctrl|Shift, 'K', swk_scroll_down }, { Ctrl, '+', swk_fontsize_increase }, { Ctrl, '-', swk_fontsize_decrease }, { 0 } diff --git a/gi_x11.c b/gi_x11.c @@ -8,45 +8,57 @@ #include <X11/keysym.h> #include <X11/Xlib.h> #include <X11/Xutil.h> +#include <draw.h> #include "swk.h" #define SWK #include "config.h" #define FONTNAME "-*-*-medium-*-*-*-14-*-*-*-*-*-*-*" +//#define FONTNAME "10x20" -static Drawable drawable; static int fs = FONTSIZE; // TODO: we need fsW and fsH static Window window; -static int screen; -static Display *display = NULL; static int first = 1; +static DC *dc = NULL; +static int col[ColorLast]; +static int colors[ColorLast] = { FGCOLOR, BGCOLOR, HICOLOR, TFCOLOR }; #define EVENTMASK PointerMotionMask | ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask int swk_gi_fontsize(int sz) { fs += sz*2; - /* TODO */ + /* TODO: resize font */ return 1; } +static Window dc_window(DC *dc, int x, int y, int w, int h) { + Drawable drawable; + Window window; + int screen = DefaultScreen(dc->dpy); + window = XCreateSimpleWindow(dc->dpy, RootWindow(dc->dpy, screen), + x, y, w, h, 1, col[ColorBG], col[ColorFG]); + drawable = XCreatePixmap(dc->dpy, window, w, h, + DefaultDepth(dc->dpy, screen)); + XSelectInput(dc->dpy, window, EVENTMASK); + XMapWindow(dc->dpy, window); + return window; +} + int swk_gi_init(SwkWindow *w) { + int i; + char buf[128]; if(first) { first = 0; - display = XOpenDisplay(NULL); - if(display == NULL) { - fprintf(stderr, "Cannot open display\n"); + if (!(dc = dc_init())) return 0; + for(i=0;i<ColorLast;i++) { + sprintf(buf, "#%06x", colors[i]); + col[i] = dc_color(dc, buf); } - screen = DefaultScreen(display); - window = XCreateSimpleWindow(display, - RootWindow(display, screen), - 10, 10, w->r.w, w->r.h, 1, - BlackPixel(display, screen), - WhitePixel(display, screen)); - drawable = XCreatePixmap(display, window, w->r.w, w->r.h, DefaultDepth(display, screen)); - XSelectInput(display, window, EVENTMASK); - XMapWindow(display, window); + dc_font(dc, FONTNAME); + // TODO: must be dc_window(dc, x, y, w, h, bg, fg) + window = dc_window(dc, 10, 10, w->r.w, w->r.h); } return swk_gi_fontsize(0); } @@ -54,7 +66,7 @@ swk_gi_init(SwkWindow *w) { int swk_gi_update(SwkWindow *w) { XWindowAttributes wa; - XGetWindowAttributes(display, window, &wa); + XGetWindowAttributes(dc->dpy, window, &wa); w->r.w = (wa.width / fs)-1; w->r.h = (wa.height / fs)-1; return 1; @@ -62,7 +74,7 @@ swk_gi_update(SwkWindow *w) { void swk_gi_exit() { - XCloseDisplay(display); + dc_free(dc); } SwkEvent * @@ -73,7 +85,7 @@ swk_gi_event(SwkWindow *w, int dowait) { XEvent event; SwkEvent *ret = &w->_e; - if(!XCheckMaskEvent(display, AnyEvent, &event)) + if(!XCheckMaskEvent(dc->dpy, -1, &event)) return NULL; switch(event.type) { case Expose: @@ -135,7 +147,20 @@ swk_gi_event(SwkWindow *w, int dowait) { ret->type = EKey; XLookupString(&event.xkey, NULL, 0, &ksym, NULL); printf("ksym=%d\n", (int)ksym); + switch(ksym) { + case 65362: + ret->data.key.keycode = KUp; + break; + case 65364: + ret->data.key.keycode = KDown; + break; + case 65361: + ret->data.key.keycode = KLeft; + break; + case 65363: + ret->data.key.keycode = KRight; + break; case XK_BackSpace: ret->data.key.keycode = 8; break; @@ -155,7 +180,7 @@ swk_gi_event(SwkWindow *w, int dowait) { fprintf(stderr, "event: key %d %d (%c)\n", ret->data.key.modmask, ret->data.key.keycode, ret->data.key.keycode); break; - case 0://SDL_QUIT: + case 0: ret->type = EQuit; break; default: @@ -167,16 +192,22 @@ swk_gi_event(SwkWindow *w, int dowait) { void swk_gi_clear() { - XClearWindow(display, window); + Rect r ={0}; + XWindowAttributes wa; + XGetWindowAttributes(dc->dpy, window, &wa); + dc_resize(dc, wa.width, wa.height); + r.w=wa.width; + r.h=wa.height; + swk_gi_fill(r, ColorBG, 0); } void swk_gi_flip() { #if 0 XWindowAttributes wa; - XGetWindowAttributes(display, window, &wa); - XCopyArea(display, drawable, window, gc, 0, 0, wa.width, wa.height, 0, 0); - XFlush(display); + XGetWindowAttributes(dc->dpy, window, &wa); + XCopyArea(dc->dpy, drawable, window, gc, 0, 0, wa.width, wa.height, 0, 0); + XFlush(dc->dpy); #endif } @@ -201,7 +232,8 @@ swk_gi_fill(Rect r, int color, int lil) { } if(!area.width) area.width = 1; if(!area.height) area.height = 1; - XFillRectangles(display, window, DefaultGC(display, screen), &area, 1); + XSetForeground(dc->dpy, dc->gc, col[color]); + XFillRectangles(dc->dpy, window, dc->gc, &area, 1); } void @@ -216,7 +248,8 @@ void swk_gi_text(Rect r, const char *text) { if(!text||!*text) return; - XDrawString(display, window, DefaultGC(display, screen), r.x*fs, ((1+r.y)*fs)-3, text, strlen (text)); + XSetForeground(dc->dpy, dc->gc, col[ColorFG]); + XDrawString(dc->dpy, window, dc->gc, r.x*fs, ((1+r.y)*fs)-3, text, strlen (text)); } void diff --git a/swk.c b/swk.c @@ -45,7 +45,7 @@ swk_update() { SwkBox *b = w->boxes[0]; swk_fit(w); swk_gi_clear(); - if (!w->colpos) { + if(!w->colpos) { b = w->boxes[1]; count--; col = w->r.w; @@ -53,7 +53,7 @@ swk_update() { for(w->r.w=col; ; b = w->boxes[1]) { swk_fit(w); roy = oy = 0; - if (b) + if(b) for(;b->cb; b++) { w->_e.box = b; if(IS_SCROLLBOX(b)) @@ -367,6 +367,8 @@ swk_entry(SwkEvent *e) { char *ptr; switch(e->type) { case EKey: + if (e->data.key.modmask&Ctrl) + return; key = e->data.key.keycode; if(key == 8) { ptr = (char*)malloc(strlen(e->box->text)+2); diff --git a/swk.h b/swk.h @@ -7,7 +7,7 @@ typedef enum { EVoid, EClick, EMotion, EKey, EExpose, EQuit, ELast } SwkEventType; typedef enum { Shift=1, Ctrl=2, Alt=4, Meta=8 } SwkKeyMod; -typedef enum { ColorFG, ColorBG, ColorHI, ColorLast } Palete; +typedef enum { ColorFG, ColorBG, ColorHI, ColorTF, ColorLast } Palete; typedef enum { KUp=0xe0, KDown=0xe1, KLeft=0xe2, KRight=0xe3 } SwkKeyCode; typedef struct SwkBox SwkBox; diff --git a/t/Makefile b/t/Makefile @@ -5,16 +5,16 @@ CFLAGS=-I.. -Wall all: test calc tlock ui test: test.o - ${CC} ${SWKLIBS} test.o -o test ../libswk.a + ${CC} test.o ../libswk.a ${SWKLIBS} -o test tlock: tlock.o - ${CC} ${SWKLIBS} tlock.o -o tlock ../libswk.a + ${CC} tlock.o ../libswk.a ${SWKLIBS} -o tlock calc: calc.o - ${CC} ${SWKLIBS} calc.o -o calc ../libswk.a + ${CC} calc.o ../libswk.a ${SWKLIBS} -o calc ui: ui.o - ${CC} ${SWKLIBS} ui.o -o ui ../libswk.a + ${CC} ui.o ../libswk.a ${SWKLIBS} -o ui clean: rm -f calc calc.o test test.o ui ui.o tlock tlock.o diff --git a/t/calc.c b/t/calc.c @@ -22,8 +22,8 @@ static void button(SwkEvent *e) { static char buffer2[sizeof(buffer)+32]; snprintf(buffer2, sizeof(buffer2), "echo '%s' | bc -q", buffer); pd = popen(buffer2, "r"); - fgets(buffer, sizeof(buffer),pd); - bufferi=strlen(buffer)-1; + fgets(buffer, sizeof(buffer), pd); + bufferi = strlen(buffer)-1; pclose(pd); } break; diff --git a/t/test.c b/t/test.c @@ -14,7 +14,7 @@ static void mybutton_about_ok(SwkEvent *e); static void mybutton(SwkEvent *e) { if(e->type == EClick) { - sprintf(text, "Do it again %d times\n", count); + sprintf(text, "Do it again %d times", count); helloworld[0].text = text; if(opt == NULL) printf("Option: none\n");