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