Add support for 'actions'

This commit is contained in:
Samuel Dudik 2020-08-15 19:32:26 +02:00
parent 46df7385ec
commit 11d9afc400
2 changed files with 30 additions and 4 deletions

View file

@ -14,3 +14,6 @@ enum corners { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT };
enum corners corner = TOP_RIGHT; enum corners corner = TOP_RIGHT;
static const unsigned int duration = 5; /* in seconds */ static const unsigned int duration = 5; /* in seconds */
#define DISMISS_BUTTON Button1
#define ACTION_BUTTON Button3

27
herbe.c
View file

@ -11,8 +11,11 @@
#include "config.h" #include "config.h"
#define EXIT_ACTION 3
Display *display; Display *display;
Window window; Window window;
int exit_code = EXIT_SUCCESS;
static void die(const char *format, ...) static void die(const char *format, ...)
{ {
@ -46,7 +49,8 @@ int get_max_len(char *body, XftFont *font, int max_text_width)
} }
for (int i = 0; i < eol; i++) for (int i = 0; i < eol; i++)
if (body[i] == '\n') { if (body[i] == '\n')
{
body[i] = ' '; body[i] = ' ';
return ++i; return ++i;
} }
@ -73,6 +77,12 @@ void expire()
XFlush(display); XFlush(display);
} }
void action()
{
exit_code = EXIT_ACTION;
expire();
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc == 1) if (argc == 1)
@ -81,6 +91,8 @@ int main(int argc, char *argv[])
signal(SIGALRM, expire); signal(SIGALRM, expire);
signal(SIGTERM, expire); signal(SIGTERM, expire);
signal(SIGINT, expire); signal(SIGINT, expire);
signal(SIGUSR1, SIG_IGN);
signal(SIGUSR2, SIG_IGN);
display = XOpenDisplay(0); display = XOpenDisplay(0);
@ -156,6 +168,9 @@ int main(int argc, char *argv[])
sem_t *mutex = sem_open("/herbe", O_CREAT, 0644, 1); sem_t *mutex = sem_open("/herbe", O_CREAT, 0644, 1);
sem_wait(mutex); sem_wait(mutex);
signal(SIGUSR1, expire);
signal(SIGUSR2, action);
if (duration != 0) if (duration != 0)
alarm(duration); alarm(duration);
@ -171,7 +186,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])); XftDrawStringUtf8(draw, &color, font, padding, line_spacing * i + text_height * (i + 1) + padding, (FcChar8 *)words[i], strlen(words[i]));
} }
if (event.type == ButtonPress) if (event.type == ButtonPress)
{
if (event.xbutton.button == DISMISS_BUTTON)
break; break;
else if (event.xbutton.button == ACTION_BUTTON)
{
exit_code = EXIT_ACTION;
break;
}
}
} }
sem_post(mutex); sem_post(mutex);
@ -186,5 +209,5 @@ int main(int argc, char *argv[])
XftFontClose(display, font); XftFontClose(display, font);
XCloseDisplay(display); XCloseDisplay(display);
exit(EXIT_SUCCESS); exit(exit_code);
} }