aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorConnor Lane Smith <cls@lubutu.com>2011-07-14 20:03:08 +0100
committerConnor Lane Smith <cls@lubutu.com>2011-07-14 20:03:08 +0100
commit2b92c95230fbbdcf47f701d9cc4fd133a7972fae (patch)
tree565a5c34b05621c85ed58f6b1d7210973a4eec03
parent16a0c0d52aa947b3fe8c9285986567189537d339 (diff)
efficiency tweaks
-rw-r--r--dmenu.c98
-rw-r--r--draw.c6
2 files changed, 44 insertions, 60 deletions
diff --git a/dmenu.c b/dmenu.c
index aa9254f..cff3e65 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -25,8 +25,8 @@ struct Item {
25 25
26static void appenditem(Item *item, Item **list, Item **last); 26static void appenditem(Item *item, Item **list, Item **last);
27static void calcoffsets(void); 27static void calcoffsets(void);
28static char *cistrstr(const char *s, const char *sub);
28static void drawmenu(void); 29static void drawmenu(void);
29static char *fstrstr(const char *s, const char *sub);
30static void grabkeyboard(void); 30static void grabkeyboard(void);
31static void insert(const char *str, ssize_t n); 31static void insert(const char *str, ssize_t n);
32static void keypress(XKeyEvent *ev); 32static void keypress(XKeyEvent *ev);
@@ -60,6 +60,7 @@ static Item *prev, *curr, *next, *sel;
60static Window win; 60static Window win;
61 61
62static int (*fstrncmp)(const char *, const char *, size_t) = strncmp; 62static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
63static char *(*fstrstr)(const char *, const char *) = strstr;
63 64
64int 65int
65main(int argc, char *argv[]) { 66main(int argc, char *argv[]) {
@@ -76,8 +77,10 @@ main(int argc, char *argv[]) {
76 topbar = False; 77 topbar = False;
77 else if(!strcmp(argv[i], "-f")) 78 else if(!strcmp(argv[i], "-f"))
78 fast = True; 79 fast = True;
79 else if(!strcmp(argv[i], "-i")) 80 else if(!strcmp(argv[i], "-i")) {
80 fstrncmp = strncasecmp; 81 fstrncmp = strncasecmp;
82 fstrstr = cistrstr;
83 }
81 else if(i+1 == argc) 84 else if(i+1 == argc)
82 usage(); 85 usage();
83 /* double flags */ 86 /* double flags */
@@ -112,7 +115,7 @@ main(int argc, char *argv[]) {
112 setup(); 115 setup();
113 run(); 116 run();
114 117
115 return EXIT_FAILURE; /* should not reach */ 118 return EXIT_FAILURE; /* unreachable */
116} 119}
117 120
118void 121void
@@ -121,6 +124,7 @@ appenditem(Item *item, Item **list, Item **last) {
121 *list = item; 124 *list = item;
122 else 125 else
123 (*last)->right = item; 126 (*last)->right = item;
127
124 item->left = *last; 128 item->left = *last;
125 item->right = NULL; 129 item->right = NULL;
126 *last = item; 130 *last = item;
@@ -143,6 +147,16 @@ calcoffsets(void) {
143 break; 147 break;
144} 148}
145 149
150char *
151cistrstr(const char *s, const char *sub) {
152 size_t len;
153
154 for(len = strlen(sub); *s; s++)
155 if(!strncasecmp(s, sub, len))
156 return (char *)s;
157 return NULL;
158}
159
146void 160void
147drawmenu(void) { 161drawmenu(void) {
148 int curpos; 162 int curpos;
@@ -188,16 +202,6 @@ drawmenu(void) {
188 mapdc(dc, win, mw, mh); 202 mapdc(dc, win, mw, mh);
189} 203}
190 204
191char *
192fstrstr(const char *s, const char *sub) {
193 size_t len;
194
195 for(len = strlen(sub); *s; s++)
196 if(!fstrncmp(s, sub, len))
197 return (char *)s;
198 return NULL;
199}
200
201void 205void
202grabkeyboard(void) { 206grabkeyboard(void) {
203 int i; 207 int i;
@@ -233,58 +237,37 @@ keypress(XKeyEvent *ev) {
233 237
234 XConvertCase(ksym, &lower, &upper); 238 XConvertCase(ksym, &lower, &upper);
235 switch(lower) { 239 switch(lower) {
236 default: 240 case XK_a: ksym = XK_Home; break;
237 return; 241 case XK_b: ksym = XK_Left; break;
238 case XK_a: 242 case XK_c: ksym = XK_Escape; break;
239 ksym = XK_Home; 243 case XK_d: ksym = XK_Delete; break;
240 break; 244 case XK_e: ksym = XK_End; break;
241 case XK_b: 245 case XK_f: ksym = XK_Right; break;
242 ksym = XK_Left; 246 case XK_h: ksym = XK_BackSpace; break;
243 break; 247 case XK_i: ksym = XK_Tab; break;
244 case XK_c: 248 case XK_j: ksym = XK_Return; break;
245 ksym = XK_Escape; 249 case XK_m: ksym = XK_Return; break;
246 break; 250 case XK_n: ksym = XK_Up; break;
247 case XK_d: 251 case XK_p: ksym = XK_Down; break;
248 ksym = XK_Delete; 252
249 break; 253 case XK_k: /* delete right */
250 case XK_e:
251 ksym = XK_End;
252 break;
253 case XK_f:
254 ksym = XK_Right;
255 break;
256 case XK_h:
257 ksym = XK_BackSpace;
258 break;
259 case XK_i:
260 ksym = XK_Tab;
261 break;
262 case XK_j:
263 case XK_m:
264 ksym = XK_Return;
265 break;
266 case XK_k: /* delete right */
267 text[cursor] = '\0'; 254 text[cursor] = '\0';
268 match(False); 255 match(False);
269 break; 256 break;
270 case XK_n: 257 case XK_u: /* delete left */
271 ksym = XK_Next;
272 break;
273 case XK_p:
274 ksym = XK_Prior;
275 break;
276 case XK_u: /* delete left */
277 insert(NULL, 0 - cursor); 258 insert(NULL, 0 - cursor);
278 break; 259 break;
279 case XK_w: /* delete word */ 260 case XK_w: /* delete word */
280 while(cursor > 0 && text[nextrune(-1)] == ' ') 261 while(cursor > 0 && text[nextrune(-1)] == ' ')
281 insert(NULL, nextrune(-1) - cursor); 262 insert(NULL, nextrune(-1) - cursor);
282 while(cursor > 0 && text[nextrune(-1)] != ' ') 263 while(cursor > 0 && text[nextrune(-1)] != ' ')
283 insert(NULL, nextrune(-1) - cursor); 264 insert(NULL, nextrune(-1) - cursor);
284 break; 265 break;
285 case XK_y: /* paste selection */ 266 case XK_y: /* paste selection */
286 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime); 267 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
287 return; 268 return;
269 default:
270 return;
288 } 271 }
289 } 272 }
290 switch(ksym) { 273 switch(ksym) {
@@ -297,8 +280,9 @@ keypress(XKeyEvent *ev) {
297 return; 280 return;
298 cursor = nextrune(+1); 281 cursor = nextrune(+1);
299 case XK_BackSpace: 282 case XK_BackSpace:
300 if(cursor > 0) 283 if(cursor == 0)
301 insert(NULL, nextrune(-1) - cursor); 284 return;
285 insert(NULL, nextrune(-1) - cursor);
302 break; 286 break;
303 case XK_End: 287 case XK_End:
304 if(text[cursor] != '\0') { 288 if(text[cursor] != '\0') {
@@ -351,7 +335,7 @@ keypress(XKeyEvent *ev) {
351 break; 335 break;
352 case XK_Return: 336 case XK_Return:
353 case XK_KP_Enter: 337 case XK_KP_Enter:
354 fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout); 338 puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
355 exit(EXIT_SUCCESS); 339 exit(EXIT_SUCCESS);
356 case XK_Right: 340 case XK_Right:
357 if(text[cursor] != '\0') { 341 if(text[cursor] != '\0') {
@@ -468,7 +452,7 @@ run(void) {
468 switch(ev.type) { 452 switch(ev.type) {
469 case Expose: 453 case Expose:
470 if(ev.xexpose.count == 0) 454 if(ev.xexpose.count == 0)
471 drawmenu(); 455 mapdc(dc, win, mw, mh);
472 break; 456 break;
473 case KeyPress: 457 case KeyPress:
474 keypress(&ev.xkey); 458 keypress(&ev.xkey);
diff --git a/draw.c b/draw.c
index 2028fd9..d0beee8 100644
--- a/draw.c
+++ b/draw.c
@@ -96,7 +96,7 @@ initdc(void) {
96 DC *dc; 96 DC *dc;
97 97
98 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) 98 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
99 fprintf(stderr, "no locale support\n"); 99 fputs("no locale support\n", stderr);
100 if(!(dc = calloc(1, sizeof *dc))) 100 if(!(dc = calloc(1, sizeof *dc)))
101 eprintf("cannot malloc %u bytes:", sizeof *dc); 101 eprintf("cannot malloc %u bytes:", sizeof *dc);
102 if(!(dc->dpy = XOpenDisplay(NULL))) 102 if(!(dc->dpy = XOpenDisplay(NULL)))
@@ -153,10 +153,10 @@ resizedc(DC *dc, unsigned int w, unsigned int h) {
153 if(dc->canvas) 153 if(dc->canvas)
154 XFreePixmap(dc->dpy, dc->canvas); 154 XFreePixmap(dc->dpy, dc->canvas);
155 155
156 dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h,
157 DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
158 dc->w = w; 156 dc->w = w;
159 dc->h = h; 157 dc->h = h;
158 dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h,
159 DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
160} 160}
161 161
162int 162int