Blame


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>
11 f5e234d6 2018-05-18 omar.polo
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>
17 f5e234d6 2018-05-18 omar.polo
18 f5e234d6 2018-05-18 omar.polo #ifdef USE_XINERAMA
19 f5e234d6 2018-05-18 omar.polo # include <X11/extensions/Xinerama.h>
20 f5e234d6 2018-05-18 omar.polo #endif
21 f5e234d6 2018-05-18 omar.polo
22 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
23 c9a3bfaa 2018-05-21 omar.polo # include <X11/Xft/Xft.h>
24 b500fe86 2018-06-07 omar.polo #endif
25 b500fe86 2018-06-07 omar.polo
26 b500fe86 2018-06-07 omar.polo #ifndef VERSION
27 b500fe86 2018-06-07 omar.polo # define VERSION "unknown"
28 c9a3bfaa 2018-05-21 omar.polo #endif
29 c9a3bfaa 2018-05-21 omar.polo
30 f5e234d6 2018-05-18 omar.polo #define nil NULL
31 347d23da 2018-05-19 omar.polo #define resname "MyMenu"
32 347d23da 2018-05-19 omar.polo #define resclass "mymenu"
33 f5e234d6 2018-05-18 omar.polo
34 9e94fcbe 2018-05-22 omar.polo #ifdef USE_XFT
35 9e94fcbe 2018-05-22 omar.polo # define default_fontname "monospace"
36 9e94fcbe 2018-05-22 omar.polo #else
37 9e94fcbe 2018-05-22 omar.polo # define default_fontname "fixed"
38 9e94fcbe 2018-05-22 omar.polo #endif
39 9e94fcbe 2018-05-22 omar.polo
40 9e94fcbe 2018-05-22 omar.polo #define MIN(a, b) ((a) < (b) ? (a) : (b))
41 f5e234d6 2018-05-18 omar.polo #define MAX(a, b) ((a) > (b) ? (a) : (b))
42 9e94fcbe 2018-05-22 omar.polo
43 9e94fcbe 2018-05-22 omar.polo // If we don't have it or we don't want an "ignore case" completion
44 9e94fcbe 2018-05-22 omar.polo // style, fall back to `strstr(3)`
45 9e94fcbe 2018-05-22 omar.polo #ifndef USE_STRCASESTR
46 fa5a6135 2018-05-25 omar.polo # define strcasestr strstr
47 9e94fcbe 2018-05-22 omar.polo #endif
48 f5e234d6 2018-05-18 omar.polo
49 95b27a5e 2018-05-19 omar.polo #define INITIAL_ITEMS 64
50 f5e234d6 2018-05-18 omar.polo
51 f5e234d6 2018-05-18 omar.polo #define cannot_allocate_memory { \
52 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not allocate memory\n"); \
53 9e94fcbe 2018-05-22 omar.polo abort(); \
54 f5e234d6 2018-05-18 omar.polo }
55 f5e234d6 2018-05-18 omar.polo
56 f5e234d6 2018-05-18 omar.polo #define check_allocation(a) { \
57 f5e234d6 2018-05-18 omar.polo if (a == nil) \
58 f5e234d6 2018-05-18 omar.polo cannot_allocate_memory; \
59 f5e234d6 2018-05-18 omar.polo }
60 f5e234d6 2018-05-18 omar.polo
61 f5e234d6 2018-05-18 omar.polo enum state {LOOPING, OK, ERR};
62 f5e234d6 2018-05-18 omar.polo
63 c9a3bfaa 2018-05-21 omar.polo enum text_type {PROMPT, COMPL, COMPL_HIGH};
64 c9a3bfaa 2018-05-21 omar.polo
65 2128b469 2018-06-30 omar.polo enum action {
66 2128b469 2018-06-30 omar.polo EXIT,
67 2128b469 2018-06-30 omar.polo CONFIRM,
68 2128b469 2018-06-30 omar.polo NEXT_COMPL,
69 2128b469 2018-06-30 omar.polo PREV_COMPL,
70 2128b469 2018-06-30 omar.polo DEL_CHAR,
71 2128b469 2018-06-30 omar.polo DEL_WORD,
72 2128b469 2018-06-30 omar.polo DEL_LINE,
73 2128b469 2018-06-30 omar.polo ADD_CHAR
74 2128b469 2018-06-30 omar.polo };
75 2128b469 2018-06-30 omar.polo
76 f5e234d6 2018-05-18 omar.polo struct rendering {
77 f5e234d6 2018-05-18 omar.polo Display *d;
78 f5e234d6 2018-05-18 omar.polo Window w;
79 e5186d6b 2018-05-26 omar.polo int width;
80 e5186d6b 2018-05-26 omar.polo int height;
81 e5186d6b 2018-05-26 omar.polo int padding;
82 e5186d6b 2018-05-26 omar.polo bool horizontal_layout;
83 e5186d6b 2018-05-26 omar.polo char *ps1;
84 e5186d6b 2018-05-26 omar.polo int ps1len;
85 f5e234d6 2018-05-18 omar.polo GC prompt;
86 f5e234d6 2018-05-18 omar.polo GC prompt_bg;
87 f5e234d6 2018-05-18 omar.polo GC completion;
88 f5e234d6 2018-05-18 omar.polo GC completion_bg;
89 f5e234d6 2018-05-18 omar.polo GC completion_highlighted;
90 f5e234d6 2018-05-18 omar.polo GC completion_highlighted_bg;
91 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
92 e5186d6b 2018-05-26 omar.polo XftFont *font;
93 c9a3bfaa 2018-05-21 omar.polo XftDraw *xftdraw;
94 c9a3bfaa 2018-05-21 omar.polo XftColor xft_prompt;
95 c9a3bfaa 2018-05-21 omar.polo XftColor xft_completion;
96 c9a3bfaa 2018-05-21 omar.polo XftColor xft_completion_highlighted;
97 c9a3bfaa 2018-05-21 omar.polo #else
98 c9a3bfaa 2018-05-21 omar.polo XFontSet *font;
99 c9a3bfaa 2018-05-21 omar.polo #endif
100 f5e234d6 2018-05-18 omar.polo };
101 f5e234d6 2018-05-18 omar.polo
102 f5e234d6 2018-05-18 omar.polo struct completions {
103 f5e234d6 2018-05-18 omar.polo char *completion;
104 f5e234d6 2018-05-18 omar.polo bool selected;
105 f5e234d6 2018-05-18 omar.polo struct completions *next;
106 f5e234d6 2018-05-18 omar.polo };
107 f5e234d6 2018-05-18 omar.polo
108 2128b469 2018-06-30 omar.polo // return a newly allocated (and empty) completion list
109 f5e234d6 2018-05-18 omar.polo struct completions *compl_new() {
110 f5e234d6 2018-05-18 omar.polo struct completions *c = malloc(sizeof(struct completions));
111 f5e234d6 2018-05-18 omar.polo
112 f5e234d6 2018-05-18 omar.polo if (c == nil)
113 f5e234d6 2018-05-18 omar.polo return c;
114 f5e234d6 2018-05-18 omar.polo
115 f5e234d6 2018-05-18 omar.polo c->completion = nil;
116 f5e234d6 2018-05-18 omar.polo c->selected = false;
117 f5e234d6 2018-05-18 omar.polo c->next = nil;
118 f5e234d6 2018-05-18 omar.polo return c;
119 f5e234d6 2018-05-18 omar.polo }
120 f5e234d6 2018-05-18 omar.polo
121 2128b469 2018-06-30 omar.polo // delete ONLY the given completion (i.e. does not free c->next...)
122 f5e234d6 2018-05-18 omar.polo void compl_delete(struct completions *c) {
123 f5e234d6 2018-05-18 omar.polo free(c);
124 f5e234d6 2018-05-18 omar.polo }
125 f5e234d6 2018-05-18 omar.polo
126 2128b469 2018-06-30 omar.polo // delete all completion
127 3997bf41 2018-05-25 omar.polo void compl_delete_rec(struct completions *c) {
128 3997bf41 2018-05-25 omar.polo while (c != nil) {
129 3997bf41 2018-05-25 omar.polo struct completions *t = c->next;
130 3997bf41 2018-05-25 omar.polo free(c);
131 3997bf41 2018-05-25 omar.polo c = t;
132 3997bf41 2018-05-25 omar.polo }
133 3997bf41 2018-05-25 omar.polo }
134 3997bf41 2018-05-25 omar.polo
135 2128b469 2018-06-30 omar.polo // given a completion list, select the next completion
136 347d23da 2018-05-19 omar.polo struct completions *compl_select_next(struct completions *c, bool n) {
137 347d23da 2018-05-19 omar.polo if (c == nil)
138 347d23da 2018-05-19 omar.polo return nil;
139 347d23da 2018-05-19 omar.polo if (n) {
140 347d23da 2018-05-19 omar.polo c->selected = true;
141 347d23da 2018-05-19 omar.polo return c;
142 347d23da 2018-05-19 omar.polo }
143 347d23da 2018-05-19 omar.polo
144 347d23da 2018-05-19 omar.polo struct completions *orig = c;
145 347d23da 2018-05-19 omar.polo while (c != nil) {
146 347d23da 2018-05-19 omar.polo if (c->selected) {
147 347d23da 2018-05-19 omar.polo c->selected = false;
148 347d23da 2018-05-19 omar.polo if (c->next != nil) {
149 347d23da 2018-05-19 omar.polo // the current one is selected and the next one exists
150 347d23da 2018-05-19 omar.polo c->next->selected = true;
151 347d23da 2018-05-19 omar.polo return c->next;
152 347d23da 2018-05-19 omar.polo } else {
153 347d23da 2018-05-19 omar.polo // the current one is selected and the next one is nill,
154 347d23da 2018-05-19 omar.polo // select the first one
155 347d23da 2018-05-19 omar.polo orig->selected = true;
156 347d23da 2018-05-19 omar.polo return orig;
157 347d23da 2018-05-19 omar.polo }
158 347d23da 2018-05-19 omar.polo }
159 347d23da 2018-05-19 omar.polo c = c->next;
160 347d23da 2018-05-19 omar.polo }
161 347d23da 2018-05-19 omar.polo return nil;
162 347d23da 2018-05-19 omar.polo }
163 347d23da 2018-05-19 omar.polo
164 2128b469 2018-06-30 omar.polo // given a completion list, select the previous
165 347d23da 2018-05-19 omar.polo struct completions *compl_select_prev(struct completions *c, bool n) {
166 347d23da 2018-05-19 omar.polo if (c == nil)
167 347d23da 2018-05-19 omar.polo return nil;
168 347d23da 2018-05-19 omar.polo
169 347d23da 2018-05-19 omar.polo struct completions *cc = c;
170 347d23da 2018-05-19 omar.polo
171 4c3b1ef0 2018-05-26 omar.polo if (n || c->selected) { // select the last one
172 4c3b1ef0 2018-05-26 omar.polo c->selected = false;
173 347d23da 2018-05-19 omar.polo while (cc != nil) {
174 347d23da 2018-05-19 omar.polo if (cc->next == nil) {
175 347d23da 2018-05-19 omar.polo cc->selected = true;
176 347d23da 2018-05-19 omar.polo return cc;
177 347d23da 2018-05-19 omar.polo }
178 347d23da 2018-05-19 omar.polo cc = cc->next;
179 347d23da 2018-05-19 omar.polo }
180 4c3b1ef0 2018-05-26 omar.polo }
181 347d23da 2018-05-19 omar.polo else // select the previous one
182 347d23da 2018-05-19 omar.polo while (cc != nil) {
183 347d23da 2018-05-19 omar.polo if (cc->next != nil && cc->next->selected) {
184 347d23da 2018-05-19 omar.polo cc->next->selected = false;
185 347d23da 2018-05-19 omar.polo cc->selected = true;
186 347d23da 2018-05-19 omar.polo return cc;
187 347d23da 2018-05-19 omar.polo }
188 347d23da 2018-05-19 omar.polo cc = cc->next;
189 347d23da 2018-05-19 omar.polo }
190 7ca8829b 2018-05-21 omar.polo
191 347d23da 2018-05-19 omar.polo return nil;
192 347d23da 2018-05-19 omar.polo }
193 347d23da 2018-05-19 omar.polo
194 2128b469 2018-06-30 omar.polo // create a completion list from a text and the list of possible completions
195 f5e234d6 2018-05-18 omar.polo struct completions *filter(char *text, char **lines) {
196 f5e234d6 2018-05-18 omar.polo int i = 0;
197 f5e234d6 2018-05-18 omar.polo struct completions *root = compl_new();
198 f5e234d6 2018-05-18 omar.polo struct completions *c = root;
199 3997bf41 2018-05-25 omar.polo if (c == nil)
200 3997bf41 2018-05-25 omar.polo return nil;
201 f5e234d6 2018-05-18 omar.polo
202 f5e234d6 2018-05-18 omar.polo for (;;) {
203 f5e234d6 2018-05-18 omar.polo char *l = lines[i];
204 f5e234d6 2018-05-18 omar.polo if (l == nil)
205 f5e234d6 2018-05-18 omar.polo break;
206 f5e234d6 2018-05-18 omar.polo
207 9e94fcbe 2018-05-22 omar.polo if (strcasestr(l, text) != nil) {
208 f5e234d6 2018-05-18 omar.polo c->next = compl_new();
209 f5e234d6 2018-05-18 omar.polo c = c->next;
210 3997bf41 2018-05-25 omar.polo if (c == nil) {
211 3997bf41 2018-05-25 omar.polo compl_delete_rec(root);
212 3997bf41 2018-05-25 omar.polo return nil;
213 3997bf41 2018-05-25 omar.polo }
214 f5e234d6 2018-05-18 omar.polo c->completion = l;
215 f5e234d6 2018-05-18 omar.polo }
216 f5e234d6 2018-05-18 omar.polo
217 f5e234d6 2018-05-18 omar.polo ++i;
218 f5e234d6 2018-05-18 omar.polo }
219 f5e234d6 2018-05-18 omar.polo
220 f5e234d6 2018-05-18 omar.polo struct completions *r = root->next;
221 f5e234d6 2018-05-18 omar.polo compl_delete(root);
222 f5e234d6 2018-05-18 omar.polo return r;
223 f5e234d6 2018-05-18 omar.polo }
224 f5e234d6 2018-05-18 omar.polo
225 2128b469 2018-06-30 omar.polo // update the given completion, that is: clean the old cs & generate a new one.
226 2128b469 2018-06-30 omar.polo struct completions *update_completions(struct completions *cs, char *text, char **lines, bool first_selected) {
227 2128b469 2018-06-30 omar.polo compl_delete_rec(cs);
228 2128b469 2018-06-30 omar.polo cs = filter(text, lines);
229 2128b469 2018-06-30 omar.polo if (first_selected && cs != nil)
230 2128b469 2018-06-30 omar.polo cs->selected = true;
231 2128b469 2018-06-30 omar.polo return cs;
232 2128b469 2018-06-30 omar.polo }
233 2128b469 2018-06-30 omar.polo
234 2128b469 2018-06-30 omar.polo // select the next, or the previous, selection and update some
235 2128b469 2018-06-30 omar.polo // state. `nothing_selected' will be updated if a new completion is
236 2128b469 2018-06-30 omar.polo // marked as selected, `text' will be updated with the text of the
237 2128b469 2018-06-30 omar.polo // completion and `textlen' with the new lenght of `text'. If the
238 2128b469 2018-06-30 omar.polo // memory cannot be allocated, `status' will be set to `ERR'.
239 2128b469 2018-06-30 omar.polo void complete(struct completions *cs, bool *nothing_selected, bool p, char **text, int *textlen, enum state *status) {
240 2128b469 2018-06-30 omar.polo struct completions *n = p
241 2128b469 2018-06-30 omar.polo ? compl_select_prev(cs, *nothing_selected)
242 2128b469 2018-06-30 omar.polo : compl_select_next(cs, *nothing_selected);
243 2128b469 2018-06-30 omar.polo
244 2128b469 2018-06-30 omar.polo if (n != nil) {
245 2128b469 2018-06-30 omar.polo *nothing_selected = false;
246 2128b469 2018-06-30 omar.polo free(*text);
247 2128b469 2018-06-30 omar.polo *text = strdup(n->completion);
248 2128b469 2018-06-30 omar.polo if (text == nil) {
249 2128b469 2018-06-30 omar.polo fprintf(stderr, "Memory allocation error!\n");
250 2128b469 2018-06-30 omar.polo *status = ERR;
251 2128b469 2018-06-30 omar.polo return;
252 2128b469 2018-06-30 omar.polo }
253 2128b469 2018-06-30 omar.polo *textlen = strlen(*text);
254 2128b469 2018-06-30 omar.polo }
255 2128b469 2018-06-30 omar.polo }
256 2128b469 2018-06-30 omar.polo
257 f5e234d6 2018-05-18 omar.polo // push the character c at the end of the string pointed by p
258 f5e234d6 2018-05-18 omar.polo int pushc(char **p, int maxlen, char c) {
259 f5e234d6 2018-05-18 omar.polo int len = strnlen(*p, maxlen);
260 f5e234d6 2018-05-18 omar.polo
261 f5e234d6 2018-05-18 omar.polo if (!(len < maxlen -2)) {
262 f5e234d6 2018-05-18 omar.polo maxlen += maxlen >> 1;
263 f5e234d6 2018-05-18 omar.polo char *newptr = realloc(*p, maxlen);
264 f5e234d6 2018-05-18 omar.polo if (newptr == nil) { // bad!
265 f5e234d6 2018-05-18 omar.polo return -1;
266 f5e234d6 2018-05-18 omar.polo }
267 f5e234d6 2018-05-18 omar.polo *p = newptr;
268 f5e234d6 2018-05-18 omar.polo }
269 f5e234d6 2018-05-18 omar.polo
270 f5e234d6 2018-05-18 omar.polo (*p)[len] = c;
271 f5e234d6 2018-05-18 omar.polo (*p)[len+1] = '\0';
272 f5e234d6 2018-05-18 omar.polo return maxlen;
273 f5e234d6 2018-05-18 omar.polo }
274 f5e234d6 2018-05-18 omar.polo
275 2128b469 2018-06-30 omar.polo // return the number of character
276 347d23da 2018-05-19 omar.polo int utf8strnlen(char *s, int maxlen) {
277 347d23da 2018-05-19 omar.polo int len = 0;
278 347d23da 2018-05-19 omar.polo while (*s && maxlen > 0) {
279 347d23da 2018-05-19 omar.polo len += (*s++ & 0xc0) != 0x80;
280 347d23da 2018-05-19 omar.polo maxlen--;
281 347d23da 2018-05-19 omar.polo }
282 347d23da 2018-05-19 omar.polo return len;
283 347d23da 2018-05-19 omar.polo }
284 347d23da 2018-05-19 omar.polo
285 347d23da 2018-05-19 omar.polo // remove the last *glyph* from the *utf8* string!
286 347d23da 2018-05-19 omar.polo // this is different from just setting the last byte to 0 (in some
287 347d23da 2018-05-19 omar.polo // cases ofc). The actual implementation is quite inefficient because
288 2128b469 2018-06-30 omar.polo // it remove the last byte until the number of glyphs doesn't change
289 f5e234d6 2018-05-18 omar.polo void popc(char *p, int maxlen) {
290 f5e234d6 2018-05-18 omar.polo int len = strnlen(p, maxlen);
291 347d23da 2018-05-19 omar.polo
292 347d23da 2018-05-19 omar.polo if (len == 0)
293 347d23da 2018-05-19 omar.polo return;
294 347d23da 2018-05-19 omar.polo
295 347d23da 2018-05-19 omar.polo int ulen = utf8strnlen(p, maxlen);
296 347d23da 2018-05-19 omar.polo while (len > 0 && utf8strnlen(p, maxlen) == ulen) {
297 347d23da 2018-05-19 omar.polo len--;
298 347d23da 2018-05-19 omar.polo p[len] = 0;
299 347d23da 2018-05-19 omar.polo }
300 f5e234d6 2018-05-18 omar.polo }
301 f5e234d6 2018-05-18 omar.polo
302 8758854a 2018-05-20 omar.polo // If the string is surrounded by quotes (`"`) remove them and replace
303 8758854a 2018-05-20 omar.polo // every `\"` in the string with `"`
304 8758854a 2018-05-20 omar.polo char *normalize_str(const char *str) {
305 8758854a 2018-05-20 omar.polo int len = strlen(str);
306 8758854a 2018-05-20 omar.polo if (len == 0)
307 8758854a 2018-05-20 omar.polo return nil;
308 8758854a 2018-05-20 omar.polo
309 8758854a 2018-05-20 omar.polo char *s = calloc(len, sizeof(char));
310 8758854a 2018-05-20 omar.polo check_allocation(s);
311 8758854a 2018-05-20 omar.polo int p = 0;
312 8758854a 2018-05-20 omar.polo while (*str) {
313 8758854a 2018-05-20 omar.polo char c = *str;
314 8758854a 2018-05-20 omar.polo if (*str == '\\') {
315 8758854a 2018-05-20 omar.polo if (*(str + 1)) {
316 8758854a 2018-05-20 omar.polo s[p] = *(str + 1);
317 8758854a 2018-05-20 omar.polo p++;
318 8758854a 2018-05-20 omar.polo str += 2; // skip this and the next char
319 8758854a 2018-05-20 omar.polo continue;
320 8758854a 2018-05-20 omar.polo } else {
321 8758854a 2018-05-20 omar.polo break;
322 8758854a 2018-05-20 omar.polo }
323 8758854a 2018-05-20 omar.polo }
324 8758854a 2018-05-20 omar.polo if (c == '"') {
325 8758854a 2018-05-20 omar.polo str++; // skip only this char
326 8758854a 2018-05-20 omar.polo continue;
327 8758854a 2018-05-20 omar.polo }
328 8758854a 2018-05-20 omar.polo s[p] = c;
329 8758854a 2018-05-20 omar.polo p++;
330 8758854a 2018-05-20 omar.polo str++;
331 8758854a 2018-05-20 omar.polo }
332 8758854a 2018-05-20 omar.polo return s;
333 8758854a 2018-05-20 omar.polo }
334 8758854a 2018-05-20 omar.polo
335 f5e234d6 2018-05-18 omar.polo // read an arbitrary long line from stdin and return a pointer to it
336 f5e234d6 2018-05-18 omar.polo // TODO: resize the allocated memory to exactly fit the string once
337 f5e234d6 2018-05-18 omar.polo // read?
338 f5e234d6 2018-05-18 omar.polo char *readline(bool *eof) {
339 f5e234d6 2018-05-18 omar.polo int maxlen = 8;
340 f5e234d6 2018-05-18 omar.polo char *str = calloc(maxlen, sizeof(char));
341 f5e234d6 2018-05-18 omar.polo if (str == nil) {
342 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Cannot allocate memory!\n");
343 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
344 f5e234d6 2018-05-18 omar.polo }
345 f5e234d6 2018-05-18 omar.polo
346 f5e234d6 2018-05-18 omar.polo int c;
347 f5e234d6 2018-05-18 omar.polo while((c = getchar()) != EOF) {
348 f5e234d6 2018-05-18 omar.polo if (c == '\n')
349 f5e234d6 2018-05-18 omar.polo return str;
350 f5e234d6 2018-05-18 omar.polo else
351 f5e234d6 2018-05-18 omar.polo maxlen = pushc(&str, maxlen, c);
352 f5e234d6 2018-05-18 omar.polo
353 f5e234d6 2018-05-18 omar.polo if (maxlen == -1) {
354 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Cannot allocate memory!\n");
355 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
356 f5e234d6 2018-05-18 omar.polo }
357 f5e234d6 2018-05-18 omar.polo }
358 f5e234d6 2018-05-18 omar.polo *eof = true;
359 f5e234d6 2018-05-18 omar.polo return str;
360 f5e234d6 2018-05-18 omar.polo }
361 f5e234d6 2018-05-18 omar.polo
362 e9f0b467 2018-06-07 omar.polo // read an arbitrary amount of text until an EOF and store it in
363 e9f0b467 2018-06-07 omar.polo // lns. `items` is the capacity of lns. It may increase lns with
364 e9f0b467 2018-06-07 omar.polo // `realloc(3)` to store more line. Return the number of lines
365 e9f0b467 2018-06-07 omar.polo // read. The last item will always be a NULL pointer. It ignore the
366 e9f0b467 2018-06-07 omar.polo // "null" (empty) lines
367 95b27a5e 2018-05-19 omar.polo int readlines (char ***lns, int items) {
368 f5e234d6 2018-05-18 omar.polo bool finished = false;
369 f5e234d6 2018-05-18 omar.polo int n = 0;
370 95b27a5e 2018-05-19 omar.polo char **lines = *lns;
371 95b27a5e 2018-05-19 omar.polo while (true) {
372 f5e234d6 2018-05-18 omar.polo lines[n] = readline(&finished);
373 f5e234d6 2018-05-18 omar.polo
374 6da98882 2018-05-20 omar.polo if (strlen(lines[n]) == 0 || lines[n][0] == '\n') {
375 6da98882 2018-05-20 omar.polo free(lines[n]);
376 f5e234d6 2018-05-18 omar.polo --n; // forget about this line
377 6da98882 2018-05-20 omar.polo }
378 f5e234d6 2018-05-18 omar.polo
379 f5e234d6 2018-05-18 omar.polo if (finished)
380 f5e234d6 2018-05-18 omar.polo break;
381 f5e234d6 2018-05-18 omar.polo
382 f5e234d6 2018-05-18 omar.polo ++n;
383 95b27a5e 2018-05-19 omar.polo
384 95b27a5e 2018-05-19 omar.polo if (n == items - 1) {
385 95b27a5e 2018-05-19 omar.polo items += items >>1;
386 95b27a5e 2018-05-19 omar.polo char **l = realloc(lines, sizeof(char*) * items);
387 95b27a5e 2018-05-19 omar.polo check_allocation(l);
388 95b27a5e 2018-05-19 omar.polo *lns = l;
389 95b27a5e 2018-05-19 omar.polo lines = l;
390 95b27a5e 2018-05-19 omar.polo }
391 f5e234d6 2018-05-18 omar.polo }
392 95b27a5e 2018-05-19 omar.polo
393 f5e234d6 2018-05-18 omar.polo n++;
394 f5e234d6 2018-05-18 omar.polo lines[n] = nil;
395 95b27a5e 2018-05-19 omar.polo return items;
396 c9a3bfaa 2018-05-21 omar.polo }
397 c9a3bfaa 2018-05-21 omar.polo
398 25bf99d2 2018-05-22 omar.polo // Compute the dimension of the string str once rendered, return the
399 25bf99d2 2018-05-22 omar.polo // width and save the width and the height in ret_width and ret_height
400 c9a3bfaa 2018-05-21 omar.polo int text_extents(char *str, int len, struct rendering *r, int *ret_width, int *ret_height) {
401 c9a3bfaa 2018-05-21 omar.polo int height;
402 c9a3bfaa 2018-05-21 omar.polo int width;
403 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
404 c9a3bfaa 2018-05-21 omar.polo XGlyphInfo gi;
405 c9a3bfaa 2018-05-21 omar.polo XftTextExtentsUtf8(r->d, r->font, str, len, &gi);
406 25bf99d2 2018-05-22 omar.polo /* height = gi.height; */
407 e5186d6b 2018-05-26 omar.polo /* height = (gi.height + (r->font->ascent - r->font->descent)/2) / 2; */
408 e5186d6b 2018-05-26 omar.polo /* height = (r->font->ascent - r->font->descent)/2 + gi.height*2; */
409 e5186d6b 2018-05-26 omar.polo height = r->font->ascent - r->font->descent;
410 c9a3bfaa 2018-05-21 omar.polo width = gi.width - gi.x;
411 c9a3bfaa 2018-05-21 omar.polo #else
412 c9a3bfaa 2018-05-21 omar.polo XRectangle rect;
413 c9a3bfaa 2018-05-21 omar.polo XmbTextExtents(*r->font, str, len, nil, &rect);
414 c9a3bfaa 2018-05-21 omar.polo height = rect.height;
415 c9a3bfaa 2018-05-21 omar.polo width = rect.width;
416 c9a3bfaa 2018-05-21 omar.polo #endif
417 c9a3bfaa 2018-05-21 omar.polo if (ret_width != nil) *ret_width = width;
418 c9a3bfaa 2018-05-21 omar.polo if (ret_height != nil) *ret_height = height;
419 c9a3bfaa 2018-05-21 omar.polo return width;
420 c9a3bfaa 2018-05-21 omar.polo }
421 c9a3bfaa 2018-05-21 omar.polo
422 25bf99d2 2018-05-22 omar.polo // Draw the string str
423 c9a3bfaa 2018-05-21 omar.polo void draw_string(char *str, int len, int x, int y, struct rendering *r, enum text_type tt) {
424 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
425 c9a3bfaa 2018-05-21 omar.polo XftColor xftcolor;
426 c9a3bfaa 2018-05-21 omar.polo if (tt == PROMPT) xftcolor = r->xft_prompt;
427 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL) xftcolor = r->xft_completion;
428 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL_HIGH) xftcolor = r->xft_completion_highlighted;
429 c9a3bfaa 2018-05-21 omar.polo
430 c9a3bfaa 2018-05-21 omar.polo XftDrawStringUtf8(r->xftdraw, &xftcolor, r->font, x, y, str, len);
431 c9a3bfaa 2018-05-21 omar.polo #else
432 c9a3bfaa 2018-05-21 omar.polo GC gc;
433 c9a3bfaa 2018-05-21 omar.polo if (tt == PROMPT) gc = r->prompt;
434 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL) gc = r->completion;
435 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL_HIGH) gc = r->completion_highlighted;
436 c9a3bfaa 2018-05-21 omar.polo Xutf8DrawString(r->d, r->w, *r->font, gc, x, y, str, len);
437 c9a3bfaa 2018-05-21 omar.polo #endif
438 f5e234d6 2018-05-18 omar.polo }
439 f5e234d6 2018-05-18 omar.polo
440 25bf99d2 2018-05-22 omar.polo // Duplicate the string str and substitute every space with a 'n'
441 c9a3bfaa 2018-05-21 omar.polo char *strdupn(char *str) {
442 c9a3bfaa 2018-05-21 omar.polo int len = strlen(str);
443 c9a3bfaa 2018-05-21 omar.polo
444 c9a3bfaa 2018-05-21 omar.polo if (str == nil || len == 0)
445 c9a3bfaa 2018-05-21 omar.polo return nil;
446 c9a3bfaa 2018-05-21 omar.polo
447 c9a3bfaa 2018-05-21 omar.polo char *dup = strdup(str);
448 c9a3bfaa 2018-05-21 omar.polo if (dup == nil)
449 c9a3bfaa 2018-05-21 omar.polo return nil;
450 c9a3bfaa 2018-05-21 omar.polo
451 c9a3bfaa 2018-05-21 omar.polo for (int i = 0; i < len; ++i)
452 c9a3bfaa 2018-05-21 omar.polo if (dup[i] == ' ')
453 c9a3bfaa 2018-05-21 omar.polo dup[i] = 'n';
454 c9a3bfaa 2018-05-21 omar.polo
455 c9a3bfaa 2018-05-21 omar.polo return dup;
456 c9a3bfaa 2018-05-21 omar.polo }
457 c9a3bfaa 2018-05-21 omar.polo
458 f5e234d6 2018-05-18 omar.polo // |------------------|----------------------------------------------|
459 f5e234d6 2018-05-18 omar.polo // | 20 char text | completion | completion | completion | compl |
460 f5e234d6 2018-05-18 omar.polo // |------------------|----------------------------------------------|
461 36a15a9f 2018-05-19 omar.polo void draw_horizontally(struct rendering *r, char *text, struct completions *cs) {
462 f5e234d6 2018-05-18 omar.polo int prompt_width = 20; // char
463 f5e234d6 2018-05-18 omar.polo
464 c9a3bfaa 2018-05-21 omar.polo int width, height;
465 c9a3bfaa 2018-05-21 omar.polo char *ps1_dup = strdupn(r->ps1);
466 e5186d6b 2018-05-26 omar.polo if (ps1_dup == nil)
467 e5186d6b 2018-05-26 omar.polo return;
468 e5186d6b 2018-05-26 omar.polo
469 c9a3bfaa 2018-05-21 omar.polo ps1_dup = ps1_dup == nil ? r->ps1 : ps1_dup;
470 c9a3bfaa 2018-05-21 omar.polo int ps1xlen = text_extents(ps1_dup, r->ps1len, r, &width, &height);
471 c9a3bfaa 2018-05-21 omar.polo free(ps1_dup);
472 8758854a 2018-05-20 omar.polo int start_at = ps1xlen;
473 8758854a 2018-05-20 omar.polo
474 c9a3bfaa 2018-05-21 omar.polo start_at = text_extents("n", 1, r, nil, nil);
475 e5186d6b 2018-05-26 omar.polo start_at = start_at * prompt_width + r->padding;
476 347d23da 2018-05-19 omar.polo
477 c9a3bfaa 2018-05-21 omar.polo int texty = (height + r->height) >>1;
478 347d23da 2018-05-19 omar.polo
479 f5e234d6 2018-05-18 omar.polo XFillRectangle(r->d, r->w, r->prompt_bg, 0, 0, start_at, r->height);
480 f5e234d6 2018-05-18 omar.polo
481 f5e234d6 2018-05-18 omar.polo int text_len = strlen(text);
482 f5e234d6 2018-05-18 omar.polo if (text_len > prompt_width)
483 f5e234d6 2018-05-18 omar.polo text = text + (text_len - prompt_width);
484 e5186d6b 2018-05-26 omar.polo draw_string(r->ps1, r->ps1len, r->padding, texty, r, PROMPT);
485 e5186d6b 2018-05-26 omar.polo draw_string(text, MIN(text_len, prompt_width), r->padding + ps1xlen, texty, r, PROMPT);
486 f5e234d6 2018-05-18 omar.polo
487 f5e234d6 2018-05-18 omar.polo XFillRectangle(r->d, r->w, r->completion_bg, start_at, 0, r->width, r->height);
488 f5e234d6 2018-05-18 omar.polo
489 f5e234d6 2018-05-18 omar.polo while (cs != nil) {
490 c9a3bfaa 2018-05-21 omar.polo enum text_type tt = cs->selected ? COMPL_HIGH : COMPL;
491 f5e234d6 2018-05-18 omar.polo GC h = cs->selected ? r->completion_highlighted_bg : r->completion_bg;
492 f5e234d6 2018-05-18 omar.polo
493 f5e234d6 2018-05-18 omar.polo int len = strlen(cs->completion);
494 c9a3bfaa 2018-05-21 omar.polo int text_width = text_extents(cs->completion, len, r, nil, nil);
495 f5e234d6 2018-05-18 omar.polo
496 e5186d6b 2018-05-26 omar.polo XFillRectangle(r->d, r->w, h, start_at, 0, text_width + r->padding*2, r->height);
497 f5e234d6 2018-05-18 omar.polo
498 e5186d6b 2018-05-26 omar.polo draw_string(cs->completion, len, start_at + r->padding, texty, r, tt);
499 347d23da 2018-05-19 omar.polo
500 e5186d6b 2018-05-26 omar.polo start_at += text_width + r->padding * 2;
501 f5e234d6 2018-05-18 omar.polo
502 0ee198aa 2018-05-19 omar.polo if (start_at > r->width)
503 0ee198aa 2018-05-19 omar.polo break; // don't draw completion if the space isn't enough
504 0ee198aa 2018-05-19 omar.polo
505 f5e234d6 2018-05-18 omar.polo cs = cs->next;
506 f5e234d6 2018-05-18 omar.polo }
507 f5e234d6 2018-05-18 omar.polo
508 f5e234d6 2018-05-18 omar.polo XFlush(r->d);
509 f5e234d6 2018-05-18 omar.polo }
510 36a15a9f 2018-05-19 omar.polo
511 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
512 36a15a9f 2018-05-19 omar.polo // | prompt |
513 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
514 36a15a9f 2018-05-19 omar.polo // | completion |
515 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
516 36a15a9f 2018-05-19 omar.polo // | completion |
517 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
518 e5186d6b 2018-05-26 omar.polo void draw_vertically(struct rendering *r, char *text, struct completions *cs) {
519 c9a3bfaa 2018-05-21 omar.polo int height, width;
520 e5186d6b 2018-05-26 omar.polo text_extents("fjpgl", 5, r, nil, &height);
521 e5186d6b 2018-05-26 omar.polo int start_at = height + r->padding;
522 36a15a9f 2018-05-19 omar.polo
523 36a15a9f 2018-05-19 omar.polo XFillRectangle(r->d, r->w, r->completion_bg, 0, 0, r->width, r->height);
524 36a15a9f 2018-05-19 omar.polo XFillRectangle(r->d, r->w, r->prompt_bg, 0, 0, r->width, start_at);
525 36a15a9f 2018-05-19 omar.polo
526 c9a3bfaa 2018-05-21 omar.polo char *ps1_dup = strdupn(r->ps1);
527 c9a3bfaa 2018-05-21 omar.polo ps1_dup = ps1_dup == nil ? r->ps1 : ps1_dup;
528 c9a3bfaa 2018-05-21 omar.polo int ps1xlen = text_extents(ps1_dup, r->ps1len, r, nil, nil);
529 c9a3bfaa 2018-05-21 omar.polo free(ps1_dup);
530 c9a3bfaa 2018-05-21 omar.polo
531 e5186d6b 2018-05-26 omar.polo draw_string(r->ps1, r->ps1len, r->padding, height + r->padding, r, PROMPT);
532 e5186d6b 2018-05-26 omar.polo draw_string(text, strlen(text), r->padding + ps1xlen, height + r->padding, r, PROMPT);
533 e5186d6b 2018-05-26 omar.polo start_at += r->padding;
534 c9a3bfaa 2018-05-21 omar.polo
535 36a15a9f 2018-05-19 omar.polo while (cs != nil) {
536 c9a3bfaa 2018-05-21 omar.polo enum text_type tt = cs->selected ? COMPL_HIGH : COMPL;
537 36a15a9f 2018-05-19 omar.polo GC h = cs->selected ? r->completion_highlighted_bg : r->completion_bg;
538 36a15a9f 2018-05-19 omar.polo
539 36a15a9f 2018-05-19 omar.polo int len = strlen(cs->completion);
540 c9a3bfaa 2018-05-21 omar.polo text_extents(cs->completion, len, r, &width, &height);
541 e5186d6b 2018-05-26 omar.polo XFillRectangle(r->d, r->w, h, 0, start_at, r->width, height + r->padding*2);
542 e5186d6b 2018-05-26 omar.polo draw_string(cs->completion, len, r->padding, start_at + height + r->padding, r, tt);
543 36a15a9f 2018-05-19 omar.polo
544 e5186d6b 2018-05-26 omar.polo start_at += height + r->padding *2;
545 0ee198aa 2018-05-19 omar.polo
546 0ee198aa 2018-05-19 omar.polo if (start_at > r->height)
547 0ee198aa 2018-05-19 omar.polo break; // don't draw completion if the space isn't enough
548 0ee198aa 2018-05-19 omar.polo
549 36a15a9f 2018-05-19 omar.polo cs = cs->next;
550 36a15a9f 2018-05-19 omar.polo }
551 36a15a9f 2018-05-19 omar.polo
552 36a15a9f 2018-05-19 omar.polo XFlush(r->d);
553 36a15a9f 2018-05-19 omar.polo }
554 36a15a9f 2018-05-19 omar.polo
555 36a15a9f 2018-05-19 omar.polo void draw(struct rendering *r, char *text, struct completions *cs) {
556 36a15a9f 2018-05-19 omar.polo if (r->horizontal_layout)
557 36a15a9f 2018-05-19 omar.polo draw_horizontally(r, text, cs);
558 36a15a9f 2018-05-19 omar.polo else
559 36a15a9f 2018-05-19 omar.polo draw_vertically(r, text, cs);
560 36a15a9f 2018-05-19 omar.polo }
561 36a15a9f 2018-05-19 omar.polo
562 f5e234d6 2018-05-18 omar.polo /* Set some WM stuff */
563 f5e234d6 2018-05-18 omar.polo void set_win_atoms_hints(Display *d, Window w, int width, int height) {
564 f5e234d6 2018-05-18 omar.polo Atom type;
565 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_WINDOW_TYPE_DOCK", false);
566 f5e234d6 2018-05-18 omar.polo XChangeProperty(
567 f5e234d6 2018-05-18 omar.polo d,
568 f5e234d6 2018-05-18 omar.polo w,
569 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_WINDOW_TYPE", false),
570 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
571 f5e234d6 2018-05-18 omar.polo 32,
572 f5e234d6 2018-05-18 omar.polo PropModeReplace,
573 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
574 f5e234d6 2018-05-18 omar.polo 1
575 f5e234d6 2018-05-18 omar.polo );
576 f5e234d6 2018-05-18 omar.polo
577 f5e234d6 2018-05-18 omar.polo /* some window managers honor this properties */
578 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_STATE_ABOVE", false);
579 f5e234d6 2018-05-18 omar.polo XChangeProperty(d,
580 f5e234d6 2018-05-18 omar.polo w,
581 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_STATE", false),
582 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
583 f5e234d6 2018-05-18 omar.polo 32,
584 f5e234d6 2018-05-18 omar.polo PropModeReplace,
585 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
586 f5e234d6 2018-05-18 omar.polo 1
587 f5e234d6 2018-05-18 omar.polo );
588 f5e234d6 2018-05-18 omar.polo
589 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_STATE_FOCUSED", false);
590 f5e234d6 2018-05-18 omar.polo XChangeProperty(d,
591 f5e234d6 2018-05-18 omar.polo w,
592 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_STATE", false),
593 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
594 f5e234d6 2018-05-18 omar.polo 32,
595 f5e234d6 2018-05-18 omar.polo PropModeAppend,
596 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
597 f5e234d6 2018-05-18 omar.polo 1
598 f5e234d6 2018-05-18 omar.polo );
599 f5e234d6 2018-05-18 omar.polo
600 f5e234d6 2018-05-18 omar.polo // setting window hints
601 f5e234d6 2018-05-18 omar.polo XClassHint *class_hint = XAllocClassHint();
602 f5e234d6 2018-05-18 omar.polo if (class_hint == nil) {
603 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not allocate memory for class hint\n");
604 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
605 f5e234d6 2018-05-18 omar.polo }
606 7ef37ee0 2018-05-21 omar.polo class_hint->res_name = resname;
607 7ef37ee0 2018-05-21 omar.polo class_hint->res_class = resclass;
608 f5e234d6 2018-05-18 omar.polo XSetClassHint(d, w, class_hint);
609 f5e234d6 2018-05-18 omar.polo XFree(class_hint);
610 f5e234d6 2018-05-18 omar.polo
611 f5e234d6 2018-05-18 omar.polo XSizeHints *size_hint = XAllocSizeHints();
612 f5e234d6 2018-05-18 omar.polo if (size_hint == nil) {
613 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not allocate memory for size hint\n");
614 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
615 f5e234d6 2018-05-18 omar.polo }
616 7ef37ee0 2018-05-21 omar.polo size_hint->flags = PMinSize | PBaseSize;
617 f5e234d6 2018-05-18 omar.polo size_hint->min_width = width;
618 f5e234d6 2018-05-18 omar.polo size_hint->base_width = width;
619 f5e234d6 2018-05-18 omar.polo size_hint->min_height = height;
620 f5e234d6 2018-05-18 omar.polo size_hint->base_height = height;
621 f5e234d6 2018-05-18 omar.polo
622 f5e234d6 2018-05-18 omar.polo XFlush(d);
623 f5e234d6 2018-05-18 omar.polo }
624 f5e234d6 2018-05-18 omar.polo
625 f5e234d6 2018-05-18 omar.polo void get_wh(Display *d, Window *w, int *width, int *height) {
626 f5e234d6 2018-05-18 omar.polo XWindowAttributes win_attr;
627 f5e234d6 2018-05-18 omar.polo XGetWindowAttributes(d, *w, &win_attr);
628 f5e234d6 2018-05-18 omar.polo *height = win_attr.height;
629 f5e234d6 2018-05-18 omar.polo *width = win_attr.width;
630 f5e234d6 2018-05-18 omar.polo }
631 f5e234d6 2018-05-18 omar.polo
632 f5e234d6 2018-05-18 omar.polo // I know this may seem a little hackish BUT is the only way I managed
633 f5e234d6 2018-05-18 omar.polo // to actually grab that goddam keyboard. Only one call to
634 f5e234d6 2018-05-18 omar.polo // XGrabKeyboard does not always end up with the keyboard grabbed!
635 f5e234d6 2018-05-18 omar.polo int take_keyboard(Display *d, Window w) {
636 686e9237 2018-05-18 omar.polo int i;
637 686e9237 2018-05-18 omar.polo for (i = 0; i < 100; i++) {
638 686e9237 2018-05-18 omar.polo if (XGrabKeyboard(d, w, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
639 686e9237 2018-05-18 omar.polo return 1;
640 686e9237 2018-05-18 omar.polo usleep(1000);
641 686e9237 2018-05-18 omar.polo }
642 686e9237 2018-05-18 omar.polo return 0;
643 f5e234d6 2018-05-18 omar.polo }
644 f5e234d6 2018-05-18 omar.polo
645 2128b469 2018-06-30 omar.polo // release the keyboard.
646 f5e234d6 2018-05-18 omar.polo void release_keyboard(Display *d) {
647 686e9237 2018-05-18 omar.polo XUngrabKeyboard(d, CurrentTime);
648 f5e234d6 2018-05-18 omar.polo }
649 f5e234d6 2018-05-18 omar.polo
650 25bf99d2 2018-05-22 omar.polo int parse_integer(const char *str, int default_value) {
651 f5e234d6 2018-05-18 omar.polo errno = 0;
652 f5e234d6 2018-05-18 omar.polo char *ep;
653 f5e234d6 2018-05-18 omar.polo long lval = strtol(str, &ep, 10);
654 f5e234d6 2018-05-18 omar.polo if (str[0] == '\0' || *ep != '\0') { // NaN
655 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "'%s' is not a valid number! Using %d as default.\n", str, default_value);
656 f5e234d6 2018-05-18 omar.polo return default_value;
657 f5e234d6 2018-05-18 omar.polo }
658 f5e234d6 2018-05-18 omar.polo if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
659 f5e234d6 2018-05-18 omar.polo (lval > INT_MAX || lval < INT_MIN)) {
660 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "%s out of range! Using %d as default.\n", str, default_value);
661 f5e234d6 2018-05-18 omar.polo return default_value;
662 f5e234d6 2018-05-18 omar.polo }
663 f5e234d6 2018-05-18 omar.polo return lval;
664 25bf99d2 2018-05-22 omar.polo }
665 25bf99d2 2018-05-22 omar.polo
666 2128b469 2018-06-30 omar.polo // like parse_integer, but if the value ends with a `%' then its
667 2128b469 2018-06-30 omar.polo // treated like a percentage (`max' is used to compute the percentage)
668 25bf99d2 2018-05-22 omar.polo int parse_int_with_percentage(const char *str, int default_value, int max) {
669 25bf99d2 2018-05-22 omar.polo int len = strlen(str);
670 25bf99d2 2018-05-22 omar.polo if (len > 0 && str[len-1] == '%') {
671 25bf99d2 2018-05-22 omar.polo char *cpy = strdup(str);
672 25bf99d2 2018-05-22 omar.polo check_allocation(cpy);
673 25bf99d2 2018-05-22 omar.polo cpy[len-1] = '\0';
674 25bf99d2 2018-05-22 omar.polo int val = parse_integer(cpy, default_value);
675 25bf99d2 2018-05-22 omar.polo free(cpy);
676 25bf99d2 2018-05-22 omar.polo return val * max / 100;
677 25bf99d2 2018-05-22 omar.polo }
678 576ab141 2018-05-22 omar.polo return parse_integer(str, default_value);
679 f5e234d6 2018-05-18 omar.polo }
680 f5e234d6 2018-05-18 omar.polo
681 2128b469 2018-06-30 omar.polo // like parse_int_with_percentage but understands the special value
682 2128b469 2018-06-30 omar.polo // "middle" that is (max - self) / 2
683 f5e234d6 2018-05-18 omar.polo int parse_int_with_middle(const char *str, int default_value, int max, int self) {
684 f5e234d6 2018-05-18 omar.polo if (!strcmp(str, "middle")) {
685 f5e234d6 2018-05-18 omar.polo return (max - self)/2;
686 f5e234d6 2018-05-18 omar.polo }
687 25bf99d2 2018-05-22 omar.polo return parse_int_with_percentage(str, default_value, max);
688 f5e234d6 2018-05-18 omar.polo }
689 f5e234d6 2018-05-18 omar.polo
690 2128b469 2018-06-30 omar.polo // Given the name of the program (argv[0]?) print a small help on stderr
691 e9f0b467 2018-06-07 omar.polo void usage(char *prgname) {
692 e9f0b467 2018-06-07 omar.polo fprintf(stderr, "Usage: %s [flags]\n", prgname);
693 e9f0b467 2018-06-07 omar.polo fprintf(stderr, "\t-a: automatic mode, the first completion is "
694 e9f0b467 2018-06-07 omar.polo "always selected;\n");
695 e9f0b467 2018-06-07 omar.polo fprintf(stderr, "\t-h: print this help.\n");
696 e9f0b467 2018-06-07 omar.polo }
697 e9f0b467 2018-06-07 omar.polo
698 2128b469 2018-06-30 omar.polo // Given an event, try to understand what the user wants. If the
699 2128b469 2018-06-30 omar.polo // return value is ADD_CHAR then `input' is a pointer to a string that
700 2128b469 2018-06-30 omar.polo // will need to be free'ed.
701 2128b469 2018-06-30 omar.polo enum action parse_event(Display *d, XKeyPressedEvent *ev, XIC xic, char **input) {
702 2128b469 2018-06-30 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_BackSpace))
703 2128b469 2018-06-30 omar.polo return DEL_CHAR;
704 2128b469 2018-06-30 omar.polo
705 2128b469 2018-06-30 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Tab))
706 2128b469 2018-06-30 omar.polo return ev->state & ShiftMask ? PREV_COMPL : NEXT_COMPL;
707 2128b469 2018-06-30 omar.polo
708 2128b469 2018-06-30 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Return))
709 2128b469 2018-06-30 omar.polo return CONFIRM;
710 2128b469 2018-06-30 omar.polo
711 2128b469 2018-06-30 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Escape))
712 2128b469 2018-06-30 omar.polo return EXIT;
713 2128b469 2018-06-30 omar.polo
714 2128b469 2018-06-30 omar.polo // try to read what the user pressed
715 2128b469 2018-06-30 omar.polo int symbol = 0;
716 2128b469 2018-06-30 omar.polo Status s = 0;
717 2128b469 2018-06-30 omar.polo Xutf8LookupString(xic, ev, (char*)&symbol, 4, 0, &s);
718 2128b469 2018-06-30 omar.polo if (s == XBufferOverflow) {
719 2128b469 2018-06-30 omar.polo // should not happen since there are no utf-8 characters larger
720 2128b469 2018-06-30 omar.polo // than 24bits
721 2128b469 2018-06-30 omar.polo fprintf(stderr, "Buffer overflow when trying to create keyboard symbol map.\n");
722 2128b469 2018-06-30 omar.polo return EXIT;
723 2128b469 2018-06-30 omar.polo }
724 2128b469 2018-06-30 omar.polo char *str = (char*)&symbol;
725 2128b469 2018-06-30 omar.polo
726 2128b469 2018-06-30 omar.polo if (ev->state & ControlMask) {
727 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-u
728 2128b469 2018-06-30 omar.polo return DEL_LINE;
729 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-w
730 2128b469 2018-06-30 omar.polo return DEL_WORD;
731 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-h
732 2128b469 2018-06-30 omar.polo return DEL_CHAR;
733 2128b469 2018-06-30 omar.polo if (!strcmp(str, "\r")) // C-m
734 2128b469 2018-06-30 omar.polo return CONFIRM;
735 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-p
736 2128b469 2018-06-30 omar.polo return PREV_COMPL;
737 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-n
738 2128b469 2018-06-30 omar.polo return NEXT_COMPL;
739 2128b469 2018-06-30 omar.polo }
740 2128b469 2018-06-30 omar.polo
741 2128b469 2018-06-30 omar.polo *input = strdup((char*)&symbol);
742 2128b469 2018-06-30 omar.polo if (*input == nil) {
743 2128b469 2018-06-30 omar.polo fprintf(stderr, "Error while allocating memory for key.\n");
744 2128b469 2018-06-30 omar.polo return EXIT;
745 2128b469 2018-06-30 omar.polo }
746 2128b469 2018-06-30 omar.polo
747 2128b469 2018-06-30 omar.polo return ADD_CHAR;
748 2128b469 2018-06-30 omar.polo }
749 2128b469 2018-06-30 omar.polo
750 2128b469 2018-06-30 omar.polo // clean up some resources
751 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) {
752 e9f0b467 2018-06-07 omar.polo release_keyboard(r->d);
753 e9f0b467 2018-06-07 omar.polo
754 e9f0b467 2018-06-07 omar.polo #ifdef USE_XFT
755 e9f0b467 2018-06-07 omar.polo XftColorFree(r->d, DefaultVisual(r->d, 0), DefaultColormap(r->d, 0), &r->xft_prompt);
756 e9f0b467 2018-06-07 omar.polo XftColorFree(r->d, DefaultVisual(r->d, 0), DefaultColormap(r->d, 0), &r->xft_completion);
757 e9f0b467 2018-06-07 omar.polo XftColorFree(r->d, DefaultVisual(r->d, 0), DefaultColormap(r->d, 0), &r->xft_completion_highlighted);
758 e9f0b467 2018-06-07 omar.polo #endif
759 e9f0b467 2018-06-07 omar.polo
760 e9f0b467 2018-06-07 omar.polo free(ps1);
761 e9f0b467 2018-06-07 omar.polo free(fontname);
762 e9f0b467 2018-06-07 omar.polo free(text);
763 e9f0b467 2018-06-07 omar.polo
764 e9f0b467 2018-06-07 omar.polo char *l = nil;
765 e9f0b467 2018-06-07 omar.polo char **lns = lines;
766 e9f0b467 2018-06-07 omar.polo while ((l = *lns) != nil) {
767 e9f0b467 2018-06-07 omar.polo free(l);
768 e9f0b467 2018-06-07 omar.polo ++lns;
769 e9f0b467 2018-06-07 omar.polo }
770 e9f0b467 2018-06-07 omar.polo free(lines);
771 e9f0b467 2018-06-07 omar.polo compl_delete(cs);
772 e9f0b467 2018-06-07 omar.polo
773 e9f0b467 2018-06-07 omar.polo XDestroyWindow(r->d, r->w);
774 e9f0b467 2018-06-07 omar.polo XCloseDisplay(r->d);
775 e9f0b467 2018-06-07 omar.polo
776 e9f0b467 2018-06-07 omar.polo return status;
777 e9f0b467 2018-06-07 omar.polo }
778 e9f0b467 2018-06-07 omar.polo
779 e9f0b467 2018-06-07 omar.polo int main(int argc, char **argv) {
780 e9f0b467 2018-06-07 omar.polo // by default the first completion isn't selected
781 e9f0b467 2018-06-07 omar.polo bool first_selected = false;
782 e9f0b467 2018-06-07 omar.polo
783 e9f0b467 2018-06-07 omar.polo // parse the command line options
784 e9f0b467 2018-06-07 omar.polo int ch;
785 b500fe86 2018-06-07 omar.polo while ((ch = getopt(argc, argv, "ahv")) != -1) {
786 e9f0b467 2018-06-07 omar.polo switch (ch) {
787 e9f0b467 2018-06-07 omar.polo case 'a':
788 e9f0b467 2018-06-07 omar.polo first_selected = true;
789 e9f0b467 2018-06-07 omar.polo break;
790 e9f0b467 2018-06-07 omar.polo case 'h':
791 e9f0b467 2018-06-07 omar.polo usage(*argv);
792 b500fe86 2018-06-07 omar.polo return 0;
793 b500fe86 2018-06-07 omar.polo case 'v':
794 b500fe86 2018-06-07 omar.polo fprintf(stderr, "%s version: %s\n", *argv, VERSION);
795 e9f0b467 2018-06-07 omar.polo return 0;
796 e9f0b467 2018-06-07 omar.polo default:
797 e9f0b467 2018-06-07 omar.polo usage(*argv);
798 e9f0b467 2018-06-07 omar.polo return EX_USAGE;
799 e9f0b467 2018-06-07 omar.polo }
800 e9f0b467 2018-06-07 omar.polo }
801 e9f0b467 2018-06-07 omar.polo
802 95b27a5e 2018-05-19 omar.polo char **lines = calloc(INITIAL_ITEMS, sizeof(char*));
803 e9f0b467 2018-06-07 omar.polo readlines(&lines, INITIAL_ITEMS);
804 f5e234d6 2018-05-18 omar.polo
805 f5e234d6 2018-05-18 omar.polo setlocale(LC_ALL, getenv("LANG"));
806 f5e234d6 2018-05-18 omar.polo
807 f5e234d6 2018-05-18 omar.polo enum state status = LOOPING;
808 f5e234d6 2018-05-18 omar.polo
809 f5e234d6 2018-05-18 omar.polo // where the monitor start (used only with xinerama)
810 f5e234d6 2018-05-18 omar.polo int offset_x = 0;
811 f5e234d6 2018-05-18 omar.polo int offset_y = 0;
812 f5e234d6 2018-05-18 omar.polo
813 f5e234d6 2018-05-18 omar.polo // width and height of the window
814 f5e234d6 2018-05-18 omar.polo int width = 400;
815 f5e234d6 2018-05-18 omar.polo int height = 20;
816 f5e234d6 2018-05-18 omar.polo
817 f5e234d6 2018-05-18 omar.polo // position on the screen
818 f5e234d6 2018-05-18 omar.polo int x = 0;
819 f5e234d6 2018-05-18 omar.polo int y = 0;
820 f5e234d6 2018-05-18 omar.polo
821 e9f0b467 2018-06-07 omar.polo // the default padding
822 e5186d6b 2018-05-26 omar.polo int padding = 10;
823 e5186d6b 2018-05-26 omar.polo
824 8758854a 2018-05-20 omar.polo char *ps1 = strdup("$ ");
825 8758854a 2018-05-20 omar.polo check_allocation(ps1);
826 8758854a 2018-05-20 omar.polo
827 9e94fcbe 2018-05-22 omar.polo char *fontname = strdup(default_fontname);
828 f5e234d6 2018-05-18 omar.polo check_allocation(fontname);
829 f5e234d6 2018-05-18 omar.polo
830 f5e234d6 2018-05-18 omar.polo int textlen = 10;
831 f5e234d6 2018-05-18 omar.polo char *text = malloc(textlen * sizeof(char));
832 f5e234d6 2018-05-18 omar.polo check_allocation(text);
833 f5e234d6 2018-05-18 omar.polo
834 e9f0b467 2018-06-07 omar.polo bool nothing_selected = first_selected;
835 e9f0b467 2018-06-07 omar.polo /* struct completions *cs = filter(text, lines); */
836 e9f0b467 2018-06-07 omar.polo struct completions *cs = nil;
837 2128b469 2018-06-30 omar.polo cs = update_completions(cs, text, lines, first_selected);
838 f5e234d6 2018-05-18 omar.polo
839 f5e234d6 2018-05-18 omar.polo // start talking to xorg
840 f5e234d6 2018-05-18 omar.polo Display *d = XOpenDisplay(nil);
841 f5e234d6 2018-05-18 omar.polo if (d == nil) {
842 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not open display!\n");
843 f5e234d6 2018-05-18 omar.polo return EX_UNAVAILABLE;
844 f5e234d6 2018-05-18 omar.polo }
845 f5e234d6 2018-05-18 omar.polo
846 f5e234d6 2018-05-18 omar.polo // get display size
847 f5e234d6 2018-05-18 omar.polo // XXX: is getting the default root window dimension correct?
848 f5e234d6 2018-05-18 omar.polo XWindowAttributes xwa;
849 f5e234d6 2018-05-18 omar.polo XGetWindowAttributes(d, DefaultRootWindow(d), &xwa);
850 f5e234d6 2018-05-18 omar.polo int d_width = xwa.width;
851 f5e234d6 2018-05-18 omar.polo int d_height = xwa.height;
852 f5e234d6 2018-05-18 omar.polo
853 f5e234d6 2018-05-18 omar.polo #ifdef USE_XINERAMA
854 f5e234d6 2018-05-18 omar.polo if (XineramaIsActive(d)) {
855 7ca8829b 2018-05-21 omar.polo // find the mice
856 7ca8829b 2018-05-21 omar.polo int number_of_screens = XScreenCount(d);
857 7ca8829b 2018-05-21 omar.polo Window r;
858 7ca8829b 2018-05-21 omar.polo Window root;
859 7ca8829b 2018-05-21 omar.polo int root_x, root_y, win_x, win_y;
860 7ca8829b 2018-05-21 omar.polo unsigned int mask;
861 7ca8829b 2018-05-21 omar.polo bool res;
862 7ca8829b 2018-05-21 omar.polo for (int i = 0; i < number_of_screens; ++i) {
863 7ca8829b 2018-05-21 omar.polo root = XRootWindow(d, i);
864 7ca8829b 2018-05-21 omar.polo res = XQueryPointer(d, root, &r, &r, &root_x, &root_y, &win_x, &win_y, &mask);
865 7ca8829b 2018-05-21 omar.polo if (res) break;
866 7ca8829b 2018-05-21 omar.polo }
867 7ca8829b 2018-05-21 omar.polo if (!res) {
868 7ca8829b 2018-05-21 omar.polo fprintf(stderr, "No mouse found.\n");
869 7ca8829b 2018-05-21 omar.polo root_x = 0;
870 7ca8829b 2018-05-21 omar.polo root_y = 0;
871 7ca8829b 2018-05-21 omar.polo }
872 7ca8829b 2018-05-21 omar.polo
873 7ca8829b 2018-05-21 omar.polo // now find in which monitor the mice is on
874 f5e234d6 2018-05-18 omar.polo int monitors;
875 f5e234d6 2018-05-18 omar.polo XineramaScreenInfo *info = XineramaQueryScreens(d, &monitors);
876 7ca8829b 2018-05-21 omar.polo if (info) {
877 f5e234d6 2018-05-18 omar.polo for (int i = 0; i < monitors; ++i) {
878 7ca8829b 2018-05-21 omar.polo if (info[i].x_org <= root_x && root_x <= (info[i].x_org + info[i].width)
879 7ca8829b 2018-05-21 omar.polo && info[i].y_org <= root_y && root_y <= (info[i].y_org + info[i].height)) {
880 7ca8829b 2018-05-21 omar.polo offset_x = info[i].x_org;
881 7ca8829b 2018-05-21 omar.polo offset_y = info[i].y_org;
882 f5e234d6 2018-05-18 omar.polo d_width = info[i].width;
883 f5e234d6 2018-05-18 omar.polo d_height = info[i].height;
884 7ca8829b 2018-05-21 omar.polo break;
885 f5e234d6 2018-05-18 omar.polo }
886 f5e234d6 2018-05-18 omar.polo }
887 7ca8829b 2018-05-21 omar.polo }
888 f5e234d6 2018-05-18 omar.polo XFree(info);
889 f5e234d6 2018-05-18 omar.polo }
890 f5e234d6 2018-05-18 omar.polo #endif
891 f5e234d6 2018-05-18 omar.polo
892 f5e234d6 2018-05-18 omar.polo Colormap cmap = DefaultColormap(d, DefaultScreen(d));
893 f5e234d6 2018-05-18 omar.polo XColor p_fg, p_bg,
894 f5e234d6 2018-05-18 omar.polo compl_fg, compl_bg,
895 f5e234d6 2018-05-18 omar.polo compl_highlighted_fg, compl_highlighted_bg;
896 f5e234d6 2018-05-18 omar.polo
897 36a15a9f 2018-05-19 omar.polo bool horizontal_layout = true;
898 36a15a9f 2018-05-19 omar.polo
899 f5e234d6 2018-05-18 omar.polo // read resource
900 f5e234d6 2018-05-18 omar.polo XrmInitialize();
901 f5e234d6 2018-05-18 omar.polo char *xrm = XResourceManagerString(d);
902 347d23da 2018-05-19 omar.polo XrmDatabase xdb = nil;
903 f5e234d6 2018-05-18 omar.polo if (xrm != nil) {
904 347d23da 2018-05-19 omar.polo xdb = XrmGetStringDatabase(xrm);
905 f5e234d6 2018-05-18 omar.polo XrmValue value;
906 f5e234d6 2018-05-18 omar.polo char *datatype[20];
907 f5e234d6 2018-05-18 omar.polo
908 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.font", "*", datatype, &value) == true) {
909 f5e234d6 2018-05-18 omar.polo fontname = strdup(value.addr);
910 f5e234d6 2018-05-18 omar.polo check_allocation(fontname);
911 f5e234d6 2018-05-18 omar.polo }
912 f5e234d6 2018-05-18 omar.polo else
913 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no font defined, using %s\n", fontname);
914 36a15a9f 2018-05-19 omar.polo
915 36a15a9f 2018-05-19 omar.polo if (XrmGetResource(xdb, "MyMenu.layout", "*", datatype, &value) == true) {
916 36a15a9f 2018-05-19 omar.polo char *v = strdup(value.addr);
917 36a15a9f 2018-05-19 omar.polo check_allocation(v);
918 36a15a9f 2018-05-19 omar.polo horizontal_layout = !strcmp(v, "horizontal");
919 36a15a9f 2018-05-19 omar.polo free(v);
920 36a15a9f 2018-05-19 omar.polo }
921 36a15a9f 2018-05-19 omar.polo else
922 36a15a9f 2018-05-19 omar.polo fprintf(stderr, "no layout defined, using horizontal\n");
923 f5e234d6 2018-05-18 omar.polo
924 8758854a 2018-05-20 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt", "*", datatype, &value) == true) {
925 8758854a 2018-05-20 omar.polo free(ps1);
926 8758854a 2018-05-20 omar.polo ps1 = normalize_str(value.addr);
927 8758854a 2018-05-20 omar.polo } else
928 8758854a 2018-05-20 omar.polo fprintf(stderr, "no prompt defined, using \"%s\" as default\n", ps1);
929 8758854a 2018-05-20 omar.polo
930 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.width", "*", datatype, &value) == true)
931 25bf99d2 2018-05-22 omar.polo width = parse_int_with_percentage(value.addr, width, d_width);
932 f5e234d6 2018-05-18 omar.polo else
933 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no width defined, using %d\n", width);
934 f5e234d6 2018-05-18 omar.polo
935 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.height", "*", datatype, &value) == true)
936 25bf99d2 2018-05-22 omar.polo height = parse_int_with_percentage(value.addr, height, d_height);
937 f5e234d6 2018-05-18 omar.polo else
938 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no height defined, using %d\n", height);
939 f5e234d6 2018-05-18 omar.polo
940 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.x", "*", datatype, &value) == true)
941 f5e234d6 2018-05-18 omar.polo x = parse_int_with_middle(value.addr, x, d_width, width);
942 f5e234d6 2018-05-18 omar.polo else
943 e5186d6b 2018-05-26 omar.polo fprintf(stderr, "no x defined, using %d\n", x);
944 f5e234d6 2018-05-18 omar.polo
945 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.y", "*", datatype, &value) == true)
946 f5e234d6 2018-05-18 omar.polo y = parse_int_with_middle(value.addr, y, d_height, height);
947 f5e234d6 2018-05-18 omar.polo else
948 e5186d6b 2018-05-26 omar.polo fprintf(stderr, "no y defined, using %d\n", y);
949 f5e234d6 2018-05-18 omar.polo
950 e5186d6b 2018-05-26 omar.polo if (XrmGetResource(xdb, "MyMenu.padding", "*", datatype, &value) == true)
951 e5186d6b 2018-05-26 omar.polo padding = parse_integer(value.addr, padding);
952 e5186d6b 2018-05-26 omar.polo else
953 e5186d6b 2018-05-26 omar.polo fprintf(stderr, "no y defined, using %d\n", padding);
954 e5186d6b 2018-05-26 omar.polo
955 f5e234d6 2018-05-18 omar.polo XColor tmp;
956 f5e234d6 2018-05-18 omar.polo // TODO: tmp needs to be free'd after every allocation?
957 f5e234d6 2018-05-18 omar.polo
958 f5e234d6 2018-05-18 omar.polo // prompt
959 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.foreground", "*", datatype, &value) == true)
960 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &p_fg, &tmp);
961 f5e234d6 2018-05-18 omar.polo else
962 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &p_fg, &tmp);
963 f5e234d6 2018-05-18 omar.polo
964 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.background", "*", datatype, &value) == true)
965 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &p_bg, &tmp);
966 f5e234d6 2018-05-18 omar.polo else
967 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &p_bg, &tmp);
968 f5e234d6 2018-05-18 omar.polo
969 f5e234d6 2018-05-18 omar.polo // completion
970 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.foreground", "*", datatype, &value) == true)
971 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_fg, &tmp);
972 f5e234d6 2018-05-18 omar.polo else
973 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_fg, &tmp);
974 f5e234d6 2018-05-18 omar.polo
975 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.background", "*", datatype, &value) == true)
976 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_bg, &tmp);
977 f5e234d6 2018-05-18 omar.polo else
978 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_bg, &tmp);
979 f5e234d6 2018-05-18 omar.polo
980 f5e234d6 2018-05-18 omar.polo // completion highlighted
981 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion_highlighted.foreground", "*", datatype, &value) == true)
982 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_highlighted_fg, &tmp);
983 f5e234d6 2018-05-18 omar.polo else
984 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_highlighted_fg, &tmp);
985 f5e234d6 2018-05-18 omar.polo
986 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion_highlighted.background", "*", datatype, &value) == true)
987 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_highlighted_bg, &tmp);
988 f5e234d6 2018-05-18 omar.polo else
989 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_highlighted_bg, &tmp);
990 f5e234d6 2018-05-18 omar.polo } else {
991 f5e234d6 2018-05-18 omar.polo XColor tmp;
992 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &p_fg, &tmp);
993 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &p_bg, &tmp);
994 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_fg, &tmp);
995 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_bg, &tmp);
996 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_highlighted_fg, &tmp);
997 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_highlighted_bg, &tmp);
998 f5e234d6 2018-05-18 omar.polo }
999 f5e234d6 2018-05-18 omar.polo
1000 f5e234d6 2018-05-18 omar.polo // load the font
1001 347d23da 2018-05-19 omar.polo /* XFontStruct *font = XLoadQueryFont(d, fontname); */
1002 347d23da 2018-05-19 omar.polo /* if (font == nil) { */
1003 347d23da 2018-05-19 omar.polo /* fprintf(stderr, "Unable to load %s font\n", fontname); */
1004 347d23da 2018-05-19 omar.polo /* font = XLoadQueryFont(d, "fixed"); */
1005 347d23da 2018-05-19 omar.polo /* } */
1006 347d23da 2018-05-19 omar.polo // load the font
1007 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
1008 c9a3bfaa 2018-05-21 omar.polo XftFont *font = XftFontOpenName(d, DefaultScreen(d), fontname);
1009 c9a3bfaa 2018-05-21 omar.polo #else
1010 347d23da 2018-05-19 omar.polo char **missing_charset_list;
1011 347d23da 2018-05-19 omar.polo int missing_charset_count;
1012 347d23da 2018-05-19 omar.polo XFontSet font = XCreateFontSet(d, fontname, &missing_charset_list, &missing_charset_count, nil);
1013 f5e234d6 2018-05-18 omar.polo if (font == nil) {
1014 347d23da 2018-05-19 omar.polo fprintf(stderr, "Unable to load the font(s) %s\n", fontname);
1015 347d23da 2018-05-19 omar.polo return EX_UNAVAILABLE;
1016 f5e234d6 2018-05-18 omar.polo }
1017 c9a3bfaa 2018-05-21 omar.polo #endif
1018 f5e234d6 2018-05-18 omar.polo
1019 f5e234d6 2018-05-18 omar.polo // create the window
1020 f5e234d6 2018-05-18 omar.polo XSetWindowAttributes attr;
1021 55bd6842 2018-05-21 omar.polo attr.override_redirect = true;
1022 f5e234d6 2018-05-18 omar.polo
1023 f5e234d6 2018-05-18 omar.polo Window w = XCreateWindow(d, // display
1024 f5e234d6 2018-05-18 omar.polo DefaultRootWindow(d), // parent
1025 f5e234d6 2018-05-18 omar.polo x + offset_x, y + offset_y, // x y
1026 f5e234d6 2018-05-18 omar.polo width, height, // w h
1027 f5e234d6 2018-05-18 omar.polo 0, // border width
1028 f5e234d6 2018-05-18 omar.polo DefaultDepth(d, DefaultScreen(d)), // depth
1029 f5e234d6 2018-05-18 omar.polo InputOutput, // class
1030 f5e234d6 2018-05-18 omar.polo DefaultVisual(d, DefaultScreen(d)), // visual
1031 55bd6842 2018-05-21 omar.polo CWOverrideRedirect, // value mask
1032 f5e234d6 2018-05-18 omar.polo &attr);
1033 f5e234d6 2018-05-18 omar.polo
1034 f5e234d6 2018-05-18 omar.polo set_win_atoms_hints(d, w, width, height);
1035 f5e234d6 2018-05-18 omar.polo
1036 f5e234d6 2018-05-18 omar.polo // we want some events
1037 c9a3bfaa 2018-05-21 omar.polo XSelectInput(d, w, StructureNotifyMask | KeyPressMask | KeymapStateMask);
1038 f5e234d6 2018-05-18 omar.polo
1039 f5e234d6 2018-05-18 omar.polo // make the window appear on the screen
1040 f5e234d6 2018-05-18 omar.polo XMapWindow(d, w);
1041 f5e234d6 2018-05-18 omar.polo
1042 f5e234d6 2018-05-18 omar.polo // wait for the MapNotify event (i.e. the event "window rendered")
1043 f5e234d6 2018-05-18 omar.polo for (;;) {
1044 f5e234d6 2018-05-18 omar.polo XEvent e;
1045 f5e234d6 2018-05-18 omar.polo XNextEvent(d, &e);
1046 f5e234d6 2018-05-18 omar.polo if (e.type == MapNotify)
1047 f5e234d6 2018-05-18 omar.polo break;
1048 f5e234d6 2018-05-18 omar.polo }
1049 f5e234d6 2018-05-18 omar.polo
1050 f5e234d6 2018-05-18 omar.polo // get the *real* width & height after the window was rendered
1051 f5e234d6 2018-05-18 omar.polo get_wh(d, &w, &width, &height);
1052 f5e234d6 2018-05-18 omar.polo
1053 f5e234d6 2018-05-18 omar.polo // grab keyboard
1054 f5e234d6 2018-05-18 omar.polo take_keyboard(d, w);
1055 f5e234d6 2018-05-18 omar.polo
1056 f5e234d6 2018-05-18 omar.polo // Create some graphics contexts
1057 f5e234d6 2018-05-18 omar.polo XGCValues values;
1058 347d23da 2018-05-19 omar.polo /* values.font = font->fid; */
1059 f5e234d6 2018-05-18 omar.polo
1060 f5e234d6 2018-05-18 omar.polo struct rendering r = {
1061 f5e234d6 2018-05-18 omar.polo .d = d,
1062 f5e234d6 2018-05-18 omar.polo .w = w,
1063 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
1064 c9a3bfaa 2018-05-21 omar.polo .font = font,
1065 c9a3bfaa 2018-05-21 omar.polo #else
1066 c9a3bfaa 2018-05-21 omar.polo .font = &font,
1067 c9a3bfaa 2018-05-21 omar.polo #endif
1068 347d23da 2018-05-19 omar.polo .prompt = XCreateGC(d, w, 0, &values),
1069 347d23da 2018-05-19 omar.polo .prompt_bg = XCreateGC(d, w, 0, &values),
1070 347d23da 2018-05-19 omar.polo .completion = XCreateGC(d, w, 0, &values),
1071 347d23da 2018-05-19 omar.polo .completion_bg = XCreateGC(d, w, 0, &values),
1072 347d23da 2018-05-19 omar.polo .completion_highlighted = XCreateGC(d, w, 0, &values),
1073 347d23da 2018-05-19 omar.polo .completion_highlighted_bg = XCreateGC(d, w, 0, &values),
1074 f5e234d6 2018-05-18 omar.polo .width = width,
1075 f5e234d6 2018-05-18 omar.polo .height = height,
1076 e5186d6b 2018-05-26 omar.polo .padding = padding,
1077 8758854a 2018-05-20 omar.polo .horizontal_layout = horizontal_layout,
1078 8758854a 2018-05-20 omar.polo .ps1 = ps1,
1079 8758854a 2018-05-20 omar.polo .ps1len = strlen(ps1)
1080 f5e234d6 2018-05-18 omar.polo };
1081 f5e234d6 2018-05-18 omar.polo
1082 7ca8829b 2018-05-21 omar.polo #ifdef USE_XFT
1083 7ca8829b 2018-05-21 omar.polo r.xftdraw = XftDrawCreate(d, w, DefaultVisual(d, 0), DefaultColormap(d, 0));
1084 c9a3bfaa 2018-05-21 omar.polo
1085 7ca8829b 2018-05-21 omar.polo // prompt
1086 7ca8829b 2018-05-21 omar.polo XRenderColor xrcolor;
1087 7ca8829b 2018-05-21 omar.polo xrcolor.red = p_fg.red;
1088 7ca8829b 2018-05-21 omar.polo xrcolor.green = p_fg.red;
1089 7ca8829b 2018-05-21 omar.polo xrcolor.blue = p_fg.red;
1090 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
1091 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_prompt);
1092 c9a3bfaa 2018-05-21 omar.polo
1093 7ca8829b 2018-05-21 omar.polo // completion
1094 7ca8829b 2018-05-21 omar.polo xrcolor.red = compl_fg.red;
1095 7ca8829b 2018-05-21 omar.polo xrcolor.green = compl_fg.green;
1096 7ca8829b 2018-05-21 omar.polo xrcolor.blue = compl_fg.blue;
1097 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
1098 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_completion);
1099 7ca8829b 2018-05-21 omar.polo
1100 7ca8829b 2018-05-21 omar.polo // completion highlighted
1101 7ca8829b 2018-05-21 omar.polo xrcolor.red = compl_highlighted_fg.red;
1102 7ca8829b 2018-05-21 omar.polo xrcolor.green = compl_highlighted_fg.green;
1103 7ca8829b 2018-05-21 omar.polo xrcolor.blue = compl_highlighted_fg.blue;
1104 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
1105 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_completion_highlighted);
1106 c9a3bfaa 2018-05-21 omar.polo #endif
1107 7ca8829b 2018-05-21 omar.polo
1108 f5e234d6 2018-05-18 omar.polo // load the colors in our GCs
1109 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.prompt, p_fg.pixel);
1110 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.prompt_bg, p_bg.pixel);
1111 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion, compl_fg.pixel);
1112 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_bg, compl_bg.pixel);
1113 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_highlighted, compl_highlighted_fg.pixel);
1114 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_highlighted_bg, compl_highlighted_bg.pixel);
1115 347d23da 2018-05-19 omar.polo
1116 347d23da 2018-05-19 omar.polo // open the X input method
1117 347d23da 2018-05-19 omar.polo XIM xim = XOpenIM(d, xdb, resname, resclass);
1118 347d23da 2018-05-19 omar.polo check_allocation(xim);
1119 f5e234d6 2018-05-18 omar.polo
1120 347d23da 2018-05-19 omar.polo XIMStyles *xis = nil;
1121 347d23da 2018-05-19 omar.polo if (XGetIMValues(xim, XNQueryInputStyle, &xis, NULL) || !xis) {
1122 347d23da 2018-05-19 omar.polo fprintf(stderr, "Input Styles could not be retrieved\n");
1123 347d23da 2018-05-19 omar.polo return EX_UNAVAILABLE;
1124 347d23da 2018-05-19 omar.polo }
1125 347d23da 2018-05-19 omar.polo
1126 347d23da 2018-05-19 omar.polo XIMStyle bestMatchStyle = 0;
1127 347d23da 2018-05-19 omar.polo for (int i = 0; i < xis->count_styles; ++i) {
1128 347d23da 2018-05-19 omar.polo XIMStyle ts = xis->supported_styles[i];
1129 347d23da 2018-05-19 omar.polo if (ts == (XIMPreeditNothing | XIMStatusNothing)) {
1130 347d23da 2018-05-19 omar.polo bestMatchStyle = ts;
1131 347d23da 2018-05-19 omar.polo break;
1132 347d23da 2018-05-19 omar.polo }
1133 347d23da 2018-05-19 omar.polo }
1134 347d23da 2018-05-19 omar.polo XFree(xis);
1135 347d23da 2018-05-19 omar.polo
1136 347d23da 2018-05-19 omar.polo if (!bestMatchStyle) {
1137 347d23da 2018-05-19 omar.polo fprintf(stderr, "No matching input style could be determined\n");
1138 347d23da 2018-05-19 omar.polo }
1139 347d23da 2018-05-19 omar.polo
1140 347d23da 2018-05-19 omar.polo XIC xic = XCreateIC(xim, XNInputStyle, bestMatchStyle, XNClientWindow, w, XNFocusWindow, w, NULL);
1141 347d23da 2018-05-19 omar.polo check_allocation(xic);
1142 347d23da 2018-05-19 omar.polo
1143 f5e234d6 2018-05-18 omar.polo // draw the window for the first time
1144 f5e234d6 2018-05-18 omar.polo draw(&r, text, cs);
1145 f5e234d6 2018-05-18 omar.polo
1146 f5e234d6 2018-05-18 omar.polo // main loop
1147 f5e234d6 2018-05-18 omar.polo while (status == LOOPING) {
1148 f5e234d6 2018-05-18 omar.polo XEvent e;
1149 f5e234d6 2018-05-18 omar.polo XNextEvent(d, &e);
1150 f5e234d6 2018-05-18 omar.polo
1151 347d23da 2018-05-19 omar.polo if (XFilterEvent(&e, w))
1152 347d23da 2018-05-19 omar.polo continue;
1153 347d23da 2018-05-19 omar.polo
1154 f5e234d6 2018-05-18 omar.polo switch (e.type) {
1155 f5e234d6 2018-05-18 omar.polo case KeymapNotify:
1156 f5e234d6 2018-05-18 omar.polo XRefreshKeyboardMapping(&e.xmapping);
1157 f5e234d6 2018-05-18 omar.polo break;
1158 f5e234d6 2018-05-18 omar.polo
1159 347d23da 2018-05-19 omar.polo case KeyPress: {
1160 347d23da 2018-05-19 omar.polo XKeyPressedEvent *ev = (XKeyPressedEvent*)&e;
1161 f5e234d6 2018-05-18 omar.polo
1162 2128b469 2018-06-30 omar.polo char *input;
1163 2128b469 2018-06-30 omar.polo switch (parse_event(d, ev, xic, &input)) {
1164 2128b469 2018-06-30 omar.polo case EXIT:
1165 2128b469 2018-06-30 omar.polo status = ERR;
1166 2128b469 2018-06-30 omar.polo break;
1167 f5e234d6 2018-05-18 omar.polo
1168 2128b469 2018-06-30 omar.polo case CONFIRM:
1169 2128b469 2018-06-30 omar.polo status = OK;
1170 e9f0b467 2018-06-07 omar.polo if (first_selected) {
1171 2128b469 2018-06-30 omar.polo complete(cs, &first_selected, false, &text, &textlen, &status);
1172 e9f0b467 2018-06-07 omar.polo }
1173 2128b469 2018-06-30 omar.polo break;
1174 2128b469 2018-06-30 omar.polo
1175 2128b469 2018-06-30 omar.polo case PREV_COMPL: {
1176 2128b469 2018-06-30 omar.polo complete(cs, &nothing_selected, true, &text, &textlen, &status);
1177 2128b469 2018-06-30 omar.polo break;
1178 347d23da 2018-05-19 omar.polo }
1179 f5e234d6 2018-05-18 omar.polo
1180 2128b469 2018-06-30 omar.polo case NEXT_COMPL: {
1181 2128b469 2018-06-30 omar.polo complete(cs, &nothing_selected, false, &text, &textlen, &status);
1182 2128b469 2018-06-30 omar.polo break;
1183 347d23da 2018-05-19 omar.polo }
1184 f5e234d6 2018-05-18 omar.polo
1185 2128b469 2018-06-30 omar.polo case DEL_CHAR:
1186 2128b469 2018-06-30 omar.polo nothing_selected = first_selected;
1187 2128b469 2018-06-30 omar.polo popc(text, textlen);
1188 2128b469 2018-06-30 omar.polo cs = update_completions(cs, text, lines, first_selected);
1189 2128b469 2018-06-30 omar.polo break;
1190 347d23da 2018-05-19 omar.polo
1191 2128b469 2018-06-30 omar.polo case DEL_WORD: {
1192 2128b469 2018-06-30 omar.polo nothing_selected = first_selected;
1193 347d23da 2018-05-19 omar.polo
1194 2128b469 2018-06-30 omar.polo // `textlen` is the lenght of the allocated string, not the
1195 2128b469 2018-06-30 omar.polo // lenght of the ACTUAL string
1196 2128b469 2018-06-30 omar.polo int p = strlen(text) -1;
1197 2128b469 2018-06-30 omar.polo if (p > 0) { // delete the current char
1198 2128b469 2018-06-30 omar.polo text[p] = 0;
1199 2128b469 2018-06-30 omar.polo p--;
1200 2128b469 2018-06-30 omar.polo }
1201 347d23da 2018-05-19 omar.polo
1202 2128b469 2018-06-30 omar.polo // erase the alphanumeric char
1203 2128b469 2018-06-30 omar.polo while (p >= 0 && isalnum(text[p])) {
1204 2128b469 2018-06-30 omar.polo text[p] = 0;
1205 2128b469 2018-06-30 omar.polo p--;
1206 2128b469 2018-06-30 omar.polo }
1207 2128b469 2018-06-30 omar.polo
1208 2128b469 2018-06-30 omar.polo // erase also trailing white spaces
1209 2128b469 2018-06-30 omar.polo while (p >= 0 && isspace(text[p])) {
1210 2128b469 2018-06-30 omar.polo text[p] = 0;
1211 2128b469 2018-06-30 omar.polo p--;
1212 2128b469 2018-06-30 omar.polo }
1213 2128b469 2018-06-30 omar.polo cs = update_completions(cs, text, lines, first_selected);
1214 2128b469 2018-06-30 omar.polo break;
1215 f5e234d6 2018-05-18 omar.polo }
1216 f5e234d6 2018-05-18 omar.polo
1217 2128b469 2018-06-30 omar.polo case DEL_LINE: {
1218 2128b469 2018-06-30 omar.polo nothing_selected = first_selected;
1219 2128b469 2018-06-30 omar.polo for (int i = 0; i < textlen; ++i)
1220 2128b469 2018-06-30 omar.polo text[i] = 0;
1221 2128b469 2018-06-30 omar.polo cs = update_completions(cs, text, lines, first_selected);
1222 2128b469 2018-06-30 omar.polo break;
1223 347d23da 2018-05-19 omar.polo }
1224 347d23da 2018-05-19 omar.polo
1225 2128b469 2018-06-30 omar.polo case ADD_CHAR: {
1226 2128b469 2018-06-30 omar.polo int str_len = strlen(input);
1227 2128b469 2018-06-30 omar.polo for (int i = 0; i < str_len; ++i) {
1228 2128b469 2018-06-30 omar.polo textlen = pushc(&text, textlen, input[i]);
1229 2128b469 2018-06-30 omar.polo if (textlen == -1) {
1230 2128b469 2018-06-30 omar.polo fprintf(stderr, "Memory allocation error\n");
1231 2128b469 2018-06-30 omar.polo status = ERR;
1232 2128b469 2018-06-30 omar.polo break;
1233 2128b469 2018-06-30 omar.polo }
1234 2128b469 2018-06-30 omar.polo nothing_selected = first_selected;
1235 2128b469 2018-06-30 omar.polo cs = update_completions(cs, text, lines, first_selected);
1236 2128b469 2018-06-30 omar.polo free(input);
1237 2128b469 2018-06-30 omar.polo }
1238 2128b469 2018-06-30 omar.polo }
1239 2128b469 2018-06-30 omar.polo }
1240 347d23da 2018-05-19 omar.polo }
1241 f5e234d6 2018-05-18 omar.polo draw(&r, text, cs);
1242 f5e234d6 2018-05-18 omar.polo break;
1243 f5e234d6 2018-05-18 omar.polo
1244 f5e234d6 2018-05-18 omar.polo default:
1245 347d23da 2018-05-19 omar.polo fprintf(stderr, "Unknown event %d\n", e.type);
1246 f5e234d6 2018-05-18 omar.polo }
1247 f5e234d6 2018-05-18 omar.polo }
1248 f5e234d6 2018-05-18 omar.polo
1249 f5e234d6 2018-05-18 omar.polo if (status == OK)
1250 f5e234d6 2018-05-18 omar.polo printf("%s\n", text);
1251 6da98882 2018-05-20 omar.polo
1252 e9f0b467 2018-06-07 omar.polo return exit_cleanup(&r, ps1, fontname, text, lines, cs, status == OK ? 0 : 1);
1253 f5e234d6 2018-05-18 omar.polo }