]> git.sur5r.net Git - i3/i3/blob - src/sighandler.c
Merge branch 'master' into next
[i3/i3] / src / sighandler.c
1 #undef I3__FILE__
2 #define I3__FILE__ "sighandler.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8  * © 2009-2010 Jan-Erik Rediger
9  *
10  * sighandler.c: Interactive crash dialog upon SIGSEGV/SIGABRT/SIGFPE (offers
11  *               to restart inplace).
12  *
13  */
14 #include "all.h"
15
16 #include <ev.h>
17 #include <iconv.h>
18 #include <signal.h>
19
20 #include <xcb/xcb_event.h>
21
22 #include <X11/keysym.h>
23
24 static xcb_gcontext_t pixmap_gc;
25 static xcb_pixmap_t pixmap;
26 static int raised_signal;
27
28 static char *crash_text[] = {
29     "i3 just crashed.",
30     "To debug this problem, either attach gdb now",
31     "or press",
32     "- 'e' to exit and get a core-dump,",
33     "- 'r' to restart i3 in-place or",
34     "- 'f' to forget the current layout and restart"
35 };
36 static int crash_text_longest = 5;
37
38 /*
39  * Draw the window containing the info text
40  *
41  */
42 static int sig_draw_window(xcb_window_t win, int width, int height, int font_height, i3String **crash_text_i3strings) {
43     /* re-draw the background */
44     xcb_rectangle_t border = { 0, 0, width, height},
45                     inner = { 2, 2, width - 4, height - 4};
46     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel("#FF0000") });
47     xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
48     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel("#000000") });
49     xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
50
51     /* restore font color */
52     set_font_colors(pixmap_gc, get_colorpixel("#FFFFFF"), get_colorpixel("#000000"));
53
54     for (int i = 0; crash_text_i3strings[i] != NULL; ++i) {
55         draw_text(crash_text_i3strings[i], pixmap, pixmap_gc,
56                 8, 5 + i * font_height, width - 16);
57     }
58
59     /* Copy the contents of the pixmap to the real window */
60     xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, width, height);
61     xcb_flush(conn);
62
63     return 1;
64 }
65
66 /*
67  * Handles keypresses of 'e' or 'r' to exit or restart i3
68  *
69  */
70 static int sig_handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
71     uint16_t state = event->state;
72
73     /* Apparantly, after activating numlock once, the numlock modifier
74      * stays turned on (use xev(1) to verify). So, to resolve useful
75      * keysyms, we remove the numlock flag from the event state */
76     state &= ~xcb_numlock_mask;
77
78     xcb_keysym_t sym = xcb_key_press_lookup_keysym(keysyms, event, state);
79
80     if (sym == 'e') {
81         DLOG("User issued exit-command, raising error again.\n");
82         raise(raised_signal);
83         exit(1);
84     }
85
86     if (sym == 'r')
87         i3_restart(false);
88
89     if (sym == 'f')
90         i3_restart(true);
91
92     return 1;
93 }
94
95 /*
96  * Opens the window we use for input/output and maps it
97  *
98  */
99 static xcb_window_t open_input_window(xcb_connection_t *conn, Rect screen_rect, uint32_t width, uint32_t height) {
100     xcb_window_t win = xcb_generate_id(conn);
101
102     uint32_t mask = 0;
103     uint32_t values[2];
104
105     mask |= XCB_CW_BACK_PIXEL;
106     values[0] = 0;
107
108     mask |= XCB_CW_OVERRIDE_REDIRECT;
109     values[1] = 1;
110
111     /* center each popup on the specified screen */
112     uint32_t x = screen_rect.x + ((screen_rect.width / 2) - (width / 2)),
113              y = screen_rect.y + ((screen_rect.height / 2) - (height / 2));
114
115     xcb_create_window(conn,
116                       XCB_COPY_FROM_PARENT,
117                       win, /* the window id */
118                       root, /* parent == root */
119                       x, y, width, height, /* dimensions */
120                       0, /* border = 0, we draw our own */
121                       XCB_WINDOW_CLASS_INPUT_OUTPUT,
122                       XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
123                       mask,
124                       values);
125
126     /* Map the window (= make it visible) */
127     xcb_map_window(conn, win);
128
129     return win;
130 }
131
132 /*
133  * Handle signals
134  * It creates a window asking the user to restart in-place
135  * or exit to generate a core dump
136  *
137  */
138 void handle_signal(int sig, siginfo_t *info, void *data) {
139     DLOG("i3 crashed. SIG: %d\n", sig);
140
141     struct sigaction action;
142     action.sa_handler = SIG_DFL;
143     sigaction(sig, &action, NULL);
144     raised_signal = sig;
145
146     /* width and height of the popup window, so that the text fits in */
147     int crash_text_num = sizeof(crash_text) / sizeof(char*);
148     int height = 13 + (crash_text_num * config.font.height);
149
150     int crash_text_length = sizeof(crash_text) / sizeof(char*);
151     i3String **crash_text_i3strings = smalloc(sizeof(i3String *) * (crash_text_length + 1));
152     /* Pre-compute i3Strings for our text */
153     for (int i = 0; i < crash_text_length; ++i) {
154         crash_text_i3strings[i] = i3string_from_utf8(crash_text[i]);
155     }
156     crash_text_i3strings[crash_text_length] = NULL;
157     /* calculate width for longest text */
158     int font_width = predict_text_width(crash_text_i3strings[crash_text_longest]);
159     int width = font_width + 20;
160
161     /* Open a popup window on each virtual screen */
162     Output *screen;
163     xcb_window_t win;
164     TAILQ_FOREACH(screen, &outputs, outputs) {
165         if (!screen->active)
166             continue;
167         win = open_input_window(conn, screen->rect, width, height);
168
169         /* Create pixmap */
170         pixmap = xcb_generate_id(conn);
171         pixmap_gc = xcb_generate_id(conn);
172         xcb_create_pixmap(conn, root_depth, pixmap, win, width, height);
173         xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
174
175         /* Grab the keyboard to get all input */
176         xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
177
178         /* Grab the cursor inside the popup */
179         xcb_grab_pointer(conn, false, win, XCB_NONE, XCB_GRAB_MODE_ASYNC,
180                          XCB_GRAB_MODE_ASYNC, win, XCB_NONE, XCB_CURRENT_TIME);
181
182         sig_draw_window(win, width, height, config.font.height, crash_text_i3strings);
183         xcb_flush(conn);
184     }
185
186     xcb_generic_event_t *event;
187     /* Yay, more own eventhandlers… */
188     while ((event = xcb_wait_for_event(conn))) {
189         /* Strip off the highest bit (set if the event is generated) */
190         int type = (event->response_type & 0x7F);
191         if (type == XCB_KEY_PRESS) {
192             sig_handle_key_press(NULL, conn, (xcb_key_press_event_t*)event);
193         }
194         free(event);
195     }
196 }
197
198 /*
199  * Setup signal handlers to safely handle SIGSEGV and SIGFPE
200  *
201  */
202 void setup_signal_handler(void) {
203     struct sigaction action;
204
205     action.sa_sigaction = handle_signal;
206     action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
207     sigemptyset(&action.sa_mask);
208
209     /* Catch all signals with default action "Core", see signal(7) */
210     if (sigaction(SIGQUIT, &action, NULL) == -1 ||
211         sigaction(SIGILL, &action, NULL) == -1 ||
212         sigaction(SIGABRT, &action, NULL) == -1 ||
213         sigaction(SIGFPE, &action, NULL) == -1 ||
214         sigaction(SIGSEGV, &action, NULL) == -1)
215         ELOG("Could not setup signal handler");
216 }