]> git.sur5r.net Git - i3/i3/blob - src/sighandler.c
Remove dependency on xcb-event (Thanks Felicitus)
[i3/i3] / src / sighandler.c
1 /*
2  * vim:ts=8: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_keysyms.h>
25
26 #include <X11/keysym.h>
27
28 #include "i3.h"
29 #include "util.h"
30 #include "xcb.h"
31 #include "log.h"
32 #include "config.h"
33 #include "randr.h"
34
35 static xcb_gcontext_t pixmap_gc;
36 static xcb_pixmap_t pixmap;
37 static int raised_signal;
38
39 static char *crash_text[] = {
40         "i3 just crashed.",
41         "To debug this problem, either attach gdb now",
42         "or press 'e' to exit and get a core-dump.",
43         "If you want to keep your session,",
44         "press 'r' to restart i3 in-place."
45 };
46 static int crash_text_longest = 1;
47
48 /*
49  * Draw the window containing the info text
50  *
51  */
52 static int sig_draw_window(xcb_connection_t *conn, xcb_window_t win, int width, int height, int font_height) {
53         /* re-draw the background */
54         xcb_rectangle_t border = { 0, 0, width, height},
55                         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);
60
61         /* restore font color */
62         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FFFFFF"));
63
64         for (int i = 0; i < sizeof(crash_text) / sizeof(char*); i++) {
65                 int text_len = strlen(crash_text[i]);
66                 char *full_text = convert_utf8_to_ucs2(crash_text[i], &text_len);
67                 xcb_image_text_16(conn, text_len, pixmap, pixmap_gc, 8 /* X */,
68                                 3 + (i + 1) * font_height /* Y = baseline of font */,
69                                 (xcb_char2b_t*)full_text);
70                 free(full_text);
71         }
72
73         /* Copy the contents of the pixmap to the real window */
74         xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, width, height);
75         xcb_flush(conn);
76
77         return 1;
78 }
79
80 /*
81  * Handles keypresses of 'e' or 'r' to exit or restart i3
82  *
83  */
84 static int sig_handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
85         uint16_t state = event->state;
86
87         /* Apparantly, after activating numlock once, the numlock modifier
88          * stays turned on (use xev(1) to verify). So, to resolve useful
89          * keysyms, we remove the numlock flag from the event state */
90         state &= ~xcb_numlock_mask;
91
92         xcb_keysym_t sym = xcb_key_press_lookup_keysym(keysyms, event, state);
93
94         if (sym == 'e') {
95                 DLOG("User issued exit-command, raising error again.\n");
96                 raise(raised_signal);
97                 exit(1);
98         }
99
100         if (sym == 'r')
101                 i3_restart();
102
103         return 1;
104 }
105
106 /*
107  * Opens the window we use for input/output and maps it
108  *
109  */
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);
112
113         uint32_t mask = 0;
114         uint32_t values[2];
115
116         mask |= XCB_CW_BACK_PIXEL;
117         values[0] = 0;
118
119         mask |= XCB_CW_OVERRIDE_REDIRECT;
120         values[1] = 1;
121
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));
125
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 */
134                         mask,
135                         values);
136
137         /* Map the window (= make it visible) */
138         xcb_map_window(conn, win);
139
140         return win;
141 }
142
143 /*
144  * Handle signals
145  * It creates a window asking the user to restart in-place
146  * or exit to generate a core dump
147  *
148  */
149 void handle_signal(int sig, siginfo_t *info, void *data) {
150         DLOG("i3 crashed. SIG: %d\n", sig);
151
152         struct sigaction action;
153         action.sa_handler = SIG_DFL;
154         sigaction(sig, &action, NULL);
155         raised_signal = sig;
156
157         xcb_connection_t *conn = global_conn;
158
159         i3Font *font = load_font(conn, config.font);
160
161         /* width and height of the popup window, so that the text fits in */
162         int crash_text_num = sizeof(crash_text) / sizeof(char*);
163         int height = 13 + (crash_text_num * font->height);
164
165         /* calculate width for longest text */
166         int text_len = strlen(crash_text[crash_text_longest]);
167         char *longest_text = convert_utf8_to_ucs2(crash_text[crash_text_longest], &text_len);
168         int font_width = predict_text_width(conn, config.font, longest_text, text_len);
169         int width = font_width + 20;
170
171         /* Open a popup window on each virtual screen */
172         Output *screen;
173         xcb_window_t win;
174         TAILQ_FOREACH(screen, &outputs, outputs) {
175                 if (!screen->active)
176                         continue;
177                 win = open_input_window(conn, screen->rect, width, height);
178
179                 /* Create pixmap */
180                 pixmap = xcb_generate_id(conn);
181                 pixmap_gc = xcb_generate_id(conn);
182                 xcb_create_pixmap(conn, root_depth, pixmap, win, width, height);
183                 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
184
185                 /* Create graphics context */
186                 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font->id);
187
188                 /* Grab the keyboard to get all input */
189                 xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
190
191                 /* Grab the cursor inside the popup */
192                 xcb_grab_pointer(conn, false, win, XCB_NONE, XCB_GRAB_MODE_ASYNC,
193                                  XCB_GRAB_MODE_ASYNC, win, XCB_NONE, XCB_CURRENT_TIME);
194
195                 sig_draw_window(conn, win, width, height, font->height);
196                 xcb_flush(conn);
197         }
198
199         xcb_generic_event_t *event;
200         /* Yay, more own eventhandlers… */
201         while ((event = xcb_wait_for_event(conn))) {
202                 /* Strip off the highest bit (set if the event is generated) */
203                 int type = (event->response_type & 0x7F);
204                 if (type == XCB_KEY_PRESS) {
205                         sig_handle_key_press(NULL, conn, (xcb_key_press_event_t*)event);
206                 }
207                 free(event);
208         }
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 }