forked from Tomas-M/xlunch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xlunch.c
2633 lines (2361 loc) · 91.9 KB
/
xlunch.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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// this code is not optimal in many ways, but works just nicely.
// Licence: GNU GPL v3
// Authors: Tomas Matejicek <www.slax.org>
// Peter Munch-Ellingsen <www.peterme.net>
const int VERSION_MAJOR = 3; // Major version, changes when breaking backwards compatability
const int VERSION_MINOR = 2; // Minor version, changes when new functionality is added
const int VERSION_PATCH = 11; // Patch version, changes when something is changed without changing deliberate functionality (eg. a bugfix or an optimisation)
#define _GNU_SOURCE
/* open and O_RDWR,O_CREAT */
#include <fcntl.h>
/* include X11 stuff */
#include <X11/Xlib.h>
/* include Imlib2 stuff */
#include <Imlib2.h>
/* sprintf include */
#include <stdio.h>
/* strcpy include */
#include <string.h>
/* exit include */
#include <stdlib.h>
/* exec include */
#include <unistd.h>
/* long options include*/
#include <getopt.h>
/* cursor */
#include <X11/cursorfont.h>
/* X utils */
#include <X11/Xutil.h>
#include <X11/Xatom.h>
/* parse commandline arguments */
#include <ctype.h>
/* one instance */
#include <sys/file.h>
/* check stdin */
#include <sys/poll.h>
#include <errno.h>
/* some globals for our window & X display */
Display *disp;
Window win;
XVisualInfo vinfo;
XSetWindowAttributes attr;
int x11_fd;
struct pollfd eventfds[2];
XIM im;
XIC ic;
int screen;
int screen_width;
int screen_height;
// Let's define a linked list node:
typedef struct node
{
char title[256];
char icon[256];
char cmd[512];
int hovered;
int clicked;
int hidden;
int x;
int y;
struct node * next;
} node_t;
typedef struct button
{
char icon_normal[256];
char icon_highlight[256];
char cmd[512];
int hovered;
int clicked;
int x;
int y;
int w;
int h;
struct button * next;
} button_t;
typedef struct shortcut
{
char *key;
node_t *entry;
struct shortcut *next;
} shortcut_t;
typedef struct keynode {
char key[255];
struct keynode * next;
} keynode_t;
typedef struct color {
int r, g, b, a;
} color_t;
typedef struct percentable {
int percent;
int value;
} percentable_t;
int entries_count = 0;
node_t * entries = NULL;
button_t * buttons = NULL;
shortcut_t * shortcuts = NULL;
keynode_t * cmdline = NULL;
enum exit_code {
OKAY,
ESCAPE = 0x20,
RIGHTCLICK,
VOIDCLICK,
FOCUSLOST,
INTERNALCMD,
LOCKERROR = 0x40,
ALLOCERROR,
FONTERROR,
CONFIGERROR,
WINERROR,
LOCALEERROR,
INPUTMERROR,
INPUTCERROR,
POLLERROR
};
static struct option long_options[] =
{
{"tc", required_argument, 0, 1009},
{"textcolor", required_argument, 0, 1009},
{"pc", required_argument, 0, 1010},
{"promptcolor", required_argument, 0, 1010},
{"backgroundcolor", required_argument, 0, 1011},
{"bc", required_argument, 0, 1011},
{"hc", required_argument, 0, 1012},
{"highlightcolor", required_argument, 0, 1012},
{"name", required_argument, 0, 1013},
{"config", required_argument, 0, 1014},
{"bgfill", no_argument, 0, 1015},
{"focuslostterminate", no_argument, 0, 1016},
{"borderratio", required_argument, 0, 1017},
{"sideborderratio", required_argument, 0, 1018},
{"scroll", no_argument, 0, 1019},
{"button", required_argument, 0, 'A'},
{"textafter", no_argument, 0, 'a'},
{"border", required_argument, 0, 'b'},
{"sideborder", required_argument, 0, 'B'},
{"center", no_argument, 0, 'C'},
{"columns", required_argument, 0, 'c'},
{"desktop", no_argument, 0, 'd'},
{"hidemissing", no_argument, 0, 'e'},
{"font", required_argument, 0, 'f'},
{"promptfont", required_argument, 0, 'F'},
{"background", required_argument, 0, 'g'},
{"rootwindowbackground", no_argument, 0, 'G'},
{"height", required_argument, 0, 'h'},
{"help", no_argument, 0, 'H'},
{"iconpadding", required_argument, 0, 'I'},
{"input", required_argument, 0, 'i'},
{"highlight", required_argument, 0, 'L'},
{"leastmargin", required_argument, 0, 'l'},
{"clearmemory", no_argument, 0, 'M'},
{"multiple", no_argument, 0, 'm'},
{"noprompt", no_argument, 0, 'n'},
{"outputonly", no_argument, 0, 'o'},
{"textotherside", no_argument, 0, 'O'},
{"prompt", required_argument, 0, 'p'},
{"promptspacing", required_argument, 0, 'P'},
{"dontquit", no_argument, 0, 'q'},
{"reverse", no_argument, 0, 'R'},
{"rows", required_argument, 0, 'r'},
{"iconsize", required_argument, 0, 's'},
{"selectonly", no_argument, 0, 'S'},
{"textpadding", required_argument, 0, 'T'},
{"voidclickterminate", no_argument, 0, 't'},
{"shortcuts", required_argument, 0, 'U'},
{"upsidedown", no_argument, 0, 'u'},
{"leastvmargin", required_argument, 0, 'V'},
{"version", no_argument, 0, 'v'},
{"width", required_argument, 0, 'w'},
{"windowed", no_argument, 0, 'W'},
{"paddingswap", no_argument, 0, 'X'},
{"xposition", required_argument, 0, 'x'},
{"yposition", required_argument, 0, 'y'},
{0, 0, 0, 0}
};
int icon_size = 48;
int ucolumns = 0;
int columns;
int urows = 0;
int rows;
int column_margin = 0;
int row_margin = 0;
int icon_padding = 10;
int text_padding = 10;
int border;
int side_border = 0;
int border_ratio = 50;
int side_border_ratio = 50;
int cell_width;
int cell_height;
int font_height;
int prompt_font_height;
int use_root_img = 0;
char commandline[10024];
char commandlinetext[10024];
int prompt_x;
int prompt_y;
char * background_file = "";
char * highlight_file = "";
char * input_file = "";
FILE * input_source = NULL;
char * prompt = "";
char * font_name = "";
char * prompt_font_name = "";
char * program_name;
int bg_fill = 0;
int no_prompt = 0;
int prompt_spacing = 48;
int windowed = 0;
int multiple_instances = 0;
int uposx = -1;
int uposy = -1;
int uwidth = 0;
int uheight = 0;
percentable_t uborder = { .percent = -1, .value = 0 };
percentable_t uside_border = { .percent = -1, .value = 0 };
int void_click_terminate = 0;
int focus_lost_terminate = 0;
int dont_quit = 0;
int reverse = 0;
int output_only = 0;
int select_only = 0;
int text_after = 0;
int text_other_side = 0;
int clear_memory = 0;
int upside_down = 0;
int padding_swap = 0;
int least_margin = 0;
int least_v_margin = -1;
int hide_missing = 0;
int center_icons = 0;
int scroll = 0;
int scrolled_past= 0;
int hovered_entry = 0;
color_t text_color = {.r = 255, .g = 255, .b = 255, .a = 255};
color_t prompt_color = {.r = 255, .g = 255, .b = 255, .a = 255};
color_t background_color = {.r = 46, .g = 52, .b = 64, .a = 255};
int background_color_set = 0;
color_t highlight_color = {.r = 255, .g = 255, .b = 255, .a = 50};
#define MOUSE 1
#define KEYBOARD 2
int hoverset=MOUSE;
int desktop_mode=0;
int lock;
/* areas to update */
Imlib_Updates updates, current_update;
/* our background image, rendered only once */
Imlib_Image background = NULL;
/* highlighting image (placed under icon on hover) */
Imlib_Image highlight = NULL;
/* image variable */
Imlib_Image image = NULL;
void calculate_percentage(int maxvalue, percentable_t* percentable)
{
if(percentable->percent != -1) {
percentable->value = (maxvalue * percentable->percent) / 100;
}
}
void recalc_cells()
{
int margined_cell_width, margined_cell_height;
if (least_v_margin == -1) least_v_margin = least_margin;
if (text_after){
cell_width=icon_size+icon_padding*2;
cell_height=icon_size+icon_padding*2;
margined_cell_width=icon_size+icon_padding*2+least_margin;
margined_cell_height=icon_size+icon_padding*2+least_v_margin;
if(ucolumns == 0)
ucolumns = 1;
} else {
cell_width=icon_size+icon_padding*2;
cell_height=icon_size+icon_padding*2+font_height+text_padding;
margined_cell_width=icon_size+icon_padding*2+least_margin;
margined_cell_height=icon_size+icon_padding*2+font_height+text_padding+least_v_margin;
}
border = screen_width/10;
if (uborder.value > 0) border = uborder.value;
if (uborder.value == -1 && uborder.percent == -1){
if (ucolumns == 0){
border = 0;
} else {
side_border = (screen_width - (ucolumns * cell_width + (ucolumns - 1) * least_margin))/2;
border = (screen_height - (no_prompt ? 0 : prompt_font_height + prompt_spacing) - (urows * cell_height + (urows - 1) * least_v_margin))/2;
}
}
if (uside_border.value == 0 && uside_border.percent == -1) side_border = border;
if (uside_border.value > 0) side_border = uside_border.value;
if (uside_border.value == -1 && uside_border.percent == -1){
if (ucolumns == 0){
side_border = 0;
} else {
side_border = (screen_width - (ucolumns * cell_width + (ucolumns - 1) * least_margin))/2;
}
}
int usable_width;
int usable_height;
// These do while loops should run at most three times, it's just to avoid copying code
do {
usable_width = screen_width-side_border*2;
if (no_prompt) {
usable_height = screen_height-border*2;
} else {
usable_height = screen_height-border*2-prompt_spacing-prompt_font_height;
}
// If the usable_width is too small, take some space away from the border
if (usable_width < cell_width) {
side_border = (screen_width - cell_width - 1)/2;
} else if (usable_height < cell_height) {
border = (screen_height - cell_height - prompt_spacing - prompt_font_height - 1)/2;
}
} while ((usable_width < cell_width && screen_width > cell_width) || (usable_height < cell_height && screen_height > cell_height));
// If columns were not manually overriden, calculate the most it can possibly contain
if (ucolumns == 0){
columns = usable_width/margined_cell_width;
} else{
columns = ucolumns;
}
if (urows == 0){
rows = usable_height/margined_cell_height;
} else{
rows = urows;
}
// If we don't have space for a single column or row, force it.
if (columns == 0) {
columns = 1;
}
if (rows == 0) {
rows = 1;
}
if (text_after){
cell_width = (usable_width - least_margin*(columns - 1))/columns;
}
// The space between the icon tiles to fill all the space
if (columns == 1){
column_margin = (usable_width - cell_width*columns);
} else {
column_margin = (usable_width - cell_width*columns)/(columns - 1);
}
if (rows == 1){
row_margin = (usable_height - cell_height*rows);
} else {
row_margin = (usable_height - cell_height*rows)/(rows - 1);
}
// These are kept in case manual positioning is reintroduced
prompt_x = (side_border * side_border_ratio) / 50;
prompt_y = (border * border_ratio) / 50;
/*
if(uside_border == 0){
side_border = border;
}*/
}
void restack()
{
if (desktop_mode) XLowerWindow(disp,win);
else XRaiseWindow(disp,win);
}
char* strncpyutf8(char* dst, const char* src, size_t num)
{
if(num)
{
size_t sizeSrc = strlen(src); // number of bytes not including null
while(sizeSrc>num)
{
const char* lastByte = src + sizeSrc; // initially \0 at end
while(lastByte-- > src) // test previous chars
if((*lastByte & 0xC0) != 0x80) // utf8 start byte found
break;
sizeSrc = lastByte-src;
}
memcpy(dst, src, sizeSrc);
dst[sizeSrc] = '\0';
}
return dst;
}
void arrange_positions()
{
node_t * current = entries;
int i = 0;
int j = 0;
while (current != NULL)
{
if (current->hidden)
{
current->x = 0;
current->y = 0;
}
else
{
int entries_last_line = entries_count - j * columns;
if (center_icons && entries_last_line < columns) {
int width = entries_last_line * cell_width + (entries_last_line - 1) * column_margin;
int usable_width = screen_width - side_border * 2;
int margin = usable_width - width;
current->x = (side_border * side_border_ratio) / 50 + margin / 2 + i * (cell_width + column_margin);
} else {
current->x = (side_border * side_border_ratio) / 50 + i * (cell_width+column_margin);
}
if (no_prompt) {
current->y = (border * border_ratio) / 50 + (j - scrolled_past) * (cell_height+row_margin);
} else {
current->y = (border * border_ratio) / 50 + prompt_font_height + prompt_spacing + (j - scrolled_past) * (cell_height+row_margin);
}
if (upside_down) {
current->y=screen_height - cell_height - current->y;
}
if (i == columns-1) {
i = 0;
j++;
} else {
i++;
}
}
current = current->next;
}
}
void push_key(char * key)
{
keynode_t * current = cmdline;
if (current==NULL)
{
cmdline = malloc(sizeof(keynode_t));
strcpy(cmdline->key,key);
cmdline->next = NULL;
return;
}
while (current->next != NULL) {
current = current->next;
}
/* now we can add a new item */
current->next = malloc(sizeof(keynode_t));
strcpy(current->next->key,key);
current->next->next = NULL;
}
void pop_key()
{
if (cmdline==NULL) return;
// if there is only one item, remove it
if (cmdline->next==NULL)
{
free(cmdline);
cmdline=NULL;
return;
}
keynode_t * current = cmdline;
while (current->next->next != NULL) {
current = current->next;
}
/* now we can remove last item */
free(current->next);
current->next=NULL;
}
void clear_entries(){
node_t * current = entries;
while (current != NULL) {
node_t * last = current;
if (clear_memory) {
memset(last->title, 0, 256);
memset(last->icon, 0, 256);
memset(last->cmd, 0, 512);
// Free the entire struct, except for it's next pointer
memset(last, 0, sizeof(node_t) - sizeof(node_t *));
}
free(last);
current = current->next;
}
entries = NULL;
}
void cleanup()
{
flock(lock, LOCK_UN | LOCK_NB);
// destroy window, disconnect display, and exit
XDestroyWindow(disp,win);
XFlush(disp);
XCloseDisplay(disp);
if(input_source == stdin){
int fd = fileno(stdin);
int flags = fcntl(fd, F_GETFL, 0);
flags &= ~O_NONBLOCK;
fcntl(fd, F_SETFL, flags);
fclose(input_source);
}
clear_entries();
button_t * button = buttons;
buttons = NULL;
while (button != NULL) {
button_t *last = button;
button = button->next;
free(last);
}
shortcut_t * shortcut = shortcuts;
shortcuts = NULL;
while (shortcut != NULL) {
shortcut_t *last = shortcut;
shortcut = shortcut->next;
free(last->key);
free(last);
}
}
Imlib_Image load_image(char * icon) {
Imlib_Load_Error load_error;
Imlib_Image image = imlib_load_image_with_error_return(icon, &load_error);
if(image) {
imlib_context_set_image(image);
imlib_free_image();
} else {
fprintf(stderr, "Could not load icon %s, Imlib failed with: ", icon);
switch(load_error) {
case IMLIB_LOAD_ERROR_NONE:
fprintf(stderr, "IMLIB_LOAD_ERROR_NONE");
break;
case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST:
fprintf(stderr, "IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST");
break;
case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY:
fprintf(stderr, "IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY");
break;
case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ:
fprintf(stderr, "IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ");
break;
case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT:
fprintf(stderr, "IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT");
break;
case IMLIB_LOAD_ERROR_PATH_TOO_LONG:
fprintf(stderr, "IMLIB_LOAD_ERROR_PATH_TOO_LONG");
break;
case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT:
fprintf(stderr, "IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT");
break;
case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY:
fprintf(stderr, "IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY");
break;
case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE:
fprintf(stderr, "IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE");
break;
case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS:
fprintf(stderr, "IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS");
break;
case IMLIB_LOAD_ERROR_OUT_OF_MEMORY:
fprintf(stderr, "IMLIB_LOAD_ERROR_OUT_OF_MEMORY");
break;
case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS:
fprintf(stderr, "IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS");
break;
case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE:
fprintf(stderr, "IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE");
break;
case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE:
fprintf(stderr, "IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE");
break;
case IMLIB_LOAD_ERROR_UNKNOWN:
fprintf(stderr, "IMLIB_LOAD_ERROR_UNKNOWN");
break;
}
fprintf(stderr, "\n");
/*
cleanup();
exit(1);*/
}
return image;
}
void push_entry(node_t * new_entry)//(char * title, char * icon, char * cmd, int x, int y)
{
node_t * current = entries;
int hasicon = (strlen(new_entry->icon) != 0);
/* Pre-load the image into the cache, this is done to check for error messages
* If a user has more images then can be shown this might incur a performance hit */
if (hasicon) {
Imlib_Image image = load_image(new_entry->icon);
if (image == NULL) {
if (hide_missing) return;
strcpy(new_entry->icon, "/usr/share/icons/hicolor/48x48/apps/xlunch_ghost.png");
}
}
// Set default values for internal state
new_entry->x=0;
new_entry->y=0;
new_entry->hidden=0;
new_entry->hovered=0;
new_entry->clicked=0;
new_entry->next = NULL;
shortcut_t *shortcut = shortcuts;
while (shortcut != NULL) {
if (shortcut->entry == NULL) {
shortcut->entry = new_entry;
break;
}
shortcut = shortcut->next;
}
// empty list, add first directly
if (current==NULL)
{
entries = new_entry;
entries->next = NULL;
return;
}
// Otherwise determine position
if(!reverse){
// Go to end of list and add there
while (current->next != NULL) {
current = current->next;
}
current->next = new_entry;
current->next->next = NULL;
} else {
// Add at head of list
new_entry->next = entries;
entries = new_entry;
}
entries_count++;
}
char *strtok_new(char * string, char const * delimiter){
static char *source = NULL;
char *p, *riturn = 0;
if(string != NULL) source = string;
if(source == NULL) return NULL;
if((p = strpbrk (source, delimiter)) != NULL) {
*p = 0;
riturn = source;
source = ++p;
}
return riturn;
}
char* concat(const char *s1, const char *s2)
{
char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
if(result != 0) {
strcpy(result, s1);
strcat(result, s2);
return result;
}
exit(ALLOCERROR);
}
FILE * determine_input_source(){
FILE * fp;
if(input_source == NULL) {
char * homeconf = NULL;
char * home = getenv("HOME");
if (home!=NULL)
{
homeconf = concat(home,"/.config/xlunch/entries.dsv");
}
if (strlen(input_file)==0){
fp = stdin;
int flags;
int fd = fileno(fp);
flags = fcntl(fd, F_GETFL, 0);
flags |= O_NONBLOCK;
fcntl(fd, F_SETFL, flags);
struct pollfd fds;
fds.fd = 0; /* this is STDIN */
fds.events = POLLIN;
// Give poll a little timeout to make give the piping program some time
if (poll(&fds, 1, 10) == 0){
int flags = fcntl(fd, F_GETFL, 0);
flags &= ~O_NONBLOCK;
fcntl(fd, F_SETFL, flags);
fclose(stdin);
fp = fopen(homeconf, "rb");
}
} else {
fp = fopen(input_file, "rb");
}
if (fp == NULL)
{
fprintf(stderr, "Error opening entries file from %s.\nReverting back to system conf.\n", input_file);
input_file = "/etc/xlunch/entries.dsv";
fp = fopen(input_file, "rb");
if (fp == NULL)
{
fprintf(stderr, "Error opening entries file %s\n", input_file);
fprintf(stderr, "You may need to create it. Icon file format is following:\n");
fprintf(stderr, "title;icon_path;command\n");
fprintf(stderr, "title;icon_path;command\n");
fprintf(stderr, "title;icon_path;command\n");
}
}
free(homeconf);
} else {
fp = input_source;
}
return fp;
}
int mouse_over_cell(node_t * cell, int index, int mouse_x, int mouse_y)
{
if (cell->hidden) return 0;
if (index > scrolled_past*columns && index < scrolled_past*columns + rows*columns +1
&& mouse_x >= cell->x
&& mouse_x < cell->x+cell_width
&& mouse_y >= cell->y
&& mouse_y < cell->y+cell_height) return 1;
else return 0;
}
int mouse_over_button(button_t * button, int mouse_x, int mouse_y)
{
int x = (button->x < 0 ? screen_width + button->x + 1 - button->w : button->x);
int y = (button->y < 0 ? screen_height + button->y + 1 - button->h : button->y);
if ( mouse_x >= x
&& mouse_x < x+button->w
&& mouse_y >= y
&& mouse_y < y+button->h) return 1;
else return 0;
}
Pixmap GetRootPixmap(Display* display, Window *root)
{
Pixmap currentRootPixmap;
Atom act_type;
int act_format;
unsigned long nitems, bytes_after;
unsigned char *data = NULL;
Atom _XROOTPMAP_ID;
_XROOTPMAP_ID = XInternAtom(display, "_XROOTPMAP_ID", False);
if (XGetWindowProperty(display, *root, _XROOTPMAP_ID, 0, 1, False,
XA_PIXMAP, &act_type, &act_format, &nitems, &bytes_after,
&data) == Success) {
if (data) {
currentRootPixmap = *((Pixmap *) data);
XFree(data);
}
}
return currentRootPixmap;
}
int get_root_image_to_imlib_data(DATA32 * direct)
{
int screen;
Window root;
Display* display;
XWindowAttributes attrs;
Pixmap bg;
XImage* img;
display = XOpenDisplay(NULL);
if (display == NULL) return 0;
screen = DefaultScreen(display);
root = RootWindow(display, screen);
XGetWindowAttributes(display, root, &attrs);
bg = GetRootPixmap(display, &root);
img = XGetImage(display, bg, 0, 0, attrs.width, attrs.height, ~0, ZPixmap);
XFreePixmap(display, bg);
if (!img) {
XCloseDisplay(display);
return 0;
}
unsigned long pixel;
int x, y;
for (y = 0; y < img->height; y++)
{
for (x = 0; x < img->width; x++)
{
pixel = XGetPixel(img,x,y);
direct[y*img->width+x+0] = 0xffffffff&pixel;
}
}
XDestroyImage(img);
return 1;
}
void joincmdline()
{
strcpy(commandline,"");
keynode_t * current = cmdline;
while (current != NULL) {
strcat(commandline,current->key);
current = current->next;
}
}
void joincmdlinetext()
{
if (no_prompt) return;
if (strlen(prompt)==0) prompt="Run: ";
strcpy(commandlinetext,prompt);
keynode_t * current = cmdline;
while (current != NULL) {
strcat(commandlinetext,current->key);
current = current->next;
}
strcat(commandlinetext,"_");
}
void set_scroll_level(int new_scroll) {
if (scroll){
if (new_scroll != scrolled_past) {
scrolled_past = new_scroll;
if (scrolled_past > (entries_count - 1)/columns - rows + 1) {
scrolled_past = (entries_count - 1)/columns - rows + 1;
}
if (scrolled_past < 0) {
scrolled_past = 0;
}
arrange_positions();
updates = imlib_update_append_rect(updates, 0, 0, screen_width, screen_height);
}
}
}
void set_hover(int hovered, node_t * cell, int hover)
{
if (hover) hovered_entry = hovered;
if (cell==NULL) return;
if (cell->hidden) return;
if (cell->hovered!=hover) updates = imlib_update_append_rect(updates, cell->x, cell->y, cell_width, cell_height);
cell->hovered=hover;
}
void hover_entry(int entry){
hoverset = KEYBOARD;
int to_row = (entry+columns-1)/columns;
int display_row = (hovered_entry-(scrolled_past*columns)+columns-1)/columns;
if (entry>(columns*rows)+scrolled_past*columns || entry<=scrolled_past*columns) {
set_scroll_level(to_row - display_row);
}
int j = 1;
int max = scrolled_past*columns + columns*rows;
max = (max > entries_count ? entries_count : max);
int i = (entry < scrolled_past*columns + 1
? scrolled_past*columns + 1
: (entry > max
? max
: entry));
node_t * current = entries;
while (current != NULL) {
if (j == i) set_hover(i, current, 1);
else set_hover(i, current, 0);
if (!current->hidden) j++;
current = current->next;
}
}
void filter_entries()
{
node_t * current = entries;
entries_count = 0;
while (current != NULL)
{
if (strlen(commandline)==0 || strcasestr(current->title,commandline)!=NULL || strcasestr(current->cmd,commandline)!=NULL){
current->hidden=0;
entries_count ++;
} else {
current->hidden=1;
current->clicked=0;
set_hover(0, current, 1);
}
current=current->next;
}
set_scroll_level(0);
}
int starts_with(const char *pre, const char *str)
{
size_t lenpre = strlen(pre),
lenstr = strlen(str);
return lenstr < lenpre ? 0 : strncmp(pre, str, lenpre) == 0;
}
void run_command(char * cmd_orig);
void run_internal_command(char * cmd_orig) {
int will_quit = 0;
char * cmd = malloc(strlen(cmd_orig)+1);
memcpy(cmd, cmd_orig, strlen(cmd_orig)+1);
char *p = strtok( cmd, " ");
if (strcmp(p, "scroll") == 0) {
p = strtok( NULL, " ");
if (strcmp(p, "top") == 0){
set_scroll_level(0);
} else if (strcmp(p, "bottom") == 0){
set_scroll_level(entries_count);
} else {
if (p[0] == '+' || p[0] == '-')
set_scroll_level(scrolled_past + atoi(p));
else
set_scroll_level(atoi(p));
}
} else if (strcmp(p, "hover") == 0) {
if (hoverset != KEYBOARD) hovered_entry = 0;
p = strtok( NULL, " ");
int new_hover;
if (strcmp(p, "start") == 0){
new_hover = 1;
} else if (strcmp(p, "end") == 0){
new_hover = entries_count;
} else {
if (p[0] == '+' || p[0] == '-')
new_hover = hovered_entry + atoi(p);
else
new_hover = atoi(p);
}
hover_entry(new_hover);
} else if (strcmp(p, "exec") == 0) {
int old_oo = output_only;
output_only = 0;
run_command(strtok(NULL, "\""));
output_only = old_oo;
} else if (strcmp(p, "print") == 0){
printf("%s\n", strtok(NULL, "\""));
} else if (strcmp(p, "quit") == 0) {
will_quit = 1;
}
if (p != NULL) {
p = strtok(NULL, "");
if (p != NULL) {
if (p[0] == ':') {
run_internal_command(&p[1]);
} else {
fprintf(stderr, "Internal command \"%s\" supplied with extraneous parameters \"%s\"\n", cmd_orig, p);
}
}
}
free(cmd);
if (will_quit) {
cleanup();