swk

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

tlock.c (1519B)


      1 #include <stdio.h>
      2 #include <signal.h>
      3 #include <unistd.h>
      4 #include <time.h>
      5 #include <swk.h>
      6 
      7 // TODO: disable alarm if dpms off
      8 // TODO: enable alarm when dpms on
      9 
     10 static SwkBox contents[];
     11 static int count = 0;
     12 static char timestring[80];
     13 
     14 static void settimestring() {
     15 	struct tm lt;
     16 	time_t t = time(0);
     17 	localtime_r(&t, &lt);
     18 	snprintf(timestring, sizeof(timestring),
     19 		"  %04d/%02d/%02d    %02d:%02d:%02d",
     20 		1900+lt.tm_year, lt.tm_mon+1, lt.tm_mday, 
     21 		lt.tm_hour, lt.tm_min, lt.tm_sec);
     22 }
     23 
     24 static void timepoll() {
     25 	settimestring();
     26 	swk_update();
     27 	if(count--<0)
     28 		count = contents[2].scroll = 0;
     29 	alarm(1);
     30 }
     31 
     32 static void mylocklabel(SwkEvent *e) {
     33 	if(e->type == EMotion) {
     34 		count = 3;
     35 	} else
     36 	if(e->type == EExpose) {
     37 		int pos = e->box->r.y;
     38 		if(pos<3 || pos>e->win->r.h) {
     39 			printf("swkexit coz pos = %d\n", pos);
     40 			swk_exit();
     41 		}
     42 	}
     43 	swk_label(e);
     44 }
     45 
     46 static SwkBox contents[] = {
     47 	{ .cb=swk_label, .text=timestring },
     48 	{ .cb=swk_separator },
     49 	SWK_BOX_NEWLINE(-1),
     50 	SWK_BOX_NEWLINE(6), // Trick to avoid unexpected swkexit
     51 	{ .cb=mylocklabel, .text="      slide out to unlock", },
     52 	{ .cb=NULL }
     53 };
     54 
     55 static void init_alarm() {
     56 	signal(SIGALRM, timepoll);
     57 	alarm(1);
     58 	settimestring();
     59 }
     60 
     61 int main() {
     62 	SwkWindow w = {
     63 		.title="touch lock",
     64 		.boxes={contents,NULL},
     65 		.box=contents,
     66 		.r={200, 100, 800, 500} // x,y, w,h
     67 	};
     68 	if(!swk_use(&w))
     69 		return 1;
     70 	init_alarm();
     71 	swk_fontsize_increase();
     72 	swk_fontsize_increase();
     73 	swk_fontsize_increase();
     74 	swk_fontsize_increase();
     75 	swk_loop();
     76 	return 0;
     77 }