49 | 49 |
typedef struct {
|
50 | 50 |
i3String *label;
|
51 | 51 |
char *action;
|
52 | |
bool exec_in_terminal;
|
53 | 52 |
int16_t x;
|
54 | 53 |
uint16_t width;
|
55 | 54 |
} button_t;
|
|
123 | 122 |
wait(0);
|
124 | 123 |
}
|
125 | 124 |
|
126 | |
static void execute_in_terminal(const char *command) {
|
|
125 |
static button_t *get_button_at(int16_t x, int16_t y) {
|
|
126 |
for (int c = 0; c < buttoncnt; c++)
|
|
127 |
if (x >= (buttons[c].x) && x <= (buttons[c].x + buttons[c].width))
|
|
128 |
return &buttons[c];
|
|
129 |
|
|
130 |
return NULL;
|
|
131 |
}
|
|
132 |
|
|
133 |
static void handle_button_press(xcb_connection_t *conn, xcb_button_press_event_t *event) {
|
|
134 |
printf("button pressed on x = %d, y = %d\n",
|
|
135 |
event->event_x, event->event_y);
|
|
136 |
/* TODO: set a flag for the button, re-render */
|
|
137 |
}
|
|
138 |
|
|
139 |
/*
|
|
140 |
* Called when the user releases the mouse button. Checks whether the
|
|
141 |
* coordinates are over a button and executes the appropriate action.
|
|
142 |
*
|
|
143 |
*/
|
|
144 |
static void handle_button_release(xcb_connection_t *conn, xcb_button_release_event_t *event) {
|
|
145 |
printf("button released on x = %d, y = %d\n",
|
|
146 |
event->event_x, event->event_y);
|
|
147 |
/* If the user hits the close button, we exit(0) */
|
|
148 |
if (event->event_x >= btn_close.x && event->event_x < btn_close.x + btn_close.width)
|
|
149 |
exit(0);
|
|
150 |
button_t *button = get_button_at(event->event_x, event->event_y);
|
|
151 |
if (!button)
|
|
152 |
return;
|
|
153 |
|
127 | 154 |
/* We need to create a custom script containing our actual command
|
128 | 155 |
* since not every terminal emulator which is contained in
|
129 | 156 |
* i3-sensible-terminal supports -e with multiple arguments (and not
|
|
144 | 171 |
warn("Could not fdopen() temporary script to store the nagbar command");
|
145 | 172 |
return;
|
146 | 173 |
}
|
147 | |
fprintf(script, "#!/bin/sh\nrm %s\n%s", script_path, command);
|
|
174 |
fprintf(script, "#!/bin/sh\nrm %s\n%s", script_path, button->action);
|
148 | 175 |
/* Also closes fd */
|
149 | 176 |
fclose(script);
|
150 | 177 |
|
|
166 | 193 |
free(terminal_cmd);
|
167 | 194 |
free(script_path);
|
168 | 195 |
free(exe_path);
|
169 | |
}
|
170 | |
|
171 | |
static button_t *get_button_at(int16_t x, int16_t y) {
|
172 | |
for (int c = 0; c < buttoncnt; c++)
|
173 | |
if (x >= (buttons[c].x) && x <= (buttons[c].x + buttons[c].width))
|
174 | |
return &buttons[c];
|
175 | |
|
176 | |
return NULL;
|
177 | |
}
|
178 | |
|
179 | |
static void handle_button_press(xcb_connection_t *conn, xcb_button_press_event_t *event) {
|
180 | |
printf("button pressed on x = %d, y = %d\n",
|
181 | |
event->event_x, event->event_y);
|
182 | |
/* TODO: set a flag for the button, re-render */
|
183 | |
}
|
184 | |
|
185 | |
/*
|
186 | |
* Called when the user releases the mouse button. Checks whether the
|
187 | |
* coordinates are over a button and executes the appropriate action.
|
188 | |
*
|
189 | |
*/
|
190 | |
static void handle_button_release(xcb_connection_t *conn, xcb_button_release_event_t *event) {
|
191 | |
printf("button released on x = %d, y = %d\n",
|
192 | |
event->event_x, event->event_y);
|
193 | |
/* If the user hits the close button, we exit(0) */
|
194 | |
if (event->event_x >= btn_close.x && event->event_x < btn_close.x + btn_close.width)
|
195 | |
exit(0);
|
196 | |
button_t *button = get_button_at(event->event_x, event->event_y);
|
197 | |
if (!button) {
|
198 | |
return;
|
199 | |
}
|
200 | |
|
201 | |
if (button->exec_in_terminal) {
|
202 | |
execute_in_terminal(button->action);
|
203 | |
} else {
|
204 | |
start_application(button->action);
|
205 | |
}
|
206 | 196 |
|
207 | 197 |
/* TODO: unset flag, re-render */
|
208 | 198 |
}
|
|
367 | 357 |
{"version", no_argument, 0, 'v'},
|
368 | 358 |
{"font", required_argument, 0, 'f'},
|
369 | 359 |
{"button", required_argument, 0, 'b'},
|
370 | |
{"button-sh", required_argument, 0, 'B'},
|
371 | 360 |
{"help", no_argument, 0, 'h'},
|
372 | 361 |
{"message", required_argument, 0, 'm'},
|
373 | 362 |
{"type", required_argument, 0, 't'},
|
374 | 363 |
{0, 0, 0, 0}};
|
375 | 364 |
|
376 | |
char *options_string = "B:b:f:m:t:vh";
|
|
365 |
char *options_string = "b:f:m:t:vh";
|
377 | 366 |
|
378 | 367 |
prompt = i3string_from_utf8("Please do not run this program.");
|
379 | 368 |
|
|
395 | 384 |
break;
|
396 | 385 |
case 'h':
|
397 | 386 |
printf("i3-nagbar " I3_VERSION "\n");
|
398 | |
printf("i3-nagbar [-m <message>] [-b <button> <terminal-action>] "
|
399 | |
"[-B <button> <shell-action> [-t warning|error] [-f <font>] [-v]\n");
|
|
387 |
printf("i3-nagbar [-m <message>] [-b <button> <action>] [-t warning|error] [-f <font>] [-v]\n");
|
400 | 388 |
return 0;
|
401 | |
/* falls through */
|
402 | |
case 'B':
|
403 | 389 |
case 'b':
|
404 | 390 |
buttons = srealloc(buttons, sizeof(button_t) * (buttoncnt + 1));
|
405 | 391 |
buttons[buttoncnt].label = i3string_from_utf8(optarg);
|
406 | 392 |
buttons[buttoncnt].action = argv[optind];
|
407 | |
if (o == 'b') {
|
408 | |
buttons[buttoncnt].exec_in_terminal = true;
|
409 | |
printf("button with label *%s* and terminal action *%s*\n",
|
410 | |
i3string_as_utf8(buttons[buttoncnt].label),
|
411 | |
buttons[buttoncnt].action);
|
412 | |
} else {
|
413 | |
buttons[buttoncnt].exec_in_terminal = false;
|
414 | |
printf("button with label *%s* and shell action *%s*\n",
|
415 | |
i3string_as_utf8(buttons[buttoncnt].label),
|
416 | |
buttons[buttoncnt].action);
|
417 | |
}
|
|
393 |
printf("button with label *%s* and action *%s*\n",
|
|
394 |
i3string_as_utf8(buttons[buttoncnt].label),
|
|
395 |
buttons[buttoncnt].action);
|
418 | 396 |
buttoncnt++;
|
419 | 397 |
printf("now %d buttons\n", buttoncnt);
|
420 | 398 |
if (optind < argc)
|