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 9e94fcbe 2018-05-22 omar.polo #ifdef USE_XFT
37 9e94fcbe 2018-05-22 omar.polo # define default_fontname "monospace"
39 9e94fcbe 2018-05-22 omar.polo # define default_fontname "fixed"
42 9e94fcbe 2018-05-22 omar.polo #define MIN(a, b) ((a) < (b) ? (a) : (b))
43 f5e234d6 2018-05-18 omar.polo #define MAX(a, b) ((a) > (b) ? (a) : (b))
45 9e94fcbe 2018-05-22 omar.polo // If we don't have it or we don't want an "ignore case" completion
46 9e94fcbe 2018-05-22 omar.polo // style, fall back to `strstr(3)`
47 9e94fcbe 2018-05-22 omar.polo #ifndef USE_STRCASESTR
48 fa5a6135 2018-05-25 omar.polo # define strcasestr strstr
51 95b27a5e 2018-05-19 omar.polo #define INITIAL_ITEMS 64
53 b10f01c1 2018-07-06 omar.polo #define check_allocation(a) { \
54 b10f01c1 2018-07-06 omar.polo if (a == nil) { \
55 b10f01c1 2018-07-06 omar.polo fprintf(stderr, "Could not allocate memory\n"); \
56 b10f01c1 2018-07-06 omar.polo abort(); \
60 8b929918 2018-07-01 omar.polo // The possible state of the event loop.
61 f5e234d6 2018-05-18 omar.polo enum state {LOOPING, OK, ERR};
63 8b929918 2018-07-01 omar.polo // for the drawing-related function. The text to be rendered could be
64 8b929918 2018-07-01 omar.polo // the prompt, a completion or a highlighted completion
65 c9a3bfaa 2018-05-21 omar.polo enum text_type {PROMPT, COMPL, COMPL_HIGH};
67 8b929918 2018-07-01 omar.polo // These are the possible action to be performed after user input.
68 2128b469 2018-06-30 omar.polo enum action {
70 2128b469 2018-06-30 omar.polo CONFIRM,
71 2128b469 2018-06-30 omar.polo NEXT_COMPL,
72 2128b469 2018-06-30 omar.polo PREV_COMPL,
73 2128b469 2018-06-30 omar.polo DEL_CHAR,
74 2128b469 2018-06-30 omar.polo DEL_WORD,
75 2128b469 2018-06-30 omar.polo DEL_LINE,
76 6254fed8 2018-07-06 omar.polo ADD_CHAR,
77 6254fed8 2018-07-06 omar.polo TOGGLE_FIRST_SELECTED
80 f5e234d6 2018-05-18 omar.polo struct rendering {
81 f5e234d6 2018-05-18 omar.polo Display *d;
82 f5e234d6 2018-05-18 omar.polo Window w;
83 e5186d6b 2018-05-26 omar.polo int width;
84 e5186d6b 2018-05-26 omar.polo int height;
85 e5186d6b 2018-05-26 omar.polo int padding;
86 e5186d6b 2018-05-26 omar.polo bool horizontal_layout;
87 e5186d6b 2018-05-26 omar.polo char *ps1;
88 e5186d6b 2018-05-26 omar.polo int ps1len;
89 f5e234d6 2018-05-18 omar.polo GC prompt;
90 f5e234d6 2018-05-18 omar.polo GC prompt_bg;
91 f5e234d6 2018-05-18 omar.polo GC completion;
92 f5e234d6 2018-05-18 omar.polo GC completion_bg;
93 f5e234d6 2018-05-18 omar.polo GC completion_highlighted;
94 f5e234d6 2018-05-18 omar.polo GC completion_highlighted_bg;
95 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
96 e5186d6b 2018-05-26 omar.polo XftFont *font;
97 c9a3bfaa 2018-05-21 omar.polo XftDraw *xftdraw;
98 c9a3bfaa 2018-05-21 omar.polo XftColor xft_prompt;
99 c9a3bfaa 2018-05-21 omar.polo XftColor xft_completion;
100 c9a3bfaa 2018-05-21 omar.polo XftColor xft_completion_highlighted;
102 c9a3bfaa 2018-05-21 omar.polo XFontSet *font;
103 c9a3bfaa 2018-05-21 omar.polo #endif
106 8b929918 2018-07-01 omar.polo // A simple linked list to store the completions.
107 f5e234d6 2018-05-18 omar.polo struct completions {
108 f5e234d6 2018-05-18 omar.polo char *completion;
109 f5e234d6 2018-05-18 omar.polo bool selected;
110 f5e234d6 2018-05-18 omar.polo struct completions *next;
113 2128b469 2018-06-30 omar.polo // return a newly allocated (and empty) completion list
114 f5e234d6 2018-05-18 omar.polo struct completions *compl_new() {
115 f5e234d6 2018-05-18 omar.polo struct completions *c = malloc(sizeof(struct completions));
117 f5e234d6 2018-05-18 omar.polo if (c == nil)
118 f5e234d6 2018-05-18 omar.polo return c;
120 f5e234d6 2018-05-18 omar.polo c->completion = nil;
121 f5e234d6 2018-05-18 omar.polo c->selected = false;
122 f5e234d6 2018-05-18 omar.polo c->next = nil;
123 f5e234d6 2018-05-18 omar.polo return c;
126 2128b469 2018-06-30 omar.polo // delete ONLY the given completion (i.e. does not free c->next...)
127 f5e234d6 2018-05-18 omar.polo void compl_delete(struct completions *c) {
128 f5e234d6 2018-05-18 omar.polo free(c);
131 2128b469 2018-06-30 omar.polo // delete all completion
132 3997bf41 2018-05-25 omar.polo void compl_delete_rec(struct completions *c) {
133 3997bf41 2018-05-25 omar.polo while (c != nil) {
134 3997bf41 2018-05-25 omar.polo struct completions *t = c->next;
135 3997bf41 2018-05-25 omar.polo free(c);
136 3997bf41 2018-05-25 omar.polo c = t;
140 36319ab7 2018-07-01 omar.polo // given a completion list, select the next completion and return the
141 36319ab7 2018-07-01 omar.polo // element that is selected
142 36319ab7 2018-07-01 omar.polo struct completions *compl_select_next(struct completions *c) {
143 347d23da 2018-05-19 omar.polo if (c == nil)
144 347d23da 2018-05-19 omar.polo return nil;
146 347d23da 2018-05-19 omar.polo struct completions *orig = c;
147 347d23da 2018-05-19 omar.polo while (c != nil) {
148 347d23da 2018-05-19 omar.polo if (c->selected) {
149 347d23da 2018-05-19 omar.polo c->selected = false;
150 347d23da 2018-05-19 omar.polo if (c->next != nil) {
151 347d23da 2018-05-19 omar.polo // the current one is selected and the next one exists
152 347d23da 2018-05-19 omar.polo c->next->selected = true;
153 347d23da 2018-05-19 omar.polo return c->next;
154 347d23da 2018-05-19 omar.polo } else {
155 36319ab7 2018-07-01 omar.polo // the current one is selected and the next one is nil,
156 347d23da 2018-05-19 omar.polo // select the first one
157 347d23da 2018-05-19 omar.polo orig->selected = true;
158 347d23da 2018-05-19 omar.polo return orig;
161 347d23da 2018-05-19 omar.polo c = c->next;
164 36319ab7 2018-07-01 omar.polo orig->selected = true;
165 36319ab7 2018-07-01 omar.polo return orig;
168 36319ab7 2018-07-01 omar.polo // given a completion list, select the previous and return the element
169 36319ab7 2018-07-01 omar.polo // that is selected
170 36319ab7 2018-07-01 omar.polo struct completions *compl_select_prev(struct completions *c) {
171 347d23da 2018-05-19 omar.polo if (c == nil)
172 347d23da 2018-05-19 omar.polo return nil;
174 36319ab7 2018-07-01 omar.polo struct completions *last = nil;
176 36319ab7 2018-07-01 omar.polo if (c->selected) { // if the first is selected, select the last one
177 4c3b1ef0 2018-05-26 omar.polo c->selected = false;
178 36319ab7 2018-07-01 omar.polo while (c != nil) {
179 36319ab7 2018-07-01 omar.polo if (c->next == nil) {
180 b10f01c1 2018-07-06 omar.polo c->selected = true;
181 b10f01c1 2018-07-06 omar.polo return c;
183 36319ab7 2018-07-01 omar.polo c = c->next;
187 36319ab7 2018-07-01 omar.polo // if the selected one is inside the list, select the previous one
188 36319ab7 2018-07-01 omar.polo while (c != nil) {
189 36319ab7 2018-07-01 omar.polo if (c->next == nil) { // if c is the last, save it for later
190 36319ab7 2018-07-01 omar.polo last = c;
193 36319ab7 2018-07-01 omar.polo if (c->next && c->next->selected) {
194 36319ab7 2018-07-01 omar.polo c->selected = true;
195 36319ab7 2018-07-01 omar.polo c->next->selected = false;
196 36319ab7 2018-07-01 omar.polo return c;
198 36319ab7 2018-07-01 omar.polo c = c->next;
201 36319ab7 2018-07-01 omar.polo // if nothing were selected, select the last one
202 36319ab7 2018-07-01 omar.polo if (c != nil)
203 36319ab7 2018-07-01 omar.polo c->selected = true;
204 36319ab7 2018-07-01 omar.polo return c;
207 2128b469 2018-06-30 omar.polo // create a completion list from a text and the list of possible completions
208 f5e234d6 2018-05-18 omar.polo struct completions *filter(char *text, char **lines) {
209 f5e234d6 2018-05-18 omar.polo int i = 0;
210 f5e234d6 2018-05-18 omar.polo struct completions *root = compl_new();
211 f5e234d6 2018-05-18 omar.polo struct completions *c = root;
212 3997bf41 2018-05-25 omar.polo if (c == nil)
213 3997bf41 2018-05-25 omar.polo return nil;
215 f5e234d6 2018-05-18 omar.polo for (;;) {
216 f5e234d6 2018-05-18 omar.polo char *l = lines[i];
217 f5e234d6 2018-05-18 omar.polo if (l == nil)
218 f5e234d6 2018-05-18 omar.polo break;
220 9e94fcbe 2018-05-22 omar.polo if (strcasestr(l, text) != nil) {
221 f5e234d6 2018-05-18 omar.polo c->next = compl_new();
222 f5e234d6 2018-05-18 omar.polo c = c->next;
223 3997bf41 2018-05-25 omar.polo if (c == nil) {
224 3997bf41 2018-05-25 omar.polo compl_delete_rec(root);
225 3997bf41 2018-05-25 omar.polo return nil;
227 f5e234d6 2018-05-18 omar.polo c->completion = l;
233 f5e234d6 2018-05-18 omar.polo struct completions *r = root->next;
234 f5e234d6 2018-05-18 omar.polo compl_delete(root);
235 f5e234d6 2018-05-18 omar.polo return r;
238 2128b469 2018-06-30 omar.polo // update the given completion, that is: clean the old cs & generate a new one.
239 2128b469 2018-06-30 omar.polo struct completions *update_completions(struct completions *cs, char *text, char **lines, bool first_selected) {
240 2128b469 2018-06-30 omar.polo compl_delete_rec(cs);
241 2128b469 2018-06-30 omar.polo cs = filter(text, lines);
242 2128b469 2018-06-30 omar.polo if (first_selected && cs != nil)
243 2128b469 2018-06-30 omar.polo cs->selected = true;
244 2128b469 2018-06-30 omar.polo return cs;
247 2128b469 2018-06-30 omar.polo // select the next, or the previous, selection and update some
248 36319ab7 2018-07-01 omar.polo // state. `text' will be updated with the text of the completion and
249 36319ab7 2018-07-01 omar.polo // `textlen' with the new lenght of `text'. If the memory cannot be
250 36319ab7 2018-07-01 omar.polo // allocated, `status' will be set to `ERR'.
251 36319ab7 2018-07-01 omar.polo void complete(struct completions *cs, bool first_selected, bool p, char **text, int *textlen, enum state *status) {
252 36319ab7 2018-07-01 omar.polo // if the first is always selected, and the first entry is different
253 36319ab7 2018-07-01 omar.polo // from the text, expand the text and return
254 36319ab7 2018-07-01 omar.polo if (first_selected
255 36319ab7 2018-07-01 omar.polo && cs != nil
256 36319ab7 2018-07-01 omar.polo && cs->selected
257 36319ab7 2018-07-01 omar.polo && strcmp(cs->completion, *text) != 0
258 36319ab7 2018-07-01 omar.polo && !p) {
259 36319ab7 2018-07-01 omar.polo free(*text);
260 36319ab7 2018-07-01 omar.polo *text = strdup(cs->completion);
261 36319ab7 2018-07-01 omar.polo if (text == nil) {
262 36319ab7 2018-07-01 omar.polo fprintf(stderr, "Memory allocation error!\n");
263 36319ab7 2018-07-01 omar.polo *status = ERR;
264 36319ab7 2018-07-01 omar.polo return;
266 36319ab7 2018-07-01 omar.polo *textlen = strlen(*text);
267 36319ab7 2018-07-01 omar.polo return;
270 2128b469 2018-06-30 omar.polo struct completions *n = p
271 36319ab7 2018-07-01 omar.polo ? compl_select_prev(cs)
272 36319ab7 2018-07-01 omar.polo : compl_select_next(cs);
274 2128b469 2018-06-30 omar.polo if (n != nil) {
275 2128b469 2018-06-30 omar.polo free(*text);
276 2128b469 2018-06-30 omar.polo *text = strdup(n->completion);
277 2128b469 2018-06-30 omar.polo if (text == nil) {
278 2128b469 2018-06-30 omar.polo fprintf(stderr, "Memory allocation error!\n");
279 2128b469 2018-06-30 omar.polo *status = ERR;
280 2128b469 2018-06-30 omar.polo return;
282 2128b469 2018-06-30 omar.polo *textlen = strlen(*text);
287 f5e234d6 2018-05-18 omar.polo // push the character c at the end of the string pointed by p
288 f5e234d6 2018-05-18 omar.polo int pushc(char **p, int maxlen, char c) {
289 f5e234d6 2018-05-18 omar.polo int len = strnlen(*p, maxlen);
291 f5e234d6 2018-05-18 omar.polo if (!(len < maxlen -2)) {
292 f5e234d6 2018-05-18 omar.polo maxlen += maxlen >> 1;
293 f5e234d6 2018-05-18 omar.polo char *newptr = realloc(*p, maxlen);
294 f5e234d6 2018-05-18 omar.polo if (newptr == nil) { // bad!
295 f5e234d6 2018-05-18 omar.polo return -1;
297 f5e234d6 2018-05-18 omar.polo *p = newptr;
300 f5e234d6 2018-05-18 omar.polo (*p)[len] = c;
301 f5e234d6 2018-05-18 omar.polo (*p)[len+1] = '\0';
302 f5e234d6 2018-05-18 omar.polo return maxlen;
305 2128b469 2018-06-30 omar.polo // return the number of character
306 347d23da 2018-05-19 omar.polo int utf8strnlen(char *s, int maxlen) {
307 347d23da 2018-05-19 omar.polo int len = 0;
308 347d23da 2018-05-19 omar.polo while (*s && maxlen > 0) {
309 347d23da 2018-05-19 omar.polo len += (*s++ & 0xc0) != 0x80;
310 347d23da 2018-05-19 omar.polo maxlen--;
312 347d23da 2018-05-19 omar.polo return len;
315 347d23da 2018-05-19 omar.polo // remove the last *glyph* from the *utf8* string!
316 347d23da 2018-05-19 omar.polo // this is different from just setting the last byte to 0 (in some
317 347d23da 2018-05-19 omar.polo // cases ofc). The actual implementation is quite inefficient because
318 2128b469 2018-06-30 omar.polo // it remove the last byte until the number of glyphs doesn't change
319 f5e234d6 2018-05-18 omar.polo void popc(char *p, int maxlen) {
320 f5e234d6 2018-05-18 omar.polo int len = strnlen(p, maxlen);
322 347d23da 2018-05-19 omar.polo if (len == 0)
323 347d23da 2018-05-19 omar.polo return;
325 347d23da 2018-05-19 omar.polo int ulen = utf8strnlen(p, maxlen);
326 347d23da 2018-05-19 omar.polo while (len > 0 && utf8strnlen(p, maxlen) == ulen) {
327 347d23da 2018-05-19 omar.polo len--;
328 347d23da 2018-05-19 omar.polo p[len] = 0;
332 8758854a 2018-05-20 omar.polo // If the string is surrounded by quotes (`"`) remove them and replace
333 8758854a 2018-05-20 omar.polo // every `\"` in the string with `"`
334 8758854a 2018-05-20 omar.polo char *normalize_str(const char *str) {
335 8758854a 2018-05-20 omar.polo int len = strlen(str);
336 8758854a 2018-05-20 omar.polo if (len == 0)
337 8758854a 2018-05-20 omar.polo return nil;
339 8758854a 2018-05-20 omar.polo char *s = calloc(len, sizeof(char));
340 8758854a 2018-05-20 omar.polo check_allocation(s);
341 8758854a 2018-05-20 omar.polo int p = 0;
342 8758854a 2018-05-20 omar.polo while (*str) {
343 8758854a 2018-05-20 omar.polo char c = *str;
344 8758854a 2018-05-20 omar.polo if (*str == '\\') {
345 8758854a 2018-05-20 omar.polo if (*(str + 1)) {
346 8758854a 2018-05-20 omar.polo s[p] = *(str + 1);
348 8758854a 2018-05-20 omar.polo str += 2; // skip this and the next char
349 8758854a 2018-05-20 omar.polo continue;
350 8758854a 2018-05-20 omar.polo } else {
351 8758854a 2018-05-20 omar.polo break;
354 8758854a 2018-05-20 omar.polo if (c == '"') {
355 8758854a 2018-05-20 omar.polo str++; // skip only this char
356 8758854a 2018-05-20 omar.polo continue;
358 8758854a 2018-05-20 omar.polo s[p] = c;
360 8758854a 2018-05-20 omar.polo str++;
362 8758854a 2018-05-20 omar.polo return s;
365 f5e234d6 2018-05-18 omar.polo // read an arbitrary long line from stdin and return a pointer to it
366 f5e234d6 2018-05-18 omar.polo // TODO: resize the allocated memory to exactly fit the string once
367 f5e234d6 2018-05-18 omar.polo // read?
368 f5e234d6 2018-05-18 omar.polo char *readline(bool *eof) {
369 f5e234d6 2018-05-18 omar.polo int maxlen = 8;
370 f5e234d6 2018-05-18 omar.polo char *str = calloc(maxlen, sizeof(char));
371 f5e234d6 2018-05-18 omar.polo if (str == nil) {
372 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Cannot allocate memory!\n");
373 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
376 f5e234d6 2018-05-18 omar.polo int c;
377 f5e234d6 2018-05-18 omar.polo while((c = getchar()) != EOF) {
378 f5e234d6 2018-05-18 omar.polo if (c == '\n')
379 f5e234d6 2018-05-18 omar.polo return str;
381 f5e234d6 2018-05-18 omar.polo maxlen = pushc(&str, maxlen, c);
383 f5e234d6 2018-05-18 omar.polo if (maxlen == -1) {
384 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Cannot allocate memory!\n");
385 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
388 f5e234d6 2018-05-18 omar.polo *eof = true;
389 f5e234d6 2018-05-18 omar.polo return str;
392 e9f0b467 2018-06-07 omar.polo // read an arbitrary amount of text until an EOF and store it in
393 e9f0b467 2018-06-07 omar.polo // lns. `items` is the capacity of lns. It may increase lns with
394 e9f0b467 2018-06-07 omar.polo // `realloc(3)` to store more line. Return the number of lines
395 e9f0b467 2018-06-07 omar.polo // read. The last item will always be a NULL pointer. It ignore the
396 e9f0b467 2018-06-07 omar.polo // "null" (empty) lines
397 95b27a5e 2018-05-19 omar.polo int readlines (char ***lns, int items) {
398 f5e234d6 2018-05-18 omar.polo bool finished = false;
399 f5e234d6 2018-05-18 omar.polo int n = 0;
400 95b27a5e 2018-05-19 omar.polo char **lines = *lns;
401 95b27a5e 2018-05-19 omar.polo while (true) {
402 f5e234d6 2018-05-18 omar.polo lines[n] = readline(&finished);
404 6da98882 2018-05-20 omar.polo if (strlen(lines[n]) == 0 || lines[n][0] == '\n') {
405 6da98882 2018-05-20 omar.polo free(lines[n]);
406 f5e234d6 2018-05-18 omar.polo --n; // forget about this line
409 f5e234d6 2018-05-18 omar.polo if (finished)
410 f5e234d6 2018-05-18 omar.polo break;
414 95b27a5e 2018-05-19 omar.polo if (n == items - 1) {
415 95b27a5e 2018-05-19 omar.polo items += items >>1;
416 95b27a5e 2018-05-19 omar.polo char **l = realloc(lines, sizeof(char*) * items);
417 95b27a5e 2018-05-19 omar.polo check_allocation(l);
418 95b27a5e 2018-05-19 omar.polo *lns = l;
419 95b27a5e 2018-05-19 omar.polo lines = l;
424 f5e234d6 2018-05-18 omar.polo lines[n] = nil;
425 95b27a5e 2018-05-19 omar.polo return items;
428 25bf99d2 2018-05-22 omar.polo // Compute the dimension of the string str once rendered, return the
429 25bf99d2 2018-05-22 omar.polo // width and save the width and the height in ret_width and ret_height
430 c9a3bfaa 2018-05-21 omar.polo int text_extents(char *str, int len, struct rendering *r, int *ret_width, int *ret_height) {
431 c9a3bfaa 2018-05-21 omar.polo int height;
432 c9a3bfaa 2018-05-21 omar.polo int width;
433 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
434 c9a3bfaa 2018-05-21 omar.polo XGlyphInfo gi;
435 c9a3bfaa 2018-05-21 omar.polo XftTextExtentsUtf8(r->d, r->font, str, len, &gi);
436 25bf99d2 2018-05-22 omar.polo /* height = gi.height; */
437 e5186d6b 2018-05-26 omar.polo /* height = (gi.height + (r->font->ascent - r->font->descent)/2) / 2; */
438 e5186d6b 2018-05-26 omar.polo /* height = (r->font->ascent - r->font->descent)/2 + gi.height*2; */
439 e5186d6b 2018-05-26 omar.polo height = r->font->ascent - r->font->descent;
440 c9a3bfaa 2018-05-21 omar.polo width = gi.width - gi.x;
442 c9a3bfaa 2018-05-21 omar.polo XRectangle rect;
443 c9a3bfaa 2018-05-21 omar.polo XmbTextExtents(*r->font, str, len, nil, &rect);
444 c9a3bfaa 2018-05-21 omar.polo height = rect.height;
445 c9a3bfaa 2018-05-21 omar.polo width = rect.width;
446 c9a3bfaa 2018-05-21 omar.polo #endif
447 c9a3bfaa 2018-05-21 omar.polo if (ret_width != nil) *ret_width = width;
448 c9a3bfaa 2018-05-21 omar.polo if (ret_height != nil) *ret_height = height;
449 c9a3bfaa 2018-05-21 omar.polo return width;
452 25bf99d2 2018-05-22 omar.polo // Draw the string str
453 c9a3bfaa 2018-05-21 omar.polo void draw_string(char *str, int len, int x, int y, struct rendering *r, enum text_type tt) {
454 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
455 c9a3bfaa 2018-05-21 omar.polo XftColor xftcolor;
456 c9a3bfaa 2018-05-21 omar.polo if (tt == PROMPT) xftcolor = r->xft_prompt;
457 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL) xftcolor = r->xft_completion;
458 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL_HIGH) xftcolor = r->xft_completion_highlighted;
460 c9a3bfaa 2018-05-21 omar.polo XftDrawStringUtf8(r->xftdraw, &xftcolor, r->font, x, y, str, len);
462 c9a3bfaa 2018-05-21 omar.polo GC gc;
463 c9a3bfaa 2018-05-21 omar.polo if (tt == PROMPT) gc = r->prompt;
464 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL) gc = r->completion;
465 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL_HIGH) gc = r->completion_highlighted;
466 c9a3bfaa 2018-05-21 omar.polo Xutf8DrawString(r->d, r->w, *r->font, gc, x, y, str, len);
467 c9a3bfaa 2018-05-21 omar.polo #endif
470 25bf99d2 2018-05-22 omar.polo // Duplicate the string str and substitute every space with a 'n'
471 c9a3bfaa 2018-05-21 omar.polo char *strdupn(char *str) {
472 c9a3bfaa 2018-05-21 omar.polo int len = strlen(str);
474 c9a3bfaa 2018-05-21 omar.polo if (str == nil || len == 0)
475 c9a3bfaa 2018-05-21 omar.polo return nil;
477 c9a3bfaa 2018-05-21 omar.polo char *dup = strdup(str);
478 c9a3bfaa 2018-05-21 omar.polo if (dup == nil)
479 c9a3bfaa 2018-05-21 omar.polo return nil;
481 c9a3bfaa 2018-05-21 omar.polo for (int i = 0; i < len; ++i)
482 c9a3bfaa 2018-05-21 omar.polo if (dup[i] == ' ')
483 c9a3bfaa 2018-05-21 omar.polo dup[i] = 'n';
485 c9a3bfaa 2018-05-21 omar.polo return dup;
488 f5e234d6 2018-05-18 omar.polo // |------------------|----------------------------------------------|
489 f5e234d6 2018-05-18 omar.polo // | 20 char text | completion | completion | completion | compl |
490 f5e234d6 2018-05-18 omar.polo // |------------------|----------------------------------------------|
491 36a15a9f 2018-05-19 omar.polo void draw_horizontally(struct rendering *r, char *text, struct completions *cs) {
492 f5e234d6 2018-05-18 omar.polo int prompt_width = 20; // char
494 c9a3bfaa 2018-05-21 omar.polo int width, height;
495 c9a3bfaa 2018-05-21 omar.polo char *ps1_dup = strdupn(r->ps1);
496 e5186d6b 2018-05-26 omar.polo if (ps1_dup == nil)
497 e5186d6b 2018-05-26 omar.polo return;
499 c9a3bfaa 2018-05-21 omar.polo ps1_dup = ps1_dup == nil ? r->ps1 : ps1_dup;
500 c9a3bfaa 2018-05-21 omar.polo int ps1xlen = text_extents(ps1_dup, r->ps1len, r, &width, &height);
501 c9a3bfaa 2018-05-21 omar.polo free(ps1_dup);
502 8758854a 2018-05-20 omar.polo int start_at = ps1xlen;
504 c9a3bfaa 2018-05-21 omar.polo start_at = text_extents("n", 1, r, nil, nil);
505 e5186d6b 2018-05-26 omar.polo start_at = start_at * prompt_width + r->padding;
507 c9a3bfaa 2018-05-21 omar.polo int texty = (height + r->height) >>1;
509 f5e234d6 2018-05-18 omar.polo XFillRectangle(r->d, r->w, r->prompt_bg, 0, 0, start_at, r->height);
511 f5e234d6 2018-05-18 omar.polo int text_len = strlen(text);
512 f5e234d6 2018-05-18 omar.polo if (text_len > prompt_width)
513 f5e234d6 2018-05-18 omar.polo text = text + (text_len - prompt_width);
514 e5186d6b 2018-05-26 omar.polo draw_string(r->ps1, r->ps1len, r->padding, texty, r, PROMPT);
515 e5186d6b 2018-05-26 omar.polo draw_string(text, MIN(text_len, prompt_width), r->padding + ps1xlen, texty, r, PROMPT);
517 f5e234d6 2018-05-18 omar.polo XFillRectangle(r->d, r->w, r->completion_bg, start_at, 0, r->width, r->height);
519 f5e234d6 2018-05-18 omar.polo while (cs != nil) {
520 c9a3bfaa 2018-05-21 omar.polo enum text_type tt = cs->selected ? COMPL_HIGH : COMPL;
521 f5e234d6 2018-05-18 omar.polo GC h = cs->selected ? r->completion_highlighted_bg : r->completion_bg;
523 f5e234d6 2018-05-18 omar.polo int len = strlen(cs->completion);
524 c9a3bfaa 2018-05-21 omar.polo int text_width = text_extents(cs->completion, len, r, nil, nil);
526 e5186d6b 2018-05-26 omar.polo XFillRectangle(r->d, r->w, h, start_at, 0, text_width + r->padding*2, r->height);
528 e5186d6b 2018-05-26 omar.polo draw_string(cs->completion, len, start_at + r->padding, texty, r, tt);
530 e5186d6b 2018-05-26 omar.polo start_at += text_width + r->padding * 2;
532 0ee198aa 2018-05-19 omar.polo if (start_at > r->width)
533 0ee198aa 2018-05-19 omar.polo break; // don't draw completion if the space isn't enough
535 f5e234d6 2018-05-18 omar.polo cs = cs->next;
538 f5e234d6 2018-05-18 omar.polo XFlush(r->d);
541 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
542 36a15a9f 2018-05-19 omar.polo // | prompt |
543 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
544 36a15a9f 2018-05-19 omar.polo // | completion |
545 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
546 36a15a9f 2018-05-19 omar.polo // | completion |
547 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
548 e5186d6b 2018-05-26 omar.polo void draw_vertically(struct rendering *r, char *text, struct completions *cs) {
549 c9a3bfaa 2018-05-21 omar.polo int height, width;
550 e5186d6b 2018-05-26 omar.polo text_extents("fjpgl", 5, r, nil, &height);
551 e5186d6b 2018-05-26 omar.polo int start_at = height + r->padding;
553 36a15a9f 2018-05-19 omar.polo XFillRectangle(r->d, r->w, r->completion_bg, 0, 0, r->width, r->height);
554 36a15a9f 2018-05-19 omar.polo XFillRectangle(r->d, r->w, r->prompt_bg, 0, 0, r->width, start_at);
556 c9a3bfaa 2018-05-21 omar.polo char *ps1_dup = strdupn(r->ps1);
557 c9a3bfaa 2018-05-21 omar.polo ps1_dup = ps1_dup == nil ? r->ps1 : ps1_dup;
558 c9a3bfaa 2018-05-21 omar.polo int ps1xlen = text_extents(ps1_dup, r->ps1len, r, nil, nil);
559 c9a3bfaa 2018-05-21 omar.polo free(ps1_dup);
561 e5186d6b 2018-05-26 omar.polo draw_string(r->ps1, r->ps1len, r->padding, height + r->padding, r, PROMPT);
562 e5186d6b 2018-05-26 omar.polo draw_string(text, strlen(text), r->padding + ps1xlen, height + r->padding, r, PROMPT);
563 e5186d6b 2018-05-26 omar.polo start_at += r->padding;
565 36a15a9f 2018-05-19 omar.polo while (cs != nil) {
566 c9a3bfaa 2018-05-21 omar.polo enum text_type tt = cs->selected ? COMPL_HIGH : COMPL;
567 36a15a9f 2018-05-19 omar.polo GC h = cs->selected ? r->completion_highlighted_bg : r->completion_bg;
569 36a15a9f 2018-05-19 omar.polo int len = strlen(cs->completion);
570 c9a3bfaa 2018-05-21 omar.polo text_extents(cs->completion, len, r, &width, &height);
571 e5186d6b 2018-05-26 omar.polo XFillRectangle(r->d, r->w, h, 0, start_at, r->width, height + r->padding*2);
572 e5186d6b 2018-05-26 omar.polo draw_string(cs->completion, len, r->padding, start_at + height + r->padding, r, tt);
574 e5186d6b 2018-05-26 omar.polo start_at += height + r->padding *2;
576 0ee198aa 2018-05-19 omar.polo if (start_at > r->height)
577 0ee198aa 2018-05-19 omar.polo break; // don't draw completion if the space isn't enough
579 36a15a9f 2018-05-19 omar.polo cs = cs->next;
582 36a15a9f 2018-05-19 omar.polo XFlush(r->d);
585 36a15a9f 2018-05-19 omar.polo void draw(struct rendering *r, char *text, struct completions *cs) {
586 36a15a9f 2018-05-19 omar.polo if (r->horizontal_layout)
587 36a15a9f 2018-05-19 omar.polo draw_horizontally(r, text, cs);
589 36a15a9f 2018-05-19 omar.polo draw_vertically(r, text, cs);
592 f5e234d6 2018-05-18 omar.polo /* Set some WM stuff */
593 f5e234d6 2018-05-18 omar.polo void set_win_atoms_hints(Display *d, Window w, int width, int height) {
594 f5e234d6 2018-05-18 omar.polo Atom type;
595 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_WINDOW_TYPE_DOCK", false);
596 f5e234d6 2018-05-18 omar.polo XChangeProperty(
599 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_WINDOW_TYPE", false),
600 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
602 f5e234d6 2018-05-18 omar.polo PropModeReplace,
603 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
607 f5e234d6 2018-05-18 omar.polo /* some window managers honor this properties */
608 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_STATE_ABOVE", false);
609 f5e234d6 2018-05-18 omar.polo XChangeProperty(d,
611 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_STATE", false),
612 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
614 f5e234d6 2018-05-18 omar.polo PropModeReplace,
615 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
619 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_STATE_FOCUSED", false);
620 f5e234d6 2018-05-18 omar.polo XChangeProperty(d,
622 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_STATE", false),
623 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
625 f5e234d6 2018-05-18 omar.polo PropModeAppend,
626 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
630 f5e234d6 2018-05-18 omar.polo // setting window hints
631 f5e234d6 2018-05-18 omar.polo XClassHint *class_hint = XAllocClassHint();
632 f5e234d6 2018-05-18 omar.polo if (class_hint == nil) {
633 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not allocate memory for class hint\n");
634 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
636 7ef37ee0 2018-05-21 omar.polo class_hint->res_name = resname;
637 7ef37ee0 2018-05-21 omar.polo class_hint->res_class = resclass;
638 f5e234d6 2018-05-18 omar.polo XSetClassHint(d, w, class_hint);
639 f5e234d6 2018-05-18 omar.polo XFree(class_hint);
641 f5e234d6 2018-05-18 omar.polo XSizeHints *size_hint = XAllocSizeHints();
642 f5e234d6 2018-05-18 omar.polo if (size_hint == nil) {
643 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not allocate memory for size hint\n");
644 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
646 7ef37ee0 2018-05-21 omar.polo size_hint->flags = PMinSize | PBaseSize;
647 f5e234d6 2018-05-18 omar.polo size_hint->min_width = width;
648 f5e234d6 2018-05-18 omar.polo size_hint->base_width = width;
649 f5e234d6 2018-05-18 omar.polo size_hint->min_height = height;
650 f5e234d6 2018-05-18 omar.polo size_hint->base_height = height;
652 f5e234d6 2018-05-18 omar.polo XFlush(d);
655 8b929918 2018-07-01 omar.polo // write the width and height of the window `w' respectively in `width'
656 8b929918 2018-07-01 omar.polo // and `height'.
657 f5e234d6 2018-05-18 omar.polo void get_wh(Display *d, Window *w, int *width, int *height) {
658 f5e234d6 2018-05-18 omar.polo XWindowAttributes win_attr;
659 f5e234d6 2018-05-18 omar.polo XGetWindowAttributes(d, *w, &win_attr);
660 f5e234d6 2018-05-18 omar.polo *height = win_attr.height;
661 f5e234d6 2018-05-18 omar.polo *width = win_attr.width;
664 f5e234d6 2018-05-18 omar.polo // I know this may seem a little hackish BUT is the only way I managed
665 f5e234d6 2018-05-18 omar.polo // to actually grab that goddam keyboard. Only one call to
666 f5e234d6 2018-05-18 omar.polo // XGrabKeyboard does not always end up with the keyboard grabbed!
667 f5e234d6 2018-05-18 omar.polo int take_keyboard(Display *d, Window w) {
668 686e9237 2018-05-18 omar.polo int i;
669 686e9237 2018-05-18 omar.polo for (i = 0; i < 100; i++) {
670 686e9237 2018-05-18 omar.polo if (XGrabKeyboard(d, w, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
671 686e9237 2018-05-18 omar.polo return 1;
672 686e9237 2018-05-18 omar.polo usleep(1000);
674 686e9237 2018-05-18 omar.polo return 0;
677 2128b469 2018-06-30 omar.polo // release the keyboard.
678 f5e234d6 2018-05-18 omar.polo void release_keyboard(Display *d) {
679 686e9237 2018-05-18 omar.polo XUngrabKeyboard(d, CurrentTime);
682 8b929918 2018-07-01 omar.polo // Given a string, try to parse it as a number or return
683 8b929918 2018-07-01 omar.polo // `default_value'.
684 25bf99d2 2018-05-22 omar.polo int parse_integer(const char *str, int default_value) {
685 f5e234d6 2018-05-18 omar.polo errno = 0;
686 f5e234d6 2018-05-18 omar.polo char *ep;
687 f5e234d6 2018-05-18 omar.polo long lval = strtol(str, &ep, 10);
688 f5e234d6 2018-05-18 omar.polo if (str[0] == '\0' || *ep != '\0') { // NaN
689 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "'%s' is not a valid number! Using %d as default.\n", str, default_value);
690 f5e234d6 2018-05-18 omar.polo return default_value;
692 f5e234d6 2018-05-18 omar.polo if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
693 f5e234d6 2018-05-18 omar.polo (lval > INT_MAX || lval < INT_MIN)) {
694 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "%s out of range! Using %d as default.\n", str, default_value);
695 f5e234d6 2018-05-18 omar.polo return default_value;
697 f5e234d6 2018-05-18 omar.polo return lval;
700 2128b469 2018-06-30 omar.polo // like parse_integer, but if the value ends with a `%' then its
701 2128b469 2018-06-30 omar.polo // treated like a percentage (`max' is used to compute the percentage)
702 25bf99d2 2018-05-22 omar.polo int parse_int_with_percentage(const char *str, int default_value, int max) {
703 25bf99d2 2018-05-22 omar.polo int len = strlen(str);
704 25bf99d2 2018-05-22 omar.polo if (len > 0 && str[len-1] == '%') {
705 25bf99d2 2018-05-22 omar.polo char *cpy = strdup(str);
706 25bf99d2 2018-05-22 omar.polo check_allocation(cpy);
707 25bf99d2 2018-05-22 omar.polo cpy[len-1] = '\0';
708 25bf99d2 2018-05-22 omar.polo int val = parse_integer(cpy, default_value);
709 25bf99d2 2018-05-22 omar.polo free(cpy);
710 25bf99d2 2018-05-22 omar.polo return val * max / 100;
712 576ab141 2018-05-22 omar.polo return parse_integer(str, default_value);
715 2128b469 2018-06-30 omar.polo // like parse_int_with_percentage but understands the special value
716 2128b469 2018-06-30 omar.polo // "middle" that is (max - self) / 2
717 f5e234d6 2018-05-18 omar.polo int parse_int_with_middle(const char *str, int default_value, int max, int self) {
718 f5e234d6 2018-05-18 omar.polo if (!strcmp(str, "middle")) {
719 f5e234d6 2018-05-18 omar.polo return (max - self)/2;
721 25bf99d2 2018-05-22 omar.polo return parse_int_with_percentage(str, default_value, max);
724 2128b469 2018-06-30 omar.polo // Given the name of the program (argv[0]?) print a small help on stderr
725 e9f0b467 2018-06-07 omar.polo void usage(char *prgname) {
726 e9f0b467 2018-06-07 omar.polo fprintf(stderr, "Usage: %s [flags]\n", prgname);
727 e9f0b467 2018-06-07 omar.polo fprintf(stderr, "\t-a: automatic mode, the first completion is "
728 b10f01c1 2018-07-06 omar.polo "always selected;\n");
729 e9f0b467 2018-06-07 omar.polo fprintf(stderr, "\t-h: print this help.\n");
732 2128b469 2018-06-30 omar.polo // Given an event, try to understand what the user wants. If the
733 2128b469 2018-06-30 omar.polo // return value is ADD_CHAR then `input' is a pointer to a string that
734 2128b469 2018-06-30 omar.polo // will need to be free'ed.
735 2128b469 2018-06-30 omar.polo enum action parse_event(Display *d, XKeyPressedEvent *ev, XIC xic, char **input) {
736 2128b469 2018-06-30 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_BackSpace))
737 2128b469 2018-06-30 omar.polo return DEL_CHAR;
739 2128b469 2018-06-30 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Tab))
740 2128b469 2018-06-30 omar.polo return ev->state & ShiftMask ? PREV_COMPL : NEXT_COMPL;
742 2128b469 2018-06-30 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Return))
743 2128b469 2018-06-30 omar.polo return CONFIRM;
745 2128b469 2018-06-30 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Escape))
746 2128b469 2018-06-30 omar.polo return EXIT;
748 2128b469 2018-06-30 omar.polo // try to read what the user pressed
749 2128b469 2018-06-30 omar.polo int symbol = 0;
750 2128b469 2018-06-30 omar.polo Status s = 0;
751 2128b469 2018-06-30 omar.polo Xutf8LookupString(xic, ev, (char*)&symbol, 4, 0, &s);
752 2128b469 2018-06-30 omar.polo if (s == XBufferOverflow) {
753 2128b469 2018-06-30 omar.polo // should not happen since there are no utf-8 characters larger
754 2128b469 2018-06-30 omar.polo // than 24bits
755 2128b469 2018-06-30 omar.polo fprintf(stderr, "Buffer overflow when trying to create keyboard symbol map.\n");
756 2128b469 2018-06-30 omar.polo return EXIT;
758 2128b469 2018-06-30 omar.polo char *str = (char*)&symbol;
760 2128b469 2018-06-30 omar.polo if (ev->state & ControlMask) {
761 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-u
762 2128b469 2018-06-30 omar.polo return DEL_LINE;
763 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-w
764 2128b469 2018-06-30 omar.polo return DEL_WORD;
765 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-h
766 2128b469 2018-06-30 omar.polo return DEL_CHAR;
767 2128b469 2018-06-30 omar.polo if (!strcmp(str, "\r")) // C-m
768 2128b469 2018-06-30 omar.polo return CONFIRM;
769 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-p
770 2128b469 2018-06-30 omar.polo return PREV_COMPL;
771 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-n
772 2128b469 2018-06-30 omar.polo return NEXT_COMPL;
773 bee0837c 2018-07-01 omar.polo if (!strcmp(str, "")) // C-c
774 bee0837c 2018-07-01 omar.polo return EXIT;
775 6254fed8 2018-07-06 omar.polo if (!strcmp(str, "\t")) // C-i
776 6254fed8 2018-07-06 omar.polo return TOGGLE_FIRST_SELECTED;
779 2128b469 2018-06-30 omar.polo *input = strdup((char*)&symbol);
780 2128b469 2018-06-30 omar.polo if (*input == nil) {
781 2128b469 2018-06-30 omar.polo fprintf(stderr, "Error while allocating memory for key.\n");
782 2128b469 2018-06-30 omar.polo return EXIT;
785 2128b469 2018-06-30 omar.polo return ADD_CHAR;
788 2128b469 2018-06-30 omar.polo // clean up some resources
789 e9f0b467 2018-06-07 omar.polo int exit_cleanup(struct rendering *r, char *ps1, char *fontname, char *text, char **lines, struct completions *cs, int status) {
790 e9f0b467 2018-06-07 omar.polo release_keyboard(r->d);
792 e9f0b467 2018-06-07 omar.polo #ifdef USE_XFT
793 e9f0b467 2018-06-07 omar.polo XftColorFree(r->d, DefaultVisual(r->d, 0), DefaultColormap(r->d, 0), &r->xft_prompt);
794 e9f0b467 2018-06-07 omar.polo XftColorFree(r->d, DefaultVisual(r->d, 0), DefaultColormap(r->d, 0), &r->xft_completion);
795 e9f0b467 2018-06-07 omar.polo XftColorFree(r->d, DefaultVisual(r->d, 0), DefaultColormap(r->d, 0), &r->xft_completion_highlighted);
796 e9f0b467 2018-06-07 omar.polo #endif
798 e9f0b467 2018-06-07 omar.polo free(ps1);
799 e9f0b467 2018-06-07 omar.polo free(fontname);
800 e9f0b467 2018-06-07 omar.polo free(text);
802 e9f0b467 2018-06-07 omar.polo char *l = nil;
803 e9f0b467 2018-06-07 omar.polo char **lns = lines;
804 e9f0b467 2018-06-07 omar.polo while ((l = *lns) != nil) {
805 e9f0b467 2018-06-07 omar.polo free(l);
806 e9f0b467 2018-06-07 omar.polo ++lns;
808 e9f0b467 2018-06-07 omar.polo free(lines);
809 e9f0b467 2018-06-07 omar.polo compl_delete(cs);
811 e9f0b467 2018-06-07 omar.polo XDestroyWindow(r->d, r->w);
812 e9f0b467 2018-06-07 omar.polo XCloseDisplay(r->d);
814 e9f0b467 2018-06-07 omar.polo return status;
817 e9f0b467 2018-06-07 omar.polo int main(int argc, char **argv) {
818 1ef91f4e 2018-07-03 omar.polo #ifdef HAVE_PLEDGE
819 1ef91f4e 2018-07-03 omar.polo // stdio & rpat: to read and write stdio/stdout
820 1ef91f4e 2018-07-03 omar.polo // unix: to connect to Xorg
821 1ef91f4e 2018-07-03 omar.polo pledge("stdio rpath unix", "");
822 1ef91f4e 2018-07-03 omar.polo #endif
824 e9f0b467 2018-06-07 omar.polo // by default the first completion isn't selected
825 e9f0b467 2018-06-07 omar.polo bool first_selected = false;
827 e9f0b467 2018-06-07 omar.polo // parse the command line options
828 e9f0b467 2018-06-07 omar.polo int ch;
829 b500fe86 2018-06-07 omar.polo while ((ch = getopt(argc, argv, "ahv")) != -1) {
830 e9f0b467 2018-06-07 omar.polo switch (ch) {
831 449f27a6 2018-07-06 omar.polo case 'a':
832 b10f01c1 2018-07-06 omar.polo first_selected = true;
833 b10f01c1 2018-07-06 omar.polo break;
834 449f27a6 2018-07-06 omar.polo case 'h':
835 b10f01c1 2018-07-06 omar.polo usage(*argv);
836 b10f01c1 2018-07-06 omar.polo return 0;
837 449f27a6 2018-07-06 omar.polo case 'v':
838 b10f01c1 2018-07-06 omar.polo fprintf(stderr, "%s version: %s\n", *argv, VERSION);
839 b10f01c1 2018-07-06 omar.polo return 0;
840 449f27a6 2018-07-06 omar.polo default:
841 b10f01c1 2018-07-06 omar.polo usage(*argv);
842 b10f01c1 2018-07-06 omar.polo return EX_USAGE;
846 95b27a5e 2018-05-19 omar.polo char **lines = calloc(INITIAL_ITEMS, sizeof(char*));
847 e9f0b467 2018-06-07 omar.polo readlines(&lines, INITIAL_ITEMS);
849 f5e234d6 2018-05-18 omar.polo setlocale(LC_ALL, getenv("LANG"));
851 f5e234d6 2018-05-18 omar.polo enum state status = LOOPING;
853 f5e234d6 2018-05-18 omar.polo // where the monitor start (used only with xinerama)
854 f5e234d6 2018-05-18 omar.polo int offset_x = 0;
855 f5e234d6 2018-05-18 omar.polo int offset_y = 0;
857 f5e234d6 2018-05-18 omar.polo // width and height of the window
858 f5e234d6 2018-05-18 omar.polo int width = 400;
859 f5e234d6 2018-05-18 omar.polo int height = 20;
861 f5e234d6 2018-05-18 omar.polo // position on the screen
862 f5e234d6 2018-05-18 omar.polo int x = 0;
863 f5e234d6 2018-05-18 omar.polo int y = 0;
865 e9f0b467 2018-06-07 omar.polo // the default padding
866 e5186d6b 2018-05-26 omar.polo int padding = 10;
868 8758854a 2018-05-20 omar.polo char *ps1 = strdup("$ ");
869 8758854a 2018-05-20 omar.polo check_allocation(ps1);
871 9e94fcbe 2018-05-22 omar.polo char *fontname = strdup(default_fontname);
872 f5e234d6 2018-05-18 omar.polo check_allocation(fontname);
874 f5e234d6 2018-05-18 omar.polo int textlen = 10;
875 f5e234d6 2018-05-18 omar.polo char *text = malloc(textlen * sizeof(char));
876 f5e234d6 2018-05-18 omar.polo check_allocation(text);
878 e9f0b467 2018-06-07 omar.polo /* struct completions *cs = filter(text, lines); */
879 e9f0b467 2018-06-07 omar.polo struct completions *cs = nil;
880 2128b469 2018-06-30 omar.polo cs = update_completions(cs, text, lines, first_selected);
882 f5e234d6 2018-05-18 omar.polo // start talking to xorg
883 f5e234d6 2018-05-18 omar.polo Display *d = XOpenDisplay(nil);
884 f5e234d6 2018-05-18 omar.polo if (d == nil) {
885 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not open display!\n");
886 f5e234d6 2018-05-18 omar.polo return EX_UNAVAILABLE;
889 f5e234d6 2018-05-18 omar.polo // get display size
890 f5e234d6 2018-05-18 omar.polo // XXX: is getting the default root window dimension correct?
891 f5e234d6 2018-05-18 omar.polo XWindowAttributes xwa;
892 f5e234d6 2018-05-18 omar.polo XGetWindowAttributes(d, DefaultRootWindow(d), &xwa);
893 f5e234d6 2018-05-18 omar.polo int d_width = xwa.width;
894 f5e234d6 2018-05-18 omar.polo int d_height = xwa.height;
896 f5e234d6 2018-05-18 omar.polo #ifdef USE_XINERAMA
897 f5e234d6 2018-05-18 omar.polo if (XineramaIsActive(d)) {
898 7ca8829b 2018-05-21 omar.polo // find the mice
899 7ca8829b 2018-05-21 omar.polo int number_of_screens = XScreenCount(d);
900 7ca8829b 2018-05-21 omar.polo Window r;
901 7ca8829b 2018-05-21 omar.polo Window root;
902 7ca8829b 2018-05-21 omar.polo int root_x, root_y, win_x, win_y;
903 7ca8829b 2018-05-21 omar.polo unsigned int mask;
904 7ca8829b 2018-05-21 omar.polo bool res;
905 7ca8829b 2018-05-21 omar.polo for (int i = 0; i < number_of_screens; ++i) {
906 7ca8829b 2018-05-21 omar.polo root = XRootWindow(d, i);
907 7ca8829b 2018-05-21 omar.polo res = XQueryPointer(d, root, &r, &r, &root_x, &root_y, &win_x, &win_y, &mask);
908 7ca8829b 2018-05-21 omar.polo if (res) break;
910 7ca8829b 2018-05-21 omar.polo if (!res) {
911 7ca8829b 2018-05-21 omar.polo fprintf(stderr, "No mouse found.\n");
912 7ca8829b 2018-05-21 omar.polo root_x = 0;
913 7ca8829b 2018-05-21 omar.polo root_y = 0;
916 7ca8829b 2018-05-21 omar.polo // now find in which monitor the mice is on
917 f5e234d6 2018-05-18 omar.polo int monitors;
918 f5e234d6 2018-05-18 omar.polo XineramaScreenInfo *info = XineramaQueryScreens(d, &monitors);
919 7ca8829b 2018-05-21 omar.polo if (info) {
920 f5e234d6 2018-05-18 omar.polo for (int i = 0; i < monitors; ++i) {
921 7ca8829b 2018-05-21 omar.polo if (info[i].x_org <= root_x && root_x <= (info[i].x_org + info[i].width)
922 7ca8829b 2018-05-21 omar.polo && info[i].y_org <= root_y && root_y <= (info[i].y_org + info[i].height)) {
923 7ca8829b 2018-05-21 omar.polo offset_x = info[i].x_org;
924 7ca8829b 2018-05-21 omar.polo offset_y = info[i].y_org;
925 f5e234d6 2018-05-18 omar.polo d_width = info[i].width;
926 f5e234d6 2018-05-18 omar.polo d_height = info[i].height;
927 7ca8829b 2018-05-21 omar.polo break;
931 f5e234d6 2018-05-18 omar.polo XFree(info);
933 f5e234d6 2018-05-18 omar.polo #endif
935 f5e234d6 2018-05-18 omar.polo Colormap cmap = DefaultColormap(d, DefaultScreen(d));
936 f5e234d6 2018-05-18 omar.polo XColor p_fg, p_bg,
937 f5e234d6 2018-05-18 omar.polo compl_fg, compl_bg,
938 f5e234d6 2018-05-18 omar.polo compl_highlighted_fg, compl_highlighted_bg;
940 36a15a9f 2018-05-19 omar.polo bool horizontal_layout = true;
942 f5e234d6 2018-05-18 omar.polo // read resource
943 f5e234d6 2018-05-18 omar.polo XrmInitialize();
944 f5e234d6 2018-05-18 omar.polo char *xrm = XResourceManagerString(d);
945 347d23da 2018-05-19 omar.polo XrmDatabase xdb = nil;
946 f5e234d6 2018-05-18 omar.polo if (xrm != nil) {
947 347d23da 2018-05-19 omar.polo xdb = XrmGetStringDatabase(xrm);
948 f5e234d6 2018-05-18 omar.polo XrmValue value;
949 f5e234d6 2018-05-18 omar.polo char *datatype[20];
951 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.font", "*", datatype, &value) == true) {
952 f5e234d6 2018-05-18 omar.polo fontname = strdup(value.addr);
953 f5e234d6 2018-05-18 omar.polo check_allocation(fontname);
956 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no font defined, using %s\n", fontname);
958 36a15a9f 2018-05-19 omar.polo if (XrmGetResource(xdb, "MyMenu.layout", "*", datatype, &value) == true) {
959 36a15a9f 2018-05-19 omar.polo char *v = strdup(value.addr);
960 36a15a9f 2018-05-19 omar.polo check_allocation(v);
961 36a15a9f 2018-05-19 omar.polo horizontal_layout = !strcmp(v, "horizontal");
962 36a15a9f 2018-05-19 omar.polo free(v);
965 36a15a9f 2018-05-19 omar.polo fprintf(stderr, "no layout defined, using horizontal\n");
967 8758854a 2018-05-20 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt", "*", datatype, &value) == true) {
968 8758854a 2018-05-20 omar.polo free(ps1);
969 8758854a 2018-05-20 omar.polo ps1 = normalize_str(value.addr);
970 8758854a 2018-05-20 omar.polo } else
971 8758854a 2018-05-20 omar.polo fprintf(stderr, "no prompt defined, using \"%s\" as default\n", ps1);
973 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.width", "*", datatype, &value) == true)
974 25bf99d2 2018-05-22 omar.polo width = parse_int_with_percentage(value.addr, width, d_width);
976 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no width defined, using %d\n", width);
978 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.height", "*", datatype, &value) == true)
979 25bf99d2 2018-05-22 omar.polo height = parse_int_with_percentage(value.addr, height, d_height);
981 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no height defined, using %d\n", height);
983 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.x", "*", datatype, &value) == true)
984 f5e234d6 2018-05-18 omar.polo x = parse_int_with_middle(value.addr, x, d_width, width);
986 e5186d6b 2018-05-26 omar.polo fprintf(stderr, "no x defined, using %d\n", x);
988 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.y", "*", datatype, &value) == true)
989 f5e234d6 2018-05-18 omar.polo y = parse_int_with_middle(value.addr, y, d_height, height);
991 e5186d6b 2018-05-26 omar.polo fprintf(stderr, "no y defined, using %d\n", y);
993 e5186d6b 2018-05-26 omar.polo if (XrmGetResource(xdb, "MyMenu.padding", "*", datatype, &value) == true)
994 e5186d6b 2018-05-26 omar.polo padding = parse_integer(value.addr, padding);
996 26b1f485 2018-07-03 omar.polo fprintf(stderr, "no padding defined, using %d\n", padding);
998 f5e234d6 2018-05-18 omar.polo XColor tmp;
999 f5e234d6 2018-05-18 omar.polo // TODO: tmp needs to be free'd after every allocation?
1001 f5e234d6 2018-05-18 omar.polo // prompt
1002 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.foreground", "*", datatype, &value) == true)
1003 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &p_fg, &tmp);
1005 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &p_fg, &tmp);
1007 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.background", "*", datatype, &value) == true)
1008 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &p_bg, &tmp);
1010 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &p_bg, &tmp);
1012 f5e234d6 2018-05-18 omar.polo // completion
1013 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.foreground", "*", datatype, &value) == true)
1014 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_fg, &tmp);
1016 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_fg, &tmp);
1018 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.background", "*", datatype, &value) == true)
1019 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_bg, &tmp);
1021 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_bg, &tmp);
1023 f5e234d6 2018-05-18 omar.polo // completion highlighted
1024 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion_highlighted.foreground", "*", datatype, &value) == true)
1025 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_highlighted_fg, &tmp);
1027 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_highlighted_fg, &tmp);
1029 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion_highlighted.background", "*", datatype, &value) == true)
1030 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_highlighted_bg, &tmp);
1032 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_highlighted_bg, &tmp);
1033 f5e234d6 2018-05-18 omar.polo } else {
1034 f5e234d6 2018-05-18 omar.polo XColor tmp;
1035 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &p_fg, &tmp);
1036 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &p_bg, &tmp);
1037 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_fg, &tmp);
1038 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_bg, &tmp);
1039 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_highlighted_fg, &tmp);
1040 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_highlighted_bg, &tmp);
1043 f5e234d6 2018-05-18 omar.polo // load the font
1044 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
1045 c9a3bfaa 2018-05-21 omar.polo XftFont *font = XftFontOpenName(d, DefaultScreen(d), fontname);
1046 c9a3bfaa 2018-05-21 omar.polo #else
1047 347d23da 2018-05-19 omar.polo char **missing_charset_list;
1048 347d23da 2018-05-19 omar.polo int missing_charset_count;
1049 347d23da 2018-05-19 omar.polo XFontSet font = XCreateFontSet(d, fontname, &missing_charset_list, &missing_charset_count, nil);
1050 f5e234d6 2018-05-18 omar.polo if (font == nil) {
1051 347d23da 2018-05-19 omar.polo fprintf(stderr, "Unable to load the font(s) %s\n", fontname);
1052 347d23da 2018-05-19 omar.polo return EX_UNAVAILABLE;
1054 c9a3bfaa 2018-05-21 omar.polo #endif
1056 f5e234d6 2018-05-18 omar.polo // create the window
1057 f5e234d6 2018-05-18 omar.polo XSetWindowAttributes attr;
1058 55bd6842 2018-05-21 omar.polo attr.override_redirect = true;
1060 f5e234d6 2018-05-18 omar.polo Window w = XCreateWindow(d, // display
1061 f5e234d6 2018-05-18 omar.polo DefaultRootWindow(d), // parent
1062 f5e234d6 2018-05-18 omar.polo x + offset_x, y + offset_y, // x y
1063 f5e234d6 2018-05-18 omar.polo width, height, // w h
1064 f5e234d6 2018-05-18 omar.polo 0, // border width
1065 f5e234d6 2018-05-18 omar.polo DefaultDepth(d, DefaultScreen(d)), // depth
1066 f5e234d6 2018-05-18 omar.polo InputOutput, // class
1067 f5e234d6 2018-05-18 omar.polo DefaultVisual(d, DefaultScreen(d)), // visual
1068 55bd6842 2018-05-21 omar.polo CWOverrideRedirect, // value mask
1069 f5e234d6 2018-05-18 omar.polo &attr);
1071 f5e234d6 2018-05-18 omar.polo set_win_atoms_hints(d, w, width, height);
1073 f5e234d6 2018-05-18 omar.polo // we want some events
1074 c9a3bfaa 2018-05-21 omar.polo XSelectInput(d, w, StructureNotifyMask | KeyPressMask | KeymapStateMask);
1076 f5e234d6 2018-05-18 omar.polo // make the window appear on the screen
1077 f5e234d6 2018-05-18 omar.polo XMapWindow(d, w);
1079 f5e234d6 2018-05-18 omar.polo // wait for the MapNotify event (i.e. the event "window rendered")
1080 f5e234d6 2018-05-18 omar.polo for (;;) {
1081 f5e234d6 2018-05-18 omar.polo XEvent e;
1082 f5e234d6 2018-05-18 omar.polo XNextEvent(d, &e);
1083 f5e234d6 2018-05-18 omar.polo if (e.type == MapNotify)
1084 f5e234d6 2018-05-18 omar.polo break;
1087 f5e234d6 2018-05-18 omar.polo // get the *real* width & height after the window was rendered
1088 f5e234d6 2018-05-18 omar.polo get_wh(d, &w, &width, &height);
1090 f5e234d6 2018-05-18 omar.polo // grab keyboard
1091 f5e234d6 2018-05-18 omar.polo take_keyboard(d, w);
1093 f5e234d6 2018-05-18 omar.polo // Create some graphics contexts
1094 f5e234d6 2018-05-18 omar.polo XGCValues values;
1095 347d23da 2018-05-19 omar.polo /* values.font = font->fid; */
1097 f5e234d6 2018-05-18 omar.polo struct rendering r = {
1098 f5e234d6 2018-05-18 omar.polo .d = d,
1099 f5e234d6 2018-05-18 omar.polo .w = w,
1100 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
1101 c9a3bfaa 2018-05-21 omar.polo .font = font,
1102 c9a3bfaa 2018-05-21 omar.polo #else
1103 c9a3bfaa 2018-05-21 omar.polo .font = &font,
1104 c9a3bfaa 2018-05-21 omar.polo #endif
1105 347d23da 2018-05-19 omar.polo .prompt = XCreateGC(d, w, 0, &values),
1106 347d23da 2018-05-19 omar.polo .prompt_bg = XCreateGC(d, w, 0, &values),
1107 347d23da 2018-05-19 omar.polo .completion = XCreateGC(d, w, 0, &values),
1108 347d23da 2018-05-19 omar.polo .completion_bg = XCreateGC(d, w, 0, &values),
1109 347d23da 2018-05-19 omar.polo .completion_highlighted = XCreateGC(d, w, 0, &values),
1110 347d23da 2018-05-19 omar.polo .completion_highlighted_bg = XCreateGC(d, w, 0, &values),
1111 f5e234d6 2018-05-18 omar.polo .width = width,
1112 f5e234d6 2018-05-18 omar.polo .height = height,
1113 e5186d6b 2018-05-26 omar.polo .padding = padding,
1114 8758854a 2018-05-20 omar.polo .horizontal_layout = horizontal_layout,
1115 8758854a 2018-05-20 omar.polo .ps1 = ps1,
1116 8758854a 2018-05-20 omar.polo .ps1len = strlen(ps1)
1119 7ca8829b 2018-05-21 omar.polo #ifdef USE_XFT
1120 7ca8829b 2018-05-21 omar.polo r.xftdraw = XftDrawCreate(d, w, DefaultVisual(d, 0), DefaultColormap(d, 0));
1122 7ca8829b 2018-05-21 omar.polo // prompt
1123 7ca8829b 2018-05-21 omar.polo XRenderColor xrcolor;
1124 7ca8829b 2018-05-21 omar.polo xrcolor.red = p_fg.red;
1125 7ca8829b 2018-05-21 omar.polo xrcolor.green = p_fg.red;
1126 7ca8829b 2018-05-21 omar.polo xrcolor.blue = p_fg.red;
1127 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
1128 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_prompt);
1130 7ca8829b 2018-05-21 omar.polo // completion
1131 7ca8829b 2018-05-21 omar.polo xrcolor.red = compl_fg.red;
1132 7ca8829b 2018-05-21 omar.polo xrcolor.green = compl_fg.green;
1133 7ca8829b 2018-05-21 omar.polo xrcolor.blue = compl_fg.blue;
1134 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
1135 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_completion);
1137 7ca8829b 2018-05-21 omar.polo // completion highlighted
1138 7ca8829b 2018-05-21 omar.polo xrcolor.red = compl_highlighted_fg.red;
1139 7ca8829b 2018-05-21 omar.polo xrcolor.green = compl_highlighted_fg.green;
1140 7ca8829b 2018-05-21 omar.polo xrcolor.blue = compl_highlighted_fg.blue;
1141 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
1142 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_completion_highlighted);
1143 c9a3bfaa 2018-05-21 omar.polo #endif
1145 f5e234d6 2018-05-18 omar.polo // load the colors in our GCs
1146 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.prompt, p_fg.pixel);
1147 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.prompt_bg, p_bg.pixel);
1148 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion, compl_fg.pixel);
1149 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_bg, compl_bg.pixel);
1150 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_highlighted, compl_highlighted_fg.pixel);
1151 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_highlighted_bg, compl_highlighted_bg.pixel);
1153 347d23da 2018-05-19 omar.polo // open the X input method
1154 347d23da 2018-05-19 omar.polo XIM xim = XOpenIM(d, xdb, resname, resclass);
1155 347d23da 2018-05-19 omar.polo check_allocation(xim);
1157 347d23da 2018-05-19 omar.polo XIMStyles *xis = nil;
1158 347d23da 2018-05-19 omar.polo if (XGetIMValues(xim, XNQueryInputStyle, &xis, NULL) || !xis) {
1159 347d23da 2018-05-19 omar.polo fprintf(stderr, "Input Styles could not be retrieved\n");
1160 347d23da 2018-05-19 omar.polo return EX_UNAVAILABLE;
1163 347d23da 2018-05-19 omar.polo XIMStyle bestMatchStyle = 0;
1164 347d23da 2018-05-19 omar.polo for (int i = 0; i < xis->count_styles; ++i) {
1165 347d23da 2018-05-19 omar.polo XIMStyle ts = xis->supported_styles[i];
1166 347d23da 2018-05-19 omar.polo if (ts == (XIMPreeditNothing | XIMStatusNothing)) {
1167 347d23da 2018-05-19 omar.polo bestMatchStyle = ts;
1168 347d23da 2018-05-19 omar.polo break;
1171 347d23da 2018-05-19 omar.polo XFree(xis);
1173 347d23da 2018-05-19 omar.polo if (!bestMatchStyle) {
1174 347d23da 2018-05-19 omar.polo fprintf(stderr, "No matching input style could be determined\n");
1177 347d23da 2018-05-19 omar.polo XIC xic = XCreateIC(xim, XNInputStyle, bestMatchStyle, XNClientWindow, w, XNFocusWindow, w, NULL);
1178 347d23da 2018-05-19 omar.polo check_allocation(xic);
1180 f5e234d6 2018-05-18 omar.polo // draw the window for the first time
1181 f5e234d6 2018-05-18 omar.polo draw(&r, text, cs);
1183 f5e234d6 2018-05-18 omar.polo // main loop
1184 f5e234d6 2018-05-18 omar.polo while (status == LOOPING) {
1185 f5e234d6 2018-05-18 omar.polo XEvent e;
1186 f5e234d6 2018-05-18 omar.polo XNextEvent(d, &e);
1188 347d23da 2018-05-19 omar.polo if (XFilterEvent(&e, w))
1189 347d23da 2018-05-19 omar.polo continue;
1191 f5e234d6 2018-05-18 omar.polo switch (e.type) {
1192 449f27a6 2018-07-06 omar.polo case KeymapNotify:
1193 b10f01c1 2018-07-06 omar.polo XRefreshKeyboardMapping(&e.xmapping);
1194 b10f01c1 2018-07-06 omar.polo break;
1196 449f27a6 2018-07-06 omar.polo case KeyPress: {
1197 b10f01c1 2018-07-06 omar.polo XKeyPressedEvent *ev = (XKeyPressedEvent*)&e;
1199 b10f01c1 2018-07-06 omar.polo char *input;
1200 b10f01c1 2018-07-06 omar.polo switch (parse_event(d, ev, xic, &input)) {
1201 b10f01c1 2018-07-06 omar.polo case EXIT:
1202 b10f01c1 2018-07-06 omar.polo status = ERR;
1203 b10f01c1 2018-07-06 omar.polo break;
1205 b10f01c1 2018-07-06 omar.polo case CONFIRM:
1206 b10f01c1 2018-07-06 omar.polo status = OK;
1208 b10f01c1 2018-07-06 omar.polo // if first_selected is active and the first completion is
1209 b10f01c1 2018-07-06 omar.polo // active be sure to 'expand' the text to match the selection
1210 b10f01c1 2018-07-06 omar.polo if (first_selected && cs && cs->selected) {
1211 b10f01c1 2018-07-06 omar.polo free(text);
1212 b10f01c1 2018-07-06 omar.polo text = strdup(cs->completion);
1213 b10f01c1 2018-07-06 omar.polo if (text == nil) {
1214 b10f01c1 2018-07-06 omar.polo fprintf(stderr, "Memory allocation error");
1215 b10f01c1 2018-07-06 omar.polo status = ERR;
1217 b10f01c1 2018-07-06 omar.polo textlen = strlen(text);
1219 b10f01c1 2018-07-06 omar.polo break;
1221 b10f01c1 2018-07-06 omar.polo case PREV_COMPL: {
1222 b10f01c1 2018-07-06 omar.polo complete(cs, first_selected, true, &text, &textlen, &status);
1223 b10f01c1 2018-07-06 omar.polo break;
1226 b10f01c1 2018-07-06 omar.polo case NEXT_COMPL: {
1227 b10f01c1 2018-07-06 omar.polo complete(cs, first_selected, false, &text, &textlen, &status);
1228 b10f01c1 2018-07-06 omar.polo break;
1231 b10f01c1 2018-07-06 omar.polo case DEL_CHAR:
1232 b10f01c1 2018-07-06 omar.polo popc(text, textlen);
1233 b10f01c1 2018-07-06 omar.polo cs = update_completions(cs, text, lines, first_selected);
1234 b10f01c1 2018-07-06 omar.polo break;
1236 b10f01c1 2018-07-06 omar.polo case DEL_WORD: {
1237 b10f01c1 2018-07-06 omar.polo // `textlen` is the lenght of the allocated string, not the
1238 b10f01c1 2018-07-06 omar.polo // lenght of the ACTUAL string
1239 b10f01c1 2018-07-06 omar.polo int p = strlen(text) -1;
1240 b10f01c1 2018-07-06 omar.polo if (p > 0) { // delete the current char
1241 b10f01c1 2018-07-06 omar.polo text[p] = 0;
1245 b10f01c1 2018-07-06 omar.polo // erase the alphanumeric char
1246 b10f01c1 2018-07-06 omar.polo while (p >= 0 && isalnum(text[p])) {
1247 b10f01c1 2018-07-06 omar.polo text[p] = 0;
1251 b10f01c1 2018-07-06 omar.polo // erase also trailing white spaces
1252 b10f01c1 2018-07-06 omar.polo while (p >= 0 && isspace(text[p])) {
1253 b10f01c1 2018-07-06 omar.polo text[p] = 0;
1256 b10f01c1 2018-07-06 omar.polo cs = update_completions(cs, text, lines, first_selected);
1257 b10f01c1 2018-07-06 omar.polo break;
1260 b10f01c1 2018-07-06 omar.polo case DEL_LINE: {
1261 b10f01c1 2018-07-06 omar.polo for (int i = 0; i < textlen; ++i)
1262 b10f01c1 2018-07-06 omar.polo text[i] = 0;
1263 b10f01c1 2018-07-06 omar.polo cs = update_completions(cs, text, lines, first_selected);
1264 b10f01c1 2018-07-06 omar.polo break;
1267 b10f01c1 2018-07-06 omar.polo case ADD_CHAR: {
1268 b10f01c1 2018-07-06 omar.polo int str_len = strlen(input);
1269 b10f01c1 2018-07-06 omar.polo for (int i = 0; i < str_len; ++i) {
1270 b10f01c1 2018-07-06 omar.polo textlen = pushc(&text, textlen, input[i]);
1271 b10f01c1 2018-07-06 omar.polo if (textlen == -1) {
1272 b10f01c1 2018-07-06 omar.polo fprintf(stderr, "Memory allocation error\n");
1273 b10f01c1 2018-07-06 omar.polo status = ERR;
1274 b10f01c1 2018-07-06 omar.polo break;
1276 b10f01c1 2018-07-06 omar.polo cs = update_completions(cs, text, lines, first_selected);
1277 b10f01c1 2018-07-06 omar.polo free(input);
1279 b10f01c1 2018-07-06 omar.polo break;
1282 6254fed8 2018-07-06 omar.polo case TOGGLE_FIRST_SELECTED:
1283 6254fed8 2018-07-06 omar.polo first_selected = !first_selected;
1284 6254fed8 2018-07-06 omar.polo if (cs)
1285 6254fed8 2018-07-06 omar.polo cs->selected = !cs->selected;
1286 6254fed8 2018-07-06 omar.polo break;
1290 449f27a6 2018-07-06 omar.polo default:
1291 b10f01c1 2018-07-06 omar.polo fprintf(stderr, "Unknown event %d\n", e.type);
1294 14291de3 2018-07-03 omar.polo draw(&r, text, cs);
1297 f5e234d6 2018-05-18 omar.polo if (status == OK)
1298 f5e234d6 2018-05-18 omar.polo printf("%s\n", text);
1300 e9f0b467 2018-06-07 omar.polo return exit_cleanup(&r, ps1, fontname, text, lines, cs, status == OK ? 0 : 1);