commit 523ced42462268d32bebebb07410ca8cd57a8e3a
parent 8095ebd990e8c44177d55a1ddf0f14abfb66a432
Author: pancake <pancake@nopcode.org>
Date: Wed, 9 Jun 2010 23:03:19 +0200
protect rendering function with a singleton
do not pass any argument to swk_update()
add 'tlock' example program with clock
Diffstat:
swk.c | | | 27 | ++++++++++++++++----------- |
t/Makefile | | | 9 | ++++++--- |
t/tlock.c | | | 74 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 96 insertions(+), 14 deletions(-)
diff --git a/swk.c b/swk.c
@@ -7,6 +7,7 @@
static SwkWindow *w = NULL;
static int running = 0;
+static __thread int rendering = 0;
int
swk_use(SwkWindow *win) {
@@ -20,7 +21,7 @@ swk_use(SwkWindow *win) {
if(!running && !swk_gi_init(w))
return 0;
running = 1;
- swk_update(w);
+ swk_update();
return 1;
}
@@ -28,6 +29,9 @@ void
swk_update() {
char text[8];
int roy, oy, scroll = 0;
+ if(rendering)
+ return;
+ rendering = 1;
w->_e.type = EExpose;
if(swk_gi_update(w)) {
SwkBox *b = w->boxes;
@@ -38,7 +42,7 @@ swk_update() {
w->_e.box = b;
if(b->r.w==-1 && b->r.h==-1 && ((int)(size_t)b->data)<0)
roy = oy+1;
- if (b->scroll)
+ if(b->scroll)
scroll = b->scroll;
if(roy && b->r.y < roy) {
sprintf(text, "(%d)", scroll);
@@ -47,16 +51,17 @@ swk_update() {
r.y = roy;
r.w = 3;
swk_gi_text(r, text);
- r.x--;
- swk_gi_line(r.x, roy, r.w, 0, ColorHI);
+ swk_gi_line(--r.x, roy, r.w, 0, ColorHI);
}
else b->cb(&w->_e);
oy = b->r.y;
}
swk_gi_flip();
- } else running = 0;
+ }
+ rendering = 0;
}
+// TODO: enqueue events here instead of use a global variable?
void
swk_exit() {
running = 0;
@@ -83,7 +88,6 @@ swk_fontsize_decrease() {
swk_update(w);
}
-
static void
setscrollbox(int delta) {
SwkBox *r = NULL;
@@ -107,7 +111,8 @@ swk_scroll_down() {
setscrollbox(-2);
}
-static void swk_fit_row(SwkBox *a, SwkBox *b, int y) {
+static void
+swk_fit_row(SwkBox *a, SwkBox *b, int y) {
SwkBox *btmp;
int count = 0, x = 0;
for(btmp=a; btmp<b; btmp++)
@@ -199,14 +204,14 @@ swk_handle_event(SwkEvent *e) {
e->box = e->win->box;
if(e->win->box)
e->win->box->cb(e);
- swk_update(e->win);
+ swk_update();
break;
case EMotion:
for(b=e->win->boxes; b->cb; b++) {
if(SWK_HIT(b->r, e->data.motion)) {
e->win->box = e->box = b;
b->cb(e);
- swk_update(e->win);
+ swk_update();
break;
}
}
@@ -228,10 +233,10 @@ swk_handle_event(SwkEvent *e) {
}
}
}
- swk_update(e->win);
+ swk_update();
break;
case EExpose:
- swk_update(e->win);
+ swk_update();
break;
case EQuit:
swk_gi_exit();
diff --git a/t/Makefile b/t/Makefile
@@ -1,14 +1,17 @@
include ../swk.mk
-CFLAGS=-I..
+CFLAGS=-I.. -Wall
-all: test ui
+all: test tlock ui
test: test.o
${CC} ${SWKLIBS} test.o -o test ../libswk.a
+tlock: tlock.o
+ ${CC} ${SWKLIBS} tlock.o -o tlock ../libswk.a
+
ui: ui.o
${CC} ${SWKLIBS} ui.o -o ui ../libswk.a
clean:
- rm -f test test.o ui ui.o
+ rm -f test test.o ui ui.o tlock tlock.o
diff --git a/t/tlock.c b/t/tlock.c
@@ -0,0 +1,74 @@
+#include <stdio.h>
+#include <signal.h>
+#include <unistd.h>
+#include <time.h>
+#include <swk.h>
+
+// TODO: disable alarm if dpms off
+// TODO: enable alarm when dpms on
+
+static SwkBox contents[];
+#define COUNT 0
+static int count = COUNT;
+
+static char timestring[80];
+static void settimestring() {
+ struct tm lt;
+ time_t t = time(0);
+ localtime_r(&t, <);
+ snprintf(timestring, sizeof(timestring),
+ "%04d/%02d/%02d %02d:%02d:%02d",
+ 1900+lt.tm_year, lt.tm_mon+1, lt.tm_mday,
+ lt.tm_hour, lt.tm_min, lt.tm_sec);
+}
+
+static void timepoll() {
+ settimestring();
+ swk_update();
+ if(count--<0) {
+ contents[2].scroll = 0;
+ count = COUNT;
+ }
+ alarm(1);
+}
+
+static void mylocklabel(SwkEvent *e) {
+ if(e->type == EMotion) {
+ count = 5;
+ }
+ if(e->type == EExpose) {
+ int pos = e->box->r.y;
+ if(pos<3 || pos>e->win->r.h) {
+ printf("swkexit\n");
+ swk_exit();
+ }
+ }
+ swk_label(e);
+}
+
+static SwkBox contents[] = {
+ { .cb=swk_label, .text=timestring },
+ { .cb=swk_separator },
+ SWK_BOX_NEWLINE(-1),
+ { .cb=mylocklabel, .text=" slide out to unlock", },
+ { .cb=NULL }
+};
+
+static void init_alarm() {
+ signal(SIGALRM, timepoll);
+ alarm(1);
+ settimestring();
+}
+
+int main() {
+ SwkWindow w = {
+ .title="touch lock",
+ .boxes=contents,
+ .box=contents,
+ };
+ if(!swk_use(&w))
+ return 1;
+ init_alarm();
+ swk_loop();
+ return 0;
+}