summaryrefslogtreecommitdiffstats
path: root/xlnch.c
blob: d2e31784333ee09362d3cc5f8f0f21fbdc752519 (plain) (blame)
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
/**
 * xlnch -- D e s c r i p t i o n
 * Copyright (C) 2019  Anastasis Grammenos
 *
 * 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 2
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

/* Basic X code from:
 * Brian Hammond 2/9/96. http://mech.math.msu.su/~nap/2/GWindow/xintro.html
 */

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#include <sys/wait.h>

#include <wordexp.h>

#define MAX(A, B) ((A) > (B) ? (A) : (B))

Display *dis;
int screen;
Window win;
GC gc;

Colormap cm;
XColor red;

typedef struct {
	unsigned int w, h;
	Display *dis;
	int screen;
        Window root, win;
	Drawable drawable;
	GC gc;
  //	XftColor *scheme;
} Drw;
Drw drw;

int w_width = 0;
int w_height = 0;

void
die(const char *fmt, ...) {
	va_list ap;

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);

	if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
		fputc(' ', stderr);
		perror(NULL);
	} else {
		fputc('\n', stderr);
	}

	exit(1);
}

void get_curson_pos(int *x, int *y)
{
  int win_x_return, win_y_return;
  unsigned int mask_return;
  Window root_return, child_return;

  XQueryPointer(drw.dis, drw.root, &root_return, &child_return,
                x, y, &win_x_return, &win_y_return, &mask_return);
}

void init()
{
  XWindowAttributes wa;
  XSetWindowAttributes swa;

  drw.dis = XOpenDisplay(NULL);
  drw.screen = DefaultScreen(drw.dis);
  drw.root = RootWindow(drw.dis, drw.screen);
  if (!XGetWindowAttributes(drw.dis, drw.root, &wa))
    die("could not get embedding window attributes: 0x%lx", drw.root);

  drw.w = wa.width;
  drw.h = wa.height;
  //drw.drawable = XCreatePixmap(drw.dis, drw.root, drw.w, drw.h, DefaultDepth(drw.dis, drw.screen));
  drw.gc = XCreateGC(drw.dis, drw.root, 0, NULL);
  XSetLineAttributes(drw.dis, drw.gc, 1, LineSolid, CapButt, JoinMiter);

  swa.override_redirect = True;
  swa.background_pixel = BlackPixel(drw.dis, drw.screen);
  swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;

  int x, y;
  get_curson_pos(&x, &y);

  int height =  16 + (9 + 3)*w_height - 3 + 16,
      width =  16 + w_width*9;
  drw.win = XCreateWindow(drw.dis, drw.root, x, y, width, height, 0,
                          CopyFromParent, CopyFromParent, CopyFromParent,
                          CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);

  printf("Window pos: {%d, %d}", x, y);
  fflush(stdout);

  cm = DefaultColormap(drw.dis, drw.screen);
  if (! XAllocNamedColor(drw.dis, cm, "red", &red, &red)) {
    fprintf(stderr, "XAllocNamedColor - failed to allocated 'red' color.\n");
    exit(1);
  }
  XMapRaised(drw.dis, drw.win);
  XRaiseWindow(drw.dis, drw.win);
}

void init_x()
{
    /* get the colors black and white (see section for details) */
    unsigned long black,white;

    /* use the information from the environment variable DISPLAY
       to create the X connection:
    */
    dis=XOpenDisplay((char *)0);
    screen=DefaultScreen(dis);
    black=BlackPixel(dis,screen);
    white=WhitePixel(dis, screen);

    /* once the display is initialized, create the window.
       This window will be have be 200 pixels across and 300 down.
       It will have the foreground white and background black
    */
    win=XCreateSimpleWindow(dis,DefaultRootWindow(dis),0,0,
                            200, 300, 1, white, black);

    /* here is where some properties of the window can be set.
       The third and fourth items indicate the name which appears
       at the top of the window and the name of the minimized window
       respectively.
    */
    XSetStandardProperties(dis,win,"xlnch","xlnch",None,NULL,0,NULL);

    XClassHint *hint = XAllocClassHint();
    if (hint) {
        hint->res_name = "xlnch";
        hint->res_class = "xlnch";
    }
    XSetClassHint(dis,win,hint);
    XFree(hint);

    /* this routine determines which types of input are allowed in
       the input.  see the appropriate section for details...
    */
    XSelectInput(dis, win, ExposureMask|ButtonPressMask|KeyPressMask);

    /* create the Graphics Context */
    gc=XCreateGC(dis, win, 0,0);

    /* here is another routine to set the foreground and background
       colors _currently_ in use in the window.
    */
    XSetBackground(dis,gc,black);
    XSetForeground(dis,gc,white);

    cm = DefaultColormap(dis, screen);
    if (! XAllocNamedColor(dis, cm, "red", &red, &red)) {
    	fprintf(stderr, "XAllocNamedColor - failed to allocated 'red' color.\n");
    	exit(1);
    }

    /* Font font = XLoadFont(dis,"*mono*"); */
    /* XSetFont(dis,gc,font); */

    /* clear the window and bring it on top of the other windows */
    XClearWindow(dis, win);
    XMapRaised(dis, win);
}

void close_x()
{
    XFreeGC(drw.dis, drw.gc);
    XDestroyWindow(drw.dis,drw.win);
    XCloseDisplay(drw.dis);
}


#define BUF_SIZE 2048
char buf[BUF_SIZE] = "";

struct lnch_t {
    int exits;
    char key;
    char desc[512];
    char cmd[1024];
};

struct lnch_t lnch[32];
unsigned int idx = 0;

void trim(char * str)
{
    int i;
    int begin = 0;
    int end = strlen(str) - 1;

    while (isspace((unsigned char) str[begin]))
        begin++;

    while (isspace((unsigned char) str[end]) && (end >= begin))
        end--;

    /* Shift all characters back to the start of the string array. */
    for (i = begin; i <= end; i++)
        str[i - begin] = str[i];

    str[i - begin] = '\0';
}

int parse_line(char * line)
{
    char *token;
    char delim[2] = ":";

    lnch[idx].exits = 1;

    if (line[0] == '#')
        return 0;

    if (line[0] == '&') {
        lnch[idx].exits = 0;
        line++;
    }
    
    /* Get key entry */
    token = strtok(line, delim);

    if (strlen(token) != 1) {
        printf("error on token %s", token) ;
        return 1;
    }
    lnch[idx].key = token[0];
    line+=2; // pass the key, pass the `:`

    /* Get description */
    token = strtok(NULL, delim);
    if (!token) return 1;

    w_width = MAX((int) strlen(token), w_width);
    strcpy(lnch[idx].desc, token);
    line+=(strlen(token) + 1 ); // pass the decription, pass the `:`

    /* Get command */
    strcpy(lnch[idx].cmd, line);

    /* If description is epmty command becomes the descritpion */
    if (!strlen(lnch[idx].desc))
      strcpy(lnch[idx].desc, line);

    idx++;
    return 0;
}


int run_cmd(char * cmd)
{
    wordexp_t p;
    int rc = wordexp(cmd, &p, 0);
    switch (rc) {
    case WRDE_BADCHAR:
        printf("Illegal occurrence of newline or one of |, &, ;, <, >, (, ), {, }.\n");
        break;
    case WRDE_BADVAL:
        printf("An undefined shell variable was referenced, and the WRDE_UNDEF flag told us to consider this an error.\n");
        break;
    case WRDE_CMDSUB:
        printf("Command substitution requested, but the WRDE_NOCMD flag told us to consider this an error.\n");
        break;
    case WRDE_NOSPACE:
        printf("Out of memory.\n");
        break;
    case WRDE_SYNTAX:
        printf("Shell syntax error, such as unbalanced parentheses or unmatched quotes.\n");
        break;
    default:
        printf("Running:");
        for (size_t i = 0; i < p.we_wordc; i++) {
            printf(" %s", p.we_wordv[i]);
        }
        printf("\n");

        /* double fork from i3wm's exec implementation */
        if (fork() == 0) {
            setsid();
            if (fork() == 0) {
                execvp(p.we_wordv[0], p.we_wordv);
                /* never reached */
            }
            _exit(EXIT_SUCCESS);
        }
        wait(0);
    }
    wordfree(&p);
    return rc;
}

void grabfocus()
{
        struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000  };
        Window focuswin;
        int i, revertwin;

        for (i = 0; i < 100; ++i) {
                XGetInputFocus(drw.dis, &focuswin, &revertwin);
                if (focuswin == drw.win)
                        return;
                XSetInputFocus(drw.dis, drw.win, RevertToParent, CurrentTime);
                nanosleep(&ts, NULL);
        }
        printf("cannot grab focus\n");
}

void grabkeyboard()
{
        struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000  };
        int i;

        /* try to grab keyboard, we may have to wait for another process to ungrab */
        for (i = 0; i < 1000; i++) {
                if (XGrabKeyboard(drw.dis, drw.root, True, GrabModeAsync,
                                  GrabModeAsync, CurrentTime) == GrabSuccess)
                        return;
                nanosleep(&ts, NULL);
        }
        printf("Cannot grab keyboard\n");
}

void draw_text()
{
    char text[2048];
    int x = 10, y = 10;
    for (unsigned int i = 0; i < idx; i++) {
        if (lnch[i].exits)
            XSetForeground(drw.dis,drw.gc,WhitePixel(drw.dis,drw.screen));
        else
            XSetForeground(drw.dis,drw.gc,red.pixel);
        sprintf(text, "%c: %s", lnch[i].key, lnch[i].desc);
        XDrawString(drw.dis,drw.win,drw.gc,x,y+=13, text, strlen(text));
    }
}

void redraw()
{
    XClearWindow(drw.dis, drw.win);
    draw_text();
}

int main (int argc, char *argv[])
{
    if (argc < 2) {
      printf("Usage:\n%s xlnchrc\n", argv[0]);
      printf("xlnchrc format:\n[&]<key>:<description>:<command>\n");
      return -1;
    }

    FILE *f;

    if (!strcmp(argv[1], "-"))
        f = stdin;
    else {
        f = fopen(argv[1], "r");
        /* printf("xlnchrc required\n"); */
        /* return -1; */
    }

    while (fgets(buf, BUF_SIZE, f)) {
        trim(buf);
        parse_line(buf);
        w_height++;
    }


    if (f != stdin) fclose(f);
  
    XEvent event;		/* the XEvent declaration !!! */
    KeySym key;		/* a dealie-bob to handle KeyPress Events */
    char text[255];		/* a char buffer for KeyPress Events */

    int rc;
    init();
    //    system("sleep 100");
    grabkeyboard();
    grabfocus();

    /* look for events forever... */
    while(1) {
        /* get the next event and stuff it into our event variable.
           Note:  only events we set the mask for are detected!
        */
        XNextEvent(drw.dis, &event);

        if (event.type==Expose && event.xexpose.count==0) {
            /* the window was exposed redraw it! */
            redraw();
        }

        /* Keep window in view */
        if (event.type==VisibilityNotify &&
            event.xvisibility.state != VisibilityUnobscured)
          XRaiseWindow(drw.dis, drw.win);


        if (event.type==KeyPress &&
            XLookupString(&event.xkey,text,255,&key,0)==1) {
            /* use the XLookupString routine to convert the invent
               KeyPress data into regular text.  Weird but necessary...
            */
            if (text[0]=='q') {
                close_x();
                exit(0);
            }
            printf("You pressed the %c key!\n",text[0]);

            for (unsigned int i = 0; i < idx; i++) {
                if (lnch[i].key == text[0]) {
                    rc = run_cmd(lnch[i].cmd);
                    if (lnch[i].exits) {
                        close_x();
                        exit(0);
                    }
                }
            }
        }
        if (event.type==ButtonPress) {
            /* tell where the mouse Button was Pressed */
            int x=event.xbutton.x,
                y=event.xbutton.y;

            strcpy(text,">>xlnch<<");
            XSetForeground(drw.dis,drw.gc,rand()%255);
            XDrawString(drw.dis,drw.win,drw.gc,x,y, text, strlen(text));
        }

        if (event.type==FocusIn) {
            if (event.xfocus.window != win) {
                grabfocus();
            }
        }
    }
}