changeset: 60:d96a4c3afebb
tag: tip
user: pancake <pancake_AT_nopcode.org>
date: Sun Aug 22 07:54:34 2010 +0200
files: Makefile TODO config.def.h gi_x11.c swk.c swk.h t/Makefile t/calc.c t/test.c
description:
honor config.h colors and fix window boundaries in gi_x11
use libdraw as dependency for gi_x11
and other minor cosmetic changes
diff -r d3af7c8893d1 -r d96a4c3afebb Makefile
--- a/Makefile Thu Aug 19 11:18:02 2010 +0100
+++ b/Makefile Sun Aug 22 07:54:34 2010 +0200
@@ -9,7 +9,7 @@
GI_LIBS=-lSDL -lSDL_ttf -lSDL_image
else
ifeq (${GI},x11)
-GI_LIBS=-lX11
+GI_LIBS=-lX11 -ldraw
endif
endif
diff -r d3af7c8893d1 -r d96a4c3afebb TODO
--- a/TODO Thu Aug 19 11:18:02 2010 +0100
+++ b/TODO Sun Aug 22 07:54:34 2010 +0200
@@ -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 -r d3af7c8893d1 -r d96a4c3afebb config.def.h
--- a/config.def.h Thu Aug 19 11:18:02 2010 +0100
+++ b/config.def.h Sun Aug 22 07:54:34 2010 +0200
@@ -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 @@
{ 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 -r d3af7c8893d1 -r d96a4c3afebb gi_x11.c
--- a/gi_x11.c Thu Aug 19 11:18:02 2010 +0100
+++ b/gi_x11.c Sun Aug 22 07:54:34 2010 +0200
@@ -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 @@
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 @@
void
swk_gi_exit() {
- XCloseDisplay(display);
+ dc_free(dc);
}
SwkEvent *
@@ -73,7 +85,7 @@
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 @@
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 @@
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 @@
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 @@
}
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 @@
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 -r d3af7c8893d1 -r d96a4c3afebb swk.c
--- a/swk.c Thu Aug 19 11:18:02 2010 +0100
+++ b/swk.c Sun Aug 22 07:54:34 2010 +0200
@@ -45,7 +45,7 @@
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 @@
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 @@
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 -r d3af7c8893d1 -r d96a4c3afebb swk.h
--- a/swk.h Thu Aug 19 11:18:02 2010 +0100
+++ b/swk.h Sun Aug 22 07:54:34 2010 +0200
@@ -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 -r d3af7c8893d1 -r d96a4c3afebb t/Makefile
--- a/t/Makefile Thu Aug 19 11:18:02 2010 +0100
+++ b/t/Makefile Sun Aug 22 07:54:34 2010 +0200
@@ -5,16 +5,16 @@
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 -r d3af7c8893d1 -r d96a4c3afebb t/calc.c
--- a/t/calc.c Thu Aug 19 11:18:02 2010 +0100
+++ b/t/calc.c Sun Aug 22 07:54:34 2010 +0200
@@ -22,8 +22,8 @@
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 -r d3af7c8893d1 -r d96a4c3afebb t/test.c
--- a/t/test.c Thu Aug 19 11:18:02 2010 +0100
+++ b/t/test.c Sun Aug 22 07:54:34 2010 +0200
@@ -14,7 +14,7 @@
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");
Received on Sun Aug 22 2010 - 07:58:15 CEST
This archive was generated by hypermail 2.2.0 : Sun Aug 22 2010 - 08:00:07 CEST