[hackers] [swk] handle up/down arrow keys || pancake

From: <hg_AT_suckless.org>
Date: Thu, 22 Apr 2010 13:07:45 +0000 (UTC)

changeset: 16:704afc9e5950
tag: tip
user: pancake <pancake_AT_nopcode.org>
date: Thu Apr 22 15:01:14 2010 +0200
files: gi_sdl.c swk.c swk.h test.c
description:
handle up/down arrow keys
implement and use swk_password widget

diff -r 80c5e5a3c2ce -r 704afc9e5950 gi_sdl.c
--- a/gi_sdl.c Wed Apr 21 23:08:01 2010 +0200
+++ b/gi_sdl.c Thu Apr 22 15:01:14 2010 +0200
@@ -126,20 +126,32 @@
                 fprintf(stderr, "event: click %d\n", event.button.button);
                 break;
         case SDL_KEYDOWN:
+ ret->data.key.modmask = 0;
+ ret->type = EKey;
+ if(event.key.keysym.mod & KMOD_CTRL)
+ ret->data.key.modmask |= Ctrl;
+ if(event.key.keysym.mod & KMOD_SHIFT)
+ ret->data.key.modmask |= Shift;
+ if(event.key.keysym.mod & KMOD_ALT)
+ ret->data.key.modmask |= Alt;
+ if(event.key.keysym.mod & KMOD_META)
+ ret->data.key.modmask |= Meta;
                 if(ret->data.key.keycode != 0 && event.key.keysym.unicode != 0) {
- ret->type = EKey;
                         ret->data.key.keycode = event.key.keysym.unicode;
- ret->data.key.modmask = 0;
- if(event.key.keysym.mod & KMOD_CTRL)
- ret->data.key.modmask |= Ctrl;
- if(event.key.keysym.mod & KMOD_SHIFT)
- ret->data.key.modmask |= Shift;
- if(event.key.keysym.mod & KMOD_ALT)
- ret->data.key.modmask |= Alt;
- if(event.key.keysym.mod & KMOD_META)
- ret->data.key.modmask |= Meta;
                         fprintf(stderr, "event: key %d %d\n",
                                 ret->data.key.modmask, ret->data.key.keycode);
+ } else {
+ switch(event.key.keysym.sym) {
+ case 273:
+ ret->data.key.keycode = KUp;
+ break;
+ case 274:
+ ret->data.key.keycode = KDown;
+ break;
+ default:
+ ret->type = -1;
+ break;
+ }
                 }
                 break;
         case SDL_QUIT:
diff -r 80c5e5a3c2ce -r 704afc9e5950 swk.c
--- a/swk.c Wed Apr 21 23:08:01 2010 +0200
+++ b/swk.c Thu Apr 22 15:01:14 2010 +0200
@@ -120,20 +120,28 @@
                                 break;
                         }
                 } else
- if(e->data.key.keycode == 9) { // TAB
+ switch(e->data.key.keycode) {
+ case KUp:
+ swk_focus_prev(e->win);
+ break;
+ case KDown:
+ swk_focus_next(e->win);
+ break;
+ case 9: // TAB
                         if(e->data.key.modmask)
                                 swk_focus_prev(e->win);
                         else swk_focus_next(e->win);
                         swk_update(e->win);
- } else
- if(e->data.key.keycode == 13) { // ENTER
+ break;
+ case 13: // ENTER
                         e->box = e->win->box;
                         e->type = EClick;
- } else
- if(e->data.key.keycode == 27) { // ESC
+ break;
+ case 27: // ESC
                         e->box = e->win->box;
                         e->type = EQuit;
                         swk_exit();
+ break;
                 }
                 // send key to focused box
                 e->box = e->win->box;
@@ -227,6 +235,33 @@
 }
 
 void
+swk_password(SwkEvent *e) {
+ int len;
+ Rect r;
+ char *str, *ptr;
+ switch(e->type) {
+ case EExpose:
+ r = e->box->r;
+ if(e->win->box == e->box)
+ swk_gi_line(r.x, r.y+1, r.w, 0, ColorHI);
+ len = strlen(e->box->text);
+ if (len>0) {
+ ptr = str = malloc(len+1);
+ for(;len--;ptr++)
+ *ptr='*';
+ *ptr='\0';
+ swk_gi_text(r, str);
+ free(str);
+ }
+ break;
+ case EClick:
+ printf("password: %s\n", e->box->text);
+ default:
+ swk_entry(e);
+ }
+}
+
+void
 swk_entry(SwkEvent *e) {
         int len, key;
         char *ptr;
diff -r 80c5e5a3c2ce -r 704afc9e5950 swk.h
--- a/swk.h Wed Apr 21 23:08:01 2010 +0200
+++ b/swk.h Thu Apr 22 15:01:14 2010 +0200
@@ -6,6 +6,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 { KUp=0xe0, KDown=0xe1, KLeft=0xe2, KRight=0xe3 } SwkKeyCode;
 
 typedef struct SwkBox SwkBox;
 typedef struct SwkWindow SwkWindow;
@@ -80,6 +81,7 @@
 void swk_button(SwkEvent *e);
 void swk_label(SwkEvent *e);
 void swk_entry(SwkEvent *e);
+void swk_password(SwkEvent *e);
 void swk_filler(SwkEvent *e);
 
 /* graphic backend */
diff -r 80c5e5a3c2ce -r 704afc9e5950 test.c
--- a/test.c Wed Apr 21 23:08:01 2010 +0200
+++ b/test.c Thu Apr 22 15:01:14 2010 +0200
@@ -23,7 +23,7 @@
         { .cb=swk_filler, },
         SWK_BOX_NEWLINE(1),
         { .cb=swk_label, .text="Password:", },
- { .cb=swk_entry, .text="****", },
+ { .cb=swk_password, .text="1234", },
         { .cb=swk_filler, },
         SWK_BOX_NEWLINE(2),
         { .cb=mybutton, .text="yes" },
Received on Thu Apr 22 2010 - 13:07:45 UTC

This archive was generated by hypermail 2.2.0 : Thu Apr 22 2010 - 13:12:03 UTC