[hackers] [swk] protect rendering function with a singleton || pancake

From: <hg_AT_suckless.org>
Date: Wed, 9 Jun 2010 21:08:48 +0000 (UTC)

changeset: 45:a79818ebdde3
tag: tip
user: pancake <pancake_AT_nopcode.org>
date: Wed Jun 09 23:03:19 2010 +0200
files: swk.c t/Makefile t/tlock.c
description:
protect rendering function with a singleton
do not pass any argument to swk_update()
add 'tlock' example program with clock

diff -r f76c0a387780 -r a79818ebdde3 swk.c
--- a/swk.c Sat May 15 13:24:31 2010 +0200
+++ b/swk.c Wed Jun 09 23:03:19 2010 +0200
@@ -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 @@
         if(!running && !swk_gi_init(w))
                 return 0;
         running = 1;
- swk_update(w);
+ swk_update();
         return 1;
 }
 
@@ -28,6 +29,9 @@
 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 @@
                         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 @@
                                 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_update(w);
 }
 
-
 static void
 setscrollbox(int delta) {
         SwkBox *r = NULL;
@@ -107,7 +111,8 @@
         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 @@
                 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_update(e->win);
+ swk_update();
                 break;
         case EExpose:
- swk_update(e->win);
+ swk_update();
                 break;
         case EQuit:
                 swk_gi_exit();
diff -r f76c0a387780 -r a79818ebdde3 t/Makefile
--- a/t/Makefile Sat May 15 13:24:31 2010 +0200
+++ b/t/Makefile Wed Jun 09 23:03:19 2010 +0200
@@ -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 -r f76c0a387780 -r a79818ebdde3 t/tlock.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/t/tlock.c Wed Jun 09 23:03:19 2010 +0200
@@ -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, &lt);
+ 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;
+}
Received on Wed Jun 09 2010 - 21:08:48 UTC

This archive was generated by hypermail 2.2.0 : Wed Jun 09 2010 - 21:12:03 UTC