rearranged several stuff
50772070
6 file(s) · +219 −269
| 1 | 1 | MIT/X Consortium License |
|
| 2 | 2 | ||
| 3 | 3 | (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com> |
|
| 4 | + | (C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com> |
|
| 4 | 5 | ||
| 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a |
|
| 6 | 7 | copy of this software and associated documentation files (the "Software"), |
| 1 | - | # dwm version |
|
| 1 | + | # dmenu version |
|
| 2 | 2 | VERSION = 0.0 |
|
| 3 | 3 | ||
| 4 | 4 | # Customize below to fit your system |
| 7 | 7 | #include <X11/Xlib.h> |
|
| 8 | 8 | #include <X11/Xlocale.h> |
|
| 9 | 9 | ||
| 10 | + | #define SPACE 30 /* px */ |
|
| 11 | + | ||
| 10 | 12 | typedef struct Brush Brush; |
|
| 11 | 13 | typedef struct DC DC; |
|
| 12 | 14 | typedef struct Fnt Fnt; |
|
| 29 | 31 | GC gc; |
|
| 30 | 32 | }; |
|
| 31 | 33 | ||
| 32 | - | struct Brush { |
|
| 33 | - | GC gc; |
|
| 34 | - | Drawable drawable; |
|
| 35 | - | int x, y, w, h; |
|
| 36 | - | Fnt font; |
|
| 37 | - | unsigned long bg; |
|
| 38 | - | unsigned long fg; |
|
| 39 | - | unsigned long border; |
|
| 40 | - | }; |
|
| 41 | - | ||
| 42 | - | ||
| 34 | + | extern int screen; |
|
| 35 | + | extern Display *dpy; |
|
| 36 | + | extern DC dc; |
|
| 43 | 37 | ||
| 44 | 38 | /* draw.c */ |
|
| 45 | - | extern void draw(Display *dpy, Brush *b, Bool border, const char *text); |
|
| 46 | - | extern void loadcolors(Display *dpy, int screen, Brush *b, |
|
| 47 | - | const char *bg, const char *fg, const char *bo); |
|
| 48 | - | extern void loadfont(Display *dpy, Fnt *font, const char *fontstr); |
|
| 49 | - | extern unsigned int textnw(Fnt *font, char *text, unsigned int len); |
|
| 50 | - | extern unsigned int textw(Fnt *font, char *text); |
|
| 51 | - | extern unsigned int texth(Fnt *font); |
|
| 39 | + | extern void drawtext(const char *text, Bool invert, Bool border); |
|
| 40 | + | extern unsigned long getcolor(const char *colstr); |
|
| 41 | + | extern void setfont(const char *fontstr); |
|
| 42 | + | extern unsigned int textw(const char *text); |
|
| 52 | 43 | ||
| 53 | 44 | /* util.c */ |
|
| 54 | 45 | extern void *emalloc(unsigned int size); |
|
| 55 | - | extern void *emallocz(unsigned int size); |
|
| 56 | 46 | extern void eprint(const char *errstr, ...); |
|
| 57 | 47 | extern char *estrdup(const char *str); |
|
| 58 | - | extern void swap(void **p1, void **p2); |
|
| 2 | 2 | * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> |
|
| 3 | 3 | * See LICENSE file for license details. |
|
| 4 | 4 | */ |
|
| 5 | - | ||
| 5 | + | #include "dmenu.h" |
|
| 6 | 6 | #include <stdio.h> |
|
| 7 | 7 | #include <string.h> |
|
| 8 | + | #include <X11/Xlocale.h> |
|
| 8 | 9 | ||
| 9 | - | #include "dmenu.h" |
|
| 10 | + | /* static */ |
|
| 10 | 11 | ||
| 11 | 12 | static void |
|
| 12 | - | drawborder(Display *dpy, Brush *b) |
|
| 13 | + | drawborder(void) |
|
| 13 | 14 | { |
|
| 14 | 15 | XPoint points[5]; |
|
| 15 | - | XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter); |
|
| 16 | - | XSetForeground(dpy, b->gc, b->border); |
|
| 17 | - | points[0].x = b->x; |
|
| 18 | - | points[0].y = b->y; |
|
| 19 | - | points[1].x = b->w - 1; |
|
| 16 | + | ||
| 17 | + | XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter); |
|
| 18 | + | XSetForeground(dpy, dc.gc, dc.border); |
|
| 19 | + | points[0].x = dc.x; |
|
| 20 | + | points[0].y = dc.y; |
|
| 21 | + | points[1].x = dc.w - 1; |
|
| 20 | 22 | points[1].y = 0; |
|
| 21 | 23 | points[2].x = 0; |
|
| 22 | - | points[2].y = b->h - 1; |
|
| 23 | - | points[3].x = -(b->w - 1); |
|
| 24 | + | points[2].y = dc.h - 1; |
|
| 25 | + | points[3].x = -(dc.w - 1); |
|
| 24 | 26 | points[3].y = 0; |
|
| 25 | 27 | points[4].x = 0; |
|
| 26 | - | points[4].y = -(b->h - 1); |
|
| 27 | - | XDrawLines(dpy, b->drawable, b->gc, points, 5, CoordModePrevious); |
|
| 28 | + | points[4].y = -(dc.h - 1); |
|
| 29 | + | XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious); |
|
| 30 | + | } |
|
| 31 | + | ||
| 32 | + | static unsigned int |
|
| 33 | + | textnw(const char *text, unsigned int len) |
|
| 34 | + | { |
|
| 35 | + | XRectangle r; |
|
| 36 | + | ||
| 37 | + | if(dc.font.set) { |
|
| 38 | + | XmbTextExtents(dc.font.set, text, len, NULL, &r); |
|
| 39 | + | return r.width; |
|
| 40 | + | } |
|
| 41 | + | return XTextWidth(dc.font.xfont, text, len); |
|
| 28 | 42 | } |
|
| 29 | 43 | ||
| 44 | + | /* extern */ |
|
| 45 | + | ||
| 30 | 46 | void |
|
| 31 | - | draw(Display *dpy, Brush *b, Bool border, const char *text) |
|
| 47 | + | drawtext(const char *text, Bool invert, Bool border) |
|
| 32 | 48 | { |
|
| 33 | - | unsigned int x, y, w, h, len; |
|
| 49 | + | int x, y, w, h; |
|
| 34 | 50 | static char buf[256]; |
|
| 51 | + | unsigned int len; |
|
| 35 | 52 | XGCValues gcv; |
|
| 36 | - | XRectangle r = { b->x, b->y, b->w, b->h }; |
|
| 53 | + | XRectangle r = { dc.x, dc.y, dc.w, dc.h }; |
|
| 37 | 54 | ||
| 38 | - | XSetForeground(dpy, b->gc, b->bg); |
|
| 39 | - | XFillRectangles(dpy, b->drawable, b->gc, &r, 1); |
|
| 55 | + | XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg); |
|
| 56 | + | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); |
|
| 40 | 57 | ||
| 41 | 58 | w = 0; |
|
| 42 | 59 | if(border) |
|
| 43 | - | drawborder(dpy, b); |
|
| 60 | + | drawborder(); |
|
| 44 | 61 | ||
| 45 | 62 | if(!text) |
|
| 46 | 63 | return; |
|
| 51 | 68 | memcpy(buf, text, len); |
|
| 52 | 69 | buf[len] = 0; |
|
| 53 | 70 | ||
| 54 | - | h = b->font.ascent + b->font.descent; |
|
| 55 | - | y = b->y + (b->h / 2) - (h / 2) + b->font.ascent; |
|
| 56 | - | x = b->x + (h / 2); |
|
| 71 | + | h = dc.font.ascent + dc.font.descent; |
|
| 72 | + | y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; |
|
| 73 | + | x = dc.x + (h / 2); |
|
| 57 | 74 | ||
| 58 | 75 | /* shorten text if necessary */ |
|
| 59 | - | while(len && (w = textnw(&b->font, buf, len)) > b->w - h) |
|
| 76 | + | while(len && (w = textnw(buf, len)) > dc.w - h) |
|
| 60 | 77 | buf[--len] = 0; |
|
| 61 | 78 | ||
| 62 | - | if(w > b->w) |
|
| 79 | + | if(w > dc.w) |
|
| 63 | 80 | return; /* too long */ |
|
| 64 | 81 | ||
| 65 | - | gcv.foreground = b->fg; |
|
| 66 | - | gcv.background = b->bg; |
|
| 67 | - | if(b->font.set) { |
|
| 68 | - | XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv); |
|
| 69 | - | XmbDrawImageString(dpy, b->drawable, b->font.set, b->gc, |
|
| 82 | + | gcv.foreground = invert ? dc.bg : dc.fg; |
|
| 83 | + | gcv.background = invert ? dc.fg : dc.bg; |
|
| 84 | + | if(dc.font.set) { |
|
| 85 | + | XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv); |
|
| 86 | + | XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc, |
|
| 70 | 87 | x, y, buf, len); |
|
| 71 | 88 | } |
|
| 72 | 89 | else { |
|
| 73 | - | gcv.font = b->font.xfont->fid; |
|
| 74 | - | XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv); |
|
| 75 | - | XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len); |
|
| 90 | + | gcv.font = dc.font.xfont->fid; |
|
| 91 | + | XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv); |
|
| 92 | + | XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len); |
|
| 76 | 93 | } |
|
| 77 | 94 | } |
|
| 78 | 95 | ||
| 79 | - | static unsigned long |
|
| 80 | - | xloadcolors(Display *dpy, Colormap cmap, const char *colstr) |
|
| 96 | + | unsigned long |
|
| 97 | + | getcolor(const char *colstr) |
|
| 81 | 98 | { |
|
| 99 | + | Colormap cmap = DefaultColormap(dpy, screen); |
|
| 82 | 100 | XColor color; |
|
| 101 | + | ||
| 83 | 102 | XAllocNamedColor(dpy, cmap, colstr, &color, &color); |
|
| 84 | 103 | return color.pixel; |
|
| 85 | 104 | } |
|
| 86 | 105 | ||
| 87 | 106 | void |
|
| 88 | - | loadcolors(Display *dpy, int screen, Brush *b, |
|
| 89 | - | const char *bg, const char *fg, const char *border) |
|
| 90 | - | { |
|
| 91 | - | Colormap cmap = DefaultColormap(dpy, screen); |
|
| 92 | - | b->bg = xloadcolors(dpy, cmap, bg); |
|
| 93 | - | b->fg = xloadcolors(dpy, cmap, fg); |
|
| 94 | - | b->border = xloadcolors(dpy, cmap, border); |
|
| 95 | - | } |
|
| 96 | - | ||
| 97 | - | unsigned int |
|
| 98 | - | textnw(Fnt *font, char *text, unsigned int len) |
|
| 99 | - | { |
|
| 100 | - | XRectangle r; |
|
| 101 | - | if(font->set) { |
|
| 102 | - | XmbTextExtents(font->set, text, len, NULL, &r); |
|
| 103 | - | return r.width; |
|
| 104 | - | } |
|
| 105 | - | return XTextWidth(font->xfont, text, len); |
|
| 106 | - | } |
|
| 107 | - | ||
| 108 | - | unsigned int |
|
| 109 | - | textw(Fnt *font, char *text) |
|
| 110 | - | { |
|
| 111 | - | return textnw(font, text, strlen(text)); |
|
| 112 | - | } |
|
| 113 | - | ||
| 114 | - | unsigned int |
|
| 115 | - | texth(Fnt *font) |
|
| 116 | - | { |
|
| 117 | - | return font->height + 4; |
|
| 118 | - | } |
|
| 119 | - | ||
| 120 | - | void |
|
| 121 | - | loadfont(Display *dpy, Fnt *font, const char *fontstr) |
|
| 107 | + | setfont(const char *fontstr) |
|
| 122 | 108 | { |
|
| 123 | 109 | char **missing, *def; |
|
| 124 | - | int n; |
|
| 110 | + | int i, n; |
|
| 125 | 111 | ||
| 126 | 112 | missing = NULL; |
|
| 127 | - | def = "?"; |
|
| 128 | 113 | setlocale(LC_ALL, ""); |
|
| 129 | - | if(font->set) |
|
| 130 | - | XFreeFontSet(dpy, font->set); |
|
| 131 | - | font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); |
|
| 114 | + | if(dc.font.set) |
|
| 115 | + | XFreeFontSet(dpy, dc.font.set); |
|
| 116 | + | dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); |
|
| 132 | 117 | if(missing) { |
|
| 133 | 118 | while(n--) |
|
| 134 | 119 | fprintf(stderr, "missing fontset: %s\n", missing[n]); |
|
| 135 | 120 | XFreeStringList(missing); |
|
| 136 | - | if(font->set) { |
|
| 137 | - | XFreeFontSet(dpy, font->set); |
|
| 138 | - | font->set = NULL; |
|
| 121 | + | if(dc.font.set) { |
|
| 122 | + | XFreeFontSet(dpy, dc.font.set); |
|
| 123 | + | dc.font.set = NULL; |
|
| 139 | 124 | } |
|
| 140 | 125 | } |
|
| 141 | - | if(font->set) { |
|
| 126 | + | if(dc.font.set) { |
|
| 142 | 127 | XFontSetExtents *font_extents; |
|
| 143 | 128 | XFontStruct **xfonts; |
|
| 144 | 129 | char **font_names; |
|
| 145 | - | unsigned int i; |
|
| 146 | 130 | ||
| 147 | - | font->ascent = font->descent = 0; |
|
| 148 | - | font_extents = XExtentsOfFontSet(font->set); |
|
| 149 | - | n = XFontsOfFontSet(font->set, &xfonts, &font_names); |
|
| 150 | - | for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) { |
|
| 151 | - | if(font->ascent < (*xfonts)->ascent) |
|
| 152 | - | font->ascent = (*xfonts)->ascent; |
|
| 153 | - | if(font->descent < (*xfonts)->descent) |
|
| 154 | - | font->descent = (*xfonts)->descent; |
|
| 131 | + | dc.font.ascent = dc.font.descent = 0; |
|
| 132 | + | font_extents = XExtentsOfFontSet(dc.font.set); |
|
| 133 | + | n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names); |
|
| 134 | + | for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) { |
|
| 135 | + | if(dc.font.ascent < (*xfonts)->ascent) |
|
| 136 | + | dc.font.ascent = (*xfonts)->ascent; |
|
| 137 | + | if(dc.font.descent < (*xfonts)->descent) |
|
| 138 | + | dc.font.descent = (*xfonts)->descent; |
|
| 155 | 139 | xfonts++; |
|
| 156 | 140 | } |
|
| 157 | 141 | } |
|
| 158 | 142 | else { |
|
| 159 | - | if(font->xfont) |
|
| 160 | - | XFreeFont(dpy, font->xfont); |
|
| 161 | - | font->xfont = NULL; |
|
| 162 | - | font->xfont = XLoadQueryFont(dpy, fontstr); |
|
| 163 | - | if (!font->xfont) |
|
| 164 | - | font->xfont = XLoadQueryFont(dpy, "fixed"); |
|
| 165 | - | if (!font->xfont) |
|
| 166 | - | eprint("error, cannot load 'fixed' font\n"); |
|
| 167 | - | font->ascent = font->xfont->ascent; |
|
| 168 | - | font->descent = font->xfont->descent; |
|
| 143 | + | if(dc.font.xfont) |
|
| 144 | + | XFreeFont(dpy, dc.font.xfont); |
|
| 145 | + | dc.font.xfont = NULL; |
|
| 146 | + | dc.font.xfont = XLoadQueryFont(dpy, fontstr); |
|
| 147 | + | if (!dc.font.xfont) |
|
| 148 | + | dc.font.xfont = XLoadQueryFont(dpy, "fixed"); |
|
| 149 | + | if (!dc.font.xfont) |
|
| 150 | + | eprint("error, cannot init 'fixed' font\n"); |
|
| 151 | + | dc.font.ascent = dc.font.xfont->ascent; |
|
| 152 | + | dc.font.descent = dc.font.xfont->descent; |
|
| 169 | 153 | } |
|
| 170 | - | font->height = font->ascent + font->descent; |
|
| 154 | + | dc.font.height = dc.font.ascent + dc.font.descent; |
|
| 155 | + | } |
|
| 156 | + | ||
| 157 | + | unsigned int |
|
| 158 | + | textw(const char *text) |
|
| 159 | + | { |
|
| 160 | + | return textnw(text, strlen(text)) + dc.font.height; |
|
| 171 | 161 | } |
|
| 16 | 16 | #include <X11/keysym.h> |
|
| 17 | 17 | ||
| 18 | 18 | typedef struct Item Item; |
|
| 19 | - | ||
| 20 | 19 | struct Item { |
|
| 21 | 20 | Item *next; /* traverses all items */ |
|
| 22 | 21 | Item *left, *right; /* traverses items matching current search pattern */ |
|
| 23 | 22 | char *text; |
|
| 24 | 23 | }; |
|
| 25 | 24 | ||
| 26 | - | static Display *dpy; |
|
| 27 | - | static Window root; |
|
| 28 | - | static Window win; |
|
| 29 | - | static Bool done = False; |
|
| 30 | - | ||
| 31 | - | static Item *allitem = NULL; /* first of all items */ |
|
| 32 | - | static Item *item = NULL; /* first of pattern matching items */ |
|
| 33 | - | static Item *sel = NULL; |
|
| 34 | - | static Item *nextoff = NULL; |
|
| 35 | - | static Item *prevoff = NULL; |
|
| 36 | - | static Item *curroff = NULL; |
|
| 25 | + | /* static */ |
|
| 37 | 26 | ||
| 38 | - | static int screen, mx, my, mw, mh; |
|
| 39 | - | static char *title = NULL; |
|
| 40 | - | static char text[4096]; |
|
| 27 | + | static char *title, text[4096]; |
|
| 28 | + | static int mx, my, mw, mh; |
|
| 41 | 29 | static int ret = 0; |
|
| 42 | 30 | static int nitem = 0; |
|
| 43 | 31 | static unsigned int cmdw = 0; |
|
| 44 | 32 | static unsigned int tw = 0; |
|
| 45 | 33 | static unsigned int cw = 0; |
|
| 46 | - | static const int seek = 30; /* 30px */ |
|
| 47 | - | ||
| 48 | - | static Brush brush = {0}; |
|
| 49 | - | ||
| 50 | - | static void draw_menu(); |
|
| 51 | - | static void kpress(XKeyEvent * e); |
|
| 52 | - | ||
| 53 | - | static char version[] = "dmenu - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n"; |
|
| 34 | + | static Bool done = False; |
|
| 35 | + | static Item *allitems = NULL; /* first of all items */ |
|
| 36 | + | static Item *item = NULL; /* first of pattern matching items */ |
|
| 37 | + | static Item *sel = NULL; |
|
| 38 | + | static Item *next = NULL; |
|
| 39 | + | static Item *prev = NULL; |
|
| 40 | + | static Item *curr = NULL; |
|
| 41 | + | static Window root; |
|
| 42 | + | static Window win; |
|
| 54 | 43 | ||
| 55 | 44 | static void |
|
| 56 | - | update_offsets() |
|
| 45 | + | calcoffsets() |
|
| 57 | 46 | { |
|
| 58 | - | unsigned int tw, w = cmdw + 2 * seek; |
|
| 47 | + | unsigned int tw, w; |
|
| 59 | 48 | ||
| 60 | - | if(!curroff) |
|
| 49 | + | if(!curr) |
|
| 61 | 50 | return; |
|
| 62 | 51 | ||
| 63 | - | for(nextoff = curroff; nextoff; nextoff=nextoff->right) { |
|
| 64 | - | tw = textw(&brush.font, nextoff->text); |
|
| 52 | + | w = cmdw + 2 * SPACE; |
|
| 53 | + | for(next = curr; next; next=next->right) { |
|
| 54 | + | tw = textw(next->text); |
|
| 65 | 55 | if(tw > mw / 3) |
|
| 66 | 56 | tw = mw / 3; |
|
| 67 | - | w += tw + brush.font.height; |
|
| 57 | + | w += tw; |
|
| 68 | 58 | if(w > mw) |
|
| 69 | 59 | break; |
|
| 70 | 60 | } |
|
| 71 | 61 | ||
| 72 | - | w = cmdw + 2 * seek; |
|
| 73 | - | for(prevoff = curroff; prevoff && prevoff->left; prevoff=prevoff->left) { |
|
| 74 | - | tw = textw(&brush.font, prevoff->left->text); |
|
| 62 | + | w = cmdw + 2 * SPACE; |
|
| 63 | + | for(prev = curr; prev && prev->left; prev=prev->left) { |
|
| 64 | + | tw = textw(prev->left->text); |
|
| 75 | 65 | if(tw > mw / 3) |
|
| 76 | 66 | tw = mw / 3; |
|
| 77 | - | w += tw + brush.font.height; |
|
| 67 | + | w += tw; |
|
| 78 | 68 | if(w > mw) |
|
| 79 | 69 | break; |
|
| 80 | 70 | } |
|
| 81 | 71 | } |
|
| 82 | 72 | ||
| 83 | 73 | static void |
|
| 84 | - | update_items(char *pattern) |
|
| 74 | + | drawmenu() |
|
| 85 | 75 | { |
|
| 86 | - | unsigned int plen = strlen(pattern); |
|
| 76 | + | Item *i; |
|
| 77 | + | ||
| 78 | + | dc.x = 0; |
|
| 79 | + | dc.y = 0; |
|
| 80 | + | dc.w = mw; |
|
| 81 | + | dc.h = mh; |
|
| 82 | + | drawtext(NULL, False, False); |
|
| 83 | + | ||
| 84 | + | /* print command */ |
|
| 85 | + | if(!title || text[0]) { |
|
| 86 | + | cmdw = cw; |
|
| 87 | + | if(cmdw && item) |
|
| 88 | + | dc.w = cmdw; |
|
| 89 | + | drawtext(text, False, False); |
|
| 90 | + | } |
|
| 91 | + | else { |
|
| 92 | + | cmdw = tw; |
|
| 93 | + | dc.w = cmdw; |
|
| 94 | + | drawtext(title, False, False); |
|
| 95 | + | } |
|
| 96 | + | dc.x += dc.w; |
|
| 97 | + | ||
| 98 | + | if(curr) { |
|
| 99 | + | dc.w = SPACE; |
|
| 100 | + | drawtext((curr && curr->left) ? "<" : NULL, False, False); |
|
| 101 | + | dc.x += dc.w; |
|
| 102 | + | ||
| 103 | + | /* determine maximum items */ |
|
| 104 | + | for(i = curr; i != next; i=i->right) { |
|
| 105 | + | dc.border = False; |
|
| 106 | + | dc.w = textw(i->text); |
|
| 107 | + | if(dc.w > mw / 3) |
|
| 108 | + | dc.w = mw / 3; |
|
| 109 | + | drawtext(i->text, sel == i, sel == i); |
|
| 110 | + | dc.x += dc.w; |
|
| 111 | + | } |
|
| 112 | + | ||
| 113 | + | dc.x = mw - SPACE; |
|
| 114 | + | dc.w = SPACE; |
|
| 115 | + | drawtext(next ? ">" : NULL, False, False); |
|
| 116 | + | } |
|
| 117 | + | XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0); |
|
| 118 | + | XFlush(dpy); |
|
| 119 | + | } |
|
| 120 | + | ||
| 121 | + | static void |
|
| 122 | + | input(char *pattern) |
|
| 123 | + | { |
|
| 124 | + | unsigned int plen; |
|
| 87 | 125 | Item *i, *j; |
|
| 88 | 126 | ||
| 89 | 127 | if(!pattern) |
|
| 94 | 132 | else |
|
| 95 | 133 | cmdw = tw; |
|
| 96 | 134 | ||
| 135 | + | plen = strlen(pattern); |
|
| 97 | 136 | item = j = NULL; |
|
| 98 | 137 | nitem = 0; |
|
| 99 | 138 | ||
| 100 | - | for(i = allitem; i; i=i->next) |
|
| 139 | + | for(i = allitems; i; i=i->next) |
|
| 101 | 140 | if(!plen || !strncmp(pattern, i->text, plen)) { |
|
| 102 | 141 | if(!j) |
|
| 103 | 142 | item = i; |
|
| 108 | 147 | j = i; |
|
| 109 | 148 | nitem++; |
|
| 110 | 149 | } |
|
| 111 | - | for(i = allitem; i; i=i->next) |
|
| 150 | + | for(i = allitems; i; i=i->next) |
|
| 112 | 151 | if(plen && strncmp(pattern, i->text, plen) |
|
| 113 | 152 | && strstr(i->text, pattern)) { |
|
| 114 | 153 | if(!j) |
|
| 121 | 160 | nitem++; |
|
| 122 | 161 | } |
|
| 123 | 162 | ||
| 124 | - | curroff = prevoff = nextoff = sel = item; |
|
| 125 | - | ||
| 126 | - | update_offsets(); |
|
| 127 | - | } |
|
| 128 | - | ||
| 129 | - | /* creates brush structs for brush mode drawing */ |
|
| 130 | - | static void |
|
| 131 | - | draw_menu() |
|
| 132 | - | { |
|
| 133 | - | Item *i; |
|
| 134 | - | ||
| 135 | - | brush.x = 0; |
|
| 136 | - | brush.y = 0; |
|
| 137 | - | brush.w = mw; |
|
| 138 | - | brush.h = mh; |
|
| 139 | - | draw(dpy, &brush, False, 0); |
|
| 140 | - | ||
| 141 | - | /* print command */ |
|
| 142 | - | if(!title || text[0]) { |
|
| 143 | - | cmdw = cw; |
|
| 144 | - | if(cmdw && item) |
|
| 145 | - | brush.w = cmdw; |
|
| 146 | - | draw(dpy, &brush, False, text); |
|
| 147 | - | } |
|
| 148 | - | else { |
|
| 149 | - | cmdw = tw; |
|
| 150 | - | brush.w = cmdw; |
|
| 151 | - | draw(dpy, &brush, False, title); |
|
| 152 | - | } |
|
| 153 | - | brush.x += brush.w; |
|
| 154 | - | ||
| 155 | - | if(curroff) { |
|
| 156 | - | brush.w = seek; |
|
| 157 | - | draw(dpy, &brush, False, (curroff && curroff->left) ? "<" : 0); |
|
| 158 | - | brush.x += brush.w; |
|
| 159 | - | ||
| 160 | - | /* determine maximum items */ |
|
| 161 | - | for(i = curroff; i != nextoff; i=i->right) { |
|
| 162 | - | brush.border = False; |
|
| 163 | - | brush.w = textw(&brush.font, i->text); |
|
| 164 | - | if(brush.w > mw / 3) |
|
| 165 | - | brush.w = mw / 3; |
|
| 166 | - | brush.w += brush.font.height; |
|
| 167 | - | if(sel == i) { |
|
| 168 | - | swap((void **)&brush.fg, (void **)&brush.bg); |
|
| 169 | - | draw(dpy, &brush, True, i->text); |
|
| 170 | - | swap((void **)&brush.fg, (void **)&brush.bg); |
|
| 171 | - | } |
|
| 172 | - | else |
|
| 173 | - | draw(dpy, &brush, False, i->text); |
|
| 174 | - | brush.x += brush.w; |
|
| 175 | - | } |
|
| 176 | - | ||
| 177 | - | brush.x = mw - seek; |
|
| 178 | - | brush.w = seek; |
|
| 179 | - | draw(dpy, &brush, False, nextoff ? ">" : 0); |
|
| 180 | - | } |
|
| 181 | - | XCopyArea(dpy, brush.drawable, win, brush.gc, 0, 0, mw, mh, 0, 0); |
|
| 182 | - | XFlush(dpy); |
|
| 163 | + | curr = prev = next = sel = item; |
|
| 164 | + | calcoffsets(); |
|
| 183 | 165 | } |
|
| 184 | 166 | ||
| 185 | 167 | static void |
|
| 186 | 168 | kpress(XKeyEvent * e) |
|
| 187 | 169 | { |
|
| 188 | - | KeySym ksym; |
|
| 189 | 170 | char buf[32]; |
|
| 190 | 171 | int num, prev_nitem; |
|
| 191 | - | unsigned int i, len = strlen(text); |
|
| 172 | + | unsigned int i, len; |
|
| 173 | + | KeySym ksym; |
|
| 192 | 174 | ||
| 175 | + | len = strlen(text); |
|
| 193 | 176 | buf[0] = 0; |
|
| 194 | 177 | num = XLookupString(e, buf, sizeof(buf), &ksym, 0); |
|
| 195 | 178 | ||
| 210 | 193 | case XK_U: |
|
| 211 | 194 | case XK_u: |
|
| 212 | 195 | text[0] = 0; |
|
| 213 | - | update_items(text); |
|
| 214 | - | draw_menu(); |
|
| 196 | + | input(text); |
|
| 197 | + | drawmenu(); |
|
| 215 | 198 | return; |
|
| 216 | 199 | break; |
|
| 217 | 200 | case XK_bracketleft: |
|
| 224 | 207 | if(!(sel && sel->left)) |
|
| 225 | 208 | return; |
|
| 226 | 209 | sel=sel->left; |
|
| 227 | - | if(sel->right == curroff) { |
|
| 228 | - | curroff = prevoff; |
|
| 229 | - | update_offsets(); |
|
| 210 | + | if(sel->right == curr) { |
|
| 211 | + | curr = prev; |
|
| 212 | + | calcoffsets(); |
|
| 230 | 213 | } |
|
| 231 | 214 | break; |
|
| 232 | 215 | case XK_Tab: |
|
| 233 | 216 | if(!sel) |
|
| 234 | 217 | return; |
|
| 235 | 218 | strncpy(text, sel->text, sizeof(text)); |
|
| 236 | - | update_items(text); |
|
| 219 | + | input(text); |
|
| 237 | 220 | break; |
|
| 238 | 221 | case XK_Right: |
|
| 239 | 222 | if(!(sel && sel->right)) |
|
| 240 | 223 | return; |
|
| 241 | 224 | sel=sel->right; |
|
| 242 | - | if(sel == nextoff) { |
|
| 243 | - | curroff = nextoff; |
|
| 244 | - | update_offsets(); |
|
| 225 | + | if(sel == next) { |
|
| 226 | + | curr = next; |
|
| 227 | + | calcoffsets(); |
|
| 245 | 228 | } |
|
| 246 | 229 | break; |
|
| 247 | 230 | case XK_Return: |
|
| 265 | 248 | prev_nitem = nitem; |
|
| 266 | 249 | do { |
|
| 267 | 250 | text[--i] = 0; |
|
| 268 | - | update_items(text); |
|
| 251 | + | input(text); |
|
| 269 | 252 | } while(i && nitem && prev_nitem == nitem); |
|
| 270 | - | update_items(text); |
|
| 253 | + | input(text); |
|
| 271 | 254 | } |
|
| 272 | 255 | break; |
|
| 273 | 256 | default: |
|
| 277 | 260 | strncat(text, buf, sizeof(text)); |
|
| 278 | 261 | else |
|
| 279 | 262 | strncpy(text, buf, sizeof(text)); |
|
| 280 | - | update_items(text); |
|
| 263 | + | input(text); |
|
| 281 | 264 | } |
|
| 282 | 265 | } |
|
| 283 | - | draw_menu(); |
|
| 266 | + | drawmenu(); |
|
| 284 | 267 | } |
|
| 285 | 268 | ||
| 286 | 269 | static char * |
|
| 287 | - | read_allitems() |
|
| 270 | + | readinput() |
|
| 288 | 271 | { |
|
| 289 | 272 | static char *maxname = NULL; |
|
| 290 | 273 | char *p, buf[1024]; |
|
| 306 | 289 | new->next = new->left = new->right = NULL; |
|
| 307 | 290 | new->text = p; |
|
| 308 | 291 | if(!i) |
|
| 309 | - | allitem = new; |
|
| 292 | + | allitems = new; |
|
| 310 | 293 | else |
|
| 311 | 294 | i->next = new; |
|
| 312 | 295 | i = new; |
|
| 315 | 298 | return maxname; |
|
| 316 | 299 | } |
|
| 317 | 300 | ||
| 301 | + | /* extern */ |
|
| 302 | + | ||
| 303 | + | int screen; |
|
| 304 | + | Display *dpy; |
|
| 305 | + | DC dc = {0}; |
|
| 306 | + | ||
| 318 | 307 | int |
|
| 319 | 308 | main(int argc, char *argv[]) |
|
| 320 | 309 | { |
|
| 321 | - | int i; |
|
| 322 | - | XSetWindowAttributes wa; |
|
| 323 | 310 | char *maxname; |
|
| 311 | + | int i; |
|
| 324 | 312 | XEvent ev; |
|
| 313 | + | XSetWindowAttributes wa; |
|
| 325 | 314 | ||
| 326 | 315 | /* command line args */ |
|
| 327 | 316 | for(i = 1; i < argc; i++) { |
|
| 328 | 317 | if (argv[i][0] == '-') |
|
| 329 | 318 | switch (argv[i][1]) { |
|
| 330 | 319 | case 'v': |
|
| 331 | - | fprintf(stdout, "%s", version); |
|
| 332 | - | exit(0); |
|
| 320 | + | fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout); |
|
| 321 | + | exit(EXIT_SUCCESS); |
|
| 333 | 322 | break; |
|
| 334 | 323 | case 't': |
|
| 335 | 324 | if(++i < argc) { |
|
| 350 | 339 | screen = DefaultScreen(dpy); |
|
| 351 | 340 | root = RootWindow(dpy, screen); |
|
| 352 | 341 | ||
| 353 | - | maxname = read_allitems(); |
|
| 342 | + | maxname = readinput(); |
|
| 354 | 343 | ||
| 355 | 344 | /* grab as early as possible, but after reading all items!!! */ |
|
| 356 | 345 | while(XGrabKeyboard(dpy, root, True, GrabModeAsync, |
|
| 358 | 347 | usleep(1000); |
|
| 359 | 348 | ||
| 360 | 349 | /* style */ |
|
| 361 | - | loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR); |
|
| 362 | - | loadfont(dpy, &brush.font, FONT); |
|
| 350 | + | dc.bg = getcolor(BGCOLOR); |
|
| 351 | + | dc.fg = getcolor(FGCOLOR); |
|
| 352 | + | dc.border = getcolor(BORDERCOLOR); |
|
| 353 | + | setfont(FONT); |
|
| 363 | 354 | ||
| 364 | 355 | wa.override_redirect = 1; |
|
| 365 | 356 | wa.background_pixmap = ParentRelative; |
|
| 367 | 358 | ||
| 368 | 359 | mx = my = 0; |
|
| 369 | 360 | mw = DisplayWidth(dpy, screen); |
|
| 370 | - | mh = texth(&brush.font); |
|
| 361 | + | mh = dc.font.height + 4; |
|
| 371 | 362 | ||
| 372 | 363 | win = XCreateWindow(dpy, root, mx, my, mw, mh, 0, |
|
| 373 | 364 | DefaultDepth(dpy, screen), CopyFromParent, |
|
| 374 | 365 | DefaultVisual(dpy, screen), |
|
| 375 | 366 | CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); |
|
| 376 | 367 | XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm)); |
|
| 377 | - | XFlush(dpy); |
|
| 378 | 368 | ||
| 379 | 369 | /* pixmap */ |
|
| 380 | - | brush.gc = XCreateGC(dpy, root, 0, 0); |
|
| 381 | - | brush.drawable = XCreatePixmap(dpy, win, mw, mh, |
|
| 382 | - | DefaultDepth(dpy, screen)); |
|
| 383 | - | XFlush(dpy); |
|
| 370 | + | dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen)); |
|
| 371 | + | dc.gc = XCreateGC(dpy, root, 0, 0); |
|
| 384 | 372 | ||
| 385 | 373 | if(maxname) |
|
| 386 | - | cw = textw(&brush.font, maxname) + brush.font.height; |
|
| 374 | + | cw = textw(maxname); |
|
| 387 | 375 | if(cw > mw / 3) |
|
| 388 | 376 | cw = mw / 3; |
|
| 389 | 377 | ||
| 390 | 378 | if(title) { |
|
| 391 | - | tw = textw(&brush.font, title) + brush.font.height; |
|
| 379 | + | tw = textw(title); |
|
| 392 | 380 | if(tw > mw / 3) |
|
| 393 | 381 | tw = mw / 3; |
|
| 394 | 382 | } |
|
| 396 | 384 | cmdw = title ? tw : cw; |
|
| 397 | 385 | ||
| 398 | 386 | text[0] = 0; |
|
| 399 | - | update_items(text); |
|
| 387 | + | input(text); |
|
| 400 | 388 | XMapRaised(dpy, win); |
|
| 401 | - | draw_menu(); |
|
| 402 | - | XFlush(dpy); |
|
| 389 | + | drawmenu(); |
|
| 390 | + | XSync(dpy, False); |
|
| 403 | 391 | ||
| 404 | 392 | /* main event loop */ |
|
| 405 | 393 | while(!done && !XNextEvent(dpy, &ev)) { |
|
| 409 | 397 | break; |
|
| 410 | 398 | case Expose: |
|
| 411 | 399 | if(ev.xexpose.count == 0) |
|
| 412 | - | draw_menu(); |
|
| 400 | + | drawmenu(); |
|
| 413 | 401 | break; |
|
| 414 | 402 | default: |
|
| 415 | 403 | break; |
|
| 417 | 405 | } |
|
| 418 | 406 | ||
| 419 | 407 | XUngrabKeyboard(dpy, CurrentTime); |
|
| 420 | - | XFreePixmap(dpy, brush.drawable); |
|
| 421 | - | XFreeGC(dpy, brush.gc); |
|
| 408 | + | XFreePixmap(dpy, dc.drawable); |
|
| 409 | + | XFreeGC(dpy, dc.gc); |
|
| 422 | 410 | XDestroyWindow(dpy, win); |
|
| 423 | 411 | XCloseDisplay(dpy); |
|
| 424 | 412 | ||
| 29 | 29 | return res; |
|
| 30 | 30 | } |
|
| 31 | 31 | ||
| 32 | - | void * |
|
| 33 | - | emallocz(unsigned int size) |
|
| 34 | - | { |
|
| 35 | - | void *res = calloc(1, size); |
|
| 36 | - | ||
| 37 | - | if(!res) |
|
| 38 | - | bad_malloc(size); |
|
| 39 | - | return res; |
|
| 40 | - | } |
|
| 41 | - | ||
| 42 | 32 | void |
|
| 43 | 33 | eprint(const char *errstr, ...) |
|
| 44 | 34 | { |
|
| 58 | 48 | bad_malloc(strlen(str)); |
|
| 59 | 49 | return res; |
|
| 60 | 50 | } |
|
| 61 | - | ||
| 62 | - | void |
|
| 63 | - | swap(void **p1, void **p2) |
|
| 64 | - | { |
|
| 65 | - | void *tmp = *p1; |
|
| 66 | - | *p1 = *p2; |
|
| 67 | - | *p2 = tmp; |
|
| 68 | - | } |
|