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