]> git.sur5r.net Git - i3/i3/blob - src/sighandler.c
Bugfix: use the global root variable, don’t get the first one (Thanks quaec)
[i3/i3] / src / sighandler.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009-2010 Michael Stapelberg and contributors
7  * © 2009-2010 Jan-Erik Rediger
8  *
9  * See file LICENSE for license information.
10  *
11  * sighandler.c: contains all functions for signal handling
12  *
13  */
14 #include <ev.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <iconv.h>
20 #include <signal.h>
21
22 #include <xcb/xcb.h>
23 #include <xcb/xcb_aux.h>
24 #include <xcb/xcb_event.h>
25 #include <xcb/xcb_keysyms.h>
26
27 #include <X11/keysym.h>
28
29 #include "i3.h"
30 #include "util.h"
31 #include "xcb.h"
32 #include "log.h"
33 #include "config.h"
34 #include "randr.h"
35
36 static xcb_gcontext_t pixmap_gc;
37 static xcb_pixmap_t pixmap;
38 static int raised_signal;
39
40 static char *crash_text[] = {
41     "i3 just crashed.",
42     "To debug this problem, either attach gdb now",
43     "or press",
44     "- 'e' to exit and get a core-dump,",
45     "- 'r' to restart i3 in-place or",
46     "- 'f' to forget the current layout and restart"
47 };
48 static int crash_text_longest = 5;
49
50 /*
51  * Draw the window containing the info text
52  *
53  */
54 static int sig_draw_window(xcb_window_t win, int width, int height, int font_height) {
55     /* re-draw the background */
56     xcb_rectangle_t border = { 0, 0, width, height},
57                     inner = { 2, 2, width - 4, height - 4};
58     xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel("#FF0000"));
59     xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
60     xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel("#000000"));
61     xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
62
63     /* restore font color */
64     xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel("#FFFFFF"));
65
66     for (int i = 0; i < sizeof(crash_text) / sizeof(char*); i++) {
67         int text_len = strlen(crash_text[i]);
68         char *full_text = convert_utf8_to_ucs2(crash_text[i], &text_len);
69         xcb_image_text_16(conn, text_len, pixmap, pixmap_gc, 8 /* X */,
70                           3 + (i + 1) * font_height /* Y = baseline of font */,
71                           (xcb_char2b_t*)full_text);
72         free(full_text);
73     }
74
75     /* Copy the contents of the pixmap to the real window */
76     xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, width, height);
77     xcb_flush(conn);
78
79     return 1;
80 }
81
82 /*
83  * Handles keypresses of 'e' or 'r' to exit or restart i3
84  *
85  */
86 static int sig_handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
87     uint16_t state = event->state;
88
89     /* Apparantly, after activating numlock once, the numlock modifier
90      * stays turned on (use xev(1) to verify). So, to resolve useful
91      * keysyms, we remove the numlock flag from the event state */
92     state &= ~xcb_numlock_mask;
93
94     xcb_keysym_t sym = xcb_key_press_lookup_keysym(keysyms, event, state);
95
96     if (sym == 'e') {
97         DLOG("User issued exit-command, raising error again.\n");
98         raise(raised_signal);
99         exit(1);
100     }
101
102     if (sym == 'r')
103         i3_restart(false);
104
105     if (sym == 'f')
106         i3_restart(true);
107
108     return 1;
109 }
110
111 /*
112  * Opens the window we use for input/output and maps it
113  *
114  */
115 static xcb_window_t open_input_window(xcb_connection_t *conn, Rect screen_rect, uint32_t width, uint32_t height) {
116     xcb_window_t win = xcb_generate_id(conn);
117
118     uint32_t mask = 0;
119     uint32_t values[2];
120
121     mask |= XCB_CW_BACK_PIXEL;
122     values[0] = 0;
123
124     mask |= XCB_CW_OVERRIDE_REDIRECT;
125     values[1] = 1;
126
127     /* center each popup on the specified screen */
128     uint32_t x = screen_rect.x + ((screen_rect.width / 2) - (width / 2)),
129              y = screen_rect.y + ((screen_rect.height / 2) - (height / 2));
130
131     xcb_create_window(conn,
132                       XCB_COPY_FROM_PARENT,
133                       win, /* the window id */
134                       root, /* parent == root */
135                       x, y, width, height, /* dimensions */
136                       0, /* border = 0, we draw our own */
137                       XCB_WINDOW_CLASS_INPUT_OUTPUT,
138                       XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
139                       mask,
140                       values);
141
142     /* Map the window (= make it visible) */
143     xcb_map_window(conn, win);
144
145     return win;
146 }
147
148 /*
149  * Handle signals
150  * It creates a window asking the user to restart in-place
151  * or exit to generate a core dump
152  *
153  */
154 void handle_signal(int sig, siginfo_t *info, void *data) {
155     DLOG("i3 crashed. SIG: %d\n", sig);
156
157     struct sigaction action;
158     action.sa_handler = SIG_DFL;
159     sigaction(sig, &action, NULL);
160     raised_signal = sig;
161
162     /* setup event handler for key presses */
163     xcb_event_handlers_t sig_evenths;
164     memset(&sig_evenths, 0, sizeof(xcb_event_handlers_t));
165     xcb_event_handlers_init(conn, &sig_evenths);
166     xcb_event_set_key_press_handler(&sig_evenths, sig_handle_key_press, NULL);
167
168     i3Font *font = load_font(conn, config.font);
169
170     /* width and height of the popup window, so that the text fits in */
171     int crash_text_num = sizeof(crash_text) / sizeof(char*);
172     int height = 13 + (crash_text_num * font->height);
173
174     /* calculate width for longest text */
175     int text_len = strlen(crash_text[crash_text_longest]);
176     char *longest_text = convert_utf8_to_ucs2(crash_text[crash_text_longest], &text_len);
177     int font_width = predict_text_width(conn, config.font, longest_text, text_len);
178     int width = font_width + 20;
179
180     /* Open a popup window on each virtual screen */
181     Output *screen;
182     xcb_window_t win;
183     TAILQ_FOREACH(screen, &outputs, outputs) {
184         if (!screen->active)
185             continue;
186         win = open_input_window(conn, screen->rect, width, height);
187
188         /* Create pixmap */
189         pixmap = xcb_generate_id(conn);
190         pixmap_gc = xcb_generate_id(conn);
191         xcb_create_pixmap(conn, root_depth, pixmap, win, width, height);
192         xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
193
194         /* Create graphics context */
195         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font->id);
196
197         /* Grab the keyboard to get all input */
198         xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
199
200         /* Grab the cursor inside the popup */
201         xcb_grab_pointer(conn, false, win, XCB_NONE, XCB_GRAB_MODE_ASYNC,
202                          XCB_GRAB_MODE_ASYNC, win, XCB_NONE, XCB_CURRENT_TIME);
203
204         sig_draw_window(win, width, height, font->height);
205         xcb_flush(conn);
206     }
207
208     xcb_event_wait_for_event_loop(&sig_evenths);
209 }
210
211 /*
212  * Setup signal handlers to safely handle SIGSEGV and SIGFPE
213  *
214  */
215 void setup_signal_handler() {
216     struct sigaction action;
217
218     action.sa_sigaction = handle_signal;
219     action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
220     sigemptyset(&action.sa_mask);
221
222     if (sigaction(SIGSEGV, &action, NULL) == -1 ||
223         sigaction(SIGFPE, &action, NULL) == -1)
224         ELOG("Could not setup signal handler");
225 }