1 c758d8da 2019-10-18 omar.polo #include <ctype.h> /* isalnum */
2 92803b00 2019-10-19 omar.polo #include <err.h>
3 c758d8da 2019-10-18 omar.polo #include <errno.h>
4 c758d8da 2019-10-18 omar.polo #include <limits.h>
5 c758d8da 2019-10-18 omar.polo #include <locale.h> /* setlocale */
6 c758d8da 2019-10-18 omar.polo #include <stdint.h>
7 f5e234d6 2018-05-18 omar.polo #include <stdio.h>
8 f5e234d6 2018-05-18 omar.polo #include <stdlib.h>
9 c758d8da 2019-10-18 omar.polo #include <string.h> /* strdup, strlen */
10 f5e234d6 2018-05-18 omar.polo #include <sysexits.h>
11 1a4491e5 2018-09-19 omar.polo #include <unistd.h>
13 c758d8da 2019-10-18 omar.polo #include <X11/Xcms.h>
14 f5e234d6 2018-05-18 omar.polo #include <X11/Xlib.h>
15 f5e234d6 2018-05-18 omar.polo #include <X11/Xresource.h>
16 c758d8da 2019-10-18 omar.polo #include <X11/Xutil.h>
17 347d23da 2018-05-19 omar.polo #include <X11/keysym.h>
19 f5e234d6 2018-05-18 omar.polo #ifdef USE_XINERAMA
20 c758d8da 2019-10-18 omar.polo #include <X11/extensions/Xinerama.h>
23 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
24 c758d8da 2019-10-18 omar.polo #include <X11/Xft/Xft.h>
27 b500fe86 2018-06-07 omar.polo #ifndef VERSION
28 c758d8da 2019-10-18 omar.polo #define VERSION "unknown"
31 347d23da 2018-05-19 omar.polo #define resname "MyMenu"
32 347d23da 2018-05-19 omar.polo #define resclass "mymenu"
34 b5d751bd 2018-07-07 omar.polo #define SYM_BUF_SIZE 4
36 9e94fcbe 2018-05-22 omar.polo #ifdef USE_XFT
37 c758d8da 2019-10-18 omar.polo #define default_fontname "monospace"
39 c758d8da 2019-10-18 omar.polo #define default_fontname "fixed"
42 df29e05e 2018-10-17 omar.polo #define ARGS "Aahmve:p:P:l:f:W:H:x:y:b:B:t:T:c:C:s:S:d:G:g:I:i:J:j:"
44 9e94fcbe 2018-05-22 omar.polo #define MIN(a, b) ((a) < (b) ? (a) : (b))
45 f5e234d6 2018-05-18 omar.polo #define MAX(a, b) ((a) > (b) ? (a) : (b))
47 5e448381 2018-10-09 omar.polo #define EXPANDBITS(x) (((x & 0xf0) * 0x100) | (x & 0x0f) * 0x10)
50 1d13acf5 2018-10-04 omar.polo * If we don't have or we don't want an "ignore case" completion
51 1d13acf5 2018-10-04 omar.polo * style, fall back to `strstr(3)`
53 9e94fcbe 2018-05-22 omar.polo #ifndef USE_STRCASESTR
54 c758d8da 2019-10-18 omar.polo #define strcasestr strstr
57 1d13acf5 2018-10-04 omar.polo /* The number of char to read */
58 4f769efa 2018-10-06 omar.polo #define STDIN_CHUNKS 128
60 1d13acf5 2018-10-04 omar.polo /* The number of lines to allocate in advance */
61 4f769efa 2018-10-06 omar.polo #define LINES_CHUNK 64
63 4c0d79a0 2018-10-09 omar.polo #define inner_height(r) (r->height - r->borders[0] - r->borders[2])
64 c758d8da 2019-10-18 omar.polo #define inner_width(r) (r->width - r->borders[1] - r->borders[3])
66 1d13acf5 2018-10-04 omar.polo /* The states of the event loop */
67 c758d8da 2019-10-18 omar.polo enum state { LOOPING, OK_LOOP, OK, ERR };
70 1d13acf5 2018-10-04 omar.polo * For the drawing-related function. The text to be rendere could be
71 1d13acf5 2018-10-04 omar.polo * the prompt, a completion or a highlighted completion
73 c758d8da 2019-10-18 omar.polo enum obj_type { PROMPT, COMPL, COMPL_HIGH };
75 1d13acf5 2018-10-04 omar.polo /* These are the possible action to be performed after user input. */
76 2128b469 2018-06-30 omar.polo enum action {
79 1d13acf5 2018-10-04 omar.polo CONFIRM,
80 1d13acf5 2018-10-04 omar.polo CONFIRM_CONTINUE,
81 1d13acf5 2018-10-04 omar.polo NEXT_COMPL,
82 1d13acf5 2018-10-04 omar.polo PREV_COMPL,
83 1d13acf5 2018-10-04 omar.polo DEL_CHAR,
84 1d13acf5 2018-10-04 omar.polo DEL_WORD,
85 1d13acf5 2018-10-04 omar.polo DEL_LINE,
86 1d13acf5 2018-10-04 omar.polo ADD_CHAR,
87 a81dbe5d 2019-10-19 omar.polo TOGGLE_FIRST_SELECTED,
88 a81dbe5d 2019-10-19 omar.polo SCROLL_DOWN,
89 a81dbe5d 2019-10-19 omar.polo SCROLL_UP,
92 1d13acf5 2018-10-04 omar.polo /* A big set of values that needs to be carried around for drawing. A
93 a81dbe5d 2019-10-19 omar.polo * big struct to rule them all */
94 f5e234d6 2018-05-18 omar.polo struct rendering {
95 c758d8da 2019-10-18 omar.polo Display *d; /* Connection to xorg */
96 c758d8da 2019-10-18 omar.polo Window w;
97 c758d8da 2019-10-18 omar.polo XIM xim;
98 c758d8da 2019-10-18 omar.polo int width;
99 c758d8da 2019-10-18 omar.polo int height;
100 c758d8da 2019-10-18 omar.polo int p_padding[4];
101 c758d8da 2019-10-18 omar.polo int c_padding[4];
102 c758d8da 2019-10-18 omar.polo int ch_padding[4];
103 c758d8da 2019-10-18 omar.polo int x_zero; /* the "zero" on the x axis (may not be exactly 0 'cause
104 c758d8da 2019-10-18 omar.polo the borders) */
105 c758d8da 2019-10-18 omar.polo int y_zero; /* like x_zero but for the y axis */
107 a81dbe5d 2019-10-19 omar.polo int offset; /* scroll offset */
109 c758d8da 2019-10-18 omar.polo short free_text;
110 c758d8da 2019-10-18 omar.polo short first_selected;
111 c758d8da 2019-10-18 omar.polo short multiple_select;
113 1d13acf5 2018-10-04 omar.polo /* four border width */
114 c758d8da 2019-10-18 omar.polo int borders[4];
115 c758d8da 2019-10-18 omar.polo int p_borders[4];
116 c758d8da 2019-10-18 omar.polo int c_borders[4];
117 c758d8da 2019-10-18 omar.polo int ch_borders[4];
119 c758d8da 2019-10-18 omar.polo short horizontal_layout;
121 1d13acf5 2018-10-04 omar.polo /* prompt */
122 c758d8da 2019-10-18 omar.polo char *ps1;
123 c758d8da 2019-10-18 omar.polo int ps1len;
124 c758d8da 2019-10-18 omar.polo int ps1w; /* ps1 width */
125 c758d8da 2019-10-18 omar.polo int ps1h; /* ps1 height */
127 c758d8da 2019-10-18 omar.polo int text_height; /* cache for the vertical layout */
129 c758d8da 2019-10-18 omar.polo XIC xic;
131 1d13acf5 2018-10-04 omar.polo /* colors */
132 c758d8da 2019-10-18 omar.polo GC fgs[4];
133 c758d8da 2019-10-18 omar.polo GC bgs[4];
134 c758d8da 2019-10-18 omar.polo GC borders_bg[4];
135 c758d8da 2019-10-18 omar.polo GC p_borders_bg[4];
136 c758d8da 2019-10-18 omar.polo GC c_borders_bg[4];
137 c758d8da 2019-10-18 omar.polo GC ch_borders_bg[4];
138 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
139 c758d8da 2019-10-18 omar.polo XftFont *font;
140 c758d8da 2019-10-18 omar.polo XftDraw *xftdraw;
141 c758d8da 2019-10-18 omar.polo XftColor xft_colors[3];
143 c758d8da 2019-10-18 omar.polo XFontSet font;
144 c9a3bfaa 2018-05-21 omar.polo #endif
147 b5d751bd 2018-07-07 omar.polo struct completion {
148 c758d8da 2019-10-18 omar.polo char *completion;
149 c758d8da 2019-10-18 omar.polo char *rcompletion;
150 a81dbe5d 2019-10-19 omar.polo int offset; /* the x (or y, depending on the layout) coordinate at
151 a81dbe5d 2019-10-19 omar.polo which the item is rendered */
154 1d13acf5 2018-10-04 omar.polo /* Wrap the linked list of completions */
155 b5d751bd 2018-07-07 omar.polo struct completions {
156 c758d8da 2019-10-18 omar.polo struct completion *completions;
157 c758d8da 2019-10-18 omar.polo ssize_t selected;
158 c758d8da 2019-10-18 omar.polo size_t length;
161 6bb186a7 2018-09-13 omar.polo /* idea stolen from lemonbar. ty lemonboy */
162 6bb186a7 2018-09-13 omar.polo typedef union {
163 1d13acf5 2018-10-04 omar.polo struct {
164 c758d8da 2019-10-18 omar.polo uint8_t b;
165 c758d8da 2019-10-18 omar.polo uint8_t g;
166 c758d8da 2019-10-18 omar.polo uint8_t r;
167 c758d8da 2019-10-18 omar.polo uint8_t a;
168 4f769efa 2018-10-06 omar.polo } rgba;
169 c758d8da 2019-10-18 omar.polo uint32_t v;
170 6bb186a7 2018-09-13 omar.polo } rgba_t;
172 c758d8da 2019-10-18 omar.polo extern char *optarg;
173 c758d8da 2019-10-18 omar.polo extern int optind;
175 1d13acf5 2018-10-04 omar.polo /* Return a newly allocated (and empty) completion list */
176 1d13acf5 2018-10-04 omar.polo struct completions *
177 1d13acf5 2018-10-04 omar.polo compls_new(size_t length)
179 1d13acf5 2018-10-04 omar.polo struct completions *cs = malloc(sizeof(struct completions));
181 1d13acf5 2018-10-04 omar.polo if (cs == NULL)
182 1d13acf5 2018-10-04 omar.polo return cs;
184 1d13acf5 2018-10-04 omar.polo cs->completions = calloc(length, sizeof(struct completion));
185 1d13acf5 2018-10-04 omar.polo if (cs->completions == NULL) {
186 1d13acf5 2018-10-04 omar.polo free(cs);
187 1d13acf5 2018-10-04 omar.polo return NULL;
190 1d13acf5 2018-10-04 omar.polo cs->selected = -1;
191 1d13acf5 2018-10-04 omar.polo cs->length = length;
192 1d13acf5 2018-10-04 omar.polo return cs;
195 1d13acf5 2018-10-04 omar.polo /* Delete the wrapper and the whole list */
197 1d13acf5 2018-10-04 omar.polo compls_delete(struct completions *cs)
199 1d13acf5 2018-10-04 omar.polo if (cs == NULL)
200 1d13acf5 2018-10-04 omar.polo return;
202 1d13acf5 2018-10-04 omar.polo free(cs->completions);
203 1d13acf5 2018-10-04 omar.polo free(cs);
207 1d13acf5 2018-10-04 omar.polo * Create a completion list from a text and the list of possible
208 1d13acf5 2018-10-04 omar.polo * completions (null terminated). Expects a non-null `cs'. `lines' and
209 1d13acf5 2018-10-04 omar.polo * `vlines' should have the same length OR `vlines' is NULL.
212 1d13acf5 2018-10-04 omar.polo filter(struct completions *cs, char *text, char **lines, char **vlines)
214 c758d8da 2019-10-18 omar.polo size_t index = 0;
215 c758d8da 2019-10-18 omar.polo size_t matching = 0;
216 c758d8da 2019-10-18 omar.polo char *l;
218 1d13acf5 2018-10-04 omar.polo if (vlines == NULL)
219 1d13acf5 2018-10-04 omar.polo vlines = lines;
221 26b541d8 2018-10-04 omar.polo while (1) {
222 1d13acf5 2018-10-04 omar.polo if (lines[index] == NULL)
223 1d13acf5 2018-10-04 omar.polo break;
225 4f769efa 2018-10-06 omar.polo l = vlines[index] != NULL ? vlines[index] : lines[index];
227 1d13acf5 2018-10-04 omar.polo if (strcasestr(l, text) != NULL) {
228 1d13acf5 2018-10-04 omar.polo struct completion *c = &cs->completions[matching];
229 c758d8da 2019-10-18 omar.polo c->completion = l;
230 1d13acf5 2018-10-04 omar.polo c->rcompletion = lines[index];
231 1d13acf5 2018-10-04 omar.polo matching++;
234 1d13acf5 2018-10-04 omar.polo index++;
236 1d13acf5 2018-10-04 omar.polo cs->length = matching;
237 1d13acf5 2018-10-04 omar.polo cs->selected = -1;
240 1d13acf5 2018-10-04 omar.polo /* Update the given completion */
242 c758d8da 2019-10-18 omar.polo update_completions(struct completions *cs, char *text, char **lines,
243 c758d8da 2019-10-18 omar.polo char **vlines, short first_selected)
245 1d13acf5 2018-10-04 omar.polo filter(cs, text, lines, vlines);
246 1d13acf5 2018-10-04 omar.polo if (first_selected && cs->length > 0)
247 1d13acf5 2018-10-04 omar.polo cs->selected = 0;
251 1d13acf5 2018-10-04 omar.polo * Select the next or previous selection and update some state. `text'
252 1d13acf5 2018-10-04 omar.polo * will be updated with the text of the completion and `textlen' with
253 1d13acf5 2018-10-04 omar.polo * the new length. If the memory cannot be allocated `status' will be
254 1d13acf5 2018-10-04 omar.polo * set to `ERR'.
257 c758d8da 2019-10-18 omar.polo complete(struct completions *cs, short first_selected, short p, char **text,
258 c758d8da 2019-10-18 omar.polo int *textlen, enum state *status)
260 c758d8da 2019-10-18 omar.polo struct completion *n;
261 c758d8da 2019-10-18 omar.polo int index;
263 1d13acf5 2018-10-04 omar.polo if (cs == NULL || cs->length == 0)
264 1d13acf5 2018-10-04 omar.polo return;
267 1d13acf5 2018-10-04 omar.polo * If the first is always selected and the first entry is
268 1d13acf5 2018-10-04 omar.polo * different from the text, expand the text and return
270 c758d8da 2019-10-18 omar.polo if (first_selected && cs->selected == 0
271 c758d8da 2019-10-18 omar.polo && strcmp(cs->completions->completion, *text) != 0 && !p) {
272 1d13acf5 2018-10-04 omar.polo free(*text);
273 1d13acf5 2018-10-04 omar.polo *text = strdup(cs->completions->completion);
274 1d13acf5 2018-10-04 omar.polo if (text == NULL) {
275 1d13acf5 2018-10-04 omar.polo *status = ERR;
276 1d13acf5 2018-10-04 omar.polo return;
278 1d13acf5 2018-10-04 omar.polo *textlen = strlen(*text);
279 1d13acf5 2018-10-04 omar.polo return;
282 1d13acf5 2018-10-04 omar.polo index = cs->selected;
284 1d13acf5 2018-10-04 omar.polo if (index == -1 && p)
285 1d13acf5 2018-10-04 omar.polo index = 0;
286 c758d8da 2019-10-18 omar.polo index = cs->selected
287 c758d8da 2019-10-18 omar.polo = (cs->length + (p ? index - 1 : index + 1)) % cs->length;
289 1d13acf5 2018-10-04 omar.polo n = &cs->completions[cs->selected];
291 1d13acf5 2018-10-04 omar.polo free(*text);
292 1d13acf5 2018-10-04 omar.polo *text = strdup(n->completion);
293 1d13acf5 2018-10-04 omar.polo if (text == NULL) {
294 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Memory allocation error!\n");
295 1d13acf5 2018-10-04 omar.polo *status = ERR;
296 1d13acf5 2018-10-04 omar.polo return;
298 1d13acf5 2018-10-04 omar.polo *textlen = strlen(*text);
301 1d13acf5 2018-10-04 omar.polo /* Push the character c at the end of the string pointed by p */
303 1d13acf5 2018-10-04 omar.polo pushc(char **p, int maxlen, char c)
305 c758d8da 2019-10-18 omar.polo int len;
307 4f769efa 2018-10-06 omar.polo len = strnlen(*p, maxlen);
308 c758d8da 2019-10-18 omar.polo if (!(len < maxlen - 2)) {
309 1d13acf5 2018-10-04 omar.polo char *newptr;
311 1d13acf5 2018-10-04 omar.polo maxlen += maxlen >> 1;
312 1d13acf5 2018-10-04 omar.polo newptr = realloc(*p, maxlen);
313 1d13acf5 2018-10-04 omar.polo if (newptr == NULL) /* bad */
314 1d13acf5 2018-10-04 omar.polo return -1;
315 1d13acf5 2018-10-04 omar.polo *p = newptr;
318 1d13acf5 2018-10-04 omar.polo (*p)[len] = c;
319 c758d8da 2019-10-18 omar.polo (*p)[len + 1] = '\0';
320 1d13acf5 2018-10-04 omar.polo return maxlen;
324 1d13acf5 2018-10-04 omar.polo * Remove the last rune from the *UTF-8* string! This is different
325 1d13acf5 2018-10-04 omar.polo * from just setting the last byte to 0 (in some cases ofc). Return a
326 1d13acf5 2018-10-04 omar.polo * pointer (e) to the last nonzero char. If e < p then p is empty!
328 1d13acf5 2018-10-04 omar.polo char *
329 1d13acf5 2018-10-04 omar.polo popc(char *p)
331 1d13acf5 2018-10-04 omar.polo int len = strlen(p);
332 1d13acf5 2018-10-04 omar.polo char *e;
334 1d13acf5 2018-10-04 omar.polo if (len == 0)
335 1d13acf5 2018-10-04 omar.polo return p;
337 1d13acf5 2018-10-04 omar.polo e = p + len - 1;
340 1d13acf5 2018-10-04 omar.polo char c = *e;
342 4f769efa 2018-10-06 omar.polo *e = '\0';
343 4f769efa 2018-10-06 omar.polo e -= 1;
346 1d13acf5 2018-10-04 omar.polo * If c is a starting byte (11......) or is under
347 1d13acf5 2018-10-04 omar.polo * U+007F we're done.
349 1d13acf5 2018-10-04 omar.polo if (((c & 0x80) && (c & 0x40)) || !(c & 0x80))
350 1d13acf5 2018-10-04 omar.polo break;
351 1d13acf5 2018-10-04 omar.polo } while (e >= p);
353 1d13acf5 2018-10-04 omar.polo return e;
356 1d13acf5 2018-10-04 omar.polo /* Remove the last word plus trailing white spaces from the given string */
358 1d13acf5 2018-10-04 omar.polo popw(char *w)
360 c758d8da 2019-10-18 omar.polo int len;
361 c758d8da 2019-10-18 omar.polo short in_word = 1;
363 4f769efa 2018-10-06 omar.polo if ((len = strlen(w)) == 0)
364 1d13acf5 2018-10-04 omar.polo return;
366 26b541d8 2018-10-04 omar.polo while (1) {
367 1d13acf5 2018-10-04 omar.polo char *e = popc(w);
369 1d13acf5 2018-10-04 omar.polo if (e < w)
370 1d13acf5 2018-10-04 omar.polo return;
372 1d13acf5 2018-10-04 omar.polo if (in_word && isspace(*e))
373 26b541d8 2018-10-04 omar.polo in_word = 0;
375 1d13acf5 2018-10-04 omar.polo if (!in_word && !isspace(*e))
376 1d13acf5 2018-10-04 omar.polo return;
381 1d13acf5 2018-10-04 omar.polo * If the string is surrounded by quates (`"') remove them and replace
382 1d13acf5 2018-10-04 omar.polo * every `\"' in the string with a single double-quote.
384 1d13acf5 2018-10-04 omar.polo char *
385 1d13acf5 2018-10-04 omar.polo normalize_str(const char *str)
387 c758d8da 2019-10-18 omar.polo int len, p;
388 c758d8da 2019-10-18 omar.polo char *s;
390 4f769efa 2018-10-06 omar.polo if ((len = strlen(str)) == 0)
391 1d13acf5 2018-10-04 omar.polo return NULL;
393 92803b00 2019-10-19 omar.polo if ((s = calloc(len, sizeof(char))) == NULL)
394 92803b00 2019-10-19 omar.polo err(1, "calloc");
395 1d13acf5 2018-10-04 omar.polo p = 0;
397 1d13acf5 2018-10-04 omar.polo while (*str) {
398 1d13acf5 2018-10-04 omar.polo char c = *str;
400 1d13acf5 2018-10-04 omar.polo if (*str == '\\') {
401 1d13acf5 2018-10-04 omar.polo if (*(str + 1)) {
402 1d13acf5 2018-10-04 omar.polo s[p] = *(str + 1);
404 1d13acf5 2018-10-04 omar.polo str += 2; /* skip this and the next char */
405 1d13acf5 2018-10-04 omar.polo continue;
406 1d13acf5 2018-10-04 omar.polo } else
407 1d13acf5 2018-10-04 omar.polo break;
409 1d13acf5 2018-10-04 omar.polo if (c == '"') {
410 c758d8da 2019-10-18 omar.polo str++; /* skip only this char */
411 1d13acf5 2018-10-04 omar.polo continue;
413 1d13acf5 2018-10-04 omar.polo s[p] = c;
415 1d13acf5 2018-10-04 omar.polo str++;
418 1d13acf5 2018-10-04 omar.polo return s;
421 1d13acf5 2018-10-04 omar.polo size_t
422 1d13acf5 2018-10-04 omar.polo read_stdin(char **buf)
424 c758d8da 2019-10-18 omar.polo size_t offset = 0;
425 c758d8da 2019-10-18 omar.polo size_t len = STDIN_CHUNKS;
427 1d13acf5 2018-10-04 omar.polo *buf = malloc(len * sizeof(char));
428 1d13acf5 2018-10-04 omar.polo if (*buf == NULL)
429 1d13acf5 2018-10-04 omar.polo goto err;
431 26b541d8 2018-10-04 omar.polo while (1) {
432 1d13acf5 2018-10-04 omar.polo ssize_t r;
433 1d13acf5 2018-10-04 omar.polo size_t i;
435 1d13acf5 2018-10-04 omar.polo r = read(0, *buf + offset, STDIN_CHUNKS);
437 1d13acf5 2018-10-04 omar.polo if (r < 1)
438 1d13acf5 2018-10-04 omar.polo return len;
440 1d13acf5 2018-10-04 omar.polo offset += r;
442 1d13acf5 2018-10-04 omar.polo len += STDIN_CHUNKS;
443 1d13acf5 2018-10-04 omar.polo *buf = realloc(*buf, len);
444 1d13acf5 2018-10-04 omar.polo if (*buf == NULL)
445 1d13acf5 2018-10-04 omar.polo goto err;
447 1d13acf5 2018-10-04 omar.polo for (i = offset; i < len; ++i)
448 1d13acf5 2018-10-04 omar.polo (*buf)[i] = '\0';
452 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Error in allocating memory for stdin.\n");
453 1d13acf5 2018-10-04 omar.polo exit(EX_UNAVAILABLE);
456 1d13acf5 2018-10-04 omar.polo size_t
457 1d13acf5 2018-10-04 omar.polo readlines(char ***lns, char **buf)
459 c758d8da 2019-10-18 omar.polo size_t i, len, ll, lines;
460 c758d8da 2019-10-18 omar.polo short in_line = 0;
462 1d13acf5 2018-10-04 omar.polo lines = 0;
464 1d13acf5 2018-10-04 omar.polo *buf = NULL;
465 1d13acf5 2018-10-04 omar.polo len = read_stdin(buf);
467 1d13acf5 2018-10-04 omar.polo ll = LINES_CHUNK;
468 c758d8da 2019-10-18 omar.polo *lns = malloc(ll * sizeof(char *));
470 1d13acf5 2018-10-04 omar.polo if (*lns == NULL)
471 1d13acf5 2018-10-04 omar.polo goto err;
473 4f769efa 2018-10-06 omar.polo for (i = 0; i < len; i++) {
474 1d13acf5 2018-10-04 omar.polo char c = (*buf)[i];
476 1d13acf5 2018-10-04 omar.polo if (c == '\0')
477 1d13acf5 2018-10-04 omar.polo break;
479 1d13acf5 2018-10-04 omar.polo if (c == '\n')
480 1d13acf5 2018-10-04 omar.polo (*buf)[i] = '\0';
482 1d13acf5 2018-10-04 omar.polo if (in_line && c == '\n')
483 26b541d8 2018-10-04 omar.polo in_line = 0;
485 1d13acf5 2018-10-04 omar.polo if (!in_line && c != '\n') {
486 26b541d8 2018-10-04 omar.polo in_line = 1;
487 1d13acf5 2018-10-04 omar.polo (*lns)[lines] = (*buf) + i;
488 1d13acf5 2018-10-04 omar.polo lines++;
490 26b541d8 2018-10-04 omar.polo if (lines == ll) { /* resize */
491 1d13acf5 2018-10-04 omar.polo ll += LINES_CHUNK;
492 c758d8da 2019-10-18 omar.polo *lns = realloc(*lns, ll * sizeof(char *));
493 1d13acf5 2018-10-04 omar.polo if (*lns == NULL)
494 1d13acf5 2018-10-04 omar.polo goto err;
499 1d13acf5 2018-10-04 omar.polo (*lns)[lines] = NULL;
501 1d13acf5 2018-10-04 omar.polo return lines;
504 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Error in memory allocation.\n");
505 1d13acf5 2018-10-04 omar.polo exit(EX_UNAVAILABLE);
509 1d13acf5 2018-10-04 omar.polo * Compute the dimensions of the string str once rendered.
510 1d13acf5 2018-10-04 omar.polo * It'll return the width and set ret_width and ret_height if not NULL
513 c758d8da 2019-10-18 omar.polo text_extents(char *str, int len, struct rendering *r, int *ret_width,
514 c758d8da 2019-10-18 omar.polo int *ret_height)
516 c758d8da 2019-10-18 omar.polo int height, width;
517 1d13acf5 2018-10-04 omar.polo #ifdef USE_XFT
518 c758d8da 2019-10-18 omar.polo XGlyphInfo gi;
519 1d13acf5 2018-10-04 omar.polo XftTextExtentsUtf8(r->d, r->font, str, len, &gi);
520 1d13acf5 2018-10-04 omar.polo height = r->font->ascent - r->font->descent;
521 1d13acf5 2018-10-04 omar.polo width = gi.width - gi.x;
523 c758d8da 2019-10-18 omar.polo XRectangle rect;
524 1d13acf5 2018-10-04 omar.polo XmbTextExtents(r->font, str, len, NULL, &rect);
525 1d13acf5 2018-10-04 omar.polo height = rect.height;
526 1d13acf5 2018-10-04 omar.polo width = rect.width;
527 1d13acf5 2018-10-04 omar.polo #endif
528 c758d8da 2019-10-18 omar.polo if (ret_width != NULL)
529 c758d8da 2019-10-18 omar.polo *ret_width = width;
530 c758d8da 2019-10-18 omar.polo if (ret_height != NULL)
531 c758d8da 2019-10-18 omar.polo *ret_height = height;
532 1d13acf5 2018-10-04 omar.polo return width;
536 c758d8da 2019-10-18 omar.polo draw_string(char *str, int len, int x, int y, struct rendering *r,
537 c758d8da 2019-10-18 omar.polo enum obj_type tt)
539 1d13acf5 2018-10-04 omar.polo #ifdef USE_XFT
540 1d13acf5 2018-10-04 omar.polo XftColor xftcolor;
541 c758d8da 2019-10-18 omar.polo if (tt == PROMPT)
542 c758d8da 2019-10-18 omar.polo xftcolor = r->xft_colors[0];
543 c758d8da 2019-10-18 omar.polo if (tt == COMPL)
544 c758d8da 2019-10-18 omar.polo xftcolor = r->xft_colors[1];
545 c758d8da 2019-10-18 omar.polo if (tt == COMPL_HIGH)
546 c758d8da 2019-10-18 omar.polo xftcolor = r->xft_colors[2];
548 1d13acf5 2018-10-04 omar.polo XftDrawStringUtf8(r->xftdraw, &xftcolor, r->font, x, y, str, len);
550 1d13acf5 2018-10-04 omar.polo GC gc;
551 c758d8da 2019-10-18 omar.polo if (tt == PROMPT)
552 c758d8da 2019-10-18 omar.polo gc = r->fgs[0];
553 c758d8da 2019-10-18 omar.polo if (tt == COMPL)
554 c758d8da 2019-10-18 omar.polo gc = r->fgs[1];
555 c758d8da 2019-10-18 omar.polo if (tt == COMPL_HIGH)
556 c758d8da 2019-10-18 omar.polo gc = r->fgs[2];
557 1d13acf5 2018-10-04 omar.polo Xutf8DrawString(r->d, r->w, r->font, gc, x, y, str, len);
558 1d13acf5 2018-10-04 omar.polo #endif
561 1d13acf5 2018-10-04 omar.polo /* Duplicate the string and substitute every space with a 'n` */
562 1d13acf5 2018-10-04 omar.polo char *
563 1d13acf5 2018-10-04 omar.polo strdupn(char *str)
565 c758d8da 2019-10-18 omar.polo int len, i;
566 c758d8da 2019-10-18 omar.polo char *dup;
568 1d13acf5 2018-10-04 omar.polo len = strlen(str);
570 1d13acf5 2018-10-04 omar.polo if (str == NULL || len == 0)
571 1d13acf5 2018-10-04 omar.polo return NULL;
573 4f769efa 2018-10-06 omar.polo if ((dup = strdup(str)) == NULL)
574 1d13acf5 2018-10-04 omar.polo return NULL;
576 1d13acf5 2018-10-04 omar.polo for (i = 0; i < len; ++i)
577 1d13acf5 2018-10-04 omar.polo if (dup[i] == ' ')
578 1d13acf5 2018-10-04 omar.polo dup[i] = 'n';
580 1d13acf5 2018-10-04 omar.polo return dup;
584 c758d8da 2019-10-18 omar.polo draw_v_box(struct rendering *r, int y, char *prefix, int prefix_width,
585 c758d8da 2019-10-18 omar.polo enum obj_type t, char *text)
587 c758d8da 2019-10-18 omar.polo GC *border_color, bg;
588 c758d8da 2019-10-18 omar.polo int *padding, *borders;
589 c758d8da 2019-10-18 omar.polo int ret = 0, inner_width, inner_height, x;
591 df29e05e 2018-10-17 omar.polo switch (t) {
592 df29e05e 2018-10-17 omar.polo case PROMPT:
593 c758d8da 2019-10-18 omar.polo border_color = r->p_borders_bg;
594 c758d8da 2019-10-18 omar.polo padding = r->p_padding;
595 c758d8da 2019-10-18 omar.polo borders = r->p_borders;
596 c758d8da 2019-10-18 omar.polo bg = r->bgs[0];
597 df29e05e 2018-10-17 omar.polo break;
598 df29e05e 2018-10-17 omar.polo case COMPL:
599 c758d8da 2019-10-18 omar.polo border_color = r->c_borders_bg;
600 c758d8da 2019-10-18 omar.polo padding = r->c_padding;
601 c758d8da 2019-10-18 omar.polo borders = r->c_borders;
602 c758d8da 2019-10-18 omar.polo bg = r->bgs[1];
603 df29e05e 2018-10-17 omar.polo break;
604 df29e05e 2018-10-17 omar.polo case COMPL_HIGH:
605 c758d8da 2019-10-18 omar.polo border_color = r->ch_borders_bg;
606 c758d8da 2019-10-18 omar.polo padding = r->ch_padding;
607 c758d8da 2019-10-18 omar.polo borders = r->ch_borders;
608 c758d8da 2019-10-18 omar.polo bg = r->bgs[2];
609 df29e05e 2018-10-17 omar.polo break;
612 c758d8da 2019-10-18 omar.polo ret = borders[0] + padding[0] + r->text_height + padding[2]
613 c758d8da 2019-10-18 omar.polo + borders[2];
615 df29e05e 2018-10-17 omar.polo inner_width = inner_width(r) - borders[1] - borders[3];
616 df29e05e 2018-10-17 omar.polo inner_height = padding[0] + r->text_height + padding[2];
618 df29e05e 2018-10-17 omar.polo /* Border top */
619 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, border_color[0], r->x_zero, y, r->width,
620 c758d8da 2019-10-18 omar.polo borders[0]);
622 df29e05e 2018-10-17 omar.polo /* Border right */
623 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, border_color[1],
624 c758d8da 2019-10-18 omar.polo r->x_zero + inner_width(r) - borders[1], y, borders[1], ret);
626 df29e05e 2018-10-17 omar.polo /* Border bottom */
627 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, border_color[2], r->x_zero,
628 c758d8da 2019-10-18 omar.polo y + borders[0] + padding[0] + r->text_height + padding[2],
629 c758d8da 2019-10-18 omar.polo r->width, borders[2]);
631 df29e05e 2018-10-17 omar.polo /* Border left */
632 c758d8da 2019-10-18 omar.polo XFillRectangle(
633 c758d8da 2019-10-18 omar.polo r->d, r->w, border_color[3], r->x_zero, y, borders[3], ret);
635 df29e05e 2018-10-17 omar.polo /* bg */
636 df29e05e 2018-10-17 omar.polo x = r->x_zero + borders[3];
637 df29e05e 2018-10-17 omar.polo y += borders[0];
638 df29e05e 2018-10-17 omar.polo XFillRectangle(r->d, r->w, bg, x, y, inner_width, inner_height);
640 df29e05e 2018-10-17 omar.polo /* content */
641 df29e05e 2018-10-17 omar.polo y += padding[0] + r->text_height;
642 df29e05e 2018-10-17 omar.polo x += padding[3];
643 df29e05e 2018-10-17 omar.polo if (prefix != NULL) {
644 df29e05e 2018-10-17 omar.polo draw_string(prefix, strlen(prefix), x, y, r, t);
645 df29e05e 2018-10-17 omar.polo x += prefix_width;
647 df29e05e 2018-10-17 omar.polo draw_string(text, strlen(text), x, y, r, t);
649 df29e05e 2018-10-17 omar.polo return ret;
653 c758d8da 2019-10-18 omar.polo draw_h_box(struct rendering *r, int x, char *prefix, int prefix_width,
654 c758d8da 2019-10-18 omar.polo enum obj_type t, char *text)
656 c758d8da 2019-10-18 omar.polo GC *border_color, bg;
657 c758d8da 2019-10-18 omar.polo int *padding, *borders;
658 c758d8da 2019-10-18 omar.polo int ret = 0, inner_width, inner_height, y, text_width;
660 df29e05e 2018-10-17 omar.polo switch (t) {
661 df29e05e 2018-10-17 omar.polo case PROMPT:
662 c758d8da 2019-10-18 omar.polo border_color = r->p_borders_bg;
663 c758d8da 2019-10-18 omar.polo padding = r->p_padding;
664 c758d8da 2019-10-18 omar.polo borders = r->p_borders;
665 c758d8da 2019-10-18 omar.polo bg = r->bgs[0];
666 df29e05e 2018-10-17 omar.polo break;
667 df29e05e 2018-10-17 omar.polo case COMPL:
668 c758d8da 2019-10-18 omar.polo border_color = r->c_borders_bg;
669 c758d8da 2019-10-18 omar.polo padding = r->c_padding;
670 c758d8da 2019-10-18 omar.polo borders = r->c_borders;
671 c758d8da 2019-10-18 omar.polo bg = r->bgs[1];
672 df29e05e 2018-10-17 omar.polo break;
673 df29e05e 2018-10-17 omar.polo case COMPL_HIGH:
674 c758d8da 2019-10-18 omar.polo border_color = r->ch_borders_bg;
675 c758d8da 2019-10-18 omar.polo padding = r->ch_padding;
676 c758d8da 2019-10-18 omar.polo borders = r->ch_borders;
677 c758d8da 2019-10-18 omar.polo bg = r->bgs[2];
678 df29e05e 2018-10-17 omar.polo break;
681 df29e05e 2018-10-17 omar.polo if (padding[0] < 0 || padding[2] < 0)
682 c758d8da 2019-10-18 omar.polo padding[0] = padding[2]
683 c758d8da 2019-10-18 omar.polo = (inner_height(r) - borders[0] - borders[2]
684 c758d8da 2019-10-18 omar.polo - r->text_height)
687 df29e05e 2018-10-17 omar.polo /* If they are still lesser than 0, set 'em to 0 */
688 df29e05e 2018-10-17 omar.polo if (padding[0] < 0 || padding[2] < 0)
689 df29e05e 2018-10-17 omar.polo padding[0] = padding[2] = 0;
691 df29e05e 2018-10-17 omar.polo /* Get the text width */
692 df29e05e 2018-10-17 omar.polo text_extents(text, strlen(text), r, &text_width, NULL);
693 df29e05e 2018-10-17 omar.polo if (prefix != NULL)
694 df29e05e 2018-10-17 omar.polo text_width += prefix_width;
696 df29e05e 2018-10-17 omar.polo ret = borders[3] + padding[3] + text_width + padding[1] + borders[1];
698 df29e05e 2018-10-17 omar.polo inner_width = padding[3] + text_width + padding[1];
699 df29e05e 2018-10-17 omar.polo inner_height = inner_height(r) - borders[0] - borders[2];
701 df29e05e 2018-10-17 omar.polo /* Border top */
702 c758d8da 2019-10-18 omar.polo XFillRectangle(
703 c758d8da 2019-10-18 omar.polo r->d, r->w, border_color[0], x, r->y_zero, ret, borders[0]);
705 df29e05e 2018-10-17 omar.polo /* Border right */
706 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, border_color[1],
707 c758d8da 2019-10-18 omar.polo x + borders[3] + inner_width, r->y_zero, borders[1],
708 c758d8da 2019-10-18 omar.polo inner_height(r));
710 df29e05e 2018-10-17 omar.polo /* Border bottom */
711 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, border_color[2], x,
712 c758d8da 2019-10-18 omar.polo r->y_zero + inner_height(r) - borders[2], ret, borders[2]);
714 df29e05e 2018-10-17 omar.polo /* Border left */
715 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, border_color[3], x, r->y_zero, borders[3],
716 c758d8da 2019-10-18 omar.polo inner_height(r));
718 df29e05e 2018-10-17 omar.polo /* bg */
719 df29e05e 2018-10-17 omar.polo x += borders[3];
720 df29e05e 2018-10-17 omar.polo y = r->y_zero + borders[0];
721 df29e05e 2018-10-17 omar.polo XFillRectangle(r->d, r->w, bg, x, y, inner_width, inner_height);
723 df29e05e 2018-10-17 omar.polo /* content */
724 df29e05e 2018-10-17 omar.polo y += padding[0] + r->text_height;
725 df29e05e 2018-10-17 omar.polo x += padding[3];
726 df29e05e 2018-10-17 omar.polo if (prefix != NULL) {
727 df29e05e 2018-10-17 omar.polo draw_string(prefix, strlen(prefix), x, y, r, t);
728 df29e05e 2018-10-17 omar.polo x += prefix_width;
730 df29e05e 2018-10-17 omar.polo draw_string(text, strlen(text), x, y, r, t);
732 df29e05e 2018-10-17 omar.polo return ret;
735 51986bb8 2018-10-06 omar.polo /* ,-----------------------------------------------------------------, */
736 df29e05e 2018-10-17 omar.polo /* | 20 char text | completion | completion | completion | compl | */
737 df29e05e 2018-10-17 omar.polo /* `-----------------------------------------------------------------' */
739 df29e05e 2018-10-17 omar.polo draw_horizontally(struct rendering *r, char *text, struct completions *cs)
741 c758d8da 2019-10-18 omar.polo size_t i;
742 c758d8da 2019-10-18 omar.polo int x = r->x_zero;
744 df29e05e 2018-10-17 omar.polo /* Draw the prompt */
745 df29e05e 2018-10-17 omar.polo x += draw_h_box(r, x, r->ps1, r->ps1w, PROMPT, text);
747 df29e05e 2018-10-17 omar.polo for (i = r->offset; i < cs->length; ++i) {
748 c758d8da 2019-10-18 omar.polo enum obj_type t
749 c758d8da 2019-10-18 omar.polo = cs->selected == (ssize_t)i ? COMPL_HIGH : COMPL;
751 a81dbe5d 2019-10-19 omar.polo cs->completions[i].offset = x;
753 c758d8da 2019-10-18 omar.polo x += draw_h_box(
754 c758d8da 2019-10-18 omar.polo r, x, NULL, 0, t, cs->completions[i].completion);
756 df29e05e 2018-10-17 omar.polo if (x > inner_width(r))
757 df29e05e 2018-10-17 omar.polo break;
760 a81dbe5d 2019-10-19 omar.polo for (i += 1; i < cs->length; ++i)
761 a81dbe5d 2019-10-19 omar.polo cs->completions[i].offset = -1;
764 df29e05e 2018-10-17 omar.polo /* ,-----------------------------------------------------------------, */
765 1d13acf5 2018-10-04 omar.polo /* | prompt | */
766 1d13acf5 2018-10-04 omar.polo /* |-----------------------------------------------------------------| */
767 1d13acf5 2018-10-04 omar.polo /* | completion | */
768 1d13acf5 2018-10-04 omar.polo /* |-----------------------------------------------------------------| */
769 1d13acf5 2018-10-04 omar.polo /* | completion | */
770 51986bb8 2018-10-06 omar.polo /* `-----------------------------------------------------------------' */
772 1d13acf5 2018-10-04 omar.polo draw_vertically(struct rendering *r, char *text, struct completions *cs)
774 c758d8da 2019-10-18 omar.polo size_t i;
775 c758d8da 2019-10-18 omar.polo int y = r->y_zero;
777 df29e05e 2018-10-17 omar.polo y += draw_v_box(r, y, r->ps1, r->ps1w, PROMPT, text);
779 1d13acf5 2018-10-04 omar.polo for (i = r->offset; i < cs->length; ++i) {
780 c758d8da 2019-10-18 omar.polo enum obj_type t
781 c758d8da 2019-10-18 omar.polo = cs->selected == (ssize_t)i ? COMPL_HIGH : COMPL;
783 a81dbe5d 2019-10-19 omar.polo cs->completions[i].offset = y;
785 c758d8da 2019-10-18 omar.polo y += draw_v_box(
786 c758d8da 2019-10-18 omar.polo r, y, NULL, 0, t, cs->completions[i].completion);
788 df29e05e 2018-10-17 omar.polo if (y > inner_height(r))
789 df29e05e 2018-10-17 omar.polo break;
792 a81dbe5d 2019-10-19 omar.polo for (i += 1; i < cs->length; ++i)
793 a81dbe5d 2019-10-19 omar.polo cs->completions[i].offset = -1;
797 1d13acf5 2018-10-04 omar.polo draw(struct rendering *r, char *text, struct completions *cs)
799 df29e05e 2018-10-17 omar.polo /* Draw the background */
800 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, r->bgs[1], r->x_zero, r->y_zero,
801 c758d8da 2019-10-18 omar.polo inner_width(r), inner_height(r));
803 df29e05e 2018-10-17 omar.polo /* Draw the contents */
804 1d13acf5 2018-10-04 omar.polo if (r->horizontal_layout)
805 1d13acf5 2018-10-04 omar.polo draw_horizontally(r, text, cs);
807 1d13acf5 2018-10-04 omar.polo draw_vertically(r, text, cs);
809 1d13acf5 2018-10-04 omar.polo /* Draw the borders */
810 4c0d79a0 2018-10-09 omar.polo if (r->borders[0] != 0)
811 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, r->borders_bg[0], 0, 0, r->width,
812 c758d8da 2019-10-18 omar.polo r->borders[0]);
814 4c0d79a0 2018-10-09 omar.polo if (r->borders[1] != 0)
815 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, r->borders_bg[1],
816 c758d8da 2019-10-18 omar.polo r->width - r->borders[1], 0, r->borders[1],
817 c758d8da 2019-10-18 omar.polo r->height);
819 4c0d79a0 2018-10-09 omar.polo if (r->borders[2] != 0)
820 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, r->borders_bg[2], 0,
821 c758d8da 2019-10-18 omar.polo r->height - r->borders[2], r->width, r->borders[2]);
823 4c0d79a0 2018-10-09 omar.polo if (r->borders[3] != 0)
824 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, r->borders_bg[3], 0, 0,
825 c758d8da 2019-10-18 omar.polo r->borders[3], r->height);
827 1d13acf5 2018-10-04 omar.polo /* render! */
828 1d13acf5 2018-10-04 omar.polo XFlush(r->d);
831 1d13acf5 2018-10-04 omar.polo /* Set some WM stuff */
833 1d13acf5 2018-10-04 omar.polo set_win_atoms_hints(Display *d, Window w, int width, int height)
835 c758d8da 2019-10-18 omar.polo Atom type;
836 c758d8da 2019-10-18 omar.polo XClassHint *class_hint;
837 c758d8da 2019-10-18 omar.polo XSizeHints *size_hint;
839 26b541d8 2018-10-04 omar.polo type = XInternAtom(d, "_NET_WM_WINDOW_TYPE_DOCK", 0);
840 c758d8da 2019-10-18 omar.polo XChangeProperty(d, w, XInternAtom(d, "_NET_WM_WINDOW_TYPE", 0),
841 c758d8da 2019-10-18 omar.polo XInternAtom(d, "ATOM", 0), 32, PropModeReplace,
842 c758d8da 2019-10-18 omar.polo (unsigned char *)&type, 1);
844 1d13acf5 2018-10-04 omar.polo /* some window managers honor this properties */
845 26b541d8 2018-10-04 omar.polo type = XInternAtom(d, "_NET_WM_STATE_ABOVE", 0);
846 c758d8da 2019-10-18 omar.polo XChangeProperty(d, w, XInternAtom(d, "_NET_WM_STATE", 0),
847 c758d8da 2019-10-18 omar.polo XInternAtom(d, "ATOM", 0), 32, PropModeReplace,
848 c758d8da 2019-10-18 omar.polo (unsigned char *)&type, 1);
850 26b541d8 2018-10-04 omar.polo type = XInternAtom(d, "_NET_WM_STATE_FOCUSED", 0);
851 c758d8da 2019-10-18 omar.polo XChangeProperty(d, w, XInternAtom(d, "_NET_WM_STATE", 0),
852 c758d8da 2019-10-18 omar.polo XInternAtom(d, "ATOM", 0), 32, PropModeAppend,
853 c758d8da 2019-10-18 omar.polo (unsigned char *)&type, 1);
855 1d13acf5 2018-10-04 omar.polo /* Setting window hints */
856 1d13acf5 2018-10-04 omar.polo class_hint = XAllocClassHint();
857 1d13acf5 2018-10-04 omar.polo if (class_hint == NULL) {
858 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Could not allocate memory for class hint\n");
859 1d13acf5 2018-10-04 omar.polo exit(EX_UNAVAILABLE);
862 1d13acf5 2018-10-04 omar.polo class_hint->res_name = resname;
863 1d13acf5 2018-10-04 omar.polo class_hint->res_class = resclass;
864 1d13acf5 2018-10-04 omar.polo XSetClassHint(d, w, class_hint);
865 1d13acf5 2018-10-04 omar.polo XFree(class_hint);
867 1d13acf5 2018-10-04 omar.polo size_hint = XAllocSizeHints();
868 1d13acf5 2018-10-04 omar.polo if (size_hint == NULL) {
869 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Could not allocate memory for size hint\n");
870 1d13acf5 2018-10-04 omar.polo exit(EX_UNAVAILABLE);
873 1d13acf5 2018-10-04 omar.polo size_hint->flags = PMinSize | PBaseSize;
874 1d13acf5 2018-10-04 omar.polo size_hint->min_width = width;
875 1d13acf5 2018-10-04 omar.polo size_hint->base_width = width;
876 1d13acf5 2018-10-04 omar.polo size_hint->min_height = height;
877 1d13acf5 2018-10-04 omar.polo size_hint->base_height = height;
879 1d13acf5 2018-10-04 omar.polo XFlush(d);
882 1d13acf5 2018-10-04 omar.polo /* Get the width and height of the window `w' */
884 1d13acf5 2018-10-04 omar.polo get_wh(Display *d, Window *w, int *width, int *height)
886 1d13acf5 2018-10-04 omar.polo XWindowAttributes win_attr;
888 1d13acf5 2018-10-04 omar.polo XGetWindowAttributes(d, *w, &win_attr);
889 1d13acf5 2018-10-04 omar.polo *height = win_attr.height;
890 1d13acf5 2018-10-04 omar.polo *width = win_attr.width;
894 1d13acf5 2018-10-04 omar.polo grabfocus(Display *d, Window w)
896 c758d8da 2019-10-18 omar.polo int i;
897 1d13acf5 2018-10-04 omar.polo for (i = 0; i < 100; ++i) {
898 1d13acf5 2018-10-04 omar.polo Window focuswin;
899 1d13acf5 2018-10-04 omar.polo int revert_to_win;
901 1d13acf5 2018-10-04 omar.polo XGetInputFocus(d, &focuswin, &revert_to_win);
903 1d13acf5 2018-10-04 omar.polo if (focuswin == w)
904 26b541d8 2018-10-04 omar.polo return 1;
906 1d13acf5 2018-10-04 omar.polo XSetInputFocus(d, w, RevertToParent, CurrentTime);
907 1d13acf5 2018-10-04 omar.polo usleep(1000);
909 1d13acf5 2018-10-04 omar.polo return 0;
913 1d13acf5 2018-10-04 omar.polo * I know this may seem a little hackish BUT is the only way I managed
914 1d13acf5 2018-10-04 omar.polo * to actually grab that goddam keyboard. Only one call to
915 1d13acf5 2018-10-04 omar.polo * XGrabKeyboard does not always end up with the keyboard grabbed!
918 1d13acf5 2018-10-04 omar.polo take_keyboard(Display *d, Window w)
920 1d13acf5 2018-10-04 omar.polo int i;
921 1d13acf5 2018-10-04 omar.polo for (i = 0; i < 100; i++) {
922 c758d8da 2019-10-18 omar.polo if (XGrabKeyboard(d, w, 1, GrabModeAsync, GrabModeAsync,
923 c758d8da 2019-10-18 omar.polo CurrentTime)
924 c758d8da 2019-10-18 omar.polo == GrabSuccess)
925 1d13acf5 2018-10-04 omar.polo return 1;
926 1d13acf5 2018-10-04 omar.polo usleep(1000);
928 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Cannot grab keyboard\n");
929 1d13acf5 2018-10-04 omar.polo return 0;
932 1d13acf5 2018-10-04 omar.polo unsigned long
933 1d13acf5 2018-10-04 omar.polo parse_color(const char *str, const char *def)
935 1d13acf5 2018-10-04 omar.polo size_t len;
936 1d13acf5 2018-10-04 omar.polo rgba_t tmp;
937 1d13acf5 2018-10-04 omar.polo char *ep;
939 1d13acf5 2018-10-04 omar.polo if (str == NULL)
940 1d13acf5 2018-10-04 omar.polo goto invc;
942 1d13acf5 2018-10-04 omar.polo len = strlen(str);
944 1d13acf5 2018-10-04 omar.polo /* +1 for the # ath the start */
945 1d13acf5 2018-10-04 omar.polo if (*str != '#' || len > 9 || len < 4)
946 1d13acf5 2018-10-04 omar.polo goto invc;
947 c758d8da 2019-10-18 omar.polo ++str; /* skip the # */
949 1d13acf5 2018-10-04 omar.polo errno = 0;
950 1d13acf5 2018-10-04 omar.polo tmp = (rgba_t)(uint32_t)strtoul(str, &ep, 16);
952 1d13acf5 2018-10-04 omar.polo if (errno)
953 1d13acf5 2018-10-04 omar.polo goto invc;
955 c758d8da 2019-10-18 omar.polo switch (len - 1) {
956 1d13acf5 2018-10-04 omar.polo case 3:
957 1d13acf5 2018-10-04 omar.polo /* expand #rgb -> #rrggbb */
958 c758d8da 2019-10-18 omar.polo tmp.v = (tmp.v & 0xf00) * 0x1100 | (tmp.v & 0x0f0) * 0x0110
959 1d13acf5 2018-10-04 omar.polo | (tmp.v & 0x00f) * 0x0011;
960 1d13acf5 2018-10-04 omar.polo case 6:
961 1d13acf5 2018-10-04 omar.polo /* assume 0xff opacity */
962 4f769efa 2018-10-06 omar.polo tmp.rgba.a = 0xff;
963 1d13acf5 2018-10-04 omar.polo break;
964 1d13acf5 2018-10-04 omar.polo } /* colors in #aarrggbb need no adjustments */
966 1d13acf5 2018-10-04 omar.polo /* premultiply the alpha */
967 4f769efa 2018-10-06 omar.polo if (tmp.rgba.a) {
968 4f769efa 2018-10-06 omar.polo tmp.rgba.r = (tmp.rgba.r * tmp.rgba.a) / 255;
969 4f769efa 2018-10-06 omar.polo tmp.rgba.g = (tmp.rgba.g * tmp.rgba.a) / 255;
970 4f769efa 2018-10-06 omar.polo tmp.rgba.b = (tmp.rgba.b * tmp.rgba.a) / 255;
971 1d13acf5 2018-10-04 omar.polo return tmp.v;
974 1d13acf5 2018-10-04 omar.polo return 0U;
977 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Invalid color: \"%s\".\n", str);
978 1d13acf5 2018-10-04 omar.polo if (def != NULL)
979 1d13acf5 2018-10-04 omar.polo return parse_color(def, NULL);
981 1d13acf5 2018-10-04 omar.polo return 0U;
985 26b541d8 2018-10-04 omar.polo * Given a string try to parse it as a number or return `default_value'.
988 1d13acf5 2018-10-04 omar.polo parse_integer(const char *str, int default_value)
990 c758d8da 2019-10-18 omar.polo long lval;
991 c758d8da 2019-10-18 omar.polo char *ep;
993 1d13acf5 2018-10-04 omar.polo errno = 0;
994 1d13acf5 2018-10-04 omar.polo lval = strtol(str, &ep, 10);
996 26b541d8 2018-10-04 omar.polo if (str[0] == '\0' || *ep != '\0') { /* NaN */
997 c758d8da 2019-10-18 omar.polo fprintf(stderr,
998 c758d8da 2019-10-18 omar.polo "'%s' is not a valid number! Using %d as default.\n",
999 c758d8da 2019-10-18 omar.polo str, default_value);
1000 1d13acf5 2018-10-04 omar.polo return default_value;
1003 c758d8da 2019-10-18 omar.polo if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
1004 c758d8da 2019-10-18 omar.polo || (lval > INT_MAX || lval < INT_MIN)) {
1005 c758d8da 2019-10-18 omar.polo fprintf(stderr, "%s out of range! Using %d as default.\n",
1006 c758d8da 2019-10-18 omar.polo str, default_value);
1007 1d13acf5 2018-10-04 omar.polo return default_value;
1010 1d13acf5 2018-10-04 omar.polo return lval;
1013 c758d8da 2019-10-18 omar.polo /* Like parse_integer but recognize the percentages (i.e. strings ending with
1014 c758d8da 2019-10-18 omar.polo * `%') */
1016 1d13acf5 2018-10-04 omar.polo parse_int_with_percentage(const char *str, int default_value, int max)
1018 1d13acf5 2018-10-04 omar.polo int len = strlen(str);
1020 c758d8da 2019-10-18 omar.polo if (len > 0 && str[len - 1] == '%') {
1021 1d13acf5 2018-10-04 omar.polo int val;
1022 1d13acf5 2018-10-04 omar.polo char *cpy;
1024 92803b00 2019-10-19 omar.polo if ((cpy = strdup(str)) == NULL)
1025 92803b00 2019-10-19 omar.polo err(1, "strdup");
1027 c758d8da 2019-10-18 omar.polo cpy[len - 1] = '\0';
1028 1d13acf5 2018-10-04 omar.polo val = parse_integer(cpy, default_value);
1029 1d13acf5 2018-10-04 omar.polo free(cpy);
1030 1d13acf5 2018-10-04 omar.polo return val * max / 100;
1033 1d13acf5 2018-10-04 omar.polo return parse_integer(str, default_value);
1037 a8ca9c6d 2019-10-20 omar.polo get_mouse_coords(Display *d, int *x, int *y)
1039 a8ca9c6d 2019-10-20 omar.polo Window w, root;
1040 a8ca9c6d 2019-10-20 omar.polo int i;
1041 a8ca9c6d 2019-10-20 omar.polo unsigned int u;
1043 a8ca9c6d 2019-10-20 omar.polo *x = *y = 0;
1044 a8ca9c6d 2019-10-20 omar.polo root = DefaultRootWindow(d);
1046 a8ca9c6d 2019-10-20 omar.polo if (!XQueryPointer(d, root, &root, &w, x, y, &i, &i, &u)) {
1047 a8ca9c6d 2019-10-20 omar.polo for (i = 0; i < ScreenCount(d); ++i) {
1048 a8ca9c6d 2019-10-20 omar.polo if (root == RootWindow(d, i))
1049 a8ca9c6d 2019-10-20 omar.polo break;
1055 1d13acf5 2018-10-04 omar.polo * Like parse_int_with_percentage but understands some special values:
1056 1d13acf5 2018-10-04 omar.polo * - middle that is (max-self)/2
1057 0d836add 2019-10-20 omar.polo * - center = middle
1058 1d13acf5 2018-10-04 omar.polo * - start that is 0
1059 1d13acf5 2018-10-04 omar.polo * - end that is (max-self)
1060 a8ca9c6d 2019-10-20 omar.polo * - mx x coordinate of the mouse
1061 a8ca9c6d 2019-10-20 omar.polo * - my y coordinate of the mouse
1064 a8ca9c6d 2019-10-20 omar.polo parse_int_with_pos(
1065 a8ca9c6d 2019-10-20 omar.polo Display *d, const char *str, int default_value, int max, int self)
1067 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "start"))
1068 1d13acf5 2018-10-04 omar.polo return 0;
1069 f4f1270d 2019-10-18 omar.polo if (!strcmp(str, "middle") || !strcmp(str, "center"))
1070 c758d8da 2019-10-18 omar.polo return (max - self) / 2;
1071 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "end"))
1072 c758d8da 2019-10-18 omar.polo return max - self;
1073 a8ca9c6d 2019-10-20 omar.polo if (!strcmp(str, "mx") || !strcmp(str, "my")) {
1074 a8ca9c6d 2019-10-20 omar.polo int x, y;
1076 a8ca9c6d 2019-10-20 omar.polo get_mouse_coords(d, &x, &y);
1077 a8ca9c6d 2019-10-20 omar.polo if (!strcmp(str, "mx"))
1078 a8ca9c6d 2019-10-20 omar.polo return x;
1080 a8ca9c6d 2019-10-20 omar.polo return y;
1082 1d13acf5 2018-10-04 omar.polo return parse_int_with_percentage(str, default_value, max);
1085 1d13acf5 2018-10-04 omar.polo /* Parse a string like a CSS value. */
1086 1d13acf5 2018-10-04 omar.polo /* TODO: harden a bit this function */
1087 1d13acf5 2018-10-04 omar.polo char **
1088 1d13acf5 2018-10-04 omar.polo parse_csslike(const char *str)
1090 c758d8da 2019-10-18 omar.polo int i, j;
1091 c758d8da 2019-10-18 omar.polo char *s, *token, **ret;
1092 c758d8da 2019-10-18 omar.polo short any_null;
1094 1d13acf5 2018-10-04 omar.polo s = strdup(str);
1095 1d13acf5 2018-10-04 omar.polo if (s == NULL)
1096 1d13acf5 2018-10-04 omar.polo return NULL;
1098 c758d8da 2019-10-18 omar.polo ret = malloc(4 * sizeof(char *));
1099 1d13acf5 2018-10-04 omar.polo if (ret == NULL) {
1100 1d13acf5 2018-10-04 omar.polo free(s);
1101 1d13acf5 2018-10-04 omar.polo return NULL;
1104 4f769efa 2018-10-06 omar.polo for (i = 0; (token = strsep(&s, " ")) != NULL && i < 4; ++i)
1105 1d13acf5 2018-10-04 omar.polo ret[i] = strdup(token);
1107 1d13acf5 2018-10-04 omar.polo if (i == 1)
1108 4f769efa 2018-10-06 omar.polo for (j = 1; j < 4; j++)
1109 1d13acf5 2018-10-04 omar.polo ret[j] = strdup(ret[0]);
1111 1d13acf5 2018-10-04 omar.polo if (i == 2) {
1112 1d13acf5 2018-10-04 omar.polo ret[2] = strdup(ret[0]);
1113 1d13acf5 2018-10-04 omar.polo ret[3] = strdup(ret[1]);
1116 1d13acf5 2018-10-04 omar.polo if (i == 3)
1117 1d13acf5 2018-10-04 omar.polo ret[3] = strdup(ret[1]);
1119 c758d8da 2019-10-18 omar.polo /* before we didn't check for the return type of strdup, here we will
1122 26b541d8 2018-10-04 omar.polo any_null = 0;
1123 1d13acf5 2018-10-04 omar.polo for (i = 0; i < 4; ++i)
1124 1d13acf5 2018-10-04 omar.polo any_null = ret[i] == NULL || any_null;
1126 1d13acf5 2018-10-04 omar.polo if (any_null)
1127 1d13acf5 2018-10-04 omar.polo for (i = 0; i < 4; ++i)
1128 1d13acf5 2018-10-04 omar.polo if (ret[i] != NULL)
1129 1d13acf5 2018-10-04 omar.polo free(ret[i]);
1131 1d13acf5 2018-10-04 omar.polo if (i == 0 || any_null) {
1132 1d13acf5 2018-10-04 omar.polo free(s);
1133 1d13acf5 2018-10-04 omar.polo free(ret);
1134 1d13acf5 2018-10-04 omar.polo return NULL;
1137 1d13acf5 2018-10-04 omar.polo return ret;
1141 1d13acf5 2018-10-04 omar.polo * Given an event, try to understand what the users wants. If the
1142 1d13acf5 2018-10-04 omar.polo * return value is ADD_CHAR then `input' is a pointer to a string that
1143 1d13acf5 2018-10-04 omar.polo * will need to be free'ed later.
1145 c758d8da 2019-10-18 omar.polo enum action
1146 c758d8da 2019-10-18 omar.polo parse_event(Display *d, XKeyPressedEvent *ev, XIC xic, char **input)
1148 c758d8da 2019-10-18 omar.polo char str[SYM_BUF_SIZE] = { 0 };
1149 c758d8da 2019-10-18 omar.polo Status s;
1151 1d13acf5 2018-10-04 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_BackSpace))
1152 1d13acf5 2018-10-04 omar.polo return DEL_CHAR;
1154 1d13acf5 2018-10-04 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Tab))
1155 1d13acf5 2018-10-04 omar.polo return ev->state & ShiftMask ? PREV_COMPL : NEXT_COMPL;
1157 1d13acf5 2018-10-04 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Return))
1158 1d13acf5 2018-10-04 omar.polo return CONFIRM;
1160 1d13acf5 2018-10-04 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Escape))
1161 1d13acf5 2018-10-04 omar.polo return EXIT;
1163 1d13acf5 2018-10-04 omar.polo /* Try to read what key was pressed */
1164 1d13acf5 2018-10-04 omar.polo s = 0;
1165 1d13acf5 2018-10-04 omar.polo Xutf8LookupString(xic, ev, str, SYM_BUF_SIZE, 0, &s);
1166 1d13acf5 2018-10-04 omar.polo if (s == XBufferOverflow) {
1167 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1168 c758d8da 2019-10-18 omar.polo "Buffer overflow when trying to create keyboard "
1169 c758d8da 2019-10-18 omar.polo "symbol map.\n");
1170 1d13acf5 2018-10-04 omar.polo return EXIT;
1173 1d13acf5 2018-10-04 omar.polo if (ev->state & ControlMask) {
1174 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "")) /* C-u */
1175 1d13acf5 2018-10-04 omar.polo return DEL_LINE;
1176 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "")) /* C-w */
1177 1d13acf5 2018-10-04 omar.polo return DEL_WORD;
1178 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "")) /* C-h */
1179 1d13acf5 2018-10-04 omar.polo return DEL_CHAR;
1180 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "\r")) /* C-m */
1181 1d13acf5 2018-10-04 omar.polo return CONFIRM_CONTINUE;
1182 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "")) /* C-p */
1183 1d13acf5 2018-10-04 omar.polo return PREV_COMPL;
1184 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "")) /* C-n */
1185 1d13acf5 2018-10-04 omar.polo return NEXT_COMPL;
1186 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "")) /* C-c */
1187 1d13acf5 2018-10-04 omar.polo return EXIT;
1188 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "\t")) /* C-i */
1189 1d13acf5 2018-10-04 omar.polo return TOGGLE_FIRST_SELECTED;
1192 1d13acf5 2018-10-04 omar.polo *input = strdup(str);
1193 1d13acf5 2018-10-04 omar.polo if (*input == NULL) {
1194 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Error while allocating memory for key.\n");
1195 1d13acf5 2018-10-04 omar.polo return EXIT;
1198 1d13acf5 2018-10-04 omar.polo return ADD_CHAR;
1202 c758d8da 2019-10-18 omar.polo confirm(enum state *status, struct rendering *r, struct completions *cs,
1203 c758d8da 2019-10-18 omar.polo char **text, int *textlen)
1205 1d13acf5 2018-10-04 omar.polo if ((cs->selected != -1) || (cs->length > 0 && r->first_selected)) {
1206 1d13acf5 2018-10-04 omar.polo /* if there is something selected expand it and return */
1207 1d13acf5 2018-10-04 omar.polo int index = cs->selected == -1 ? 0 : cs->selected;
1208 1d13acf5 2018-10-04 omar.polo struct completion *c = cs->completions;
1209 1d13acf5 2018-10-04 omar.polo char *t;
1211 26b541d8 2018-10-04 omar.polo while (1) {
1212 1d13acf5 2018-10-04 omar.polo if (index == 0)
1213 1d13acf5 2018-10-04 omar.polo break;
1215 1d13acf5 2018-10-04 omar.polo index--;
1218 1d13acf5 2018-10-04 omar.polo t = c->rcompletion;
1219 1d13acf5 2018-10-04 omar.polo free(*text);
1220 1d13acf5 2018-10-04 omar.polo *text = strdup(t);
1222 1d13acf5 2018-10-04 omar.polo if (*text == NULL) {
1223 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Memory allocation error\n");
1224 1d13acf5 2018-10-04 omar.polo *status = ERR;
1227 1d13acf5 2018-10-04 omar.polo *textlen = strlen(*text);
1228 1d13acf5 2018-10-04 omar.polo return;
1231 1d13acf5 2018-10-04 omar.polo if (!r->free_text) /* cannot accept arbitrary text */
1232 1d13acf5 2018-10-04 omar.polo *status = LOOPING;
1235 a81dbe5d 2019-10-19 omar.polo /* cs: completion list
1236 a81dbe5d 2019-10-19 omar.polo * offset: the offset of the click
1237 a81dbe5d 2019-10-19 omar.polo * first: the first (rendered) item
1238 a81dbe5d 2019-10-19 omar.polo * def: the default action
1240 a81dbe5d 2019-10-19 omar.polo enum action
1241 a81dbe5d 2019-10-19 omar.polo select_clicked(
1242 a81dbe5d 2019-10-19 omar.polo struct completions *cs, size_t offset, size_t first, enum action def)
1244 a81dbe5d 2019-10-19 omar.polo ssize_t selected = first;
1245 a81dbe5d 2019-10-19 omar.polo int set = 0;
1247 a81dbe5d 2019-10-19 omar.polo if (cs->length == 0)
1248 a81dbe5d 2019-10-19 omar.polo return NO_OP;
1250 a81dbe5d 2019-10-19 omar.polo if (offset < cs->completions[selected].offset)
1251 a81dbe5d 2019-10-19 omar.polo return NO_OP;
1253 a81dbe5d 2019-10-19 omar.polo /* skip the first entry */
1254 a81dbe5d 2019-10-19 omar.polo for (selected += 1; selected < cs->length; ++selected) {
1255 eda65435 2019-10-19 omar.polo if (cs->completions[selected].offset == -1)
1256 a81dbe5d 2019-10-19 omar.polo break;
1258 a81dbe5d 2019-10-19 omar.polo if (offset < cs->completions[selected].offset) {
1259 a81dbe5d 2019-10-19 omar.polo cs->selected = selected - 1;
1260 a81dbe5d 2019-10-19 omar.polo set = 1;
1261 a81dbe5d 2019-10-19 omar.polo break;
1265 a81dbe5d 2019-10-19 omar.polo if (!set)
1266 a81dbe5d 2019-10-19 omar.polo cs->selected = selected - 1;
1268 a81dbe5d 2019-10-19 omar.polo return def;
1271 a81dbe5d 2019-10-19 omar.polo enum action
1272 a81dbe5d 2019-10-19 omar.polo handle_mouse(
1273 a81dbe5d 2019-10-19 omar.polo struct rendering *r, struct completions *cs, XButtonPressedEvent *e)
1275 a81dbe5d 2019-10-19 omar.polo size_t off;
1277 a81dbe5d 2019-10-19 omar.polo if (r->horizontal_layout)
1278 a81dbe5d 2019-10-19 omar.polo off = e->x;
1280 a81dbe5d 2019-10-19 omar.polo off = e->y;
1282 a81dbe5d 2019-10-19 omar.polo switch (e->button) {
1283 a81dbe5d 2019-10-19 omar.polo case Button1:
1284 a81dbe5d 2019-10-19 omar.polo return select_clicked(cs, off, r->offset, CONFIRM);
1286 a81dbe5d 2019-10-19 omar.polo case Button3:
1287 a81dbe5d 2019-10-19 omar.polo return select_clicked(cs, off, r->offset, CONFIRM_CONTINUE);
1289 a81dbe5d 2019-10-19 omar.polo case Button4:
1290 a81dbe5d 2019-10-19 omar.polo return SCROLL_UP;
1292 a81dbe5d 2019-10-19 omar.polo case Button5:
1293 a81dbe5d 2019-10-19 omar.polo return SCROLL_DOWN;
1296 a81dbe5d 2019-10-19 omar.polo return NO_OP;
1299 1d13acf5 2018-10-04 omar.polo /* event loop */
1300 1d13acf5 2018-10-04 omar.polo enum state
1301 c758d8da 2019-10-18 omar.polo loop(struct rendering *r, char **text, int *textlen, struct completions *cs,
1302 c758d8da 2019-10-18 omar.polo char **lines, char **vlines)
1304 1d13acf5 2018-10-04 omar.polo enum state status = LOOPING;
1306 1d13acf5 2018-10-04 omar.polo while (status == LOOPING) {
1307 1d13acf5 2018-10-04 omar.polo XEvent e;
1308 1d13acf5 2018-10-04 omar.polo XNextEvent(r->d, &e);
1310 1d13acf5 2018-10-04 omar.polo if (XFilterEvent(&e, r->w))
1311 1d13acf5 2018-10-04 omar.polo continue;
1313 1d13acf5 2018-10-04 omar.polo switch (e.type) {
1314 1d13acf5 2018-10-04 omar.polo case KeymapNotify:
1315 1d13acf5 2018-10-04 omar.polo XRefreshKeyboardMapping(&e.xmapping);
1316 1d13acf5 2018-10-04 omar.polo break;
1318 1d13acf5 2018-10-04 omar.polo case FocusIn:
1319 1d13acf5 2018-10-04 omar.polo /* Re-grab focus */
1320 1d13acf5 2018-10-04 omar.polo if (e.xfocus.window != r->w)
1321 1d13acf5 2018-10-04 omar.polo grabfocus(r->d, r->w);
1322 1d13acf5 2018-10-04 omar.polo break;
1324 1d13acf5 2018-10-04 omar.polo case VisibilityNotify:
1325 1d13acf5 2018-10-04 omar.polo if (e.xvisibility.state != VisibilityUnobscured)
1326 1d13acf5 2018-10-04 omar.polo XRaiseWindow(r->d, r->w);
1327 1d13acf5 2018-10-04 omar.polo break;
1329 1d13acf5 2018-10-04 omar.polo case MapNotify:
1330 1d13acf5 2018-10-04 omar.polo get_wh(r->d, &r->w, &r->width, &r->height);
1331 1d13acf5 2018-10-04 omar.polo draw(r, *text, cs);
1332 1d13acf5 2018-10-04 omar.polo break;
1334 a81dbe5d 2019-10-19 omar.polo case KeyPress:
1335 a81dbe5d 2019-10-19 omar.polo case ButtonPress: {
1336 a81dbe5d 2019-10-19 omar.polo enum action a;
1337 a81dbe5d 2019-10-19 omar.polo char *input = NULL;
1339 a81dbe5d 2019-10-19 omar.polo if (e.type == KeyPress)
1340 a81dbe5d 2019-10-19 omar.polo a = parse_event(r->d, (XKeyPressedEvent *)&e,
1341 a81dbe5d 2019-10-19 omar.polo r->xic, &input);
1343 a81dbe5d 2019-10-19 omar.polo a = handle_mouse(
1344 a81dbe5d 2019-10-19 omar.polo r, cs, (XButtonPressedEvent *)&e);
1346 a81dbe5d 2019-10-19 omar.polo switch (a) {
1347 a81dbe5d 2019-10-19 omar.polo case NO_OP:
1348 a81dbe5d 2019-10-19 omar.polo break;
1350 1d13acf5 2018-10-04 omar.polo case EXIT:
1351 1d13acf5 2018-10-04 omar.polo status = ERR;
1352 1d13acf5 2018-10-04 omar.polo break;
1354 1d13acf5 2018-10-04 omar.polo case CONFIRM: {
1355 1d13acf5 2018-10-04 omar.polo status = OK;
1356 1d13acf5 2018-10-04 omar.polo confirm(&status, r, cs, text, textlen);
1357 1d13acf5 2018-10-04 omar.polo break;
1360 1d13acf5 2018-10-04 omar.polo case CONFIRM_CONTINUE: {
1361 1d13acf5 2018-10-04 omar.polo status = OK_LOOP;
1362 1d13acf5 2018-10-04 omar.polo confirm(&status, r, cs, text, textlen);
1363 1d13acf5 2018-10-04 omar.polo break;
1366 1d13acf5 2018-10-04 omar.polo case PREV_COMPL: {
1367 c758d8da 2019-10-18 omar.polo complete(cs, r->first_selected, 1, text,
1368 c758d8da 2019-10-18 omar.polo textlen, &status);
1369 1d13acf5 2018-10-04 omar.polo r->offset = cs->selected;
1370 1d13acf5 2018-10-04 omar.polo break;
1373 1d13acf5 2018-10-04 omar.polo case NEXT_COMPL: {
1374 c758d8da 2019-10-18 omar.polo complete(cs, r->first_selected, 0, text,
1375 c758d8da 2019-10-18 omar.polo textlen, &status);
1376 1d13acf5 2018-10-04 omar.polo r->offset = cs->selected;
1377 1d13acf5 2018-10-04 omar.polo break;
1380 1d13acf5 2018-10-04 omar.polo case DEL_CHAR:
1381 1d13acf5 2018-10-04 omar.polo popc(*text);
1382 c758d8da 2019-10-18 omar.polo update_completions(cs, *text, lines, vlines,
1383 c758d8da 2019-10-18 omar.polo r->first_selected);
1384 1d13acf5 2018-10-04 omar.polo r->offset = 0;
1385 1d13acf5 2018-10-04 omar.polo break;
1387 1d13acf5 2018-10-04 omar.polo case DEL_WORD: {
1388 1d13acf5 2018-10-04 omar.polo popw(*text);
1389 c758d8da 2019-10-18 omar.polo update_completions(cs, *text, lines, vlines,
1390 c758d8da 2019-10-18 omar.polo r->first_selected);
1391 1d13acf5 2018-10-04 omar.polo break;
1394 1d13acf5 2018-10-04 omar.polo case DEL_LINE: {
1395 1d13acf5 2018-10-04 omar.polo int i;
1396 1d13acf5 2018-10-04 omar.polo for (i = 0; i < *textlen; ++i)
1397 1d13acf5 2018-10-04 omar.polo *(*text + i) = 0;
1398 c758d8da 2019-10-18 omar.polo update_completions(cs, *text, lines, vlines,
1399 c758d8da 2019-10-18 omar.polo r->first_selected);
1400 1d13acf5 2018-10-04 omar.polo r->offset = 0;
1401 1d13acf5 2018-10-04 omar.polo break;
1404 1d13acf5 2018-10-04 omar.polo case ADD_CHAR: {
1405 1d13acf5 2018-10-04 omar.polo int str_len, i;
1407 1d13acf5 2018-10-04 omar.polo str_len = strlen(input);
1410 1d13acf5 2018-10-04 omar.polo * sometimes a strange key is pressed
1411 1d13acf5 2018-10-04 omar.polo * i.e. ctrl alone), so input will be
1412 1d13acf5 2018-10-04 omar.polo * empty. Don't need to update
1413 1d13acf5 2018-10-04 omar.polo * completion in that case
1415 1d13acf5 2018-10-04 omar.polo if (str_len == 0)
1416 1d13acf5 2018-10-04 omar.polo break;
1418 1d13acf5 2018-10-04 omar.polo for (i = 0; i < str_len; ++i) {
1419 c758d8da 2019-10-18 omar.polo *textlen = pushc(
1420 c758d8da 2019-10-18 omar.polo text, *textlen, input[i]);
1421 1d13acf5 2018-10-04 omar.polo if (*textlen == -1) {
1422 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1423 c758d8da 2019-10-18 omar.polo "Memory allocation "
1424 c758d8da 2019-10-18 omar.polo "error\n");
1425 1d13acf5 2018-10-04 omar.polo status = ERR;
1426 1d13acf5 2018-10-04 omar.polo break;
1430 1d13acf5 2018-10-04 omar.polo if (status != ERR) {
1431 c758d8da 2019-10-18 omar.polo update_completions(cs, *text, lines,
1432 c758d8da 2019-10-18 omar.polo vlines, r->first_selected);
1433 1d13acf5 2018-10-04 omar.polo free(input);
1436 1d13acf5 2018-10-04 omar.polo r->offset = 0;
1437 1d13acf5 2018-10-04 omar.polo break;
1440 1d13acf5 2018-10-04 omar.polo case TOGGLE_FIRST_SELECTED:
1441 1d13acf5 2018-10-04 omar.polo r->first_selected = !r->first_selected;
1442 1d13acf5 2018-10-04 omar.polo if (r->first_selected && cs->selected < 0)
1443 1d13acf5 2018-10-04 omar.polo cs->selected = 0;
1444 1d13acf5 2018-10-04 omar.polo if (!r->first_selected && cs->selected == 0)
1445 1d13acf5 2018-10-04 omar.polo cs->selected = -1;
1446 1d13acf5 2018-10-04 omar.polo break;
1448 a81dbe5d 2019-10-19 omar.polo case SCROLL_DOWN:
1449 c758d8da 2019-10-18 omar.polo r->offset
1450 c758d8da 2019-10-18 omar.polo = MIN(r->offset + 1, cs->length - 1);
1451 a81dbe5d 2019-10-19 omar.polo break;
1453 a81dbe5d 2019-10-19 omar.polo case SCROLL_UP:
1454 a81dbe5d 2019-10-19 omar.polo r->offset = MAX((ssize_t)r->offset - 1, 0);
1455 a81dbe5d 2019-10-19 omar.polo break;
1460 1d13acf5 2018-10-04 omar.polo draw(r, *text, cs);
1463 1d13acf5 2018-10-04 omar.polo return status;
1467 1d13acf5 2018-10-04 omar.polo load_font(struct rendering *r, const char *fontname)
1469 1d13acf5 2018-10-04 omar.polo #ifdef USE_XFT
1470 1d13acf5 2018-10-04 omar.polo r->font = XftFontOpenName(r->d, DefaultScreen(r->d), fontname);
1471 1d13acf5 2018-10-04 omar.polo return 0;
1472 1d13acf5 2018-10-04 omar.polo #else
1473 1d13acf5 2018-10-04 omar.polo char **missing_charset_list;
1474 1d13acf5 2018-10-04 omar.polo int missing_charset_count;
1476 c758d8da 2019-10-18 omar.polo r->font = XCreateFontSet(r->d, fontname, &missing_charset_list,
1477 c758d8da 2019-10-18 omar.polo &missing_charset_count, NULL);
1478 1d13acf5 2018-10-04 omar.polo if (r->font != NULL)
1479 1d13acf5 2018-10-04 omar.polo return 0;
1481 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Unable to load the font(s) %s\n", fontname);
1483 1d13acf5 2018-10-04 omar.polo if (!strcmp(fontname, default_fontname))
1484 1d13acf5 2018-10-04 omar.polo return -1;
1486 1d13acf5 2018-10-04 omar.polo return load_font(r, default_fontname);
1487 1d13acf5 2018-10-04 omar.polo #endif
1491 4f769efa 2018-10-06 omar.polo xim_init(struct rendering *r, XrmDatabase *xdb)
1493 c758d8da 2019-10-18 omar.polo XIMStyle best_match_style;
1494 c758d8da 2019-10-18 omar.polo XIMStyles *xis;
1495 c758d8da 2019-10-18 omar.polo int i;
1497 4f769efa 2018-10-06 omar.polo /* Open the X input method */
1498 92803b00 2019-10-19 omar.polo if ((r->xim = XOpenIM(r->d, *xdb, resname, resclass)) == NULL)
1499 92803b00 2019-10-19 omar.polo err(1, "XOpenIM");
1501 43e30577 2018-10-20 omar.polo if (XGetIMValues(r->xim, XNQueryInputStyle, &xis, NULL) || !xis) {
1502 4f769efa 2018-10-06 omar.polo fprintf(stderr, "Input Styles could not be retrieved\n");
1503 4f769efa 2018-10-06 omar.polo exit(EX_UNAVAILABLE);
1506 4f769efa 2018-10-06 omar.polo best_match_style = 0;
1507 4f769efa 2018-10-06 omar.polo for (i = 0; i < xis->count_styles; ++i) {
1508 4f769efa 2018-10-06 omar.polo XIMStyle ts = xis->supported_styles[i];
1509 4f769efa 2018-10-06 omar.polo if (ts == (XIMPreeditNothing | XIMStatusNothing)) {
1510 4f769efa 2018-10-06 omar.polo best_match_style = ts;
1511 4f769efa 2018-10-06 omar.polo break;
1514 4f769efa 2018-10-06 omar.polo XFree(xis);
1516 4f769efa 2018-10-06 omar.polo if (!best_match_style)
1517 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1518 c758d8da 2019-10-18 omar.polo "No matching input style could be determined\n");
1520 c758d8da 2019-10-18 omar.polo r->xic = XCreateIC(r->xim, XNInputStyle, best_match_style,
1521 c758d8da 2019-10-18 omar.polo XNClientWindow, r->w, XNFocusWindow, r->w, NULL);
1522 92803b00 2019-10-19 omar.polo if (r->xic == NULL)
1523 92803b00 2019-10-19 omar.polo err(1, "XCreateIC");
1527 c758d8da 2019-10-18 omar.polo create_window(struct rendering *r, Window parent_window, Colormap cmap,
1528 c758d8da 2019-10-18 omar.polo XVisualInfo vinfo, int x, int y, int ox, int oy,
1529 c758d8da 2019-10-18 omar.polo unsigned long background_pixel)
1531 c758d8da 2019-10-18 omar.polo XSetWindowAttributes attr;
1533 4f769efa 2018-10-06 omar.polo /* Create the window */
1534 4f769efa 2018-10-06 omar.polo attr.colormap = cmap;
1535 4f769efa 2018-10-06 omar.polo attr.override_redirect = 1;
1536 4f769efa 2018-10-06 omar.polo attr.border_pixel = 0;
1537 1f6422bc 2018-10-20 omar.polo attr.background_pixel = background_pixel;
1538 c758d8da 2019-10-18 omar.polo attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask
1539 c758d8da 2019-10-18 omar.polo | KeymapStateMask | ButtonPress | VisibilityChangeMask;
1541 c758d8da 2019-10-18 omar.polo r->w = XCreateWindow(r->d, parent_window, x + ox, y + oy, r->width,
1542 c758d8da 2019-10-18 omar.polo r->height, 0, vinfo.depth, InputOutput, vinfo.visual,
1543 c758d8da 2019-10-18 omar.polo CWBorderPixel | CWBackPixel | CWColormap | CWEventMask
1544 c758d8da 2019-10-18 omar.polo | CWOverrideRedirect,
1545 4f769efa 2018-10-06 omar.polo &attr);
1549 aed48307 2018-10-06 omar.polo ps1extents(struct rendering *r)
1551 aed48307 2018-10-06 omar.polo char *dup;
1552 aed48307 2018-10-06 omar.polo dup = strdupn(r->ps1);
1553 c758d8da 2019-10-18 omar.polo text_extents(
1554 c758d8da 2019-10-18 omar.polo dup == NULL ? r->ps1 : dup, r->ps1len, r, &r->ps1w, &r->ps1h);
1555 aed48307 2018-10-06 omar.polo free(dup);
1559 1d13acf5 2018-10-04 omar.polo usage(char *prgname)
1561 df29e05e 2018-10-17 omar.polo fprintf(stderr,
1562 df29e05e 2018-10-17 omar.polo "%s [-Aahmv] [-B colors] [-b size] [-C color] [-c color]\n"
1563 c758d8da 2019-10-18 omar.polo " [-d separator] [-e window] [-f font] [-G color] [-g "
1564 c758d8da 2019-10-18 omar.polo "size]\n"
1565 c758d8da 2019-10-18 omar.polo " [-H height] [-I color] [-i size] [-J color] [-j "
1566 c758d8da 2019-10-18 omar.polo "size] [-l layout]\n"
1567 c758d8da 2019-10-18 omar.polo " [-P padding] [-p prompt] [-S color] [-s color] [-T "
1568 c758d8da 2019-10-18 omar.polo "color]\n"
1569 c758d8da 2019-10-18 omar.polo " [-t color] [-W width] [-x coord] [-y coord]\n",
1570 c758d8da 2019-10-18 omar.polo prgname);
1574 1d13acf5 2018-10-04 omar.polo main(int argc, char **argv)
1576 c758d8da 2019-10-18 omar.polo struct completions *cs;
1577 c758d8da 2019-10-18 omar.polo struct rendering r;
1578 c758d8da 2019-10-18 omar.polo XVisualInfo vinfo;
1579 c758d8da 2019-10-18 omar.polo Colormap cmap;
1580 c758d8da 2019-10-18 omar.polo size_t nlines, i;
1581 c758d8da 2019-10-18 omar.polo Window parent_window;
1582 c758d8da 2019-10-18 omar.polo XrmDatabase xdb;
1583 c758d8da 2019-10-18 omar.polo unsigned long fgs[3], bgs[3]; /* prompt, compl, compl_highlighted */
1584 c758d8da 2019-10-18 omar.polo unsigned long borders_bg[4], p_borders_bg[4], c_borders_bg[4],
1585 c758d8da 2019-10-18 omar.polo ch_borders_bg[4]; /* N E S W */
1586 c758d8da 2019-10-18 omar.polo enum state status;
1587 c758d8da 2019-10-18 omar.polo int ch;
1588 c758d8da 2019-10-18 omar.polo int offset_x, offset_y, x, y;
1589 c758d8da 2019-10-18 omar.polo int textlen, d_width, d_height;
1590 c758d8da 2019-10-18 omar.polo short embed;
1591 c758d8da 2019-10-18 omar.polo char *sep, *parent_window_id;
1592 c758d8da 2019-10-18 omar.polo char **lines, *buf, **vlines;
1593 c758d8da 2019-10-18 omar.polo char *fontname, *text, *xrm;
1595 1d13acf5 2018-10-04 omar.polo #ifdef __OpenBSD__
1596 1d13acf5 2018-10-04 omar.polo /* stdio & rpath: to read/write stdio/stdout/stderr */
1597 1d13acf5 2018-10-04 omar.polo /* unix: to connect to XOrg */
1598 1d13acf5 2018-10-04 omar.polo pledge("stdio rpath unix", "");
1599 1d13acf5 2018-10-04 omar.polo #endif
1601 1d13acf5 2018-10-04 omar.polo sep = NULL;
1602 1d13acf5 2018-10-04 omar.polo parent_window_id = NULL;
1604 4f769efa 2018-10-06 omar.polo r.first_selected = 0;
1605 4f769efa 2018-10-06 omar.polo r.free_text = 1;
1606 4f769efa 2018-10-06 omar.polo r.multiple_select = 0;
1607 4f769efa 2018-10-06 omar.polo r.offset = 0;
1609 1d13acf5 2018-10-04 omar.polo while ((ch = getopt(argc, argv, ARGS)) != -1) {
1610 1d13acf5 2018-10-04 omar.polo switch (ch) {
1611 c758d8da 2019-10-18 omar.polo case 'h': /* help */
1612 1d13acf5 2018-10-04 omar.polo usage(*argv);
1613 1d13acf5 2018-10-04 omar.polo return 0;
1614 c758d8da 2019-10-18 omar.polo case 'v': /* version */
1615 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "%s version: %s\n", *argv, VERSION);
1616 1d13acf5 2018-10-04 omar.polo return 0;
1617 c758d8da 2019-10-18 omar.polo case 'e': /* embed */
1618 92803b00 2019-10-19 omar.polo if ((parent_window_id = strdup(optarg)) == NULL)
1619 92803b00 2019-10-19 omar.polo err(1, "strdup");
1620 1d13acf5 2018-10-04 omar.polo break;
1621 4c0d79a0 2018-10-09 omar.polo case 'd':
1622 92803b00 2019-10-19 omar.polo if ((sep = strdup(optarg)) == NULL)
1623 92803b00 2019-10-19 omar.polo err(1, "strdup");
1624 92803b00 2019-10-19 omar.polo break;
1625 4c0d79a0 2018-10-09 omar.polo case 'A':
1626 4f769efa 2018-10-06 omar.polo r.free_text = 0;
1627 1d13acf5 2018-10-04 omar.polo break;
1628 4c0d79a0 2018-10-09 omar.polo case 'm':
1629 4f769efa 2018-10-06 omar.polo r.multiple_select = 1;
1630 1d13acf5 2018-10-04 omar.polo break;
1631 1d13acf5 2018-10-04 omar.polo default:
1632 1d13acf5 2018-10-04 omar.polo break;
1636 1d13acf5 2018-10-04 omar.polo /* Read the completions */
1637 1d13acf5 2018-10-04 omar.polo lines = NULL;
1638 1d13acf5 2018-10-04 omar.polo buf = NULL;
1639 1d13acf5 2018-10-04 omar.polo nlines = readlines(&lines, &buf);
1641 1d13acf5 2018-10-04 omar.polo vlines = NULL;
1642 1d13acf5 2018-10-04 omar.polo if (sep != NULL) {
1643 1d13acf5 2018-10-04 omar.polo int l;
1644 1d13acf5 2018-10-04 omar.polo l = strlen(sep);
1645 92803b00 2019-10-19 omar.polo if ((vlines = calloc(nlines, sizeof(char *))) == NULL)
1646 92803b00 2019-10-19 omar.polo err(1, "calloc");
1648 1d13acf5 2018-10-04 omar.polo for (i = 0; i < nlines; i++) {
1649 1d13acf5 2018-10-04 omar.polo char *t;
1650 1d13acf5 2018-10-04 omar.polo t = strstr(lines[i], sep);
1651 1d13acf5 2018-10-04 omar.polo if (t == NULL)
1652 1d13acf5 2018-10-04 omar.polo vlines[i] = lines[i];
1654 1d13acf5 2018-10-04 omar.polo vlines[i] = t + l;
1658 1d13acf5 2018-10-04 omar.polo setlocale(LC_ALL, getenv("LANG"));
1660 1d13acf5 2018-10-04 omar.polo status = LOOPING;
1662 1d13acf5 2018-10-04 omar.polo /* where the monitor start (used only with xinerama) */
1663 1d13acf5 2018-10-04 omar.polo offset_x = offset_y = 0;
1665 1d13acf5 2018-10-04 omar.polo /* default width and height */
1666 4f769efa 2018-10-06 omar.polo r.width = 400;
1667 4f769efa 2018-10-06 omar.polo r.height = 20;
1669 1d13acf5 2018-10-04 omar.polo /* default position on the screen */
1670 1d13acf5 2018-10-04 omar.polo x = y = 0;
1672 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i) {
1673 df29e05e 2018-10-17 omar.polo /* default paddings */
1674 df29e05e 2018-10-17 omar.polo r.p_padding[i] = 10;
1675 df29e05e 2018-10-17 omar.polo r.c_padding[i] = 10;
1676 df29e05e 2018-10-17 omar.polo r.ch_padding[i] = 10;
1678 df29e05e 2018-10-17 omar.polo /* default borders */
1679 df29e05e 2018-10-17 omar.polo r.borders[i] = 0;
1680 df29e05e 2018-10-17 omar.polo r.p_borders[i] = 0;
1681 df29e05e 2018-10-17 omar.polo r.c_borders[i] = 0;
1682 df29e05e 2018-10-17 omar.polo r.ch_borders[i] = 0;
1685 1d13acf5 2018-10-04 omar.polo /* the prompt. We duplicate the string so later is easy to
1686 1d13acf5 2018-10-04 omar.polo * free (in the case it's been overwritten by the user) */
1687 92803b00 2019-10-19 omar.polo if ((r.ps1 = strdup("$ ")) == NULL)
1688 92803b00 2019-10-19 omar.polo err(1, "strdup");
1690 1d13acf5 2018-10-04 omar.polo /* same for the font name */
1691 92803b00 2019-10-19 omar.polo if ((fontname = strdup(default_fontname)) == NULL)
1692 92803b00 2019-10-19 omar.polo err(1, "strdup");
1694 1d13acf5 2018-10-04 omar.polo textlen = 10;
1695 92803b00 2019-10-19 omar.polo if ((text = malloc(textlen * sizeof(char))) == NULL)
1696 92803b00 2019-10-19 omar.polo err(1, "malloc");
1698 1d13acf5 2018-10-04 omar.polo /* struct completions *cs = filter(text, lines); */
1699 92803b00 2019-10-19 omar.polo if ((cs = compls_new(nlines)) == NULL)
1700 92803b00 2019-10-19 omar.polo err(1, "compls_new");
1702 1d13acf5 2018-10-04 omar.polo /* start talking to xorg */
1703 4f769efa 2018-10-06 omar.polo r.d = XOpenDisplay(NULL);
1704 4f769efa 2018-10-06 omar.polo if (r.d == NULL) {
1705 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Could not open display!\n");
1706 1d13acf5 2018-10-04 omar.polo return EX_UNAVAILABLE;
1709 26b541d8 2018-10-04 omar.polo embed = 1;
1710 c758d8da 2019-10-18 omar.polo if (!(parent_window_id
1711 c758d8da 2019-10-18 omar.polo && (parent_window = strtol(parent_window_id, NULL, 0)))) {
1712 4f769efa 2018-10-06 omar.polo parent_window = DefaultRootWindow(r.d);
1713 26b541d8 2018-10-04 omar.polo embed = 0;
1716 1d13acf5 2018-10-04 omar.polo /* get display size */
1717 4f769efa 2018-10-06 omar.polo get_wh(r.d, &parent_window, &d_width, &d_height);
1719 1d13acf5 2018-10-04 omar.polo #ifdef USE_XINERAMA
1720 4f769efa 2018-10-06 omar.polo if (!embed && XineramaIsActive(r.d)) { /* find the mice */
1721 1d13acf5 2018-10-04 omar.polo XineramaScreenInfo *info;
1722 c758d8da 2019-10-18 omar.polo Window rr;
1723 c758d8da 2019-10-18 omar.polo Window root;
1724 c758d8da 2019-10-18 omar.polo int number_of_screens, monitors, i;
1725 c758d8da 2019-10-18 omar.polo int root_x, root_y, win_x, win_y;
1726 c758d8da 2019-10-18 omar.polo unsigned int mask;
1727 c758d8da 2019-10-18 omar.polo short res;
1729 4f769efa 2018-10-06 omar.polo number_of_screens = XScreenCount(r.d);
1730 1d13acf5 2018-10-04 omar.polo for (i = 0; i < number_of_screens; ++i) {
1731 4f769efa 2018-10-06 omar.polo root = XRootWindow(r.d, i);
1732 c758d8da 2019-10-18 omar.polo res = XQueryPointer(r.d, root, &rr, &rr, &root_x,
1733 c758d8da 2019-10-18 omar.polo &root_y, &win_x, &win_y, &mask);
1734 4f769efa 2018-10-06 omar.polo if (res)
1735 4f769efa 2018-10-06 omar.polo break;
1738 1d13acf5 2018-10-04 omar.polo if (!res) {
1739 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "No mouse found.\n");
1740 1d13acf5 2018-10-04 omar.polo root_x = 0;
1741 1d13acf5 2018-10-04 omar.polo root_y = 0;
1744 26b541d8 2018-10-04 omar.polo /* Now find in which monitor the mice is */
1745 4f769efa 2018-10-06 omar.polo info = XineramaQueryScreens(r.d, &monitors);
1746 1d13acf5 2018-10-04 omar.polo if (info) {
1747 1d13acf5 2018-10-04 omar.polo for (i = 0; i < monitors; ++i) {
1748 c758d8da 2019-10-18 omar.polo if (info[i].x_org <= root_x
1749 c758d8da 2019-10-18 omar.polo && root_x <= (info[i].x_org
1750 c758d8da 2019-10-18 omar.polo + info[i].width)
1751 c758d8da 2019-10-18 omar.polo && info[i].y_org <= root_y
1752 c758d8da 2019-10-18 omar.polo && root_y <= (info[i].y_org
1753 c758d8da 2019-10-18 omar.polo + info[i].height)) {
1754 1d13acf5 2018-10-04 omar.polo offset_x = info[i].x_org;
1755 1d13acf5 2018-10-04 omar.polo offset_y = info[i].y_org;
1756 1d13acf5 2018-10-04 omar.polo d_width = info[i].width;
1757 1d13acf5 2018-10-04 omar.polo d_height = info[i].height;
1758 1d13acf5 2018-10-04 omar.polo break;
1762 1d13acf5 2018-10-04 omar.polo XFree(info);
1764 42c3f269 2018-07-08 omar.polo #endif
1766 4f769efa 2018-10-06 omar.polo XMatchVisualInfo(r.d, DefaultScreen(r.d), 32, TrueColor, &vinfo);
1767 c758d8da 2019-10-18 omar.polo cmap = XCreateColormap(
1768 c758d8da 2019-10-18 omar.polo r.d, XDefaultRootWindow(r.d), vinfo.visual, AllocNone);
1770 4c0d79a0 2018-10-09 omar.polo fgs[0] = fgs[1] = parse_color("#fff", NULL);
1771 4c0d79a0 2018-10-09 omar.polo fgs[2] = parse_color("#000", NULL);
1773 4c0d79a0 2018-10-09 omar.polo bgs[0] = bgs[1] = parse_color("#000", NULL);
1774 4c0d79a0 2018-10-09 omar.polo bgs[2] = parse_color("#fff", NULL);
1776 c758d8da 2019-10-18 omar.polo borders_bg[0] = borders_bg[1] = borders_bg[2] = borders_bg[3]
1777 c758d8da 2019-10-18 omar.polo = parse_color("#000", NULL);
1779 c758d8da 2019-10-18 omar.polo p_borders_bg[0] = p_borders_bg[1] = p_borders_bg[2] = p_borders_bg[3]
1780 c758d8da 2019-10-18 omar.polo = parse_color("#000", NULL);
1781 c758d8da 2019-10-18 omar.polo c_borders_bg[0] = c_borders_bg[1] = c_borders_bg[2] = c_borders_bg[3]
1782 c758d8da 2019-10-18 omar.polo = parse_color("#000", NULL);
1783 c758d8da 2019-10-18 omar.polo ch_borders_bg[0] = ch_borders_bg[1] = ch_borders_bg[2]
1784 c758d8da 2019-10-18 omar.polo = ch_borders_bg[3] = parse_color("#000", NULL);
1786 4f769efa 2018-10-06 omar.polo r.horizontal_layout = 1;
1788 1d13acf5 2018-10-04 omar.polo /* Read the resources */
1789 1d13acf5 2018-10-04 omar.polo XrmInitialize();
1790 4f769efa 2018-10-06 omar.polo xrm = XResourceManagerString(r.d);
1791 1d13acf5 2018-10-04 omar.polo xdb = NULL;
1792 1d13acf5 2018-10-04 omar.polo if (xrm != NULL) {
1793 1d13acf5 2018-10-04 omar.polo XrmValue value;
1794 1d13acf5 2018-10-04 omar.polo char *datatype[20];
1796 1d13acf5 2018-10-04 omar.polo xdb = XrmGetStringDatabase(xrm);
1798 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.font", "*", datatype, &value)
1799 c758d8da 2019-10-18 omar.polo == 1) {
1800 1d13acf5 2018-10-04 omar.polo free(fontname);
1801 92803b00 2019-10-19 omar.polo if ((fontname = strdup(value.addr)) == NULL)
1802 92803b00 2019-10-19 omar.polo err(1, "strdup");
1803 1d13acf5 2018-10-04 omar.polo } else {
1804 c758d8da 2019-10-18 omar.polo fprintf(stderr, "no font defined, using %s\n",
1805 c758d8da 2019-10-18 omar.polo fontname);
1808 c758d8da 2019-10-18 omar.polo if (XrmGetResource(
1809 c758d8da 2019-10-18 omar.polo xdb, "MyMenu.layout", "*", datatype, &value)
1810 c758d8da 2019-10-18 omar.polo == 1)
1811 c758d8da 2019-10-18 omar.polo r.horizontal_layout
1812 c758d8da 2019-10-18 omar.polo = !strcmp(value.addr, "horizontal");
1814 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1815 c758d8da 2019-10-18 omar.polo "no layout defined, using horizontal\n");
1817 c758d8da 2019-10-18 omar.polo if (XrmGetResource(
1818 c758d8da 2019-10-18 omar.polo xdb, "MyMenu.prompt", "*", datatype, &value)
1819 c758d8da 2019-10-18 omar.polo == 1) {
1820 4f769efa 2018-10-06 omar.polo free(r.ps1);
1821 4f769efa 2018-10-06 omar.polo r.ps1 = normalize_str(value.addr);
1822 1d13acf5 2018-10-04 omar.polo } else {
1823 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1824 c758d8da 2019-10-18 omar.polo "no prompt defined, using \"%s\" as "
1825 c758d8da 2019-10-18 omar.polo "default\n",
1826 c758d8da 2019-10-18 omar.polo r.ps1);
1829 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.border.size", "*",
1830 c758d8da 2019-10-18 omar.polo datatype, &value)
1831 c758d8da 2019-10-18 omar.polo == 1) {
1832 df29e05e 2018-10-17 omar.polo char **sizes;
1833 df29e05e 2018-10-17 omar.polo sizes = parse_csslike(value.addr);
1834 df29e05e 2018-10-17 omar.polo if (sizes != NULL)
1835 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
1836 c758d8da 2019-10-18 omar.polo r.p_borders[i]
1837 c758d8da 2019-10-18 omar.polo = parse_integer(sizes[i], 0);
1839 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1840 c758d8da 2019-10-18 omar.polo "error while parsing "
1841 c758d8da 2019-10-18 omar.polo "MyMenu.prompt.border.size");
1844 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.border.color", "*",
1845 c758d8da 2019-10-18 omar.polo datatype, &value)
1846 c758d8da 2019-10-18 omar.polo == 1) {
1847 df29e05e 2018-10-17 omar.polo char **colors;
1848 df29e05e 2018-10-17 omar.polo colors = parse_csslike(value.addr);
1849 df29e05e 2018-10-17 omar.polo if (colors != NULL)
1850 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
1851 c758d8da 2019-10-18 omar.polo p_borders_bg[i] = parse_color(
1852 c758d8da 2019-10-18 omar.polo colors[i], "#000");
1854 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1855 c758d8da 2019-10-18 omar.polo "error while parsing "
1856 c758d8da 2019-10-18 omar.polo "MyMenu.prompt.border.color");
1859 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.padding", "*",
1860 c758d8da 2019-10-18 omar.polo datatype, &value)
1861 c758d8da 2019-10-18 omar.polo == 1) {
1862 df29e05e 2018-10-17 omar.polo char **colors;
1863 df29e05e 2018-10-17 omar.polo colors = parse_csslike(value.addr);
1864 df29e05e 2018-10-17 omar.polo if (colors != NULL)
1865 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
1866 c758d8da 2019-10-18 omar.polo r.p_padding[i]
1867 c758d8da 2019-10-18 omar.polo = parse_integer(colors[i], 0);
1869 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1870 c758d8da 2019-10-18 omar.polo "error while parsing "
1871 c758d8da 2019-10-18 omar.polo "MyMenu.prompt.padding");
1874 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.width", "*", datatype, &value)
1875 c758d8da 2019-10-18 omar.polo == 1)
1876 c758d8da 2019-10-18 omar.polo r.width = parse_int_with_percentage(
1877 c758d8da 2019-10-18 omar.polo value.addr, r.width, d_width);
1879 c758d8da 2019-10-18 omar.polo fprintf(stderr, "no width defined, using %d\n",
1880 c758d8da 2019-10-18 omar.polo r.width);
1882 c758d8da 2019-10-18 omar.polo if (XrmGetResource(
1883 c758d8da 2019-10-18 omar.polo xdb, "MyMenu.height", "*", datatype, &value)
1884 c758d8da 2019-10-18 omar.polo == 1)
1885 c758d8da 2019-10-18 omar.polo r.height = parse_int_with_percentage(
1886 c758d8da 2019-10-18 omar.polo value.addr, r.height, d_height);
1888 c758d8da 2019-10-18 omar.polo fprintf(stderr, "no height defined, using %d\n",
1889 c758d8da 2019-10-18 omar.polo r.height);
1891 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.x", "*", datatype, &value)
1892 c758d8da 2019-10-18 omar.polo == 1)
1893 c758d8da 2019-10-18 omar.polo x = parse_int_with_pos(
1894 a8ca9c6d 2019-10-20 omar.polo r.d, value.addr, x, d_width, r.width);
1896 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.y", "*", datatype, &value)
1897 c758d8da 2019-10-18 omar.polo == 1)
1898 c758d8da 2019-10-18 omar.polo y = parse_int_with_pos(
1899 a8ca9c6d 2019-10-20 omar.polo r.d, value.addr, y, d_height, r.height);
1901 c758d8da 2019-10-18 omar.polo if (XrmGetResource(
1902 c758d8da 2019-10-18 omar.polo xdb, "MyMenu.border.size", "*", datatype, &value)
1903 c758d8da 2019-10-18 omar.polo == 1) {
1904 1d13acf5 2018-10-04 omar.polo char **borders;
1905 1d13acf5 2018-10-04 omar.polo borders = parse_csslike(value.addr);
1906 df29e05e 2018-10-17 omar.polo if (borders != NULL)
1907 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
1908 c758d8da 2019-10-18 omar.polo r.borders[i]
1909 c758d8da 2019-10-18 omar.polo = parse_int_with_percentage(
1910 c758d8da 2019-10-18 omar.polo borders[i], 0,
1911 c758d8da 2019-10-18 omar.polo (i % 2) == 0
1912 c758d8da 2019-10-18 omar.polo ? d_height
1913 c758d8da 2019-10-18 omar.polo : d_width);
1915 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1916 c758d8da 2019-10-18 omar.polo "error while parsing "
1917 c758d8da 2019-10-18 omar.polo "MyMenu.border.size\n");
1920 1d13acf5 2018-10-04 omar.polo /* Prompt */
1921 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.foreground", "*",
1922 c758d8da 2019-10-18 omar.polo datatype, &value)
1923 c758d8da 2019-10-18 omar.polo == 1)
1924 4c0d79a0 2018-10-09 omar.polo fgs[0] = parse_color(value.addr, "#fff");
1926 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.background", "*",
1927 c758d8da 2019-10-18 omar.polo datatype, &value)
1928 c758d8da 2019-10-18 omar.polo == 1)
1929 4c0d79a0 2018-10-09 omar.polo bgs[0] = parse_color(value.addr, "#000");
1931 1d13acf5 2018-10-04 omar.polo /* Completions */
1932 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.foreground", "*",
1933 c758d8da 2019-10-18 omar.polo datatype, &value)
1934 c758d8da 2019-10-18 omar.polo == 1)
1935 4c0d79a0 2018-10-09 omar.polo fgs[1] = parse_color(value.addr, "#fff");
1937 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.background", "*",
1938 c758d8da 2019-10-18 omar.polo datatype, &value)
1939 c758d8da 2019-10-18 omar.polo == 1)
1940 4c0d79a0 2018-10-09 omar.polo bgs[1] = parse_color(value.addr, "#000");
1942 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.padding", "*",
1943 c758d8da 2019-10-18 omar.polo datatype, &value)
1944 c758d8da 2019-10-18 omar.polo == 1) {
1945 df29e05e 2018-10-17 omar.polo char **paddings;
1946 df29e05e 2018-10-17 omar.polo paddings = parse_csslike(value.addr);
1947 df29e05e 2018-10-17 omar.polo if (paddings != NULL)
1948 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
1949 c758d8da 2019-10-18 omar.polo r.c_padding[i] = parse_integer(
1950 c758d8da 2019-10-18 omar.polo paddings[i], 0);
1952 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1953 c758d8da 2019-10-18 omar.polo "Error while parsing "
1954 c758d8da 2019-10-18 omar.polo "MyMenu.completion.padding");
1957 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.border.size", "*",
1958 c758d8da 2019-10-18 omar.polo datatype, &value)
1959 c758d8da 2019-10-18 omar.polo == 1) {
1960 df29e05e 2018-10-17 omar.polo char **sizes;
1961 df29e05e 2018-10-17 omar.polo sizes = parse_csslike(value.addr);
1962 df29e05e 2018-10-17 omar.polo if (sizes != NULL)
1963 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
1964 c758d8da 2019-10-18 omar.polo r.c_borders[i]
1965 c758d8da 2019-10-18 omar.polo = parse_integer(sizes[i], 0);
1967 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1968 c758d8da 2019-10-18 omar.polo "Error while parsing "
1969 c758d8da 2019-10-18 omar.polo "MyMenu.completion.border.size");
1972 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.border.color", "*",
1973 c758d8da 2019-10-18 omar.polo datatype, &value)
1974 c758d8da 2019-10-18 omar.polo == 1) {
1975 df29e05e 2018-10-17 omar.polo char **sizes;
1976 df29e05e 2018-10-17 omar.polo sizes = parse_csslike(value.addr);
1977 df29e05e 2018-10-17 omar.polo if (sizes != NULL)
1978 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
1979 c758d8da 2019-10-18 omar.polo c_borders_bg[i] = parse_color(
1980 c758d8da 2019-10-18 omar.polo sizes[i], "#000");
1982 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1983 c758d8da 2019-10-18 omar.polo "Error while parsing "
1984 c758d8da 2019-10-18 omar.polo "MyMenu.completion.border.color");
1987 1d13acf5 2018-10-04 omar.polo /* Completion Highlighted */
1988 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb,
1989 c758d8da 2019-10-18 omar.polo "MyMenu.completion_highlighted.foreground", "*",
1990 c758d8da 2019-10-18 omar.polo datatype, &value)
1991 c758d8da 2019-10-18 omar.polo == 1)
1992 4c0d79a0 2018-10-09 omar.polo fgs[2] = parse_color(value.addr, "#000");
1994 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb,
1995 c758d8da 2019-10-18 omar.polo "MyMenu.completion_highlighted.background", "*",
1996 c758d8da 2019-10-18 omar.polo datatype, &value)
1997 c758d8da 2019-10-18 omar.polo == 1)
1998 4c0d79a0 2018-10-09 omar.polo bgs[2] = parse_color(value.addr, "#fff");
2000 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb,
2001 c758d8da 2019-10-18 omar.polo "MyMenu.completion_highlighted.padding", "*",
2002 c758d8da 2019-10-18 omar.polo datatype, &value)
2003 c758d8da 2019-10-18 omar.polo == 1) {
2004 df29e05e 2018-10-17 omar.polo char **paddings;
2005 df29e05e 2018-10-17 omar.polo paddings = parse_csslike(value.addr);
2006 df29e05e 2018-10-17 omar.polo if (paddings != NULL)
2007 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2008 c758d8da 2019-10-18 omar.polo r.ch_padding[i] = parse_integer(
2009 c758d8da 2019-10-18 omar.polo paddings[i], 0);
2011 c758d8da 2019-10-18 omar.polo fprintf(stderr,
2012 c758d8da 2019-10-18 omar.polo "Error while parsing "
2013 c758d8da 2019-10-18 omar.polo "MyMenu.completion_highlighted."
2014 c758d8da 2019-10-18 omar.polo "padding");
2017 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb,
2018 c758d8da 2019-10-18 omar.polo "MyMenu.completion_highlighted.border.size", "*",
2019 c758d8da 2019-10-18 omar.polo datatype, &value)
2020 c758d8da 2019-10-18 omar.polo == 1) {
2021 df29e05e 2018-10-17 omar.polo char **sizes;
2022 df29e05e 2018-10-17 omar.polo sizes = parse_csslike(value.addr);
2023 df29e05e 2018-10-17 omar.polo if (sizes != NULL)
2024 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2025 c758d8da 2019-10-18 omar.polo r.ch_borders[i]
2026 c758d8da 2019-10-18 omar.polo = parse_integer(sizes[i], 0);
2028 c758d8da 2019-10-18 omar.polo fprintf(stderr,
2029 c758d8da 2019-10-18 omar.polo "Error while parsing "
2030 c758d8da 2019-10-18 omar.polo "MyMenu.completion_highlighted."
2031 c758d8da 2019-10-18 omar.polo "border.size");
2034 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb,
2035 c758d8da 2019-10-18 omar.polo "MyMenu.completion_highlighted.border.color", "*",
2036 c758d8da 2019-10-18 omar.polo datatype, &value)
2037 c758d8da 2019-10-18 omar.polo == 1) {
2038 df29e05e 2018-10-17 omar.polo char **colors;
2039 df29e05e 2018-10-17 omar.polo colors = parse_csslike(value.addr);
2040 df29e05e 2018-10-17 omar.polo if (colors != NULL)
2041 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2042 c758d8da 2019-10-18 omar.polo ch_borders_bg[i] = parse_color(
2043 c758d8da 2019-10-18 omar.polo colors[i], "#000");
2045 c758d8da 2019-10-18 omar.polo fprintf(stderr,
2046 c758d8da 2019-10-18 omar.polo "Error while parsing "
2047 c758d8da 2019-10-18 omar.polo "MyMenu.completion_highlighted."
2048 c758d8da 2019-10-18 omar.polo "border.color");
2051 26b541d8 2018-10-04 omar.polo /* Border */
2052 c758d8da 2019-10-18 omar.polo if (XrmGetResource(
2053 c758d8da 2019-10-18 omar.polo xdb, "MyMenu.border.color", "*", datatype, &value)
2054 c758d8da 2019-10-18 omar.polo == 1) {
2055 1d13acf5 2018-10-04 omar.polo char **colors;
2056 1d13acf5 2018-10-04 omar.polo colors = parse_csslike(value.addr);
2057 df29e05e 2018-10-17 omar.polo if (colors != NULL)
2058 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2059 c758d8da 2019-10-18 omar.polo borders_bg[i] = parse_color(
2060 c758d8da 2019-10-18 omar.polo colors[i], "#000");
2062 c758d8da 2019-10-18 omar.polo fprintf(stderr,
2063 c758d8da 2019-10-18 omar.polo "error while parsing "
2064 c758d8da 2019-10-18 omar.polo "MyMenu.border.color\n");
2068 26b541d8 2018-10-04 omar.polo /* Second round of args parsing */
2069 c758d8da 2019-10-18 omar.polo optind = 0; /* reset the option index */
2070 1d13acf5 2018-10-04 omar.polo while ((ch = getopt(argc, argv, ARGS)) != -1) {
2071 1d13acf5 2018-10-04 omar.polo switch (ch) {
2072 1d13acf5 2018-10-04 omar.polo case 'a':
2073 4f769efa 2018-10-06 omar.polo r.first_selected = 1;
2074 1d13acf5 2018-10-04 omar.polo break;
2075 1d13acf5 2018-10-04 omar.polo case 'A':
2076 1d13acf5 2018-10-04 omar.polo /* free_text -- already catched */
2077 1d13acf5 2018-10-04 omar.polo case 'd':
2078 1d13acf5 2018-10-04 omar.polo /* separator -- this case was already catched */
2079 1d13acf5 2018-10-04 omar.polo case 'e':
2080 1d13acf5 2018-10-04 omar.polo /* embedding mymenu this case was already catched. */
2081 1d13acf5 2018-10-04 omar.polo case 'm':
2082 c758d8da 2019-10-18 omar.polo /* multiple selection this case was already catched.
2084 1d13acf5 2018-10-04 omar.polo break;
2085 1d13acf5 2018-10-04 omar.polo case 'p': {
2086 1d13acf5 2018-10-04 omar.polo char *newprompt;
2087 1d13acf5 2018-10-04 omar.polo newprompt = strdup(optarg);
2088 1d13acf5 2018-10-04 omar.polo if (newprompt != NULL) {
2089 4f769efa 2018-10-06 omar.polo free(r.ps1);
2090 4f769efa 2018-10-06 omar.polo r.ps1 = newprompt;
2092 1d13acf5 2018-10-04 omar.polo break;
2094 1d13acf5 2018-10-04 omar.polo case 'x':
2095 a8ca9c6d 2019-10-20 omar.polo x = parse_int_with_pos(
2096 a8ca9c6d 2019-10-20 omar.polo r.d, optarg, x, d_width, r.width);
2097 1d13acf5 2018-10-04 omar.polo break;
2098 1d13acf5 2018-10-04 omar.polo case 'y':
2099 a8ca9c6d 2019-10-20 omar.polo y = parse_int_with_pos(
2100 a8ca9c6d 2019-10-20 omar.polo r.d, optarg, y, d_height, r.height);
2101 1d13acf5 2018-10-04 omar.polo break;
2102 df29e05e 2018-10-17 omar.polo case 'P': {
2103 df29e05e 2018-10-17 omar.polo char **paddings;
2104 df29e05e 2018-10-17 omar.polo if ((paddings = parse_csslike(optarg)) != NULL)
2105 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2106 c758d8da 2019-10-18 omar.polo r.p_padding[i] = parse_integer(
2107 c758d8da 2019-10-18 omar.polo paddings[i], 0);
2108 1d13acf5 2018-10-04 omar.polo break;
2110 df29e05e 2018-10-17 omar.polo case 'G': {
2111 df29e05e 2018-10-17 omar.polo char **colors;
2112 df29e05e 2018-10-17 omar.polo if ((colors = parse_csslike(optarg)) != NULL)
2113 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2114 c758d8da 2019-10-18 omar.polo p_borders_bg[i] = parse_color(
2115 c758d8da 2019-10-18 omar.polo colors[i], "#000");
2116 df29e05e 2018-10-17 omar.polo break;
2118 df29e05e 2018-10-17 omar.polo case 'g': {
2119 df29e05e 2018-10-17 omar.polo char **sizes;
2120 df29e05e 2018-10-17 omar.polo if ((sizes = parse_csslike(optarg)) != NULL)
2121 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2122 c758d8da 2019-10-18 omar.polo r.p_borders[i]
2123 c758d8da 2019-10-18 omar.polo = parse_integer(sizes[i], 0);
2124 df29e05e 2018-10-17 omar.polo break;
2126 df29e05e 2018-10-17 omar.polo case 'I': {
2127 df29e05e 2018-10-17 omar.polo char **colors;
2128 df29e05e 2018-10-17 omar.polo if ((colors = parse_csslike(optarg)) != NULL)
2129 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2130 c758d8da 2019-10-18 omar.polo c_borders_bg[i] = parse_color(
2131 c758d8da 2019-10-18 omar.polo colors[i], "#000");
2132 df29e05e 2018-10-17 omar.polo break;
2134 df29e05e 2018-10-17 omar.polo case 'i': {
2135 df29e05e 2018-10-17 omar.polo char **sizes;
2136 df29e05e 2018-10-17 omar.polo if ((sizes = parse_csslike(optarg)) != NULL)
2137 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2138 c758d8da 2019-10-18 omar.polo r.c_borders[i]
2139 c758d8da 2019-10-18 omar.polo = parse_integer(sizes[i], 0);
2140 df29e05e 2018-10-17 omar.polo break;
2142 df29e05e 2018-10-17 omar.polo case 'J': {
2143 df29e05e 2018-10-17 omar.polo char **colors;
2144 df29e05e 2018-10-17 omar.polo if ((colors = parse_csslike(optarg)) != NULL)
2145 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2146 c758d8da 2019-10-18 omar.polo ch_borders_bg[i] = parse_color(
2147 c758d8da 2019-10-18 omar.polo colors[i], "#000");
2148 df29e05e 2018-10-17 omar.polo break;
2150 df29e05e 2018-10-17 omar.polo case 'j': {
2151 df29e05e 2018-10-17 omar.polo char **sizes;
2152 df29e05e 2018-10-17 omar.polo if ((sizes = parse_csslike(optarg)) != NULL)
2153 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2154 c758d8da 2019-10-18 omar.polo r.ch_borders[i]
2155 c758d8da 2019-10-18 omar.polo = parse_integer(sizes[i], 0);
2156 df29e05e 2018-10-17 omar.polo break;
2158 1d13acf5 2018-10-04 omar.polo case 'l':
2159 4f769efa 2018-10-06 omar.polo r.horizontal_layout = !strcmp(optarg, "horizontal");
2160 1d13acf5 2018-10-04 omar.polo break;
2161 1d13acf5 2018-10-04 omar.polo case 'f': {
2162 1d13acf5 2018-10-04 omar.polo char *newfont;
2163 1d13acf5 2018-10-04 omar.polo if ((newfont = strdup(optarg)) != NULL) {
2164 1d13acf5 2018-10-04 omar.polo free(fontname);
2165 1d13acf5 2018-10-04 omar.polo fontname = newfont;
2167 1d13acf5 2018-10-04 omar.polo break;
2169 1d13acf5 2018-10-04 omar.polo case 'W':
2170 c758d8da 2019-10-18 omar.polo r.width = parse_int_with_percentage(
2171 c758d8da 2019-10-18 omar.polo optarg, r.width, d_width);
2172 1d13acf5 2018-10-04 omar.polo break;
2173 1d13acf5 2018-10-04 omar.polo case 'H':
2174 c758d8da 2019-10-18 omar.polo r.height = parse_int_with_percentage(
2175 c758d8da 2019-10-18 omar.polo optarg, r.height, d_height);
2176 1d13acf5 2018-10-04 omar.polo break;
2177 1d13acf5 2018-10-04 omar.polo case 'b': {
2178 1d13acf5 2018-10-04 omar.polo char **borders;
2179 1d13acf5 2018-10-04 omar.polo if ((borders = parse_csslike(optarg)) != NULL) {
2180 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2181 c758d8da 2019-10-18 omar.polo r.borders[i] = parse_integer(
2182 c758d8da 2019-10-18 omar.polo borders[i], 0);
2183 1d13acf5 2018-10-04 omar.polo } else
2184 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Error parsing b option\n");
2185 1d13acf5 2018-10-04 omar.polo break;
2187 1d13acf5 2018-10-04 omar.polo case 'B': {
2188 1d13acf5 2018-10-04 omar.polo char **colors;
2189 1d13acf5 2018-10-04 omar.polo if ((colors = parse_csslike(optarg)) != NULL) {
2190 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2191 c758d8da 2019-10-18 omar.polo borders_bg[i] = parse_color(
2192 c758d8da 2019-10-18 omar.polo colors[i], "#000");
2193 1d13acf5 2018-10-04 omar.polo } else
2194 c758d8da 2019-10-18 omar.polo fprintf(stderr,
2195 c758d8da 2019-10-18 omar.polo "error while parsing B option\n");
2196 1d13acf5 2018-10-04 omar.polo break;
2198 4f769efa 2018-10-06 omar.polo case 't':
2199 4c0d79a0 2018-10-09 omar.polo fgs[0] = parse_color(optarg, NULL);
2200 1d13acf5 2018-10-04 omar.polo break;
2201 4f769efa 2018-10-06 omar.polo case 'T':
2202 4c0d79a0 2018-10-09 omar.polo bgs[0] = parse_color(optarg, NULL);
2203 1d13acf5 2018-10-04 omar.polo break;
2204 4f769efa 2018-10-06 omar.polo case 'c':
2205 4c0d79a0 2018-10-09 omar.polo fgs[1] = parse_color(optarg, NULL);
2206 1d13acf5 2018-10-04 omar.polo break;
2207 4f769efa 2018-10-06 omar.polo case 'C':
2208 4c0d79a0 2018-10-09 omar.polo bgs[1] = parse_color(optarg, NULL);
2209 1d13acf5 2018-10-04 omar.polo break;
2210 4f769efa 2018-10-06 omar.polo case 's':
2211 4c0d79a0 2018-10-09 omar.polo fgs[2] = parse_color(optarg, NULL);
2212 1d13acf5 2018-10-04 omar.polo break;
2213 4f769efa 2018-10-06 omar.polo case 'S':
2214 4c0d79a0 2018-10-09 omar.polo fgs[2] = parse_color(optarg, NULL);
2215 1d13acf5 2018-10-04 omar.polo break;
2216 1d13acf5 2018-10-04 omar.polo default:
2217 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Unrecognized option %c\n", ch);
2218 1d13acf5 2018-10-04 omar.polo status = ERR;
2219 1d13acf5 2018-10-04 omar.polo break;
2223 df29e05e 2018-10-17 omar.polo if (r.height < 0 || r.width < 0 || x < 0 || y < 0) {
2224 df29e05e 2018-10-17 omar.polo fprintf(stderr, "height, width, x or y are lesser than 0.");
2225 df29e05e 2018-10-17 omar.polo status = ERR;
2228 1d13acf5 2018-10-04 omar.polo /* since only now we know if the first should be selected,
2229 1d13acf5 2018-10-04 omar.polo * update the completion here */
2230 4f769efa 2018-10-06 omar.polo update_completions(cs, text, lines, vlines, r.first_selected);
2232 c758d8da 2019-10-18 omar.polo /* update the prompt lenght, only now we surely know the length of it
2234 4f769efa 2018-10-06 omar.polo r.ps1len = strlen(r.ps1);
2236 26b541d8 2018-10-04 omar.polo /* Create the window */
2237 c758d8da 2019-10-18 omar.polo create_window(&r, parent_window, cmap, vinfo, x, y, offset_x,
2238 c758d8da 2019-10-18 omar.polo offset_y, bgs[1]);
2239 4f769efa 2018-10-06 omar.polo set_win_atoms_hints(r.d, r.w, r.width, r.height);
2240 4f769efa 2018-10-06 omar.polo XMapRaised(r.d, r.w);
2242 1d13acf5 2018-10-04 omar.polo /* If embed, listen for other events as well */
2243 1d13acf5 2018-10-04 omar.polo if (embed) {
2244 c758d8da 2019-10-18 omar.polo Window *children, parent, root;
2245 c758d8da 2019-10-18 omar.polo unsigned int children_no;
2247 4f769efa 2018-10-06 omar.polo XSelectInput(r.d, parent_window, FocusChangeMask);
2248 c758d8da 2019-10-18 omar.polo if (XQueryTree(r.d, parent_window, &root, &parent, &children,
2249 c758d8da 2019-10-18 omar.polo &children_no)
2250 c758d8da 2019-10-18 omar.polo && children) {
2251 c758d8da 2019-10-18 omar.polo for (i = 0; i < children_no && children[i] != r.w;
2253 c758d8da 2019-10-18 omar.polo XSelectInput(
2254 c758d8da 2019-10-18 omar.polo r.d, children[i], FocusChangeMask);
2255 1d13acf5 2018-10-04 omar.polo XFree(children);
2257 4f769efa 2018-10-06 omar.polo grabfocus(r.d, r.w);
2260 4f769efa 2018-10-06 omar.polo take_keyboard(r.d, r.w);
2262 4c0d79a0 2018-10-09 omar.polo r.x_zero = r.borders[3];
2263 4c0d79a0 2018-10-09 omar.polo r.y_zero = r.borders[0];
2266 c758d8da 2019-10-18 omar.polo XGCValues values;
2268 df29e05e 2018-10-17 omar.polo for (i = 0; i < 3; ++i) {
2269 df29e05e 2018-10-17 omar.polo r.fgs[i] = XCreateGC(r.d, r.w, 0, &values);
2270 df29e05e 2018-10-17 omar.polo r.bgs[i] = XCreateGC(r.d, r.w, 0, &values);
2273 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i) {
2274 df29e05e 2018-10-17 omar.polo r.borders_bg[i] = XCreateGC(r.d, r.w, 0, &values);
2275 df29e05e 2018-10-17 omar.polo r.p_borders_bg[i] = XCreateGC(r.d, r.w, 0, &values);
2276 df29e05e 2018-10-17 omar.polo r.c_borders_bg[i] = XCreateGC(r.d, r.w, 0, &values);
2277 df29e05e 2018-10-17 omar.polo r.ch_borders_bg[i] = XCreateGC(r.d, r.w, 0, &values);
2281 df29e05e 2018-10-17 omar.polo /* Load the colors in our GCs */
2282 df29e05e 2018-10-17 omar.polo for (i = 0; i < 3; ++i) {
2283 df29e05e 2018-10-17 omar.polo XSetForeground(r.d, r.fgs[i], fgs[i]);
2284 df29e05e 2018-10-17 omar.polo XSetForeground(r.d, r.bgs[i], bgs[i]);
2287 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i) {
2288 df29e05e 2018-10-17 omar.polo XSetForeground(r.d, r.borders_bg[i], borders_bg[i]);
2289 df29e05e 2018-10-17 omar.polo XSetForeground(r.d, r.p_borders_bg[i], p_borders_bg[i]);
2290 df29e05e 2018-10-17 omar.polo XSetForeground(r.d, r.c_borders_bg[i], c_borders_bg[i]);
2291 df29e05e 2018-10-17 omar.polo XSetForeground(r.d, r.ch_borders_bg[i], ch_borders_bg[i]);
2294 1d13acf5 2018-10-04 omar.polo if (load_font(&r, fontname) == -1)
2295 1d13acf5 2018-10-04 omar.polo status = ERR;
2297 1d13acf5 2018-10-04 omar.polo #ifdef USE_XFT
2298 df29e05e 2018-10-17 omar.polo r.xftdraw = XftDrawCreate(r.d, r.w, vinfo.visual, cmap);
2300 3d95a42e 2018-10-19 omar.polo for (i = 0; i < 3; ++i) {
2301 c758d8da 2019-10-18 omar.polo rgba_t c;
2302 c758d8da 2019-10-18 omar.polo XRenderColor xrcolor;
2304 c758d8da 2019-10-18 omar.polo c = *(rgba_t *)&fgs[i];
2305 c758d8da 2019-10-18 omar.polo xrcolor.red = EXPANDBITS(c.rgba.r);
2306 c758d8da 2019-10-18 omar.polo xrcolor.green = EXPANDBITS(c.rgba.g);
2307 c758d8da 2019-10-18 omar.polo xrcolor.blue = EXPANDBITS(c.rgba.b);
2308 c758d8da 2019-10-18 omar.polo xrcolor.alpha = EXPANDBITS(c.rgba.a);
2309 c758d8da 2019-10-18 omar.polo XftColorAllocValue(
2310 c758d8da 2019-10-18 omar.polo r.d, vinfo.visual, cmap, &xrcolor, &r.xft_colors[i]);
2312 c9a3bfaa 2018-05-21 omar.polo #endif
2314 aed48307 2018-10-06 omar.polo /* compute prompt dimensions */
2315 aed48307 2018-10-06 omar.polo ps1extents(&r);
2317 4f769efa 2018-10-06 omar.polo xim_init(&r, &xdb);
2319 aed48307 2018-10-06 omar.polo #ifdef __OpenBSD__
2320 aed48307 2018-10-06 omar.polo /* Now we need only the ability to write */
2321 aed48307 2018-10-06 omar.polo pledge("stdio", "");
2322 aed48307 2018-10-06 omar.polo #endif
2324 df29e05e 2018-10-17 omar.polo /* Cache text height */
2325 df29e05e 2018-10-17 omar.polo text_extents("fyjpgl", 6, &r, NULL, &r.text_height);
2327 5e448381 2018-10-09 omar.polo /* Draw the window for the first time */
2328 5e448381 2018-10-09 omar.polo draw(&r, text, cs);
2330 1d13acf5 2018-10-04 omar.polo /* Main loop */
2331 1d13acf5 2018-10-04 omar.polo while (status == LOOPING || status == OK_LOOP) {
2332 1d13acf5 2018-10-04 omar.polo status = loop(&r, &text, &textlen, cs, lines, vlines);
2334 1d13acf5 2018-10-04 omar.polo if (status != ERR)
2335 1d13acf5 2018-10-04 omar.polo printf("%s\n", text);
2337 4f769efa 2018-10-06 omar.polo if (!r.multiple_select && status == OK_LOOP)
2338 1d13acf5 2018-10-04 omar.polo status = OK;
2341 4f769efa 2018-10-06 omar.polo XUngrabKeyboard(r.d, CurrentTime);
2343 43e30577 2018-10-20 omar.polo #ifdef USE_XFT
2344 43e30577 2018-10-20 omar.polo for (i = 0; i < 3; ++i)
2345 43e30577 2018-10-20 omar.polo XftColorFree(r.d, vinfo.visual, cmap, &r.xft_colors[i]);
2346 43e30577 2018-10-20 omar.polo #endif
2348 43e30577 2018-10-20 omar.polo for (i = 0; i < 3; ++i) {
2349 43e30577 2018-10-20 omar.polo XFreeGC(r.d, r.fgs[i]);
2350 43e30577 2018-10-20 omar.polo XFreeGC(r.d, r.bgs[i]);
2353 43e30577 2018-10-20 omar.polo for (i = 0; i < 4; ++i) {
2354 43e30577 2018-10-20 omar.polo XFreeGC(r.d, r.borders_bg[i]);
2355 43e30577 2018-10-20 omar.polo XFreeGC(r.d, r.p_borders_bg[i]);
2356 43e30577 2018-10-20 omar.polo XFreeGC(r.d, r.c_borders_bg[i]);
2357 43e30577 2018-10-20 omar.polo XFreeGC(r.d, r.ch_borders_bg[i]);
2360 43e30577 2018-10-20 omar.polo XDestroyIC(r.xic);
2361 43e30577 2018-10-20 omar.polo XCloseIM(r.xim);
2363 b5d751bd 2018-07-07 omar.polo #ifdef USE_XFT
2364 043d71ef 2018-10-19 omar.polo for (i = 0; i < 3; ++i)
2365 043d71ef 2018-10-19 omar.polo XftColorFree(r.d, vinfo.visual, cmap, &r.xft_colors[i]);
2366 43e30577 2018-10-20 omar.polo XftFontClose(r.d, r.font);
2367 43e30577 2018-10-20 omar.polo XftDrawDestroy(r.xftdraw);
2368 43e30577 2018-10-20 omar.polo #else
2369 43e30577 2018-10-20 omar.polo XFreeFontSet(r.d, r.font);
2370 b5d751bd 2018-07-07 omar.polo #endif
2372 4f769efa 2018-10-06 omar.polo free(r.ps1);
2373 1d13acf5 2018-10-04 omar.polo free(fontname);
2374 1d13acf5 2018-10-04 omar.polo free(text);
2376 1d13acf5 2018-10-04 omar.polo free(buf);
2377 1d13acf5 2018-10-04 omar.polo free(lines);
2378 1d13acf5 2018-10-04 omar.polo free(vlines);
2379 1d13acf5 2018-10-04 omar.polo compls_delete(cs);
2381 43e30577 2018-10-20 omar.polo XFreeColormap(r.d, cmap);
2383 1d13acf5 2018-10-04 omar.polo XDestroyWindow(r.d, r.w);
2384 1d13acf5 2018-10-04 omar.polo XCloseDisplay(r.d);
2386 1d13acf5 2018-10-04 omar.polo return status != OK;