Blame


1 c758d8da 2019-10-18 omar.polo #include <ctype.h> /* isalnum */
2 92803b00 2019-10-19 omar.polo #include <err.h>
3 c758d8da 2019-10-18 omar.polo #include <errno.h>
4 c758d8da 2019-10-18 omar.polo #include <limits.h>
5 c758d8da 2019-10-18 omar.polo #include <locale.h> /* setlocale */
6 c758d8da 2019-10-18 omar.polo #include <stdint.h>
7 f5e234d6 2018-05-18 omar.polo #include <stdio.h>
8 f5e234d6 2018-05-18 omar.polo #include <stdlib.h>
9 c758d8da 2019-10-18 omar.polo #include <string.h> /* strdup, strlen */
10 f5e234d6 2018-05-18 omar.polo #include <sysexits.h>
11 1a4491e5 2018-09-19 omar.polo #include <unistd.h>
12 f5e234d6 2018-05-18 omar.polo
13 c758d8da 2019-10-18 omar.polo #include <X11/Xcms.h>
14 f5e234d6 2018-05-18 omar.polo #include <X11/Xlib.h>
15 f5e234d6 2018-05-18 omar.polo #include <X11/Xresource.h>
16 c758d8da 2019-10-18 omar.polo #include <X11/Xutil.h>
17 347d23da 2018-05-19 omar.polo #include <X11/keysym.h>
18 f5e234d6 2018-05-18 omar.polo
19 f5e234d6 2018-05-18 omar.polo #ifdef USE_XINERAMA
20 c758d8da 2019-10-18 omar.polo #include <X11/extensions/Xinerama.h>
21 f5e234d6 2018-05-18 omar.polo #endif
22 f5e234d6 2018-05-18 omar.polo
23 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
24 c758d8da 2019-10-18 omar.polo #include <X11/Xft/Xft.h>
25 b500fe86 2018-06-07 omar.polo #endif
26 b500fe86 2018-06-07 omar.polo
27 b500fe86 2018-06-07 omar.polo #ifndef VERSION
28 c758d8da 2019-10-18 omar.polo #define VERSION "unknown"
29 c9a3bfaa 2018-05-21 omar.polo #endif
30 c9a3bfaa 2018-05-21 omar.polo
31 347d23da 2018-05-19 omar.polo #define resname "MyMenu"
32 347d23da 2018-05-19 omar.polo #define resclass "mymenu"
33 f5e234d6 2018-05-18 omar.polo
34 b5d751bd 2018-07-07 omar.polo #define SYM_BUF_SIZE 4
35 b5d751bd 2018-07-07 omar.polo
36 9e94fcbe 2018-05-22 omar.polo #ifdef USE_XFT
37 c758d8da 2019-10-18 omar.polo #define default_fontname "monospace"
38 9e94fcbe 2018-05-22 omar.polo #else
39 c758d8da 2019-10-18 omar.polo #define default_fontname "fixed"
40 9e94fcbe 2018-05-22 omar.polo #endif
41 ae801529 2018-07-13 omar.polo
42 df29e05e 2018-10-17 omar.polo #define ARGS "Aahmve:p:P:l:f:W:H:x:y:b:B:t:T:c:C:s:S:d:G:g:I:i:J:j:"
43 9e94fcbe 2018-05-22 omar.polo
44 9e94fcbe 2018-05-22 omar.polo #define MIN(a, b) ((a) < (b) ? (a) : (b))
45 f5e234d6 2018-05-18 omar.polo #define MAX(a, b) ((a) > (b) ? (a) : (b))
46 9e94fcbe 2018-05-22 omar.polo
47 5e448381 2018-10-09 omar.polo #define EXPANDBITS(x) (((x & 0xf0) * 0x100) | (x & 0x0f) * 0x10)
48 6bb186a7 2018-09-13 omar.polo
49 1d13acf5 2018-10-04 omar.polo /*
50 1d13acf5 2018-10-04 omar.polo * If we don't have or we don't want an "ignore case" completion
51 1d13acf5 2018-10-04 omar.polo * style, fall back to `strstr(3)`
52 1d13acf5 2018-10-04 omar.polo */
53 9e94fcbe 2018-05-22 omar.polo #ifndef USE_STRCASESTR
54 c758d8da 2019-10-18 omar.polo #define strcasestr strstr
55 9e94fcbe 2018-05-22 omar.polo #endif
56 f5e234d6 2018-05-18 omar.polo
57 1d13acf5 2018-10-04 omar.polo /* The number of char to read */
58 4f769efa 2018-10-06 omar.polo #define STDIN_CHUNKS 128
59 1a4491e5 2018-09-19 omar.polo
60 1d13acf5 2018-10-04 omar.polo /* The number of lines to allocate in advance */
61 4f769efa 2018-10-06 omar.polo #define LINES_CHUNK 64
62 f5e234d6 2018-05-18 omar.polo
63 4c0d79a0 2018-10-09 omar.polo #define inner_height(r) (r->height - r->borders[0] - r->borders[2])
64 c758d8da 2019-10-18 omar.polo #define inner_width(r) (r->width - r->borders[1] - r->borders[3])
65 42c3f269 2018-07-08 omar.polo
66 1d13acf5 2018-10-04 omar.polo /* The states of the event loop */
67 c758d8da 2019-10-18 omar.polo enum state { LOOPING, OK_LOOP, OK, ERR };
68 f5e234d6 2018-05-18 omar.polo
69 c758d8da 2019-10-18 omar.polo /*
70 1d13acf5 2018-10-04 omar.polo * For the drawing-related function. The text to be rendere could be
71 1d13acf5 2018-10-04 omar.polo * the prompt, a completion or a highlighted completion
72 1d13acf5 2018-10-04 omar.polo */
73 c758d8da 2019-10-18 omar.polo enum obj_type { PROMPT, COMPL, COMPL_HIGH };
74 c9a3bfaa 2018-05-21 omar.polo
75 1d13acf5 2018-10-04 omar.polo /* These are the possible action to be performed after user input. */
76 2128b469 2018-06-30 omar.polo enum action {
77 a81dbe5d 2019-10-19 omar.polo NO_OP,
78 1d13acf5 2018-10-04 omar.polo EXIT,
79 1d13acf5 2018-10-04 omar.polo CONFIRM,
80 1d13acf5 2018-10-04 omar.polo CONFIRM_CONTINUE,
81 1d13acf5 2018-10-04 omar.polo NEXT_COMPL,
82 1d13acf5 2018-10-04 omar.polo PREV_COMPL,
83 1d13acf5 2018-10-04 omar.polo DEL_CHAR,
84 1d13acf5 2018-10-04 omar.polo DEL_WORD,
85 1d13acf5 2018-10-04 omar.polo DEL_LINE,
86 1d13acf5 2018-10-04 omar.polo ADD_CHAR,
87 a81dbe5d 2019-10-19 omar.polo TOGGLE_FIRST_SELECTED,
88 a81dbe5d 2019-10-19 omar.polo SCROLL_DOWN,
89 a81dbe5d 2019-10-19 omar.polo SCROLL_UP,
90 2128b469 2018-06-30 omar.polo };
91 2128b469 2018-06-30 omar.polo
92 1d13acf5 2018-10-04 omar.polo /* A big set of values that needs to be carried around for drawing. A
93 a81dbe5d 2019-10-19 omar.polo * big struct to rule them all */
94 f5e234d6 2018-05-18 omar.polo struct rendering {
95 c758d8da 2019-10-18 omar.polo Display *d; /* Connection to xorg */
96 c758d8da 2019-10-18 omar.polo Window w;
97 c758d8da 2019-10-18 omar.polo XIM xim;
98 c758d8da 2019-10-18 omar.polo int width;
99 c758d8da 2019-10-18 omar.polo int height;
100 c758d8da 2019-10-18 omar.polo int p_padding[4];
101 c758d8da 2019-10-18 omar.polo int c_padding[4];
102 c758d8da 2019-10-18 omar.polo int ch_padding[4];
103 c758d8da 2019-10-18 omar.polo int x_zero; /* the "zero" on the x axis (may not be exactly 0 'cause
104 c758d8da 2019-10-18 omar.polo the borders) */
105 c758d8da 2019-10-18 omar.polo int y_zero; /* like x_zero but for the y axis */
106 11e67c66 2018-08-11 omar.polo
107 a81dbe5d 2019-10-19 omar.polo int offset; /* scroll offset */
108 42c3f269 2018-07-08 omar.polo
109 c758d8da 2019-10-18 omar.polo short free_text;
110 c758d8da 2019-10-18 omar.polo short first_selected;
111 c758d8da 2019-10-18 omar.polo short multiple_select;
112 991c5d3c 2018-08-13 omar.polo
113 1d13acf5 2018-10-04 omar.polo /* four border width */
114 c758d8da 2019-10-18 omar.polo int borders[4];
115 c758d8da 2019-10-18 omar.polo int p_borders[4];
116 c758d8da 2019-10-18 omar.polo int c_borders[4];
117 c758d8da 2019-10-18 omar.polo int ch_borders[4];
118 42c3f269 2018-07-08 omar.polo
119 c758d8da 2019-10-18 omar.polo short horizontal_layout;
120 42c3f269 2018-07-08 omar.polo
121 1d13acf5 2018-10-04 omar.polo /* prompt */
122 c758d8da 2019-10-18 omar.polo char *ps1;
123 c758d8da 2019-10-18 omar.polo int ps1len;
124 c758d8da 2019-10-18 omar.polo int ps1w; /* ps1 width */
125 c758d8da 2019-10-18 omar.polo int ps1h; /* ps1 height */
126 42c3f269 2018-07-08 omar.polo
127 c758d8da 2019-10-18 omar.polo int text_height; /* cache for the vertical layout */
128 df29e05e 2018-10-17 omar.polo
129 c758d8da 2019-10-18 omar.polo XIC xic;
130 991c5d3c 2018-08-13 omar.polo
131 1d13acf5 2018-10-04 omar.polo /* colors */
132 c758d8da 2019-10-18 omar.polo GC fgs[4];
133 c758d8da 2019-10-18 omar.polo GC bgs[4];
134 c758d8da 2019-10-18 omar.polo GC borders_bg[4];
135 c758d8da 2019-10-18 omar.polo GC p_borders_bg[4];
136 c758d8da 2019-10-18 omar.polo GC c_borders_bg[4];
137 c758d8da 2019-10-18 omar.polo GC ch_borders_bg[4];
138 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
139 c758d8da 2019-10-18 omar.polo XftFont *font;
140 c758d8da 2019-10-18 omar.polo XftDraw *xftdraw;
141 c758d8da 2019-10-18 omar.polo XftColor xft_colors[3];
142 c9a3bfaa 2018-05-21 omar.polo #else
143 c758d8da 2019-10-18 omar.polo XFontSet font;
144 c9a3bfaa 2018-05-21 omar.polo #endif
145 f5e234d6 2018-05-18 omar.polo };
146 f5e234d6 2018-05-18 omar.polo
147 b5d751bd 2018-07-07 omar.polo struct completion {
148 c758d8da 2019-10-18 omar.polo char *completion;
149 c758d8da 2019-10-18 omar.polo char *rcompletion;
150 a81dbe5d 2019-10-19 omar.polo int offset; /* the x (or y, depending on the layout) coordinate at
151 a81dbe5d 2019-10-19 omar.polo which the item is rendered */
152 f5e234d6 2018-05-18 omar.polo };
153 f5e234d6 2018-05-18 omar.polo
154 1d13acf5 2018-10-04 omar.polo /* Wrap the linked list of completions */
155 b5d751bd 2018-07-07 omar.polo struct completions {
156 c758d8da 2019-10-18 omar.polo struct completion *completions;
157 c758d8da 2019-10-18 omar.polo ssize_t selected;
158 c758d8da 2019-10-18 omar.polo size_t length;
159 b5d751bd 2018-07-07 omar.polo };
160 b5d751bd 2018-07-07 omar.polo
161 6bb186a7 2018-09-13 omar.polo /* idea stolen from lemonbar. ty lemonboy */
162 6bb186a7 2018-09-13 omar.polo typedef union {
163 1d13acf5 2018-10-04 omar.polo struct {
164 c758d8da 2019-10-18 omar.polo uint8_t b;
165 c758d8da 2019-10-18 omar.polo uint8_t g;
166 c758d8da 2019-10-18 omar.polo uint8_t r;
167 c758d8da 2019-10-18 omar.polo uint8_t a;
168 4f769efa 2018-10-06 omar.polo } rgba;
169 c758d8da 2019-10-18 omar.polo uint32_t v;
170 6bb186a7 2018-09-13 omar.polo } rgba_t;
171 6bb186a7 2018-09-13 omar.polo
172 c758d8da 2019-10-18 omar.polo extern char *optarg;
173 c758d8da 2019-10-18 omar.polo extern int optind;
174 2dba87e5 2018-10-09 omar.polo
175 1d13acf5 2018-10-04 omar.polo /* Return a newly allocated (and empty) completion list */
176 1d13acf5 2018-10-04 omar.polo struct completions *
177 1d13acf5 2018-10-04 omar.polo compls_new(size_t length)
178 1d13acf5 2018-10-04 omar.polo {
179 1d13acf5 2018-10-04 omar.polo struct completions *cs = malloc(sizeof(struct completions));
180 347d23da 2018-05-19 omar.polo
181 1d13acf5 2018-10-04 omar.polo if (cs == NULL)
182 1d13acf5 2018-10-04 omar.polo return cs;
183 347d23da 2018-05-19 omar.polo
184 1d13acf5 2018-10-04 omar.polo cs->completions = calloc(length, sizeof(struct completion));
185 1d13acf5 2018-10-04 omar.polo if (cs->completions == NULL) {
186 1d13acf5 2018-10-04 omar.polo free(cs);
187 1d13acf5 2018-10-04 omar.polo return NULL;
188 1d13acf5 2018-10-04 omar.polo }
189 36319ab7 2018-07-01 omar.polo
190 1d13acf5 2018-10-04 omar.polo cs->selected = -1;
191 1d13acf5 2018-10-04 omar.polo cs->length = length;
192 1d13acf5 2018-10-04 omar.polo return cs;
193 1d13acf5 2018-10-04 omar.polo }
194 3518f203 2018-07-21 omar.polo
195 1d13acf5 2018-10-04 omar.polo /* Delete the wrapper and the whole list */
196 1d13acf5 2018-10-04 omar.polo void
197 1d13acf5 2018-10-04 omar.polo compls_delete(struct completions *cs)
198 1d13acf5 2018-10-04 omar.polo {
199 1d13acf5 2018-10-04 omar.polo if (cs == NULL)
200 1d13acf5 2018-10-04 omar.polo return;
201 ddd1263a 2018-09-24 omar.polo
202 1d13acf5 2018-10-04 omar.polo free(cs->completions);
203 1d13acf5 2018-10-04 omar.polo free(cs);
204 1d13acf5 2018-10-04 omar.polo }
205 f5e234d6 2018-05-18 omar.polo
206 c758d8da 2019-10-18 omar.polo /*
207 1d13acf5 2018-10-04 omar.polo * Create a completion list from a text and the list of possible
208 1d13acf5 2018-10-04 omar.polo * completions (null terminated). Expects a non-null `cs'. `lines' and
209 1d13acf5 2018-10-04 omar.polo * `vlines' should have the same length OR `vlines' is NULL.
210 1d13acf5 2018-10-04 omar.polo */
211 1d13acf5 2018-10-04 omar.polo void
212 1d13acf5 2018-10-04 omar.polo filter(struct completions *cs, char *text, char **lines, char **vlines)
213 1d13acf5 2018-10-04 omar.polo {
214 c758d8da 2019-10-18 omar.polo size_t index = 0;
215 c758d8da 2019-10-18 omar.polo size_t matching = 0;
216 c758d8da 2019-10-18 omar.polo char *l;
217 f5e234d6 2018-05-18 omar.polo
218 1d13acf5 2018-10-04 omar.polo if (vlines == NULL)
219 1d13acf5 2018-10-04 omar.polo vlines = lines;
220 f5e234d6 2018-05-18 omar.polo
221 26b541d8 2018-10-04 omar.polo while (1) {
222 1d13acf5 2018-10-04 omar.polo if (lines[index] == NULL)
223 1d13acf5 2018-10-04 omar.polo break;
224 2128b469 2018-06-30 omar.polo
225 4f769efa 2018-10-06 omar.polo l = vlines[index] != NULL ? vlines[index] : lines[index];
226 b5d751bd 2018-07-07 omar.polo
227 1d13acf5 2018-10-04 omar.polo if (strcasestr(l, text) != NULL) {
228 1d13acf5 2018-10-04 omar.polo struct completion *c = &cs->completions[matching];
229 c758d8da 2019-10-18 omar.polo c->completion = l;
230 1d13acf5 2018-10-04 omar.polo c->rcompletion = lines[index];
231 1d13acf5 2018-10-04 omar.polo matching++;
232 1d13acf5 2018-10-04 omar.polo }
233 36319ab7 2018-07-01 omar.polo
234 1d13acf5 2018-10-04 omar.polo index++;
235 1d13acf5 2018-10-04 omar.polo }
236 1d13acf5 2018-10-04 omar.polo cs->length = matching;
237 1d13acf5 2018-10-04 omar.polo cs->selected = -1;
238 1d13acf5 2018-10-04 omar.polo }
239 2128b469 2018-06-30 omar.polo
240 1d13acf5 2018-10-04 omar.polo /* Update the given completion */
241 1d13acf5 2018-10-04 omar.polo void
242 c758d8da 2019-10-18 omar.polo update_completions(struct completions *cs, char *text, char **lines,
243 c758d8da 2019-10-18 omar.polo char **vlines, short first_selected)
244 1d13acf5 2018-10-04 omar.polo {
245 1d13acf5 2018-10-04 omar.polo filter(cs, text, lines, vlines);
246 1d13acf5 2018-10-04 omar.polo if (first_selected && cs->length > 0)
247 1d13acf5 2018-10-04 omar.polo cs->selected = 0;
248 1d13acf5 2018-10-04 omar.polo }
249 b5d751bd 2018-07-07 omar.polo
250 1d13acf5 2018-10-04 omar.polo /*
251 1d13acf5 2018-10-04 omar.polo * Select the next or previous selection and update some state. `text'
252 1d13acf5 2018-10-04 omar.polo * will be updated with the text of the completion and `textlen' with
253 1d13acf5 2018-10-04 omar.polo * the new length. If the memory cannot be allocated `status' will be
254 1d13acf5 2018-10-04 omar.polo * set to `ERR'.
255 1d13acf5 2018-10-04 omar.polo */
256 1d13acf5 2018-10-04 omar.polo void
257 c758d8da 2019-10-18 omar.polo complete(struct completions *cs, short first_selected, short p, char **text,
258 c758d8da 2019-10-18 omar.polo int *textlen, enum state *status)
259 1d13acf5 2018-10-04 omar.polo {
260 c758d8da 2019-10-18 omar.polo struct completion *n;
261 c758d8da 2019-10-18 omar.polo int index;
262 b5d751bd 2018-07-07 omar.polo
263 1d13acf5 2018-10-04 omar.polo if (cs == NULL || cs->length == 0)
264 1d13acf5 2018-10-04 omar.polo return;
265 2128b469 2018-06-30 omar.polo
266 1d13acf5 2018-10-04 omar.polo /*
267 1d13acf5 2018-10-04 omar.polo * If the first is always selected and the first entry is
268 1d13acf5 2018-10-04 omar.polo * different from the text, expand the text and return
269 1d13acf5 2018-10-04 omar.polo */
270 c758d8da 2019-10-18 omar.polo if (first_selected && cs->selected == 0
271 c758d8da 2019-10-18 omar.polo && strcmp(cs->completions->completion, *text) != 0 && !p) {
272 1d13acf5 2018-10-04 omar.polo free(*text);
273 1d13acf5 2018-10-04 omar.polo *text = strdup(cs->completions->completion);
274 1d13acf5 2018-10-04 omar.polo if (text == NULL) {
275 1d13acf5 2018-10-04 omar.polo *status = ERR;
276 1d13acf5 2018-10-04 omar.polo return;
277 1d13acf5 2018-10-04 omar.polo }
278 1d13acf5 2018-10-04 omar.polo *textlen = strlen(*text);
279 1d13acf5 2018-10-04 omar.polo return;
280 1d13acf5 2018-10-04 omar.polo }
281 f5e234d6 2018-05-18 omar.polo
282 1d13acf5 2018-10-04 omar.polo index = cs->selected;
283 f5e234d6 2018-05-18 omar.polo
284 1d13acf5 2018-10-04 omar.polo if (index == -1 && p)
285 1d13acf5 2018-10-04 omar.polo index = 0;
286 c758d8da 2019-10-18 omar.polo index = cs->selected
287 c758d8da 2019-10-18 omar.polo = (cs->length + (p ? index - 1 : index + 1)) % cs->length;
288 347d23da 2018-05-19 omar.polo
289 1d13acf5 2018-10-04 omar.polo n = &cs->completions[cs->selected];
290 347d23da 2018-05-19 omar.polo
291 1d13acf5 2018-10-04 omar.polo free(*text);
292 1d13acf5 2018-10-04 omar.polo *text = strdup(n->completion);
293 1d13acf5 2018-10-04 omar.polo if (text == NULL) {
294 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Memory allocation error!\n");
295 1d13acf5 2018-10-04 omar.polo *status = ERR;
296 1d13acf5 2018-10-04 omar.polo return;
297 1d13acf5 2018-10-04 omar.polo }
298 1d13acf5 2018-10-04 omar.polo *textlen = strlen(*text);
299 f5e234d6 2018-05-18 omar.polo }
300 f5e234d6 2018-05-18 omar.polo
301 1d13acf5 2018-10-04 omar.polo /* Push the character c at the end of the string pointed by p */
302 1d13acf5 2018-10-04 omar.polo int
303 1d13acf5 2018-10-04 omar.polo pushc(char **p, int maxlen, char c)
304 1d13acf5 2018-10-04 omar.polo {
305 c758d8da 2019-10-18 omar.polo int len;
306 1e4bc498 2018-07-15 omar.polo
307 4f769efa 2018-10-06 omar.polo len = strnlen(*p, maxlen);
308 c758d8da 2019-10-18 omar.polo if (!(len < maxlen - 2)) {
309 1d13acf5 2018-10-04 omar.polo char *newptr;
310 1e4bc498 2018-07-15 omar.polo
311 1d13acf5 2018-10-04 omar.polo maxlen += maxlen >> 1;
312 1d13acf5 2018-10-04 omar.polo newptr = realloc(*p, maxlen);
313 1d13acf5 2018-10-04 omar.polo if (newptr == NULL) /* bad */
314 1d13acf5 2018-10-04 omar.polo return -1;
315 1d13acf5 2018-10-04 omar.polo *p = newptr;
316 1d13acf5 2018-10-04 omar.polo }
317 1e4bc498 2018-07-15 omar.polo
318 1d13acf5 2018-10-04 omar.polo (*p)[len] = c;
319 c758d8da 2019-10-18 omar.polo (*p)[len + 1] = '\0';
320 1d13acf5 2018-10-04 omar.polo return maxlen;
321 1e4bc498 2018-07-15 omar.polo }
322 1e4bc498 2018-07-15 omar.polo
323 c758d8da 2019-10-18 omar.polo /*
324 1d13acf5 2018-10-04 omar.polo * Remove the last rune from the *UTF-8* string! This is different
325 1d13acf5 2018-10-04 omar.polo * from just setting the last byte to 0 (in some cases ofc). Return a
326 1d13acf5 2018-10-04 omar.polo * pointer (e) to the last nonzero char. If e < p then p is empty!
327 1d13acf5 2018-10-04 omar.polo */
328 1d13acf5 2018-10-04 omar.polo char *
329 1d13acf5 2018-10-04 omar.polo popc(char *p)
330 1d13acf5 2018-10-04 omar.polo {
331 1d13acf5 2018-10-04 omar.polo int len = strlen(p);
332 1d13acf5 2018-10-04 omar.polo char *e;
333 8758854a 2018-05-20 omar.polo
334 1d13acf5 2018-10-04 omar.polo if (len == 0)
335 1d13acf5 2018-10-04 omar.polo return p;
336 8758854a 2018-05-20 omar.polo
337 1d13acf5 2018-10-04 omar.polo e = p + len - 1;
338 f5e234d6 2018-05-18 omar.polo
339 1d13acf5 2018-10-04 omar.polo do {
340 1d13acf5 2018-10-04 omar.polo char c = *e;
341 e66a5010 2018-07-21 omar.polo
342 4f769efa 2018-10-06 omar.polo *e = '\0';
343 4f769efa 2018-10-06 omar.polo e -= 1;
344 f5e234d6 2018-05-18 omar.polo
345 1d13acf5 2018-10-04 omar.polo /*
346 1d13acf5 2018-10-04 omar.polo * If c is a starting byte (11......) or is under
347 1d13acf5 2018-10-04 omar.polo * U+007F we're done.
348 1d13acf5 2018-10-04 omar.polo */
349 1d13acf5 2018-10-04 omar.polo if (((c & 0x80) && (c & 0x40)) || !(c & 0x80))
350 1d13acf5 2018-10-04 omar.polo break;
351 1d13acf5 2018-10-04 omar.polo } while (e >= p);
352 f5e234d6 2018-05-18 omar.polo
353 1d13acf5 2018-10-04 omar.polo return e;
354 1a4491e5 2018-09-19 omar.polo }
355 1a4491e5 2018-09-19 omar.polo
356 1d13acf5 2018-10-04 omar.polo /* Remove the last word plus trailing white spaces from the given string */
357 1d13acf5 2018-10-04 omar.polo void
358 1d13acf5 2018-10-04 omar.polo popw(char *w)
359 1d13acf5 2018-10-04 omar.polo {
360 c758d8da 2019-10-18 omar.polo int len;
361 c758d8da 2019-10-18 omar.polo short in_word = 1;
362 1a4491e5 2018-09-19 omar.polo
363 4f769efa 2018-10-06 omar.polo if ((len = strlen(w)) == 0)
364 1d13acf5 2018-10-04 omar.polo return;
365 ddd1263a 2018-09-24 omar.polo
366 26b541d8 2018-10-04 omar.polo while (1) {
367 1d13acf5 2018-10-04 omar.polo char *e = popc(w);
368 1a4491e5 2018-09-19 omar.polo
369 1d13acf5 2018-10-04 omar.polo if (e < w)
370 1d13acf5 2018-10-04 omar.polo return;
371 1a4491e5 2018-09-19 omar.polo
372 1d13acf5 2018-10-04 omar.polo if (in_word && isspace(*e))
373 26b541d8 2018-10-04 omar.polo in_word = 0;
374 1a4491e5 2018-09-19 omar.polo
375 1d13acf5 2018-10-04 omar.polo if (!in_word && !isspace(*e))
376 1d13acf5 2018-10-04 omar.polo return;
377 1d13acf5 2018-10-04 omar.polo }
378 1d13acf5 2018-10-04 omar.polo }
379 1a4491e5 2018-09-19 omar.polo
380 c758d8da 2019-10-18 omar.polo /*
381 1d13acf5 2018-10-04 omar.polo * If the string is surrounded by quates (`"') remove them and replace
382 1d13acf5 2018-10-04 omar.polo * every `\"' in the string with a single double-quote.
383 1d13acf5 2018-10-04 omar.polo */
384 1d13acf5 2018-10-04 omar.polo char *
385 1d13acf5 2018-10-04 omar.polo normalize_str(const char *str)
386 1d13acf5 2018-10-04 omar.polo {
387 c758d8da 2019-10-18 omar.polo int len, p;
388 c758d8da 2019-10-18 omar.polo char *s;
389 1a4491e5 2018-09-19 omar.polo
390 4f769efa 2018-10-06 omar.polo if ((len = strlen(str)) == 0)
391 1d13acf5 2018-10-04 omar.polo return NULL;
392 95b27a5e 2018-05-19 omar.polo
393 92803b00 2019-10-19 omar.polo if ((s = calloc(len, sizeof(char))) == NULL)
394 92803b00 2019-10-19 omar.polo err(1, "calloc");
395 1d13acf5 2018-10-04 omar.polo p = 0;
396 ddd1263a 2018-09-24 omar.polo
397 1d13acf5 2018-10-04 omar.polo while (*str) {
398 1d13acf5 2018-10-04 omar.polo char c = *str;
399 ddd1263a 2018-09-24 omar.polo
400 1d13acf5 2018-10-04 omar.polo if (*str == '\\') {
401 1d13acf5 2018-10-04 omar.polo if (*(str + 1)) {
402 1d13acf5 2018-10-04 omar.polo s[p] = *(str + 1);
403 1d13acf5 2018-10-04 omar.polo p++;
404 1d13acf5 2018-10-04 omar.polo str += 2; /* skip this and the next char */
405 1d13acf5 2018-10-04 omar.polo continue;
406 1d13acf5 2018-10-04 omar.polo } else
407 1d13acf5 2018-10-04 omar.polo break;
408 1d13acf5 2018-10-04 omar.polo }
409 1d13acf5 2018-10-04 omar.polo if (c == '"') {
410 c758d8da 2019-10-18 omar.polo str++; /* skip only this char */
411 1d13acf5 2018-10-04 omar.polo continue;
412 1d13acf5 2018-10-04 omar.polo }
413 1d13acf5 2018-10-04 omar.polo s[p] = c;
414 1d13acf5 2018-10-04 omar.polo p++;
415 1d13acf5 2018-10-04 omar.polo str++;
416 1d13acf5 2018-10-04 omar.polo }
417 c9a3bfaa 2018-05-21 omar.polo
418 1d13acf5 2018-10-04 omar.polo return s;
419 c9a3bfaa 2018-05-21 omar.polo }
420 c9a3bfaa 2018-05-21 omar.polo
421 1d13acf5 2018-10-04 omar.polo size_t
422 1d13acf5 2018-10-04 omar.polo read_stdin(char **buf)
423 1d13acf5 2018-10-04 omar.polo {
424 c758d8da 2019-10-18 omar.polo size_t offset = 0;
425 c758d8da 2019-10-18 omar.polo size_t len = STDIN_CHUNKS;
426 c9a3bfaa 2018-05-21 omar.polo
427 1d13acf5 2018-10-04 omar.polo *buf = malloc(len * sizeof(char));
428 1d13acf5 2018-10-04 omar.polo if (*buf == NULL)
429 1d13acf5 2018-10-04 omar.polo goto err;
430 f5e234d6 2018-05-18 omar.polo
431 26b541d8 2018-10-04 omar.polo while (1) {
432 1d13acf5 2018-10-04 omar.polo ssize_t r;
433 1d13acf5 2018-10-04 omar.polo size_t i;
434 c9a3bfaa 2018-05-21 omar.polo
435 1d13acf5 2018-10-04 omar.polo r = read(0, *buf + offset, STDIN_CHUNKS);
436 c9a3bfaa 2018-05-21 omar.polo
437 1d13acf5 2018-10-04 omar.polo if (r < 1)
438 1d13acf5 2018-10-04 omar.polo return len;
439 c9a3bfaa 2018-05-21 omar.polo
440 1d13acf5 2018-10-04 omar.polo offset += r;
441 c9a3bfaa 2018-05-21 omar.polo
442 1d13acf5 2018-10-04 omar.polo len += STDIN_CHUNKS;
443 1d13acf5 2018-10-04 omar.polo *buf = realloc(*buf, len);
444 1d13acf5 2018-10-04 omar.polo if (*buf == NULL)
445 1d13acf5 2018-10-04 omar.polo goto err;
446 1d13acf5 2018-10-04 omar.polo
447 1d13acf5 2018-10-04 omar.polo for (i = offset; i < len; ++i)
448 1d13acf5 2018-10-04 omar.polo (*buf)[i] = '\0';
449 1d13acf5 2018-10-04 omar.polo }
450 1d13acf5 2018-10-04 omar.polo
451 c758d8da 2019-10-18 omar.polo err:
452 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Error in allocating memory for stdin.\n");
453 1d13acf5 2018-10-04 omar.polo exit(EX_UNAVAILABLE);
454 c9a3bfaa 2018-05-21 omar.polo }
455 c9a3bfaa 2018-05-21 omar.polo
456 1d13acf5 2018-10-04 omar.polo size_t
457 1d13acf5 2018-10-04 omar.polo readlines(char ***lns, char **buf)
458 1d13acf5 2018-10-04 omar.polo {
459 c758d8da 2019-10-18 omar.polo size_t i, len, ll, lines;
460 c758d8da 2019-10-18 omar.polo short in_line = 0;
461 f5e234d6 2018-05-18 omar.polo
462 1d13acf5 2018-10-04 omar.polo lines = 0;
463 8758854a 2018-05-20 omar.polo
464 1d13acf5 2018-10-04 omar.polo *buf = NULL;
465 1d13acf5 2018-10-04 omar.polo len = read_stdin(buf);
466 347d23da 2018-05-19 omar.polo
467 1d13acf5 2018-10-04 omar.polo ll = LINES_CHUNK;
468 c758d8da 2019-10-18 omar.polo *lns = malloc(ll * sizeof(char *));
469 347d23da 2018-05-19 omar.polo
470 1d13acf5 2018-10-04 omar.polo if (*lns == NULL)
471 1d13acf5 2018-10-04 omar.polo goto err;
472 f5e234d6 2018-05-18 omar.polo
473 4f769efa 2018-10-06 omar.polo for (i = 0; i < len; i++) {
474 1d13acf5 2018-10-04 omar.polo char c = (*buf)[i];
475 f5e234d6 2018-05-18 omar.polo
476 1d13acf5 2018-10-04 omar.polo if (c == '\0')
477 1d13acf5 2018-10-04 omar.polo break;
478 f5e234d6 2018-05-18 omar.polo
479 1d13acf5 2018-10-04 omar.polo if (c == '\n')
480 1d13acf5 2018-10-04 omar.polo (*buf)[i] = '\0';
481 f5e234d6 2018-05-18 omar.polo
482 1d13acf5 2018-10-04 omar.polo if (in_line && c == '\n')
483 26b541d8 2018-10-04 omar.polo in_line = 0;
484 124df174 2018-08-11 omar.polo
485 1d13acf5 2018-10-04 omar.polo if (!in_line && c != '\n') {
486 26b541d8 2018-10-04 omar.polo in_line = 1;
487 1d13acf5 2018-10-04 omar.polo (*lns)[lines] = (*buf) + i;
488 1d13acf5 2018-10-04 omar.polo lines++;
489 f5e234d6 2018-05-18 omar.polo
490 26b541d8 2018-10-04 omar.polo if (lines == ll) { /* resize */
491 1d13acf5 2018-10-04 omar.polo ll += LINES_CHUNK;
492 c758d8da 2019-10-18 omar.polo *lns = realloc(*lns, ll * sizeof(char *));
493 1d13acf5 2018-10-04 omar.polo if (*lns == NULL)
494 1d13acf5 2018-10-04 omar.polo goto err;
495 1d13acf5 2018-10-04 omar.polo }
496 1d13acf5 2018-10-04 omar.polo }
497 1d13acf5 2018-10-04 omar.polo }
498 f5e234d6 2018-05-18 omar.polo
499 1d13acf5 2018-10-04 omar.polo (*lns)[lines] = NULL;
500 347d23da 2018-05-19 omar.polo
501 1d13acf5 2018-10-04 omar.polo return lines;
502 f5e234d6 2018-05-18 omar.polo
503 c758d8da 2019-10-18 omar.polo err:
504 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Error in memory allocation.\n");
505 1d13acf5 2018-10-04 omar.polo exit(EX_UNAVAILABLE);
506 f5e234d6 2018-05-18 omar.polo }
507 36a15a9f 2018-05-19 omar.polo
508 1d13acf5 2018-10-04 omar.polo /*
509 1d13acf5 2018-10-04 omar.polo * Compute the dimensions of the string str once rendered.
510 1d13acf5 2018-10-04 omar.polo * It'll return the width and set ret_width and ret_height if not NULL
511 1d13acf5 2018-10-04 omar.polo */
512 1d13acf5 2018-10-04 omar.polo int
513 c758d8da 2019-10-18 omar.polo text_extents(char *str, int len, struct rendering *r, int *ret_width,
514 c758d8da 2019-10-18 omar.polo int *ret_height)
515 1d13acf5 2018-10-04 omar.polo {
516 c758d8da 2019-10-18 omar.polo int height, width;
517 1d13acf5 2018-10-04 omar.polo #ifdef USE_XFT
518 c758d8da 2019-10-18 omar.polo XGlyphInfo gi;
519 1d13acf5 2018-10-04 omar.polo XftTextExtentsUtf8(r->d, r->font, str, len, &gi);
520 1d13acf5 2018-10-04 omar.polo height = r->font->ascent - r->font->descent;
521 1d13acf5 2018-10-04 omar.polo width = gi.width - gi.x;
522 1d13acf5 2018-10-04 omar.polo #else
523 c758d8da 2019-10-18 omar.polo XRectangle rect;
524 1d13acf5 2018-10-04 omar.polo XmbTextExtents(r->font, str, len, NULL, &rect);
525 1d13acf5 2018-10-04 omar.polo height = rect.height;
526 1d13acf5 2018-10-04 omar.polo width = rect.width;
527 1d13acf5 2018-10-04 omar.polo #endif
528 c758d8da 2019-10-18 omar.polo if (ret_width != NULL)
529 c758d8da 2019-10-18 omar.polo *ret_width = width;
530 c758d8da 2019-10-18 omar.polo if (ret_height != NULL)
531 c758d8da 2019-10-18 omar.polo *ret_height = height;
532 1d13acf5 2018-10-04 omar.polo return width;
533 1d13acf5 2018-10-04 omar.polo }
534 36a15a9f 2018-05-19 omar.polo
535 1d13acf5 2018-10-04 omar.polo void
536 c758d8da 2019-10-18 omar.polo draw_string(char *str, int len, int x, int y, struct rendering *r,
537 c758d8da 2019-10-18 omar.polo enum obj_type tt)
538 1d13acf5 2018-10-04 omar.polo {
539 1d13acf5 2018-10-04 omar.polo #ifdef USE_XFT
540 1d13acf5 2018-10-04 omar.polo XftColor xftcolor;
541 c758d8da 2019-10-18 omar.polo if (tt == PROMPT)
542 c758d8da 2019-10-18 omar.polo xftcolor = r->xft_colors[0];
543 c758d8da 2019-10-18 omar.polo if (tt == COMPL)
544 c758d8da 2019-10-18 omar.polo xftcolor = r->xft_colors[1];
545 c758d8da 2019-10-18 omar.polo if (tt == COMPL_HIGH)
546 c758d8da 2019-10-18 omar.polo xftcolor = r->xft_colors[2];
547 36a15a9f 2018-05-19 omar.polo
548 1d13acf5 2018-10-04 omar.polo XftDrawStringUtf8(r->xftdraw, &xftcolor, r->font, x, y, str, len);
549 1d13acf5 2018-10-04 omar.polo #else
550 1d13acf5 2018-10-04 omar.polo GC gc;
551 c758d8da 2019-10-18 omar.polo if (tt == PROMPT)
552 c758d8da 2019-10-18 omar.polo gc = r->fgs[0];
553 c758d8da 2019-10-18 omar.polo if (tt == COMPL)
554 c758d8da 2019-10-18 omar.polo gc = r->fgs[1];
555 c758d8da 2019-10-18 omar.polo if (tt == COMPL_HIGH)
556 c758d8da 2019-10-18 omar.polo gc = r->fgs[2];
557 1d13acf5 2018-10-04 omar.polo Xutf8DrawString(r->d, r->w, r->font, gc, x, y, str, len);
558 1d13acf5 2018-10-04 omar.polo #endif
559 1d13acf5 2018-10-04 omar.polo }
560 c9a3bfaa 2018-05-21 omar.polo
561 1d13acf5 2018-10-04 omar.polo /* Duplicate the string and substitute every space with a 'n` */
562 1d13acf5 2018-10-04 omar.polo char *
563 1d13acf5 2018-10-04 omar.polo strdupn(char *str)
564 1d13acf5 2018-10-04 omar.polo {
565 c758d8da 2019-10-18 omar.polo int len, i;
566 c758d8da 2019-10-18 omar.polo char *dup;
567 d29c160f 2018-07-27 omar.polo
568 1d13acf5 2018-10-04 omar.polo len = strlen(str);
569 c9a3bfaa 2018-05-21 omar.polo
570 1d13acf5 2018-10-04 omar.polo if (str == NULL || len == 0)
571 1d13acf5 2018-10-04 omar.polo return NULL;
572 b5d751bd 2018-07-07 omar.polo
573 4f769efa 2018-10-06 omar.polo if ((dup = strdup(str)) == NULL)
574 1d13acf5 2018-10-04 omar.polo return NULL;
575 36a15a9f 2018-05-19 omar.polo
576 1d13acf5 2018-10-04 omar.polo for (i = 0; i < len; ++i)
577 1d13acf5 2018-10-04 omar.polo if (dup[i] == ' ')
578 1d13acf5 2018-10-04 omar.polo dup[i] = 'n';
579 0ee198aa 2018-05-19 omar.polo
580 1d13acf5 2018-10-04 omar.polo return dup;
581 36a15a9f 2018-05-19 omar.polo }
582 36a15a9f 2018-05-19 omar.polo
583 df29e05e 2018-10-17 omar.polo int
584 c758d8da 2019-10-18 omar.polo draw_v_box(struct rendering *r, int y, char *prefix, int prefix_width,
585 c758d8da 2019-10-18 omar.polo enum obj_type t, char *text)
586 1d13acf5 2018-10-04 omar.polo {
587 c758d8da 2019-10-18 omar.polo GC *border_color, bg;
588 c758d8da 2019-10-18 omar.polo int *padding, *borders;
589 c758d8da 2019-10-18 omar.polo int ret = 0, inner_width, inner_height, x;
590 42c3f269 2018-07-08 omar.polo
591 df29e05e 2018-10-17 omar.polo switch (t) {
592 df29e05e 2018-10-17 omar.polo case PROMPT:
593 c758d8da 2019-10-18 omar.polo border_color = r->p_borders_bg;
594 c758d8da 2019-10-18 omar.polo padding = r->p_padding;
595 c758d8da 2019-10-18 omar.polo borders = r->p_borders;
596 c758d8da 2019-10-18 omar.polo bg = r->bgs[0];
597 df29e05e 2018-10-17 omar.polo break;
598 df29e05e 2018-10-17 omar.polo case COMPL:
599 c758d8da 2019-10-18 omar.polo border_color = r->c_borders_bg;
600 c758d8da 2019-10-18 omar.polo padding = r->c_padding;
601 c758d8da 2019-10-18 omar.polo borders = r->c_borders;
602 c758d8da 2019-10-18 omar.polo bg = r->bgs[1];
603 df29e05e 2018-10-17 omar.polo break;
604 df29e05e 2018-10-17 omar.polo case COMPL_HIGH:
605 c758d8da 2019-10-18 omar.polo border_color = r->ch_borders_bg;
606 c758d8da 2019-10-18 omar.polo padding = r->ch_padding;
607 c758d8da 2019-10-18 omar.polo borders = r->ch_borders;
608 c758d8da 2019-10-18 omar.polo bg = r->bgs[2];
609 df29e05e 2018-10-17 omar.polo break;
610 df29e05e 2018-10-17 omar.polo }
611 42c3f269 2018-07-08 omar.polo
612 c758d8da 2019-10-18 omar.polo ret = borders[0] + padding[0] + r->text_height + padding[2]
613 c758d8da 2019-10-18 omar.polo + borders[2];
614 42c3f269 2018-07-08 omar.polo
615 df29e05e 2018-10-17 omar.polo inner_width = inner_width(r) - borders[1] - borders[3];
616 df29e05e 2018-10-17 omar.polo inner_height = padding[0] + r->text_height + padding[2];
617 1d13acf5 2018-10-04 omar.polo
618 df29e05e 2018-10-17 omar.polo /* Border top */
619 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, border_color[0], r->x_zero, y, r->width,
620 c758d8da 2019-10-18 omar.polo borders[0]);
621 1d13acf5 2018-10-04 omar.polo
622 df29e05e 2018-10-17 omar.polo /* Border right */
623 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, border_color[1],
624 c758d8da 2019-10-18 omar.polo r->x_zero + inner_width(r) - borders[1], y, borders[1], ret);
625 df29e05e 2018-10-17 omar.polo
626 df29e05e 2018-10-17 omar.polo /* Border bottom */
627 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, border_color[2], r->x_zero,
628 c758d8da 2019-10-18 omar.polo y + borders[0] + padding[0] + r->text_height + padding[2],
629 c758d8da 2019-10-18 omar.polo r->width, borders[2]);
630 1d13acf5 2018-10-04 omar.polo
631 df29e05e 2018-10-17 omar.polo /* Border left */
632 c758d8da 2019-10-18 omar.polo XFillRectangle(
633 c758d8da 2019-10-18 omar.polo r->d, r->w, border_color[3], r->x_zero, y, borders[3], ret);
634 1d13acf5 2018-10-04 omar.polo
635 df29e05e 2018-10-17 omar.polo /* bg */
636 df29e05e 2018-10-17 omar.polo x = r->x_zero + borders[3];
637 df29e05e 2018-10-17 omar.polo y += borders[0];
638 df29e05e 2018-10-17 omar.polo XFillRectangle(r->d, r->w, bg, x, y, inner_width, inner_height);
639 1d13acf5 2018-10-04 omar.polo
640 df29e05e 2018-10-17 omar.polo /* content */
641 df29e05e 2018-10-17 omar.polo y += padding[0] + r->text_height;
642 df29e05e 2018-10-17 omar.polo x += padding[3];
643 df29e05e 2018-10-17 omar.polo if (prefix != NULL) {
644 df29e05e 2018-10-17 omar.polo draw_string(prefix, strlen(prefix), x, y, r, t);
645 df29e05e 2018-10-17 omar.polo x += prefix_width;
646 df29e05e 2018-10-17 omar.polo }
647 df29e05e 2018-10-17 omar.polo draw_string(text, strlen(text), x, y, r, t);
648 1d13acf5 2018-10-04 omar.polo
649 df29e05e 2018-10-17 omar.polo return ret;
650 df29e05e 2018-10-17 omar.polo }
651 1d13acf5 2018-10-04 omar.polo
652 df29e05e 2018-10-17 omar.polo int
653 c758d8da 2019-10-18 omar.polo draw_h_box(struct rendering *r, int x, char *prefix, int prefix_width,
654 c758d8da 2019-10-18 omar.polo enum obj_type t, char *text)
655 df29e05e 2018-10-17 omar.polo {
656 c758d8da 2019-10-18 omar.polo GC *border_color, bg;
657 c758d8da 2019-10-18 omar.polo int *padding, *borders;
658 c758d8da 2019-10-18 omar.polo int ret = 0, inner_width, inner_height, y, text_width;
659 df29e05e 2018-10-17 omar.polo
660 df29e05e 2018-10-17 omar.polo switch (t) {
661 df29e05e 2018-10-17 omar.polo case PROMPT:
662 c758d8da 2019-10-18 omar.polo border_color = r->p_borders_bg;
663 c758d8da 2019-10-18 omar.polo padding = r->p_padding;
664 c758d8da 2019-10-18 omar.polo borders = r->p_borders;
665 c758d8da 2019-10-18 omar.polo bg = r->bgs[0];
666 df29e05e 2018-10-17 omar.polo break;
667 df29e05e 2018-10-17 omar.polo case COMPL:
668 c758d8da 2019-10-18 omar.polo border_color = r->c_borders_bg;
669 c758d8da 2019-10-18 omar.polo padding = r->c_padding;
670 c758d8da 2019-10-18 omar.polo borders = r->c_borders;
671 c758d8da 2019-10-18 omar.polo bg = r->bgs[1];
672 df29e05e 2018-10-17 omar.polo break;
673 df29e05e 2018-10-17 omar.polo case COMPL_HIGH:
674 c758d8da 2019-10-18 omar.polo border_color = r->ch_borders_bg;
675 c758d8da 2019-10-18 omar.polo padding = r->ch_padding;
676 c758d8da 2019-10-18 omar.polo borders = r->ch_borders;
677 c758d8da 2019-10-18 omar.polo bg = r->bgs[2];
678 df29e05e 2018-10-17 omar.polo break;
679 df29e05e 2018-10-17 omar.polo }
680 df29e05e 2018-10-17 omar.polo
681 df29e05e 2018-10-17 omar.polo if (padding[0] < 0 || padding[2] < 0)
682 c758d8da 2019-10-18 omar.polo padding[0] = padding[2]
683 c758d8da 2019-10-18 omar.polo = (inner_height(r) - borders[0] - borders[2]
684 c758d8da 2019-10-18 omar.polo - r->text_height)
685 c758d8da 2019-10-18 omar.polo / 2;
686 df29e05e 2018-10-17 omar.polo
687 df29e05e 2018-10-17 omar.polo /* If they are still lesser than 0, set 'em to 0 */
688 df29e05e 2018-10-17 omar.polo if (padding[0] < 0 || padding[2] < 0)
689 df29e05e 2018-10-17 omar.polo padding[0] = padding[2] = 0;
690 df29e05e 2018-10-17 omar.polo
691 df29e05e 2018-10-17 omar.polo /* Get the text width */
692 df29e05e 2018-10-17 omar.polo text_extents(text, strlen(text), r, &text_width, NULL);
693 df29e05e 2018-10-17 omar.polo if (prefix != NULL)
694 df29e05e 2018-10-17 omar.polo text_width += prefix_width;
695 df29e05e 2018-10-17 omar.polo
696 df29e05e 2018-10-17 omar.polo ret = borders[3] + padding[3] + text_width + padding[1] + borders[1];
697 df29e05e 2018-10-17 omar.polo
698 df29e05e 2018-10-17 omar.polo inner_width = padding[3] + text_width + padding[1];
699 df29e05e 2018-10-17 omar.polo inner_height = inner_height(r) - borders[0] - borders[2];
700 df29e05e 2018-10-17 omar.polo
701 df29e05e 2018-10-17 omar.polo /* Border top */
702 c758d8da 2019-10-18 omar.polo XFillRectangle(
703 c758d8da 2019-10-18 omar.polo r->d, r->w, border_color[0], x, r->y_zero, ret, borders[0]);
704 df29e05e 2018-10-17 omar.polo
705 df29e05e 2018-10-17 omar.polo /* Border right */
706 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, border_color[1],
707 c758d8da 2019-10-18 omar.polo x + borders[3] + inner_width, r->y_zero, borders[1],
708 c758d8da 2019-10-18 omar.polo inner_height(r));
709 df29e05e 2018-10-17 omar.polo
710 df29e05e 2018-10-17 omar.polo /* Border bottom */
711 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, border_color[2], x,
712 c758d8da 2019-10-18 omar.polo r->y_zero + inner_height(r) - borders[2], ret, borders[2]);
713 df29e05e 2018-10-17 omar.polo
714 df29e05e 2018-10-17 omar.polo /* Border left */
715 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, border_color[3], x, r->y_zero, borders[3],
716 c758d8da 2019-10-18 omar.polo inner_height(r));
717 df29e05e 2018-10-17 omar.polo
718 df29e05e 2018-10-17 omar.polo /* bg */
719 df29e05e 2018-10-17 omar.polo x += borders[3];
720 df29e05e 2018-10-17 omar.polo y = r->y_zero + borders[0];
721 df29e05e 2018-10-17 omar.polo XFillRectangle(r->d, r->w, bg, x, y, inner_width, inner_height);
722 df29e05e 2018-10-17 omar.polo
723 df29e05e 2018-10-17 omar.polo /* content */
724 df29e05e 2018-10-17 omar.polo y += padding[0] + r->text_height;
725 df29e05e 2018-10-17 omar.polo x += padding[3];
726 df29e05e 2018-10-17 omar.polo if (prefix != NULL) {
727 df29e05e 2018-10-17 omar.polo draw_string(prefix, strlen(prefix), x, y, r, t);
728 df29e05e 2018-10-17 omar.polo x += prefix_width;
729 1d13acf5 2018-10-04 omar.polo }
730 df29e05e 2018-10-17 omar.polo draw_string(text, strlen(text), x, y, r, t);
731 df29e05e 2018-10-17 omar.polo
732 df29e05e 2018-10-17 omar.polo return ret;
733 36a15a9f 2018-05-19 omar.polo }
734 36a15a9f 2018-05-19 omar.polo
735 51986bb8 2018-10-06 omar.polo /* ,-----------------------------------------------------------------, */
736 df29e05e 2018-10-17 omar.polo /* | 20 char text | completion | completion | completion | compl | */
737 df29e05e 2018-10-17 omar.polo /* `-----------------------------------------------------------------' */
738 df29e05e 2018-10-17 omar.polo void
739 df29e05e 2018-10-17 omar.polo draw_horizontally(struct rendering *r, char *text, struct completions *cs)
740 df29e05e 2018-10-17 omar.polo {
741 c758d8da 2019-10-18 omar.polo size_t i;
742 c758d8da 2019-10-18 omar.polo int x = r->x_zero;
743 df29e05e 2018-10-17 omar.polo
744 df29e05e 2018-10-17 omar.polo /* Draw the prompt */
745 df29e05e 2018-10-17 omar.polo x += draw_h_box(r, x, r->ps1, r->ps1w, PROMPT, text);
746 df29e05e 2018-10-17 omar.polo
747 df29e05e 2018-10-17 omar.polo for (i = r->offset; i < cs->length; ++i) {
748 c758d8da 2019-10-18 omar.polo enum obj_type t
749 c758d8da 2019-10-18 omar.polo = cs->selected == (ssize_t)i ? COMPL_HIGH : COMPL;
750 df29e05e 2018-10-17 omar.polo
751 a81dbe5d 2019-10-19 omar.polo cs->completions[i].offset = x;
752 a81dbe5d 2019-10-19 omar.polo
753 c758d8da 2019-10-18 omar.polo x += draw_h_box(
754 c758d8da 2019-10-18 omar.polo r, x, NULL, 0, t, cs->completions[i].completion);
755 df29e05e 2018-10-17 omar.polo
756 df29e05e 2018-10-17 omar.polo if (x > inner_width(r))
757 df29e05e 2018-10-17 omar.polo break;
758 df29e05e 2018-10-17 omar.polo }
759 a81dbe5d 2019-10-19 omar.polo
760 a81dbe5d 2019-10-19 omar.polo for (i += 1; i < cs->length; ++i)
761 a81dbe5d 2019-10-19 omar.polo cs->completions[i].offset = -1;
762 df29e05e 2018-10-17 omar.polo }
763 df29e05e 2018-10-17 omar.polo
764 df29e05e 2018-10-17 omar.polo /* ,-----------------------------------------------------------------, */
765 1d13acf5 2018-10-04 omar.polo /* | prompt | */
766 1d13acf5 2018-10-04 omar.polo /* |-----------------------------------------------------------------| */
767 1d13acf5 2018-10-04 omar.polo /* | completion | */
768 1d13acf5 2018-10-04 omar.polo /* |-----------------------------------------------------------------| */
769 1d13acf5 2018-10-04 omar.polo /* | completion | */
770 51986bb8 2018-10-06 omar.polo /* `-----------------------------------------------------------------' */
771 1d13acf5 2018-10-04 omar.polo void
772 1d13acf5 2018-10-04 omar.polo draw_vertically(struct rendering *r, char *text, struct completions *cs)
773 1d13acf5 2018-10-04 omar.polo {
774 c758d8da 2019-10-18 omar.polo size_t i;
775 c758d8da 2019-10-18 omar.polo int y = r->y_zero;
776 f5e234d6 2018-05-18 omar.polo
777 df29e05e 2018-10-17 omar.polo y += draw_v_box(r, y, r->ps1, r->ps1w, PROMPT, text);
778 f5e234d6 2018-05-18 omar.polo
779 1d13acf5 2018-10-04 omar.polo for (i = r->offset; i < cs->length; ++i) {
780 c758d8da 2019-10-18 omar.polo enum obj_type t
781 c758d8da 2019-10-18 omar.polo = cs->selected == (ssize_t)i ? COMPL_HIGH : COMPL;
782 1d13acf5 2018-10-04 omar.polo
783 a81dbe5d 2019-10-19 omar.polo cs->completions[i].offset = y;
784 a81dbe5d 2019-10-19 omar.polo
785 c758d8da 2019-10-18 omar.polo y += draw_v_box(
786 c758d8da 2019-10-18 omar.polo r, y, NULL, 0, t, cs->completions[i].completion);
787 1d13acf5 2018-10-04 omar.polo
788 df29e05e 2018-10-17 omar.polo if (y > inner_height(r))
789 df29e05e 2018-10-17 omar.polo break;
790 1d13acf5 2018-10-04 omar.polo }
791 a81dbe5d 2019-10-19 omar.polo
792 a81dbe5d 2019-10-19 omar.polo for (i += 1; i < cs->length; ++i)
793 a81dbe5d 2019-10-19 omar.polo cs->completions[i].offset = -1;
794 f5e234d6 2018-05-18 omar.polo }
795 f5e234d6 2018-05-18 omar.polo
796 1d13acf5 2018-10-04 omar.polo void
797 1d13acf5 2018-10-04 omar.polo draw(struct rendering *r, char *text, struct completions *cs)
798 1d13acf5 2018-10-04 omar.polo {
799 df29e05e 2018-10-17 omar.polo /* Draw the background */
800 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, r->bgs[1], r->x_zero, r->y_zero,
801 c758d8da 2019-10-18 omar.polo inner_width(r), inner_height(r));
802 df29e05e 2018-10-17 omar.polo
803 df29e05e 2018-10-17 omar.polo /* Draw the contents */
804 1d13acf5 2018-10-04 omar.polo if (r->horizontal_layout)
805 1d13acf5 2018-10-04 omar.polo draw_horizontally(r, text, cs);
806 1d13acf5 2018-10-04 omar.polo else
807 1d13acf5 2018-10-04 omar.polo draw_vertically(r, text, cs);
808 1d13acf5 2018-10-04 omar.polo
809 1d13acf5 2018-10-04 omar.polo /* Draw the borders */
810 4c0d79a0 2018-10-09 omar.polo if (r->borders[0] != 0)
811 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, r->borders_bg[0], 0, 0, r->width,
812 c758d8da 2019-10-18 omar.polo r->borders[0]);
813 1d13acf5 2018-10-04 omar.polo
814 4c0d79a0 2018-10-09 omar.polo if (r->borders[1] != 0)
815 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, r->borders_bg[1],
816 c758d8da 2019-10-18 omar.polo r->width - r->borders[1], 0, r->borders[1],
817 c758d8da 2019-10-18 omar.polo r->height);
818 1d13acf5 2018-10-04 omar.polo
819 4c0d79a0 2018-10-09 omar.polo if (r->borders[2] != 0)
820 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, r->borders_bg[2], 0,
821 c758d8da 2019-10-18 omar.polo r->height - r->borders[2], r->width, r->borders[2]);
822 1d13acf5 2018-10-04 omar.polo
823 4c0d79a0 2018-10-09 omar.polo if (r->borders[3] != 0)
824 c758d8da 2019-10-18 omar.polo XFillRectangle(r->d, r->w, r->borders_bg[3], 0, 0,
825 c758d8da 2019-10-18 omar.polo r->borders[3], r->height);
826 4c0d79a0 2018-10-09 omar.polo
827 1d13acf5 2018-10-04 omar.polo /* render! */
828 1d13acf5 2018-10-04 omar.polo XFlush(r->d);
829 1d13acf5 2018-10-04 omar.polo }
830 1d13acf5 2018-10-04 omar.polo
831 1d13acf5 2018-10-04 omar.polo /* Set some WM stuff */
832 1d13acf5 2018-10-04 omar.polo void
833 1d13acf5 2018-10-04 omar.polo set_win_atoms_hints(Display *d, Window w, int width, int height)
834 1d13acf5 2018-10-04 omar.polo {
835 c758d8da 2019-10-18 omar.polo Atom type;
836 c758d8da 2019-10-18 omar.polo XClassHint *class_hint;
837 c758d8da 2019-10-18 omar.polo XSizeHints *size_hint;
838 1d13acf5 2018-10-04 omar.polo
839 26b541d8 2018-10-04 omar.polo type = XInternAtom(d, "_NET_WM_WINDOW_TYPE_DOCK", 0);
840 c758d8da 2019-10-18 omar.polo XChangeProperty(d, w, XInternAtom(d, "_NET_WM_WINDOW_TYPE", 0),
841 c758d8da 2019-10-18 omar.polo XInternAtom(d, "ATOM", 0), 32, PropModeReplace,
842 c758d8da 2019-10-18 omar.polo (unsigned char *)&type, 1);
843 1d13acf5 2018-10-04 omar.polo
844 1d13acf5 2018-10-04 omar.polo /* some window managers honor this properties */
845 26b541d8 2018-10-04 omar.polo type = XInternAtom(d, "_NET_WM_STATE_ABOVE", 0);
846 c758d8da 2019-10-18 omar.polo XChangeProperty(d, w, XInternAtom(d, "_NET_WM_STATE", 0),
847 c758d8da 2019-10-18 omar.polo XInternAtom(d, "ATOM", 0), 32, PropModeReplace,
848 c758d8da 2019-10-18 omar.polo (unsigned char *)&type, 1);
849 1d13acf5 2018-10-04 omar.polo
850 26b541d8 2018-10-04 omar.polo type = XInternAtom(d, "_NET_WM_STATE_FOCUSED", 0);
851 c758d8da 2019-10-18 omar.polo XChangeProperty(d, w, XInternAtom(d, "_NET_WM_STATE", 0),
852 c758d8da 2019-10-18 omar.polo XInternAtom(d, "ATOM", 0), 32, PropModeAppend,
853 c758d8da 2019-10-18 omar.polo (unsigned char *)&type, 1);
854 1d13acf5 2018-10-04 omar.polo
855 1d13acf5 2018-10-04 omar.polo /* Setting window hints */
856 1d13acf5 2018-10-04 omar.polo class_hint = XAllocClassHint();
857 1d13acf5 2018-10-04 omar.polo if (class_hint == NULL) {
858 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Could not allocate memory for class hint\n");
859 1d13acf5 2018-10-04 omar.polo exit(EX_UNAVAILABLE);
860 1d13acf5 2018-10-04 omar.polo }
861 4f769efa 2018-10-06 omar.polo
862 1d13acf5 2018-10-04 omar.polo class_hint->res_name = resname;
863 1d13acf5 2018-10-04 omar.polo class_hint->res_class = resclass;
864 1d13acf5 2018-10-04 omar.polo XSetClassHint(d, w, class_hint);
865 1d13acf5 2018-10-04 omar.polo XFree(class_hint);
866 844addbb 2018-07-15 omar.polo
867 1d13acf5 2018-10-04 omar.polo size_hint = XAllocSizeHints();
868 1d13acf5 2018-10-04 omar.polo if (size_hint == NULL) {
869 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Could not allocate memory for size hint\n");
870 1d13acf5 2018-10-04 omar.polo exit(EX_UNAVAILABLE);
871 1d13acf5 2018-10-04 omar.polo }
872 4f769efa 2018-10-06 omar.polo
873 1d13acf5 2018-10-04 omar.polo size_hint->flags = PMinSize | PBaseSize;
874 1d13acf5 2018-10-04 omar.polo size_hint->min_width = width;
875 1d13acf5 2018-10-04 omar.polo size_hint->base_width = width;
876 1d13acf5 2018-10-04 omar.polo size_hint->min_height = height;
877 1d13acf5 2018-10-04 omar.polo size_hint->base_height = height;
878 f5e234d6 2018-05-18 omar.polo
879 1d13acf5 2018-10-04 omar.polo XFlush(d);
880 f5e234d6 2018-05-18 omar.polo }
881 f5e234d6 2018-05-18 omar.polo
882 1d13acf5 2018-10-04 omar.polo /* Get the width and height of the window `w' */
883 1d13acf5 2018-10-04 omar.polo void
884 1d13acf5 2018-10-04 omar.polo get_wh(Display *d, Window *w, int *width, int *height)
885 1d13acf5 2018-10-04 omar.polo {
886 1d13acf5 2018-10-04 omar.polo XWindowAttributes win_attr;
887 1d13acf5 2018-10-04 omar.polo
888 1d13acf5 2018-10-04 omar.polo XGetWindowAttributes(d, *w, &win_attr);
889 1d13acf5 2018-10-04 omar.polo *height = win_attr.height;
890 1d13acf5 2018-10-04 omar.polo *width = win_attr.width;
891 6bb186a7 2018-09-13 omar.polo }
892 6bb186a7 2018-09-13 omar.polo
893 1d13acf5 2018-10-04 omar.polo int
894 1d13acf5 2018-10-04 omar.polo grabfocus(Display *d, Window w)
895 1d13acf5 2018-10-04 omar.polo {
896 c758d8da 2019-10-18 omar.polo int i;
897 1d13acf5 2018-10-04 omar.polo for (i = 0; i < 100; ++i) {
898 1d13acf5 2018-10-04 omar.polo Window focuswin;
899 1d13acf5 2018-10-04 omar.polo int revert_to_win;
900 6bb186a7 2018-09-13 omar.polo
901 1d13acf5 2018-10-04 omar.polo XGetInputFocus(d, &focuswin, &revert_to_win);
902 6bb186a7 2018-09-13 omar.polo
903 1d13acf5 2018-10-04 omar.polo if (focuswin == w)
904 26b541d8 2018-10-04 omar.polo return 1;
905 6bb186a7 2018-09-13 omar.polo
906 1d13acf5 2018-10-04 omar.polo XSetInputFocus(d, w, RevertToParent, CurrentTime);
907 1d13acf5 2018-10-04 omar.polo usleep(1000);
908 1d13acf5 2018-10-04 omar.polo }
909 1d13acf5 2018-10-04 omar.polo return 0;
910 1d13acf5 2018-10-04 omar.polo }
911 6bb186a7 2018-09-13 omar.polo
912 c758d8da 2019-10-18 omar.polo /*
913 1d13acf5 2018-10-04 omar.polo * I know this may seem a little hackish BUT is the only way I managed
914 1d13acf5 2018-10-04 omar.polo * to actually grab that goddam keyboard. Only one call to
915 1d13acf5 2018-10-04 omar.polo * XGrabKeyboard does not always end up with the keyboard grabbed!
916 1d13acf5 2018-10-04 omar.polo */
917 1d13acf5 2018-10-04 omar.polo int
918 1d13acf5 2018-10-04 omar.polo take_keyboard(Display *d, Window w)
919 1d13acf5 2018-10-04 omar.polo {
920 1d13acf5 2018-10-04 omar.polo int i;
921 1d13acf5 2018-10-04 omar.polo for (i = 0; i < 100; i++) {
922 c758d8da 2019-10-18 omar.polo if (XGrabKeyboard(d, w, 1, GrabModeAsync, GrabModeAsync,
923 c758d8da 2019-10-18 omar.polo CurrentTime)
924 c758d8da 2019-10-18 omar.polo == GrabSuccess)
925 1d13acf5 2018-10-04 omar.polo return 1;
926 1d13acf5 2018-10-04 omar.polo usleep(1000);
927 1d13acf5 2018-10-04 omar.polo }
928 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Cannot grab keyboard\n");
929 1d13acf5 2018-10-04 omar.polo return 0;
930 f5e234d6 2018-05-18 omar.polo }
931 f5e234d6 2018-05-18 omar.polo
932 1d13acf5 2018-10-04 omar.polo unsigned long
933 1d13acf5 2018-10-04 omar.polo parse_color(const char *str, const char *def)
934 1d13acf5 2018-10-04 omar.polo {
935 1d13acf5 2018-10-04 omar.polo size_t len;
936 1d13acf5 2018-10-04 omar.polo rgba_t tmp;
937 1d13acf5 2018-10-04 omar.polo char *ep;
938 25bf99d2 2018-05-22 omar.polo
939 1d13acf5 2018-10-04 omar.polo if (str == NULL)
940 1d13acf5 2018-10-04 omar.polo goto invc;
941 f5e234d6 2018-05-18 omar.polo
942 1d13acf5 2018-10-04 omar.polo len = strlen(str);
943 f5e234d6 2018-05-18 omar.polo
944 1d13acf5 2018-10-04 omar.polo /* +1 for the # ath the start */
945 1d13acf5 2018-10-04 omar.polo if (*str != '#' || len > 9 || len < 4)
946 1d13acf5 2018-10-04 omar.polo goto invc;
947 c758d8da 2019-10-18 omar.polo ++str; /* skip the # */
948 42c3f269 2018-07-08 omar.polo
949 1d13acf5 2018-10-04 omar.polo errno = 0;
950 1d13acf5 2018-10-04 omar.polo tmp = (rgba_t)(uint32_t)strtoul(str, &ep, 16);
951 42c3f269 2018-07-08 omar.polo
952 1d13acf5 2018-10-04 omar.polo if (errno)
953 1d13acf5 2018-10-04 omar.polo goto invc;
954 42c3f269 2018-07-08 omar.polo
955 c758d8da 2019-10-18 omar.polo switch (len - 1) {
956 1d13acf5 2018-10-04 omar.polo case 3:
957 1d13acf5 2018-10-04 omar.polo /* expand #rgb -> #rrggbb */
958 c758d8da 2019-10-18 omar.polo tmp.v = (tmp.v & 0xf00) * 0x1100 | (tmp.v & 0x0f0) * 0x0110
959 1d13acf5 2018-10-04 omar.polo | (tmp.v & 0x00f) * 0x0011;
960 1d13acf5 2018-10-04 omar.polo case 6:
961 1d13acf5 2018-10-04 omar.polo /* assume 0xff opacity */
962 4f769efa 2018-10-06 omar.polo tmp.rgba.a = 0xff;
963 1d13acf5 2018-10-04 omar.polo break;
964 1d13acf5 2018-10-04 omar.polo } /* colors in #aarrggbb need no adjustments */
965 42c3f269 2018-07-08 omar.polo
966 1d13acf5 2018-10-04 omar.polo /* premultiply the alpha */
967 4f769efa 2018-10-06 omar.polo if (tmp.rgba.a) {
968 4f769efa 2018-10-06 omar.polo tmp.rgba.r = (tmp.rgba.r * tmp.rgba.a) / 255;
969 4f769efa 2018-10-06 omar.polo tmp.rgba.g = (tmp.rgba.g * tmp.rgba.a) / 255;
970 4f769efa 2018-10-06 omar.polo tmp.rgba.b = (tmp.rgba.b * tmp.rgba.a) / 255;
971 1d13acf5 2018-10-04 omar.polo return tmp.v;
972 1d13acf5 2018-10-04 omar.polo }
973 42c3f269 2018-07-08 omar.polo
974 1d13acf5 2018-10-04 omar.polo return 0U;
975 42c3f269 2018-07-08 omar.polo
976 c758d8da 2019-10-18 omar.polo invc:
977 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Invalid color: \"%s\".\n", str);
978 1d13acf5 2018-10-04 omar.polo if (def != NULL)
979 1d13acf5 2018-10-04 omar.polo return parse_color(def, NULL);
980 1d13acf5 2018-10-04 omar.polo else
981 1d13acf5 2018-10-04 omar.polo return 0U;
982 1d13acf5 2018-10-04 omar.polo }
983 42c3f269 2018-07-08 omar.polo
984 c758d8da 2019-10-18 omar.polo /*
985 26b541d8 2018-10-04 omar.polo * Given a string try to parse it as a number or return `default_value'.
986 26b541d8 2018-10-04 omar.polo */
987 1d13acf5 2018-10-04 omar.polo int
988 1d13acf5 2018-10-04 omar.polo parse_integer(const char *str, int default_value)
989 1d13acf5 2018-10-04 omar.polo {
990 c758d8da 2019-10-18 omar.polo long lval;
991 c758d8da 2019-10-18 omar.polo char *ep;
992 42c3f269 2018-07-08 omar.polo
993 1d13acf5 2018-10-04 omar.polo errno = 0;
994 1d13acf5 2018-10-04 omar.polo lval = strtol(str, &ep, 10);
995 42c3f269 2018-07-08 omar.polo
996 26b541d8 2018-10-04 omar.polo if (str[0] == '\0' || *ep != '\0') { /* NaN */
997 c758d8da 2019-10-18 omar.polo fprintf(stderr,
998 c758d8da 2019-10-18 omar.polo "'%s' is not a valid number! Using %d as default.\n",
999 c758d8da 2019-10-18 omar.polo str, default_value);
1000 1d13acf5 2018-10-04 omar.polo return default_value;
1001 1d13acf5 2018-10-04 omar.polo }
1002 42c3f269 2018-07-08 omar.polo
1003 c758d8da 2019-10-18 omar.polo if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
1004 c758d8da 2019-10-18 omar.polo || (lval > INT_MAX || lval < INT_MIN)) {
1005 c758d8da 2019-10-18 omar.polo fprintf(stderr, "%s out of range! Using %d as default.\n",
1006 c758d8da 2019-10-18 omar.polo str, default_value);
1007 1d13acf5 2018-10-04 omar.polo return default_value;
1008 1d13acf5 2018-10-04 omar.polo }
1009 1d13acf5 2018-10-04 omar.polo
1010 1d13acf5 2018-10-04 omar.polo return lval;
1011 42c3f269 2018-07-08 omar.polo }
1012 42c3f269 2018-07-08 omar.polo
1013 c758d8da 2019-10-18 omar.polo /* Like parse_integer but recognize the percentages (i.e. strings ending with
1014 c758d8da 2019-10-18 omar.polo * `%') */
1015 1d13acf5 2018-10-04 omar.polo int
1016 1d13acf5 2018-10-04 omar.polo parse_int_with_percentage(const char *str, int default_value, int max)
1017 1d13acf5 2018-10-04 omar.polo {
1018 1d13acf5 2018-10-04 omar.polo int len = strlen(str);
1019 2128b469 2018-06-30 omar.polo
1020 c758d8da 2019-10-18 omar.polo if (len > 0 && str[len - 1] == '%') {
1021 1d13acf5 2018-10-04 omar.polo int val;
1022 1d13acf5 2018-10-04 omar.polo char *cpy;
1023 2128b469 2018-06-30 omar.polo
1024 92803b00 2019-10-19 omar.polo if ((cpy = strdup(str)) == NULL)
1025 92803b00 2019-10-19 omar.polo err(1, "strdup");
1026 92803b00 2019-10-19 omar.polo
1027 c758d8da 2019-10-18 omar.polo cpy[len - 1] = '\0';
1028 1d13acf5 2018-10-04 omar.polo val = parse_integer(cpy, default_value);
1029 1d13acf5 2018-10-04 omar.polo free(cpy);
1030 1d13acf5 2018-10-04 omar.polo return val * max / 100;
1031 1d13acf5 2018-10-04 omar.polo }
1032 4f769efa 2018-10-06 omar.polo
1033 1d13acf5 2018-10-04 omar.polo return parse_integer(str, default_value);
1034 2128b469 2018-06-30 omar.polo }
1035 2128b469 2018-06-30 omar.polo
1036 c758d8da 2019-10-18 omar.polo /*
1037 1d13acf5 2018-10-04 omar.polo * Like parse_int_with_percentage but understands some special values:
1038 1d13acf5 2018-10-04 omar.polo * - middle that is (max-self)/2
1039 0d836add 2019-10-20 omar.polo * - center = middle
1040 1d13acf5 2018-10-04 omar.polo * - start that is 0
1041 1d13acf5 2018-10-04 omar.polo * - end that is (max-self)
1042 1d13acf5 2018-10-04 omar.polo */
1043 1d13acf5 2018-10-04 omar.polo int
1044 1d13acf5 2018-10-04 omar.polo parse_int_with_pos(const char *str, int default_value, int max, int self)
1045 1d13acf5 2018-10-04 omar.polo {
1046 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "start"))
1047 1d13acf5 2018-10-04 omar.polo return 0;
1048 f4f1270d 2019-10-18 omar.polo if (!strcmp(str, "middle") || !strcmp(str, "center"))
1049 c758d8da 2019-10-18 omar.polo return (max - self) / 2;
1050 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "end"))
1051 c758d8da 2019-10-18 omar.polo return max - self;
1052 1d13acf5 2018-10-04 omar.polo return parse_int_with_percentage(str, default_value, max);
1053 991c5d3c 2018-08-13 omar.polo }
1054 991c5d3c 2018-08-13 omar.polo
1055 1d13acf5 2018-10-04 omar.polo /* Parse a string like a CSS value. */
1056 1d13acf5 2018-10-04 omar.polo /* TODO: harden a bit this function */
1057 1d13acf5 2018-10-04 omar.polo char **
1058 1d13acf5 2018-10-04 omar.polo parse_csslike(const char *str)
1059 1d13acf5 2018-10-04 omar.polo {
1060 c758d8da 2019-10-18 omar.polo int i, j;
1061 c758d8da 2019-10-18 omar.polo char *s, *token, **ret;
1062 c758d8da 2019-10-18 omar.polo short any_null;
1063 991c5d3c 2018-08-13 omar.polo
1064 1d13acf5 2018-10-04 omar.polo s = strdup(str);
1065 1d13acf5 2018-10-04 omar.polo if (s == NULL)
1066 1d13acf5 2018-10-04 omar.polo return NULL;
1067 991c5d3c 2018-08-13 omar.polo
1068 c758d8da 2019-10-18 omar.polo ret = malloc(4 * sizeof(char *));
1069 1d13acf5 2018-10-04 omar.polo if (ret == NULL) {
1070 1d13acf5 2018-10-04 omar.polo free(s);
1071 1d13acf5 2018-10-04 omar.polo return NULL;
1072 1d13acf5 2018-10-04 omar.polo }
1073 991c5d3c 2018-08-13 omar.polo
1074 4f769efa 2018-10-06 omar.polo for (i = 0; (token = strsep(&s, " ")) != NULL && i < 4; ++i)
1075 1d13acf5 2018-10-04 omar.polo ret[i] = strdup(token);
1076 991c5d3c 2018-08-13 omar.polo
1077 1d13acf5 2018-10-04 omar.polo if (i == 1)
1078 4f769efa 2018-10-06 omar.polo for (j = 1; j < 4; j++)
1079 1d13acf5 2018-10-04 omar.polo ret[j] = strdup(ret[0]);
1080 991c5d3c 2018-08-13 omar.polo
1081 1d13acf5 2018-10-04 omar.polo if (i == 2) {
1082 1d13acf5 2018-10-04 omar.polo ret[2] = strdup(ret[0]);
1083 1d13acf5 2018-10-04 omar.polo ret[3] = strdup(ret[1]);
1084 1d13acf5 2018-10-04 omar.polo }
1085 991c5d3c 2018-08-13 omar.polo
1086 1d13acf5 2018-10-04 omar.polo if (i == 3)
1087 1d13acf5 2018-10-04 omar.polo ret[3] = strdup(ret[1]);
1088 991c5d3c 2018-08-13 omar.polo
1089 c758d8da 2019-10-18 omar.polo /* before we didn't check for the return type of strdup, here we will
1090 c758d8da 2019-10-18 omar.polo */
1091 991c5d3c 2018-08-13 omar.polo
1092 26b541d8 2018-10-04 omar.polo any_null = 0;
1093 1d13acf5 2018-10-04 omar.polo for (i = 0; i < 4; ++i)
1094 1d13acf5 2018-10-04 omar.polo any_null = ret[i] == NULL || any_null;
1095 991c5d3c 2018-08-13 omar.polo
1096 1d13acf5 2018-10-04 omar.polo if (any_null)
1097 1d13acf5 2018-10-04 omar.polo for (i = 0; i < 4; ++i)
1098 1d13acf5 2018-10-04 omar.polo if (ret[i] != NULL)
1099 1d13acf5 2018-10-04 omar.polo free(ret[i]);
1100 991c5d3c 2018-08-13 omar.polo
1101 1d13acf5 2018-10-04 omar.polo if (i == 0 || any_null) {
1102 1d13acf5 2018-10-04 omar.polo free(s);
1103 1d13acf5 2018-10-04 omar.polo free(ret);
1104 1d13acf5 2018-10-04 omar.polo return NULL;
1105 1d13acf5 2018-10-04 omar.polo }
1106 991c5d3c 2018-08-13 omar.polo
1107 1d13acf5 2018-10-04 omar.polo return ret;
1108 1d13acf5 2018-10-04 omar.polo }
1109 991c5d3c 2018-08-13 omar.polo
1110 c758d8da 2019-10-18 omar.polo /*
1111 1d13acf5 2018-10-04 omar.polo * Given an event, try to understand what the users wants. If the
1112 1d13acf5 2018-10-04 omar.polo * return value is ADD_CHAR then `input' is a pointer to a string that
1113 1d13acf5 2018-10-04 omar.polo * will need to be free'ed later.
1114 1d13acf5 2018-10-04 omar.polo */
1115 c758d8da 2019-10-18 omar.polo enum action
1116 c758d8da 2019-10-18 omar.polo parse_event(Display *d, XKeyPressedEvent *ev, XIC xic, char **input)
1117 1d13acf5 2018-10-04 omar.polo {
1118 c758d8da 2019-10-18 omar.polo char str[SYM_BUF_SIZE] = { 0 };
1119 c758d8da 2019-10-18 omar.polo Status s;
1120 991c5d3c 2018-08-13 omar.polo
1121 1d13acf5 2018-10-04 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_BackSpace))
1122 1d13acf5 2018-10-04 omar.polo return DEL_CHAR;
1123 991c5d3c 2018-08-13 omar.polo
1124 1d13acf5 2018-10-04 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Tab))
1125 1d13acf5 2018-10-04 omar.polo return ev->state & ShiftMask ? PREV_COMPL : NEXT_COMPL;
1126 991c5d3c 2018-08-13 omar.polo
1127 1d13acf5 2018-10-04 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Return))
1128 1d13acf5 2018-10-04 omar.polo return CONFIRM;
1129 991c5d3c 2018-08-13 omar.polo
1130 1d13acf5 2018-10-04 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Escape))
1131 1d13acf5 2018-10-04 omar.polo return EXIT;
1132 991c5d3c 2018-08-13 omar.polo
1133 1d13acf5 2018-10-04 omar.polo /* Try to read what key was pressed */
1134 1d13acf5 2018-10-04 omar.polo s = 0;
1135 1d13acf5 2018-10-04 omar.polo Xutf8LookupString(xic, ev, str, SYM_BUF_SIZE, 0, &s);
1136 1d13acf5 2018-10-04 omar.polo if (s == XBufferOverflow) {
1137 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1138 c758d8da 2019-10-18 omar.polo "Buffer overflow when trying to create keyboard "
1139 c758d8da 2019-10-18 omar.polo "symbol map.\n");
1140 1d13acf5 2018-10-04 omar.polo return EXIT;
1141 1d13acf5 2018-10-04 omar.polo }
1142 991c5d3c 2018-08-13 omar.polo
1143 1d13acf5 2018-10-04 omar.polo if (ev->state & ControlMask) {
1144 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "")) /* C-u */
1145 1d13acf5 2018-10-04 omar.polo return DEL_LINE;
1146 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "")) /* C-w */
1147 1d13acf5 2018-10-04 omar.polo return DEL_WORD;
1148 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "")) /* C-h */
1149 1d13acf5 2018-10-04 omar.polo return DEL_CHAR;
1150 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "\r")) /* C-m */
1151 1d13acf5 2018-10-04 omar.polo return CONFIRM_CONTINUE;
1152 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "")) /* C-p */
1153 1d13acf5 2018-10-04 omar.polo return PREV_COMPL;
1154 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "")) /* C-n */
1155 1d13acf5 2018-10-04 omar.polo return NEXT_COMPL;
1156 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "")) /* C-c */
1157 1d13acf5 2018-10-04 omar.polo return EXIT;
1158 1d13acf5 2018-10-04 omar.polo if (!strcmp(str, "\t")) /* C-i */
1159 1d13acf5 2018-10-04 omar.polo return TOGGLE_FIRST_SELECTED;
1160 1d13acf5 2018-10-04 omar.polo }
1161 991c5d3c 2018-08-13 omar.polo
1162 1d13acf5 2018-10-04 omar.polo *input = strdup(str);
1163 1d13acf5 2018-10-04 omar.polo if (*input == NULL) {
1164 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Error while allocating memory for key.\n");
1165 1d13acf5 2018-10-04 omar.polo return EXIT;
1166 1d13acf5 2018-10-04 omar.polo }
1167 991c5d3c 2018-08-13 omar.polo
1168 1d13acf5 2018-10-04 omar.polo return ADD_CHAR;
1169 1d13acf5 2018-10-04 omar.polo }
1170 991c5d3c 2018-08-13 omar.polo
1171 1d13acf5 2018-10-04 omar.polo void
1172 c758d8da 2019-10-18 omar.polo confirm(enum state *status, struct rendering *r, struct completions *cs,
1173 c758d8da 2019-10-18 omar.polo char **text, int *textlen)
1174 1d13acf5 2018-10-04 omar.polo {
1175 1d13acf5 2018-10-04 omar.polo if ((cs->selected != -1) || (cs->length > 0 && r->first_selected)) {
1176 1d13acf5 2018-10-04 omar.polo /* if there is something selected expand it and return */
1177 1d13acf5 2018-10-04 omar.polo int index = cs->selected == -1 ? 0 : cs->selected;
1178 1d13acf5 2018-10-04 omar.polo struct completion *c = cs->completions;
1179 1d13acf5 2018-10-04 omar.polo char *t;
1180 991c5d3c 2018-08-13 omar.polo
1181 26b541d8 2018-10-04 omar.polo while (1) {
1182 1d13acf5 2018-10-04 omar.polo if (index == 0)
1183 1d13acf5 2018-10-04 omar.polo break;
1184 1d13acf5 2018-10-04 omar.polo c++;
1185 1d13acf5 2018-10-04 omar.polo index--;
1186 1d13acf5 2018-10-04 omar.polo }
1187 991c5d3c 2018-08-13 omar.polo
1188 1d13acf5 2018-10-04 omar.polo t = c->rcompletion;
1189 1d13acf5 2018-10-04 omar.polo free(*text);
1190 1d13acf5 2018-10-04 omar.polo *text = strdup(t);
1191 991c5d3c 2018-08-13 omar.polo
1192 1d13acf5 2018-10-04 omar.polo if (*text == NULL) {
1193 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Memory allocation error\n");
1194 1d13acf5 2018-10-04 omar.polo *status = ERR;
1195 1d13acf5 2018-10-04 omar.polo }
1196 991c5d3c 2018-08-13 omar.polo
1197 1d13acf5 2018-10-04 omar.polo *textlen = strlen(*text);
1198 1d13acf5 2018-10-04 omar.polo return;
1199 1d13acf5 2018-10-04 omar.polo }
1200 1d13acf5 2018-10-04 omar.polo
1201 1d13acf5 2018-10-04 omar.polo if (!r->free_text) /* cannot accept arbitrary text */
1202 1d13acf5 2018-10-04 omar.polo *status = LOOPING;
1203 a81dbe5d 2019-10-19 omar.polo }
1204 a81dbe5d 2019-10-19 omar.polo
1205 a81dbe5d 2019-10-19 omar.polo /* cs: completion list
1206 a81dbe5d 2019-10-19 omar.polo * offset: the offset of the click
1207 a81dbe5d 2019-10-19 omar.polo * first: the first (rendered) item
1208 a81dbe5d 2019-10-19 omar.polo * def: the default action
1209 a81dbe5d 2019-10-19 omar.polo */
1210 a81dbe5d 2019-10-19 omar.polo enum action
1211 a81dbe5d 2019-10-19 omar.polo select_clicked(
1212 a81dbe5d 2019-10-19 omar.polo struct completions *cs, size_t offset, size_t first, enum action def)
1213 a81dbe5d 2019-10-19 omar.polo {
1214 a81dbe5d 2019-10-19 omar.polo ssize_t selected = first;
1215 a81dbe5d 2019-10-19 omar.polo int set = 0;
1216 a81dbe5d 2019-10-19 omar.polo
1217 a81dbe5d 2019-10-19 omar.polo if (cs->length == 0)
1218 a81dbe5d 2019-10-19 omar.polo return NO_OP;
1219 a81dbe5d 2019-10-19 omar.polo
1220 a81dbe5d 2019-10-19 omar.polo if (offset < cs->completions[selected].offset)
1221 a81dbe5d 2019-10-19 omar.polo return NO_OP;
1222 a81dbe5d 2019-10-19 omar.polo
1223 a81dbe5d 2019-10-19 omar.polo /* skip the first entry */
1224 a81dbe5d 2019-10-19 omar.polo for (selected += 1; selected < cs->length; ++selected) {
1225 eda65435 2019-10-19 omar.polo if (cs->completions[selected].offset == -1)
1226 a81dbe5d 2019-10-19 omar.polo break;
1227 eda65435 2019-10-19 omar.polo
1228 a81dbe5d 2019-10-19 omar.polo if (offset < cs->completions[selected].offset) {
1229 a81dbe5d 2019-10-19 omar.polo cs->selected = selected - 1;
1230 a81dbe5d 2019-10-19 omar.polo set = 1;
1231 a81dbe5d 2019-10-19 omar.polo break;
1232 a81dbe5d 2019-10-19 omar.polo }
1233 a81dbe5d 2019-10-19 omar.polo }
1234 a81dbe5d 2019-10-19 omar.polo
1235 a81dbe5d 2019-10-19 omar.polo if (!set)
1236 a81dbe5d 2019-10-19 omar.polo cs->selected = selected - 1;
1237 a81dbe5d 2019-10-19 omar.polo
1238 a81dbe5d 2019-10-19 omar.polo return def;
1239 a81dbe5d 2019-10-19 omar.polo }
1240 a81dbe5d 2019-10-19 omar.polo
1241 a81dbe5d 2019-10-19 omar.polo enum action
1242 a81dbe5d 2019-10-19 omar.polo handle_mouse(
1243 a81dbe5d 2019-10-19 omar.polo struct rendering *r, struct completions *cs, XButtonPressedEvent *e)
1244 a81dbe5d 2019-10-19 omar.polo {
1245 a81dbe5d 2019-10-19 omar.polo size_t off;
1246 a81dbe5d 2019-10-19 omar.polo
1247 a81dbe5d 2019-10-19 omar.polo if (r->horizontal_layout)
1248 a81dbe5d 2019-10-19 omar.polo off = e->x;
1249 a81dbe5d 2019-10-19 omar.polo else
1250 a81dbe5d 2019-10-19 omar.polo off = e->y;
1251 a81dbe5d 2019-10-19 omar.polo
1252 a81dbe5d 2019-10-19 omar.polo switch (e->button) {
1253 a81dbe5d 2019-10-19 omar.polo case Button1:
1254 a81dbe5d 2019-10-19 omar.polo return select_clicked(cs, off, r->offset, CONFIRM);
1255 a81dbe5d 2019-10-19 omar.polo
1256 a81dbe5d 2019-10-19 omar.polo case Button3:
1257 a81dbe5d 2019-10-19 omar.polo return select_clicked(cs, off, r->offset, CONFIRM_CONTINUE);
1258 a81dbe5d 2019-10-19 omar.polo
1259 a81dbe5d 2019-10-19 omar.polo case Button4:
1260 a81dbe5d 2019-10-19 omar.polo return SCROLL_UP;
1261 a81dbe5d 2019-10-19 omar.polo
1262 a81dbe5d 2019-10-19 omar.polo case Button5:
1263 a81dbe5d 2019-10-19 omar.polo return SCROLL_DOWN;
1264 a81dbe5d 2019-10-19 omar.polo }
1265 a81dbe5d 2019-10-19 omar.polo
1266 a81dbe5d 2019-10-19 omar.polo return NO_OP;
1267 e9f0b467 2018-06-07 omar.polo }
1268 e9f0b467 2018-06-07 omar.polo
1269 1d13acf5 2018-10-04 omar.polo /* event loop */
1270 1d13acf5 2018-10-04 omar.polo enum state
1271 c758d8da 2019-10-18 omar.polo loop(struct rendering *r, char **text, int *textlen, struct completions *cs,
1272 c758d8da 2019-10-18 omar.polo char **lines, char **vlines)
1273 1d13acf5 2018-10-04 omar.polo {
1274 1d13acf5 2018-10-04 omar.polo enum state status = LOOPING;
1275 3518f203 2018-07-21 omar.polo
1276 1d13acf5 2018-10-04 omar.polo while (status == LOOPING) {
1277 1d13acf5 2018-10-04 omar.polo XEvent e;
1278 1d13acf5 2018-10-04 omar.polo XNextEvent(r->d, &e);
1279 1ef91f4e 2018-07-03 omar.polo
1280 1d13acf5 2018-10-04 omar.polo if (XFilterEvent(&e, r->w))
1281 1d13acf5 2018-10-04 omar.polo continue;
1282 e9f0b467 2018-06-07 omar.polo
1283 1d13acf5 2018-10-04 omar.polo switch (e.type) {
1284 1d13acf5 2018-10-04 omar.polo case KeymapNotify:
1285 1d13acf5 2018-10-04 omar.polo XRefreshKeyboardMapping(&e.xmapping);
1286 1d13acf5 2018-10-04 omar.polo break;
1287 844addbb 2018-07-15 omar.polo
1288 1d13acf5 2018-10-04 omar.polo case FocusIn:
1289 1d13acf5 2018-10-04 omar.polo /* Re-grab focus */
1290 1d13acf5 2018-10-04 omar.polo if (e.xfocus.window != r->w)
1291 1d13acf5 2018-10-04 omar.polo grabfocus(r->d, r->w);
1292 1d13acf5 2018-10-04 omar.polo break;
1293 3518f203 2018-07-21 omar.polo
1294 1d13acf5 2018-10-04 omar.polo case VisibilityNotify:
1295 1d13acf5 2018-10-04 omar.polo if (e.xvisibility.state != VisibilityUnobscured)
1296 1d13acf5 2018-10-04 omar.polo XRaiseWindow(r->d, r->w);
1297 1d13acf5 2018-10-04 omar.polo break;
1298 991c5d3c 2018-08-13 omar.polo
1299 1d13acf5 2018-10-04 omar.polo case MapNotify:
1300 1d13acf5 2018-10-04 omar.polo get_wh(r->d, &r->w, &r->width, &r->height);
1301 1d13acf5 2018-10-04 omar.polo draw(r, *text, cs);
1302 1d13acf5 2018-10-04 omar.polo break;
1303 e9f0b467 2018-06-07 omar.polo
1304 a81dbe5d 2019-10-19 omar.polo case KeyPress:
1305 a81dbe5d 2019-10-19 omar.polo case ButtonPress: {
1306 a81dbe5d 2019-10-19 omar.polo enum action a;
1307 a81dbe5d 2019-10-19 omar.polo char *input = NULL;
1308 1a4491e5 2018-09-19 omar.polo
1309 a81dbe5d 2019-10-19 omar.polo if (e.type == KeyPress)
1310 a81dbe5d 2019-10-19 omar.polo a = parse_event(r->d, (XKeyPressedEvent *)&e,
1311 a81dbe5d 2019-10-19 omar.polo r->xic, &input);
1312 a81dbe5d 2019-10-19 omar.polo else
1313 a81dbe5d 2019-10-19 omar.polo a = handle_mouse(
1314 a81dbe5d 2019-10-19 omar.polo r, cs, (XButtonPressedEvent *)&e);
1315 a81dbe5d 2019-10-19 omar.polo
1316 a81dbe5d 2019-10-19 omar.polo switch (a) {
1317 a81dbe5d 2019-10-19 omar.polo case NO_OP:
1318 a81dbe5d 2019-10-19 omar.polo break;
1319 a81dbe5d 2019-10-19 omar.polo
1320 1d13acf5 2018-10-04 omar.polo case EXIT:
1321 1d13acf5 2018-10-04 omar.polo status = ERR;
1322 1d13acf5 2018-10-04 omar.polo break;
1323 f5e234d6 2018-05-18 omar.polo
1324 1d13acf5 2018-10-04 omar.polo case CONFIRM: {
1325 1d13acf5 2018-10-04 omar.polo status = OK;
1326 1d13acf5 2018-10-04 omar.polo confirm(&status, r, cs, text, textlen);
1327 1d13acf5 2018-10-04 omar.polo break;
1328 1d13acf5 2018-10-04 omar.polo }
1329 3518f203 2018-07-21 omar.polo
1330 1d13acf5 2018-10-04 omar.polo case CONFIRM_CONTINUE: {
1331 1d13acf5 2018-10-04 omar.polo status = OK_LOOP;
1332 1d13acf5 2018-10-04 omar.polo confirm(&status, r, cs, text, textlen);
1333 1d13acf5 2018-10-04 omar.polo break;
1334 1d13acf5 2018-10-04 omar.polo }
1335 f5e234d6 2018-05-18 omar.polo
1336 1d13acf5 2018-10-04 omar.polo case PREV_COMPL: {
1337 c758d8da 2019-10-18 omar.polo complete(cs, r->first_selected, 1, text,
1338 c758d8da 2019-10-18 omar.polo textlen, &status);
1339 1d13acf5 2018-10-04 omar.polo r->offset = cs->selected;
1340 1d13acf5 2018-10-04 omar.polo break;
1341 1d13acf5 2018-10-04 omar.polo }
1342 f5e234d6 2018-05-18 omar.polo
1343 1d13acf5 2018-10-04 omar.polo case NEXT_COMPL: {
1344 c758d8da 2019-10-18 omar.polo complete(cs, r->first_selected, 0, text,
1345 c758d8da 2019-10-18 omar.polo textlen, &status);
1346 1d13acf5 2018-10-04 omar.polo r->offset = cs->selected;
1347 1d13acf5 2018-10-04 omar.polo break;
1348 1d13acf5 2018-10-04 omar.polo }
1349 f5e234d6 2018-05-18 omar.polo
1350 1d13acf5 2018-10-04 omar.polo case DEL_CHAR:
1351 1d13acf5 2018-10-04 omar.polo popc(*text);
1352 c758d8da 2019-10-18 omar.polo update_completions(cs, *text, lines, vlines,
1353 c758d8da 2019-10-18 omar.polo r->first_selected);
1354 1d13acf5 2018-10-04 omar.polo r->offset = 0;
1355 1d13acf5 2018-10-04 omar.polo break;
1356 f5e234d6 2018-05-18 omar.polo
1357 1d13acf5 2018-10-04 omar.polo case DEL_WORD: {
1358 1d13acf5 2018-10-04 omar.polo popw(*text);
1359 c758d8da 2019-10-18 omar.polo update_completions(cs, *text, lines, vlines,
1360 c758d8da 2019-10-18 omar.polo r->first_selected);
1361 1d13acf5 2018-10-04 omar.polo break;
1362 1d13acf5 2018-10-04 omar.polo }
1363 f5e234d6 2018-05-18 omar.polo
1364 1d13acf5 2018-10-04 omar.polo case DEL_LINE: {
1365 1d13acf5 2018-10-04 omar.polo int i;
1366 1d13acf5 2018-10-04 omar.polo for (i = 0; i < *textlen; ++i)
1367 1d13acf5 2018-10-04 omar.polo *(*text + i) = 0;
1368 c758d8da 2019-10-18 omar.polo update_completions(cs, *text, lines, vlines,
1369 c758d8da 2019-10-18 omar.polo r->first_selected);
1370 1d13acf5 2018-10-04 omar.polo r->offset = 0;
1371 1d13acf5 2018-10-04 omar.polo break;
1372 1d13acf5 2018-10-04 omar.polo }
1373 e5186d6b 2018-05-26 omar.polo
1374 1d13acf5 2018-10-04 omar.polo case ADD_CHAR: {
1375 1d13acf5 2018-10-04 omar.polo int str_len, i;
1376 42c3f269 2018-07-08 omar.polo
1377 1d13acf5 2018-10-04 omar.polo str_len = strlen(input);
1378 8758854a 2018-05-20 omar.polo
1379 1d13acf5 2018-10-04 omar.polo /*
1380 1d13acf5 2018-10-04 omar.polo * sometimes a strange key is pressed
1381 1d13acf5 2018-10-04 omar.polo * i.e. ctrl alone), so input will be
1382 1d13acf5 2018-10-04 omar.polo * empty. Don't need to update
1383 1d13acf5 2018-10-04 omar.polo * completion in that case
1384 1d13acf5 2018-10-04 omar.polo */
1385 1d13acf5 2018-10-04 omar.polo if (str_len == 0)
1386 1d13acf5 2018-10-04 omar.polo break;
1387 f5e234d6 2018-05-18 omar.polo
1388 1d13acf5 2018-10-04 omar.polo for (i = 0; i < str_len; ++i) {
1389 c758d8da 2019-10-18 omar.polo *textlen = pushc(
1390 c758d8da 2019-10-18 omar.polo text, *textlen, input[i]);
1391 1d13acf5 2018-10-04 omar.polo if (*textlen == -1) {
1392 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1393 c758d8da 2019-10-18 omar.polo "Memory allocation "
1394 c758d8da 2019-10-18 omar.polo "error\n");
1395 1d13acf5 2018-10-04 omar.polo status = ERR;
1396 1d13acf5 2018-10-04 omar.polo break;
1397 1d13acf5 2018-10-04 omar.polo }
1398 1d13acf5 2018-10-04 omar.polo }
1399 f5e234d6 2018-05-18 omar.polo
1400 1d13acf5 2018-10-04 omar.polo if (status != ERR) {
1401 c758d8da 2019-10-18 omar.polo update_completions(cs, *text, lines,
1402 c758d8da 2019-10-18 omar.polo vlines, r->first_selected);
1403 1d13acf5 2018-10-04 omar.polo free(input);
1404 1d13acf5 2018-10-04 omar.polo }
1405 f5e234d6 2018-05-18 omar.polo
1406 1d13acf5 2018-10-04 omar.polo r->offset = 0;
1407 1d13acf5 2018-10-04 omar.polo break;
1408 1d13acf5 2018-10-04 omar.polo }
1409 f5e234d6 2018-05-18 omar.polo
1410 1d13acf5 2018-10-04 omar.polo case TOGGLE_FIRST_SELECTED:
1411 1d13acf5 2018-10-04 omar.polo r->first_selected = !r->first_selected;
1412 1d13acf5 2018-10-04 omar.polo if (r->first_selected && cs->selected < 0)
1413 1d13acf5 2018-10-04 omar.polo cs->selected = 0;
1414 1d13acf5 2018-10-04 omar.polo if (!r->first_selected && cs->selected == 0)
1415 1d13acf5 2018-10-04 omar.polo cs->selected = -1;
1416 1d13acf5 2018-10-04 omar.polo break;
1417 844addbb 2018-07-15 omar.polo
1418 a81dbe5d 2019-10-19 omar.polo case SCROLL_DOWN:
1419 c758d8da 2019-10-18 omar.polo r->offset
1420 c758d8da 2019-10-18 omar.polo = MIN(r->offset + 1, cs->length - 1);
1421 a81dbe5d 2019-10-19 omar.polo break;
1422 f5e234d6 2018-05-18 omar.polo
1423 a81dbe5d 2019-10-19 omar.polo case SCROLL_UP:
1424 a81dbe5d 2019-10-19 omar.polo r->offset = MAX((ssize_t)r->offset - 1, 0);
1425 a81dbe5d 2019-10-19 omar.polo break;
1426 a81dbe5d 2019-10-19 omar.polo }
1427 1d13acf5 2018-10-04 omar.polo }
1428 1d13acf5 2018-10-04 omar.polo }
1429 f5e234d6 2018-05-18 omar.polo
1430 1d13acf5 2018-10-04 omar.polo draw(r, *text, cs);
1431 1d13acf5 2018-10-04 omar.polo }
1432 6bb186a7 2018-09-13 omar.polo
1433 1d13acf5 2018-10-04 omar.polo return status;
1434 1d13acf5 2018-10-04 omar.polo }
1435 6bb186a7 2018-09-13 omar.polo
1436 1d13acf5 2018-10-04 omar.polo int
1437 1d13acf5 2018-10-04 omar.polo load_font(struct rendering *r, const char *fontname)
1438 1d13acf5 2018-10-04 omar.polo {
1439 1d13acf5 2018-10-04 omar.polo #ifdef USE_XFT
1440 1d13acf5 2018-10-04 omar.polo r->font = XftFontOpenName(r->d, DefaultScreen(r->d), fontname);
1441 1d13acf5 2018-10-04 omar.polo return 0;
1442 1d13acf5 2018-10-04 omar.polo #else
1443 1d13acf5 2018-10-04 omar.polo char **missing_charset_list;
1444 1d13acf5 2018-10-04 omar.polo int missing_charset_count;
1445 6bb186a7 2018-09-13 omar.polo
1446 c758d8da 2019-10-18 omar.polo r->font = XCreateFontSet(r->d, fontname, &missing_charset_list,
1447 c758d8da 2019-10-18 omar.polo &missing_charset_count, NULL);
1448 1d13acf5 2018-10-04 omar.polo if (r->font != NULL)
1449 1d13acf5 2018-10-04 omar.polo return 0;
1450 6bb186a7 2018-09-13 omar.polo
1451 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Unable to load the font(s) %s\n", fontname);
1452 36a15a9f 2018-05-19 omar.polo
1453 1d13acf5 2018-10-04 omar.polo if (!strcmp(fontname, default_fontname))
1454 1d13acf5 2018-10-04 omar.polo return -1;
1455 f5e234d6 2018-05-18 omar.polo
1456 1d13acf5 2018-10-04 omar.polo return load_font(r, default_fontname);
1457 1d13acf5 2018-10-04 omar.polo #endif
1458 1d13acf5 2018-10-04 omar.polo }
1459 36a15a9f 2018-05-19 omar.polo
1460 1d13acf5 2018-10-04 omar.polo void
1461 4f769efa 2018-10-06 omar.polo xim_init(struct rendering *r, XrmDatabase *xdb)
1462 4f769efa 2018-10-06 omar.polo {
1463 c758d8da 2019-10-18 omar.polo XIMStyle best_match_style;
1464 c758d8da 2019-10-18 omar.polo XIMStyles *xis;
1465 c758d8da 2019-10-18 omar.polo int i;
1466 4f769efa 2018-10-06 omar.polo
1467 4f769efa 2018-10-06 omar.polo /* Open the X input method */
1468 92803b00 2019-10-19 omar.polo if ((r->xim = XOpenIM(r->d, *xdb, resname, resclass)) == NULL)
1469 92803b00 2019-10-19 omar.polo err(1, "XOpenIM");
1470 4f769efa 2018-10-06 omar.polo
1471 43e30577 2018-10-20 omar.polo if (XGetIMValues(r->xim, XNQueryInputStyle, &xis, NULL) || !xis) {
1472 4f769efa 2018-10-06 omar.polo fprintf(stderr, "Input Styles could not be retrieved\n");
1473 4f769efa 2018-10-06 omar.polo exit(EX_UNAVAILABLE);
1474 4f769efa 2018-10-06 omar.polo }
1475 4f769efa 2018-10-06 omar.polo
1476 4f769efa 2018-10-06 omar.polo best_match_style = 0;
1477 4f769efa 2018-10-06 omar.polo for (i = 0; i < xis->count_styles; ++i) {
1478 4f769efa 2018-10-06 omar.polo XIMStyle ts = xis->supported_styles[i];
1479 4f769efa 2018-10-06 omar.polo if (ts == (XIMPreeditNothing | XIMStatusNothing)) {
1480 4f769efa 2018-10-06 omar.polo best_match_style = ts;
1481 4f769efa 2018-10-06 omar.polo break;
1482 4f769efa 2018-10-06 omar.polo }
1483 4f769efa 2018-10-06 omar.polo }
1484 4f769efa 2018-10-06 omar.polo XFree(xis);
1485 4f769efa 2018-10-06 omar.polo
1486 4f769efa 2018-10-06 omar.polo if (!best_match_style)
1487 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1488 c758d8da 2019-10-18 omar.polo "No matching input style could be determined\n");
1489 4f769efa 2018-10-06 omar.polo
1490 c758d8da 2019-10-18 omar.polo r->xic = XCreateIC(r->xim, XNInputStyle, best_match_style,
1491 c758d8da 2019-10-18 omar.polo XNClientWindow, r->w, XNFocusWindow, r->w, NULL);
1492 92803b00 2019-10-19 omar.polo if (r->xic == NULL)
1493 92803b00 2019-10-19 omar.polo err(1, "XCreateIC");
1494 4f769efa 2018-10-06 omar.polo }
1495 4f769efa 2018-10-06 omar.polo
1496 4f769efa 2018-10-06 omar.polo void
1497 c758d8da 2019-10-18 omar.polo create_window(struct rendering *r, Window parent_window, Colormap cmap,
1498 c758d8da 2019-10-18 omar.polo XVisualInfo vinfo, int x, int y, int ox, int oy,
1499 c758d8da 2019-10-18 omar.polo unsigned long background_pixel)
1500 4f769efa 2018-10-06 omar.polo {
1501 c758d8da 2019-10-18 omar.polo XSetWindowAttributes attr;
1502 4f769efa 2018-10-06 omar.polo
1503 4f769efa 2018-10-06 omar.polo /* Create the window */
1504 4f769efa 2018-10-06 omar.polo attr.colormap = cmap;
1505 4f769efa 2018-10-06 omar.polo attr.override_redirect = 1;
1506 4f769efa 2018-10-06 omar.polo attr.border_pixel = 0;
1507 1f6422bc 2018-10-20 omar.polo attr.background_pixel = background_pixel;
1508 c758d8da 2019-10-18 omar.polo attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask
1509 c758d8da 2019-10-18 omar.polo | KeymapStateMask | ButtonPress | VisibilityChangeMask;
1510 4f769efa 2018-10-06 omar.polo
1511 c758d8da 2019-10-18 omar.polo r->w = XCreateWindow(r->d, parent_window, x + ox, y + oy, r->width,
1512 c758d8da 2019-10-18 omar.polo r->height, 0, vinfo.depth, InputOutput, vinfo.visual,
1513 c758d8da 2019-10-18 omar.polo CWBorderPixel | CWBackPixel | CWColormap | CWEventMask
1514 c758d8da 2019-10-18 omar.polo | CWOverrideRedirect,
1515 4f769efa 2018-10-06 omar.polo &attr);
1516 4f769efa 2018-10-06 omar.polo }
1517 4f769efa 2018-10-06 omar.polo
1518 4f769efa 2018-10-06 omar.polo void
1519 aed48307 2018-10-06 omar.polo ps1extents(struct rendering *r)
1520 aed48307 2018-10-06 omar.polo {
1521 aed48307 2018-10-06 omar.polo char *dup;
1522 aed48307 2018-10-06 omar.polo dup = strdupn(r->ps1);
1523 c758d8da 2019-10-18 omar.polo text_extents(
1524 c758d8da 2019-10-18 omar.polo dup == NULL ? r->ps1 : dup, r->ps1len, r, &r->ps1w, &r->ps1h);
1525 aed48307 2018-10-06 omar.polo free(dup);
1526 aed48307 2018-10-06 omar.polo }
1527 aed48307 2018-10-06 omar.polo
1528 aed48307 2018-10-06 omar.polo void
1529 1d13acf5 2018-10-04 omar.polo usage(char *prgname)
1530 1d13acf5 2018-10-04 omar.polo {
1531 df29e05e 2018-10-17 omar.polo fprintf(stderr,
1532 df29e05e 2018-10-17 omar.polo "%s [-Aahmv] [-B colors] [-b size] [-C color] [-c color]\n"
1533 c758d8da 2019-10-18 omar.polo " [-d separator] [-e window] [-f font] [-G color] [-g "
1534 c758d8da 2019-10-18 omar.polo "size]\n"
1535 c758d8da 2019-10-18 omar.polo " [-H height] [-I color] [-i size] [-J color] [-j "
1536 c758d8da 2019-10-18 omar.polo "size] [-l layout]\n"
1537 c758d8da 2019-10-18 omar.polo " [-P padding] [-p prompt] [-S color] [-s color] [-T "
1538 c758d8da 2019-10-18 omar.polo "color]\n"
1539 c758d8da 2019-10-18 omar.polo " [-t color] [-W width] [-x coord] [-y coord]\n",
1540 c758d8da 2019-10-18 omar.polo prgname);
1541 1d13acf5 2018-10-04 omar.polo }
1542 f5e234d6 2018-05-18 omar.polo
1543 1d13acf5 2018-10-04 omar.polo int
1544 1d13acf5 2018-10-04 omar.polo main(int argc, char **argv)
1545 1d13acf5 2018-10-04 omar.polo {
1546 c758d8da 2019-10-18 omar.polo struct completions *cs;
1547 c758d8da 2019-10-18 omar.polo struct rendering r;
1548 c758d8da 2019-10-18 omar.polo XVisualInfo vinfo;
1549 c758d8da 2019-10-18 omar.polo Colormap cmap;
1550 c758d8da 2019-10-18 omar.polo size_t nlines, i;
1551 c758d8da 2019-10-18 omar.polo Window parent_window;
1552 c758d8da 2019-10-18 omar.polo XrmDatabase xdb;
1553 c758d8da 2019-10-18 omar.polo unsigned long fgs[3], bgs[3]; /* prompt, compl, compl_highlighted */
1554 c758d8da 2019-10-18 omar.polo unsigned long borders_bg[4], p_borders_bg[4], c_borders_bg[4],
1555 c758d8da 2019-10-18 omar.polo ch_borders_bg[4]; /* N E S W */
1556 c758d8da 2019-10-18 omar.polo enum state status;
1557 c758d8da 2019-10-18 omar.polo int ch;
1558 c758d8da 2019-10-18 omar.polo int offset_x, offset_y, x, y;
1559 c758d8da 2019-10-18 omar.polo int textlen, d_width, d_height;
1560 c758d8da 2019-10-18 omar.polo short embed;
1561 c758d8da 2019-10-18 omar.polo char *sep, *parent_window_id;
1562 c758d8da 2019-10-18 omar.polo char **lines, *buf, **vlines;
1563 c758d8da 2019-10-18 omar.polo char *fontname, *text, *xrm;
1564 f5e234d6 2018-05-18 omar.polo
1565 1d13acf5 2018-10-04 omar.polo #ifdef __OpenBSD__
1566 1d13acf5 2018-10-04 omar.polo /* stdio & rpath: to read/write stdio/stdout/stderr */
1567 1d13acf5 2018-10-04 omar.polo /* unix: to connect to XOrg */
1568 1d13acf5 2018-10-04 omar.polo pledge("stdio rpath unix", "");
1569 1d13acf5 2018-10-04 omar.polo #endif
1570 f5e234d6 2018-05-18 omar.polo
1571 1d13acf5 2018-10-04 omar.polo sep = NULL;
1572 1d13acf5 2018-10-04 omar.polo parent_window_id = NULL;
1573 f5e234d6 2018-05-18 omar.polo
1574 4f769efa 2018-10-06 omar.polo r.first_selected = 0;
1575 4f769efa 2018-10-06 omar.polo r.free_text = 1;
1576 4f769efa 2018-10-06 omar.polo r.multiple_select = 0;
1577 4f769efa 2018-10-06 omar.polo r.offset = 0;
1578 4f769efa 2018-10-06 omar.polo
1579 1d13acf5 2018-10-04 omar.polo while ((ch = getopt(argc, argv, ARGS)) != -1) {
1580 1d13acf5 2018-10-04 omar.polo switch (ch) {
1581 c758d8da 2019-10-18 omar.polo case 'h': /* help */
1582 1d13acf5 2018-10-04 omar.polo usage(*argv);
1583 1d13acf5 2018-10-04 omar.polo return 0;
1584 c758d8da 2019-10-18 omar.polo case 'v': /* version */
1585 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "%s version: %s\n", *argv, VERSION);
1586 1d13acf5 2018-10-04 omar.polo return 0;
1587 c758d8da 2019-10-18 omar.polo case 'e': /* embed */
1588 92803b00 2019-10-19 omar.polo if ((parent_window_id = strdup(optarg)) == NULL)
1589 92803b00 2019-10-19 omar.polo err(1, "strdup");
1590 1d13acf5 2018-10-04 omar.polo break;
1591 4c0d79a0 2018-10-09 omar.polo case 'd':
1592 92803b00 2019-10-19 omar.polo if ((sep = strdup(optarg)) == NULL)
1593 92803b00 2019-10-19 omar.polo err(1, "strdup");
1594 92803b00 2019-10-19 omar.polo break;
1595 4c0d79a0 2018-10-09 omar.polo case 'A':
1596 4f769efa 2018-10-06 omar.polo r.free_text = 0;
1597 1d13acf5 2018-10-04 omar.polo break;
1598 4c0d79a0 2018-10-09 omar.polo case 'm':
1599 4f769efa 2018-10-06 omar.polo r.multiple_select = 1;
1600 1d13acf5 2018-10-04 omar.polo break;
1601 1d13acf5 2018-10-04 omar.polo default:
1602 1d13acf5 2018-10-04 omar.polo break;
1603 1d13acf5 2018-10-04 omar.polo }
1604 1d13acf5 2018-10-04 omar.polo }
1605 e5186d6b 2018-05-26 omar.polo
1606 1d13acf5 2018-10-04 omar.polo /* Read the completions */
1607 1d13acf5 2018-10-04 omar.polo lines = NULL;
1608 1d13acf5 2018-10-04 omar.polo buf = NULL;
1609 1d13acf5 2018-10-04 omar.polo nlines = readlines(&lines, &buf);
1610 42c3f269 2018-07-08 omar.polo
1611 1d13acf5 2018-10-04 omar.polo vlines = NULL;
1612 1d13acf5 2018-10-04 omar.polo if (sep != NULL) {
1613 1d13acf5 2018-10-04 omar.polo int l;
1614 1d13acf5 2018-10-04 omar.polo l = strlen(sep);
1615 92803b00 2019-10-19 omar.polo if ((vlines = calloc(nlines, sizeof(char *))) == NULL)
1616 92803b00 2019-10-19 omar.polo err(1, "calloc");
1617 f5e234d6 2018-05-18 omar.polo
1618 1d13acf5 2018-10-04 omar.polo for (i = 0; i < nlines; i++) {
1619 1d13acf5 2018-10-04 omar.polo char *t;
1620 1d13acf5 2018-10-04 omar.polo t = strstr(lines[i], sep);
1621 1d13acf5 2018-10-04 omar.polo if (t == NULL)
1622 1d13acf5 2018-10-04 omar.polo vlines[i] = lines[i];
1623 1d13acf5 2018-10-04 omar.polo else
1624 1d13acf5 2018-10-04 omar.polo vlines[i] = t + l;
1625 1d13acf5 2018-10-04 omar.polo }
1626 1d13acf5 2018-10-04 omar.polo }
1627 f5e234d6 2018-05-18 omar.polo
1628 1d13acf5 2018-10-04 omar.polo setlocale(LC_ALL, getenv("LANG"));
1629 f5e234d6 2018-05-18 omar.polo
1630 1d13acf5 2018-10-04 omar.polo status = LOOPING;
1631 f5e234d6 2018-05-18 omar.polo
1632 1d13acf5 2018-10-04 omar.polo /* where the monitor start (used only with xinerama) */
1633 1d13acf5 2018-10-04 omar.polo offset_x = offset_y = 0;
1634 f5e234d6 2018-05-18 omar.polo
1635 1d13acf5 2018-10-04 omar.polo /* default width and height */
1636 4f769efa 2018-10-06 omar.polo r.width = 400;
1637 4f769efa 2018-10-06 omar.polo r.height = 20;
1638 f5e234d6 2018-05-18 omar.polo
1639 1d13acf5 2018-10-04 omar.polo /* default position on the screen */
1640 1d13acf5 2018-10-04 omar.polo x = y = 0;
1641 42c3f269 2018-07-08 omar.polo
1642 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i) {
1643 df29e05e 2018-10-17 omar.polo /* default paddings */
1644 df29e05e 2018-10-17 omar.polo r.p_padding[i] = 10;
1645 df29e05e 2018-10-17 omar.polo r.c_padding[i] = 10;
1646 df29e05e 2018-10-17 omar.polo r.ch_padding[i] = 10;
1647 f5e234d6 2018-05-18 omar.polo
1648 df29e05e 2018-10-17 omar.polo /* default borders */
1649 df29e05e 2018-10-17 omar.polo r.borders[i] = 0;
1650 df29e05e 2018-10-17 omar.polo r.p_borders[i] = 0;
1651 df29e05e 2018-10-17 omar.polo r.c_borders[i] = 0;
1652 df29e05e 2018-10-17 omar.polo r.ch_borders[i] = 0;
1653 df29e05e 2018-10-17 omar.polo }
1654 ae801529 2018-07-13 omar.polo
1655 1d13acf5 2018-10-04 omar.polo /* the prompt. We duplicate the string so later is easy to
1656 1d13acf5 2018-10-04 omar.polo * free (in the case it's been overwritten by the user) */
1657 92803b00 2019-10-19 omar.polo if ((r.ps1 = strdup("$ ")) == NULL)
1658 92803b00 2019-10-19 omar.polo err(1, "strdup");
1659 ae801529 2018-07-13 omar.polo
1660 1d13acf5 2018-10-04 omar.polo /* same for the font name */
1661 92803b00 2019-10-19 omar.polo if ((fontname = strdup(default_fontname)) == NULL)
1662 92803b00 2019-10-19 omar.polo err(1, "strdup");
1663 f5e234d6 2018-05-18 omar.polo
1664 1d13acf5 2018-10-04 omar.polo textlen = 10;
1665 92803b00 2019-10-19 omar.polo if ((text = malloc(textlen * sizeof(char))) == NULL)
1666 92803b00 2019-10-19 omar.polo err(1, "malloc");
1667 f5e234d6 2018-05-18 omar.polo
1668 1d13acf5 2018-10-04 omar.polo /* struct completions *cs = filter(text, lines); */
1669 92803b00 2019-10-19 omar.polo if ((cs = compls_new(nlines)) == NULL)
1670 92803b00 2019-10-19 omar.polo err(1, "compls_new");
1671 f5e234d6 2018-05-18 omar.polo
1672 1d13acf5 2018-10-04 omar.polo /* start talking to xorg */
1673 4f769efa 2018-10-06 omar.polo r.d = XOpenDisplay(NULL);
1674 4f769efa 2018-10-06 omar.polo if (r.d == NULL) {
1675 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Could not open display!\n");
1676 1d13acf5 2018-10-04 omar.polo return EX_UNAVAILABLE;
1677 1d13acf5 2018-10-04 omar.polo }
1678 f5e234d6 2018-05-18 omar.polo
1679 26b541d8 2018-10-04 omar.polo embed = 1;
1680 c758d8da 2019-10-18 omar.polo if (!(parent_window_id
1681 c758d8da 2019-10-18 omar.polo && (parent_window = strtol(parent_window_id, NULL, 0)))) {
1682 4f769efa 2018-10-06 omar.polo parent_window = DefaultRootWindow(r.d);
1683 26b541d8 2018-10-04 omar.polo embed = 0;
1684 1d13acf5 2018-10-04 omar.polo }
1685 f5e234d6 2018-05-18 omar.polo
1686 1d13acf5 2018-10-04 omar.polo /* get display size */
1687 4f769efa 2018-10-06 omar.polo get_wh(r.d, &parent_window, &d_width, &d_height);
1688 f5e234d6 2018-05-18 omar.polo
1689 1d13acf5 2018-10-04 omar.polo #ifdef USE_XINERAMA
1690 4f769efa 2018-10-06 omar.polo if (!embed && XineramaIsActive(r.d)) { /* find the mice */
1691 1d13acf5 2018-10-04 omar.polo XineramaScreenInfo *info;
1692 c758d8da 2019-10-18 omar.polo Window rr;
1693 c758d8da 2019-10-18 omar.polo Window root;
1694 c758d8da 2019-10-18 omar.polo int number_of_screens, monitors, i;
1695 c758d8da 2019-10-18 omar.polo int root_x, root_y, win_x, win_y;
1696 c758d8da 2019-10-18 omar.polo unsigned int mask;
1697 c758d8da 2019-10-18 omar.polo short res;
1698 f5e234d6 2018-05-18 omar.polo
1699 4f769efa 2018-10-06 omar.polo number_of_screens = XScreenCount(r.d);
1700 1d13acf5 2018-10-04 omar.polo for (i = 0; i < number_of_screens; ++i) {
1701 4f769efa 2018-10-06 omar.polo root = XRootWindow(r.d, i);
1702 c758d8da 2019-10-18 omar.polo res = XQueryPointer(r.d, root, &rr, &rr, &root_x,
1703 c758d8da 2019-10-18 omar.polo &root_y, &win_x, &win_y, &mask);
1704 4f769efa 2018-10-06 omar.polo if (res)
1705 4f769efa 2018-10-06 omar.polo break;
1706 1d13acf5 2018-10-04 omar.polo }
1707 4f769efa 2018-10-06 omar.polo
1708 1d13acf5 2018-10-04 omar.polo if (!res) {
1709 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "No mouse found.\n");
1710 1d13acf5 2018-10-04 omar.polo root_x = 0;
1711 1d13acf5 2018-10-04 omar.polo root_y = 0;
1712 1d13acf5 2018-10-04 omar.polo }
1713 f5e234d6 2018-05-18 omar.polo
1714 26b541d8 2018-10-04 omar.polo /* Now find in which monitor the mice is */
1715 4f769efa 2018-10-06 omar.polo info = XineramaQueryScreens(r.d, &monitors);
1716 1d13acf5 2018-10-04 omar.polo if (info) {
1717 1d13acf5 2018-10-04 omar.polo for (i = 0; i < monitors; ++i) {
1718 c758d8da 2019-10-18 omar.polo if (info[i].x_org <= root_x
1719 c758d8da 2019-10-18 omar.polo && root_x <= (info[i].x_org
1720 c758d8da 2019-10-18 omar.polo + info[i].width)
1721 c758d8da 2019-10-18 omar.polo && info[i].y_org <= root_y
1722 c758d8da 2019-10-18 omar.polo && root_y <= (info[i].y_org
1723 c758d8da 2019-10-18 omar.polo + info[i].height)) {
1724 1d13acf5 2018-10-04 omar.polo offset_x = info[i].x_org;
1725 1d13acf5 2018-10-04 omar.polo offset_y = info[i].y_org;
1726 1d13acf5 2018-10-04 omar.polo d_width = info[i].width;
1727 1d13acf5 2018-10-04 omar.polo d_height = info[i].height;
1728 1d13acf5 2018-10-04 omar.polo break;
1729 1d13acf5 2018-10-04 omar.polo }
1730 1d13acf5 2018-10-04 omar.polo }
1731 1d13acf5 2018-10-04 omar.polo }
1732 1d13acf5 2018-10-04 omar.polo XFree(info);
1733 1d13acf5 2018-10-04 omar.polo }
1734 42c3f269 2018-07-08 omar.polo #endif
1735 f5e234d6 2018-05-18 omar.polo
1736 4f769efa 2018-10-06 omar.polo XMatchVisualInfo(r.d, DefaultScreen(r.d), 32, TrueColor, &vinfo);
1737 c758d8da 2019-10-18 omar.polo cmap = XCreateColormap(
1738 c758d8da 2019-10-18 omar.polo r.d, XDefaultRootWindow(r.d), vinfo.visual, AllocNone);
1739 c9a3bfaa 2018-05-21 omar.polo
1740 4c0d79a0 2018-10-09 omar.polo fgs[0] = fgs[1] = parse_color("#fff", NULL);
1741 4c0d79a0 2018-10-09 omar.polo fgs[2] = parse_color("#000", NULL);
1742 c9a3bfaa 2018-05-21 omar.polo
1743 4c0d79a0 2018-10-09 omar.polo bgs[0] = bgs[1] = parse_color("#000", NULL);
1744 4c0d79a0 2018-10-09 omar.polo bgs[2] = parse_color("#fff", NULL);
1745 7ca8829b 2018-05-21 omar.polo
1746 c758d8da 2019-10-18 omar.polo borders_bg[0] = borders_bg[1] = borders_bg[2] = borders_bg[3]
1747 c758d8da 2019-10-18 omar.polo = parse_color("#000", NULL);
1748 6bb186a7 2018-09-13 omar.polo
1749 c758d8da 2019-10-18 omar.polo p_borders_bg[0] = p_borders_bg[1] = p_borders_bg[2] = p_borders_bg[3]
1750 c758d8da 2019-10-18 omar.polo = parse_color("#000", NULL);
1751 c758d8da 2019-10-18 omar.polo c_borders_bg[0] = c_borders_bg[1] = c_borders_bg[2] = c_borders_bg[3]
1752 c758d8da 2019-10-18 omar.polo = parse_color("#000", NULL);
1753 c758d8da 2019-10-18 omar.polo ch_borders_bg[0] = ch_borders_bg[1] = ch_borders_bg[2]
1754 c758d8da 2019-10-18 omar.polo = ch_borders_bg[3] = parse_color("#000", NULL);
1755 df29e05e 2018-10-17 omar.polo
1756 4f769efa 2018-10-06 omar.polo r.horizontal_layout = 1;
1757 6bb186a7 2018-09-13 omar.polo
1758 1d13acf5 2018-10-04 omar.polo /* Read the resources */
1759 1d13acf5 2018-10-04 omar.polo XrmInitialize();
1760 4f769efa 2018-10-06 omar.polo xrm = XResourceManagerString(r.d);
1761 1d13acf5 2018-10-04 omar.polo xdb = NULL;
1762 1d13acf5 2018-10-04 omar.polo if (xrm != NULL) {
1763 1d13acf5 2018-10-04 omar.polo XrmValue value;
1764 1d13acf5 2018-10-04 omar.polo char *datatype[20];
1765 1d13acf5 2018-10-04 omar.polo
1766 1d13acf5 2018-10-04 omar.polo xdb = XrmGetStringDatabase(xrm);
1767 1d13acf5 2018-10-04 omar.polo
1768 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.font", "*", datatype, &value)
1769 c758d8da 2019-10-18 omar.polo == 1) {
1770 1d13acf5 2018-10-04 omar.polo free(fontname);
1771 92803b00 2019-10-19 omar.polo if ((fontname = strdup(value.addr)) == NULL)
1772 92803b00 2019-10-19 omar.polo err(1, "strdup");
1773 1d13acf5 2018-10-04 omar.polo } else {
1774 c758d8da 2019-10-18 omar.polo fprintf(stderr, "no font defined, using %s\n",
1775 c758d8da 2019-10-18 omar.polo fontname);
1776 1d13acf5 2018-10-04 omar.polo }
1777 1d13acf5 2018-10-04 omar.polo
1778 c758d8da 2019-10-18 omar.polo if (XrmGetResource(
1779 c758d8da 2019-10-18 omar.polo xdb, "MyMenu.layout", "*", datatype, &value)
1780 c758d8da 2019-10-18 omar.polo == 1)
1781 c758d8da 2019-10-18 omar.polo r.horizontal_layout
1782 c758d8da 2019-10-18 omar.polo = !strcmp(value.addr, "horizontal");
1783 1d13acf5 2018-10-04 omar.polo else
1784 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1785 c758d8da 2019-10-18 omar.polo "no layout defined, using horizontal\n");
1786 1d13acf5 2018-10-04 omar.polo
1787 c758d8da 2019-10-18 omar.polo if (XrmGetResource(
1788 c758d8da 2019-10-18 omar.polo xdb, "MyMenu.prompt", "*", datatype, &value)
1789 c758d8da 2019-10-18 omar.polo == 1) {
1790 4f769efa 2018-10-06 omar.polo free(r.ps1);
1791 4f769efa 2018-10-06 omar.polo r.ps1 = normalize_str(value.addr);
1792 1d13acf5 2018-10-04 omar.polo } else {
1793 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1794 c758d8da 2019-10-18 omar.polo "no prompt defined, using \"%s\" as "
1795 c758d8da 2019-10-18 omar.polo "default\n",
1796 c758d8da 2019-10-18 omar.polo r.ps1);
1797 1d13acf5 2018-10-04 omar.polo }
1798 1d13acf5 2018-10-04 omar.polo
1799 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.border.size", "*",
1800 c758d8da 2019-10-18 omar.polo datatype, &value)
1801 c758d8da 2019-10-18 omar.polo == 1) {
1802 df29e05e 2018-10-17 omar.polo char **sizes;
1803 df29e05e 2018-10-17 omar.polo sizes = parse_csslike(value.addr);
1804 df29e05e 2018-10-17 omar.polo if (sizes != NULL)
1805 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
1806 c758d8da 2019-10-18 omar.polo r.p_borders[i]
1807 c758d8da 2019-10-18 omar.polo = parse_integer(sizes[i], 0);
1808 df29e05e 2018-10-17 omar.polo else
1809 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1810 c758d8da 2019-10-18 omar.polo "error while parsing "
1811 c758d8da 2019-10-18 omar.polo "MyMenu.prompt.border.size");
1812 df29e05e 2018-10-17 omar.polo }
1813 df29e05e 2018-10-17 omar.polo
1814 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.border.color", "*",
1815 c758d8da 2019-10-18 omar.polo datatype, &value)
1816 c758d8da 2019-10-18 omar.polo == 1) {
1817 df29e05e 2018-10-17 omar.polo char **colors;
1818 df29e05e 2018-10-17 omar.polo colors = parse_csslike(value.addr);
1819 df29e05e 2018-10-17 omar.polo if (colors != NULL)
1820 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
1821 c758d8da 2019-10-18 omar.polo p_borders_bg[i] = parse_color(
1822 c758d8da 2019-10-18 omar.polo colors[i], "#000");
1823 df29e05e 2018-10-17 omar.polo else
1824 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1825 c758d8da 2019-10-18 omar.polo "error while parsing "
1826 c758d8da 2019-10-18 omar.polo "MyMenu.prompt.border.color");
1827 df29e05e 2018-10-17 omar.polo }
1828 df29e05e 2018-10-17 omar.polo
1829 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.padding", "*",
1830 c758d8da 2019-10-18 omar.polo datatype, &value)
1831 c758d8da 2019-10-18 omar.polo == 1) {
1832 df29e05e 2018-10-17 omar.polo char **colors;
1833 df29e05e 2018-10-17 omar.polo colors = parse_csslike(value.addr);
1834 df29e05e 2018-10-17 omar.polo if (colors != NULL)
1835 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
1836 c758d8da 2019-10-18 omar.polo r.p_padding[i]
1837 c758d8da 2019-10-18 omar.polo = parse_integer(colors[i], 0);
1838 df29e05e 2018-10-17 omar.polo else
1839 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1840 c758d8da 2019-10-18 omar.polo "error while parsing "
1841 c758d8da 2019-10-18 omar.polo "MyMenu.prompt.padding");
1842 df29e05e 2018-10-17 omar.polo }
1843 df29e05e 2018-10-17 omar.polo
1844 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.width", "*", datatype, &value)
1845 c758d8da 2019-10-18 omar.polo == 1)
1846 c758d8da 2019-10-18 omar.polo r.width = parse_int_with_percentage(
1847 c758d8da 2019-10-18 omar.polo value.addr, r.width, d_width);
1848 1d13acf5 2018-10-04 omar.polo else
1849 c758d8da 2019-10-18 omar.polo fprintf(stderr, "no width defined, using %d\n",
1850 c758d8da 2019-10-18 omar.polo r.width);
1851 1d13acf5 2018-10-04 omar.polo
1852 c758d8da 2019-10-18 omar.polo if (XrmGetResource(
1853 c758d8da 2019-10-18 omar.polo xdb, "MyMenu.height", "*", datatype, &value)
1854 c758d8da 2019-10-18 omar.polo == 1)
1855 c758d8da 2019-10-18 omar.polo r.height = parse_int_with_percentage(
1856 c758d8da 2019-10-18 omar.polo value.addr, r.height, d_height);
1857 1d13acf5 2018-10-04 omar.polo else
1858 c758d8da 2019-10-18 omar.polo fprintf(stderr, "no height defined, using %d\n",
1859 c758d8da 2019-10-18 omar.polo r.height);
1860 1d13acf5 2018-10-04 omar.polo
1861 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.x", "*", datatype, &value)
1862 c758d8da 2019-10-18 omar.polo == 1)
1863 c758d8da 2019-10-18 omar.polo x = parse_int_with_pos(
1864 c758d8da 2019-10-18 omar.polo value.addr, x, d_width, r.width);
1865 1d13acf5 2018-10-04 omar.polo
1866 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.y", "*", datatype, &value)
1867 c758d8da 2019-10-18 omar.polo == 1)
1868 c758d8da 2019-10-18 omar.polo y = parse_int_with_pos(
1869 c758d8da 2019-10-18 omar.polo value.addr, y, d_height, r.height);
1870 1d13acf5 2018-10-04 omar.polo
1871 c758d8da 2019-10-18 omar.polo if (XrmGetResource(
1872 c758d8da 2019-10-18 omar.polo xdb, "MyMenu.border.size", "*", datatype, &value)
1873 c758d8da 2019-10-18 omar.polo == 1) {
1874 1d13acf5 2018-10-04 omar.polo char **borders;
1875 1d13acf5 2018-10-04 omar.polo borders = parse_csslike(value.addr);
1876 df29e05e 2018-10-17 omar.polo if (borders != NULL)
1877 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
1878 c758d8da 2019-10-18 omar.polo r.borders[i]
1879 c758d8da 2019-10-18 omar.polo = parse_int_with_percentage(
1880 c758d8da 2019-10-18 omar.polo borders[i], 0,
1881 c758d8da 2019-10-18 omar.polo (i % 2) == 0
1882 c758d8da 2019-10-18 omar.polo ? d_height
1883 c758d8da 2019-10-18 omar.polo : d_width);
1884 df29e05e 2018-10-17 omar.polo else
1885 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1886 c758d8da 2019-10-18 omar.polo "error while parsing "
1887 c758d8da 2019-10-18 omar.polo "MyMenu.border.size\n");
1888 df29e05e 2018-10-17 omar.polo }
1889 1d13acf5 2018-10-04 omar.polo
1890 1d13acf5 2018-10-04 omar.polo /* Prompt */
1891 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.foreground", "*",
1892 c758d8da 2019-10-18 omar.polo datatype, &value)
1893 c758d8da 2019-10-18 omar.polo == 1)
1894 4c0d79a0 2018-10-09 omar.polo fgs[0] = parse_color(value.addr, "#fff");
1895 1d13acf5 2018-10-04 omar.polo
1896 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.background", "*",
1897 c758d8da 2019-10-18 omar.polo datatype, &value)
1898 c758d8da 2019-10-18 omar.polo == 1)
1899 4c0d79a0 2018-10-09 omar.polo bgs[0] = parse_color(value.addr, "#000");
1900 1d13acf5 2018-10-04 omar.polo
1901 1d13acf5 2018-10-04 omar.polo /* Completions */
1902 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.foreground", "*",
1903 c758d8da 2019-10-18 omar.polo datatype, &value)
1904 c758d8da 2019-10-18 omar.polo == 1)
1905 4c0d79a0 2018-10-09 omar.polo fgs[1] = parse_color(value.addr, "#fff");
1906 1d13acf5 2018-10-04 omar.polo
1907 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.background", "*",
1908 c758d8da 2019-10-18 omar.polo datatype, &value)
1909 c758d8da 2019-10-18 omar.polo == 1)
1910 4c0d79a0 2018-10-09 omar.polo bgs[1] = parse_color(value.addr, "#000");
1911 1d13acf5 2018-10-04 omar.polo
1912 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.padding", "*",
1913 c758d8da 2019-10-18 omar.polo datatype, &value)
1914 c758d8da 2019-10-18 omar.polo == 1) {
1915 df29e05e 2018-10-17 omar.polo char **paddings;
1916 df29e05e 2018-10-17 omar.polo paddings = parse_csslike(value.addr);
1917 df29e05e 2018-10-17 omar.polo if (paddings != NULL)
1918 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
1919 c758d8da 2019-10-18 omar.polo r.c_padding[i] = parse_integer(
1920 c758d8da 2019-10-18 omar.polo paddings[i], 0);
1921 df29e05e 2018-10-17 omar.polo else
1922 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1923 c758d8da 2019-10-18 omar.polo "Error while parsing "
1924 c758d8da 2019-10-18 omar.polo "MyMenu.completion.padding");
1925 df29e05e 2018-10-17 omar.polo }
1926 df29e05e 2018-10-17 omar.polo
1927 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.border.size", "*",
1928 c758d8da 2019-10-18 omar.polo datatype, &value)
1929 c758d8da 2019-10-18 omar.polo == 1) {
1930 df29e05e 2018-10-17 omar.polo char **sizes;
1931 df29e05e 2018-10-17 omar.polo sizes = parse_csslike(value.addr);
1932 df29e05e 2018-10-17 omar.polo if (sizes != NULL)
1933 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
1934 c758d8da 2019-10-18 omar.polo r.c_borders[i]
1935 c758d8da 2019-10-18 omar.polo = parse_integer(sizes[i], 0);
1936 df29e05e 2018-10-17 omar.polo else
1937 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1938 c758d8da 2019-10-18 omar.polo "Error while parsing "
1939 c758d8da 2019-10-18 omar.polo "MyMenu.completion.border.size");
1940 df29e05e 2018-10-17 omar.polo }
1941 df29e05e 2018-10-17 omar.polo
1942 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.border.color", "*",
1943 c758d8da 2019-10-18 omar.polo datatype, &value)
1944 c758d8da 2019-10-18 omar.polo == 1) {
1945 df29e05e 2018-10-17 omar.polo char **sizes;
1946 df29e05e 2018-10-17 omar.polo sizes = parse_csslike(value.addr);
1947 df29e05e 2018-10-17 omar.polo if (sizes != NULL)
1948 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
1949 c758d8da 2019-10-18 omar.polo c_borders_bg[i] = parse_color(
1950 c758d8da 2019-10-18 omar.polo sizes[i], "#000");
1951 df29e05e 2018-10-17 omar.polo else
1952 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1953 c758d8da 2019-10-18 omar.polo "Error while parsing "
1954 c758d8da 2019-10-18 omar.polo "MyMenu.completion.border.color");
1955 df29e05e 2018-10-17 omar.polo }
1956 df29e05e 2018-10-17 omar.polo
1957 1d13acf5 2018-10-04 omar.polo /* Completion Highlighted */
1958 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb,
1959 c758d8da 2019-10-18 omar.polo "MyMenu.completion_highlighted.foreground", "*",
1960 c758d8da 2019-10-18 omar.polo datatype, &value)
1961 c758d8da 2019-10-18 omar.polo == 1)
1962 4c0d79a0 2018-10-09 omar.polo fgs[2] = parse_color(value.addr, "#000");
1963 1d13acf5 2018-10-04 omar.polo
1964 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb,
1965 c758d8da 2019-10-18 omar.polo "MyMenu.completion_highlighted.background", "*",
1966 c758d8da 2019-10-18 omar.polo datatype, &value)
1967 c758d8da 2019-10-18 omar.polo == 1)
1968 4c0d79a0 2018-10-09 omar.polo bgs[2] = parse_color(value.addr, "#fff");
1969 1d13acf5 2018-10-04 omar.polo
1970 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb,
1971 c758d8da 2019-10-18 omar.polo "MyMenu.completion_highlighted.padding", "*",
1972 c758d8da 2019-10-18 omar.polo datatype, &value)
1973 c758d8da 2019-10-18 omar.polo == 1) {
1974 df29e05e 2018-10-17 omar.polo char **paddings;
1975 df29e05e 2018-10-17 omar.polo paddings = parse_csslike(value.addr);
1976 df29e05e 2018-10-17 omar.polo if (paddings != NULL)
1977 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
1978 c758d8da 2019-10-18 omar.polo r.ch_padding[i] = parse_integer(
1979 c758d8da 2019-10-18 omar.polo paddings[i], 0);
1980 df29e05e 2018-10-17 omar.polo else
1981 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1982 c758d8da 2019-10-18 omar.polo "Error while parsing "
1983 c758d8da 2019-10-18 omar.polo "MyMenu.completion_highlighted."
1984 c758d8da 2019-10-18 omar.polo "padding");
1985 df29e05e 2018-10-17 omar.polo }
1986 df29e05e 2018-10-17 omar.polo
1987 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb,
1988 c758d8da 2019-10-18 omar.polo "MyMenu.completion_highlighted.border.size", "*",
1989 c758d8da 2019-10-18 omar.polo datatype, &value)
1990 c758d8da 2019-10-18 omar.polo == 1) {
1991 df29e05e 2018-10-17 omar.polo char **sizes;
1992 df29e05e 2018-10-17 omar.polo sizes = parse_csslike(value.addr);
1993 df29e05e 2018-10-17 omar.polo if (sizes != NULL)
1994 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
1995 c758d8da 2019-10-18 omar.polo r.ch_borders[i]
1996 c758d8da 2019-10-18 omar.polo = parse_integer(sizes[i], 0);
1997 df29e05e 2018-10-17 omar.polo else
1998 c758d8da 2019-10-18 omar.polo fprintf(stderr,
1999 c758d8da 2019-10-18 omar.polo "Error while parsing "
2000 c758d8da 2019-10-18 omar.polo "MyMenu.completion_highlighted."
2001 c758d8da 2019-10-18 omar.polo "border.size");
2002 df29e05e 2018-10-17 omar.polo }
2003 df29e05e 2018-10-17 omar.polo
2004 c758d8da 2019-10-18 omar.polo if (XrmGetResource(xdb,
2005 c758d8da 2019-10-18 omar.polo "MyMenu.completion_highlighted.border.color", "*",
2006 c758d8da 2019-10-18 omar.polo datatype, &value)
2007 c758d8da 2019-10-18 omar.polo == 1) {
2008 df29e05e 2018-10-17 omar.polo char **colors;
2009 df29e05e 2018-10-17 omar.polo colors = parse_csslike(value.addr);
2010 df29e05e 2018-10-17 omar.polo if (colors != NULL)
2011 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2012 c758d8da 2019-10-18 omar.polo ch_borders_bg[i] = parse_color(
2013 c758d8da 2019-10-18 omar.polo colors[i], "#000");
2014 df29e05e 2018-10-17 omar.polo else
2015 c758d8da 2019-10-18 omar.polo fprintf(stderr,
2016 c758d8da 2019-10-18 omar.polo "Error while parsing "
2017 c758d8da 2019-10-18 omar.polo "MyMenu.completion_highlighted."
2018 c758d8da 2019-10-18 omar.polo "border.color");
2019 df29e05e 2018-10-17 omar.polo }
2020 df29e05e 2018-10-17 omar.polo
2021 26b541d8 2018-10-04 omar.polo /* Border */
2022 c758d8da 2019-10-18 omar.polo if (XrmGetResource(
2023 c758d8da 2019-10-18 omar.polo xdb, "MyMenu.border.color", "*", datatype, &value)
2024 c758d8da 2019-10-18 omar.polo == 1) {
2025 1d13acf5 2018-10-04 omar.polo char **colors;
2026 1d13acf5 2018-10-04 omar.polo colors = parse_csslike(value.addr);
2027 df29e05e 2018-10-17 omar.polo if (colors != NULL)
2028 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2029 c758d8da 2019-10-18 omar.polo borders_bg[i] = parse_color(
2030 c758d8da 2019-10-18 omar.polo colors[i], "#000");
2031 df29e05e 2018-10-17 omar.polo else
2032 c758d8da 2019-10-18 omar.polo fprintf(stderr,
2033 c758d8da 2019-10-18 omar.polo "error while parsing "
2034 c758d8da 2019-10-18 omar.polo "MyMenu.border.color\n");
2035 1d13acf5 2018-10-04 omar.polo }
2036 1d13acf5 2018-10-04 omar.polo }
2037 1d13acf5 2018-10-04 omar.polo
2038 26b541d8 2018-10-04 omar.polo /* Second round of args parsing */
2039 c758d8da 2019-10-18 omar.polo optind = 0; /* reset the option index */
2040 1d13acf5 2018-10-04 omar.polo while ((ch = getopt(argc, argv, ARGS)) != -1) {
2041 1d13acf5 2018-10-04 omar.polo switch (ch) {
2042 1d13acf5 2018-10-04 omar.polo case 'a':
2043 4f769efa 2018-10-06 omar.polo r.first_selected = 1;
2044 1d13acf5 2018-10-04 omar.polo break;
2045 1d13acf5 2018-10-04 omar.polo case 'A':
2046 1d13acf5 2018-10-04 omar.polo /* free_text -- already catched */
2047 1d13acf5 2018-10-04 omar.polo case 'd':
2048 1d13acf5 2018-10-04 omar.polo /* separator -- this case was already catched */
2049 1d13acf5 2018-10-04 omar.polo case 'e':
2050 1d13acf5 2018-10-04 omar.polo /* embedding mymenu this case was already catched. */
2051 1d13acf5 2018-10-04 omar.polo case 'm':
2052 c758d8da 2019-10-18 omar.polo /* multiple selection this case was already catched.
2053 c758d8da 2019-10-18 omar.polo */
2054 1d13acf5 2018-10-04 omar.polo break;
2055 1d13acf5 2018-10-04 omar.polo case 'p': {
2056 1d13acf5 2018-10-04 omar.polo char *newprompt;
2057 1d13acf5 2018-10-04 omar.polo newprompt = strdup(optarg);
2058 1d13acf5 2018-10-04 omar.polo if (newprompt != NULL) {
2059 4f769efa 2018-10-06 omar.polo free(r.ps1);
2060 4f769efa 2018-10-06 omar.polo r.ps1 = newprompt;
2061 1d13acf5 2018-10-04 omar.polo }
2062 1d13acf5 2018-10-04 omar.polo break;
2063 1d13acf5 2018-10-04 omar.polo }
2064 1d13acf5 2018-10-04 omar.polo case 'x':
2065 4f769efa 2018-10-06 omar.polo x = parse_int_with_pos(optarg, x, d_width, r.width);
2066 1d13acf5 2018-10-04 omar.polo break;
2067 1d13acf5 2018-10-04 omar.polo case 'y':
2068 4f769efa 2018-10-06 omar.polo y = parse_int_with_pos(optarg, y, d_height, r.height);
2069 1d13acf5 2018-10-04 omar.polo break;
2070 df29e05e 2018-10-17 omar.polo case 'P': {
2071 df29e05e 2018-10-17 omar.polo char **paddings;
2072 df29e05e 2018-10-17 omar.polo if ((paddings = parse_csslike(optarg)) != NULL)
2073 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2074 c758d8da 2019-10-18 omar.polo r.p_padding[i] = parse_integer(
2075 c758d8da 2019-10-18 omar.polo paddings[i], 0);
2076 1d13acf5 2018-10-04 omar.polo break;
2077 df29e05e 2018-10-17 omar.polo }
2078 df29e05e 2018-10-17 omar.polo case 'G': {
2079 df29e05e 2018-10-17 omar.polo char **colors;
2080 df29e05e 2018-10-17 omar.polo if ((colors = parse_csslike(optarg)) != NULL)
2081 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2082 c758d8da 2019-10-18 omar.polo p_borders_bg[i] = parse_color(
2083 c758d8da 2019-10-18 omar.polo colors[i], "#000");
2084 df29e05e 2018-10-17 omar.polo break;
2085 df29e05e 2018-10-17 omar.polo }
2086 df29e05e 2018-10-17 omar.polo case 'g': {
2087 df29e05e 2018-10-17 omar.polo char **sizes;
2088 df29e05e 2018-10-17 omar.polo if ((sizes = parse_csslike(optarg)) != NULL)
2089 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2090 c758d8da 2019-10-18 omar.polo r.p_borders[i]
2091 c758d8da 2019-10-18 omar.polo = parse_integer(sizes[i], 0);
2092 df29e05e 2018-10-17 omar.polo break;
2093 df29e05e 2018-10-17 omar.polo }
2094 df29e05e 2018-10-17 omar.polo case 'I': {
2095 df29e05e 2018-10-17 omar.polo char **colors;
2096 df29e05e 2018-10-17 omar.polo if ((colors = parse_csslike(optarg)) != NULL)
2097 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2098 c758d8da 2019-10-18 omar.polo c_borders_bg[i] = parse_color(
2099 c758d8da 2019-10-18 omar.polo colors[i], "#000");
2100 df29e05e 2018-10-17 omar.polo break;
2101 df29e05e 2018-10-17 omar.polo }
2102 df29e05e 2018-10-17 omar.polo case 'i': {
2103 df29e05e 2018-10-17 omar.polo char **sizes;
2104 df29e05e 2018-10-17 omar.polo if ((sizes = parse_csslike(optarg)) != NULL)
2105 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2106 c758d8da 2019-10-18 omar.polo r.c_borders[i]
2107 c758d8da 2019-10-18 omar.polo = parse_integer(sizes[i], 0);
2108 df29e05e 2018-10-17 omar.polo break;
2109 df29e05e 2018-10-17 omar.polo }
2110 df29e05e 2018-10-17 omar.polo case 'J': {
2111 df29e05e 2018-10-17 omar.polo char **colors;
2112 df29e05e 2018-10-17 omar.polo if ((colors = parse_csslike(optarg)) != NULL)
2113 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2114 c758d8da 2019-10-18 omar.polo ch_borders_bg[i] = parse_color(
2115 c758d8da 2019-10-18 omar.polo colors[i], "#000");
2116 df29e05e 2018-10-17 omar.polo break;
2117 df29e05e 2018-10-17 omar.polo }
2118 df29e05e 2018-10-17 omar.polo case 'j': {
2119 df29e05e 2018-10-17 omar.polo char **sizes;
2120 df29e05e 2018-10-17 omar.polo if ((sizes = parse_csslike(optarg)) != NULL)
2121 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2122 c758d8da 2019-10-18 omar.polo r.ch_borders[i]
2123 c758d8da 2019-10-18 omar.polo = parse_integer(sizes[i], 0);
2124 df29e05e 2018-10-17 omar.polo break;
2125 df29e05e 2018-10-17 omar.polo }
2126 1d13acf5 2018-10-04 omar.polo case 'l':
2127 4f769efa 2018-10-06 omar.polo r.horizontal_layout = !strcmp(optarg, "horizontal");
2128 1d13acf5 2018-10-04 omar.polo break;
2129 1d13acf5 2018-10-04 omar.polo case 'f': {
2130 1d13acf5 2018-10-04 omar.polo char *newfont;
2131 1d13acf5 2018-10-04 omar.polo if ((newfont = strdup(optarg)) != NULL) {
2132 1d13acf5 2018-10-04 omar.polo free(fontname);
2133 1d13acf5 2018-10-04 omar.polo fontname = newfont;
2134 1d13acf5 2018-10-04 omar.polo }
2135 1d13acf5 2018-10-04 omar.polo break;
2136 1d13acf5 2018-10-04 omar.polo }
2137 1d13acf5 2018-10-04 omar.polo case 'W':
2138 c758d8da 2019-10-18 omar.polo r.width = parse_int_with_percentage(
2139 c758d8da 2019-10-18 omar.polo optarg, r.width, d_width);
2140 1d13acf5 2018-10-04 omar.polo break;
2141 1d13acf5 2018-10-04 omar.polo case 'H':
2142 c758d8da 2019-10-18 omar.polo r.height = parse_int_with_percentage(
2143 c758d8da 2019-10-18 omar.polo optarg, r.height, d_height);
2144 1d13acf5 2018-10-04 omar.polo break;
2145 1d13acf5 2018-10-04 omar.polo case 'b': {
2146 1d13acf5 2018-10-04 omar.polo char **borders;
2147 1d13acf5 2018-10-04 omar.polo if ((borders = parse_csslike(optarg)) != NULL) {
2148 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2149 c758d8da 2019-10-18 omar.polo r.borders[i] = parse_integer(
2150 c758d8da 2019-10-18 omar.polo borders[i], 0);
2151 1d13acf5 2018-10-04 omar.polo } else
2152 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Error parsing b option\n");
2153 1d13acf5 2018-10-04 omar.polo break;
2154 1d13acf5 2018-10-04 omar.polo }
2155 1d13acf5 2018-10-04 omar.polo case 'B': {
2156 1d13acf5 2018-10-04 omar.polo char **colors;
2157 1d13acf5 2018-10-04 omar.polo if ((colors = parse_csslike(optarg)) != NULL) {
2158 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i)
2159 c758d8da 2019-10-18 omar.polo borders_bg[i] = parse_color(
2160 c758d8da 2019-10-18 omar.polo colors[i], "#000");
2161 1d13acf5 2018-10-04 omar.polo } else
2162 c758d8da 2019-10-18 omar.polo fprintf(stderr,
2163 c758d8da 2019-10-18 omar.polo "error while parsing B option\n");
2164 1d13acf5 2018-10-04 omar.polo break;
2165 1d13acf5 2018-10-04 omar.polo }
2166 4f769efa 2018-10-06 omar.polo case 't':
2167 4c0d79a0 2018-10-09 omar.polo fgs[0] = parse_color(optarg, NULL);
2168 1d13acf5 2018-10-04 omar.polo break;
2169 4f769efa 2018-10-06 omar.polo case 'T':
2170 4c0d79a0 2018-10-09 omar.polo bgs[0] = parse_color(optarg, NULL);
2171 1d13acf5 2018-10-04 omar.polo break;
2172 4f769efa 2018-10-06 omar.polo case 'c':
2173 4c0d79a0 2018-10-09 omar.polo fgs[1] = parse_color(optarg, NULL);
2174 1d13acf5 2018-10-04 omar.polo break;
2175 4f769efa 2018-10-06 omar.polo case 'C':
2176 4c0d79a0 2018-10-09 omar.polo bgs[1] = parse_color(optarg, NULL);
2177 1d13acf5 2018-10-04 omar.polo break;
2178 4f769efa 2018-10-06 omar.polo case 's':
2179 4c0d79a0 2018-10-09 omar.polo fgs[2] = parse_color(optarg, NULL);
2180 1d13acf5 2018-10-04 omar.polo break;
2181 4f769efa 2018-10-06 omar.polo case 'S':
2182 4c0d79a0 2018-10-09 omar.polo fgs[2] = parse_color(optarg, NULL);
2183 1d13acf5 2018-10-04 omar.polo break;
2184 1d13acf5 2018-10-04 omar.polo default:
2185 1d13acf5 2018-10-04 omar.polo fprintf(stderr, "Unrecognized option %c\n", ch);
2186 1d13acf5 2018-10-04 omar.polo status = ERR;
2187 1d13acf5 2018-10-04 omar.polo break;
2188 1d13acf5 2018-10-04 omar.polo }
2189 1d13acf5 2018-10-04 omar.polo }
2190 1d13acf5 2018-10-04 omar.polo
2191 df29e05e 2018-10-17 omar.polo if (r.height < 0 || r.width < 0 || x < 0 || y < 0) {
2192 df29e05e 2018-10-17 omar.polo fprintf(stderr, "height, width, x or y are lesser than 0.");
2193 df29e05e 2018-10-17 omar.polo status = ERR;
2194 df29e05e 2018-10-17 omar.polo }
2195 df29e05e 2018-10-17 omar.polo
2196 1d13acf5 2018-10-04 omar.polo /* since only now we know if the first should be selected,
2197 1d13acf5 2018-10-04 omar.polo * update the completion here */
2198 4f769efa 2018-10-06 omar.polo update_completions(cs, text, lines, vlines, r.first_selected);
2199 1d13acf5 2018-10-04 omar.polo
2200 c758d8da 2019-10-18 omar.polo /* update the prompt lenght, only now we surely know the length of it
2201 c758d8da 2019-10-18 omar.polo */
2202 4f769efa 2018-10-06 omar.polo r.ps1len = strlen(r.ps1);
2203 4f769efa 2018-10-06 omar.polo
2204 26b541d8 2018-10-04 omar.polo /* Create the window */
2205 c758d8da 2019-10-18 omar.polo create_window(&r, parent_window, cmap, vinfo, x, y, offset_x,
2206 c758d8da 2019-10-18 omar.polo offset_y, bgs[1]);
2207 4f769efa 2018-10-06 omar.polo set_win_atoms_hints(r.d, r.w, r.width, r.height);
2208 4f769efa 2018-10-06 omar.polo XMapRaised(r.d, r.w);
2209 1d13acf5 2018-10-04 omar.polo
2210 1d13acf5 2018-10-04 omar.polo /* If embed, listen for other events as well */
2211 1d13acf5 2018-10-04 omar.polo if (embed) {
2212 c758d8da 2019-10-18 omar.polo Window *children, parent, root;
2213 c758d8da 2019-10-18 omar.polo unsigned int children_no;
2214 1d13acf5 2018-10-04 omar.polo
2215 4f769efa 2018-10-06 omar.polo XSelectInput(r.d, parent_window, FocusChangeMask);
2216 c758d8da 2019-10-18 omar.polo if (XQueryTree(r.d, parent_window, &root, &parent, &children,
2217 c758d8da 2019-10-18 omar.polo &children_no)
2218 c758d8da 2019-10-18 omar.polo && children) {
2219 c758d8da 2019-10-18 omar.polo for (i = 0; i < children_no && children[i] != r.w;
2220 c758d8da 2019-10-18 omar.polo ++i)
2221 c758d8da 2019-10-18 omar.polo XSelectInput(
2222 c758d8da 2019-10-18 omar.polo r.d, children[i], FocusChangeMask);
2223 1d13acf5 2018-10-04 omar.polo XFree(children);
2224 1d13acf5 2018-10-04 omar.polo }
2225 4f769efa 2018-10-06 omar.polo grabfocus(r.d, r.w);
2226 1d13acf5 2018-10-04 omar.polo }
2227 1d13acf5 2018-10-04 omar.polo
2228 4f769efa 2018-10-06 omar.polo take_keyboard(r.d, r.w);
2229 1d13acf5 2018-10-04 omar.polo
2230 4c0d79a0 2018-10-09 omar.polo r.x_zero = r.borders[3];
2231 4c0d79a0 2018-10-09 omar.polo r.y_zero = r.borders[0];
2232 1d13acf5 2018-10-04 omar.polo
2233 4f769efa 2018-10-06 omar.polo {
2234 c758d8da 2019-10-18 omar.polo XGCValues values;
2235 4f769efa 2018-10-06 omar.polo
2236 df29e05e 2018-10-17 omar.polo for (i = 0; i < 3; ++i) {
2237 df29e05e 2018-10-17 omar.polo r.fgs[i] = XCreateGC(r.d, r.w, 0, &values);
2238 df29e05e 2018-10-17 omar.polo r.bgs[i] = XCreateGC(r.d, r.w, 0, &values);
2239 df29e05e 2018-10-17 omar.polo }
2240 df29e05e 2018-10-17 omar.polo
2241 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i) {
2242 df29e05e 2018-10-17 omar.polo r.borders_bg[i] = XCreateGC(r.d, r.w, 0, &values);
2243 df29e05e 2018-10-17 omar.polo r.p_borders_bg[i] = XCreateGC(r.d, r.w, 0, &values);
2244 df29e05e 2018-10-17 omar.polo r.c_borders_bg[i] = XCreateGC(r.d, r.w, 0, &values);
2245 df29e05e 2018-10-17 omar.polo r.ch_borders_bg[i] = XCreateGC(r.d, r.w, 0, &values);
2246 df29e05e 2018-10-17 omar.polo }
2247 4f769efa 2018-10-06 omar.polo }
2248 4f769efa 2018-10-06 omar.polo
2249 df29e05e 2018-10-17 omar.polo /* Load the colors in our GCs */
2250 df29e05e 2018-10-17 omar.polo for (i = 0; i < 3; ++i) {
2251 df29e05e 2018-10-17 omar.polo XSetForeground(r.d, r.fgs[i], fgs[i]);
2252 df29e05e 2018-10-17 omar.polo XSetForeground(r.d, r.bgs[i], bgs[i]);
2253 df29e05e 2018-10-17 omar.polo }
2254 df29e05e 2018-10-17 omar.polo
2255 df29e05e 2018-10-17 omar.polo for (i = 0; i < 4; ++i) {
2256 df29e05e 2018-10-17 omar.polo XSetForeground(r.d, r.borders_bg[i], borders_bg[i]);
2257 df29e05e 2018-10-17 omar.polo XSetForeground(r.d, r.p_borders_bg[i], p_borders_bg[i]);
2258 df29e05e 2018-10-17 omar.polo XSetForeground(r.d, r.c_borders_bg[i], c_borders_bg[i]);
2259 df29e05e 2018-10-17 omar.polo XSetForeground(r.d, r.ch_borders_bg[i], ch_borders_bg[i]);
2260 df29e05e 2018-10-17 omar.polo }
2261 df29e05e 2018-10-17 omar.polo
2262 1d13acf5 2018-10-04 omar.polo if (load_font(&r, fontname) == -1)
2263 1d13acf5 2018-10-04 omar.polo status = ERR;
2264 1d13acf5 2018-10-04 omar.polo
2265 1d13acf5 2018-10-04 omar.polo #ifdef USE_XFT
2266 df29e05e 2018-10-17 omar.polo r.xftdraw = XftDrawCreate(r.d, r.w, vinfo.visual, cmap);
2267 1d13acf5 2018-10-04 omar.polo
2268 3d95a42e 2018-10-19 omar.polo for (i = 0; i < 3; ++i) {
2269 c758d8da 2019-10-18 omar.polo rgba_t c;
2270 c758d8da 2019-10-18 omar.polo XRenderColor xrcolor;
2271 1d13acf5 2018-10-04 omar.polo
2272 c758d8da 2019-10-18 omar.polo c = *(rgba_t *)&fgs[i];
2273 c758d8da 2019-10-18 omar.polo xrcolor.red = EXPANDBITS(c.rgba.r);
2274 c758d8da 2019-10-18 omar.polo xrcolor.green = EXPANDBITS(c.rgba.g);
2275 c758d8da 2019-10-18 omar.polo xrcolor.blue = EXPANDBITS(c.rgba.b);
2276 c758d8da 2019-10-18 omar.polo xrcolor.alpha = EXPANDBITS(c.rgba.a);
2277 c758d8da 2019-10-18 omar.polo XftColorAllocValue(
2278 c758d8da 2019-10-18 omar.polo r.d, vinfo.visual, cmap, &xrcolor, &r.xft_colors[i]);
2279 1d13acf5 2018-10-04 omar.polo }
2280 c9a3bfaa 2018-05-21 omar.polo #endif
2281 7ca8829b 2018-05-21 omar.polo
2282 aed48307 2018-10-06 omar.polo /* compute prompt dimensions */
2283 aed48307 2018-10-06 omar.polo ps1extents(&r);
2284 347d23da 2018-05-19 omar.polo
2285 4f769efa 2018-10-06 omar.polo xim_init(&r, &xdb);
2286 f5e234d6 2018-05-18 omar.polo
2287 aed48307 2018-10-06 omar.polo #ifdef __OpenBSD__
2288 aed48307 2018-10-06 omar.polo /* Now we need only the ability to write */
2289 aed48307 2018-10-06 omar.polo pledge("stdio", "");
2290 aed48307 2018-10-06 omar.polo #endif
2291 aed48307 2018-10-06 omar.polo
2292 df29e05e 2018-10-17 omar.polo /* Cache text height */
2293 df29e05e 2018-10-17 omar.polo text_extents("fyjpgl", 6, &r, NULL, &r.text_height);
2294 df29e05e 2018-10-17 omar.polo
2295 5e448381 2018-10-09 omar.polo /* Draw the window for the first time */
2296 5e448381 2018-10-09 omar.polo draw(&r, text, cs);
2297 5e448381 2018-10-09 omar.polo
2298 1d13acf5 2018-10-04 omar.polo /* Main loop */
2299 1d13acf5 2018-10-04 omar.polo while (status == LOOPING || status == OK_LOOP) {
2300 1d13acf5 2018-10-04 omar.polo status = loop(&r, &text, &textlen, cs, lines, vlines);
2301 f5e234d6 2018-05-18 omar.polo
2302 1d13acf5 2018-10-04 omar.polo if (status != ERR)
2303 1d13acf5 2018-10-04 omar.polo printf("%s\n", text);
2304 844addbb 2018-07-15 omar.polo
2305 4f769efa 2018-10-06 omar.polo if (!r.multiple_select && status == OK_LOOP)
2306 1d13acf5 2018-10-04 omar.polo status = OK;
2307 1d13acf5 2018-10-04 omar.polo }
2308 f5e234d6 2018-05-18 omar.polo
2309 4f769efa 2018-10-06 omar.polo XUngrabKeyboard(r.d, CurrentTime);
2310 43e30577 2018-10-20 omar.polo
2311 43e30577 2018-10-20 omar.polo #ifdef USE_XFT
2312 43e30577 2018-10-20 omar.polo for (i = 0; i < 3; ++i)
2313 43e30577 2018-10-20 omar.polo XftColorFree(r.d, vinfo.visual, cmap, &r.xft_colors[i]);
2314 43e30577 2018-10-20 omar.polo #endif
2315 43e30577 2018-10-20 omar.polo
2316 43e30577 2018-10-20 omar.polo for (i = 0; i < 3; ++i) {
2317 43e30577 2018-10-20 omar.polo XFreeGC(r.d, r.fgs[i]);
2318 43e30577 2018-10-20 omar.polo XFreeGC(r.d, r.bgs[i]);
2319 43e30577 2018-10-20 omar.polo }
2320 b5d751bd 2018-07-07 omar.polo
2321 43e30577 2018-10-20 omar.polo for (i = 0; i < 4; ++i) {
2322 43e30577 2018-10-20 omar.polo XFreeGC(r.d, r.borders_bg[i]);
2323 43e30577 2018-10-20 omar.polo XFreeGC(r.d, r.p_borders_bg[i]);
2324 43e30577 2018-10-20 omar.polo XFreeGC(r.d, r.c_borders_bg[i]);
2325 43e30577 2018-10-20 omar.polo XFreeGC(r.d, r.ch_borders_bg[i]);
2326 43e30577 2018-10-20 omar.polo }
2327 43e30577 2018-10-20 omar.polo
2328 43e30577 2018-10-20 omar.polo XDestroyIC(r.xic);
2329 43e30577 2018-10-20 omar.polo XCloseIM(r.xim);
2330 43e30577 2018-10-20 omar.polo
2331 b5d751bd 2018-07-07 omar.polo #ifdef USE_XFT
2332 043d71ef 2018-10-19 omar.polo for (i = 0; i < 3; ++i)
2333 043d71ef 2018-10-19 omar.polo XftColorFree(r.d, vinfo.visual, cmap, &r.xft_colors[i]);
2334 43e30577 2018-10-20 omar.polo XftFontClose(r.d, r.font);
2335 43e30577 2018-10-20 omar.polo XftDrawDestroy(r.xftdraw);
2336 43e30577 2018-10-20 omar.polo #else
2337 43e30577 2018-10-20 omar.polo XFreeFontSet(r.d, r.font);
2338 b5d751bd 2018-07-07 omar.polo #endif
2339 b5d751bd 2018-07-07 omar.polo
2340 4f769efa 2018-10-06 omar.polo free(r.ps1);
2341 1d13acf5 2018-10-04 omar.polo free(fontname);
2342 1d13acf5 2018-10-04 omar.polo free(text);
2343 b5d751bd 2018-07-07 omar.polo
2344 1d13acf5 2018-10-04 omar.polo free(buf);
2345 1d13acf5 2018-10-04 omar.polo free(lines);
2346 1d13acf5 2018-10-04 omar.polo free(vlines);
2347 1d13acf5 2018-10-04 omar.polo compls_delete(cs);
2348 b5d751bd 2018-07-07 omar.polo
2349 43e30577 2018-10-20 omar.polo XFreeColormap(r.d, cmap);
2350 43e30577 2018-10-20 omar.polo
2351 1d13acf5 2018-10-04 omar.polo XDestroyWindow(r.d, r.w);
2352 1d13acf5 2018-10-04 omar.polo XCloseDisplay(r.d);
2353 b5d751bd 2018-07-07 omar.polo
2354 1d13acf5 2018-10-04 omar.polo return status != OK;
2355 f5e234d6 2018-05-18 omar.polo }