diff options
Diffstat (limited to 'xlnch.c')
-rw-r--r-- | xlnch.c | 213 |
1 files changed, 51 insertions, 162 deletions
@@ -26,7 +26,6 @@ #include <X11/Xos.h> #include <stdio.h> -#include <stdarg.h> #include <stdlib.h> #include <string.h> #include <ctype.h> @@ -36,47 +35,36 @@ #include <wordexp.h> -#define MAX(A, B) ((A) > (B) ? (A) : (B)) +#include "util.h" -Display *dis; -int screen; -Window win; -GC gc; +#define BUF_SIZE 2048 +#define MAX_ENTRIES 32 + +char buf[BUF_SIZE] = ""; + +struct lnch_t { + int exits; + char key; + char desc[512]; + char cmd[1024]; +}; + +struct lnch_t lnch[MAX_ENTRIES]; +unsigned int idx = 0; 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; +Display *display; +int screen; +Window root, win; +//Drawable drawable; +GC gc; +// XftColor *scheme; 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) { @@ -84,7 +72,7 @@ void get_curson_pos(int *x, int *y) unsigned int mask_return; Window root_return, child_return; - XQueryPointer(drw.dis, drw.root, &root_return, &child_return, + XQueryPointer(display, root, &root_return, &child_return, x, y, &win_x_return, &win_y_return, &mask_return); } @@ -93,20 +81,17 @@ 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); + display = XOpenDisplay(NULL); + screen = DefaultScreen(display); + root = RootWindow(display, screen); + if (!XGetWindowAttributes(display, root, &wa)) + die("could not get embedding window attributes: 0x%lx", 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); + gc = XCreateGC(display, root, 0, NULL); + XSetLineAttributes(display, gc, 1, LineSolid, CapButt, JoinMiter); swa.override_redirect = True; - swa.background_pixel = BlackPixel(drw.dis, drw.screen); + swa.background_pixel = BlackPixel(display, screen); swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask; int x, y; @@ -114,123 +99,27 @@ void init() 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, + win = XCreateWindow(display, 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)) { + cm = DefaultColormap(display, screen); + if (! XAllocNamedColor(display, 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); + XMapRaised(display, win); + XRaiseWindow(display, 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'; + XFreeGC(display, gc); + XDestroyWindow(display,win); + XCloseDisplay(display); } int parse_line(char * line) @@ -327,10 +216,10 @@ void grabfocus() int i, revertwin; for (i = 0; i < 100; ++i) { - XGetInputFocus(drw.dis, &focuswin, &revertwin); - if (focuswin == drw.win) + XGetInputFocus(display, &focuswin, &revertwin); + if (focuswin == win) return; - XSetInputFocus(drw.dis, drw.win, RevertToParent, CurrentTime); + XSetInputFocus(display, win, RevertToParent, CurrentTime); nanosleep(&ts, NULL); } printf("cannot grab focus\n"); @@ -343,7 +232,7 @@ void grabkeyboard() /* 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, + if (XGrabKeyboard(display, root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) return; nanosleep(&ts, NULL); @@ -357,17 +246,17 @@ void draw_text() 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)); + XSetForeground(display,gc,WhitePixel(display,screen)); else - XSetForeground(drw.dis,drw.gc,red.pixel); + XSetForeground(display,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)); + XDrawString(display,win,gc,x,y+=13, text, strlen(text)); } } void redraw() { - XClearWindow(drw.dis, drw.win); + XClearWindow(display, win); draw_text(); } @@ -413,7 +302,7 @@ int main (int argc, char *argv[]) /* 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); + XNextEvent(display, &event); if (event.type==Expose && event.xexpose.count==0) { /* the window was exposed redraw it! */ @@ -423,7 +312,7 @@ int main (int argc, char *argv[]) /* Keep window in view */ if (event.type==VisibilityNotify && event.xvisibility.state != VisibilityUnobscured) - XRaiseWindow(drw.dis, drw.win); + XRaiseWindow(display, win); if (event.type==KeyPress && @@ -442,7 +331,7 @@ int main (int argc, char *argv[]) rc = run_cmd(lnch[i].cmd); if (lnch[i].exits) { close_x(); - exit(0); + exit(rc); } } } @@ -453,8 +342,8 @@ int main (int argc, char *argv[]) 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)); + XSetForeground(display,gc,rand()%255); + XDrawString(display,win,gc,x,y, text, strlen(text)); } if (event.type==FocusIn) { |