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