1 f5e234d6 2018-05-18 omar.polo /* mymenu -- simple dmenu alternative */
3 f5e234d6 2018-05-18 omar.polo /* Copyright (C) 2018 Omar Polo <omar.polo@europecom.net>
5 f5e234d6 2018-05-18 omar.polo * This program is free software: you can redistribute it and/or modify
6 f5e234d6 2018-05-18 omar.polo * it under the terms of the GNU General Public License as published by
7 f5e234d6 2018-05-18 omar.polo * the Free Software Foundation, either version 3 of the License, or
8 f5e234d6 2018-05-18 omar.polo * (at your option) any later version.
10 f5e234d6 2018-05-18 omar.polo * This program is distributed in the hope that it will be useful,
11 f5e234d6 2018-05-18 omar.polo * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 f5e234d6 2018-05-18 omar.polo * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 f5e234d6 2018-05-18 omar.polo * GNU General Public License for more details.
15 f5e234d6 2018-05-18 omar.polo * You should have received a copy of the GNU General Public License
16 f5e234d6 2018-05-18 omar.polo * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 f5e234d6 2018-05-18 omar.polo #include <stdio.h>
20 f5e234d6 2018-05-18 omar.polo #include <stdlib.h>
21 f5e234d6 2018-05-18 omar.polo #include <string.h> // strdup, strnlen, ...
22 f5e234d6 2018-05-18 omar.polo #include <ctype.h> // isalnum
23 f5e234d6 2018-05-18 omar.polo #include <locale.h> // setlocale
24 f5e234d6 2018-05-18 omar.polo #include <unistd.h>
25 f5e234d6 2018-05-18 omar.polo #include <sysexits.h>
26 f5e234d6 2018-05-18 omar.polo #include <stdbool.h>
27 f5e234d6 2018-05-18 omar.polo #include <limits.h>
28 f5e234d6 2018-05-18 omar.polo #include <errno.h>
30 f5e234d6 2018-05-18 omar.polo #include <X11/Xlib.h>
31 f5e234d6 2018-05-18 omar.polo #include <X11/Xutil.h> // XLookupString
32 f5e234d6 2018-05-18 omar.polo #include <X11/Xresource.h>
33 f5e234d6 2018-05-18 omar.polo #include <X11/Xcms.h> // colors
34 347d23da 2018-05-19 omar.polo #include <X11/keysym.h>
36 f5e234d6 2018-05-18 omar.polo #ifdef USE_XINERAMA
37 f5e234d6 2018-05-18 omar.polo # include <X11/extensions/Xinerama.h>
40 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
41 c9a3bfaa 2018-05-21 omar.polo # include <X11/Xft/Xft.h>
44 f5e234d6 2018-05-18 omar.polo #define nil NULL
45 347d23da 2018-05-19 omar.polo #define resname "MyMenu"
46 347d23da 2018-05-19 omar.polo #define resclass "mymenu"
48 7ca8829b 2018-05-21 omar.polo #define MIN(a, b) (((a) < (b)) ? (a) : (b))
49 f5e234d6 2018-05-18 omar.polo #define MAX(a, b) ((a) > (b) ? (a) : (b))
51 f5e234d6 2018-05-18 omar.polo #define update_completions(cs, text, lines) { \
52 f5e234d6 2018-05-18 omar.polo compl_delete(cs); \
53 f5e234d6 2018-05-18 omar.polo cs = filter(text, lines); \
56 95b27a5e 2018-05-19 omar.polo #define INITIAL_ITEMS 64
58 f5e234d6 2018-05-18 omar.polo #define TODO(s) { \
59 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "TODO! " s "\n"); \
62 f5e234d6 2018-05-18 omar.polo #define cannot_allocate_memory { \
63 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not allocate memory\n"); \
64 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE); \
67 f5e234d6 2018-05-18 omar.polo #define check_allocation(a) { \
68 f5e234d6 2018-05-18 omar.polo if (a == nil) \
69 f5e234d6 2018-05-18 omar.polo cannot_allocate_memory; \
72 f5e234d6 2018-05-18 omar.polo enum state {LOOPING, OK, ERR};
74 c9a3bfaa 2018-05-21 omar.polo enum text_type {PROMPT, COMPL, COMPL_HIGH};
76 f5e234d6 2018-05-18 omar.polo struct rendering {
77 f5e234d6 2018-05-18 omar.polo Display *d;
78 f5e234d6 2018-05-18 omar.polo Window w;
79 f5e234d6 2018-05-18 omar.polo GC prompt;
80 f5e234d6 2018-05-18 omar.polo GC prompt_bg;
81 f5e234d6 2018-05-18 omar.polo GC completion;
82 f5e234d6 2018-05-18 omar.polo GC completion_bg;
83 f5e234d6 2018-05-18 omar.polo GC completion_highlighted;
84 f5e234d6 2018-05-18 omar.polo GC completion_highlighted_bg;
85 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
86 c9a3bfaa 2018-05-21 omar.polo XftDraw *xftdraw;
87 c9a3bfaa 2018-05-21 omar.polo XftColor xft_prompt;
88 c9a3bfaa 2018-05-21 omar.polo XftColor xft_completion;
89 c9a3bfaa 2018-05-21 omar.polo XftColor xft_completion_highlighted;
90 c9a3bfaa 2018-05-21 omar.polo XftFont *font;
92 c9a3bfaa 2018-05-21 omar.polo XFontSet *font;
94 f5e234d6 2018-05-18 omar.polo int width;
95 f5e234d6 2018-05-18 omar.polo int height;
96 36a15a9f 2018-05-19 omar.polo bool horizontal_layout;
97 8758854a 2018-05-20 omar.polo char *ps1;
98 8758854a 2018-05-20 omar.polo int ps1len;
101 f5e234d6 2018-05-18 omar.polo struct completions {
102 f5e234d6 2018-05-18 omar.polo char *completion;
103 f5e234d6 2018-05-18 omar.polo bool selected;
104 f5e234d6 2018-05-18 omar.polo struct completions *next;
107 f5e234d6 2018-05-18 omar.polo struct completions *compl_new() {
108 f5e234d6 2018-05-18 omar.polo struct completions *c = malloc(sizeof(struct completions));
110 f5e234d6 2018-05-18 omar.polo if (c == nil)
111 f5e234d6 2018-05-18 omar.polo return c;
113 f5e234d6 2018-05-18 omar.polo c->completion = nil;
114 f5e234d6 2018-05-18 omar.polo c->selected = false;
115 f5e234d6 2018-05-18 omar.polo c->next = nil;
116 f5e234d6 2018-05-18 omar.polo return c;
119 f5e234d6 2018-05-18 omar.polo void compl_delete(struct completions *c) {
120 f5e234d6 2018-05-18 omar.polo free(c);
123 347d23da 2018-05-19 omar.polo struct completions *compl_select_next(struct completions *c, bool n) {
124 347d23da 2018-05-19 omar.polo if (c == nil)
125 347d23da 2018-05-19 omar.polo return nil;
126 347d23da 2018-05-19 omar.polo if (n) {
127 347d23da 2018-05-19 omar.polo c->selected = true;
128 347d23da 2018-05-19 omar.polo return c;
131 347d23da 2018-05-19 omar.polo struct completions *orig = c;
132 347d23da 2018-05-19 omar.polo while (c != nil) {
133 347d23da 2018-05-19 omar.polo if (c->selected) {
134 347d23da 2018-05-19 omar.polo c->selected = false;
135 347d23da 2018-05-19 omar.polo if (c->next != nil) {
136 347d23da 2018-05-19 omar.polo // the current one is selected and the next one exists
137 347d23da 2018-05-19 omar.polo c->next->selected = true;
138 347d23da 2018-05-19 omar.polo return c->next;
139 347d23da 2018-05-19 omar.polo } else {
140 347d23da 2018-05-19 omar.polo // the current one is selected and the next one is nill,
141 347d23da 2018-05-19 omar.polo // select the first one
142 347d23da 2018-05-19 omar.polo orig->selected = true;
143 347d23da 2018-05-19 omar.polo return orig;
146 347d23da 2018-05-19 omar.polo c = c->next;
148 347d23da 2018-05-19 omar.polo return nil;
151 347d23da 2018-05-19 omar.polo struct completions *compl_select_prev(struct completions *c, bool n) {
152 347d23da 2018-05-19 omar.polo if (c == nil)
153 347d23da 2018-05-19 omar.polo return nil;
155 347d23da 2018-05-19 omar.polo struct completions *cc = c;
157 347d23da 2018-05-19 omar.polo if (n) // select the last one
158 347d23da 2018-05-19 omar.polo while (cc != nil) {
159 347d23da 2018-05-19 omar.polo if (cc->next == nil) {
160 347d23da 2018-05-19 omar.polo cc->selected = true;
161 347d23da 2018-05-19 omar.polo return cc;
163 347d23da 2018-05-19 omar.polo cc = cc->next;
165 347d23da 2018-05-19 omar.polo else // select the previous one
166 347d23da 2018-05-19 omar.polo while (cc != nil) {
167 347d23da 2018-05-19 omar.polo if (cc->next != nil && cc->next->selected) {
168 347d23da 2018-05-19 omar.polo cc->next->selected = false;
169 347d23da 2018-05-19 omar.polo cc->selected = true;
170 347d23da 2018-05-19 omar.polo return cc;
172 347d23da 2018-05-19 omar.polo cc = cc->next;
175 347d23da 2018-05-19 omar.polo return nil;
178 f5e234d6 2018-05-18 omar.polo struct completions *filter(char *text, char **lines) {
179 f5e234d6 2018-05-18 omar.polo int i = 0;
180 f5e234d6 2018-05-18 omar.polo struct completions *root = compl_new();
181 f5e234d6 2018-05-18 omar.polo struct completions *c = root;
183 f5e234d6 2018-05-18 omar.polo for (;;) {
184 f5e234d6 2018-05-18 omar.polo char *l = lines[i];
185 f5e234d6 2018-05-18 omar.polo if (l == nil)
186 f5e234d6 2018-05-18 omar.polo break;
188 f5e234d6 2018-05-18 omar.polo if (strstr(l, text) != nil) {
189 f5e234d6 2018-05-18 omar.polo c->next = compl_new();
190 f5e234d6 2018-05-18 omar.polo c = c->next;
191 f5e234d6 2018-05-18 omar.polo c->completion = l;
197 f5e234d6 2018-05-18 omar.polo struct completions *r = root->next;
198 f5e234d6 2018-05-18 omar.polo compl_delete(root);
199 f5e234d6 2018-05-18 omar.polo return r;
202 f5e234d6 2018-05-18 omar.polo // push the character c at the end of the string pointed by p
203 f5e234d6 2018-05-18 omar.polo int pushc(char **p, int maxlen, char c) {
204 f5e234d6 2018-05-18 omar.polo int len = strnlen(*p, maxlen);
206 f5e234d6 2018-05-18 omar.polo if (!(len < maxlen -2)) {
207 f5e234d6 2018-05-18 omar.polo maxlen += maxlen >> 1;
208 f5e234d6 2018-05-18 omar.polo char *newptr = realloc(*p, maxlen);
209 f5e234d6 2018-05-18 omar.polo if (newptr == nil) { // bad!
210 f5e234d6 2018-05-18 omar.polo return -1;
212 f5e234d6 2018-05-18 omar.polo *p = newptr;
215 f5e234d6 2018-05-18 omar.polo (*p)[len] = c;
216 f5e234d6 2018-05-18 omar.polo (*p)[len+1] = '\0';
217 f5e234d6 2018-05-18 omar.polo return maxlen;
220 347d23da 2018-05-19 omar.polo int utf8strnlen(char *s, int maxlen) {
221 347d23da 2018-05-19 omar.polo int len = 0;
222 347d23da 2018-05-19 omar.polo while (*s && maxlen > 0) {
223 347d23da 2018-05-19 omar.polo len += (*s++ & 0xc0) != 0x80;
224 347d23da 2018-05-19 omar.polo maxlen--;
226 347d23da 2018-05-19 omar.polo return len;
229 347d23da 2018-05-19 omar.polo // remove the last *glyph* from the *utf8* string!
230 347d23da 2018-05-19 omar.polo // this is different from just setting the last byte to 0 (in some
231 347d23da 2018-05-19 omar.polo // cases ofc). The actual implementation is quite inefficient because
232 347d23da 2018-05-19 omar.polo // it remove the last char until the number of glyphs doesn't change
233 f5e234d6 2018-05-18 omar.polo void popc(char *p, int maxlen) {
234 f5e234d6 2018-05-18 omar.polo int len = strnlen(p, maxlen);
236 347d23da 2018-05-19 omar.polo if (len == 0)
237 347d23da 2018-05-19 omar.polo return;
239 347d23da 2018-05-19 omar.polo int ulen = utf8strnlen(p, maxlen);
240 347d23da 2018-05-19 omar.polo while (len > 0 && utf8strnlen(p, maxlen) == ulen) {
241 347d23da 2018-05-19 omar.polo len--;
242 347d23da 2018-05-19 omar.polo p[len] = 0;
246 8758854a 2018-05-20 omar.polo // If the string is surrounded by quotes (`"`) remove them and replace
247 8758854a 2018-05-20 omar.polo // every `\"` in the string with `"`
248 8758854a 2018-05-20 omar.polo char *normalize_str(const char *str) {
249 8758854a 2018-05-20 omar.polo int len = strlen(str);
250 8758854a 2018-05-20 omar.polo if (len == 0)
251 8758854a 2018-05-20 omar.polo return nil;
253 8758854a 2018-05-20 omar.polo char *s = calloc(len, sizeof(char));
254 8758854a 2018-05-20 omar.polo check_allocation(s);
255 8758854a 2018-05-20 omar.polo int p = 0;
256 8758854a 2018-05-20 omar.polo while (*str) {
257 8758854a 2018-05-20 omar.polo char c = *str;
258 8758854a 2018-05-20 omar.polo if (*str == '\\') {
259 8758854a 2018-05-20 omar.polo if (*(str + 1)) {
260 8758854a 2018-05-20 omar.polo s[p] = *(str + 1);
262 8758854a 2018-05-20 omar.polo str += 2; // skip this and the next char
263 8758854a 2018-05-20 omar.polo continue;
264 8758854a 2018-05-20 omar.polo } else {
265 8758854a 2018-05-20 omar.polo break;
268 8758854a 2018-05-20 omar.polo if (c == '"') {
269 8758854a 2018-05-20 omar.polo str++; // skip only this char
270 8758854a 2018-05-20 omar.polo continue;
272 8758854a 2018-05-20 omar.polo s[p] = c;
274 8758854a 2018-05-20 omar.polo str++;
276 8758854a 2018-05-20 omar.polo return s;
279 f5e234d6 2018-05-18 omar.polo // read an arbitrary long line from stdin and return a pointer to it
280 f5e234d6 2018-05-18 omar.polo // TODO: resize the allocated memory to exactly fit the string once
281 f5e234d6 2018-05-18 omar.polo // read?
282 f5e234d6 2018-05-18 omar.polo char *readline(bool *eof) {
283 f5e234d6 2018-05-18 omar.polo int maxlen = 8;
284 f5e234d6 2018-05-18 omar.polo char *str = calloc(maxlen, sizeof(char));
285 f5e234d6 2018-05-18 omar.polo if (str == nil) {
286 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Cannot allocate memory!\n");
287 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
290 f5e234d6 2018-05-18 omar.polo int c;
291 f5e234d6 2018-05-18 omar.polo while((c = getchar()) != EOF) {
292 f5e234d6 2018-05-18 omar.polo if (c == '\n')
293 f5e234d6 2018-05-18 omar.polo return str;
295 f5e234d6 2018-05-18 omar.polo maxlen = pushc(&str, maxlen, c);
297 f5e234d6 2018-05-18 omar.polo if (maxlen == -1) {
298 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Cannot allocate memory!\n");
299 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
302 f5e234d6 2018-05-18 omar.polo *eof = true;
303 f5e234d6 2018-05-18 omar.polo return str;
306 95b27a5e 2018-05-19 omar.polo int readlines (char ***lns, int items) {
307 f5e234d6 2018-05-18 omar.polo bool finished = false;
308 f5e234d6 2018-05-18 omar.polo int n = 0;
309 95b27a5e 2018-05-19 omar.polo char **lines = *lns;
310 95b27a5e 2018-05-19 omar.polo while (true) {
311 f5e234d6 2018-05-18 omar.polo lines[n] = readline(&finished);
313 6da98882 2018-05-20 omar.polo if (strlen(lines[n]) == 0 || lines[n][0] == '\n') {
314 6da98882 2018-05-20 omar.polo free(lines[n]);
315 f5e234d6 2018-05-18 omar.polo --n; // forget about this line
318 f5e234d6 2018-05-18 omar.polo if (finished)
319 f5e234d6 2018-05-18 omar.polo break;
323 95b27a5e 2018-05-19 omar.polo if (n == items - 1) {
324 95b27a5e 2018-05-19 omar.polo items += items >>1;
325 95b27a5e 2018-05-19 omar.polo char **l = realloc(lines, sizeof(char*) * items);
326 95b27a5e 2018-05-19 omar.polo check_allocation(l);
327 95b27a5e 2018-05-19 omar.polo *lns = l;
328 95b27a5e 2018-05-19 omar.polo lines = l;
333 f5e234d6 2018-05-18 omar.polo lines[n] = nil;
334 95b27a5e 2018-05-19 omar.polo return items;
337 c9a3bfaa 2018-05-21 omar.polo int text_extents(char *str, int len, struct rendering *r, int *ret_width, int *ret_height) {
338 c9a3bfaa 2018-05-21 omar.polo int height;
339 c9a3bfaa 2018-05-21 omar.polo int width;
340 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
341 c9a3bfaa 2018-05-21 omar.polo XGlyphInfo gi;
342 c9a3bfaa 2018-05-21 omar.polo XftTextExtentsUtf8(r->d, r->font, str, len, &gi);
343 c9a3bfaa 2018-05-21 omar.polo height = (r->font->ascent - r->font->descent)/2;
344 c9a3bfaa 2018-05-21 omar.polo width = gi.width - gi.x;
346 c9a3bfaa 2018-05-21 omar.polo XRectangle rect;
347 c9a3bfaa 2018-05-21 omar.polo XmbTextExtents(*r->font, str, len, nil, &rect);
348 c9a3bfaa 2018-05-21 omar.polo height = rect.height;
349 c9a3bfaa 2018-05-21 omar.polo width = rect.width;
350 c9a3bfaa 2018-05-21 omar.polo #endif
351 c9a3bfaa 2018-05-21 omar.polo if (ret_width != nil) *ret_width = width;
352 c9a3bfaa 2018-05-21 omar.polo if (ret_height != nil) *ret_height = height;
353 c9a3bfaa 2018-05-21 omar.polo return width;
356 c9a3bfaa 2018-05-21 omar.polo void draw_string(char *str, int len, int x, int y, struct rendering *r, enum text_type tt) {
357 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
358 c9a3bfaa 2018-05-21 omar.polo XftColor xftcolor;
359 c9a3bfaa 2018-05-21 omar.polo if (tt == PROMPT) xftcolor = r->xft_prompt;
360 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL) xftcolor = r->xft_completion;
361 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL_HIGH) xftcolor = r->xft_completion_highlighted;
363 c9a3bfaa 2018-05-21 omar.polo XftDrawStringUtf8(r->xftdraw, &xftcolor, r->font, x, y, str, len);
365 c9a3bfaa 2018-05-21 omar.polo GC gc;
366 c9a3bfaa 2018-05-21 omar.polo if (tt == PROMPT) gc = r->prompt;
367 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL) gc = r->completion;
368 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL_HIGH) gc = r->completion_highlighted;
369 c9a3bfaa 2018-05-21 omar.polo Xutf8DrawString(r->d, r->w, *r->font, gc, x, y, str, len);
370 c9a3bfaa 2018-05-21 omar.polo #endif
373 c9a3bfaa 2018-05-21 omar.polo char *strdupn(char *str) {
374 c9a3bfaa 2018-05-21 omar.polo int len = strlen(str);
376 c9a3bfaa 2018-05-21 omar.polo if (str == nil || len == 0)
377 c9a3bfaa 2018-05-21 omar.polo return nil;
379 c9a3bfaa 2018-05-21 omar.polo char *dup = strdup(str);
380 c9a3bfaa 2018-05-21 omar.polo if (dup == nil)
381 c9a3bfaa 2018-05-21 omar.polo return nil;
383 c9a3bfaa 2018-05-21 omar.polo for (int i = 0; i < len; ++i)
384 c9a3bfaa 2018-05-21 omar.polo if (dup[i] == ' ')
385 c9a3bfaa 2018-05-21 omar.polo dup[i] = 'n';
387 c9a3bfaa 2018-05-21 omar.polo return dup;
390 f5e234d6 2018-05-18 omar.polo // |------------------|----------------------------------------------|
391 f5e234d6 2018-05-18 omar.polo // | 20 char text | completion | completion | completion | compl |
392 f5e234d6 2018-05-18 omar.polo // |------------------|----------------------------------------------|
393 36a15a9f 2018-05-19 omar.polo void draw_horizontally(struct rendering *r, char *text, struct completions *cs) {
394 f5e234d6 2018-05-18 omar.polo // TODO: make these dynamic?
395 f5e234d6 2018-05-18 omar.polo int prompt_width = 20; // char
396 f5e234d6 2018-05-18 omar.polo int padding = 10;
398 c9a3bfaa 2018-05-21 omar.polo int width, height;
399 c9a3bfaa 2018-05-21 omar.polo char *ps1_dup = strdupn(r->ps1);
400 c9a3bfaa 2018-05-21 omar.polo ps1_dup = ps1_dup == nil ? r->ps1 : ps1_dup;
401 c9a3bfaa 2018-05-21 omar.polo int ps1xlen = text_extents(ps1_dup, r->ps1len, r, &width, &height);
402 c9a3bfaa 2018-05-21 omar.polo free(ps1_dup);
403 8758854a 2018-05-20 omar.polo int start_at = ps1xlen;
405 c9a3bfaa 2018-05-21 omar.polo start_at = text_extents("n", 1, r, nil, nil);
406 347d23da 2018-05-19 omar.polo start_at = start_at * prompt_width + padding;
408 c9a3bfaa 2018-05-21 omar.polo int texty = (height + r->height) >>1;
410 f5e234d6 2018-05-18 omar.polo XFillRectangle(r->d, r->w, r->prompt_bg, 0, 0, start_at, r->height);
412 f5e234d6 2018-05-18 omar.polo int text_len = strlen(text);
413 f5e234d6 2018-05-18 omar.polo if (text_len > prompt_width)
414 f5e234d6 2018-05-18 omar.polo text = text + (text_len - prompt_width);
415 c9a3bfaa 2018-05-21 omar.polo draw_string(r->ps1, r->ps1len, padding, texty, r, PROMPT);
416 c9a3bfaa 2018-05-21 omar.polo draw_string(text, MIN(text_len, prompt_width), padding + ps1xlen, texty, r, PROMPT);
418 f5e234d6 2018-05-18 omar.polo XFillRectangle(r->d, r->w, r->completion_bg, start_at, 0, r->width, r->height);
420 f5e234d6 2018-05-18 omar.polo while (cs != nil) {
421 c9a3bfaa 2018-05-21 omar.polo enum text_type tt = cs->selected ? COMPL_HIGH : COMPL;
422 f5e234d6 2018-05-18 omar.polo GC h = cs->selected ? r->completion_highlighted_bg : r->completion_bg;
424 f5e234d6 2018-05-18 omar.polo int len = strlen(cs->completion);
425 c9a3bfaa 2018-05-21 omar.polo int text_width = text_extents(cs->completion, len, r, nil, nil);
427 f5e234d6 2018-05-18 omar.polo XFillRectangle(r->d, r->w, h, start_at, 0, text_width + padding*2, r->height);
429 c9a3bfaa 2018-05-21 omar.polo draw_string(cs->completion, len, start_at + padding, texty, r, tt);
431 f5e234d6 2018-05-18 omar.polo start_at += text_width + padding * 2;
433 0ee198aa 2018-05-19 omar.polo if (start_at > r->width)
434 0ee198aa 2018-05-19 omar.polo break; // don't draw completion if the space isn't enough
436 f5e234d6 2018-05-18 omar.polo cs = cs->next;
439 f5e234d6 2018-05-18 omar.polo XFlush(r->d);
442 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
443 36a15a9f 2018-05-19 omar.polo // | prompt |
444 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
445 36a15a9f 2018-05-19 omar.polo // | completion |
446 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
447 36a15a9f 2018-05-19 omar.polo // | completion |
448 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
449 36a15a9f 2018-05-19 omar.polo // TODO: dunno why but in the call to Xutf8DrawString, by logic,
450 36a15a9f 2018-05-19 omar.polo // should be padding and not padding*2, but the text doesn't seem to
451 36a15a9f 2018-05-19 omar.polo // be vertically centered otherwise....
452 36a15a9f 2018-05-19 omar.polo void draw_vertically(struct rendering *r, char *text, struct completions *cs) {
453 36a15a9f 2018-05-19 omar.polo int padding = 10; // TODO make this dynamic
455 c9a3bfaa 2018-05-21 omar.polo int height, width;
456 c9a3bfaa 2018-05-21 omar.polo text_extents("fjpgl", 5, r, &width, &height);
457 c9a3bfaa 2018-05-21 omar.polo int start_at = height + padding*2;
459 36a15a9f 2018-05-19 omar.polo XFillRectangle(r->d, r->w, r->completion_bg, 0, 0, r->width, r->height);
460 36a15a9f 2018-05-19 omar.polo XFillRectangle(r->d, r->w, r->prompt_bg, 0, 0, r->width, start_at);
462 c9a3bfaa 2018-05-21 omar.polo char *ps1_dup = strdupn(r->ps1);
463 c9a3bfaa 2018-05-21 omar.polo ps1_dup = ps1_dup == nil ? r->ps1 : ps1_dup;
464 c9a3bfaa 2018-05-21 omar.polo int ps1xlen = text_extents(ps1_dup, r->ps1len, r, nil, nil);
465 c9a3bfaa 2018-05-21 omar.polo free(ps1_dup);
467 c9a3bfaa 2018-05-21 omar.polo draw_string(r->ps1, r->ps1len, padding, padding*2, r, PROMPT);
468 c9a3bfaa 2018-05-21 omar.polo draw_string(text, strlen(text), padding + ps1xlen, padding*2, r, PROMPT);
470 36a15a9f 2018-05-19 omar.polo while (cs != nil) {
471 c9a3bfaa 2018-05-21 omar.polo enum text_type tt = cs->selected ? COMPL_HIGH : COMPL;
472 36a15a9f 2018-05-19 omar.polo GC h = cs->selected ? r->completion_highlighted_bg : r->completion_bg;
474 36a15a9f 2018-05-19 omar.polo int len = strlen(cs->completion);
475 c9a3bfaa 2018-05-21 omar.polo text_extents(cs->completion, len, r, &width, &height);
476 c9a3bfaa 2018-05-21 omar.polo XFillRectangle(r->d, r->w, h, 0, start_at, r->width, height + padding*2);
477 c9a3bfaa 2018-05-21 omar.polo draw_string(cs->completion, len, padding, start_at + padding*2, r, tt);
479 c9a3bfaa 2018-05-21 omar.polo start_at += height + padding *2;
481 0ee198aa 2018-05-19 omar.polo if (start_at > r->height)
482 0ee198aa 2018-05-19 omar.polo break; // don't draw completion if the space isn't enough
484 36a15a9f 2018-05-19 omar.polo cs = cs->next;
487 36a15a9f 2018-05-19 omar.polo XFlush(r->d);
490 36a15a9f 2018-05-19 omar.polo void draw(struct rendering *r, char *text, struct completions *cs) {
491 36a15a9f 2018-05-19 omar.polo if (r->horizontal_layout)
492 36a15a9f 2018-05-19 omar.polo draw_horizontally(r, text, cs);
494 36a15a9f 2018-05-19 omar.polo draw_vertically(r, text, cs);
497 f5e234d6 2018-05-18 omar.polo /* Set some WM stuff */
498 f5e234d6 2018-05-18 omar.polo void set_win_atoms_hints(Display *d, Window w, int width, int height) {
499 f5e234d6 2018-05-18 omar.polo Atom type;
500 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_WINDOW_TYPE_DOCK", false);
501 f5e234d6 2018-05-18 omar.polo XChangeProperty(
504 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_WINDOW_TYPE", false),
505 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
507 f5e234d6 2018-05-18 omar.polo PropModeReplace,
508 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
512 f5e234d6 2018-05-18 omar.polo /* some window managers honor this properties */
513 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_STATE_ABOVE", false);
514 f5e234d6 2018-05-18 omar.polo XChangeProperty(d,
516 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_STATE", false),
517 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
519 f5e234d6 2018-05-18 omar.polo PropModeReplace,
520 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
524 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_STATE_FOCUSED", false);
525 f5e234d6 2018-05-18 omar.polo XChangeProperty(d,
527 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_STATE", false),
528 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
530 f5e234d6 2018-05-18 omar.polo PropModeAppend,
531 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
535 f5e234d6 2018-05-18 omar.polo // setting window hints
536 f5e234d6 2018-05-18 omar.polo XClassHint *class_hint = XAllocClassHint();
537 f5e234d6 2018-05-18 omar.polo if (class_hint == nil) {
538 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not allocate memory for class hint\n");
539 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
541 7ef37ee0 2018-05-21 omar.polo class_hint->res_name = resname;
542 7ef37ee0 2018-05-21 omar.polo class_hint->res_class = resclass;
543 f5e234d6 2018-05-18 omar.polo XSetClassHint(d, w, class_hint);
544 f5e234d6 2018-05-18 omar.polo XFree(class_hint);
546 f5e234d6 2018-05-18 omar.polo XSizeHints *size_hint = XAllocSizeHints();
547 f5e234d6 2018-05-18 omar.polo if (size_hint == nil) {
548 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not allocate memory for size hint\n");
549 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
551 7ef37ee0 2018-05-21 omar.polo size_hint->flags = PMinSize | PBaseSize;
552 f5e234d6 2018-05-18 omar.polo size_hint->min_width = width;
553 f5e234d6 2018-05-18 omar.polo size_hint->base_width = width;
554 f5e234d6 2018-05-18 omar.polo size_hint->min_height = height;
555 f5e234d6 2018-05-18 omar.polo size_hint->base_height = height;
557 f5e234d6 2018-05-18 omar.polo XFlush(d);
560 f5e234d6 2018-05-18 omar.polo void get_wh(Display *d, Window *w, int *width, int *height) {
561 f5e234d6 2018-05-18 omar.polo XWindowAttributes win_attr;
562 f5e234d6 2018-05-18 omar.polo XGetWindowAttributes(d, *w, &win_attr);
563 f5e234d6 2018-05-18 omar.polo *height = win_attr.height;
564 f5e234d6 2018-05-18 omar.polo *width = win_attr.width;
567 f5e234d6 2018-05-18 omar.polo // I know this may seem a little hackish BUT is the only way I managed
568 f5e234d6 2018-05-18 omar.polo // to actually grab that goddam keyboard. Only one call to
569 f5e234d6 2018-05-18 omar.polo // XGrabKeyboard does not always end up with the keyboard grabbed!
570 f5e234d6 2018-05-18 omar.polo int take_keyboard(Display *d, Window w) {
571 686e9237 2018-05-18 omar.polo int i;
572 686e9237 2018-05-18 omar.polo for (i = 0; i < 100; i++) {
573 686e9237 2018-05-18 omar.polo if (XGrabKeyboard(d, w, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
574 686e9237 2018-05-18 omar.polo return 1;
575 686e9237 2018-05-18 omar.polo usleep(1000);
577 686e9237 2018-05-18 omar.polo return 0;
580 f5e234d6 2018-05-18 omar.polo void release_keyboard(Display *d) {
581 686e9237 2018-05-18 omar.polo XUngrabKeyboard(d, CurrentTime);
584 f5e234d6 2018-05-18 omar.polo int parse_integer(const char *str, int default_value, int max) {
585 f5e234d6 2018-05-18 omar.polo int len = strlen(str);
586 f5e234d6 2018-05-18 omar.polo if (len > 0 && str[len-1] == '%') {
587 f5e234d6 2018-05-18 omar.polo char *cpy = strdup(str);
588 f5e234d6 2018-05-18 omar.polo check_allocation(cpy);
589 f5e234d6 2018-05-18 omar.polo cpy[len-1] = '\0';
590 f5e234d6 2018-05-18 omar.polo int val = parse_integer(cpy, default_value, max);
591 f5e234d6 2018-05-18 omar.polo free(cpy);
592 f5e234d6 2018-05-18 omar.polo return val * max / 100;
595 f5e234d6 2018-05-18 omar.polo errno = 0;
596 f5e234d6 2018-05-18 omar.polo char *ep;
597 f5e234d6 2018-05-18 omar.polo long lval = strtol(str, &ep, 10);
598 f5e234d6 2018-05-18 omar.polo if (str[0] == '\0' || *ep != '\0') { // NaN
599 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "'%s' is not a valid number! Using %d as default.\n", str, default_value);
600 f5e234d6 2018-05-18 omar.polo return default_value;
602 f5e234d6 2018-05-18 omar.polo if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
603 f5e234d6 2018-05-18 omar.polo (lval > INT_MAX || lval < INT_MIN)) {
604 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "%s out of range! Using %d as default.\n", str, default_value);
605 f5e234d6 2018-05-18 omar.polo return default_value;
607 f5e234d6 2018-05-18 omar.polo return lval;
610 f5e234d6 2018-05-18 omar.polo int parse_int_with_middle(const char *str, int default_value, int max, int self) {
611 f5e234d6 2018-05-18 omar.polo if (!strcmp(str, "middle")) {
612 f5e234d6 2018-05-18 omar.polo return (max - self)/2;
614 f5e234d6 2018-05-18 omar.polo return parse_integer(str, default_value, max);
617 f5e234d6 2018-05-18 omar.polo int main() {
618 95b27a5e 2018-05-19 omar.polo char **lines = calloc(INITIAL_ITEMS, sizeof(char*));
619 6da98882 2018-05-20 omar.polo int nlines = readlines(&lines, INITIAL_ITEMS);
621 f5e234d6 2018-05-18 omar.polo setlocale(LC_ALL, getenv("LANG"));
623 f5e234d6 2018-05-18 omar.polo enum state status = LOOPING;
625 f5e234d6 2018-05-18 omar.polo // where the monitor start (used only with xinerama)
626 f5e234d6 2018-05-18 omar.polo int offset_x = 0;
627 f5e234d6 2018-05-18 omar.polo int offset_y = 0;
629 f5e234d6 2018-05-18 omar.polo // width and height of the window
630 f5e234d6 2018-05-18 omar.polo int width = 400;
631 f5e234d6 2018-05-18 omar.polo int height = 20;
633 f5e234d6 2018-05-18 omar.polo // position on the screen
634 f5e234d6 2018-05-18 omar.polo int x = 0;
635 f5e234d6 2018-05-18 omar.polo int y = 0;
637 8758854a 2018-05-20 omar.polo char *ps1 = strdup("$ ");
638 8758854a 2018-05-20 omar.polo check_allocation(ps1);
640 f5e234d6 2018-05-18 omar.polo char *fontname = strdup("fixed");
641 f5e234d6 2018-05-18 omar.polo check_allocation(fontname);
643 f5e234d6 2018-05-18 omar.polo int textlen = 10;
644 f5e234d6 2018-05-18 omar.polo char *text = malloc(textlen * sizeof(char));
645 f5e234d6 2018-05-18 omar.polo check_allocation(text);
647 f5e234d6 2018-05-18 omar.polo struct completions *cs = filter(text, lines);
648 f5e234d6 2018-05-18 omar.polo bool nothing_selected = true;
650 f5e234d6 2018-05-18 omar.polo // start talking to xorg
651 f5e234d6 2018-05-18 omar.polo Display *d = XOpenDisplay(nil);
652 f5e234d6 2018-05-18 omar.polo if (d == nil) {
653 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not open display!\n");
654 f5e234d6 2018-05-18 omar.polo return EX_UNAVAILABLE;
657 f5e234d6 2018-05-18 omar.polo // get display size
658 f5e234d6 2018-05-18 omar.polo // XXX: is getting the default root window dimension correct?
659 f5e234d6 2018-05-18 omar.polo XWindowAttributes xwa;
660 f5e234d6 2018-05-18 omar.polo XGetWindowAttributes(d, DefaultRootWindow(d), &xwa);
661 f5e234d6 2018-05-18 omar.polo int d_width = xwa.width;
662 f5e234d6 2018-05-18 omar.polo int d_height = xwa.height;
664 f5e234d6 2018-05-18 omar.polo #ifdef USE_XINERAMA
665 f5e234d6 2018-05-18 omar.polo if (XineramaIsActive(d)) {
666 7ca8829b 2018-05-21 omar.polo // find the mice
667 7ca8829b 2018-05-21 omar.polo int number_of_screens = XScreenCount(d);
668 7ca8829b 2018-05-21 omar.polo bool result;
669 7ca8829b 2018-05-21 omar.polo Window r;
670 7ca8829b 2018-05-21 omar.polo Window root;
671 7ca8829b 2018-05-21 omar.polo int root_x, root_y, win_x, win_y;
672 7ca8829b 2018-05-21 omar.polo unsigned int mask;
673 7ca8829b 2018-05-21 omar.polo bool res;
674 7ca8829b 2018-05-21 omar.polo for (int i = 0; i < number_of_screens; ++i) {
675 7ca8829b 2018-05-21 omar.polo root = XRootWindow(d, i);
676 7ca8829b 2018-05-21 omar.polo res = XQueryPointer(d, root, &r, &r, &root_x, &root_y, &win_x, &win_y, &mask);
677 7ca8829b 2018-05-21 omar.polo if (res) break;
679 7ca8829b 2018-05-21 omar.polo if (!res) {
680 7ca8829b 2018-05-21 omar.polo fprintf(stderr, "No mouse found.\n");
681 7ca8829b 2018-05-21 omar.polo root_x = 0;
682 7ca8829b 2018-05-21 omar.polo root_y = 0;
685 7ca8829b 2018-05-21 omar.polo // now find in which monitor the mice is on
686 f5e234d6 2018-05-18 omar.polo int monitors;
687 f5e234d6 2018-05-18 omar.polo XineramaScreenInfo *info = XineramaQueryScreens(d, &monitors);
688 7ca8829b 2018-05-21 omar.polo if (info) {
689 f5e234d6 2018-05-18 omar.polo for (int i = 0; i < monitors; ++i) {
690 7ca8829b 2018-05-21 omar.polo if (info[i].x_org <= root_x && root_x <= (info[i].x_org + info[i].width)
691 7ca8829b 2018-05-21 omar.polo && info[i].y_org <= root_y && root_y <= (info[i].y_org + info[i].height)) {
692 7ca8829b 2018-05-21 omar.polo offset_x = info[i].x_org;
693 7ca8829b 2018-05-21 omar.polo offset_y = info[i].y_org;
694 f5e234d6 2018-05-18 omar.polo d_width = info[i].width;
695 f5e234d6 2018-05-18 omar.polo d_height = info[i].height;
696 7ca8829b 2018-05-21 omar.polo break;
700 f5e234d6 2018-05-18 omar.polo XFree(info);
702 f5e234d6 2018-05-18 omar.polo #endif
704 f5e234d6 2018-05-18 omar.polo Colormap cmap = DefaultColormap(d, DefaultScreen(d));
705 f5e234d6 2018-05-18 omar.polo XColor p_fg, p_bg,
706 f5e234d6 2018-05-18 omar.polo compl_fg, compl_bg,
707 f5e234d6 2018-05-18 omar.polo compl_highlighted_fg, compl_highlighted_bg;
709 36a15a9f 2018-05-19 omar.polo bool horizontal_layout = true;
711 f5e234d6 2018-05-18 omar.polo // read resource
712 f5e234d6 2018-05-18 omar.polo XrmInitialize();
713 f5e234d6 2018-05-18 omar.polo char *xrm = XResourceManagerString(d);
714 347d23da 2018-05-19 omar.polo XrmDatabase xdb = nil;
715 f5e234d6 2018-05-18 omar.polo if (xrm != nil) {
716 347d23da 2018-05-19 omar.polo xdb = XrmGetStringDatabase(xrm);
717 f5e234d6 2018-05-18 omar.polo XrmValue value;
718 f5e234d6 2018-05-18 omar.polo char *datatype[20];
720 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.font", "*", datatype, &value) == true) {
721 f5e234d6 2018-05-18 omar.polo fontname = strdup(value.addr);
722 f5e234d6 2018-05-18 omar.polo check_allocation(fontname);
725 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no font defined, using %s\n", fontname);
727 36a15a9f 2018-05-19 omar.polo if (XrmGetResource(xdb, "MyMenu.layout", "*", datatype, &value) == true) {
728 36a15a9f 2018-05-19 omar.polo char *v = strdup(value.addr);
729 36a15a9f 2018-05-19 omar.polo check_allocation(v);
730 36a15a9f 2018-05-19 omar.polo horizontal_layout = !strcmp(v, "horizontal");
731 36a15a9f 2018-05-19 omar.polo free(v);
734 36a15a9f 2018-05-19 omar.polo fprintf(stderr, "no layout defined, using horizontal\n");
736 8758854a 2018-05-20 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt", "*", datatype, &value) == true) {
737 8758854a 2018-05-20 omar.polo free(ps1);
738 8758854a 2018-05-20 omar.polo ps1 = normalize_str(value.addr);
739 8758854a 2018-05-20 omar.polo } else
740 8758854a 2018-05-20 omar.polo fprintf(stderr, "no prompt defined, using \"%s\" as default\n", ps1);
742 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.width", "*", datatype, &value) == true)
743 f5e234d6 2018-05-18 omar.polo width = parse_integer(value.addr, width, d_width);
745 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no width defined, using %d\n", width);
747 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.height", "*", datatype, &value) == true)
748 f5e234d6 2018-05-18 omar.polo height = parse_integer(value.addr, height, d_height);
750 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no height defined, using %d\n", height);
752 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.x", "*", datatype, &value) == true)
753 f5e234d6 2018-05-18 omar.polo x = parse_int_with_middle(value.addr, x, d_width, width);
755 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no x defined, using %d\n", width);
757 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.y", "*", datatype, &value) == true)
758 f5e234d6 2018-05-18 omar.polo y = parse_int_with_middle(value.addr, y, d_height, height);
760 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no y defined, using %d\n", height);
762 f5e234d6 2018-05-18 omar.polo XColor tmp;
763 f5e234d6 2018-05-18 omar.polo // TODO: tmp needs to be free'd after every allocation?
765 f5e234d6 2018-05-18 omar.polo // prompt
766 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.foreground", "*", datatype, &value) == true)
767 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &p_fg, &tmp);
769 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &p_fg, &tmp);
771 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.background", "*", datatype, &value) == true)
772 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &p_bg, &tmp);
774 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &p_bg, &tmp);
776 f5e234d6 2018-05-18 omar.polo // completion
777 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.foreground", "*", datatype, &value) == true)
778 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_fg, &tmp);
780 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_fg, &tmp);
782 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.background", "*", datatype, &value) == true)
783 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_bg, &tmp);
785 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_bg, &tmp);
787 f5e234d6 2018-05-18 omar.polo // completion highlighted
788 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion_highlighted.foreground", "*", datatype, &value) == true)
789 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_highlighted_fg, &tmp);
791 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_highlighted_fg, &tmp);
793 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion_highlighted.background", "*", datatype, &value) == true)
794 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_highlighted_bg, &tmp);
796 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_highlighted_bg, &tmp);
797 f5e234d6 2018-05-18 omar.polo } else {
798 f5e234d6 2018-05-18 omar.polo XColor tmp;
799 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &p_fg, &tmp);
800 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &p_bg, &tmp);
801 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_fg, &tmp);
802 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_bg, &tmp);
803 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_highlighted_fg, &tmp);
804 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_highlighted_bg, &tmp);
807 f5e234d6 2018-05-18 omar.polo // load the font
808 347d23da 2018-05-19 omar.polo /* XFontStruct *font = XLoadQueryFont(d, fontname); */
809 347d23da 2018-05-19 omar.polo /* if (font == nil) { */
810 347d23da 2018-05-19 omar.polo /* fprintf(stderr, "Unable to load %s font\n", fontname); */
811 347d23da 2018-05-19 omar.polo /* font = XLoadQueryFont(d, "fixed"); */
812 347d23da 2018-05-19 omar.polo /* } */
813 347d23da 2018-05-19 omar.polo // load the font
814 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
815 c9a3bfaa 2018-05-21 omar.polo XftFont *font = XftFontOpenName(d, DefaultScreen(d), fontname);
817 347d23da 2018-05-19 omar.polo char **missing_charset_list;
818 347d23da 2018-05-19 omar.polo int missing_charset_count;
819 347d23da 2018-05-19 omar.polo XFontSet font = XCreateFontSet(d, fontname, &missing_charset_list, &missing_charset_count, nil);
820 f5e234d6 2018-05-18 omar.polo if (font == nil) {
821 347d23da 2018-05-19 omar.polo fprintf(stderr, "Unable to load the font(s) %s\n", fontname);
822 347d23da 2018-05-19 omar.polo return EX_UNAVAILABLE;
824 c9a3bfaa 2018-05-21 omar.polo #endif
826 f5e234d6 2018-05-18 omar.polo // create the window
827 f5e234d6 2018-05-18 omar.polo XSetWindowAttributes attr;
828 55bd6842 2018-05-21 omar.polo attr.override_redirect = true;
830 f5e234d6 2018-05-18 omar.polo Window w = XCreateWindow(d, // display
831 f5e234d6 2018-05-18 omar.polo DefaultRootWindow(d), // parent
832 f5e234d6 2018-05-18 omar.polo x + offset_x, y + offset_y, // x y
833 f5e234d6 2018-05-18 omar.polo width, height, // w h
834 f5e234d6 2018-05-18 omar.polo 0, // border width
835 f5e234d6 2018-05-18 omar.polo DefaultDepth(d, DefaultScreen(d)), // depth
836 f5e234d6 2018-05-18 omar.polo InputOutput, // class
837 f5e234d6 2018-05-18 omar.polo DefaultVisual(d, DefaultScreen(d)), // visual
838 55bd6842 2018-05-21 omar.polo CWOverrideRedirect, // value mask
839 f5e234d6 2018-05-18 omar.polo &attr);
841 f5e234d6 2018-05-18 omar.polo set_win_atoms_hints(d, w, width, height);
843 f5e234d6 2018-05-18 omar.polo // we want some events
844 c9a3bfaa 2018-05-21 omar.polo XSelectInput(d, w, StructureNotifyMask | KeyPressMask | KeymapStateMask);
846 f5e234d6 2018-05-18 omar.polo // make the window appear on the screen
847 f5e234d6 2018-05-18 omar.polo XMapWindow(d, w);
849 f5e234d6 2018-05-18 omar.polo // wait for the MapNotify event (i.e. the event "window rendered")
850 f5e234d6 2018-05-18 omar.polo for (;;) {
851 f5e234d6 2018-05-18 omar.polo XEvent e;
852 f5e234d6 2018-05-18 omar.polo XNextEvent(d, &e);
853 f5e234d6 2018-05-18 omar.polo if (e.type == MapNotify)
854 f5e234d6 2018-05-18 omar.polo break;
857 f5e234d6 2018-05-18 omar.polo // get the *real* width & height after the window was rendered
858 f5e234d6 2018-05-18 omar.polo get_wh(d, &w, &width, &height);
860 f5e234d6 2018-05-18 omar.polo // grab keyboard
861 f5e234d6 2018-05-18 omar.polo take_keyboard(d, w);
863 f5e234d6 2018-05-18 omar.polo // Create some graphics contexts
864 f5e234d6 2018-05-18 omar.polo XGCValues values;
865 347d23da 2018-05-19 omar.polo /* values.font = font->fid; */
867 f5e234d6 2018-05-18 omar.polo struct rendering r = {
868 f5e234d6 2018-05-18 omar.polo .d = d,
869 f5e234d6 2018-05-18 omar.polo .w = w,
870 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
871 c9a3bfaa 2018-05-21 omar.polo .font = font,
873 c9a3bfaa 2018-05-21 omar.polo .font = &font,
874 c9a3bfaa 2018-05-21 omar.polo #endif
875 347d23da 2018-05-19 omar.polo .prompt = XCreateGC(d, w, 0, &values),
876 347d23da 2018-05-19 omar.polo .prompt_bg = XCreateGC(d, w, 0, &values),
877 347d23da 2018-05-19 omar.polo .completion = XCreateGC(d, w, 0, &values),
878 347d23da 2018-05-19 omar.polo .completion_bg = XCreateGC(d, w, 0, &values),
879 347d23da 2018-05-19 omar.polo .completion_highlighted = XCreateGC(d, w, 0, &values),
880 347d23da 2018-05-19 omar.polo .completion_highlighted_bg = XCreateGC(d, w, 0, &values),
881 f5e234d6 2018-05-18 omar.polo .width = width,
882 f5e234d6 2018-05-18 omar.polo .height = height,
883 8758854a 2018-05-20 omar.polo .horizontal_layout = horizontal_layout,
884 8758854a 2018-05-20 omar.polo .ps1 = ps1,
885 8758854a 2018-05-20 omar.polo .ps1len = strlen(ps1)
888 7ca8829b 2018-05-21 omar.polo #ifdef USE_XFT
889 7ca8829b 2018-05-21 omar.polo r.xftdraw = XftDrawCreate(d, w, DefaultVisual(d, 0), DefaultColormap(d, 0));
891 7ca8829b 2018-05-21 omar.polo // prompt
892 7ca8829b 2018-05-21 omar.polo XRenderColor xrcolor;
893 7ca8829b 2018-05-21 omar.polo xrcolor.red = p_fg.red;
894 7ca8829b 2018-05-21 omar.polo xrcolor.green = p_fg.red;
895 7ca8829b 2018-05-21 omar.polo xrcolor.blue = p_fg.red;
896 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
897 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_prompt);
899 7ca8829b 2018-05-21 omar.polo // completion
900 7ca8829b 2018-05-21 omar.polo xrcolor.red = compl_fg.red;
901 7ca8829b 2018-05-21 omar.polo xrcolor.green = compl_fg.green;
902 7ca8829b 2018-05-21 omar.polo xrcolor.blue = compl_fg.blue;
903 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
904 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_completion);
906 7ca8829b 2018-05-21 omar.polo // completion highlighted
907 7ca8829b 2018-05-21 omar.polo xrcolor.red = compl_highlighted_fg.red;
908 7ca8829b 2018-05-21 omar.polo xrcolor.green = compl_highlighted_fg.green;
909 7ca8829b 2018-05-21 omar.polo xrcolor.blue = compl_highlighted_fg.blue;
910 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
911 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_completion_highlighted);
912 c9a3bfaa 2018-05-21 omar.polo #endif
914 f5e234d6 2018-05-18 omar.polo // load the colors in our GCs
915 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.prompt, p_fg.pixel);
916 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.prompt_bg, p_bg.pixel);
917 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion, compl_fg.pixel);
918 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_bg, compl_bg.pixel);
919 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_highlighted, compl_highlighted_fg.pixel);
920 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_highlighted_bg, compl_highlighted_bg.pixel);
922 347d23da 2018-05-19 omar.polo // open the X input method
923 347d23da 2018-05-19 omar.polo XIM xim = XOpenIM(d, xdb, resname, resclass);
924 347d23da 2018-05-19 omar.polo check_allocation(xim);
926 347d23da 2018-05-19 omar.polo XIMStyles *xis = nil;
927 347d23da 2018-05-19 omar.polo if (XGetIMValues(xim, XNQueryInputStyle, &xis, NULL) || !xis) {
928 347d23da 2018-05-19 omar.polo fprintf(stderr, "Input Styles could not be retrieved\n");
929 347d23da 2018-05-19 omar.polo return EX_UNAVAILABLE;
932 347d23da 2018-05-19 omar.polo XIMStyle bestMatchStyle = 0;
933 347d23da 2018-05-19 omar.polo for (int i = 0; i < xis->count_styles; ++i) {
934 347d23da 2018-05-19 omar.polo XIMStyle ts = xis->supported_styles[i];
935 347d23da 2018-05-19 omar.polo if (ts == (XIMPreeditNothing | XIMStatusNothing)) {
936 347d23da 2018-05-19 omar.polo bestMatchStyle = ts;
937 347d23da 2018-05-19 omar.polo break;
940 347d23da 2018-05-19 omar.polo XFree(xis);
942 347d23da 2018-05-19 omar.polo if (!bestMatchStyle) {
943 347d23da 2018-05-19 omar.polo fprintf(stderr, "No matching input style could be determined\n");
946 347d23da 2018-05-19 omar.polo XIC xic = XCreateIC(xim, XNInputStyle, bestMatchStyle, XNClientWindow, w, XNFocusWindow, w, NULL);
947 347d23da 2018-05-19 omar.polo check_allocation(xic);
949 f5e234d6 2018-05-18 omar.polo // draw the window for the first time
950 f5e234d6 2018-05-18 omar.polo draw(&r, text, cs);
952 f5e234d6 2018-05-18 omar.polo // main loop
953 f5e234d6 2018-05-18 omar.polo while (status == LOOPING) {
954 f5e234d6 2018-05-18 omar.polo XEvent e;
955 f5e234d6 2018-05-18 omar.polo XNextEvent(d, &e);
957 347d23da 2018-05-19 omar.polo if (XFilterEvent(&e, w))
958 347d23da 2018-05-19 omar.polo continue;
960 f5e234d6 2018-05-18 omar.polo switch (e.type) {
961 f5e234d6 2018-05-18 omar.polo case KeymapNotify:
962 f5e234d6 2018-05-18 omar.polo XRefreshKeyboardMapping(&e.xmapping);
963 f5e234d6 2018-05-18 omar.polo break;
965 347d23da 2018-05-19 omar.polo case KeyPress: {
966 347d23da 2018-05-19 omar.polo XKeyPressedEvent *ev = (XKeyPressedEvent*)&e;
968 347d23da 2018-05-19 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Tab)) {
969 347d23da 2018-05-19 omar.polo bool shift = (ev->state & ShiftMask);
970 347d23da 2018-05-19 omar.polo struct completions *n = shift ? compl_select_prev(cs, nothing_selected)
971 347d23da 2018-05-19 omar.polo : compl_select_next(cs, nothing_selected);
972 347d23da 2018-05-19 omar.polo if (n != nil) {
973 347d23da 2018-05-19 omar.polo nothing_selected = false;
974 347d23da 2018-05-19 omar.polo free(text);
975 347d23da 2018-05-19 omar.polo text = strdup(n->completion);
976 347d23da 2018-05-19 omar.polo if (text == nil) {
977 347d23da 2018-05-19 omar.polo fprintf(stderr, "Memory allocation error!\n");
978 347d23da 2018-05-19 omar.polo status = ERR;
979 347d23da 2018-05-19 omar.polo break;
981 347d23da 2018-05-19 omar.polo textlen = strlen(text);
983 f5e234d6 2018-05-18 omar.polo draw(&r, text, cs);
984 f5e234d6 2018-05-18 omar.polo break;
987 347d23da 2018-05-19 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_BackSpace)) {
988 f5e234d6 2018-05-18 omar.polo nothing_selected = true;
989 f5e234d6 2018-05-18 omar.polo popc(text, textlen);
990 f5e234d6 2018-05-18 omar.polo update_completions(cs, text, lines);
991 f5e234d6 2018-05-18 omar.polo draw(&r, text, cs);
992 f5e234d6 2018-05-18 omar.polo break;
995 347d23da 2018-05-19 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Return)) {
996 347d23da 2018-05-19 omar.polo status = OK;
997 347d23da 2018-05-19 omar.polo break;
1000 347d23da 2018-05-19 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Escape)) {
1001 347d23da 2018-05-19 omar.polo status = ERR;
1002 347d23da 2018-05-19 omar.polo break;
1005 347d23da 2018-05-19 omar.polo // try to read what the user pressed
1006 347d23da 2018-05-19 omar.polo int symbol = 0;
1007 347d23da 2018-05-19 omar.polo Status s = 0;
1008 347d23da 2018-05-19 omar.polo Xutf8LookupString(xic, ev, (char*)&symbol, 4, 0, &s);
1010 347d23da 2018-05-19 omar.polo if (s == XBufferOverflow) {
1011 347d23da 2018-05-19 omar.polo // should not happen since there are no utf-8 characters
1012 347d23da 2018-05-19 omar.polo // larger than 24bits, but is something to be aware of when
1013 347d23da 2018-05-19 omar.polo // used to directly write to a string buffer
1014 347d23da 2018-05-19 omar.polo fprintf(stderr, "Buffer overflow when trying to create keyboard symbol map.\n");
1015 347d23da 2018-05-19 omar.polo break;
1017 347d23da 2018-05-19 omar.polo char *str = (char*)&symbol;
1019 347d23da 2018-05-19 omar.polo if (ev->state & ControlMask) {
1020 347d23da 2018-05-19 omar.polo // check for some key bindings
1021 347d23da 2018-05-19 omar.polo if (!strcmp(str, "")) { // C-u
1022 f5e234d6 2018-05-18 omar.polo nothing_selected = true;
1023 347d23da 2018-05-19 omar.polo for (int i = 0; i < textlen; ++i)
1024 347d23da 2018-05-19 omar.polo text[i] = 0;
1025 f5e234d6 2018-05-18 omar.polo update_completions(cs, text, lines);
1027 347d23da 2018-05-19 omar.polo if (!strcmp(str, "")) { // C-h
1028 347d23da 2018-05-19 omar.polo nothing_selected = true;
1029 347d23da 2018-05-19 omar.polo popc(text, textlen);
1030 347d23da 2018-05-19 omar.polo update_completions(cs, text, lines);
1032 347d23da 2018-05-19 omar.polo if (!strcmp(str, "")) { // C-w
1033 347d23da 2018-05-19 omar.polo nothing_selected = true;
1035 347d23da 2018-05-19 omar.polo // `textlen` is the length of the allocated string, not the
1036 347d23da 2018-05-19 omar.polo // length of the ACTUAL string
1037 347d23da 2018-05-19 omar.polo int p = strlen(text) - 1;
1038 347d23da 2018-05-19 omar.polo if (p >= 0) { // delete the current char
1039 347d23da 2018-05-19 omar.polo text[p] = 0;
1042 347d23da 2018-05-19 omar.polo while (p >= 0 && isalnum(text[p])) {
1043 347d23da 2018-05-19 omar.polo text[p] = 0;
1046 347d23da 2018-05-19 omar.polo // erase also trailing white space
1047 347d23da 2018-05-19 omar.polo while (p >= 0 && isspace(text[p])) {
1048 347d23da 2018-05-19 omar.polo text[p] = 0;
1051 347d23da 2018-05-19 omar.polo update_completions(cs, text, lines);
1053 347d23da 2018-05-19 omar.polo if (!strcmp(str, "\r")) { // C-m
1054 347d23da 2018-05-19 omar.polo status = OK;
1056 347d23da 2018-05-19 omar.polo draw(&r, text, cs);
1057 f5e234d6 2018-05-18 omar.polo break;
1060 347d23da 2018-05-19 omar.polo int str_len = strlen(str);
1061 347d23da 2018-05-19 omar.polo for (int i = 0; i < str_len; ++i) {
1062 347d23da 2018-05-19 omar.polo textlen = pushc(&text, textlen, str[i]);
1063 347d23da 2018-05-19 omar.polo if (textlen == -1) {
1064 347d23da 2018-05-19 omar.polo fprintf(stderr, "Memory allocation error\n");
1065 347d23da 2018-05-19 omar.polo status = ERR;
1066 347d23da 2018-05-19 omar.polo break;
1068 347d23da 2018-05-19 omar.polo nothing_selected = true;
1069 347d23da 2018-05-19 omar.polo update_completions(cs, text, lines);
1073 f5e234d6 2018-05-18 omar.polo draw(&r, text, cs);
1074 f5e234d6 2018-05-18 omar.polo break;
1076 f5e234d6 2018-05-18 omar.polo default:
1077 347d23da 2018-05-19 omar.polo fprintf(stderr, "Unknown event %d\n", e.type);
1081 f5e234d6 2018-05-18 omar.polo release_keyboard(d);
1083 f5e234d6 2018-05-18 omar.polo if (status == OK)
1084 f5e234d6 2018-05-18 omar.polo printf("%s\n", text);
1086 6da98882 2018-05-20 omar.polo for (int i = 0; i < nlines; ++i) {
1087 6da98882 2018-05-20 omar.polo free(lines[i]);
1090 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
1091 c9a3bfaa 2018-05-21 omar.polo XftColorFree(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &r.xft_prompt);
1092 c9a3bfaa 2018-05-21 omar.polo XftColorFree(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &r.xft_completion);
1093 c9a3bfaa 2018-05-21 omar.polo XftColorFree(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &r.xft_completion_highlighted);
1094 c9a3bfaa 2018-05-21 omar.polo #endif
1096 8758854a 2018-05-20 omar.polo free(ps1);
1097 f5e234d6 2018-05-18 omar.polo free(fontname);
1098 f5e234d6 2018-05-18 omar.polo free(text);
1099 95b27a5e 2018-05-19 omar.polo free(lines);
1100 f5e234d6 2018-05-18 omar.polo compl_delete(cs);
1102 8758854a 2018-05-20 omar.polo XDestroyWindow(d, w);
1103 f5e234d6 2018-05-18 omar.polo XCloseDisplay(d);
1105 f5e234d6 2018-05-18 omar.polo return status == OK ? 0 : 1;