diff options
author | Anastasios Grammenos <anastasios.grammenos@noris.gr> | 2020-12-02 10:22:38 +0200 |
---|---|---|
committer | Anastasios Grammenos <anastasios.grammenos@noris.gr> | 2020-12-02 10:22:38 +0200 |
commit | 752866d2a12ad63d99a81f6734e5bc8bfd70a538 (patch) | |
tree | cccb0d8b397e88923094772c1a7232a0a9f55cec /xlnch.c | |
parent | d76580bd37c36c53a5f22d358510fc282723efb6 (diff) | |
download | xlnch-752866d2a12ad63d99a81f6734e5bc8bfd70a538.tar.gz xlnch-752866d2a12ad63d99a81f6734e5bc8bfd70a538.tar.bz2 xlnch-752866d2a12ad63d99a81f6734e5bc8bfd70a538.zip |
V2 done
Diffstat (limited to 'xlnch.c')
-rw-r--r-- | xlnch.c | 101 |
1 files changed, 47 insertions, 54 deletions
@@ -37,16 +37,17 @@ #define BUF_SIZE 2048 #define MAX_ENTRIES 32 -char buf[BUF_SIZE] = ""; +#define FONTNAME "-*-terminus-medium-*-*-*-16-*-*-*-*-*-*-*" + +char buf[BUF_SIZE] = "", + cur[BUF_SIZE] = ""; struct lnch_t { int exits; char key; char desc[512]; char cmd[1024]; -}; - -struct lnch_t lnch[MAX_ENTRIES]; +} lnch[MAX_ENTRIES]; unsigned int idx = 0; Colormap cm; @@ -90,7 +91,7 @@ void init() fprintf(stderr, "XAllocNamedColor - failed to allocated 'red' color.\n"); exit(1); } - + swa.override_redirect = True; swa.background_pixel = BlackPixel(display, screen); swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask; @@ -104,11 +105,10 @@ void init() CopyFromParent, CopyFromParent, CopyFromParent, CWOverrideRedirect | CWBackPixel | CWEventMask, &swa); - const char * fontname = "-*-terminus-medium-*-*-*-16-*-*-*-*-*-*-*"; - font = XLoadQueryFont(display, fontname); + font = XLoadQueryFont(display, FONTNAME); /* If the font could not be loaded, revert to the "fixed" font. */ if (!font) { - fprintf (stderr, "unable to load font %s: using fixed\n", fontname); + fprintf (stderr, "unable to load font %s: using fixed\n", FONTNAME); font = XLoadQueryFont (display, "fixed"); } XSetFont(display, gc, font->fid); @@ -129,9 +129,14 @@ int parse_line(char * line) char *token; char delim[2] = ":"; + /* Copy buf to cur for error reporting */ + strcpy(cur, buf); + + trim(buf); + if (strlen(line) == 0 || line[0] == '#') - return 0; + return 2; lnch[idx].exits = 1; @@ -139,7 +144,7 @@ int parse_line(char * line) lnch[idx].exits = 0; line++; } - + /* Get key entry */ token = strtok(line, delim); @@ -247,7 +252,7 @@ void grabkeyboard() void draw_text() { char text[2048]; - int x = 10, y = 10; + int x = 10, y = 2; for (unsigned int i = 0; i < idx; i++) { if (lnch[i].exits) { XSetForeground(display,gc,WhitePixel(display,screen)); @@ -260,75 +265,64 @@ void draw_text() } } -void redraw() -{ - XClearWindow(display, win); - draw_text(); -} - int run() { - XEvent event; /* the XEvent declaration !!! */ - KeySym key; /* a dealie-bob to handle KeyPress Events */ - char text[255]; /* a char buffer for KeyPress Events */ + XEvent event; + KeySym keysym; + char text[255]; + int flag, rc; - int rc; init(); draw_text(); 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(display, &event); if (event.type==Expose && event.xexpose.count==0) { /* the window was exposed redraw it! */ - redraw(); + XClearWindow(display, win); + draw_text(); } /* Keep window in view */ - if (event.type==VisibilityNotify && + else if (event.type==VisibilityNotify && event.xvisibility.state != VisibilityUnobscured) - XRaiseWindow(display, win); + XRaiseWindow(display, 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') { + else if (event.type==KeyPress) { + /* C-g and ESC quit */ + if ((event.xkey.state == 4 && event.xkey.keycode == 42) /* => C-g */ + || event.xkey.keycode == 9) { /* => ESC */ close_x(); return 0; } - printf("You pressed the %c key!\n",text[0]); + + XLookupString(&event.xkey, text, 255, &keysym, 0); + flag = 0; for (unsigned int i = 0; i < idx; i++) { if (lnch[i].key == text[0]) { rc = run_cmd(lnch[i].cmd); + flag = 1; if (lnch[i].exits) { close_x(); return rc; } } } - } - if (event.type==ButtonPress) { - /* tell where the mouse Button was Pressed */ - int x=event.xbutton.x, - y=event.xbutton.y; - - strcpy(text,">>xlnch<<"); - XSetForeground(display,gc,rand()%255); - XDrawString(display,win,gc,x,y, text, strlen(text)); + + /* Q and q quit as well, as long as they don't match anything */ + if (!flag + && event.xkey.keycode == 24) { /* Q or q */ + close_x(); + return 0; + } } - if (event.type==FocusIn) { + else if (event.type==FocusIn) { if (event.xfocus.window != win) { grabfocus(); } @@ -338,6 +332,9 @@ int run() int main (int argc, char *argv[]) { + int rc, n = 1; + FILE *f; + if ((argc > 1) && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h") || @@ -347,7 +344,6 @@ int main (int argc, char *argv[]) return 0; } - FILE *f; if (argc == 1 || !strcmp(argv[1], "-")) f = stdin; else { @@ -356,16 +352,13 @@ int main (int argc, char *argv[]) die("Cannot open file %s", argv[1]); } - int n = 1; - char tmp[BUF_SIZE] = ""; while (fgets(buf, BUF_SIZE, f)) { - trim(buf); - strcpy(tmp, buf); - if (parse_line(buf)) { + rc = parse_line(buf); + if (rc == 1) { fprintf(stderr, "%s:%d: cannot parse ā%sā\n", - argc == 1 || !strcmp(argv[1], "-") ? "stdin": argv[1], n, tmp); + argc == 1 || !strcmp(argv[1], "-") ? "stdin": argv[1], n, cur); } - else { + else if (rc == 0) { w_height++; } n++; |