4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 Michael Stapelberg and contributors
7 * © 2009-2010 Jan-Erik Rediger
9 * See file LICENSE for license information.
11 * sighandler.c: contains all functions for signal handling
23 #include <xcb/xcb_aux.h>
24 #include <xcb/xcb_event.h>
25 #include <xcb/xcb_keysyms.h>
27 #include <X11/keysym.h>
36 static xcb_gcontext_t pixmap_gc;
37 static xcb_pixmap_t pixmap;
38 static int raised_signal;
40 static char *crash_text[] = {
42 "To debug this problem, either attach gdb now",
43 "or press 'e' to exit and get a core-dump.",
44 "If you want to keep your session,",
45 "press 'r' to restart i3 in-place."
47 static int crash_text_longest = 1;
50 * Draw the window containing the info text
53 static int sig_draw_window(xcb_connection_t *conn, xcb_window_t win, int width, int height, int font_height) {
54 /* re-draw the background */
55 xcb_rectangle_t border = {0, 0, width, height}, inner = {2, 2, width-4, height-4};
56 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FF0000"));
57 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
58 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
59 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
61 /* restore font color */
62 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FFFFFF"));
67 int crash_text_num = sizeof(crash_text) / sizeof(char*);
68 for (i = 0; i < crash_text_num; i++) {
69 text_len = strlen(crash_text[i]);
70 full_text = convert_utf8_to_ucs2(crash_text[i], &text_len);
71 xcb_image_text_16(conn, text_len, pixmap, pixmap_gc, 8 /* X */,
72 3 + (i+1)*font_height /* Y = baseline of font */, (xcb_char2b_t*)full_text);
76 /* Copy the contents of the pixmap to the real window */
77 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, width, height);
85 * Handles keypresses of 'e' or 'r' to exit or restart i3
88 static int sig_handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
89 xcb_keysym_t sym = xcb_key_press_lookup_keysym(keysyms, event, event->state);
92 LOG("User issued exit-command, raising error again.\n");
103 * Opens the window we use for input/output and maps it
106 static xcb_window_t open_input_window(xcb_connection_t *conn, Rect screen_rect, uint32_t width, uint32_t height) {
107 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
108 xcb_window_t win = xcb_generate_id(conn);
113 mask |= XCB_CW_BACK_PIXEL;
116 mask |= XCB_CW_OVERRIDE_REDIRECT;
119 mask |= XCB_CW_EVENT_MASK;
120 values[2] = XCB_EVENT_MASK_EXPOSURE;
122 /* center each popup on the specified screen */
123 uint32_t x = screen_rect.x + ((screen_rect.width / 2) - (width/2)),
124 y = screen_rect.y + ((screen_rect.height / 2) - (height/2));
126 xcb_create_window(conn,
127 XCB_COPY_FROM_PARENT,
128 win, /* the window id */
129 root, /* parent == root */
130 x, y, width, height, /* dimensions */
131 0, /* border = 0, we draw our own */
132 XCB_WINDOW_CLASS_INPUT_OUTPUT,
133 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
137 /* Map the window (= make it visible) */
138 xcb_map_window(conn, win);
145 * It creates a window asking the user to restart in-place
146 * or exit to generate a core dump
149 void handle_signal(int sig, siginfo_t *info, void *data) {
150 LOG("i3 crashed. SIG: %d\n", sig);
152 struct sigaction action;
153 action.sa_handler = SIG_DFL;
154 sigaction(sig, &action, NULL);
157 xcb_connection_t *conn = global_conn;
159 /* Set up event handlers for key press and key release */
160 xcb_event_handlers_t sig_evenths;
161 memset(&sig_evenths, 0, sizeof(xcb_event_handlers_t));
162 xcb_event_handlers_init(conn, &sig_evenths);
163 xcb_event_set_key_press_handler(&sig_evenths, sig_handle_key_press, NULL);
165 i3Font *font = load_font(conn, config.font);
167 /* width and height of the popup window, so that the text fits in */
168 int crash_text_num = sizeof(crash_text) / sizeof(char*);
169 int height = 13 + (crash_text_num * font->height);
171 /* calculate width for longest text */
172 int text_len = strlen(crash_text[crash_text_longest]);
173 char *longest_text = convert_utf8_to_ucs2(crash_text[crash_text_longest], &text_len);
174 int font_width = predict_text_width(conn, config.font, longest_text, text_len);
175 int width = font_width + 20;
177 /* Open an popup window on each virtual screen */
180 TAILQ_FOREACH(screen, virtual_screens, screens) {
181 LOG("%d, %d, %d, %d\n", screen->rect.width, screen->rect.height, screen->rect.x, screen->rect.y);
183 win = open_input_window(conn, screen->rect, width, height);
186 pixmap = xcb_generate_id(conn);
187 pixmap_gc = xcb_generate_id(conn);
188 xcb_create_pixmap(conn, root_depth, pixmap, win, width, height);
189 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
191 /* Create graphics context */
192 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font->id);
194 /* Grab the keyboard to get all input */
195 xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
197 sig_draw_window(conn, win, width, height, font->height);
201 xcb_event_wait_for_event_loop(&sig_evenths);
205 * Setup signal handlers to safely handle SIGSEGV and SIGFPE
208 void setup_signal_handler() {
209 struct sigaction action;
210 action.sa_sigaction = handle_signal;
211 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
212 sigemptyset(&action.sa_mask);
213 sigaction(SIGSEGV, &action, NULL);
214 sigaction(SIGFPE, &action, NULL);