Codebase list i3-gaps / 38b777c
Revert "i3-nagbar: add button flag to execute action with /bin/sh directly" (#2893) Michael Stapelberg authored 6 years ago GitHub committed 6 years ago
2 changed file(s) with 40 addition(s) and 66 deletion(s). Raw diff Collapse all Expand all
4949 typedef struct {
5050 i3String *label;
5151 char *action;
52 bool exec_in_terminal;
5352 int16_t x;
5453 uint16_t width;
5554 } button_t;
123122 wait(0);
124123 }
125124
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
127154 /* We need to create a custom script containing our actual command
128155 * since not every terminal emulator which is contained in
129156 * i3-sensible-terminal supports -e with multiple arguments (and not
144171 warn("Could not fdopen() temporary script to store the nagbar command");
145172 return;
146173 }
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);
148175 /* Also closes fd */
149176 fclose(script);
150177
166193 free(terminal_cmd);
167194 free(script_path);
168195 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 }
206196
207197 /* TODO: unset flag, re-render */
208198 }
367357 {"version", no_argument, 0, 'v'},
368358 {"font", required_argument, 0, 'f'},
369359 {"button", required_argument, 0, 'b'},
370 {"button-sh", required_argument, 0, 'B'},
371360 {"help", no_argument, 0, 'h'},
372361 {"message", required_argument, 0, 'm'},
373362 {"type", required_argument, 0, 't'},
374363 {0, 0, 0, 0}};
375364
376 char *options_string = "B:b:f:m:t:vh";
365 char *options_string = "b:f:m:t:vh";
377366
378367 prompt = i3string_from_utf8("Please do not run this program.");
379368
395384 break;
396385 case 'h':
397386 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");
400388 return 0;
401 /* falls through */
402 case 'B':
403389 case 'b':
404390 buttons = srealloc(buttons, sizeof(button_t) * (buttoncnt + 1));
405391 buttons[buttoncnt].label = i3string_from_utf8(optarg);
406392 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);
418396 buttoncnt++;
419397 printf("now %d buttons\n", buttoncnt);
420398 if (optind < argc)
88
99 == SYNOPSIS
1010
11 i3-nagbar [-m <message>] [-b <button> <terminal-action>] [-B <button> <shell-action>] [-t warning|error] [-f <font>] [-v]
11 i3-nagbar [-m <message>] [-b <button> <action>] [-t warning|error] [-f <font>] [-v]
1212
1313 == OPTIONS
1414
2828 *-f, --font* 'font'::
2929 Select font that is being used.
3030
31 *-b, --button* 'button' 'terminal-action'::
32 Adds a button labelled 'button' to the bar. When pressed, the command given
33 in 'terminal-action' is executed inside a terminal emulator, via i3-sensible-terminal(1).
34 Multiple buttons can be defined.
35
36 *-B, --button-sh* 'button' 'shell-action'::
37 Same as *--button*, except that the command given in 'shell-action' is executed directly by the shell.
31 *-b, --button* 'button' 'action'::
32 Create a button with text 'button'. The 'action' are the shell commands that
33 will be executed by this button. Multiple buttons can be defined.
3834
3935 == DESCRIPTION
4036
5248
5349 == SEE ALSO
5450
55 i3(1), i3-sensible-terminal(1)
51 i3(1)
5652
5753 == AUTHOR
5854