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