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