-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
538 lines (445 loc) · 15.1 KB
/
main.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
/*
* ---------------------------------------------------------------------
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* ---------------------------------------------------------------------
*
* Original Authors: Aetnaeus (https://github.com/lemnos)
*
*/
#include <cairo/cairo.h>
#include <cairo/cairo-xlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <dirent.h>
#include <regex.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <linux/limits.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/cursorfont.h>
#include <X11/Xutil.h>
#include "x11.h"
#include "cfg.h"
#include "widgets/evloop.h"
#include "widgets/draw.h"
#include "widgets/menu.h"
#include "widgets/input.h"
#include "filters/stdin.h"
#include "filters/program.h"
#include "filters/noop.h"
Display *dpy;
Window win;
size_t (*selection_fn) (char *sel, char **result);
size_t (*filter_fn) (const char *input, char ***items);
struct cfg *cfg;
struct ui_evloop *evloop;
struct ui_widget *input;
struct ui_widget *menu;
char **menu_items;
size_t menu_items_sz;
//OPTS
char delim = '\0';
int field = 0;
int force_selection_flag = 0;
int input_only_flag = 0;
int use_regex_flag = 0;
char *generator_program = NULL;
char *history_file = NULL;
void print_version() {
const char *str =
"Talaria (v "VERSION")\n"
"Author: Aetnaeus (2018).\n"
"Built from git commit: "GIT_HASH"\n";
fprintf(stderr, str);
exit(1);
}
void calc_heights(int nitems, int *window_height, int *menu_height) {
int max, mh, wh;
max = cfg->h - cfg->input_separator_height - cfg->input_height - cfg->border_sz*2;
mh = nitems * (cfg->menu_divider_height + cfg->input_height);
mh = mh > max ? max : mh;
wh = mh + cfg->border_sz*2 + cfg->input_height + (nitems ? cfg->input_separator_height : 0);
if(window_height) *window_height = wh;
if(menu_height) *menu_height = mh;
}
void resize_window(struct ui_evloop *evl) {
int mh, wh;
calc_heights(menu_items_sz, &wh, &mh);
ui_evloop_resize(evl, cfg->x, cfg->y, cfg->w, wh);
ui_menu_set_height(menu, mh);
}
long gettime() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec*1000) + (long)((double)tv.tv_usec/1000);
}
int valid_hex(char *col) {
if(strlen(col) < 7) return -1;
return (col[0] == '#') && (col[strspn(col+1, "0123456789abcdefABCDEF")+1] == 0);
}
struct ui_color color_from_string(char *col) {
if(!valid_hex(col)) {
fprintf(stderr, "%s is not a valid hex string\n", col);
exit(1);
}
#define chrtobin(chr) ((chr > '9') ? (chr|0x20) - 'a' + 10 : chr - '0')
col = col[0] == '#' ? col+1 : col;
struct ui_color res;
res.r = (chrtobin(col[0]) << 4 | chrtobin(col[1]))/256.0;
res.g = (chrtobin(col[2]) << 4 | chrtobin(col[3]))/256.0;
res.b = (chrtobin(col[4]) << 4 | chrtobin(col[5]))/256.0;
res.a = (col[6] == 0 ? 256 : (chrtobin(col[6]) << 4 | chrtobin(col[7])))/256.0;
return res;
#undef chrtobin
}
Window create_unmanaged_window(Display *dpy, int x, int y, int w, int h) {
Window win;
XSetWindowAttributes attr = {.override_redirect = 1};
win = XCreateWindow(dpy,
DefaultRootWindow(dpy),
x, y, w, h, 0,
XDefaultDepth(dpy, DefaultScreen(dpy)),
InputOutput,
DefaultVisual(dpy, DefaultScreen(dpy)),
CWOverrideRedirect,
&attr);
XMapWindow(dpy, win);
XFlush(dpy);
return win;
}
cairo_surface_t * get_root_bitmap(Display *dpy) {
XWindowAttributes info;
cairo_surface_t *tmp, *xsfc;
cairo_t *cr;
Window root = DefaultRootWindow(dpy);
XGetWindowAttributes(dpy, root, &info);
tmp = cairo_xlib_surface_create(
dpy,
DefaultRootWindow(dpy),
DefaultVisual(dpy, DefaultScreen(dpy)),
info.width,
info.height);
xsfc = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, info.width, info.height);
cr = cairo_create(xsfc);
cairo_set_source_surface(cr, tmp, 0, 0);
cairo_rectangle(cr, 0, 0, info.width, info.height);
cairo_fill(cr);
cairo_surface_destroy(tmp);
cairo_destroy(cr);
return xsfc;
}
void draw_border(cairo_t *cr, int w, int h, int sz, int r, struct ui_color col) {
#define M_PI 3.14159265358979323846
cairo_set_line_width (cr, sz);
cairo_set_source_rgba(cr, col.r, col.g, col.b, col.a);
cairo_arc(cr, (double)r+sz/2.0, (double)r+sz/2.0, r, M_PI, M_PI*(3.0/2.0));
cairo_arc(cr, (double)w-r-sz/2.0, (double)r+sz/2.0, r, M_PI*(3.0/2.0), 0);
cairo_arc(cr, (double)w-r-sz/2.0, (double)h-r-sz/2.0, r, 0, M_PI/2);
cairo_arc(cr, (double)r+sz/2.0, (double)h-r-sz/2.0, r, M_PI/2.0, M_PI);
cairo_close_path(cr);
cairo_stroke(cr);
}
static void init_opts(int argc, char *argv[]) {
XWindowAttributes info;
XGetWindowAttributes(dpy, DefaultRootWindow(dpy), &info);
const char *optstr = "f:d:x:y:w:h:u:b:mrivl:p:t:";
for(char c=getopt(argc, argv, optstr);c!=-1;c=getopt(argc, argv, optstr)) {
switch(c) {
case 'v': print_version(); break;
case 'd': delim = optarg[0]; break;
case 'f': field = atoi(optarg); break;
case 'm': force_selection_flag++; break;
case 'r': use_regex_flag++; break;
case 'i': input_only_flag++; break;
case 'x': cfg->x_str = optarg; break;
case 'y': cfg->y_str = optarg; break;
case 'w': cfg->w_str = optarg; break;
case 'h': cfg->h_str = optarg; break;
case 'u': cfg->input_height = atoi(optarg); break;
case 'b': cfg->border_sz = atoi(optarg); break;
case 'l': history_file = optarg; break;
default: exit(-1);
}
}
if(optind != argc) {
generator_program = argv[argc-1];
}
}
struct cfg* get_cfg() {
char config_path[PATH_MAX];
snprintf(config_path, PATH_MAX, "%s/.talariarc", getenv("HOME"));
struct cfg *cfg = parse_cfg(config_path);
return cfg;
}
size_t read_history(char ***_history) {
size_t history_sz = 0;
char **history = NULL;
if(history_file) {
size_t cap = 0;
FILE *fp = fopen(history_file, "r");
if(fp) {
char *line = NULL;
ssize_t n = 0;
while((n=getline(&line, (size_t*)&n, fp)) != -1) {
if(history_sz == cap) {
cap += 100;
history = realloc(history, sizeof(char*)*cap);
}
if(line[n-1] == '\n')
line[n-1] = '\0';
history[history_sz++] = line;
line = NULL;
n = 0;
}
fclose(fp);
}
}
*_history = history;
return history_sz;
}
static void redraw_chrome(cairo_t *cr) {
int wh;
struct ui_color border_color, input_separator_color;
border_color = color_from_string(cfg->border_color);
input_separator_color = color_from_string(cfg->input_separator_color);
static cairo_surface_t *rootbmp = NULL;
calc_heights(menu_items_sz, &wh, NULL);
if(!rootbmp) rootbmp = get_root_bitmap(dpy); //HOTSPOT (mem)
cairo_set_source_surface(cr, rootbmp, -cfg->x, -cfg->y);
cairo_rectangle(cr, 0,0,cfg->w,wh);
cairo_fill(cr);
if(menu_items_sz) {
cairo_set_source_rgba (cr,
input_separator_color.r,
input_separator_color.g,
input_separator_color.b,
input_separator_color.a);
cairo_rectangle(cr, 0, cfg->border_sz + cfg->input_height,
cfg->w, cfg->input_separator_height);
cairo_fill(cr);
}
draw_border(cr, cfg->w, wh, cfg->border_sz, cfg->border_radius, border_color);
}
static void on_select(struct ui_menu *_, int idx) {
int sz;
char *sel;
if(force_selection_flag && !menu_items_sz) return;
if(menu_items_sz) {
sel = menu_items[idx];
sz = strlen(sel);
} else {
sel = ((struct ui_input*)input->ctx)->input;
sz = strlen(sel);
}
sz = selection_fn(sel, &sel);
//FIXME
if(history_file) {
char **history;
size_t history_sz;
FILE *fp;
history_sz = read_history(&history);
fp = fopen(history_file, "w");
if(!fp) {
fprintf(stderr, "Failed to write history to %s\n", history_file);
} else {
char *selection;
//Store the menu option in the history entry rather than
//the actual item (simplifies sorting)
selection = menu_items_sz ? menu_items[idx] : sel;
for (size_t i = 0; i < history_sz; i++) {
if(strcmp(history[i], selection))
fprintf(fp, "%s\n", history[i]);
}
fprintf(fp, "%s\n", selection);
fclose(fp);
}
}
XUnmapWindow(dpy, win);
XFlush(dpy);
write(1, sel, sz); //printf ignores \0 even when sz is specified.
write(1, "\n", 1);
exit(0);
}
static void on_input_update(struct ui_input *inp, const char *input) {
menu_items_sz = filter_fn(input, &menu_items);
ui_menu_set_items(menu, menu_items, menu_items_sz);
resize_window(inp->loop);
}
size_t file_item_completion(const char *prefix, const char *str, char ***results) {
size_t prefix_len = strlen(prefix);
size_t sz;
struct dirent *ent;
DIR *dh;
char dir[PATH_MAX];
const char *end = NULL;
const char *partial = "";
for(const char *c=str;*c;c++)
if(*c == '/')
end = c;
if(!end) return 0;
memcpy(dir, str, end-str+1);
dir[end-str+1] = '\0';
partial = end+1;
if (strstr(dir, "~/") == dir) {
char tmp[PATH_MAX];
const char *home = getenv("HOME");
sprintf(tmp, "%s/%s", home, dir+2);
strcpy(dir, tmp);
}
if(!(dh = opendir(dir)))
return 0;
sz = 0;
while(readdir(dh)) sz++;
*results = malloc(sizeof(char*) * sz);
rewinddir(dh);
sz = 0;
while((ent=readdir(dh)))
if(strstr(ent->d_name, partial) == ent->d_name &&
strcmp(".", ent->d_name) &&
strcmp("..", ent->d_name)) {
(*results)[sz] = malloc(PATH_MAX + prefix_len);
snprintf((*results)[sz], PATH_MAX+prefix_len+2,
"%s %s%s%s",
prefix,
dir,
ent->d_name,
ent->d_type == DT_DIR ? "/" : "");
sz++;
}
return sz;
}
size_t menu_item_completion(const char *str, char ***results) {
size_t n = 0;
*results = malloc(menu_items_sz * sizeof(char*));
for(size_t i=0;i<menu_items_sz;i++)
if(strstr(menu_items[i], str))
(*results)[n++] = menu_items[i];
return n;
}
size_t completion_fn(const char *_str, char ***results) {
size_t sz;
char *str = strdup(_str);
char *path = NULL;
char *prefix = str;
for(int i = strlen(str)-1;i>=0;i--)
if(str[i] == ' ') {
str[i] = '\0';
path = str + i + 1;
break;
}
if(!path)
sz = menu_item_completion(str, results);
else
sz = file_item_completion(prefix, path, results);
free(str);
return sz;
}
void init_ui(Display *dpy, Window win, char **history, size_t history_sz) {
struct ui_color
fgcol,
bgcol,
selbgcol,
selfgcol,
cursor_color,
border_color;
int menu_item_height = cfg->input_height;
fgcol = color_from_string(cfg->foreground_color);
bgcol = color_from_string(cfg->background_color);
selbgcol = color_from_string(cfg->menu_sel_background_color);
selfgcol = color_from_string(cfg->menu_sel_foreground_color);
border_color = color_from_string(cfg->border_color);
cursor_color = color_from_string(cfg->cursor_color);
evloop = ui_create_evloop(dpy, win);
evloop->pre_redraw = redraw_chrome;
input = ui_create_input(
evloop,
fc_get_fonts("Inconsolata"), //FIXME
cfg->border_sz, cfg->border_sz,
cfg->w-cfg->border_sz*2, cfg->input_height,
cursor_color, bgcol, fgcol,
completion_fn,
(const char**)history, history_sz,
on_input_update);
menu = ui_create_menu(
evloop,
fc_get_fonts("Inconsolata"), //FIXME
cfg->border_sz, cfg->input_height + cfg->border_sz + cfg->input_separator_height,
cfg->w-cfg->border_sz*2, 400,
menu_item_height * 0.75,
menu_item_height,
cfg->menu_divider_height,
bgcol, fgcol,
selbgcol, selfgcol,
NULL, 0,
on_select);
//TODO cleanup evloop code.
ui_evloop_add_widget(evloop, input);
ui_evloop_add_widget(evloop, menu);
on_input_update(input->ctx, "");
}
int main(int argc, char *argv[]) {
int sh, sw, sx, sy;
char *tmp;
char **history;
size_t history_sz;
int wh;
dpy = XOpenDisplay(NULL);
if(!dpy) {
perror("ERROR: Failed to open x11 display. (make sure X is running)");
exit(1);
}
cfg = get_cfg();
init_opts(argc, argv);
//FIXME implement history properly.
history_sz = read_history(&history);
if(generator_program) {
program_filter_init(generator_program);
selection_fn = program_filter_select;
filter_fn = program_filter_filter;
} else if(input_only_flag) {
selection_fn = noop_select;
filter_fn = noop_filter;
} else {
stdin_filter_init(delim, field, use_regex_flag, (const char**)history, history_sz);
selection_fn = stdin_filter_select;
filter_fn = stdin_filter_filter;
}
x11_get_active_screen(dpy, &sx, &sy, &sw, &sh);
cfg->w = !strcmp(cfg->w_str, "auto") ? sw/2 : atof(cfg->w_str);
cfg->h = !strcmp(cfg->h_str, "auto") ? sh/2 : atof(cfg->h_str);
menu_items_sz = filter_fn("", &menu_items);
calc_heights(menu_items_sz, &wh, NULL);
if(!strcmp(cfg->x_str, "auto"))
cfg->x = (sw-cfg->w)/2;
else
cfg->x = cfg->x_str[0] == '-' ? sw + atof(cfg->x_str) - cfg->w : atof(cfg->x_str);
if(!strcmp(cfg->y_str, "auto"))
cfg->y = (sh-wh)/2;
else
cfg->y = cfg->y_str[0] == '-' ? sh + atof(cfg->y_str) - wh : atof(cfg->y_str);
//x/y should be relative to the current screen.
cfg->y += sy;
cfg->x += sx;
win = create_unmanaged_window(dpy, cfg->x, cfg->y, cfg->w, wh);
init_ui(dpy, win, history, history_sz);
ui_evloop_run(evloop);
selection_fn("", &tmp);
exit(1);
}