2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
6 * © 2009-2010 Jan-Erik Rediger
8 * sighandler.c: Interactive crash dialog upon SIGSEGV/SIGABRT/SIGFPE (offers
18 #include <xcb/xcb_event.h>
20 #include <X11/keysym.h>
22 static xcb_gcontext_t pixmap_gc;
23 static xcb_pixmap_t pixmap;
24 static int raised_signal;
26 static char *crash_text[] = {
28 "To debug this problem, either attach gdb now",
30 "- 'e' to exit and get a core-dump,",
31 "- 'r' to restart i3 in-place or",
32 "- 'f' to forget the current layout and restart"
34 static int crash_text_longest = 5;
37 * Draw the window containing the info text
40 static int sig_draw_window(xcb_window_t win, int width, int height, int font_height) {
41 /* re-draw the background */
42 xcb_rectangle_t border = { 0, 0, width, height},
43 inner = { 2, 2, width - 4, height - 4};
44 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel("#FF0000") });
45 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
46 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel("#000000") });
47 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
49 /* restore font color */
50 set_font_colors(pixmap_gc, get_colorpixel("#FFFFFF"), get_colorpixel("#000000"));
52 for (int i = 0; i < sizeof(crash_text) / sizeof(char*); i++) {
53 draw_text(crash_text[i], strlen(crash_text[i]), false, pixmap, pixmap_gc,
54 8, 3 + (i - 1) * font_height, width - 16);
57 /* Copy the contents of the pixmap to the real window */
58 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, width, height);
65 * Handles keypresses of 'e' or 'r' to exit or restart i3
68 static int sig_handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
69 uint16_t state = event->state;
71 /* Apparantly, after activating numlock once, the numlock modifier
72 * stays turned on (use xev(1) to verify). So, to resolve useful
73 * keysyms, we remove the numlock flag from the event state */
74 state &= ~xcb_numlock_mask;
76 xcb_keysym_t sym = xcb_key_press_lookup_keysym(keysyms, event, state);
79 DLOG("User issued exit-command, raising error again.\n");
94 * Opens the window we use for input/output and maps it
97 static xcb_window_t open_input_window(xcb_connection_t *conn, Rect screen_rect, uint32_t width, uint32_t height) {
98 xcb_window_t win = xcb_generate_id(conn);
103 mask |= XCB_CW_BACK_PIXEL;
106 mask |= XCB_CW_OVERRIDE_REDIRECT;
109 /* center each popup on the specified screen */
110 uint32_t x = screen_rect.x + ((screen_rect.width / 2) - (width / 2)),
111 y = screen_rect.y + ((screen_rect.height / 2) - (height / 2));
113 xcb_create_window(conn,
114 XCB_COPY_FROM_PARENT,
115 win, /* the window id */
116 root, /* parent == root */
117 x, y, width, height, /* dimensions */
118 0, /* border = 0, we draw our own */
119 XCB_WINDOW_CLASS_INPUT_OUTPUT,
120 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
124 /* Map the window (= make it visible) */
125 xcb_map_window(conn, win);
132 * It creates a window asking the user to restart in-place
133 * or exit to generate a core dump
136 void handle_signal(int sig, siginfo_t *info, void *data) {
137 DLOG("i3 crashed. SIG: %d\n", sig);
139 struct sigaction action;
140 action.sa_handler = SIG_DFL;
141 sigaction(sig, &action, NULL);
144 /* width and height of the popup window, so that the text fits in */
145 int crash_text_num = sizeof(crash_text) / sizeof(char*);
146 int height = 13 + (crash_text_num * config.font.height);
148 /* calculate width for longest text */
149 size_t text_len = strlen(crash_text[crash_text_longest]);
150 xcb_char2b_t *longest_text = convert_utf8_to_ucs2(crash_text[crash_text_longest], &text_len);
151 int font_width = predict_text_width((char *)longest_text, text_len, true);
152 int width = font_width + 20;
154 /* Open a popup window on each virtual screen */
157 TAILQ_FOREACH(screen, &outputs, outputs) {
160 win = open_input_window(conn, screen->rect, width, height);
163 pixmap = xcb_generate_id(conn);
164 pixmap_gc = xcb_generate_id(conn);
165 xcb_create_pixmap(conn, root_depth, pixmap, win, width, height);
166 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
168 /* Grab the keyboard to get all input */
169 xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
171 /* Grab the cursor inside the popup */
172 xcb_grab_pointer(conn, false, win, XCB_NONE, XCB_GRAB_MODE_ASYNC,
173 XCB_GRAB_MODE_ASYNC, win, XCB_NONE, XCB_CURRENT_TIME);
175 sig_draw_window(win, width, height, config.font.height);
179 xcb_generic_event_t *event;
180 /* Yay, more own eventhandlers… */
181 while ((event = xcb_wait_for_event(conn))) {
182 /* Strip off the highest bit (set if the event is generated) */
183 int type = (event->response_type & 0x7F);
184 if (type == XCB_KEY_PRESS) {
185 sig_handle_key_press(NULL, conn, (xcb_key_press_event_t*)event);
192 * Setup signal handlers to safely handle SIGSEGV and SIGFPE
195 void setup_signal_handler() {
196 struct sigaction action;
198 action.sa_sigaction = handle_signal;
199 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
200 sigemptyset(&action.sa_mask);
202 /* Catch all signals with default action "Core", see signal(7) */
203 if (sigaction(SIGQUIT, &action, NULL) == -1 ||
204 sigaction(SIGILL, &action, NULL) == -1 ||
205 sigaction(SIGABRT, &action, NULL) == -1 ||
206 sigaction(SIGFPE, &action, NULL) == -1 ||
207 sigaction(SIGSEGV, &action, NULL) == -1)
208 ELOG("Could not setup signal handler");