Blame


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