forked from nyyManni/dmenu-wayland
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmenu.c
646 lines (540 loc) · 17.2 KB
/
dmenu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/* See LICENSE file for copyright and license details. */
#include <bits/stdint-intn.h>
#include <ctype.h>
#include <stdbool.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <wayland-client.h>
#include <xkbcommon/xkbcommon.h>
#include "draw.h"
#define INRECT(x,y,rx,ry,rw,rh) ((x) >= (rx) && (x) < (rx)+(rw) && (y) >= (ry) && (y) < (ry)+(rh))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
typedef struct Item Item;
struct Item {
char *text;
Item *next; /* traverses all items */
Item *left, *right; /* traverses matching items */
int32_t width;
};
typedef enum {
LEFT,
RIGHT,
CENTRE
} TextPosition;
struct {
int32_t width;
int32_t height;
int32_t text_height;
int32_t text_y;
int32_t input_field;
int32_t scroll_left;
int32_t matches;
int32_t scroll_right;
} window_config;
const char *progname;
static uint32_t color_bg = 0x222222ff;
static uint32_t color_fg = 0xbbbbbbff;
static uint32_t color_input_bg = 0x222222ff;
static uint32_t color_input_fg = 0xbbbbbbff;
static uint32_t color_prompt_bg = 0x005577ff;
static uint32_t color_prompt_fg = 0xeeeeeeff;
static uint32_t color_selected_bg = 0x005577ff;
static uint32_t color_selected_fg = 0xeeeeeeff;
static int32_t line_height = 20;
static void appenditem(Item *item, Item **list, Item **last);
static char *fstrstr(const char *s, const char *sub);
static void insert(const char *s, ssize_t n, struct dmenu_panel* panel);
static void match(struct dmenu_panel* panel);
static size_t nextrune(int incr);
static void readstdin(void);
static void alarmhandler(int signum);
static void handle_return(char* value, struct dmenu_panel* panel);
static void usage(void);
static int retcode = EXIT_SUCCESS;
static int selected_monitor = 0;
static char *selected_monitor_name = 0;
static char text[BUFSIZ];
static char text_[BUFSIZ];
static int itemcount = 0;
static int lines = 0;
static int timeout = 3;
static size_t cursor = 0;
static const char *prompt = NULL;
static bool message = false;
static bool nostdin = false;
static bool returnearly = false;
static bool show_in_bottom = false;
static bool password = false;
static TextPosition messageposition = LEFT;
static Item *items = NULL;
static Item *matches, *sel;
static Item *prev, *curr, *next;
static Item *leftmost, *rightmost;
static char *font = "Mono";
static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
void insert(const char *s, ssize_t n, struct dmenu_panel* panel) {
if(strlen(text) + n > sizeof text - 1) {
return;
}
memmove(text + cursor + n, text + cursor, sizeof text - cursor - MAX(n, 0));
if(n > 0) {
memcpy(text + cursor, s, n);
}
cursor += n;
match(panel);
}
void keyrepeat(struct dmenu_panel *panel) {
if (panel->on_keyevent) {
panel->on_keyevent(panel, panel->repeat_key_state, panel->repeat_sym,
panel->keyboard.control, panel->keyboard.shift);
}
}
void keypress(struct dmenu_panel *panel, enum wl_keyboard_key_state state,
xkb_keysym_t sym, bool ctrl, bool shft) {
char buf[8];
size_t len = strlen(text);
if (state != WL_KEYBOARD_KEY_STATE_PRESSED) return;
if (ctrl) {
switch (xkb_keysym_to_lower(sym)) {
case XKB_KEY_a:
sym = XKB_KEY_Home;
break;
case XKB_KEY_e:
sym = XKB_KEY_End;
break;
case XKB_KEY_f:
case XKB_KEY_n:
sym = XKB_KEY_Right;
break;
case XKB_KEY_b:
case XKB_KEY_p:
sym = XKB_KEY_Left;
break;
case XKB_KEY_h:
sym = XKB_KEY_BackSpace;
break;
case XKB_KEY_j:
sym = XKB_KEY_Return;
break;
case XKB_KEY_g:
case XKB_KEY_c:
retcode = EXIT_FAILURE;
dmenu_close(panel);
return;
}
}
switch (sym) {
case XKB_KEY_KP_Enter: /* fallthrough */
case XKB_KEY_Return:
handle_return((sel && !shft) ? sel->text : text, panel);
break;
case XKB_KEY_Escape:
retcode = EXIT_FAILURE;
dmenu_close(panel);
break;
case XKB_KEY_Left:
case XKB_KEY_Up:
if(cursor && (!sel || !sel->left)) {
cursor = nextrune(-1);
} if (sel && sel->left) {
sel = sel->left;
}
break;
case XKB_KEY_Right:
case XKB_KEY_Down:
if (cursor < len) {
cursor = nextrune(+1);
} else if (cursor == len) {
if (sel && sel->right) sel = sel->right;
}
break;
case XKB_KEY_End:
if(cursor < len) {
cursor = len;
break;
}
while(sel && sel->right)
sel = sel->right;
break;
case XKB_KEY_Home:
if(sel == matches) {
cursor = 0;
break;
}
sel = curr = matches;
/* calcoffsets(); */
break;
case XKB_KEY_BackSpace:
if (cursor > 0)
insert(NULL, nextrune(-1) - cursor, panel);
break;
case XKB_KEY_Delete:
if (cursor == len)
return;
cursor = nextrune(+1);
break;
case XKB_KEY_Tab:
if(!sel) return;
strncpy(text, sel->text, sizeof text);
cursor = strlen(text);
match(panel);
break;
default:
if (xkb_keysym_to_utf8(sym, buf, 8)) {
insert(buf, strnlen(buf, 8), panel);
}
}
dmenu_draw(panel);
}
void cairo_set_source_u32(cairo_t *cairo, uint32_t color) {
cairo_set_source_rgba(cairo,
(color >> (3*8) & 0xFF) / 255.0,
(color >> (2*8) & 0xFF) / 255.0,
(color >> (1*8) & 0xFF) / 255.0,
(color >> (0*8) & 0xFF) / 255.0);
}
void draw_text(cairo_t *cairo, int32_t width, int32_t height, const char *str,
int32_t *x, int32_t *y, int32_t *new_x, int32_t *new_y,
int32_t scale, uint32_t
foreground_color, uint32_t background_color, int32_t padding) {
int32_t text_width, text_height;
get_text_size(cairo, font, &text_width, &text_height,
NULL, scale, false, str);
int32_t text_y = (height * scale - text_height) / 2.0 + *y;
if (*x + padding * scale + text_width + 30 * scale > width) {
cairo_move_to(cairo, width, text_y);
pango_printf(cairo, font, scale, false, ">");
} else {
if (background_color) {
cairo_set_source_u32(cairo, background_color);
cairo_rectangle(cairo, *x, *y, (lines ? width : text_width + 2 * padding) * scale, height * scale);
cairo_fill(cairo);
}
cairo_move_to(cairo, *x + padding * scale, text_y);
cairo_set_source_u32(cairo, foreground_color);
pango_printf(cairo, font, scale, false, str);
}
*new_x += text_width + 2 * padding * scale;
*new_y += height * scale;
}
void draw(cairo_t *cairo, int32_t width, int32_t height, int32_t scale) {
int32_t x = 0;
int32_t y = 0;
int32_t bin;
int32_t item_padding = 10;
int32_t text_width, text_height;
get_text_size(cairo, font, &text_width, &text_height, NULL, scale,
false, "Aj");
int32_t text_y = (line_height * scale - text_height) / 2.0;
cairo_set_source_u32(cairo, color_bg);
cairo_paint(cairo);
if (prompt) {
draw_text(cairo, width, line_height, prompt, &x, &y,
&x, &bin, scale, color_prompt_fg, color_prompt_bg, 6);
window_config.input_field = x;
} else {
window_config.input_field = 0;
}
cairo_set_source_u32(cairo, color_input_bg);
cairo_rectangle(cairo, window_config.input_field, 0, (lines ? width : 300) * scale, line_height * scale);
cairo_fill(cairo);
memset(text_, 0, BUFSIZ);
if (password) {
memset(text_, '*', strlen(text));
} else {
strncpy(text_, text, cursor);
}
// draw input
// depending on orientation add heigth of input to y
draw_text(cairo, width, line_height, text_, &x, &y, &bin,
(lines ? &y : &bin), scale, color_input_fg, 0, 6);
{
/* draw cursor */
int32_t text_width, text_height;
get_text_size(cairo, font, &text_width, &text_height, NULL, scale,
false, text_);
int32_t padding = 6 * scale;
cairo_rectangle(cairo, x + padding + text_width, text_y,
scale, text_height);
cairo_fill(cairo);
}
if (!lines) {
x += 320 * scale;
}
/* Scroll indicator will be drawn later if required. */
int32_t scroll_indicator_pos = x;
if (!matches) return;
Item *item;
for (item = matches; item; item = item->right) {
uint32_t bg_color = sel == item ? color_selected_bg : color_bg;
uint32_t fg_color = sel == item ? color_selected_fg : color_fg;
if (x >= width || y >= height) break;
if (!lines) {
draw_text(cairo, width - 20 * scale, line_height, item->text,
&x, &y, &x, &bin, scale, fg_color, bg_color, item_padding);
} else {
draw_text(cairo, width * scale, line_height, item->text,
&x, &y, &bin, &y, scale, fg_color, bg_color, item_padding);
}
}
if (leftmost != matches) {
cairo_move_to(cairo, scroll_indicator_pos, text_y);
pango_printf(cairo, font, scale, false, "<");
}
}
uint32_t parse_color(char *str) {
if (!str) eprintf("NULL as color value\n");
size_t len = strnlen(str, BUFSIZ);
if ((len != 7 && len != 9) || str[0] != '#') {
eprintf("Color format must be '#rrggbb[aa]'\n");
}
uint32_t _val = strtol(&str[1], NULL, 16);
uint32_t color = 0x000000ff;
/* Alpha specified
* Otherwise, assume full opacity */
if (len == 9) {
color = _val;
} else {
color = (_val << 8) + 0xff;
}
return color;
}
int main(int argc, char **argv) {
int i;
progname = "dmenu";
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-v") || !strcmp(argv[1], "--version")) {
fputs("dmenu-wl-" VERSION
", © 2006-2018 dmenu engineers, see LICENSE for details\n",
stdout);
exit(EXIT_SUCCESS);
} else if (!strcmp(argv[i], "-b") || !strcmp(argv[i], "--bottom"))
show_in_bottom = true;
else if (!strcmp(argv[i], "-e") || !strcmp(argv[i], "--echo"))
message = true;
else if (!strcmp(argv[i], "-ec") || !strcmp(argv[i], "--echo-centre"))
message = true, messageposition = CENTRE;
else if (!strcmp(argv[i], "-er") || !strcmp(argv[i], "--echo-right"))
message = true, messageposition = RIGHT;
else if (!strcmp(argv[i], "-i") || !strcmp(argv[i], "--insensitive"))
fstrncmp = strncasecmp;
else if (!strcmp(argv[i], "-r") || !strcmp(argv[i], "--return-early"))
returnearly = true;
else if (!strcmp(argv[i], "-P"))
password = true;
else if (i == argc - 1) {
usage();
}
/* opts that need 1 arg */
else if (!strcmp(argv[i], "-et") || !strcmp(argv[i], "--echo-timeout"))
timeout = atoi(argv[++i]);
else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--height"))
line_height = atoi(argv[++i]);
else if (!strcmp(argv[i], "-l") || !strcmp(argv[i], "--lines"))
lines = atoi(argv[++i]);
else if (!strcmp(argv[i], "-m") || !strcmp(argv[i], "--monitor")) {
++i;
bool is_num = true;
for (int j = 0; j < strlen(argv[i]); ++j) {
if (!isdigit(argv[i][j])) {
is_num = false;
break;
}
}
if (is_num) {
selected_monitor = atoi(argv[i]);
} else {
selected_monitor = -1;
selected_monitor_name = argv[i];
}
}
else if (!strcmp(argv[i], "-p") || !strcmp(argv[i], "--prompt"))
prompt = argv[++i];
else if (!strcmp(argv[i], "-P") || !strcmp(argv[i], "--password"))
password = true;
else if (!strcmp(argv[i], "-po") || !strcmp(argv[i], "--prompt-only"))
prompt = argv[++i], nostdin = true;
else if (!strcmp(argv[i], "-fn") || !strcmp(argv[i], "--font-name"))
font = argv[++i];
else if (!strcmp(argv[i], "-nb") || !strcmp(argv[i], "--normal-background"))
color_bg = color_input_bg = parse_color(argv[++i]);
else if (!strcmp(argv[i], "-nf") || !strcmp(argv[i], "--normal-foreground"))
color_fg = color_input_fg = parse_color(argv[++i]);
else if (!strcmp(argv[i], "-sb") ||
!strcmp(argv[i], "--selected-background"))
color_prompt_bg = color_selected_bg = parse_color(argv[++i]);
else if (!strcmp(argv[i], "-sf") ||
!strcmp(argv[i], "--selected-foreground"))
color_prompt_fg = color_selected_fg = parse_color(argv[++i]);
else {
usage();
}
}
if (message) {
signal(SIGALRM, alarmhandler);
alarm(timeout);
}
if (!nostdin) {
readstdin();
}
int32_t panel_height = line_height;
if (lines) {
// +1 for input
panel_height *= lines + 1;
}
struct dmenu_panel panel;
panel.selected_monitor = selected_monitor;
panel.selected_monitor_name = selected_monitor_name;
dmenu_init_panel(&panel, panel_height, show_in_bottom);
panel.on_keyevent = keypress;
panel.on_keyrepeat = keyrepeat;
panel.draw = draw;
match(&panel);
struct monitor_info *monitor = panel.monitor;
double factor = monitor->scale / ((double)monitor->physical_width / monitor->logical_width);
window_config.height = round_to_int(panel.height / ((double)monitor->physical_width
/ monitor->logical_width));
window_config.height *= monitor->scale;
window_config.width = round_to_int(monitor->physical_width * factor);
get_text_size(panel.surface.cairo, font, NULL, &window_config.text_height,
NULL, monitor->scale, false, "Aj");
window_config.text_y = (window_config.height / 2.0) - (window_config.text_height / 2.0);
dmenu_show(&panel);
return retcode;
}
void appenditem(Item *item, Item **list, Item **last) {
if(!*last) {
*list = item;
} else {
(*last)->right = item;
}
item->left = *last;
item->right = NULL;
*last = item;
}
char * fstrstr(const char *s, const char *sub) {
size_t len;
for(len = strlen(sub); *s; s++) {
if(!fstrncmp(s, sub, len)) {
return (char *)s;
}
}
return NULL;
}
// TODO: find a better way than passing the panel for early return
void match(struct dmenu_panel* panel) {
size_t len;
Item *item, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
rightmost = leftmost = NULL;
len = strlen(text);
matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
for(item = items; item; item = item->next)
if(!fstrncmp(text, item->text, len + 1)) {
appenditem(item, &lexact, &exactend);
}
else if(!fstrncmp(text, item->text, len)) {
appenditem(item, &lprefix, &prefixend);
}
else if(fstrstr(item->text, text)) {
appenditem(item, &lsubstr, &substrend);
}
if(lexact) {
matches = lexact;
itemend = exactend;
}
if(lprefix) {
if(itemend) {
itemend->right = lprefix;
lprefix->left = itemend;
}
else {
matches = lprefix;
}
itemend = prefixend;
}
if(lsubstr) {
if(itemend) {
itemend->right = lsubstr;
lsubstr->left = itemend;
}
else
matches = lsubstr;
}
curr = prev = next = sel = matches;
/* calcoffsets(); */
leftmost = matches;
if(returnearly && !curr->right) {
printf("dieter\n");
handle_return(curr->text, panel);
}
}
size_t nextrune(int incr) {
size_t n, len;
len = strlen(text);
for(n = cursor + incr; n >= 0 && n < len && (text[n] & 0xc0) == 0x80; n += incr);
return n;
}
void readstdin(void) {
char buf[sizeof text], *p;
Item *item, **end;
for(end = &items; fgets(buf, sizeof buf, stdin); *end = item, end = &item->next) {
itemcount++;
if((p = strchr(buf, '\n'))) {
*p = '\0';
}
if(!(item = malloc(sizeof *item))) {
eprintf("cannot malloc %u bytes\n", sizeof *item);
}
item->width = -1;
if(!(item->text = strdup(buf))) {
eprintf("cannot strdup %u bytes\n", strlen(buf)+1);
}
item->next = item->left = item->right = NULL;
}
}
void handle_return(char* value, struct dmenu_panel* panel) {
dmenu_close(panel);
fputs(value, stdout);
fflush(stdout);
}
void alarmhandler(int signum) {
exit(EXIT_SUCCESS);
}
void usage(void) {
printf("Usage: dmenu [OPTION]...\n");
printf("Display newline-separated input stdin as a menubar\n");
printf("\n");
printf(" -e, --echo display text from stdin with no user\n");
printf(" interaction\n");
printf(" -ec, --echo-centre same as -e but align text centrally\n");
printf(" -er, --echo-right same as -e but align text right\n");
printf(" -et, --echo-timeout SECS close the message after SEC seconds\n");
printf(" when using -e, -ec, or -er\n");
printf(" -b, --bottom dmenu appears at the bottom of the screen\n");
printf(" -h, --height N set dmenu to be N pixels high\n");
printf(" -i, --insensitive dmenu matches menu items case insensitively\n");
printf(" -l, --lines LINES dmenu lists items vertically, within the\n");
printf(" given number of lines\n");
printf(" -m, --monitor MONITOR dmenu appears on the given Xinerama screen\n");
printf(" (does nothing on wayland, supported for)\n");
printf(" compatibility with dmenu.\n");
printf(" -p, --prompt PROMPT prompt to be displayed to the left of the\n");
printf(" input field\n");
printf(" -P, --password input will be hidden\n");
printf(" -po, --prompt-only PROMPT same as -p but don't wait for stdin\n");
printf(" useful for a prompt with no menu\n");
printf(" -r, --return-early return as soon as a single match is found\n");
printf(" -fn, --font-name FONT font or font set to be used\n");
printf(" -nb, --normal-background COLOR normal background color\n");
printf(" #RRGGBB and #RRGGBBAA supported\n");
printf(" -nf, --normal-foreground COLOR normal foreground color\n");
printf(" -sb, --selected-background COLOR selected background color\n");
printf(" -sf, --selected-foreground COLOR selected foreground color\n");
printf(" -v, --version display version information\n");
exit(EXIT_FAILURE);
}