]> git.sur5r.net Git - i3/i3/blobdiff - src/sighandler.c
i3/sighandler: Pre-compute i3Strings for text
[i3/i3] / src / sighandler.c
index ca74813d36e2e0f215a4969e3740724368faf6ab..2c53333d854e2580f43f8b2c40dc82d687390a19 100644 (file)
@@ -1,3 +1,5 @@
+#undef I3__FILE__
+#define I3__FILE__ "sighandler.c"
 /*
  * vim:ts=4:sw=4:expandtab
  *
@@ -37,7 +39,7 @@ static int crash_text_longest = 5;
  * Draw the window containing the info text
  *
  */
-static int sig_draw_window(xcb_window_t win, int width, int height, int font_height) {
+static int sig_draw_window(xcb_window_t win, int width, int height, int font_height, i3String **crash_text_i3strings) {
     /* re-draw the background */
     xcb_rectangle_t border = { 0, 0, width, height},
                     inner = { 2, 2, width - 4, height - 4};
@@ -49,9 +51,9 @@ static int sig_draw_window(xcb_window_t win, int width, int height, int font_hei
     /* restore font color */
     set_font_colors(pixmap_gc, get_colorpixel("#FFFFFF"), get_colorpixel("#000000"));
 
-    for (int i = 0; i < sizeof(crash_text) / sizeof(char*); i++) {
-        draw_text(crash_text[i], strlen(crash_text[i]), false, pixmap, pixmap_gc,
-                8, 3 + (i - 1) * font_height, width - 16);
+    for (int i = 0; crash_text_i3strings[i] != NULL; ++i) {
+        draw_text((char *)i3string_as_ucs2(crash_text_i3strings[i]), i3string_get_num_glyphs(crash_text_i3strings[i]), true, pixmap, pixmap_gc,
+                8, 5 + i * font_height, width - 16);
     }
 
     /* Copy the contents of the pixmap to the real window */
@@ -145,10 +147,15 @@ void handle_signal(int sig, siginfo_t *info, void *data) {
     int crash_text_num = sizeof(crash_text) / sizeof(char*);
     int height = 13 + (crash_text_num * config.font.height);
 
+    int crash_text_length = sizeof(crash_text) / sizeof(char*);
+    i3String **crash_text_i3strings = smalloc(sizeof(i3String *) * (crash_text_length + 1));
+    /* Pre-compute i3Strings for our text */
+    for (int i = 0; i < crash_text_length; ++i) {
+        crash_text_i3strings[i] = i3string_from_utf8(crash_text[i]);
+    }
+    crash_text_i3strings[crash_text_length] = NULL;
     /* calculate width for longest text */
-    size_t text_len = strlen(crash_text[crash_text_longest]);
-    xcb_char2b_t *longest_text = convert_utf8_to_ucs2(crash_text[crash_text_longest], &text_len);
-    int font_width = predict_text_width((char *)longest_text, text_len, true);
+    int font_width = predict_text_width((char *)i3string_as_ucs2(crash_text_i3strings[crash_text_longest]), i3string_get_num_glyphs(crash_text_i3strings[crash_text_longest]), true);
     int width = font_width + 20;
 
     /* Open a popup window on each virtual screen */
@@ -172,7 +179,7 @@ void handle_signal(int sig, siginfo_t *info, void *data) {
         xcb_grab_pointer(conn, false, win, XCB_NONE, XCB_GRAB_MODE_ASYNC,
                          XCB_GRAB_MODE_ASYNC, win, XCB_NONE, XCB_CURRENT_TIME);
 
-        sig_draw_window(win, width, height, config.font.height);
+        sig_draw_window(win, width, height, config.font.height, crash_text_i3strings);
         xcb_flush(conn);
     }
 
@@ -192,15 +199,18 @@ void handle_signal(int sig, siginfo_t *info, void *data) {
  * Setup signal handlers to safely handle SIGSEGV and SIGFPE
  *
  */
-void setup_signal_handler() {
+void setup_signal_handler(void) {
     struct sigaction action;
 
     action.sa_sigaction = handle_signal;
     action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
     sigemptyset(&action.sa_mask);
 
-    if (sigaction(SIGSEGV, &action, NULL) == -1 ||
+    /* Catch all signals with default action "Core", see signal(7) */
+    if (sigaction(SIGQUIT, &action, NULL) == -1 ||
+        sigaction(SIGILL, &action, NULL) == -1 ||
         sigaction(SIGABRT, &action, NULL) == -1 ||
-        sigaction(SIGFPE, &action, NULL) == -1)
+        sigaction(SIGFPE, &action, NULL) == -1 ||
+        sigaction(SIGSEGV, &action, NULL) == -1)
         ELOG("Could not setup signal handler");
 }