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