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