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