Replace signal with sigaction
[herbe.git] / herbe.c
diff --git a/herbe.c b/herbe.c
index 6aa8dfcf41664a67744d4c010862e02dda30e34c..42e7d73328437a0361184b316f01d69834a575f1 100644 (file)
--- a/herbe.c
+++ b/herbe.c
 
 #include "config.h"
 
+#define EXIT_ACTION 3
+
 Display *display;
 Window window;
+int exit_code = EXIT_SUCCESS;
 
 static void die(const char *format, ...)
 {
@@ -32,7 +35,6 @@ int get_max_len(char *body, XftFont *font, int max_text_width)
 
        if (info.width > max_text_width)
        {
-
                eol = max_text_width / font->max_advance_width;
                info.width = 0;
 
@@ -47,7 +49,10 @@ int get_max_len(char *body, XftFont *font, int max_text_width)
 
        for (int i = 0; i < eol; i++)
                if (body[i] == '\n')
+               {
+                       body[i] = ' ';
                        return ++i;
+               }
 
        if (info.width < max_text_width)
                return eol;
@@ -63,10 +68,11 @@ int get_max_len(char *body, XftFont *font, int max_text_width)
                return ++eol;
 }
 
-void expire()
+void expire(int sig)
 {
        XEvent event;
        event.type = ButtonPress;
+       event.xbutton.button = (sig == SIGUSR2) ? (ACTION_BUTTON) : (DISMISS_BUTTON);
        XSendEvent(display, window, 0, 0, &event);
        XFlush(display);
 }
@@ -74,11 +80,26 @@ void expire()
 int main(int argc, char *argv[])
 {
        if (argc == 1)
+       {
+               sem_unlink("/herbe");
                die("Usage: %s body", argv[0]);
+       }
+
+       struct sigaction act_expire, act_ignore;
+       act_expire.sa_handler = expire;
+       act_expire.sa_flags = SA_RESTART;
+       sigemptyset(&act_expire.sa_mask);
+
+       act_ignore.sa_handler = SIG_IGN;
+       act_ignore.sa_flags = 0;
+       sigemptyset(&act_ignore.sa_mask);
 
-       signal(SIGALRM, expire);
-       signal(SIGTERM, expire);
-       signal(SIGINT, expire);
+       sigaction(SIGALRM, &act_expire, 0);
+       sigaction(SIGTERM, &act_expire, 0);
+       sigaction(SIGINT, &act_expire, 0);
+
+       sigaction(SIGUSR1, &act_ignore, 0);
+       sigaction(SIGUSR2, &act_ignore, 0);
 
        display = XOpenDisplay(0);
 
@@ -154,6 +175,9 @@ int main(int argc, char *argv[])
        sem_t *mutex = sem_open("/herbe", O_CREAT, 0644, 1);
        sem_wait(mutex);
 
+       sigaction(SIGUSR1, &act_expire, 0);
+       sigaction(SIGUSR2, &act_expire, 0);
+
        if (duration != 0)
                alarm(duration);
 
@@ -169,7 +193,15 @@ int main(int argc, char *argv[])
                                XftDrawStringUtf8(draw, &color, font, padding, line_spacing * i + text_height * (i + 1) + padding, (FcChar8 *)words[i], strlen(words[i]));
                }
                if (event.type == ButtonPress)
-                       break;
+               {
+                       if (event.xbutton.button == DISMISS_BUTTON)
+                               break;
+                       else if (event.xbutton.button == ACTION_BUTTON)
+                       {
+                               exit_code = EXIT_ACTION;
+                               break;
+                       }
+               }
        }
 
        sem_post(mutex);
@@ -184,5 +216,5 @@ int main(int argc, char *argv[])
        XftFontClose(display, font);
        XCloseDisplay(display);
 
-       exit(EXIT_SUCCESS);
-}
+       exit(exit_code);
+}
\ No newline at end of file