2 * vim:ts=4:sw=4:expandtab
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>
31 static xcb_gcontext_t pixmap_gc;
32 static xcb_pixmap_t pixmap;
33 static int raised_signal;
35 static char *crash_text[] = {
37 "To debug this problem, either attach gdb now",
39 "- 'e' to exit and get a core-dump,",
40 "- 'r' to restart i3 in-place or",
41 "- 'f' to forget the current layout and restart"
43 static int crash_text_longest = 5;
46 * Draw the window containing the info text
49 static int sig_draw_window(xcb_window_t win, int width, int height, int font_height) {
50 /* re-draw the background */
51 xcb_rectangle_t border = { 0, 0, width, height},
52 inner = { 2, 2, width - 4, height - 4};
53 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel("#FF0000"));
54 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
55 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel("#000000"));
56 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
58 /* restore font color */
59 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel("#FFFFFF"));
61 for (int i = 0; i < sizeof(crash_text) / sizeof(char*); i++) {
62 int text_len = strlen(crash_text[i]);
63 char *full_text = convert_utf8_to_ucs2(crash_text[i], &text_len);
64 xcb_image_text_16(conn, text_len, pixmap, pixmap_gc, 8 /* X */,
65 3 + (i + 1) * font_height /* Y = baseline of font */,
66 (xcb_char2b_t*)full_text);
70 /* Copy the contents of the pixmap to the real window */
71 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, width, height);
78 * Handles keypresses of 'e' or 'r' to exit or restart i3
81 static int sig_handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
82 uint16_t state = event->state;
84 /* Apparantly, after activating numlock once, the numlock modifier
85 * stays turned on (use xev(1) to verify). So, to resolve useful
86 * keysyms, we remove the numlock flag from the event state */
87 state &= ~xcb_numlock_mask;
89 xcb_keysym_t sym = xcb_key_press_lookup_keysym(keysyms, event, state);
92 DLOG("User issued exit-command, raising error again.\n");
107 * Opens the window we use for input/output and maps it
110 static xcb_window_t open_input_window(xcb_connection_t *conn, Rect screen_rect, uint32_t width, uint32_t height) {
111 xcb_window_t win = xcb_generate_id(conn);
116 mask |= XCB_CW_BACK_PIXEL;
119 mask |= XCB_CW_OVERRIDE_REDIRECT;
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 DLOG("i3 crashed. SIG: %d\n", sig);
152 struct sigaction action;
153 action.sa_handler = SIG_DFL;
154 sigaction(sig, &action, NULL);
157 /* width and height of the popup window, so that the text fits in */
158 int crash_text_num = sizeof(crash_text) / sizeof(char*);
159 int height = 13 + (crash_text_num * config.font.height);
161 /* calculate width for longest text */
162 int text_len = strlen(crash_text[crash_text_longest]);
163 char *longest_text = convert_utf8_to_ucs2(crash_text[crash_text_longest], &text_len);
164 int font_width = predict_text_width(longest_text, text_len);
165 int width = font_width + 20;
167 /* Open a popup window on each virtual screen */
170 TAILQ_FOREACH(screen, &outputs, outputs) {
173 win = open_input_window(conn, screen->rect, width, height);
176 pixmap = xcb_generate_id(conn);
177 pixmap_gc = xcb_generate_id(conn);
178 xcb_create_pixmap(conn, root_depth, pixmap, win, width, height);
179 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
181 /* Create graphics context */
182 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, config.font.id);
184 /* Grab the keyboard to get all input */
185 xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
187 /* Grab the cursor inside the popup */
188 xcb_grab_pointer(conn, false, win, XCB_NONE, XCB_GRAB_MODE_ASYNC,
189 XCB_GRAB_MODE_ASYNC, win, XCB_NONE, XCB_CURRENT_TIME);
191 sig_draw_window(win, width, height, config.font.height);
195 xcb_generic_event_t *event;
196 /* Yay, more own eventhandlers… */
197 while ((event = xcb_wait_for_event(conn))) {
198 /* Strip off the highest bit (set if the event is generated) */
199 int type = (event->response_type & 0x7F);
200 if (type == XCB_KEY_PRESS) {
201 sig_handle_key_press(NULL, conn, (xcb_key_press_event_t*)event);
208 * Setup signal handlers to safely handle SIGSEGV and SIGFPE
211 void setup_signal_handler() {
212 struct sigaction action;
214 action.sa_sigaction = handle_signal;
215 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
216 sigemptyset(&action.sa_mask);
218 if (sigaction(SIGSEGV, &action, NULL) == -1 ||
219 sigaction(SIGABRT, &action, NULL) == -1 ||
220 sigaction(SIGFPE, &action, NULL) == -1)
221 ELOG("Could not setup signal handler");