1 f5e234d6 2018-05-18 omar.polo #include <stdio.h>
2 f5e234d6 2018-05-18 omar.polo #include <stdlib.h>
3 f5e234d6 2018-05-18 omar.polo #include <string.h> // strdup, strnlen, ...
4 f5e234d6 2018-05-18 omar.polo #include <ctype.h> // isalnum
5 f5e234d6 2018-05-18 omar.polo #include <locale.h> // setlocale
6 f5e234d6 2018-05-18 omar.polo #include <unistd.h>
7 f5e234d6 2018-05-18 omar.polo #include <sysexits.h>
8 f5e234d6 2018-05-18 omar.polo #include <stdbool.h>
9 f5e234d6 2018-05-18 omar.polo #include <limits.h>
10 f5e234d6 2018-05-18 omar.polo #include <errno.h>
12 f5e234d6 2018-05-18 omar.polo #include <X11/Xlib.h>
13 f5e234d6 2018-05-18 omar.polo #include <X11/Xutil.h> // XLookupString
14 f5e234d6 2018-05-18 omar.polo #include <X11/Xresource.h>
15 f5e234d6 2018-05-18 omar.polo #include <X11/Xcms.h> // colors
16 347d23da 2018-05-19 omar.polo #include <X11/keysym.h>
18 f5e234d6 2018-05-18 omar.polo #ifdef USE_XINERAMA
19 f5e234d6 2018-05-18 omar.polo # include <X11/extensions/Xinerama.h>
22 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
23 c9a3bfaa 2018-05-21 omar.polo # include <X11/Xft/Xft.h>
26 b500fe86 2018-06-07 omar.polo #ifndef VERSION
27 b500fe86 2018-06-07 omar.polo # define VERSION "unknown"
30 8b929918 2018-07-01 omar.polo // Comfy
31 f5e234d6 2018-05-18 omar.polo #define nil NULL
33 347d23da 2018-05-19 omar.polo #define resname "MyMenu"
34 347d23da 2018-05-19 omar.polo #define resclass "mymenu"
36 b5d751bd 2018-07-07 omar.polo #define SYM_BUF_SIZE 4
38 9e94fcbe 2018-05-22 omar.polo #ifdef USE_XFT
39 9e94fcbe 2018-05-22 omar.polo # define default_fontname "monospace"
41 9e94fcbe 2018-05-22 omar.polo # define default_fontname "fixed"
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 9e94fcbe 2018-05-22 omar.polo // If we don't have it or we don't want an "ignore case" completion
48 9e94fcbe 2018-05-22 omar.polo // style, fall back to `strstr(3)`
49 9e94fcbe 2018-05-22 omar.polo #ifndef USE_STRCASESTR
50 fa5a6135 2018-05-25 omar.polo # define strcasestr strstr
53 95b27a5e 2018-05-19 omar.polo #define INITIAL_ITEMS 64
55 b10f01c1 2018-07-06 omar.polo #define check_allocation(a) { \
56 b10f01c1 2018-07-06 omar.polo if (a == nil) { \
57 b10f01c1 2018-07-06 omar.polo fprintf(stderr, "Could not allocate memory\n"); \
58 b10f01c1 2018-07-06 omar.polo abort(); \
62 42c3f269 2018-07-08 omar.polo #define inner_height(r) (r->height - r->border_n - r->border_s)
63 42c3f269 2018-07-08 omar.polo #define inner_width(r) (r->width - r->border_e - r->border_w)
65 8b929918 2018-07-01 omar.polo // The possible state of the event loop.
66 f5e234d6 2018-05-18 omar.polo enum state {LOOPING, OK, ERR};
68 8b929918 2018-07-01 omar.polo // for the drawing-related function. The text to be rendered could be
69 8b929918 2018-07-01 omar.polo // the prompt, a completion or a highlighted completion
70 c9a3bfaa 2018-05-21 omar.polo enum text_type {PROMPT, COMPL, COMPL_HIGH};
72 8b929918 2018-07-01 omar.polo // These are the possible action to be performed after user input.
73 2128b469 2018-06-30 omar.polo enum action {
75 2128b469 2018-06-30 omar.polo CONFIRM,
76 2128b469 2018-06-30 omar.polo NEXT_COMPL,
77 2128b469 2018-06-30 omar.polo PREV_COMPL,
78 2128b469 2018-06-30 omar.polo DEL_CHAR,
79 2128b469 2018-06-30 omar.polo DEL_WORD,
80 2128b469 2018-06-30 omar.polo DEL_LINE,
81 6254fed8 2018-07-06 omar.polo ADD_CHAR,
82 6254fed8 2018-07-06 omar.polo TOGGLE_FIRST_SELECTED
85 f5e234d6 2018-05-18 omar.polo struct rendering {
86 42c3f269 2018-07-08 omar.polo Display *d; // connection to xorg
87 f5e234d6 2018-05-18 omar.polo Window w;
88 e5186d6b 2018-05-26 omar.polo int width;
89 e5186d6b 2018-05-26 omar.polo int height;
90 e5186d6b 2018-05-26 omar.polo int padding;
91 42c3f269 2018-07-08 omar.polo int x_zero; // the "zero" on the x axis (may not be 0 'cause the border)
92 42c3f269 2018-07-08 omar.polo int y_zero; // the same a x_zero, only for the y axis
94 42c3f269 2018-07-08 omar.polo // The four border
95 42c3f269 2018-07-08 omar.polo int border_n;
96 42c3f269 2018-07-08 omar.polo int border_e;
97 42c3f269 2018-07-08 omar.polo int border_s;
98 42c3f269 2018-07-08 omar.polo int border_w;
100 e5186d6b 2018-05-26 omar.polo bool horizontal_layout;
102 42c3f269 2018-07-08 omar.polo // the prompt
103 e5186d6b 2018-05-26 omar.polo char *ps1;
104 e5186d6b 2018-05-26 omar.polo int ps1len;
106 42c3f269 2018-07-08 omar.polo // colors
107 f5e234d6 2018-05-18 omar.polo GC prompt;
108 f5e234d6 2018-05-18 omar.polo GC prompt_bg;
109 f5e234d6 2018-05-18 omar.polo GC completion;
110 f5e234d6 2018-05-18 omar.polo GC completion_bg;
111 f5e234d6 2018-05-18 omar.polo GC completion_highlighted;
112 f5e234d6 2018-05-18 omar.polo GC completion_highlighted_bg;
113 42c3f269 2018-07-08 omar.polo GC border_n_bg;
114 42c3f269 2018-07-08 omar.polo GC border_e_bg;
115 42c3f269 2018-07-08 omar.polo GC border_s_bg;
116 42c3f269 2018-07-08 omar.polo GC border_w_bg;
117 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
118 e5186d6b 2018-05-26 omar.polo XftFont *font;
119 c9a3bfaa 2018-05-21 omar.polo XftDraw *xftdraw;
120 c9a3bfaa 2018-05-21 omar.polo XftColor xft_prompt;
121 c9a3bfaa 2018-05-21 omar.polo XftColor xft_completion;
122 c9a3bfaa 2018-05-21 omar.polo XftColor xft_completion_highlighted;
124 c9a3bfaa 2018-05-21 omar.polo XFontSet *font;
125 c9a3bfaa 2018-05-21 omar.polo #endif
128 8b929918 2018-07-01 omar.polo // A simple linked list to store the completions.
129 b5d751bd 2018-07-07 omar.polo struct completion {
130 f5e234d6 2018-05-18 omar.polo char *completion;
131 b5d751bd 2018-07-07 omar.polo struct completion *next;
134 b5d751bd 2018-07-07 omar.polo struct completions {
135 b5d751bd 2018-07-07 omar.polo struct completion *completions;
136 b5d751bd 2018-07-07 omar.polo int selected;
137 b5d751bd 2018-07-07 omar.polo int lenght;
140 2128b469 2018-06-30 omar.polo // return a newly allocated (and empty) completion list
141 b5d751bd 2018-07-07 omar.polo struct completions *compls_new() {
142 b5d751bd 2018-07-07 omar.polo struct completions *cs = malloc(sizeof(struct completions));
144 b5d751bd 2018-07-07 omar.polo if (cs == nil)
145 b5d751bd 2018-07-07 omar.polo return cs;
147 b5d751bd 2018-07-07 omar.polo cs->completions = nil;
148 b5d751bd 2018-07-07 omar.polo cs->selected = -1;
149 b5d751bd 2018-07-07 omar.polo cs->lenght = 0;
150 b5d751bd 2018-07-07 omar.polo return cs;
153 b5d751bd 2018-07-07 omar.polo struct completion *compl_new() {
154 b5d751bd 2018-07-07 omar.polo struct completion *c = malloc(sizeof(struct completion));
155 f5e234d6 2018-05-18 omar.polo if (c == nil)
156 f5e234d6 2018-05-18 omar.polo return c;
158 f5e234d6 2018-05-18 omar.polo c->completion = nil;
159 f5e234d6 2018-05-18 omar.polo c->next = nil;
160 f5e234d6 2018-05-18 omar.polo return c;
163 2128b469 2018-06-30 omar.polo // delete ONLY the given completion (i.e. does not free c->next...)
164 b5d751bd 2018-07-07 omar.polo void compl_delete(struct completion *c) {
165 f5e234d6 2018-05-18 omar.polo free(c);
168 b5d751bd 2018-07-07 omar.polo // delete the current completion and the next (c->next) and so on...
169 b5d751bd 2018-07-07 omar.polo void compl_delete_rec(struct completion *c) {
170 3997bf41 2018-05-25 omar.polo while (c != nil) {
171 b5d751bd 2018-07-07 omar.polo struct completion *t = c->next;
172 3997bf41 2018-05-25 omar.polo free(c);
173 3997bf41 2018-05-25 omar.polo c = t;
177 b5d751bd 2018-07-07 omar.polo void compls_delete(struct completions *cs) {
178 b5d751bd 2018-07-07 omar.polo if (cs == nil)
179 b5d751bd 2018-07-07 omar.polo return;
181 b5d751bd 2018-07-07 omar.polo compl_delete_rec(cs->completions);
182 b5d751bd 2018-07-07 omar.polo free(cs);
185 b5d751bd 2018-07-07 omar.polo // create a completion list from a text and the list of possible
186 b5d751bd 2018-07-07 omar.polo // completions (null terminated). Expects a non-null `cs'.
187 b5d751bd 2018-07-07 omar.polo void filter(struct completions *cs, char *text, char **lines) {
188 b5d751bd 2018-07-07 omar.polo struct completion *c = compl_new();
189 b5d751bd 2018-07-07 omar.polo if (c == nil) {
190 b5d751bd 2018-07-07 omar.polo return;
193 b5d751bd 2018-07-07 omar.polo cs->completions = c;
195 b5d751bd 2018-07-07 omar.polo int index = 0;
196 b5d751bd 2018-07-07 omar.polo int matching = 0;
198 b5d751bd 2018-07-07 omar.polo while (true) {
199 b5d751bd 2018-07-07 omar.polo char *l = lines[index];
200 f5e234d6 2018-05-18 omar.polo if (l == nil)
201 f5e234d6 2018-05-18 omar.polo break;
203 9e94fcbe 2018-05-22 omar.polo if (strcasestr(l, text) != nil) {
204 b5d751bd 2018-07-07 omar.polo matching++;
206 f5e234d6 2018-05-18 omar.polo c->next = compl_new();
207 f5e234d6 2018-05-18 omar.polo c = c->next;
208 3997bf41 2018-05-25 omar.polo if (c == nil) {
209 b5d751bd 2018-07-07 omar.polo compls_delete(cs);
210 b5d751bd 2018-07-07 omar.polo return;
212 f5e234d6 2018-05-18 omar.polo c->completion = l;
215 b5d751bd 2018-07-07 omar.polo index++;
218 b5d751bd 2018-07-07 omar.polo struct completion *f = cs->completions->next;
219 b5d751bd 2018-07-07 omar.polo compl_delete(cs->completions);
220 b5d751bd 2018-07-07 omar.polo cs->completions = f;
221 b5d751bd 2018-07-07 omar.polo cs->lenght = matching;
222 b5d751bd 2018-07-07 omar.polo cs->selected = -1;
225 2128b469 2018-06-30 omar.polo // update the given completion, that is: clean the old cs & generate a new one.
226 b5d751bd 2018-07-07 omar.polo void update_completions(struct completions *cs, char *text, char **lines, bool first_selected) {
227 b5d751bd 2018-07-07 omar.polo compl_delete_rec(cs->completions);
228 b5d751bd 2018-07-07 omar.polo filter(cs, text, lines);
229 b5d751bd 2018-07-07 omar.polo if (first_selected && cs->lenght > 0)
230 b5d751bd 2018-07-07 omar.polo cs->selected = 0;
233 2128b469 2018-06-30 omar.polo // select the next, or the previous, selection and update some
234 36319ab7 2018-07-01 omar.polo // state. `text' will be updated with the text of the completion and
235 36319ab7 2018-07-01 omar.polo // `textlen' with the new lenght of `text'. If the memory cannot be
236 36319ab7 2018-07-01 omar.polo // allocated, `status' will be set to `ERR'.
237 36319ab7 2018-07-01 omar.polo void complete(struct completions *cs, bool first_selected, bool p, char **text, int *textlen, enum state *status) {
238 b5d751bd 2018-07-07 omar.polo if (cs == nil || cs->lenght == 0)
239 b5d751bd 2018-07-07 omar.polo return;
241 36319ab7 2018-07-01 omar.polo // if the first is always selected, and the first entry is different
242 36319ab7 2018-07-01 omar.polo // from the text, expand the text and return
243 36319ab7 2018-07-01 omar.polo if (first_selected
244 b5d751bd 2018-07-07 omar.polo && cs->selected == 0
245 b5d751bd 2018-07-07 omar.polo && strcmp(cs->completions->completion, *text) != 0
246 36319ab7 2018-07-01 omar.polo && !p) {
247 36319ab7 2018-07-01 omar.polo free(*text);
248 b5d751bd 2018-07-07 omar.polo *text = strdup(cs->completions->completion);
249 36319ab7 2018-07-01 omar.polo if (text == nil) {
250 36319ab7 2018-07-01 omar.polo *status = ERR;
251 36319ab7 2018-07-01 omar.polo return;
253 36319ab7 2018-07-01 omar.polo *textlen = strlen(*text);
254 36319ab7 2018-07-01 omar.polo return;
257 b5d751bd 2018-07-07 omar.polo int index = cs->selected;
259 b5d751bd 2018-07-07 omar.polo if (index == -1 && p)
260 b5d751bd 2018-07-07 omar.polo index = 0;
261 42a36584 2018-07-08 omar.polo index = cs->selected = (cs->lenght + (p ? index - 1 : index + 1)) % cs->lenght;
263 b5d751bd 2018-07-07 omar.polo struct completion *n = cs->completions;
265 b5d751bd 2018-07-07 omar.polo // find the selected item
266 b5d751bd 2018-07-07 omar.polo while (index != 0) {
267 b5d751bd 2018-07-07 omar.polo index--;
268 b5d751bd 2018-07-07 omar.polo n = n->next;
271 b5d751bd 2018-07-07 omar.polo free(*text);
272 b5d751bd 2018-07-07 omar.polo *text = strdup(n->completion);
273 b5d751bd 2018-07-07 omar.polo if (text == nil) {
274 b5d751bd 2018-07-07 omar.polo fprintf(stderr, "Memory allocation error!\n");
275 b5d751bd 2018-07-07 omar.polo *status = ERR;
276 b5d751bd 2018-07-07 omar.polo return;
278 b5d751bd 2018-07-07 omar.polo *textlen = strlen(*text);
281 f5e234d6 2018-05-18 omar.polo // push the character c at the end of the string pointed by p
282 f5e234d6 2018-05-18 omar.polo int pushc(char **p, int maxlen, char c) {
283 f5e234d6 2018-05-18 omar.polo int len = strnlen(*p, maxlen);
285 f5e234d6 2018-05-18 omar.polo if (!(len < maxlen -2)) {
286 f5e234d6 2018-05-18 omar.polo maxlen += maxlen >> 1;
287 f5e234d6 2018-05-18 omar.polo char *newptr = realloc(*p, maxlen);
288 f5e234d6 2018-05-18 omar.polo if (newptr == nil) { // bad!
289 f5e234d6 2018-05-18 omar.polo return -1;
291 f5e234d6 2018-05-18 omar.polo *p = newptr;
294 f5e234d6 2018-05-18 omar.polo (*p)[len] = c;
295 f5e234d6 2018-05-18 omar.polo (*p)[len+1] = '\0';
296 f5e234d6 2018-05-18 omar.polo return maxlen;
299 2128b469 2018-06-30 omar.polo // return the number of character
300 347d23da 2018-05-19 omar.polo int utf8strnlen(char *s, int maxlen) {
301 347d23da 2018-05-19 omar.polo int len = 0;
302 347d23da 2018-05-19 omar.polo while (*s && maxlen > 0) {
303 347d23da 2018-05-19 omar.polo len += (*s++ & 0xc0) != 0x80;
304 347d23da 2018-05-19 omar.polo maxlen--;
306 347d23da 2018-05-19 omar.polo return len;
309 347d23da 2018-05-19 omar.polo // remove the last *glyph* from the *utf8* string!
310 347d23da 2018-05-19 omar.polo // this is different from just setting the last byte to 0 (in some
311 347d23da 2018-05-19 omar.polo // cases ofc). The actual implementation is quite inefficient because
312 2128b469 2018-06-30 omar.polo // it remove the last byte until the number of glyphs doesn't change
313 f5e234d6 2018-05-18 omar.polo void popc(char *p, int maxlen) {
314 f5e234d6 2018-05-18 omar.polo int len = strnlen(p, maxlen);
316 347d23da 2018-05-19 omar.polo if (len == 0)
317 347d23da 2018-05-19 omar.polo return;
319 347d23da 2018-05-19 omar.polo int ulen = utf8strnlen(p, maxlen);
320 347d23da 2018-05-19 omar.polo while (len > 0 && utf8strnlen(p, maxlen) == ulen) {
321 347d23da 2018-05-19 omar.polo len--;
322 347d23da 2018-05-19 omar.polo p[len] = 0;
326 8758854a 2018-05-20 omar.polo // If the string is surrounded by quotes (`"`) remove them and replace
327 8758854a 2018-05-20 omar.polo // every `\"` in the string with `"`
328 8758854a 2018-05-20 omar.polo char *normalize_str(const char *str) {
329 8758854a 2018-05-20 omar.polo int len = strlen(str);
330 8758854a 2018-05-20 omar.polo if (len == 0)
331 8758854a 2018-05-20 omar.polo return nil;
333 8758854a 2018-05-20 omar.polo char *s = calloc(len, sizeof(char));
334 8758854a 2018-05-20 omar.polo check_allocation(s);
335 8758854a 2018-05-20 omar.polo int p = 0;
336 8758854a 2018-05-20 omar.polo while (*str) {
337 8758854a 2018-05-20 omar.polo char c = *str;
338 8758854a 2018-05-20 omar.polo if (*str == '\\') {
339 8758854a 2018-05-20 omar.polo if (*(str + 1)) {
340 8758854a 2018-05-20 omar.polo s[p] = *(str + 1);
342 8758854a 2018-05-20 omar.polo str += 2; // skip this and the next char
343 8758854a 2018-05-20 omar.polo continue;
344 8758854a 2018-05-20 omar.polo } else {
345 8758854a 2018-05-20 omar.polo break;
348 8758854a 2018-05-20 omar.polo if (c == '"') {
349 8758854a 2018-05-20 omar.polo str++; // skip only this char
350 8758854a 2018-05-20 omar.polo continue;
352 8758854a 2018-05-20 omar.polo s[p] = c;
354 8758854a 2018-05-20 omar.polo str++;
356 8758854a 2018-05-20 omar.polo return s;
359 f5e234d6 2018-05-18 omar.polo // read an arbitrary long line from stdin and return a pointer to it
360 f5e234d6 2018-05-18 omar.polo // TODO: resize the allocated memory to exactly fit the string once
361 f5e234d6 2018-05-18 omar.polo // read?
362 f5e234d6 2018-05-18 omar.polo char *readline(bool *eof) {
363 f5e234d6 2018-05-18 omar.polo int maxlen = 8;
364 f5e234d6 2018-05-18 omar.polo char *str = calloc(maxlen, sizeof(char));
365 f5e234d6 2018-05-18 omar.polo if (str == nil) {
366 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Cannot allocate memory!\n");
367 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
370 f5e234d6 2018-05-18 omar.polo int c;
371 f5e234d6 2018-05-18 omar.polo while((c = getchar()) != EOF) {
372 f5e234d6 2018-05-18 omar.polo if (c == '\n')
373 f5e234d6 2018-05-18 omar.polo return str;
375 f5e234d6 2018-05-18 omar.polo maxlen = pushc(&str, maxlen, c);
377 f5e234d6 2018-05-18 omar.polo if (maxlen == -1) {
378 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Cannot allocate memory!\n");
379 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
382 f5e234d6 2018-05-18 omar.polo *eof = true;
383 f5e234d6 2018-05-18 omar.polo return str;
386 e9f0b467 2018-06-07 omar.polo // read an arbitrary amount of text until an EOF and store it in
387 e9f0b467 2018-06-07 omar.polo // lns. `items` is the capacity of lns. It may increase lns with
388 e9f0b467 2018-06-07 omar.polo // `realloc(3)` to store more line. Return the number of lines
389 e9f0b467 2018-06-07 omar.polo // read. The last item will always be a NULL pointer. It ignore the
390 e9f0b467 2018-06-07 omar.polo // "null" (empty) lines
391 95b27a5e 2018-05-19 omar.polo int readlines (char ***lns, int items) {
392 f5e234d6 2018-05-18 omar.polo bool finished = false;
393 f5e234d6 2018-05-18 omar.polo int n = 0;
394 95b27a5e 2018-05-19 omar.polo char **lines = *lns;
395 95b27a5e 2018-05-19 omar.polo while (true) {
396 f5e234d6 2018-05-18 omar.polo lines[n] = readline(&finished);
398 6da98882 2018-05-20 omar.polo if (strlen(lines[n]) == 0 || lines[n][0] == '\n') {
399 6da98882 2018-05-20 omar.polo free(lines[n]);
400 f5e234d6 2018-05-18 omar.polo --n; // forget about this line
403 f5e234d6 2018-05-18 omar.polo if (finished)
404 f5e234d6 2018-05-18 omar.polo break;
408 95b27a5e 2018-05-19 omar.polo if (n == items - 1) {
409 95b27a5e 2018-05-19 omar.polo items += items >>1;
410 95b27a5e 2018-05-19 omar.polo char **l = realloc(lines, sizeof(char*) * items);
411 95b27a5e 2018-05-19 omar.polo check_allocation(l);
412 95b27a5e 2018-05-19 omar.polo *lns = l;
413 95b27a5e 2018-05-19 omar.polo lines = l;
418 f5e234d6 2018-05-18 omar.polo lines[n] = nil;
419 95b27a5e 2018-05-19 omar.polo return items;
422 25bf99d2 2018-05-22 omar.polo // Compute the dimension of the string str once rendered, return the
423 25bf99d2 2018-05-22 omar.polo // width and save the width and the height in ret_width and ret_height
424 c9a3bfaa 2018-05-21 omar.polo int text_extents(char *str, int len, struct rendering *r, int *ret_width, int *ret_height) {
425 c9a3bfaa 2018-05-21 omar.polo int height;
426 c9a3bfaa 2018-05-21 omar.polo int width;
427 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
428 c9a3bfaa 2018-05-21 omar.polo XGlyphInfo gi;
429 c9a3bfaa 2018-05-21 omar.polo XftTextExtentsUtf8(r->d, r->font, str, len, &gi);
430 25bf99d2 2018-05-22 omar.polo /* height = gi.height; */
431 e5186d6b 2018-05-26 omar.polo /* height = (gi.height + (r->font->ascent - r->font->descent)/2) / 2; */
432 e5186d6b 2018-05-26 omar.polo /* height = (r->font->ascent - r->font->descent)/2 + gi.height*2; */
433 e5186d6b 2018-05-26 omar.polo height = r->font->ascent - r->font->descent;
434 c9a3bfaa 2018-05-21 omar.polo width = gi.width - gi.x;
436 c9a3bfaa 2018-05-21 omar.polo XRectangle rect;
437 c9a3bfaa 2018-05-21 omar.polo XmbTextExtents(*r->font, str, len, nil, &rect);
438 c9a3bfaa 2018-05-21 omar.polo height = rect.height;
439 c9a3bfaa 2018-05-21 omar.polo width = rect.width;
440 c9a3bfaa 2018-05-21 omar.polo #endif
441 c9a3bfaa 2018-05-21 omar.polo if (ret_width != nil) *ret_width = width;
442 c9a3bfaa 2018-05-21 omar.polo if (ret_height != nil) *ret_height = height;
443 c9a3bfaa 2018-05-21 omar.polo return width;
446 25bf99d2 2018-05-22 omar.polo // Draw the string str
447 c9a3bfaa 2018-05-21 omar.polo void draw_string(char *str, int len, int x, int y, struct rendering *r, enum text_type tt) {
448 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
449 c9a3bfaa 2018-05-21 omar.polo XftColor xftcolor;
450 c9a3bfaa 2018-05-21 omar.polo if (tt == PROMPT) xftcolor = r->xft_prompt;
451 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL) xftcolor = r->xft_completion;
452 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL_HIGH) xftcolor = r->xft_completion_highlighted;
454 c9a3bfaa 2018-05-21 omar.polo XftDrawStringUtf8(r->xftdraw, &xftcolor, r->font, x, y, str, len);
456 c9a3bfaa 2018-05-21 omar.polo GC gc;
457 c9a3bfaa 2018-05-21 omar.polo if (tt == PROMPT) gc = r->prompt;
458 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL) gc = r->completion;
459 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL_HIGH) gc = r->completion_highlighted;
460 c9a3bfaa 2018-05-21 omar.polo Xutf8DrawString(r->d, r->w, *r->font, gc, x, y, str, len);
461 c9a3bfaa 2018-05-21 omar.polo #endif
464 25bf99d2 2018-05-22 omar.polo // Duplicate the string str and substitute every space with a 'n'
465 c9a3bfaa 2018-05-21 omar.polo char *strdupn(char *str) {
466 c9a3bfaa 2018-05-21 omar.polo int len = strlen(str);
468 c9a3bfaa 2018-05-21 omar.polo if (str == nil || len == 0)
469 c9a3bfaa 2018-05-21 omar.polo return nil;
471 c9a3bfaa 2018-05-21 omar.polo char *dup = strdup(str);
472 c9a3bfaa 2018-05-21 omar.polo if (dup == nil)
473 c9a3bfaa 2018-05-21 omar.polo return nil;
475 c9a3bfaa 2018-05-21 omar.polo for (int i = 0; i < len; ++i)
476 c9a3bfaa 2018-05-21 omar.polo if (dup[i] == ' ')
477 c9a3bfaa 2018-05-21 omar.polo dup[i] = 'n';
479 c9a3bfaa 2018-05-21 omar.polo return dup;
482 f5e234d6 2018-05-18 omar.polo // |------------------|----------------------------------------------|
483 f5e234d6 2018-05-18 omar.polo // | 20 char text | completion | completion | completion | compl |
484 f5e234d6 2018-05-18 omar.polo // |------------------|----------------------------------------------|
485 36a15a9f 2018-05-19 omar.polo void draw_horizontally(struct rendering *r, char *text, struct completions *cs) {
486 f5e234d6 2018-05-18 omar.polo int prompt_width = 20; // char
488 c9a3bfaa 2018-05-21 omar.polo int width, height;
489 42c3f269 2018-07-08 omar.polo int ps1xlen = text_extents(r->ps1, r->ps1len, r, &width, &height);
490 8758854a 2018-05-20 omar.polo int start_at = ps1xlen;
492 42c3f269 2018-07-08 omar.polo start_at = r->x_zero + text_extents("n", 1, r, nil, nil);
493 e5186d6b 2018-05-26 omar.polo start_at = start_at * prompt_width + r->padding;
495 c9a3bfaa 2018-05-21 omar.polo int texty = (height + r->height) >>1;
497 42c3f269 2018-07-08 omar.polo XFillRectangle(r->d, r->w, r->prompt_bg, r->x_zero, r->y_zero, start_at, inner_height(r));
499 f5e234d6 2018-05-18 omar.polo int text_len = strlen(text);
500 f5e234d6 2018-05-18 omar.polo if (text_len > prompt_width)
501 f5e234d6 2018-05-18 omar.polo text = text + (text_len - prompt_width);
502 42c3f269 2018-07-08 omar.polo draw_string(r->ps1, r->ps1len, r->x_zero + r->padding, texty, r, PROMPT);
503 42c3f269 2018-07-08 omar.polo draw_string(text, MIN(text_len, prompt_width), r->x_zero + r->padding + ps1xlen, texty, r, PROMPT);
505 42c3f269 2018-07-08 omar.polo XFillRectangle(r->d, r->w, r->completion_bg, start_at, r->y_zero, r->width, r->height);
507 b5d751bd 2018-07-07 omar.polo struct completion *c = cs->completions;
508 b5d751bd 2018-07-07 omar.polo for (int i = 0; c != nil; ++i) {
509 b5d751bd 2018-07-07 omar.polo enum text_type tt = cs->selected == i ? COMPL_HIGH : COMPL;
510 b5d751bd 2018-07-07 omar.polo GC h = cs->selected == i ? r->completion_highlighted_bg : r->completion_bg;
512 b5d751bd 2018-07-07 omar.polo int len = strlen(c->completion);
513 b5d751bd 2018-07-07 omar.polo int text_width = text_extents(c->completion, len, r, nil, nil);
515 42c3f269 2018-07-08 omar.polo XFillRectangle(r->d, r->w, h, start_at, r->y_zero, text_width + r->padding*2, inner_height(r));
517 b5d751bd 2018-07-07 omar.polo draw_string(c->completion, len, start_at + r->padding, texty, r, tt);
519 e5186d6b 2018-05-26 omar.polo start_at += text_width + r->padding * 2;
521 42c3f269 2018-07-08 omar.polo if (start_at > inner_width(r))
522 0ee198aa 2018-05-19 omar.polo break; // don't draw completion if the space isn't enough
524 b5d751bd 2018-07-07 omar.polo c = c->next;
528 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
529 36a15a9f 2018-05-19 omar.polo // | prompt |
530 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
531 36a15a9f 2018-05-19 omar.polo // | completion |
532 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
533 36a15a9f 2018-05-19 omar.polo // | completion |
534 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
535 e5186d6b 2018-05-26 omar.polo void draw_vertically(struct rendering *r, char *text, struct completions *cs) {
536 c9a3bfaa 2018-05-21 omar.polo int height, width;
537 e5186d6b 2018-05-26 omar.polo text_extents("fjpgl", 5, r, nil, &height);
538 e5186d6b 2018-05-26 omar.polo int start_at = height + r->padding;
540 42c3f269 2018-07-08 omar.polo XFillRectangle(r->d, r->w, r->completion_bg, r->x_zero, r->y_zero, r->width, r->height);
541 42c3f269 2018-07-08 omar.polo XFillRectangle(r->d, r->w, r->prompt_bg, r->x_zero, r->y_zero, r->width, start_at);
543 42c3f269 2018-07-08 omar.polo int ps1xlen = text_extents(r->ps1, r->ps1len, r, nil, nil);
545 ec3f7824 2018-07-08 omar.polo draw_string(r->ps1, r->ps1len, r->x_zero + r->padding, height + r->padding, r, PROMPT);
546 ec3f7824 2018-07-08 omar.polo draw_string(text, strlen(text), r->x_zero + r->padding + ps1xlen, height + r->padding, r, PROMPT);
548 e5186d6b 2018-05-26 omar.polo start_at += r->padding;
550 b5d751bd 2018-07-07 omar.polo struct completion *c = cs->completions;
551 b5d751bd 2018-07-07 omar.polo for (int i = 0; c != nil; ++i){
552 b5d751bd 2018-07-07 omar.polo enum text_type tt = cs->selected == i ? COMPL_HIGH : COMPL;
553 b5d751bd 2018-07-07 omar.polo GC h = cs->selected == i ? r->completion_highlighted_bg : r->completion_bg;
555 b5d751bd 2018-07-07 omar.polo int len = strlen(c->completion);
556 b5d751bd 2018-07-07 omar.polo text_extents(c->completion, len, r, &width, &height);
557 42c3f269 2018-07-08 omar.polo XFillRectangle(r->d, r->w, h, r->x_zero, start_at, inner_width(r), height + r->padding*2);
558 42c3f269 2018-07-08 omar.polo draw_string(c->completion, len, r->x_zero + r->padding, start_at + height + r->padding, r, tt);
560 e5186d6b 2018-05-26 omar.polo start_at += height + r->padding *2;
562 42c3f269 2018-07-08 omar.polo if (start_at > inner_height(r))
563 0ee198aa 2018-05-19 omar.polo break; // don't draw completion if the space isn't enough
565 b5d751bd 2018-07-07 omar.polo c = c->next;
569 36a15a9f 2018-05-19 omar.polo void draw(struct rendering *r, char *text, struct completions *cs) {
570 36a15a9f 2018-05-19 omar.polo if (r->horizontal_layout)
571 36a15a9f 2018-05-19 omar.polo draw_horizontally(r, text, cs);
573 36a15a9f 2018-05-19 omar.polo draw_vertically(r, text, cs);
575 42c3f269 2018-07-08 omar.polo // draw the borders
577 42c3f269 2018-07-08 omar.polo if (r->border_w != 0)
578 42c3f269 2018-07-08 omar.polo XFillRectangle(r->d, r->w, r->border_w_bg, 0, 0, r->border_w, r->height);
580 42c3f269 2018-07-08 omar.polo if (r->border_e != 0)
581 42c3f269 2018-07-08 omar.polo XFillRectangle(r->d, r->w, r->border_e_bg, r->width - r->border_e, 0, r->border_e, r->height);
583 42c3f269 2018-07-08 omar.polo if (r->border_n != 0)
584 42c3f269 2018-07-08 omar.polo XFillRectangle(r->d, r->w, r->border_n_bg, 0, 0, r->width, r->border_n);
586 42c3f269 2018-07-08 omar.polo if (r->border_s != 0)
587 42c3f269 2018-07-08 omar.polo XFillRectangle(r->d, r->w, r->border_s_bg, 0, r->height - r->border_s, r->width, r->border_s);
589 42c3f269 2018-07-08 omar.polo // send all the work to x
590 42c3f269 2018-07-08 omar.polo XFlush(r->d);
593 f5e234d6 2018-05-18 omar.polo /* Set some WM stuff */
594 f5e234d6 2018-05-18 omar.polo void set_win_atoms_hints(Display *d, Window w, int width, int height) {
595 f5e234d6 2018-05-18 omar.polo Atom type;
596 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_WINDOW_TYPE_DOCK", false);
597 f5e234d6 2018-05-18 omar.polo XChangeProperty(
600 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_WINDOW_TYPE", false),
601 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
603 f5e234d6 2018-05-18 omar.polo PropModeReplace,
604 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
608 f5e234d6 2018-05-18 omar.polo /* some window managers honor this properties */
609 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_STATE_ABOVE", false);
610 f5e234d6 2018-05-18 omar.polo XChangeProperty(d,
612 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_STATE", false),
613 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
615 f5e234d6 2018-05-18 omar.polo PropModeReplace,
616 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
620 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_STATE_FOCUSED", false);
621 f5e234d6 2018-05-18 omar.polo XChangeProperty(d,
623 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_STATE", false),
624 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
626 f5e234d6 2018-05-18 omar.polo PropModeAppend,
627 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
631 f5e234d6 2018-05-18 omar.polo // setting window hints
632 f5e234d6 2018-05-18 omar.polo XClassHint *class_hint = XAllocClassHint();
633 f5e234d6 2018-05-18 omar.polo if (class_hint == nil) {
634 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not allocate memory for class hint\n");
635 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
637 7ef37ee0 2018-05-21 omar.polo class_hint->res_name = resname;
638 7ef37ee0 2018-05-21 omar.polo class_hint->res_class = resclass;
639 f5e234d6 2018-05-18 omar.polo XSetClassHint(d, w, class_hint);
640 f5e234d6 2018-05-18 omar.polo XFree(class_hint);
642 f5e234d6 2018-05-18 omar.polo XSizeHints *size_hint = XAllocSizeHints();
643 f5e234d6 2018-05-18 omar.polo if (size_hint == nil) {
644 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not allocate memory for size hint\n");
645 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
647 7ef37ee0 2018-05-21 omar.polo size_hint->flags = PMinSize | PBaseSize;
648 f5e234d6 2018-05-18 omar.polo size_hint->min_width = width;
649 f5e234d6 2018-05-18 omar.polo size_hint->base_width = width;
650 f5e234d6 2018-05-18 omar.polo size_hint->min_height = height;
651 f5e234d6 2018-05-18 omar.polo size_hint->base_height = height;
653 f5e234d6 2018-05-18 omar.polo XFlush(d);
656 8b929918 2018-07-01 omar.polo // write the width and height of the window `w' respectively in `width'
657 8b929918 2018-07-01 omar.polo // and `height'.
658 f5e234d6 2018-05-18 omar.polo void get_wh(Display *d, Window *w, int *width, int *height) {
659 f5e234d6 2018-05-18 omar.polo XWindowAttributes win_attr;
660 f5e234d6 2018-05-18 omar.polo XGetWindowAttributes(d, *w, &win_attr);
661 f5e234d6 2018-05-18 omar.polo *height = win_attr.height;
662 f5e234d6 2018-05-18 omar.polo *width = win_attr.width;
665 f5e234d6 2018-05-18 omar.polo // I know this may seem a little hackish BUT is the only way I managed
666 f5e234d6 2018-05-18 omar.polo // to actually grab that goddam keyboard. Only one call to
667 f5e234d6 2018-05-18 omar.polo // XGrabKeyboard does not always end up with the keyboard grabbed!
668 f5e234d6 2018-05-18 omar.polo int take_keyboard(Display *d, Window w) {
669 686e9237 2018-05-18 omar.polo int i;
670 686e9237 2018-05-18 omar.polo for (i = 0; i < 100; i++) {
671 686e9237 2018-05-18 omar.polo if (XGrabKeyboard(d, w, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
672 686e9237 2018-05-18 omar.polo return 1;
673 686e9237 2018-05-18 omar.polo usleep(1000);
675 686e9237 2018-05-18 omar.polo return 0;
678 2128b469 2018-06-30 omar.polo // release the keyboard.
679 f5e234d6 2018-05-18 omar.polo void release_keyboard(Display *d) {
680 686e9237 2018-05-18 omar.polo XUngrabKeyboard(d, CurrentTime);
683 8b929918 2018-07-01 omar.polo // Given a string, try to parse it as a number or return
684 8b929918 2018-07-01 omar.polo // `default_value'.
685 25bf99d2 2018-05-22 omar.polo int parse_integer(const char *str, int default_value) {
686 f5e234d6 2018-05-18 omar.polo errno = 0;
687 f5e234d6 2018-05-18 omar.polo char *ep;
688 f5e234d6 2018-05-18 omar.polo long lval = strtol(str, &ep, 10);
689 f5e234d6 2018-05-18 omar.polo if (str[0] == '\0' || *ep != '\0') { // NaN
690 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "'%s' is not a valid number! Using %d as default.\n", str, default_value);
691 f5e234d6 2018-05-18 omar.polo return default_value;
693 f5e234d6 2018-05-18 omar.polo if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
694 f5e234d6 2018-05-18 omar.polo (lval > INT_MAX || lval < INT_MIN)) {
695 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "%s out of range! Using %d as default.\n", str, default_value);
696 f5e234d6 2018-05-18 omar.polo return default_value;
698 f5e234d6 2018-05-18 omar.polo return lval;
701 2128b469 2018-06-30 omar.polo // like parse_integer, but if the value ends with a `%' then its
702 2128b469 2018-06-30 omar.polo // treated like a percentage (`max' is used to compute the percentage)
703 25bf99d2 2018-05-22 omar.polo int parse_int_with_percentage(const char *str, int default_value, int max) {
704 25bf99d2 2018-05-22 omar.polo int len = strlen(str);
705 25bf99d2 2018-05-22 omar.polo if (len > 0 && str[len-1] == '%') {
706 25bf99d2 2018-05-22 omar.polo char *cpy = strdup(str);
707 25bf99d2 2018-05-22 omar.polo check_allocation(cpy);
708 25bf99d2 2018-05-22 omar.polo cpy[len-1] = '\0';
709 25bf99d2 2018-05-22 omar.polo int val = parse_integer(cpy, default_value);
710 25bf99d2 2018-05-22 omar.polo free(cpy);
711 25bf99d2 2018-05-22 omar.polo return val * max / 100;
713 576ab141 2018-05-22 omar.polo return parse_integer(str, default_value);
716 8e122ff1 2018-07-13 omar.polo // like parse_int_with_percentage but understands some special values
717 8e122ff1 2018-07-13 omar.polo // - "middle" that is (max - self) / 2
718 8e122ff1 2018-07-13 omar.polo // - "start" that is 0
719 8e122ff1 2018-07-13 omar.polo // - "end" that is (max - self)
720 8e122ff1 2018-07-13 omar.polo int parse_int_with_pos(const char *str, int default_value, int max, int self) {
721 8e122ff1 2018-07-13 omar.polo if (!strcmp(str, "start"))
722 8e122ff1 2018-07-13 omar.polo return 0;
723 8e122ff1 2018-07-13 omar.polo if (!strcmp(str, "middle"))
724 f5e234d6 2018-05-18 omar.polo return (max - self)/2;
725 8e122ff1 2018-07-13 omar.polo if (!strcmp(str, "end"))
726 8e122ff1 2018-07-13 omar.polo return max-self;
727 25bf99d2 2018-05-22 omar.polo return parse_int_with_percentage(str, default_value, max);
730 42c3f269 2018-07-08 omar.polo // parse a string like a css value (for example like the css
731 42c3f269 2018-07-08 omar.polo // margin/padding properties). Will ALWAYS return an array of 4 word
732 42c3f269 2018-07-08 omar.polo // TODO: harden this function!
733 42c3f269 2018-07-08 omar.polo char **parse_csslike(const char *str) {
734 42c3f269 2018-07-08 omar.polo char *s = strdup(str);
735 42c3f269 2018-07-08 omar.polo if (s == nil)
736 42c3f269 2018-07-08 omar.polo return nil;
738 42c3f269 2018-07-08 omar.polo char **ret = malloc(4 * sizeof(char*));
739 42c3f269 2018-07-08 omar.polo if (ret == nil) {
740 42c3f269 2018-07-08 omar.polo free(s);
741 42c3f269 2018-07-08 omar.polo return nil;
744 42c3f269 2018-07-08 omar.polo int i = 0;
745 42c3f269 2018-07-08 omar.polo char *token;
746 42c3f269 2018-07-08 omar.polo while ((token = strsep(&s, " ")) != NULL && i < 4) {
747 42c3f269 2018-07-08 omar.polo ret[i] = strdup(token);
751 42c3f269 2018-07-08 omar.polo if (i == 1)
752 42c3f269 2018-07-08 omar.polo for (int j = 1; j < 4; j++)
753 42c3f269 2018-07-08 omar.polo ret[j] = strdup(ret[0]);
755 42c3f269 2018-07-08 omar.polo if (i == 2) {
756 42c3f269 2018-07-08 omar.polo ret[2] = strdup(ret[0]);
757 42c3f269 2018-07-08 omar.polo ret[3] = strdup(ret[1]);
760 42c3f269 2018-07-08 omar.polo if (i == 3)
761 42c3f269 2018-07-08 omar.polo ret[3] = strdup(ret[1]);
763 42c3f269 2018-07-08 omar.polo // Before we didn't check for the return type of strdup, here we will
765 42c3f269 2018-07-08 omar.polo bool any_null = false;
766 42c3f269 2018-07-08 omar.polo for (int i = 0; i < 4; ++i)
767 42c3f269 2018-07-08 omar.polo any_null = ret[i] == nil || any_null;
769 42c3f269 2018-07-08 omar.polo if (any_null)
770 42c3f269 2018-07-08 omar.polo for (int i = 0; i < 4; ++i)
771 42c3f269 2018-07-08 omar.polo if (ret[i] != nil)
772 42c3f269 2018-07-08 omar.polo free(ret[i]);
774 42c3f269 2018-07-08 omar.polo if (i == 0 || any_null) {
775 42c3f269 2018-07-08 omar.polo free(s);
776 42c3f269 2018-07-08 omar.polo free(ret);
777 42c3f269 2018-07-08 omar.polo return nil;
780 42c3f269 2018-07-08 omar.polo return ret;
783 2128b469 2018-06-30 omar.polo // Given an event, try to understand what the user wants. If the
784 2128b469 2018-06-30 omar.polo // return value is ADD_CHAR then `input' is a pointer to a string that
785 2128b469 2018-06-30 omar.polo // will need to be free'ed.
786 2128b469 2018-06-30 omar.polo enum action parse_event(Display *d, XKeyPressedEvent *ev, XIC xic, char **input) {
787 2128b469 2018-06-30 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_BackSpace))
788 2128b469 2018-06-30 omar.polo return DEL_CHAR;
790 2128b469 2018-06-30 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Tab))
791 2128b469 2018-06-30 omar.polo return ev->state & ShiftMask ? PREV_COMPL : NEXT_COMPL;
793 2128b469 2018-06-30 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Return))
794 2128b469 2018-06-30 omar.polo return CONFIRM;
796 2128b469 2018-06-30 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Escape))
797 2128b469 2018-06-30 omar.polo return EXIT;
799 2128b469 2018-06-30 omar.polo // try to read what the user pressed
800 b5d751bd 2018-07-07 omar.polo char str[SYM_BUF_SIZE] = {0};
801 2128b469 2018-06-30 omar.polo Status s = 0;
802 b5d751bd 2018-07-07 omar.polo Xutf8LookupString(xic, ev, str, SYM_BUF_SIZE, 0, &s);
803 2128b469 2018-06-30 omar.polo if (s == XBufferOverflow) {
804 2128b469 2018-06-30 omar.polo // should not happen since there are no utf-8 characters larger
805 2128b469 2018-06-30 omar.polo // than 24bits
806 2128b469 2018-06-30 omar.polo fprintf(stderr, "Buffer overflow when trying to create keyboard symbol map.\n");
807 2128b469 2018-06-30 omar.polo return EXIT;
810 2128b469 2018-06-30 omar.polo if (ev->state & ControlMask) {
811 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-u
812 2128b469 2018-06-30 omar.polo return DEL_LINE;
813 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-w
814 2128b469 2018-06-30 omar.polo return DEL_WORD;
815 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-h
816 2128b469 2018-06-30 omar.polo return DEL_CHAR;
817 2128b469 2018-06-30 omar.polo if (!strcmp(str, "\r")) // C-m
818 2128b469 2018-06-30 omar.polo return CONFIRM;
819 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-p
820 2128b469 2018-06-30 omar.polo return PREV_COMPL;
821 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-n
822 2128b469 2018-06-30 omar.polo return NEXT_COMPL;
823 bee0837c 2018-07-01 omar.polo if (!strcmp(str, "")) // C-c
824 bee0837c 2018-07-01 omar.polo return EXIT;
825 6254fed8 2018-07-06 omar.polo if (!strcmp(str, "\t")) // C-i
826 6254fed8 2018-07-06 omar.polo return TOGGLE_FIRST_SELECTED;
829 b5d751bd 2018-07-07 omar.polo *input = strdup(str);
830 2128b469 2018-06-30 omar.polo if (*input == nil) {
831 2128b469 2018-06-30 omar.polo fprintf(stderr, "Error while allocating memory for key.\n");
832 2128b469 2018-06-30 omar.polo return EXIT;
835 2128b469 2018-06-30 omar.polo return ADD_CHAR;
838 b5d751bd 2018-07-07 omar.polo // Given the name of the program (argv[0]?) print a small help on stderr
839 b5d751bd 2018-07-07 omar.polo void usage(char *prgname) {
840 b5d751bd 2018-07-07 omar.polo fprintf(stderr, "Usage: %s [flags]\n", prgname);
841 b5d751bd 2018-07-07 omar.polo fprintf(stderr, "\t-a: automatic mode, the first completion is "
842 b5d751bd 2018-07-07 omar.polo "always selected;\n");
843 b5d751bd 2018-07-07 omar.polo fprintf(stderr, "\t-h: print this help.\n");
846 e9f0b467 2018-06-07 omar.polo int main(int argc, char **argv) {
847 1ef91f4e 2018-07-03 omar.polo #ifdef HAVE_PLEDGE
848 1ef91f4e 2018-07-03 omar.polo // stdio & rpat: to read and write stdio/stdout
849 1ef91f4e 2018-07-03 omar.polo // unix: to connect to Xorg
850 1ef91f4e 2018-07-03 omar.polo pledge("stdio rpath unix", "");
851 1ef91f4e 2018-07-03 omar.polo #endif
853 e9f0b467 2018-06-07 omar.polo // by default the first completion isn't selected
854 e9f0b467 2018-06-07 omar.polo bool first_selected = false;
856 e9f0b467 2018-06-07 omar.polo // parse the command line options
857 e9f0b467 2018-06-07 omar.polo int ch;
858 b500fe86 2018-06-07 omar.polo while ((ch = getopt(argc, argv, "ahv")) != -1) {
859 e9f0b467 2018-06-07 omar.polo switch (ch) {
860 449f27a6 2018-07-06 omar.polo case 'a':
861 b10f01c1 2018-07-06 omar.polo first_selected = true;
862 b10f01c1 2018-07-06 omar.polo break;
863 449f27a6 2018-07-06 omar.polo case 'h':
864 b10f01c1 2018-07-06 omar.polo usage(*argv);
865 b10f01c1 2018-07-06 omar.polo return 0;
866 449f27a6 2018-07-06 omar.polo case 'v':
867 b10f01c1 2018-07-06 omar.polo fprintf(stderr, "%s version: %s\n", *argv, VERSION);
868 b10f01c1 2018-07-06 omar.polo return 0;
869 449f27a6 2018-07-06 omar.polo default:
870 b10f01c1 2018-07-06 omar.polo usage(*argv);
871 b10f01c1 2018-07-06 omar.polo return EX_USAGE;
875 95b27a5e 2018-05-19 omar.polo char **lines = calloc(INITIAL_ITEMS, sizeof(char*));
876 e9f0b467 2018-06-07 omar.polo readlines(&lines, INITIAL_ITEMS);
878 f5e234d6 2018-05-18 omar.polo setlocale(LC_ALL, getenv("LANG"));
880 f5e234d6 2018-05-18 omar.polo enum state status = LOOPING;
882 f5e234d6 2018-05-18 omar.polo // where the monitor start (used only with xinerama)
883 f5e234d6 2018-05-18 omar.polo int offset_x = 0;
884 f5e234d6 2018-05-18 omar.polo int offset_y = 0;
886 f5e234d6 2018-05-18 omar.polo // width and height of the window
887 f5e234d6 2018-05-18 omar.polo int width = 400;
888 f5e234d6 2018-05-18 omar.polo int height = 20;
890 f5e234d6 2018-05-18 omar.polo // position on the screen
891 f5e234d6 2018-05-18 omar.polo int x = 0;
892 f5e234d6 2018-05-18 omar.polo int y = 0;
894 e9f0b467 2018-06-07 omar.polo // the default padding
895 e5186d6b 2018-05-26 omar.polo int padding = 10;
897 42c3f269 2018-07-08 omar.polo // the default borders
898 42c3f269 2018-07-08 omar.polo int border_n = 0;
899 42c3f269 2018-07-08 omar.polo int border_e = 0;
900 42c3f269 2018-07-08 omar.polo int border_s = 0;
901 42c3f269 2018-07-08 omar.polo int border_w = 0;
903 42c3f269 2018-07-08 omar.polo // the prompt. We duplicate the string so later is easy to free (in
904 42c3f269 2018-07-08 omar.polo // the case the user provide its own prompt)
905 8758854a 2018-05-20 omar.polo char *ps1 = strdup("$ ");
906 8758854a 2018-05-20 omar.polo check_allocation(ps1);
908 42c3f269 2018-07-08 omar.polo // same for the font name
909 9e94fcbe 2018-05-22 omar.polo char *fontname = strdup(default_fontname);
910 f5e234d6 2018-05-18 omar.polo check_allocation(fontname);
912 f5e234d6 2018-05-18 omar.polo int textlen = 10;
913 f5e234d6 2018-05-18 omar.polo char *text = malloc(textlen * sizeof(char));
914 f5e234d6 2018-05-18 omar.polo check_allocation(text);
916 e9f0b467 2018-06-07 omar.polo /* struct completions *cs = filter(text, lines); */
917 b5d751bd 2018-07-07 omar.polo struct completions *cs = compls_new();
918 b5d751bd 2018-07-07 omar.polo check_allocation(cs);
920 b5d751bd 2018-07-07 omar.polo update_completions(cs, text, lines, first_selected);
922 f5e234d6 2018-05-18 omar.polo // start talking to xorg
923 f5e234d6 2018-05-18 omar.polo Display *d = XOpenDisplay(nil);
924 f5e234d6 2018-05-18 omar.polo if (d == nil) {
925 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not open display!\n");
926 f5e234d6 2018-05-18 omar.polo return EX_UNAVAILABLE;
929 f5e234d6 2018-05-18 omar.polo // get display size
930 f5e234d6 2018-05-18 omar.polo // XXX: is getting the default root window dimension correct?
931 f5e234d6 2018-05-18 omar.polo XWindowAttributes xwa;
932 f5e234d6 2018-05-18 omar.polo XGetWindowAttributes(d, DefaultRootWindow(d), &xwa);
933 f5e234d6 2018-05-18 omar.polo int d_width = xwa.width;
934 f5e234d6 2018-05-18 omar.polo int d_height = xwa.height;
936 f5e234d6 2018-05-18 omar.polo #ifdef USE_XINERAMA
937 f5e234d6 2018-05-18 omar.polo if (XineramaIsActive(d)) {
938 7ca8829b 2018-05-21 omar.polo // find the mice
939 7ca8829b 2018-05-21 omar.polo int number_of_screens = XScreenCount(d);
940 7ca8829b 2018-05-21 omar.polo Window r;
941 7ca8829b 2018-05-21 omar.polo Window root;
942 7ca8829b 2018-05-21 omar.polo int root_x, root_y, win_x, win_y;
943 7ca8829b 2018-05-21 omar.polo unsigned int mask;
944 7ca8829b 2018-05-21 omar.polo bool res;
945 7ca8829b 2018-05-21 omar.polo for (int i = 0; i < number_of_screens; ++i) {
946 7ca8829b 2018-05-21 omar.polo root = XRootWindow(d, i);
947 7ca8829b 2018-05-21 omar.polo res = XQueryPointer(d, root, &r, &r, &root_x, &root_y, &win_x, &win_y, &mask);
948 7ca8829b 2018-05-21 omar.polo if (res) break;
950 7ca8829b 2018-05-21 omar.polo if (!res) {
951 7ca8829b 2018-05-21 omar.polo fprintf(stderr, "No mouse found.\n");
952 7ca8829b 2018-05-21 omar.polo root_x = 0;
953 7ca8829b 2018-05-21 omar.polo root_y = 0;
956 7ca8829b 2018-05-21 omar.polo // now find in which monitor the mice is on
957 f5e234d6 2018-05-18 omar.polo int monitors;
958 f5e234d6 2018-05-18 omar.polo XineramaScreenInfo *info = XineramaQueryScreens(d, &monitors);
959 7ca8829b 2018-05-21 omar.polo if (info) {
960 f5e234d6 2018-05-18 omar.polo for (int i = 0; i < monitors; ++i) {
961 7ca8829b 2018-05-21 omar.polo if (info[i].x_org <= root_x && root_x <= (info[i].x_org + info[i].width)
962 7ca8829b 2018-05-21 omar.polo && info[i].y_org <= root_y && root_y <= (info[i].y_org + info[i].height)) {
963 7ca8829b 2018-05-21 omar.polo offset_x = info[i].x_org;
964 7ca8829b 2018-05-21 omar.polo offset_y = info[i].y_org;
965 f5e234d6 2018-05-18 omar.polo d_width = info[i].width;
966 f5e234d6 2018-05-18 omar.polo d_height = info[i].height;
967 7ca8829b 2018-05-21 omar.polo break;
971 f5e234d6 2018-05-18 omar.polo XFree(info);
973 f5e234d6 2018-05-18 omar.polo #endif
975 f5e234d6 2018-05-18 omar.polo Colormap cmap = DefaultColormap(d, DefaultScreen(d));
976 f5e234d6 2018-05-18 omar.polo XColor p_fg, p_bg,
977 f5e234d6 2018-05-18 omar.polo compl_fg, compl_bg,
978 42c3f269 2018-07-08 omar.polo compl_highlighted_fg, compl_highlighted_bg,
979 42c3f269 2018-07-08 omar.polo border_n_bg, border_e_bg, border_s_bg, border_w_bg;
981 36a15a9f 2018-05-19 omar.polo bool horizontal_layout = true;
983 f5e234d6 2018-05-18 omar.polo // read resource
984 f5e234d6 2018-05-18 omar.polo XrmInitialize();
985 f5e234d6 2018-05-18 omar.polo char *xrm = XResourceManagerString(d);
986 347d23da 2018-05-19 omar.polo XrmDatabase xdb = nil;
987 f5e234d6 2018-05-18 omar.polo if (xrm != nil) {
988 347d23da 2018-05-19 omar.polo xdb = XrmGetStringDatabase(xrm);
989 f5e234d6 2018-05-18 omar.polo XrmValue value;
990 f5e234d6 2018-05-18 omar.polo char *datatype[20];
992 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.font", "*", datatype, &value) == true) {
993 f5e234d6 2018-05-18 omar.polo fontname = strdup(value.addr);
994 f5e234d6 2018-05-18 omar.polo check_allocation(fontname);
997 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no font defined, using %s\n", fontname);
999 36a15a9f 2018-05-19 omar.polo if (XrmGetResource(xdb, "MyMenu.layout", "*", datatype, &value) == true) {
1000 36a15a9f 2018-05-19 omar.polo char *v = strdup(value.addr);
1001 36a15a9f 2018-05-19 omar.polo check_allocation(v);
1002 36a15a9f 2018-05-19 omar.polo horizontal_layout = !strcmp(v, "horizontal");
1003 36a15a9f 2018-05-19 omar.polo free(v);
1006 36a15a9f 2018-05-19 omar.polo fprintf(stderr, "no layout defined, using horizontal\n");
1008 8758854a 2018-05-20 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt", "*", datatype, &value) == true) {
1009 8758854a 2018-05-20 omar.polo free(ps1);
1010 8758854a 2018-05-20 omar.polo ps1 = normalize_str(value.addr);
1011 8758854a 2018-05-20 omar.polo } else
1012 8758854a 2018-05-20 omar.polo fprintf(stderr, "no prompt defined, using \"%s\" as default\n", ps1);
1014 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.width", "*", datatype, &value) == true)
1015 25bf99d2 2018-05-22 omar.polo width = parse_int_with_percentage(value.addr, width, d_width);
1017 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no width defined, using %d\n", width);
1019 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.height", "*", datatype, &value) == true)
1020 25bf99d2 2018-05-22 omar.polo height = parse_int_with_percentage(value.addr, height, d_height);
1022 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no height defined, using %d\n", height);
1024 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.x", "*", datatype, &value) == true)
1025 8e122ff1 2018-07-13 omar.polo x = parse_int_with_pos(value.addr, x, d_width, width);
1027 e5186d6b 2018-05-26 omar.polo fprintf(stderr, "no x defined, using %d\n", x);
1029 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.y", "*", datatype, &value) == true)
1030 8e122ff1 2018-07-13 omar.polo y = parse_int_with_pos(value.addr, y, d_height, height);
1032 e5186d6b 2018-05-26 omar.polo fprintf(stderr, "no y defined, using %d\n", y);
1034 e5186d6b 2018-05-26 omar.polo if (XrmGetResource(xdb, "MyMenu.padding", "*", datatype, &value) == true)
1035 e5186d6b 2018-05-26 omar.polo padding = parse_integer(value.addr, padding);
1037 26b1f485 2018-07-03 omar.polo fprintf(stderr, "no padding defined, using %d\n", padding);
1039 42c3f269 2018-07-08 omar.polo if (XrmGetResource(xdb, "MyMenu.border.size", "*", datatype, &value) == true) {
1040 42c3f269 2018-07-08 omar.polo char **borders = parse_csslike(value.addr);
1041 42c3f269 2018-07-08 omar.polo if (borders != nil) {
1042 42c3f269 2018-07-08 omar.polo border_n = parse_integer(borders[0], 0);
1043 42c3f269 2018-07-08 omar.polo border_e = parse_integer(borders[1], 0);
1044 42c3f269 2018-07-08 omar.polo border_s = parse_integer(borders[2], 0);
1045 42c3f269 2018-07-08 omar.polo border_w = parse_integer(borders[3], 0);
1046 42c3f269 2018-07-08 omar.polo } else {
1047 42c3f269 2018-07-08 omar.polo fprintf(stderr, "error while parsing MyMenu.border.size\n");
1049 42c3f269 2018-07-08 omar.polo } else {
1050 42c3f269 2018-07-08 omar.polo fprintf(stderr, "no border defined, using 0.\n");
1053 f5e234d6 2018-05-18 omar.polo XColor tmp;
1054 f5e234d6 2018-05-18 omar.polo // TODO: tmp needs to be free'd after every allocation?
1056 f5e234d6 2018-05-18 omar.polo // prompt
1057 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.foreground", "*", datatype, &value) == true)
1058 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &p_fg, &tmp);
1060 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &p_fg, &tmp);
1062 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.background", "*", datatype, &value) == true)
1063 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &p_bg, &tmp);
1065 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &p_bg, &tmp);
1067 f5e234d6 2018-05-18 omar.polo // completion
1068 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.foreground", "*", datatype, &value) == true)
1069 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_fg, &tmp);
1071 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_fg, &tmp);
1073 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.background", "*", datatype, &value) == true)
1074 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_bg, &tmp);
1076 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_bg, &tmp);
1078 f5e234d6 2018-05-18 omar.polo // completion highlighted
1079 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion_highlighted.foreground", "*", datatype, &value) == true)
1080 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_highlighted_fg, &tmp);
1082 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_highlighted_fg, &tmp);
1084 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion_highlighted.background", "*", datatype, &value) == true)
1085 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_highlighted_bg, &tmp);
1087 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_highlighted_bg, &tmp);
1089 42c3f269 2018-07-08 omar.polo // border
1090 42c3f269 2018-07-08 omar.polo if (XrmGetResource(xdb, "MyMenu.border.color", "*", datatype, &value) == true) {
1091 42c3f269 2018-07-08 omar.polo char **colors = parse_csslike(value.addr);
1092 42c3f269 2018-07-08 omar.polo if (colors != nil) {
1093 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, colors[0], &border_n_bg, &tmp);
1094 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, colors[1], &border_e_bg, &tmp);
1095 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, colors[2], &border_s_bg, &tmp);
1096 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, colors[3], &border_w_bg, &tmp);
1097 42c3f269 2018-07-08 omar.polo } else {
1098 42c3f269 2018-07-08 omar.polo fprintf(stderr, "error while parsing MyMenu.border.size\n");
1100 42c3f269 2018-07-08 omar.polo } else {
1101 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, "white", &border_n_bg, &tmp);
1102 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, "white", &border_e_bg, &tmp);
1103 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, "white", &border_s_bg, &tmp);
1104 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, "white", &border_w_bg, &tmp);
1106 f5e234d6 2018-05-18 omar.polo } else {
1107 f5e234d6 2018-05-18 omar.polo XColor tmp;
1108 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &p_fg, &tmp);
1109 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &p_bg, &tmp);
1110 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_fg, &tmp);
1111 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_bg, &tmp);
1112 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_highlighted_fg, &tmp);
1113 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, "white", &border_n_bg, &tmp);
1114 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, "white", &border_e_bg, &tmp);
1115 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, "white", &border_s_bg, &tmp);
1116 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, "white", &border_w_bg, &tmp);
1119 f5e234d6 2018-05-18 omar.polo // load the font
1120 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
1121 c9a3bfaa 2018-05-21 omar.polo XftFont *font = XftFontOpenName(d, DefaultScreen(d), fontname);
1122 c9a3bfaa 2018-05-21 omar.polo #else
1123 347d23da 2018-05-19 omar.polo char **missing_charset_list;
1124 347d23da 2018-05-19 omar.polo int missing_charset_count;
1125 347d23da 2018-05-19 omar.polo XFontSet font = XCreateFontSet(d, fontname, &missing_charset_list, &missing_charset_count, nil);
1126 f5e234d6 2018-05-18 omar.polo if (font == nil) {
1127 347d23da 2018-05-19 omar.polo fprintf(stderr, "Unable to load the font(s) %s\n", fontname);
1128 347d23da 2018-05-19 omar.polo return EX_UNAVAILABLE;
1130 c9a3bfaa 2018-05-21 omar.polo #endif
1132 f5e234d6 2018-05-18 omar.polo // create the window
1133 f5e234d6 2018-05-18 omar.polo XSetWindowAttributes attr;
1134 55bd6842 2018-05-21 omar.polo attr.override_redirect = true;
1136 f5e234d6 2018-05-18 omar.polo Window w = XCreateWindow(d, // display
1137 f5e234d6 2018-05-18 omar.polo DefaultRootWindow(d), // parent
1138 f5e234d6 2018-05-18 omar.polo x + offset_x, y + offset_y, // x y
1139 f5e234d6 2018-05-18 omar.polo width, height, // w h
1140 f5e234d6 2018-05-18 omar.polo 0, // border width
1141 f5e234d6 2018-05-18 omar.polo DefaultDepth(d, DefaultScreen(d)), // depth
1142 f5e234d6 2018-05-18 omar.polo InputOutput, // class
1143 f5e234d6 2018-05-18 omar.polo DefaultVisual(d, DefaultScreen(d)), // visual
1144 55bd6842 2018-05-21 omar.polo CWOverrideRedirect, // value mask
1145 f5e234d6 2018-05-18 omar.polo &attr);
1147 f5e234d6 2018-05-18 omar.polo set_win_atoms_hints(d, w, width, height);
1149 f5e234d6 2018-05-18 omar.polo // we want some events
1150 c9a3bfaa 2018-05-21 omar.polo XSelectInput(d, w, StructureNotifyMask | KeyPressMask | KeymapStateMask);
1152 f5e234d6 2018-05-18 omar.polo // make the window appear on the screen
1153 f5e234d6 2018-05-18 omar.polo XMapWindow(d, w);
1155 f5e234d6 2018-05-18 omar.polo // wait for the MapNotify event (i.e. the event "window rendered")
1156 f5e234d6 2018-05-18 omar.polo for (;;) {
1157 f5e234d6 2018-05-18 omar.polo XEvent e;
1158 f5e234d6 2018-05-18 omar.polo XNextEvent(d, &e);
1159 f5e234d6 2018-05-18 omar.polo if (e.type == MapNotify)
1160 f5e234d6 2018-05-18 omar.polo break;
1163 f5e234d6 2018-05-18 omar.polo // get the *real* width & height after the window was rendered
1164 f5e234d6 2018-05-18 omar.polo get_wh(d, &w, &width, &height);
1166 f5e234d6 2018-05-18 omar.polo // grab keyboard
1167 f5e234d6 2018-05-18 omar.polo take_keyboard(d, w);
1169 f5e234d6 2018-05-18 omar.polo // Create some graphics contexts
1170 f5e234d6 2018-05-18 omar.polo XGCValues values;
1171 347d23da 2018-05-19 omar.polo /* values.font = font->fid; */
1173 f5e234d6 2018-05-18 omar.polo struct rendering r = {
1174 f5e234d6 2018-05-18 omar.polo .d = d,
1175 f5e234d6 2018-05-18 omar.polo .w = w,
1176 42c3f269 2018-07-08 omar.polo .width = width,
1177 42c3f269 2018-07-08 omar.polo .height = height,
1178 42c3f269 2018-07-08 omar.polo .padding = padding,
1179 42c3f269 2018-07-08 omar.polo .x_zero = border_w,
1180 42c3f269 2018-07-08 omar.polo .y_zero = border_n,
1181 42c3f269 2018-07-08 omar.polo .border_n = border_n,
1182 42c3f269 2018-07-08 omar.polo .border_e = border_e,
1183 42c3f269 2018-07-08 omar.polo .border_s = border_s,
1184 42c3f269 2018-07-08 omar.polo .border_w = border_w,
1185 42c3f269 2018-07-08 omar.polo .horizontal_layout = horizontal_layout,
1186 42c3f269 2018-07-08 omar.polo .ps1 = ps1,
1187 42c3f269 2018-07-08 omar.polo .ps1len = strlen(ps1),
1188 347d23da 2018-05-19 omar.polo .prompt = XCreateGC(d, w, 0, &values),
1189 347d23da 2018-05-19 omar.polo .prompt_bg = XCreateGC(d, w, 0, &values),
1190 347d23da 2018-05-19 omar.polo .completion = XCreateGC(d, w, 0, &values),
1191 347d23da 2018-05-19 omar.polo .completion_bg = XCreateGC(d, w, 0, &values),
1192 347d23da 2018-05-19 omar.polo .completion_highlighted = XCreateGC(d, w, 0, &values),
1193 347d23da 2018-05-19 omar.polo .completion_highlighted_bg = XCreateGC(d, w, 0, &values),
1194 42c3f269 2018-07-08 omar.polo .border_n_bg = XCreateGC(d, w, 0, &values),
1195 42c3f269 2018-07-08 omar.polo .border_e_bg = XCreateGC(d, w, 0, &values),
1196 42c3f269 2018-07-08 omar.polo .border_s_bg = XCreateGC(d, w, 0, &values),
1197 42c3f269 2018-07-08 omar.polo .border_w_bg = XCreateGC(d, w, 0, &values),
1198 42c3f269 2018-07-08 omar.polo #ifdef USE_XFT
1199 42c3f269 2018-07-08 omar.polo .font = font,
1200 42c3f269 2018-07-08 omar.polo #else
1201 42c3f269 2018-07-08 omar.polo .font = &font,
1202 42c3f269 2018-07-08 omar.polo #endif
1205 7ca8829b 2018-05-21 omar.polo #ifdef USE_XFT
1206 7ca8829b 2018-05-21 omar.polo r.xftdraw = XftDrawCreate(d, w, DefaultVisual(d, 0), DefaultColormap(d, 0));
1208 7ca8829b 2018-05-21 omar.polo // prompt
1209 7ca8829b 2018-05-21 omar.polo XRenderColor xrcolor;
1210 7ca8829b 2018-05-21 omar.polo xrcolor.red = p_fg.red;
1211 7ca8829b 2018-05-21 omar.polo xrcolor.green = p_fg.red;
1212 7ca8829b 2018-05-21 omar.polo xrcolor.blue = p_fg.red;
1213 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
1214 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_prompt);
1216 7ca8829b 2018-05-21 omar.polo // completion
1217 7ca8829b 2018-05-21 omar.polo xrcolor.red = compl_fg.red;
1218 7ca8829b 2018-05-21 omar.polo xrcolor.green = compl_fg.green;
1219 7ca8829b 2018-05-21 omar.polo xrcolor.blue = compl_fg.blue;
1220 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
1221 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_completion);
1223 7ca8829b 2018-05-21 omar.polo // completion highlighted
1224 7ca8829b 2018-05-21 omar.polo xrcolor.red = compl_highlighted_fg.red;
1225 7ca8829b 2018-05-21 omar.polo xrcolor.green = compl_highlighted_fg.green;
1226 7ca8829b 2018-05-21 omar.polo xrcolor.blue = compl_highlighted_fg.blue;
1227 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
1228 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_completion_highlighted);
1229 c9a3bfaa 2018-05-21 omar.polo #endif
1231 f5e234d6 2018-05-18 omar.polo // load the colors in our GCs
1232 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.prompt, p_fg.pixel);
1233 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.prompt_bg, p_bg.pixel);
1234 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion, compl_fg.pixel);
1235 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_bg, compl_bg.pixel);
1236 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_highlighted, compl_highlighted_fg.pixel);
1237 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_highlighted_bg, compl_highlighted_bg.pixel);
1238 42c3f269 2018-07-08 omar.polo XSetForeground(d, r.border_n_bg, border_n_bg.pixel);
1239 42c3f269 2018-07-08 omar.polo XSetForeground(d, r.border_e_bg, border_e_bg.pixel);
1240 42c3f269 2018-07-08 omar.polo XSetForeground(d, r.border_s_bg, border_s_bg.pixel);
1241 42c3f269 2018-07-08 omar.polo XSetForeground(d, r.border_w_bg, border_w_bg.pixel);
1243 347d23da 2018-05-19 omar.polo // open the X input method
1244 347d23da 2018-05-19 omar.polo XIM xim = XOpenIM(d, xdb, resname, resclass);
1245 347d23da 2018-05-19 omar.polo check_allocation(xim);
1247 347d23da 2018-05-19 omar.polo XIMStyles *xis = nil;
1248 347d23da 2018-05-19 omar.polo if (XGetIMValues(xim, XNQueryInputStyle, &xis, NULL) || !xis) {
1249 347d23da 2018-05-19 omar.polo fprintf(stderr, "Input Styles could not be retrieved\n");
1250 347d23da 2018-05-19 omar.polo return EX_UNAVAILABLE;
1253 347d23da 2018-05-19 omar.polo XIMStyle bestMatchStyle = 0;
1254 347d23da 2018-05-19 omar.polo for (int i = 0; i < xis->count_styles; ++i) {
1255 347d23da 2018-05-19 omar.polo XIMStyle ts = xis->supported_styles[i];
1256 347d23da 2018-05-19 omar.polo if (ts == (XIMPreeditNothing | XIMStatusNothing)) {
1257 347d23da 2018-05-19 omar.polo bestMatchStyle = ts;
1258 347d23da 2018-05-19 omar.polo break;
1261 347d23da 2018-05-19 omar.polo XFree(xis);
1263 347d23da 2018-05-19 omar.polo if (!bestMatchStyle) {
1264 347d23da 2018-05-19 omar.polo fprintf(stderr, "No matching input style could be determined\n");
1267 347d23da 2018-05-19 omar.polo XIC xic = XCreateIC(xim, XNInputStyle, bestMatchStyle, XNClientWindow, w, XNFocusWindow, w, NULL);
1268 347d23da 2018-05-19 omar.polo check_allocation(xic);
1270 f5e234d6 2018-05-18 omar.polo // draw the window for the first time
1271 f5e234d6 2018-05-18 omar.polo draw(&r, text, cs);
1273 f5e234d6 2018-05-18 omar.polo // main loop
1274 f5e234d6 2018-05-18 omar.polo while (status == LOOPING) {
1275 f5e234d6 2018-05-18 omar.polo XEvent e;
1276 f5e234d6 2018-05-18 omar.polo XNextEvent(d, &e);
1278 347d23da 2018-05-19 omar.polo if (XFilterEvent(&e, w))
1279 347d23da 2018-05-19 omar.polo continue;
1281 f5e234d6 2018-05-18 omar.polo switch (e.type) {
1282 449f27a6 2018-07-06 omar.polo case KeymapNotify:
1283 b10f01c1 2018-07-06 omar.polo XRefreshKeyboardMapping(&e.xmapping);
1284 b10f01c1 2018-07-06 omar.polo break;
1286 449f27a6 2018-07-06 omar.polo case KeyPress: {
1287 b10f01c1 2018-07-06 omar.polo XKeyPressedEvent *ev = (XKeyPressedEvent*)&e;
1289 b10f01c1 2018-07-06 omar.polo char *input;
1290 b10f01c1 2018-07-06 omar.polo switch (parse_event(d, ev, xic, &input)) {
1291 b10f01c1 2018-07-06 omar.polo case EXIT:
1292 b10f01c1 2018-07-06 omar.polo status = ERR;
1293 b10f01c1 2018-07-06 omar.polo break;
1295 b10f01c1 2018-07-06 omar.polo case CONFIRM:
1296 b10f01c1 2018-07-06 omar.polo status = OK;
1298 b10f01c1 2018-07-06 omar.polo // if first_selected is active and the first completion is
1299 b10f01c1 2018-07-06 omar.polo // active be sure to 'expand' the text to match the selection
1300 b5d751bd 2018-07-07 omar.polo if (first_selected && cs && cs->selected == 0) {
1301 b10f01c1 2018-07-06 omar.polo free(text);
1302 b5d751bd 2018-07-07 omar.polo text = strdup(cs->completions->completion);
1303 b10f01c1 2018-07-06 omar.polo if (text == nil) {
1304 b10f01c1 2018-07-06 omar.polo fprintf(stderr, "Memory allocation error");
1305 b10f01c1 2018-07-06 omar.polo status = ERR;
1307 b10f01c1 2018-07-06 omar.polo textlen = strlen(text);
1309 b10f01c1 2018-07-06 omar.polo break;
1311 b10f01c1 2018-07-06 omar.polo case PREV_COMPL: {
1312 b10f01c1 2018-07-06 omar.polo complete(cs, first_selected, true, &text, &textlen, &status);
1313 b10f01c1 2018-07-06 omar.polo break;
1316 b10f01c1 2018-07-06 omar.polo case NEXT_COMPL: {
1317 b10f01c1 2018-07-06 omar.polo complete(cs, first_selected, false, &text, &textlen, &status);
1318 b10f01c1 2018-07-06 omar.polo break;
1321 b10f01c1 2018-07-06 omar.polo case DEL_CHAR:
1322 b10f01c1 2018-07-06 omar.polo popc(text, textlen);
1323 b5d751bd 2018-07-07 omar.polo update_completions(cs, text, lines, first_selected);
1324 b10f01c1 2018-07-06 omar.polo break;
1326 b10f01c1 2018-07-06 omar.polo case DEL_WORD: {
1327 b10f01c1 2018-07-06 omar.polo // `textlen` is the lenght of the allocated string, not the
1328 b10f01c1 2018-07-06 omar.polo // lenght of the ACTUAL string
1329 b10f01c1 2018-07-06 omar.polo int p = strlen(text) -1;
1330 b10f01c1 2018-07-06 omar.polo if (p > 0) { // delete the current char
1331 b10f01c1 2018-07-06 omar.polo text[p] = 0;
1335 b10f01c1 2018-07-06 omar.polo // erase the alphanumeric char
1336 b10f01c1 2018-07-06 omar.polo while (p >= 0 && isalnum(text[p])) {
1337 b10f01c1 2018-07-06 omar.polo text[p] = 0;
1341 b10f01c1 2018-07-06 omar.polo // erase also trailing white spaces
1342 b10f01c1 2018-07-06 omar.polo while (p >= 0 && isspace(text[p])) {
1343 b10f01c1 2018-07-06 omar.polo text[p] = 0;
1346 b5d751bd 2018-07-07 omar.polo update_completions(cs, text, lines, first_selected);
1347 b10f01c1 2018-07-06 omar.polo break;
1350 b10f01c1 2018-07-06 omar.polo case DEL_LINE: {
1351 b10f01c1 2018-07-06 omar.polo for (int i = 0; i < textlen; ++i)
1352 b10f01c1 2018-07-06 omar.polo text[i] = 0;
1353 b5d751bd 2018-07-07 omar.polo update_completions(cs, text, lines, first_selected);
1354 b10f01c1 2018-07-06 omar.polo break;
1357 b10f01c1 2018-07-06 omar.polo case ADD_CHAR: {
1358 b10f01c1 2018-07-06 omar.polo int str_len = strlen(input);
1360 b5d751bd 2018-07-07 omar.polo // sometimes a strange key is pressed (i.e. ctrl alone),
1361 b5d751bd 2018-07-07 omar.polo // so input will be empty. Don't need to update completion
1362 b5d751bd 2018-07-07 omar.polo // in this case
1363 b5d751bd 2018-07-07 omar.polo if (str_len == 0)
1364 b5d751bd 2018-07-07 omar.polo break;
1366 b10f01c1 2018-07-06 omar.polo for (int i = 0; i < str_len; ++i) {
1367 b10f01c1 2018-07-06 omar.polo textlen = pushc(&text, textlen, input[i]);
1368 b10f01c1 2018-07-06 omar.polo if (textlen == -1) {
1369 b10f01c1 2018-07-06 omar.polo fprintf(stderr, "Memory allocation error\n");
1370 b10f01c1 2018-07-06 omar.polo status = ERR;
1371 b10f01c1 2018-07-06 omar.polo break;
1374 b5d751bd 2018-07-07 omar.polo if (status != ERR) {
1375 b5d751bd 2018-07-07 omar.polo update_completions(cs, text, lines, first_selected);
1376 b10f01c1 2018-07-06 omar.polo free(input);
1378 b10f01c1 2018-07-06 omar.polo break;
1381 6254fed8 2018-07-06 omar.polo case TOGGLE_FIRST_SELECTED:
1382 6254fed8 2018-07-06 omar.polo first_selected = !first_selected;
1383 b5d751bd 2018-07-07 omar.polo if (first_selected && cs->selected < 0)
1384 b5d751bd 2018-07-07 omar.polo cs->selected = 0;
1385 b5d751bd 2018-07-07 omar.polo if (!first_selected && cs->selected == 0)
1386 b5d751bd 2018-07-07 omar.polo cs->selected = -1;
1387 6254fed8 2018-07-06 omar.polo break;
1392 14291de3 2018-07-03 omar.polo draw(&r, text, cs);
1395 f5e234d6 2018-05-18 omar.polo if (status == OK)
1396 f5e234d6 2018-05-18 omar.polo printf("%s\n", text);
1398 b5d751bd 2018-07-07 omar.polo release_keyboard(r.d);
1400 b5d751bd 2018-07-07 omar.polo #ifdef USE_XFT
1401 b5d751bd 2018-07-07 omar.polo XftColorFree(r.d, DefaultVisual(r.d, 0), DefaultColormap(r.d, 0), &r.xft_prompt);
1402 b5d751bd 2018-07-07 omar.polo XftColorFree(r.d, DefaultVisual(r.d, 0), DefaultColormap(r.d, 0), &r.xft_completion);
1403 b5d751bd 2018-07-07 omar.polo XftColorFree(r.d, DefaultVisual(r.d, 0), DefaultColormap(r.d, 0), &r.xft_completion_highlighted);
1404 b5d751bd 2018-07-07 omar.polo #endif
1406 b5d751bd 2018-07-07 omar.polo free(ps1);
1407 b5d751bd 2018-07-07 omar.polo free(fontname);
1408 b5d751bd 2018-07-07 omar.polo free(text);
1410 b5d751bd 2018-07-07 omar.polo char *l = nil;
1411 b5d751bd 2018-07-07 omar.polo char **lns = lines;
1412 b5d751bd 2018-07-07 omar.polo while ((l = *lns) != nil) {
1413 b5d751bd 2018-07-07 omar.polo free(l);
1414 b5d751bd 2018-07-07 omar.polo ++lns;
1417 b5d751bd 2018-07-07 omar.polo free(lines);
1418 b5d751bd 2018-07-07 omar.polo compls_delete(cs);
1420 b5d751bd 2018-07-07 omar.polo XDestroyWindow(r.d, r.w);
1421 b5d751bd 2018-07-07 omar.polo XCloseDisplay(r.d);
1423 b5d751bd 2018-07-07 omar.polo return status;