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 f6cb073d 2018-07-27 omar.polo int start_at = r->y_zero + r->padding*2 + height;
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 c9a3bfaa 2018-05-21 omar.polo
559 b5d751bd 2018-07-07 omar.polo struct completion *c = cs->completions;
560 b5d751bd 2018-07-07 omar.polo for (int i = 0; c != nil; ++i){
561 b5d751bd 2018-07-07 omar.polo enum text_type tt = cs->selected == i ? COMPL_HIGH : COMPL;
562 b5d751bd 2018-07-07 omar.polo GC h = cs->selected == i ? r->completion_highlighted_bg : r->completion_bg;
563 b5d751bd 2018-07-07 omar.polo
564 b5d751bd 2018-07-07 omar.polo int len = strlen(c->completion);
565 b5d751bd 2018-07-07 omar.polo text_extents(c->completion, len, r, &width, &height);
566 42c3f269 2018-07-08 omar.polo XFillRectangle(r->d, r->w, h, r->x_zero, start_at, inner_width(r), height + r->padding*2);
567 42c3f269 2018-07-08 omar.polo draw_string(c->completion, len, r->x_zero + r->padding, start_at + height + r->padding, r, tt);
568 36a15a9f 2018-05-19 omar.polo
569 e5186d6b 2018-05-26 omar.polo start_at += height + r->padding *2;
570 0ee198aa 2018-05-19 omar.polo
571 42c3f269 2018-07-08 omar.polo if (start_at > inner_height(r))
572 0ee198aa 2018-05-19 omar.polo break; // don't draw completion if the space isn't enough
573 0ee198aa 2018-05-19 omar.polo
574 b5d751bd 2018-07-07 omar.polo c = c->next;
575 36a15a9f 2018-05-19 omar.polo }
576 36a15a9f 2018-05-19 omar.polo }
577 36a15a9f 2018-05-19 omar.polo
578 36a15a9f 2018-05-19 omar.polo void draw(struct rendering *r, char *text, struct completions *cs) {
579 36a15a9f 2018-05-19 omar.polo if (r->horizontal_layout)
580 36a15a9f 2018-05-19 omar.polo draw_horizontally(r, text, cs);
581 36a15a9f 2018-05-19 omar.polo else
582 36a15a9f 2018-05-19 omar.polo draw_vertically(r, text, cs);
583 42c3f269 2018-07-08 omar.polo
584 42c3f269 2018-07-08 omar.polo // draw the borders
585 42c3f269 2018-07-08 omar.polo
586 42c3f269 2018-07-08 omar.polo if (r->border_w != 0)
587 42c3f269 2018-07-08 omar.polo XFillRectangle(r->d, r->w, r->border_w_bg, 0, 0, r->border_w, r->height);
588 42c3f269 2018-07-08 omar.polo
589 42c3f269 2018-07-08 omar.polo if (r->border_e != 0)
590 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);
591 42c3f269 2018-07-08 omar.polo
592 42c3f269 2018-07-08 omar.polo if (r->border_n != 0)
593 42c3f269 2018-07-08 omar.polo XFillRectangle(r->d, r->w, r->border_n_bg, 0, 0, r->width, r->border_n);
594 42c3f269 2018-07-08 omar.polo
595 42c3f269 2018-07-08 omar.polo if (r->border_s != 0)
596 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);
597 42c3f269 2018-07-08 omar.polo
598 42c3f269 2018-07-08 omar.polo // send all the work to x
599 42c3f269 2018-07-08 omar.polo XFlush(r->d);
600 36a15a9f 2018-05-19 omar.polo }
601 36a15a9f 2018-05-19 omar.polo
602 f5e234d6 2018-05-18 omar.polo /* Set some WM stuff */
603 f5e234d6 2018-05-18 omar.polo void set_win_atoms_hints(Display *d, Window w, int width, int height) {
604 f5e234d6 2018-05-18 omar.polo Atom type;
605 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_WINDOW_TYPE_DOCK", false);
606 f5e234d6 2018-05-18 omar.polo XChangeProperty(
607 f5e234d6 2018-05-18 omar.polo d,
608 f5e234d6 2018-05-18 omar.polo w,
609 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_WINDOW_TYPE", false),
610 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
611 f5e234d6 2018-05-18 omar.polo 32,
612 f5e234d6 2018-05-18 omar.polo PropModeReplace,
613 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
614 f5e234d6 2018-05-18 omar.polo 1
615 f5e234d6 2018-05-18 omar.polo );
616 f5e234d6 2018-05-18 omar.polo
617 f5e234d6 2018-05-18 omar.polo /* some window managers honor this properties */
618 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_STATE_ABOVE", false);
619 f5e234d6 2018-05-18 omar.polo XChangeProperty(d,
620 f5e234d6 2018-05-18 omar.polo w,
621 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_STATE", false),
622 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
623 f5e234d6 2018-05-18 omar.polo 32,
624 f5e234d6 2018-05-18 omar.polo PropModeReplace,
625 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
626 f5e234d6 2018-05-18 omar.polo 1
627 f5e234d6 2018-05-18 omar.polo );
628 f5e234d6 2018-05-18 omar.polo
629 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_STATE_FOCUSED", false);
630 f5e234d6 2018-05-18 omar.polo XChangeProperty(d,
631 f5e234d6 2018-05-18 omar.polo w,
632 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_STATE", false),
633 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
634 f5e234d6 2018-05-18 omar.polo 32,
635 f5e234d6 2018-05-18 omar.polo PropModeAppend,
636 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
637 f5e234d6 2018-05-18 omar.polo 1
638 f5e234d6 2018-05-18 omar.polo );
639 f5e234d6 2018-05-18 omar.polo
640 f5e234d6 2018-05-18 omar.polo // setting window hints
641 f5e234d6 2018-05-18 omar.polo XClassHint *class_hint = XAllocClassHint();
642 f5e234d6 2018-05-18 omar.polo if (class_hint == nil) {
643 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not allocate memory for class hint\n");
644 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
645 f5e234d6 2018-05-18 omar.polo }
646 7ef37ee0 2018-05-21 omar.polo class_hint->res_name = resname;
647 7ef37ee0 2018-05-21 omar.polo class_hint->res_class = resclass;
648 f5e234d6 2018-05-18 omar.polo XSetClassHint(d, w, class_hint);
649 f5e234d6 2018-05-18 omar.polo XFree(class_hint);
650 f5e234d6 2018-05-18 omar.polo
651 f5e234d6 2018-05-18 omar.polo XSizeHints *size_hint = XAllocSizeHints();
652 f5e234d6 2018-05-18 omar.polo if (size_hint == nil) {
653 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not allocate memory for size hint\n");
654 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
655 f5e234d6 2018-05-18 omar.polo }
656 7ef37ee0 2018-05-21 omar.polo size_hint->flags = PMinSize | PBaseSize;
657 f5e234d6 2018-05-18 omar.polo size_hint->min_width = width;
658 f5e234d6 2018-05-18 omar.polo size_hint->base_width = width;
659 f5e234d6 2018-05-18 omar.polo size_hint->min_height = height;
660 f5e234d6 2018-05-18 omar.polo size_hint->base_height = height;
661 f5e234d6 2018-05-18 omar.polo
662 f5e234d6 2018-05-18 omar.polo XFlush(d);
663 f5e234d6 2018-05-18 omar.polo }
664 f5e234d6 2018-05-18 omar.polo
665 8b929918 2018-07-01 omar.polo // write the width and height of the window `w' respectively in `width'
666 8b929918 2018-07-01 omar.polo // and `height'.
667 f5e234d6 2018-05-18 omar.polo void get_wh(Display *d, Window *w, int *width, int *height) {
668 f5e234d6 2018-05-18 omar.polo XWindowAttributes win_attr;
669 f5e234d6 2018-05-18 omar.polo XGetWindowAttributes(d, *w, &win_attr);
670 f5e234d6 2018-05-18 omar.polo *height = win_attr.height;
671 f5e234d6 2018-05-18 omar.polo *width = win_attr.width;
672 844addbb 2018-07-15 omar.polo }
673 844addbb 2018-07-15 omar.polo
674 844addbb 2018-07-15 omar.polo int grabfocus(Display *d, Window w) {
675 844addbb 2018-07-15 omar.polo for (int i = 0; i < 100; ++i) {
676 844addbb 2018-07-15 omar.polo Window focuswin;
677 844addbb 2018-07-15 omar.polo int revert_to_win;
678 844addbb 2018-07-15 omar.polo XGetInputFocus(d, &focuswin, &revert_to_win);
679 844addbb 2018-07-15 omar.polo if (focuswin == w)
680 844addbb 2018-07-15 omar.polo return true;
681 844addbb 2018-07-15 omar.polo XSetInputFocus(d, w, RevertToParent, CurrentTime);
682 844addbb 2018-07-15 omar.polo usleep(1000);
683 844addbb 2018-07-15 omar.polo }
684 844addbb 2018-07-15 omar.polo return 0;
685 f5e234d6 2018-05-18 omar.polo }
686 f5e234d6 2018-05-18 omar.polo
687 f5e234d6 2018-05-18 omar.polo // I know this may seem a little hackish BUT is the only way I managed
688 f5e234d6 2018-05-18 omar.polo // to actually grab that goddam keyboard. Only one call to
689 f5e234d6 2018-05-18 omar.polo // XGrabKeyboard does not always end up with the keyboard grabbed!
690 f5e234d6 2018-05-18 omar.polo int take_keyboard(Display *d, Window w) {
691 686e9237 2018-05-18 omar.polo int i;
692 686e9237 2018-05-18 omar.polo for (i = 0; i < 100; i++) {
693 686e9237 2018-05-18 omar.polo if (XGrabKeyboard(d, w, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
694 686e9237 2018-05-18 omar.polo return 1;
695 686e9237 2018-05-18 omar.polo usleep(1000);
696 686e9237 2018-05-18 omar.polo }
697 844addbb 2018-07-15 omar.polo fprintf(stderr, "Cannot grab keyboard\n");
698 686e9237 2018-05-18 omar.polo return 0;
699 f5e234d6 2018-05-18 omar.polo }
700 f5e234d6 2018-05-18 omar.polo
701 2128b469 2018-06-30 omar.polo // release the keyboard.
702 f5e234d6 2018-05-18 omar.polo void release_keyboard(Display *d) {
703 686e9237 2018-05-18 omar.polo XUngrabKeyboard(d, CurrentTime);
704 f5e234d6 2018-05-18 omar.polo }
705 f5e234d6 2018-05-18 omar.polo
706 8b929918 2018-07-01 omar.polo // Given a string, try to parse it as a number or return
707 8b929918 2018-07-01 omar.polo // `default_value'.
708 25bf99d2 2018-05-22 omar.polo int parse_integer(const char *str, int default_value) {
709 f5e234d6 2018-05-18 omar.polo errno = 0;
710 f5e234d6 2018-05-18 omar.polo char *ep;
711 f5e234d6 2018-05-18 omar.polo long lval = strtol(str, &ep, 10);
712 f5e234d6 2018-05-18 omar.polo if (str[0] == '\0' || *ep != '\0') { // NaN
713 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "'%s' is not a valid number! Using %d as default.\n", str, default_value);
714 f5e234d6 2018-05-18 omar.polo return default_value;
715 f5e234d6 2018-05-18 omar.polo }
716 f5e234d6 2018-05-18 omar.polo if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
717 f5e234d6 2018-05-18 omar.polo (lval > INT_MAX || lval < INT_MIN)) {
718 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "%s out of range! Using %d as default.\n", str, default_value);
719 f5e234d6 2018-05-18 omar.polo return default_value;
720 f5e234d6 2018-05-18 omar.polo }
721 f5e234d6 2018-05-18 omar.polo return lval;
722 25bf99d2 2018-05-22 omar.polo }
723 25bf99d2 2018-05-22 omar.polo
724 2128b469 2018-06-30 omar.polo // like parse_integer, but if the value ends with a `%' then its
725 2128b469 2018-06-30 omar.polo // treated like a percentage (`max' is used to compute the percentage)
726 25bf99d2 2018-05-22 omar.polo int parse_int_with_percentage(const char *str, int default_value, int max) {
727 25bf99d2 2018-05-22 omar.polo int len = strlen(str);
728 25bf99d2 2018-05-22 omar.polo if (len > 0 && str[len-1] == '%') {
729 25bf99d2 2018-05-22 omar.polo char *cpy = strdup(str);
730 25bf99d2 2018-05-22 omar.polo check_allocation(cpy);
731 25bf99d2 2018-05-22 omar.polo cpy[len-1] = '\0';
732 25bf99d2 2018-05-22 omar.polo int val = parse_integer(cpy, default_value);
733 25bf99d2 2018-05-22 omar.polo free(cpy);
734 25bf99d2 2018-05-22 omar.polo return val * max / 100;
735 25bf99d2 2018-05-22 omar.polo }
736 576ab141 2018-05-22 omar.polo return parse_integer(str, default_value);
737 f5e234d6 2018-05-18 omar.polo }
738 f5e234d6 2018-05-18 omar.polo
739 8e122ff1 2018-07-13 omar.polo // like parse_int_with_percentage but understands some special values
740 8e122ff1 2018-07-13 omar.polo // - "middle" that is (max - self) / 2
741 8e122ff1 2018-07-13 omar.polo // - "start" that is 0
742 8e122ff1 2018-07-13 omar.polo // - "end" that is (max - self)
743 8e122ff1 2018-07-13 omar.polo int parse_int_with_pos(const char *str, int default_value, int max, int self) {
744 8e122ff1 2018-07-13 omar.polo if (!strcmp(str, "start"))
745 8e122ff1 2018-07-13 omar.polo return 0;
746 8e122ff1 2018-07-13 omar.polo if (!strcmp(str, "middle"))
747 f5e234d6 2018-05-18 omar.polo return (max - self)/2;
748 8e122ff1 2018-07-13 omar.polo if (!strcmp(str, "end"))
749 8e122ff1 2018-07-13 omar.polo return max-self;
750 25bf99d2 2018-05-22 omar.polo return parse_int_with_percentage(str, default_value, max);
751 f5e234d6 2018-05-18 omar.polo }
752 f5e234d6 2018-05-18 omar.polo
753 42c3f269 2018-07-08 omar.polo // parse a string like a css value (for example like the css
754 42c3f269 2018-07-08 omar.polo // margin/padding properties). Will ALWAYS return an array of 4 word
755 42c3f269 2018-07-08 omar.polo // TODO: harden this function!
756 42c3f269 2018-07-08 omar.polo char **parse_csslike(const char *str) {
757 42c3f269 2018-07-08 omar.polo char *s = strdup(str);
758 42c3f269 2018-07-08 omar.polo if (s == nil)
759 42c3f269 2018-07-08 omar.polo return nil;
760 42c3f269 2018-07-08 omar.polo
761 42c3f269 2018-07-08 omar.polo char **ret = malloc(4 * sizeof(char*));
762 42c3f269 2018-07-08 omar.polo if (ret == nil) {
763 42c3f269 2018-07-08 omar.polo free(s);
764 42c3f269 2018-07-08 omar.polo return nil;
765 42c3f269 2018-07-08 omar.polo }
766 42c3f269 2018-07-08 omar.polo
767 42c3f269 2018-07-08 omar.polo int i = 0;
768 42c3f269 2018-07-08 omar.polo char *token;
769 42c3f269 2018-07-08 omar.polo while ((token = strsep(&s, " ")) != NULL && i < 4) {
770 42c3f269 2018-07-08 omar.polo ret[i] = strdup(token);
771 42c3f269 2018-07-08 omar.polo i++;
772 42c3f269 2018-07-08 omar.polo }
773 42c3f269 2018-07-08 omar.polo
774 42c3f269 2018-07-08 omar.polo if (i == 1)
775 42c3f269 2018-07-08 omar.polo for (int j = 1; j < 4; j++)
776 42c3f269 2018-07-08 omar.polo ret[j] = strdup(ret[0]);
777 42c3f269 2018-07-08 omar.polo
778 42c3f269 2018-07-08 omar.polo if (i == 2) {
779 42c3f269 2018-07-08 omar.polo ret[2] = strdup(ret[0]);
780 42c3f269 2018-07-08 omar.polo ret[3] = strdup(ret[1]);
781 42c3f269 2018-07-08 omar.polo }
782 42c3f269 2018-07-08 omar.polo
783 42c3f269 2018-07-08 omar.polo if (i == 3)
784 42c3f269 2018-07-08 omar.polo ret[3] = strdup(ret[1]);
785 42c3f269 2018-07-08 omar.polo
786 42c3f269 2018-07-08 omar.polo // Before we didn't check for the return type of strdup, here we will
787 42c3f269 2018-07-08 omar.polo
788 42c3f269 2018-07-08 omar.polo bool any_null = false;
789 42c3f269 2018-07-08 omar.polo for (int i = 0; i < 4; ++i)
790 42c3f269 2018-07-08 omar.polo any_null = ret[i] == nil || any_null;
791 42c3f269 2018-07-08 omar.polo
792 42c3f269 2018-07-08 omar.polo if (any_null)
793 42c3f269 2018-07-08 omar.polo for (int i = 0; i < 4; ++i)
794 42c3f269 2018-07-08 omar.polo if (ret[i] != nil)
795 42c3f269 2018-07-08 omar.polo free(ret[i]);
796 42c3f269 2018-07-08 omar.polo
797 42c3f269 2018-07-08 omar.polo if (i == 0 || any_null) {
798 42c3f269 2018-07-08 omar.polo free(s);
799 42c3f269 2018-07-08 omar.polo free(ret);
800 42c3f269 2018-07-08 omar.polo return nil;
801 42c3f269 2018-07-08 omar.polo }
802 42c3f269 2018-07-08 omar.polo
803 42c3f269 2018-07-08 omar.polo return ret;
804 42c3f269 2018-07-08 omar.polo }
805 42c3f269 2018-07-08 omar.polo
806 2128b469 2018-06-30 omar.polo // Given an event, try to understand what the user wants. If the
807 2128b469 2018-06-30 omar.polo // return value is ADD_CHAR then `input' is a pointer to a string that
808 2128b469 2018-06-30 omar.polo // will need to be free'ed.
809 2128b469 2018-06-30 omar.polo enum action parse_event(Display *d, XKeyPressedEvent *ev, XIC xic, char **input) {
810 2128b469 2018-06-30 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_BackSpace))
811 2128b469 2018-06-30 omar.polo return DEL_CHAR;
812 2128b469 2018-06-30 omar.polo
813 2128b469 2018-06-30 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Tab))
814 2128b469 2018-06-30 omar.polo return ev->state & ShiftMask ? PREV_COMPL : NEXT_COMPL;
815 2128b469 2018-06-30 omar.polo
816 2128b469 2018-06-30 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Return))
817 2128b469 2018-06-30 omar.polo return CONFIRM;
818 2128b469 2018-06-30 omar.polo
819 2128b469 2018-06-30 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Escape))
820 2128b469 2018-06-30 omar.polo return EXIT;
821 2128b469 2018-06-30 omar.polo
822 2128b469 2018-06-30 omar.polo // try to read what the user pressed
823 b5d751bd 2018-07-07 omar.polo char str[SYM_BUF_SIZE] = {0};
824 2128b469 2018-06-30 omar.polo Status s = 0;
825 b5d751bd 2018-07-07 omar.polo Xutf8LookupString(xic, ev, str, SYM_BUF_SIZE, 0, &s);
826 2128b469 2018-06-30 omar.polo if (s == XBufferOverflow) {
827 2128b469 2018-06-30 omar.polo // should not happen since there are no utf-8 characters larger
828 2128b469 2018-06-30 omar.polo // than 24bits
829 2128b469 2018-06-30 omar.polo fprintf(stderr, "Buffer overflow when trying to create keyboard symbol map.\n");
830 2128b469 2018-06-30 omar.polo return EXIT;
831 2128b469 2018-06-30 omar.polo }
832 2128b469 2018-06-30 omar.polo
833 2128b469 2018-06-30 omar.polo if (ev->state & ControlMask) {
834 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-u
835 2128b469 2018-06-30 omar.polo return DEL_LINE;
836 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-w
837 2128b469 2018-06-30 omar.polo return DEL_WORD;
838 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-h
839 2128b469 2018-06-30 omar.polo return DEL_CHAR;
840 2128b469 2018-06-30 omar.polo if (!strcmp(str, "\r")) // C-m
841 2128b469 2018-06-30 omar.polo return CONFIRM;
842 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-p
843 2128b469 2018-06-30 omar.polo return PREV_COMPL;
844 2128b469 2018-06-30 omar.polo if (!strcmp(str, "")) // C-n
845 2128b469 2018-06-30 omar.polo return NEXT_COMPL;
846 bee0837c 2018-07-01 omar.polo if (!strcmp(str, "")) // C-c
847 bee0837c 2018-07-01 omar.polo return EXIT;
848 6254fed8 2018-07-06 omar.polo if (!strcmp(str, "\t")) // C-i
849 6254fed8 2018-07-06 omar.polo return TOGGLE_FIRST_SELECTED;
850 2128b469 2018-06-30 omar.polo }
851 2128b469 2018-06-30 omar.polo
852 b5d751bd 2018-07-07 omar.polo *input = strdup(str);
853 2128b469 2018-06-30 omar.polo if (*input == nil) {
854 2128b469 2018-06-30 omar.polo fprintf(stderr, "Error while allocating memory for key.\n");
855 2128b469 2018-06-30 omar.polo return EXIT;
856 2128b469 2018-06-30 omar.polo }
857 2128b469 2018-06-30 omar.polo
858 2128b469 2018-06-30 omar.polo return ADD_CHAR;
859 2128b469 2018-06-30 omar.polo }
860 2128b469 2018-06-30 omar.polo
861 b5d751bd 2018-07-07 omar.polo // Given the name of the program (argv[0]?) print a small help on stderr
862 b5d751bd 2018-07-07 omar.polo void usage(char *prgname) {
863 844addbb 2018-07-15 omar.polo fprintf(stderr, "%s [-hva] [-p prompt] [-x coord] [-y coord] [-W width] [-H height]\n"
864 844addbb 2018-07-15 omar.polo " [-P padding] [-l layout] [-f font] [-b borders] [-B colors]\n"
865 844addbb 2018-07-15 omar.polo " [-t color] [-T color] [-c color] [-C color] [-s color] [-S color]\n"
866 844addbb 2018-07-15 omar.polo " [-w window_id]\n", prgname);
867 e9f0b467 2018-06-07 omar.polo }
868 e9f0b467 2018-06-07 omar.polo
869 e9f0b467 2018-06-07 omar.polo int main(int argc, char **argv) {
870 1ef91f4e 2018-07-03 omar.polo #ifdef HAVE_PLEDGE
871 1ef91f4e 2018-07-03 omar.polo // stdio & rpat: to read and write stdio/stdout
872 1ef91f4e 2018-07-03 omar.polo // unix: to connect to Xorg
873 1ef91f4e 2018-07-03 omar.polo pledge("stdio rpath unix", "");
874 1ef91f4e 2018-07-03 omar.polo #endif
875 3518f203 2018-07-21 omar.polo
876 3518f203 2018-07-21 omar.polo char *sep = nil;
877 1ef91f4e 2018-07-03 omar.polo
878 e9f0b467 2018-06-07 omar.polo // by default the first completion isn't selected
879 e9f0b467 2018-06-07 omar.polo bool first_selected = false;
880 e9f0b467 2018-06-07 omar.polo
881 c392d727 2018-07-15 omar.polo // our parent window
882 844addbb 2018-07-15 omar.polo char *parent_window_id = nil;
883 844addbb 2018-07-15 omar.polo
884 3518f203 2018-07-21 omar.polo // the user can input arbitrary text
885 3518f203 2018-07-21 omar.polo bool free_text = true;
886 3518f203 2018-07-21 omar.polo
887 844addbb 2018-07-15 omar.polo // first round of args parsing
888 e9f0b467 2018-06-07 omar.polo int ch;
889 ae801529 2018-07-13 omar.polo while ((ch = getopt(argc, argv, ARGS)) != -1) {
890 e9f0b467 2018-06-07 omar.polo switch (ch) {
891 844addbb 2018-07-15 omar.polo case 'h': // help
892 b10f01c1 2018-07-06 omar.polo usage(*argv);
893 b10f01c1 2018-07-06 omar.polo return 0;
894 844addbb 2018-07-15 omar.polo case 'v': // version
895 b10f01c1 2018-07-06 omar.polo fprintf(stderr, "%s version: %s\n", *argv, VERSION);
896 b10f01c1 2018-07-06 omar.polo return 0;
897 844addbb 2018-07-15 omar.polo case 'e': // embed
898 844addbb 2018-07-15 omar.polo parent_window_id = strdup(optarg);
899 844addbb 2018-07-15 omar.polo check_allocation(parent_window_id);
900 844addbb 2018-07-15 omar.polo break;
901 3518f203 2018-07-21 omar.polo case 'd': {
902 3518f203 2018-07-21 omar.polo sep = strdup(optarg);
903 3518f203 2018-07-21 omar.polo check_allocation(sep);
904 3518f203 2018-07-21 omar.polo }
905 3518f203 2018-07-21 omar.polo case 'A': {
906 3518f203 2018-07-21 omar.polo free_text = false;
907 3518f203 2018-07-21 omar.polo break;
908 3518f203 2018-07-21 omar.polo }
909 449f27a6 2018-07-06 omar.polo default:
910 ae801529 2018-07-13 omar.polo break;
911 e9f0b467 2018-06-07 omar.polo }
912 e9f0b467 2018-06-07 omar.polo }
913 e9f0b467 2018-06-07 omar.polo
914 c392d727 2018-07-15 omar.polo // read the lines from stdin
915 95b27a5e 2018-05-19 omar.polo char **lines = calloc(INITIAL_ITEMS, sizeof(char*));
916 3518f203 2018-07-21 omar.polo check_allocation(lines);
917 3518f203 2018-07-21 omar.polo int nlines = readlines(&lines, INITIAL_ITEMS);
918 3518f203 2018-07-21 omar.polo char **vlines = nil;
919 3518f203 2018-07-21 omar.polo if (sep != nil) {
920 3518f203 2018-07-21 omar.polo int l = strlen(sep);
921 3518f203 2018-07-21 omar.polo vlines = calloc(nlines, sizeof(char*));
922 3518f203 2018-07-21 omar.polo check_allocation(vlines);
923 f5e234d6 2018-05-18 omar.polo
924 3518f203 2018-07-21 omar.polo for (int i = 0; lines[i] != nil; i++) {
925 3518f203 2018-07-21 omar.polo char *t = strstr(lines[i], sep);
926 3518f203 2018-07-21 omar.polo if (t == nil)
927 3518f203 2018-07-21 omar.polo vlines[i] = lines[i];
928 3518f203 2018-07-21 omar.polo else
929 3518f203 2018-07-21 omar.polo vlines[i] = t + l;
930 3518f203 2018-07-21 omar.polo }
931 3518f203 2018-07-21 omar.polo }
932 3518f203 2018-07-21 omar.polo
933 f5e234d6 2018-05-18 omar.polo setlocale(LC_ALL, getenv("LANG"));
934 f5e234d6 2018-05-18 omar.polo
935 f5e234d6 2018-05-18 omar.polo enum state status = LOOPING;
936 f5e234d6 2018-05-18 omar.polo
937 f5e234d6 2018-05-18 omar.polo // where the monitor start (used only with xinerama)
938 f5e234d6 2018-05-18 omar.polo int offset_x = 0;
939 f5e234d6 2018-05-18 omar.polo int offset_y = 0;
940 f5e234d6 2018-05-18 omar.polo
941 f5e234d6 2018-05-18 omar.polo // width and height of the window
942 f5e234d6 2018-05-18 omar.polo int width = 400;
943 f5e234d6 2018-05-18 omar.polo int height = 20;
944 f5e234d6 2018-05-18 omar.polo
945 f5e234d6 2018-05-18 omar.polo // position on the screen
946 f5e234d6 2018-05-18 omar.polo int x = 0;
947 f5e234d6 2018-05-18 omar.polo int y = 0;
948 f5e234d6 2018-05-18 omar.polo
949 e9f0b467 2018-06-07 omar.polo // the default padding
950 e5186d6b 2018-05-26 omar.polo int padding = 10;
951 e5186d6b 2018-05-26 omar.polo
952 42c3f269 2018-07-08 omar.polo // the default borders
953 42c3f269 2018-07-08 omar.polo int border_n = 0;
954 42c3f269 2018-07-08 omar.polo int border_e = 0;
955 42c3f269 2018-07-08 omar.polo int border_s = 0;
956 42c3f269 2018-07-08 omar.polo int border_w = 0;
957 42c3f269 2018-07-08 omar.polo
958 42c3f269 2018-07-08 omar.polo // the prompt. We duplicate the string so later is easy to free (in
959 42c3f269 2018-07-08 omar.polo // the case the user provide its own prompt)
960 8758854a 2018-05-20 omar.polo char *ps1 = strdup("$ ");
961 8758854a 2018-05-20 omar.polo check_allocation(ps1);
962 8758854a 2018-05-20 omar.polo
963 42c3f269 2018-07-08 omar.polo // same for the font name
964 9e94fcbe 2018-05-22 omar.polo char *fontname = strdup(default_fontname);
965 f5e234d6 2018-05-18 omar.polo check_allocation(fontname);
966 f5e234d6 2018-05-18 omar.polo
967 f5e234d6 2018-05-18 omar.polo int textlen = 10;
968 f5e234d6 2018-05-18 omar.polo char *text = malloc(textlen * sizeof(char));
969 f5e234d6 2018-05-18 omar.polo check_allocation(text);
970 f5e234d6 2018-05-18 omar.polo
971 e9f0b467 2018-06-07 omar.polo /* struct completions *cs = filter(text, lines); */
972 b5d751bd 2018-07-07 omar.polo struct completions *cs = compls_new();
973 b5d751bd 2018-07-07 omar.polo check_allocation(cs);
974 f5e234d6 2018-05-18 omar.polo
975 f5e234d6 2018-05-18 omar.polo // start talking to xorg
976 f5e234d6 2018-05-18 omar.polo Display *d = XOpenDisplay(nil);
977 f5e234d6 2018-05-18 omar.polo if (d == nil) {
978 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not open display!\n");
979 f5e234d6 2018-05-18 omar.polo return EX_UNAVAILABLE;
980 f5e234d6 2018-05-18 omar.polo }
981 f5e234d6 2018-05-18 omar.polo
982 844addbb 2018-07-15 omar.polo Window parent_window;
983 844addbb 2018-07-15 omar.polo bool embed = true;
984 844addbb 2018-07-15 omar.polo if (! (parent_window_id && (parent_window = strtol(parent_window_id, nil, 0)))) {
985 844addbb 2018-07-15 omar.polo parent_window = DefaultRootWindow(d);
986 844addbb 2018-07-15 omar.polo embed = false;
987 844addbb 2018-07-15 omar.polo }
988 844addbb 2018-07-15 omar.polo
989 f5e234d6 2018-05-18 omar.polo // get display size
990 844addbb 2018-07-15 omar.polo int d_width;
991 844addbb 2018-07-15 omar.polo int d_height;
992 844addbb 2018-07-15 omar.polo get_wh(d, &parent_window, &d_width, &d_height);
993 f5e234d6 2018-05-18 omar.polo
994 f5e234d6 2018-05-18 omar.polo #ifdef USE_XINERAMA
995 844addbb 2018-07-15 omar.polo if (!embed && XineramaIsActive(d)) {
996 7ca8829b 2018-05-21 omar.polo // find the mice
997 7ca8829b 2018-05-21 omar.polo int number_of_screens = XScreenCount(d);
998 7ca8829b 2018-05-21 omar.polo Window r;
999 7ca8829b 2018-05-21 omar.polo Window root;
1000 7ca8829b 2018-05-21 omar.polo int root_x, root_y, win_x, win_y;
1001 7ca8829b 2018-05-21 omar.polo unsigned int mask;
1002 7ca8829b 2018-05-21 omar.polo bool res;
1003 7ca8829b 2018-05-21 omar.polo for (int i = 0; i < number_of_screens; ++i) {
1004 7ca8829b 2018-05-21 omar.polo root = XRootWindow(d, i);
1005 7ca8829b 2018-05-21 omar.polo res = XQueryPointer(d, root, &r, &r, &root_x, &root_y, &win_x, &win_y, &mask);
1006 7ca8829b 2018-05-21 omar.polo if (res) break;
1007 7ca8829b 2018-05-21 omar.polo }
1008 7ca8829b 2018-05-21 omar.polo if (!res) {
1009 7ca8829b 2018-05-21 omar.polo fprintf(stderr, "No mouse found.\n");
1010 7ca8829b 2018-05-21 omar.polo root_x = 0;
1011 7ca8829b 2018-05-21 omar.polo root_y = 0;
1012 7ca8829b 2018-05-21 omar.polo }
1013 7ca8829b 2018-05-21 omar.polo
1014 7ca8829b 2018-05-21 omar.polo // now find in which monitor the mice is on
1015 f5e234d6 2018-05-18 omar.polo int monitors;
1016 f5e234d6 2018-05-18 omar.polo XineramaScreenInfo *info = XineramaQueryScreens(d, &monitors);
1017 7ca8829b 2018-05-21 omar.polo if (info) {
1018 f5e234d6 2018-05-18 omar.polo for (int i = 0; i < monitors; ++i) {
1019 7ca8829b 2018-05-21 omar.polo if (info[i].x_org <= root_x && root_x <= (info[i].x_org + info[i].width)
1020 7ca8829b 2018-05-21 omar.polo && info[i].y_org <= root_y && root_y <= (info[i].y_org + info[i].height)) {
1021 7ca8829b 2018-05-21 omar.polo offset_x = info[i].x_org;
1022 7ca8829b 2018-05-21 omar.polo offset_y = info[i].y_org;
1023 f5e234d6 2018-05-18 omar.polo d_width = info[i].width;
1024 f5e234d6 2018-05-18 omar.polo d_height = info[i].height;
1025 7ca8829b 2018-05-21 omar.polo break;
1026 f5e234d6 2018-05-18 omar.polo }
1027 f5e234d6 2018-05-18 omar.polo }
1028 7ca8829b 2018-05-21 omar.polo }
1029 f5e234d6 2018-05-18 omar.polo XFree(info);
1030 f5e234d6 2018-05-18 omar.polo }
1031 f5e234d6 2018-05-18 omar.polo #endif
1032 f5e234d6 2018-05-18 omar.polo
1033 f5e234d6 2018-05-18 omar.polo Colormap cmap = DefaultColormap(d, DefaultScreen(d));
1034 f5e234d6 2018-05-18 omar.polo XColor p_fg, p_bg,
1035 f5e234d6 2018-05-18 omar.polo compl_fg, compl_bg,
1036 42c3f269 2018-07-08 omar.polo compl_highlighted_fg, compl_highlighted_bg,
1037 42c3f269 2018-07-08 omar.polo border_n_bg, border_e_bg, border_s_bg, border_w_bg;
1038 f5e234d6 2018-05-18 omar.polo
1039 36a15a9f 2018-05-19 omar.polo bool horizontal_layout = true;
1040 36a15a9f 2018-05-19 omar.polo
1041 f5e234d6 2018-05-18 omar.polo // read resource
1042 f5e234d6 2018-05-18 omar.polo XrmInitialize();
1043 f5e234d6 2018-05-18 omar.polo char *xrm = XResourceManagerString(d);
1044 347d23da 2018-05-19 omar.polo XrmDatabase xdb = nil;
1045 f5e234d6 2018-05-18 omar.polo if (xrm != nil) {
1046 347d23da 2018-05-19 omar.polo xdb = XrmGetStringDatabase(xrm);
1047 f5e234d6 2018-05-18 omar.polo XrmValue value;
1048 f5e234d6 2018-05-18 omar.polo char *datatype[20];
1049 f5e234d6 2018-05-18 omar.polo
1050 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.font", "*", datatype, &value) == true) {
1051 f5e234d6 2018-05-18 omar.polo fontname = strdup(value.addr);
1052 f5e234d6 2018-05-18 omar.polo check_allocation(fontname);
1053 c392d727 2018-07-15 omar.polo } else {
1054 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no font defined, using %s\n", fontname);
1055 c392d727 2018-07-15 omar.polo }
1056 36a15a9f 2018-05-19 omar.polo
1057 c392d727 2018-07-15 omar.polo if (XrmGetResource(xdb, "MyMenu.layout", "*", datatype, &value) == true)
1058 ae801529 2018-07-13 omar.polo horizontal_layout = !strcmp(value.addr, "horizontal");
1059 36a15a9f 2018-05-19 omar.polo else
1060 36a15a9f 2018-05-19 omar.polo fprintf(stderr, "no layout defined, using horizontal\n");
1061 f5e234d6 2018-05-18 omar.polo
1062 8758854a 2018-05-20 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt", "*", datatype, &value) == true) {
1063 8758854a 2018-05-20 omar.polo free(ps1);
1064 8758854a 2018-05-20 omar.polo ps1 = normalize_str(value.addr);
1065 c392d727 2018-07-15 omar.polo } else {
1066 8758854a 2018-05-20 omar.polo fprintf(stderr, "no prompt defined, using \"%s\" as default\n", ps1);
1067 c392d727 2018-07-15 omar.polo }
1068 8758854a 2018-05-20 omar.polo
1069 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.width", "*", datatype, &value) == true)
1070 25bf99d2 2018-05-22 omar.polo width = parse_int_with_percentage(value.addr, width, d_width);
1071 f5e234d6 2018-05-18 omar.polo else
1072 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no width defined, using %d\n", width);
1073 f5e234d6 2018-05-18 omar.polo
1074 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.height", "*", datatype, &value) == true)
1075 25bf99d2 2018-05-22 omar.polo height = parse_int_with_percentage(value.addr, height, d_height);
1076 f5e234d6 2018-05-18 omar.polo else
1077 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no height defined, using %d\n", height);
1078 f5e234d6 2018-05-18 omar.polo
1079 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.x", "*", datatype, &value) == true)
1080 8e122ff1 2018-07-13 omar.polo x = parse_int_with_pos(value.addr, x, d_width, width);
1081 f5e234d6 2018-05-18 omar.polo else
1082 e5186d6b 2018-05-26 omar.polo fprintf(stderr, "no x defined, using %d\n", x);
1083 f5e234d6 2018-05-18 omar.polo
1084 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.y", "*", datatype, &value) == true)
1085 8e122ff1 2018-07-13 omar.polo y = parse_int_with_pos(value.addr, y, d_height, height);
1086 f5e234d6 2018-05-18 omar.polo else
1087 e5186d6b 2018-05-26 omar.polo fprintf(stderr, "no y defined, using %d\n", y);
1088 f5e234d6 2018-05-18 omar.polo
1089 e5186d6b 2018-05-26 omar.polo if (XrmGetResource(xdb, "MyMenu.padding", "*", datatype, &value) == true)
1090 e5186d6b 2018-05-26 omar.polo padding = parse_integer(value.addr, padding);
1091 e5186d6b 2018-05-26 omar.polo else
1092 26b1f485 2018-07-03 omar.polo fprintf(stderr, "no padding defined, using %d\n", padding);
1093 e5186d6b 2018-05-26 omar.polo
1094 42c3f269 2018-07-08 omar.polo if (XrmGetResource(xdb, "MyMenu.border.size", "*", datatype, &value) == true) {
1095 42c3f269 2018-07-08 omar.polo char **borders = parse_csslike(value.addr);
1096 42c3f269 2018-07-08 omar.polo if (borders != nil) {
1097 42c3f269 2018-07-08 omar.polo border_n = parse_integer(borders[0], 0);
1098 42c3f269 2018-07-08 omar.polo border_e = parse_integer(borders[1], 0);
1099 42c3f269 2018-07-08 omar.polo border_s = parse_integer(borders[2], 0);
1100 42c3f269 2018-07-08 omar.polo border_w = parse_integer(borders[3], 0);
1101 42c3f269 2018-07-08 omar.polo } else {
1102 42c3f269 2018-07-08 omar.polo fprintf(stderr, "error while parsing MyMenu.border.size\n");
1103 42c3f269 2018-07-08 omar.polo }
1104 42c3f269 2018-07-08 omar.polo } else {
1105 42c3f269 2018-07-08 omar.polo fprintf(stderr, "no border defined, using 0.\n");
1106 42c3f269 2018-07-08 omar.polo }
1107 42c3f269 2018-07-08 omar.polo
1108 f5e234d6 2018-05-18 omar.polo XColor tmp;
1109 f5e234d6 2018-05-18 omar.polo // TODO: tmp needs to be free'd after every allocation?
1110 f5e234d6 2018-05-18 omar.polo
1111 f5e234d6 2018-05-18 omar.polo // prompt
1112 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.foreground", "*", datatype, &value) == true)
1113 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &p_fg, &tmp);
1114 f5e234d6 2018-05-18 omar.polo else
1115 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &p_fg, &tmp);
1116 f5e234d6 2018-05-18 omar.polo
1117 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.background", "*", datatype, &value) == true)
1118 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &p_bg, &tmp);
1119 f5e234d6 2018-05-18 omar.polo else
1120 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &p_bg, &tmp);
1121 f5e234d6 2018-05-18 omar.polo
1122 f5e234d6 2018-05-18 omar.polo // completion
1123 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.foreground", "*", datatype, &value) == true)
1124 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_fg, &tmp);
1125 f5e234d6 2018-05-18 omar.polo else
1126 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_fg, &tmp);
1127 f5e234d6 2018-05-18 omar.polo
1128 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.background", "*", datatype, &value) == true)
1129 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_bg, &tmp);
1130 f5e234d6 2018-05-18 omar.polo else
1131 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_bg, &tmp);
1132 f5e234d6 2018-05-18 omar.polo
1133 f5e234d6 2018-05-18 omar.polo // completion highlighted
1134 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion_highlighted.foreground", "*", datatype, &value) == true)
1135 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_highlighted_fg, &tmp);
1136 f5e234d6 2018-05-18 omar.polo else
1137 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_highlighted_fg, &tmp);
1138 f5e234d6 2018-05-18 omar.polo
1139 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion_highlighted.background", "*", datatype, &value) == true)
1140 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_highlighted_bg, &tmp);
1141 f5e234d6 2018-05-18 omar.polo else
1142 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_highlighted_bg, &tmp);
1143 42c3f269 2018-07-08 omar.polo
1144 42c3f269 2018-07-08 omar.polo // border
1145 42c3f269 2018-07-08 omar.polo if (XrmGetResource(xdb, "MyMenu.border.color", "*", datatype, &value) == true) {
1146 42c3f269 2018-07-08 omar.polo char **colors = parse_csslike(value.addr);
1147 42c3f269 2018-07-08 omar.polo if (colors != nil) {
1148 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, colors[0], &border_n_bg, &tmp);
1149 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, colors[1], &border_e_bg, &tmp);
1150 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, colors[2], &border_s_bg, &tmp);
1151 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, colors[3], &border_w_bg, &tmp);
1152 42c3f269 2018-07-08 omar.polo } else {
1153 ae801529 2018-07-13 omar.polo fprintf(stderr, "error while parsing MyMenu.border.color\n");
1154 42c3f269 2018-07-08 omar.polo }
1155 42c3f269 2018-07-08 omar.polo } else {
1156 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, "white", &border_n_bg, &tmp);
1157 b0d7c215 2018-07-13 omar.polo XAllocNamedColor(d, cmap, "white", &border_e_bg, &tmp);
1158 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, "white", &border_s_bg, &tmp);
1159 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, "white", &border_w_bg, &tmp);
1160 42c3f269 2018-07-08 omar.polo }
1161 f5e234d6 2018-05-18 omar.polo } else {
1162 f5e234d6 2018-05-18 omar.polo XColor tmp;
1163 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &p_fg, &tmp);
1164 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &p_bg, &tmp);
1165 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_fg, &tmp);
1166 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_bg, &tmp);
1167 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_highlighted_fg, &tmp);
1168 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, "white", &border_n_bg, &tmp);
1169 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, "white", &border_e_bg, &tmp);
1170 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, "white", &border_s_bg, &tmp);
1171 42c3f269 2018-07-08 omar.polo XAllocNamedColor(d, cmap, "white", &border_w_bg, &tmp);
1172 f5e234d6 2018-05-18 omar.polo }
1173 f5e234d6 2018-05-18 omar.polo
1174 ae801529 2018-07-13 omar.polo // second round of args parsing
1175 ae801529 2018-07-13 omar.polo optind = 0; // reset the option index
1176 ae801529 2018-07-13 omar.polo while ((ch = getopt(argc, argv, ARGS)) != -1) {
1177 ae801529 2018-07-13 omar.polo switch (ch) {
1178 ae801529 2018-07-13 omar.polo case 'a':
1179 ae801529 2018-07-13 omar.polo first_selected = true;
1180 ae801529 2018-07-13 omar.polo break;
1181 3518f203 2018-07-21 omar.polo case 'A':
1182 3518f203 2018-07-21 omar.polo // free_text -- this case was already catched
1183 3518f203 2018-07-21 omar.polo break;
1184 3518f203 2018-07-21 omar.polo case 'd':
1185 3518f203 2018-07-21 omar.polo // separator -- this case was already catched
1186 3518f203 2018-07-21 omar.polo break;
1187 844addbb 2018-07-15 omar.polo case 'e':
1188 844addbb 2018-07-15 omar.polo // (embedding mymenu) this case was already catched.
1189 844addbb 2018-07-15 omar.polo break;
1190 ae801529 2018-07-13 omar.polo case 'p': {
1191 ae801529 2018-07-13 omar.polo char *newprompt = strdup(optarg);
1192 ae801529 2018-07-13 omar.polo if (newprompt != nil) {
1193 ae801529 2018-07-13 omar.polo free(ps1);
1194 ae801529 2018-07-13 omar.polo ps1 = newprompt;
1195 ae801529 2018-07-13 omar.polo }
1196 ae801529 2018-07-13 omar.polo break;
1197 ae801529 2018-07-13 omar.polo }
1198 ae801529 2018-07-13 omar.polo case 'x':
1199 ae801529 2018-07-13 omar.polo x = parse_int_with_pos(optarg, x, d_width, width);
1200 ae801529 2018-07-13 omar.polo break;
1201 ae801529 2018-07-13 omar.polo case 'y':
1202 ae801529 2018-07-13 omar.polo y = parse_int_with_pos(optarg, y, d_height, height);
1203 ae801529 2018-07-13 omar.polo break;
1204 ae801529 2018-07-13 omar.polo case 'P':
1205 ae801529 2018-07-13 omar.polo padding = parse_integer(optarg, padding);
1206 ae801529 2018-07-13 omar.polo break;
1207 ae801529 2018-07-13 omar.polo case 'l':
1208 ae801529 2018-07-13 omar.polo horizontal_layout = !strcmp(optarg, "horizontal");
1209 ae801529 2018-07-13 omar.polo break;
1210 ae801529 2018-07-13 omar.polo case 'f': {
1211 ae801529 2018-07-13 omar.polo char *newfont = strdup(optarg);
1212 ae801529 2018-07-13 omar.polo if (newfont != nil) {
1213 ae801529 2018-07-13 omar.polo free(fontname);
1214 ae801529 2018-07-13 omar.polo fontname = newfont;
1215 ae801529 2018-07-13 omar.polo }
1216 ae801529 2018-07-13 omar.polo break;
1217 ae801529 2018-07-13 omar.polo }
1218 844addbb 2018-07-15 omar.polo case 'W':
1219 ae801529 2018-07-13 omar.polo width = parse_int_with_percentage(optarg, width, d_width);
1220 ae801529 2018-07-13 omar.polo break;
1221 844addbb 2018-07-15 omar.polo case 'H':
1222 ae801529 2018-07-13 omar.polo height = parse_int_with_percentage(optarg, height, d_height);
1223 ae801529 2018-07-13 omar.polo break;
1224 ae801529 2018-07-13 omar.polo case 'b': {
1225 ae801529 2018-07-13 omar.polo char **borders = parse_csslike(optarg);
1226 ae801529 2018-07-13 omar.polo if (borders != nil) {
1227 ae801529 2018-07-13 omar.polo border_n = parse_integer(borders[0], 0);
1228 ae801529 2018-07-13 omar.polo border_e = parse_integer(borders[1], 0);
1229 ae801529 2018-07-13 omar.polo border_s = parse_integer(borders[2], 0);
1230 ae801529 2018-07-13 omar.polo border_w = parse_integer(borders[3], 0);
1231 ae801529 2018-07-13 omar.polo } else {
1232 ae801529 2018-07-13 omar.polo fprintf(stderr, "Error parsing b option\n");
1233 ae801529 2018-07-13 omar.polo }
1234 ae801529 2018-07-13 omar.polo break;
1235 ae801529 2018-07-13 omar.polo }
1236 ae801529 2018-07-13 omar.polo case 'B': {
1237 ae801529 2018-07-13 omar.polo char **colors = parse_csslike(optarg);
1238 ae801529 2018-07-13 omar.polo if (colors != nil) {
1239 ae801529 2018-07-13 omar.polo XColor tmp;
1240 ae801529 2018-07-13 omar.polo XAllocNamedColor(d, cmap, colors[0], &border_n_bg, &tmp);
1241 ae801529 2018-07-13 omar.polo XAllocNamedColor(d, cmap, colors[1], &border_e_bg, &tmp);
1242 ae801529 2018-07-13 omar.polo XAllocNamedColor(d, cmap, colors[2], &border_s_bg, &tmp);
1243 ae801529 2018-07-13 omar.polo XAllocNamedColor(d, cmap, colors[3], &border_w_bg, &tmp);
1244 ae801529 2018-07-13 omar.polo } else {
1245 ae801529 2018-07-13 omar.polo fprintf(stderr, "error while parsing B option\n");
1246 ae801529 2018-07-13 omar.polo }
1247 ae801529 2018-07-13 omar.polo break;
1248 ae801529 2018-07-13 omar.polo }
1249 ae801529 2018-07-13 omar.polo case 't': {
1250 ae801529 2018-07-13 omar.polo XColor tmp;
1251 ae801529 2018-07-13 omar.polo XAllocNamedColor(d, cmap, optarg, &p_fg, &tmp);
1252 ae801529 2018-07-13 omar.polo break;
1253 ae801529 2018-07-13 omar.polo }
1254 ae801529 2018-07-13 omar.polo case 'T': {
1255 ae801529 2018-07-13 omar.polo XColor tmp;
1256 ae801529 2018-07-13 omar.polo XAllocNamedColor(d, cmap, optarg, &p_bg, &tmp);
1257 ae801529 2018-07-13 omar.polo break;
1258 ae801529 2018-07-13 omar.polo }
1259 ae801529 2018-07-13 omar.polo case 'c': {
1260 ae801529 2018-07-13 omar.polo XColor tmp;
1261 ae801529 2018-07-13 omar.polo XAllocNamedColor(d, cmap, optarg, &compl_fg, &tmp);
1262 ae801529 2018-07-13 omar.polo break;
1263 ae801529 2018-07-13 omar.polo }
1264 ae801529 2018-07-13 omar.polo case 'C': {
1265 ae801529 2018-07-13 omar.polo XColor tmp;
1266 ae801529 2018-07-13 omar.polo XAllocNamedColor(d, cmap, optarg, &compl_bg, &tmp);
1267 ae801529 2018-07-13 omar.polo break;
1268 ae801529 2018-07-13 omar.polo }
1269 ae801529 2018-07-13 omar.polo case 's': {
1270 ae801529 2018-07-13 omar.polo XColor tmp;
1271 ae801529 2018-07-13 omar.polo XAllocNamedColor(d, cmap, optarg, &compl_highlighted_fg, &tmp);
1272 ae801529 2018-07-13 omar.polo break;
1273 ae801529 2018-07-13 omar.polo }
1274 ae801529 2018-07-13 omar.polo case 'S': {
1275 ae801529 2018-07-13 omar.polo XColor tmp;
1276 ae801529 2018-07-13 omar.polo XAllocNamedColor(d, cmap, optarg, &compl_highlighted_bg, &tmp);
1277 ae801529 2018-07-13 omar.polo break;
1278 ae801529 2018-07-13 omar.polo }
1279 ae801529 2018-07-13 omar.polo default:
1280 ae801529 2018-07-13 omar.polo fprintf(stderr, "Unrecognized option %c\n", ch);
1281 ae801529 2018-07-13 omar.polo status = ERR;
1282 ae801529 2018-07-13 omar.polo break;
1283 ae801529 2018-07-13 omar.polo }
1284 ae801529 2018-07-13 omar.polo }
1285 ae801529 2018-07-13 omar.polo
1286 ae801529 2018-07-13 omar.polo // since only now we know if the first should be selected, update
1287 ae801529 2018-07-13 omar.polo // the completion here
1288 3518f203 2018-07-21 omar.polo update_completions(cs, text, lines, vlines, first_selected);
1289 ae801529 2018-07-13 omar.polo
1290 f5e234d6 2018-05-18 omar.polo // load the font
1291 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
1292 c9a3bfaa 2018-05-21 omar.polo XftFont *font = XftFontOpenName(d, DefaultScreen(d), fontname);
1293 c9a3bfaa 2018-05-21 omar.polo #else
1294 347d23da 2018-05-19 omar.polo char **missing_charset_list;
1295 347d23da 2018-05-19 omar.polo int missing_charset_count;
1296 347d23da 2018-05-19 omar.polo XFontSet font = XCreateFontSet(d, fontname, &missing_charset_list, &missing_charset_count, nil);
1297 f5e234d6 2018-05-18 omar.polo if (font == nil) {
1298 347d23da 2018-05-19 omar.polo fprintf(stderr, "Unable to load the font(s) %s\n", fontname);
1299 347d23da 2018-05-19 omar.polo return EX_UNAVAILABLE;
1300 f5e234d6 2018-05-18 omar.polo }
1301 c9a3bfaa 2018-05-21 omar.polo #endif
1302 f5e234d6 2018-05-18 omar.polo
1303 f5e234d6 2018-05-18 omar.polo // create the window
1304 f5e234d6 2018-05-18 omar.polo XSetWindowAttributes attr;
1305 55bd6842 2018-05-21 omar.polo attr.override_redirect = true;
1306 844addbb 2018-07-15 omar.polo attr.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
1307 f5e234d6 2018-05-18 omar.polo
1308 f5e234d6 2018-05-18 omar.polo Window w = XCreateWindow(d, // display
1309 844addbb 2018-07-15 omar.polo parent_window, // parent
1310 f5e234d6 2018-05-18 omar.polo x + offset_x, y + offset_y, // x y
1311 f5e234d6 2018-05-18 omar.polo width, height, // w h
1312 f5e234d6 2018-05-18 omar.polo 0, // border width
1313 844addbb 2018-07-15 omar.polo CopyFromParent, // depth
1314 f5e234d6 2018-05-18 omar.polo InputOutput, // class
1315 844addbb 2018-07-15 omar.polo CopyFromParent, // visual
1316 844addbb 2018-07-15 omar.polo CWEventMask | CWOverrideRedirect, // value mask (CWBackPixel in the future also?)
1317 f5e234d6 2018-05-18 omar.polo &attr);
1318 f5e234d6 2018-05-18 omar.polo
1319 f5e234d6 2018-05-18 omar.polo set_win_atoms_hints(d, w, width, height);
1320 f5e234d6 2018-05-18 omar.polo
1321 f5e234d6 2018-05-18 omar.polo // we want some events
1322 c9a3bfaa 2018-05-21 omar.polo XSelectInput(d, w, StructureNotifyMask | KeyPressMask | KeymapStateMask);
1323 844addbb 2018-07-15 omar.polo XMapRaised(d, w);
1324 f5e234d6 2018-05-18 omar.polo
1325 844addbb 2018-07-15 omar.polo // if embed, listen for other events as well
1326 844addbb 2018-07-15 omar.polo if (embed) {
1327 844addbb 2018-07-15 omar.polo XSelectInput(d, parent_window, FocusChangeMask);
1328 844addbb 2018-07-15 omar.polo Window *children, parent, root;
1329 844addbb 2018-07-15 omar.polo unsigned int children_no;
1330 844addbb 2018-07-15 omar.polo if (XQueryTree(d, parent_window, &root, &parent, &children, &children_no) && children) {
1331 844addbb 2018-07-15 omar.polo for (unsigned int i = 0; i < children_no && children[i] != w; ++i)
1332 844addbb 2018-07-15 omar.polo XSelectInput(d, children[i], FocusChangeMask);
1333 844addbb 2018-07-15 omar.polo XFree(children);
1334 844addbb 2018-07-15 omar.polo }
1335 844addbb 2018-07-15 omar.polo grabfocus(d, w);
1336 f5e234d6 2018-05-18 omar.polo }
1337 f5e234d6 2018-05-18 omar.polo
1338 f5e234d6 2018-05-18 omar.polo // grab keyboard
1339 f5e234d6 2018-05-18 omar.polo take_keyboard(d, w);
1340 f5e234d6 2018-05-18 omar.polo
1341 f5e234d6 2018-05-18 omar.polo // Create some graphics contexts
1342 f5e234d6 2018-05-18 omar.polo XGCValues values;
1343 347d23da 2018-05-19 omar.polo /* values.font = font->fid; */
1344 f5e234d6 2018-05-18 omar.polo
1345 f5e234d6 2018-05-18 omar.polo struct rendering r = {
1346 f5e234d6 2018-05-18 omar.polo .d = d,
1347 f5e234d6 2018-05-18 omar.polo .w = w,
1348 42c3f269 2018-07-08 omar.polo .width = width,
1349 42c3f269 2018-07-08 omar.polo .height = height,
1350 42c3f269 2018-07-08 omar.polo .padding = padding,
1351 42c3f269 2018-07-08 omar.polo .x_zero = border_w,
1352 42c3f269 2018-07-08 omar.polo .y_zero = border_n,
1353 42c3f269 2018-07-08 omar.polo .border_n = border_n,
1354 42c3f269 2018-07-08 omar.polo .border_e = border_e,
1355 42c3f269 2018-07-08 omar.polo .border_s = border_s,
1356 42c3f269 2018-07-08 omar.polo .border_w = border_w,
1357 42c3f269 2018-07-08 omar.polo .horizontal_layout = horizontal_layout,
1358 42c3f269 2018-07-08 omar.polo .ps1 = ps1,
1359 42c3f269 2018-07-08 omar.polo .ps1len = strlen(ps1),
1360 347d23da 2018-05-19 omar.polo .prompt = XCreateGC(d, w, 0, &values),
1361 347d23da 2018-05-19 omar.polo .prompt_bg = XCreateGC(d, w, 0, &values),
1362 347d23da 2018-05-19 omar.polo .completion = XCreateGC(d, w, 0, &values),
1363 347d23da 2018-05-19 omar.polo .completion_bg = XCreateGC(d, w, 0, &values),
1364 347d23da 2018-05-19 omar.polo .completion_highlighted = XCreateGC(d, w, 0, &values),
1365 347d23da 2018-05-19 omar.polo .completion_highlighted_bg = XCreateGC(d, w, 0, &values),
1366 42c3f269 2018-07-08 omar.polo .border_n_bg = XCreateGC(d, w, 0, &values),
1367 42c3f269 2018-07-08 omar.polo .border_e_bg = XCreateGC(d, w, 0, &values),
1368 42c3f269 2018-07-08 omar.polo .border_s_bg = XCreateGC(d, w, 0, &values),
1369 42c3f269 2018-07-08 omar.polo .border_w_bg = XCreateGC(d, w, 0, &values),
1370 42c3f269 2018-07-08 omar.polo #ifdef USE_XFT
1371 42c3f269 2018-07-08 omar.polo .font = font,
1372 42c3f269 2018-07-08 omar.polo #else
1373 42c3f269 2018-07-08 omar.polo .font = &font,
1374 42c3f269 2018-07-08 omar.polo #endif
1375 f5e234d6 2018-05-18 omar.polo };
1376 f5e234d6 2018-05-18 omar.polo
1377 7ca8829b 2018-05-21 omar.polo #ifdef USE_XFT
1378 7ca8829b 2018-05-21 omar.polo r.xftdraw = XftDrawCreate(d, w, DefaultVisual(d, 0), DefaultColormap(d, 0));
1379 c9a3bfaa 2018-05-21 omar.polo
1380 7ca8829b 2018-05-21 omar.polo // prompt
1381 7ca8829b 2018-05-21 omar.polo XRenderColor xrcolor;
1382 7ca8829b 2018-05-21 omar.polo xrcolor.red = p_fg.red;
1383 7ca8829b 2018-05-21 omar.polo xrcolor.green = p_fg.red;
1384 7ca8829b 2018-05-21 omar.polo xrcolor.blue = p_fg.red;
1385 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
1386 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_prompt);
1387 c9a3bfaa 2018-05-21 omar.polo
1388 7ca8829b 2018-05-21 omar.polo // completion
1389 7ca8829b 2018-05-21 omar.polo xrcolor.red = compl_fg.red;
1390 7ca8829b 2018-05-21 omar.polo xrcolor.green = compl_fg.green;
1391 7ca8829b 2018-05-21 omar.polo xrcolor.blue = compl_fg.blue;
1392 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
1393 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_completion);
1394 7ca8829b 2018-05-21 omar.polo
1395 7ca8829b 2018-05-21 omar.polo // completion highlighted
1396 7ca8829b 2018-05-21 omar.polo xrcolor.red = compl_highlighted_fg.red;
1397 7ca8829b 2018-05-21 omar.polo xrcolor.green = compl_highlighted_fg.green;
1398 7ca8829b 2018-05-21 omar.polo xrcolor.blue = compl_highlighted_fg.blue;
1399 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
1400 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_completion_highlighted);
1401 c9a3bfaa 2018-05-21 omar.polo #endif
1402 7ca8829b 2018-05-21 omar.polo
1403 f5e234d6 2018-05-18 omar.polo // load the colors in our GCs
1404 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.prompt, p_fg.pixel);
1405 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.prompt_bg, p_bg.pixel);
1406 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion, compl_fg.pixel);
1407 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_bg, compl_bg.pixel);
1408 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_highlighted, compl_highlighted_fg.pixel);
1409 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_highlighted_bg, compl_highlighted_bg.pixel);
1410 42c3f269 2018-07-08 omar.polo XSetForeground(d, r.border_n_bg, border_n_bg.pixel);
1411 42c3f269 2018-07-08 omar.polo XSetForeground(d, r.border_e_bg, border_e_bg.pixel);
1412 42c3f269 2018-07-08 omar.polo XSetForeground(d, r.border_s_bg, border_s_bg.pixel);
1413 42c3f269 2018-07-08 omar.polo XSetForeground(d, r.border_w_bg, border_w_bg.pixel);
1414 347d23da 2018-05-19 omar.polo
1415 347d23da 2018-05-19 omar.polo // open the X input method
1416 347d23da 2018-05-19 omar.polo XIM xim = XOpenIM(d, xdb, resname, resclass);
1417 347d23da 2018-05-19 omar.polo check_allocation(xim);
1418 f5e234d6 2018-05-18 omar.polo
1419 347d23da 2018-05-19 omar.polo XIMStyles *xis = nil;
1420 347d23da 2018-05-19 omar.polo if (XGetIMValues(xim, XNQueryInputStyle, &xis, NULL) || !xis) {
1421 347d23da 2018-05-19 omar.polo fprintf(stderr, "Input Styles could not be retrieved\n");
1422 347d23da 2018-05-19 omar.polo return EX_UNAVAILABLE;
1423 347d23da 2018-05-19 omar.polo }
1424 347d23da 2018-05-19 omar.polo
1425 347d23da 2018-05-19 omar.polo XIMStyle bestMatchStyle = 0;
1426 347d23da 2018-05-19 omar.polo for (int i = 0; i < xis->count_styles; ++i) {
1427 347d23da 2018-05-19 omar.polo XIMStyle ts = xis->supported_styles[i];
1428 347d23da 2018-05-19 omar.polo if (ts == (XIMPreeditNothing | XIMStatusNothing)) {
1429 347d23da 2018-05-19 omar.polo bestMatchStyle = ts;
1430 347d23da 2018-05-19 omar.polo break;
1431 347d23da 2018-05-19 omar.polo }
1432 347d23da 2018-05-19 omar.polo }
1433 347d23da 2018-05-19 omar.polo XFree(xis);
1434 347d23da 2018-05-19 omar.polo
1435 347d23da 2018-05-19 omar.polo if (!bestMatchStyle) {
1436 347d23da 2018-05-19 omar.polo fprintf(stderr, "No matching input style could be determined\n");
1437 347d23da 2018-05-19 omar.polo }
1438 347d23da 2018-05-19 omar.polo
1439 347d23da 2018-05-19 omar.polo XIC xic = XCreateIC(xim, XNInputStyle, bestMatchStyle, XNClientWindow, w, XNFocusWindow, w, NULL);
1440 347d23da 2018-05-19 omar.polo check_allocation(xic);
1441 347d23da 2018-05-19 omar.polo
1442 f5e234d6 2018-05-18 omar.polo // draw the window for the first time
1443 f5e234d6 2018-05-18 omar.polo draw(&r, text, cs);
1444 f5e234d6 2018-05-18 omar.polo
1445 f5e234d6 2018-05-18 omar.polo // main loop
1446 f5e234d6 2018-05-18 omar.polo while (status == LOOPING) {
1447 f5e234d6 2018-05-18 omar.polo XEvent e;
1448 f5e234d6 2018-05-18 omar.polo XNextEvent(d, &e);
1449 f5e234d6 2018-05-18 omar.polo
1450 347d23da 2018-05-19 omar.polo if (XFilterEvent(&e, w))
1451 347d23da 2018-05-19 omar.polo continue;
1452 347d23da 2018-05-19 omar.polo
1453 f5e234d6 2018-05-18 omar.polo switch (e.type) {
1454 449f27a6 2018-07-06 omar.polo case KeymapNotify:
1455 b10f01c1 2018-07-06 omar.polo XRefreshKeyboardMapping(&e.xmapping);
1456 844addbb 2018-07-15 omar.polo break;
1457 844addbb 2018-07-15 omar.polo
1458 844addbb 2018-07-15 omar.polo case FocusIn:
1459 844addbb 2018-07-15 omar.polo // re-grab focus
1460 844addbb 2018-07-15 omar.polo if (e.xfocus.window != w)
1461 844addbb 2018-07-15 omar.polo grabfocus(d, w);
1462 b10f01c1 2018-07-06 omar.polo break;
1463 f5e234d6 2018-05-18 omar.polo
1464 844addbb 2018-07-15 omar.polo case VisibilityNotify:
1465 844addbb 2018-07-15 omar.polo if (e.xvisibility.state != VisibilityUnobscured)
1466 844addbb 2018-07-15 omar.polo XRaiseWindow(d, w);
1467 844addbb 2018-07-15 omar.polo break;
1468 844addbb 2018-07-15 omar.polo
1469 844addbb 2018-07-15 omar.polo case MapNotify:
1470 844addbb 2018-07-15 omar.polo /* fprintf(stderr, "Map Notify!\n"); */
1471 844addbb 2018-07-15 omar.polo /* TODO: update the computed window and height! */
1472 844addbb 2018-07-15 omar.polo /* get_wh(d, &w, width, height); */
1473 844addbb 2018-07-15 omar.polo draw(&r, text, cs);
1474 844addbb 2018-07-15 omar.polo break;
1475 844addbb 2018-07-15 omar.polo
1476 449f27a6 2018-07-06 omar.polo case KeyPress: {
1477 b10f01c1 2018-07-06 omar.polo XKeyPressedEvent *ev = (XKeyPressedEvent*)&e;
1478 14291de3 2018-07-03 omar.polo
1479 b10f01c1 2018-07-06 omar.polo char *input;
1480 b10f01c1 2018-07-06 omar.polo switch (parse_event(d, ev, xic, &input)) {
1481 b10f01c1 2018-07-06 omar.polo case EXIT:
1482 b10f01c1 2018-07-06 omar.polo status = ERR;
1483 b10f01c1 2018-07-06 omar.polo break;
1484 f5e234d6 2018-05-18 omar.polo
1485 3518f203 2018-07-21 omar.polo case CONFIRM: {
1486 b10f01c1 2018-07-06 omar.polo status = OK;
1487 3518f203 2018-07-21 omar.polo if ((cs->selected != -1) || (cs->lenght > 0 && first_selected)) {
1488 3518f203 2018-07-21 omar.polo // if there is something selected expand it and return
1489 3518f203 2018-07-21 omar.polo int index = cs->selected == -1 ? 0 : cs->selected;
1490 3518f203 2018-07-21 omar.polo struct completion *c = cs->completions;
1491 3518f203 2018-07-21 omar.polo while (true) {
1492 3518f203 2018-07-21 omar.polo if (index == 0)
1493 3518f203 2018-07-21 omar.polo break;
1494 3518f203 2018-07-21 omar.polo c = c->next;
1495 3518f203 2018-07-21 omar.polo index--;
1496 3518f203 2018-07-21 omar.polo }
1497 3518f203 2018-07-21 omar.polo char *t = c->rcompletion;
1498 3518f203 2018-07-21 omar.polo free(text);
1499 3518f203 2018-07-21 omar.polo text = strdup(t);
1500 b10f01c1 2018-07-06 omar.polo if (text == nil) {
1501 3518f203 2018-07-21 omar.polo fprintf(stderr, "Memory allocation error\n");
1502 b10f01c1 2018-07-06 omar.polo status = ERR;
1503 b10f01c1 2018-07-06 omar.polo }
1504 b10f01c1 2018-07-06 omar.polo textlen = strlen(text);
1505 3518f203 2018-07-21 omar.polo } else {
1506 3518f203 2018-07-21 omar.polo if (!free_text) {
1507 3518f203 2018-07-21 omar.polo // cannot accept arbitrary text
1508 3518f203 2018-07-21 omar.polo status = LOOPING;
1509 3518f203 2018-07-21 omar.polo }
1510 b10f01c1 2018-07-06 omar.polo }
1511 b10f01c1 2018-07-06 omar.polo break;
1512 3518f203 2018-07-21 omar.polo }
1513 347d23da 2018-05-19 omar.polo
1514 b10f01c1 2018-07-06 omar.polo case PREV_COMPL: {
1515 b10f01c1 2018-07-06 omar.polo complete(cs, first_selected, true, &text, &textlen, &status);
1516 b10f01c1 2018-07-06 omar.polo break;
1517 b10f01c1 2018-07-06 omar.polo }
1518 2128b469 2018-06-30 omar.polo
1519 b10f01c1 2018-07-06 omar.polo case NEXT_COMPL: {
1520 b10f01c1 2018-07-06 omar.polo complete(cs, first_selected, false, &text, &textlen, &status);
1521 b10f01c1 2018-07-06 omar.polo break;
1522 b10f01c1 2018-07-06 omar.polo }
1523 b10f01c1 2018-07-06 omar.polo
1524 b10f01c1 2018-07-06 omar.polo case DEL_CHAR:
1525 c392d727 2018-07-15 omar.polo popc(text);
1526 3518f203 2018-07-21 omar.polo update_completions(cs, text, lines, vlines, first_selected);
1527 b10f01c1 2018-07-06 omar.polo break;
1528 f5e234d6 2018-05-18 omar.polo
1529 b10f01c1 2018-07-06 omar.polo case DEL_WORD: {
1530 1e4bc498 2018-07-15 omar.polo popw(text);
1531 3518f203 2018-07-21 omar.polo update_completions(cs, text, lines, vlines, first_selected);
1532 b10f01c1 2018-07-06 omar.polo break;
1533 b10f01c1 2018-07-06 omar.polo }
1534 449f27a6 2018-07-06 omar.polo
1535 b10f01c1 2018-07-06 omar.polo case DEL_LINE: {
1536 b10f01c1 2018-07-06 omar.polo for (int i = 0; i < textlen; ++i)
1537 b10f01c1 2018-07-06 omar.polo text[i] = 0;
1538 3518f203 2018-07-21 omar.polo update_completions(cs, text, lines, vlines, first_selected);
1539 b10f01c1 2018-07-06 omar.polo break;
1540 b10f01c1 2018-07-06 omar.polo }
1541 449f27a6 2018-07-06 omar.polo
1542 b10f01c1 2018-07-06 omar.polo case ADD_CHAR: {
1543 b10f01c1 2018-07-06 omar.polo int str_len = strlen(input);
1544 b5d751bd 2018-07-07 omar.polo
1545 b5d751bd 2018-07-07 omar.polo // sometimes a strange key is pressed (i.e. ctrl alone),
1546 b5d751bd 2018-07-07 omar.polo // so input will be empty. Don't need to update completion
1547 b5d751bd 2018-07-07 omar.polo // in this case
1548 b5d751bd 2018-07-07 omar.polo if (str_len == 0)
1549 b5d751bd 2018-07-07 omar.polo break;
1550 b5d751bd 2018-07-07 omar.polo
1551 b10f01c1 2018-07-06 omar.polo for (int i = 0; i < str_len; ++i) {
1552 b10f01c1 2018-07-06 omar.polo textlen = pushc(&text, textlen, input[i]);
1553 b10f01c1 2018-07-06 omar.polo if (textlen == -1) {
1554 b10f01c1 2018-07-06 omar.polo fprintf(stderr, "Memory allocation error\n");
1555 b10f01c1 2018-07-06 omar.polo status = ERR;
1556 b10f01c1 2018-07-06 omar.polo break;
1557 b10f01c1 2018-07-06 omar.polo }
1558 b5d751bd 2018-07-07 omar.polo }
1559 b5d751bd 2018-07-07 omar.polo if (status != ERR) {
1560 3518f203 2018-07-21 omar.polo update_completions(cs, text, lines, vlines, first_selected);
1561 b10f01c1 2018-07-06 omar.polo free(input);
1562 b10f01c1 2018-07-06 omar.polo }
1563 b10f01c1 2018-07-06 omar.polo break;
1564 b10f01c1 2018-07-06 omar.polo }
1565 6254fed8 2018-07-06 omar.polo
1566 6254fed8 2018-07-06 omar.polo case TOGGLE_FIRST_SELECTED:
1567 6254fed8 2018-07-06 omar.polo first_selected = !first_selected;
1568 b5d751bd 2018-07-07 omar.polo if (first_selected && cs->selected < 0)
1569 b5d751bd 2018-07-07 omar.polo cs->selected = 0;
1570 b5d751bd 2018-07-07 omar.polo if (!first_selected && cs->selected == 0)
1571 b5d751bd 2018-07-07 omar.polo cs->selected = -1;
1572 6254fed8 2018-07-06 omar.polo break;
1573 b10f01c1 2018-07-06 omar.polo }
1574 2128b469 2018-06-30 omar.polo }
1575 347d23da 2018-05-19 omar.polo }
1576 f5e234d6 2018-05-18 omar.polo
1577 14291de3 2018-07-03 omar.polo draw(&r, text, cs);
1578 f5e234d6 2018-05-18 omar.polo }
1579 f5e234d6 2018-05-18 omar.polo
1580 f5e234d6 2018-05-18 omar.polo if (status == OK)
1581 f5e234d6 2018-05-18 omar.polo printf("%s\n", text);
1582 6da98882 2018-05-20 omar.polo
1583 b5d751bd 2018-07-07 omar.polo release_keyboard(r.d);
1584 b5d751bd 2018-07-07 omar.polo
1585 b5d751bd 2018-07-07 omar.polo #ifdef USE_XFT
1586 b5d751bd 2018-07-07 omar.polo XftColorFree(r.d, DefaultVisual(r.d, 0), DefaultColormap(r.d, 0), &r.xft_prompt);
1587 b5d751bd 2018-07-07 omar.polo XftColorFree(r.d, DefaultVisual(r.d, 0), DefaultColormap(r.d, 0), &r.xft_completion);
1588 b5d751bd 2018-07-07 omar.polo XftColorFree(r.d, DefaultVisual(r.d, 0), DefaultColormap(r.d, 0), &r.xft_completion_highlighted);
1589 b5d751bd 2018-07-07 omar.polo #endif
1590 b5d751bd 2018-07-07 omar.polo
1591 b5d751bd 2018-07-07 omar.polo free(ps1);
1592 b5d751bd 2018-07-07 omar.polo free(fontname);
1593 b5d751bd 2018-07-07 omar.polo free(text);
1594 b5d751bd 2018-07-07 omar.polo
1595 b5d751bd 2018-07-07 omar.polo char *l = nil;
1596 b5d751bd 2018-07-07 omar.polo char **lns = lines;
1597 b5d751bd 2018-07-07 omar.polo while ((l = *lns) != nil) {
1598 b5d751bd 2018-07-07 omar.polo free(l);
1599 b5d751bd 2018-07-07 omar.polo ++lns;
1600 b5d751bd 2018-07-07 omar.polo }
1601 b5d751bd 2018-07-07 omar.polo
1602 b5d751bd 2018-07-07 omar.polo free(lines);
1603 3518f203 2018-07-21 omar.polo free(vlines);
1604 b5d751bd 2018-07-07 omar.polo compls_delete(cs);
1605 b5d751bd 2018-07-07 omar.polo
1606 b5d751bd 2018-07-07 omar.polo XDestroyWindow(r.d, r.w);
1607 b5d751bd 2018-07-07 omar.polo XCloseDisplay(r.d);
1608 b5d751bd 2018-07-07 omar.polo
1609 3518f203 2018-07-21 omar.polo return status != OK;
1610 f5e234d6 2018-05-18 omar.polo }