2 #define I3__FILE__ "sighandler.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8 * © 2009-2010 Jan-Erik Rediger
10 * sighandler.c: Interactive crash dialog upon SIGSEGV/SIGABRT/SIGFPE (offers
11 * to restart inplace).
20 #include <xcb/xcb_event.h>
22 #include <X11/keysym.h>
24 static xcb_gcontext_t pixmap_gc;
25 static xcb_pixmap_t pixmap;
26 static int raised_signal;
28 static char *crash_text[] = {
30 "To debug this problem, either attach gdb now",
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"
36 static int crash_text_longest = 5;
39 * Draw the window containing the info text
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);
51 /* restore font color */
52 set_font_colors(pixmap_gc, get_colorpixel("#FFFFFF"), get_colorpixel("#000000"));
54 for (int i = 0; crash_text_i3strings[i] != NULL; ++i) {
55 draw_text((char *)i3string_as_ucs2(crash_text_i3strings[i]), i3string_get_num_glyphs(crash_text_i3strings[i]), true, pixmap, pixmap_gc,
56 8, 5 + i * font_height, width - 16);
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);
67 * Handles keypresses of 'e' or 'r' to exit or restart i3
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;
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;
78 xcb_keysym_t sym = xcb_key_press_lookup_keysym(keysyms, event, state);
81 DLOG("User issued exit-command, raising error again.\n");
96 * Opens the window we use for input/output and maps it
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);
105 mask |= XCB_CW_BACK_PIXEL;
108 mask |= XCB_CW_OVERRIDE_REDIRECT;
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));
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 */
126 /* Map the window (= make it visible) */
127 xcb_map_window(conn, win);
134 * It creates a window asking the user to restart in-place
135 * or exit to generate a core dump
138 void handle_signal(int sig, siginfo_t *info, void *data) {
139 DLOG("i3 crashed. SIG: %d\n", sig);
141 struct sigaction action;
142 action.sa_handler = SIG_DFL;
143 sigaction(sig, &action, NULL);
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);
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]);
156 crash_text_i3strings[crash_text_length] = NULL;
157 /* calculate width for longest text */
158 int font_width = predict_text_width((char *)i3string_as_ucs2(crash_text_i3strings[crash_text_longest]), i3string_get_num_glyphs(crash_text_i3strings[crash_text_longest]), true);
159 int width = font_width + 20;
161 /* Open a popup window on each virtual screen */
164 TAILQ_FOREACH(screen, &outputs, outputs) {
167 win = open_input_window(conn, screen->rect, width, height);
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);
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);
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);
182 sig_draw_window(win, width, height, config.font.height, crash_text_i3strings);
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);
199 * Setup signal handlers to safely handle SIGSEGV and SIGFPE
202 void setup_signal_handler(void) {
203 struct sigaction action;
205 action.sa_sigaction = handle_signal;
206 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
207 sigemptyset(&action.sa_mask);
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");