From b008879c87880d89adda5862bb5242f719d0b357 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Tue, 16 Nov 2021 03:48:21 +0500 Subject: [PATCH] Use slstatus --- Makefile | 4 ++-- datetime.c | 61 ------------------------------------------------------ datetime.h | 9 -------- main.c | 6 +++--- status.c | 44 +++++++++++++++++++++++++++++++++++++++ status.h | 9 ++++++++ 6 files changed, 58 insertions(+), 75 deletions(-) delete mode 100644 datetime.c delete mode 100644 datetime.h create mode 100644 status.c create mode 100644 status.h diff --git a/Makefile b/Makefile index 8407c7b..7ab2849 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,8 @@ include config.mk -SRC = datetime.c main.c -HDR = datetime.h +SRC = status.c main.c +HDR = status.h OBJ = $(SRC:.c=.o) all: options polytree-session diff --git a/datetime.c b/datetime.c deleted file mode 100644 index 9ba3888..0000000 --- a/datetime.c +++ /dev/null @@ -1,61 +0,0 @@ -#include "datetime.h" - -#include -#include -#include - -static bool running = false; -static Display *display = NULL; -static pthread_t thread; -static bool thread_created = false; - -static void *run(void *arg); - -bool datetime_start() -{ - // TODO: maybe we should assert - if (running) return false; - running = true; - - if ((display = XOpenDisplay(NULL)) == NULL) { - datetime_stop(); - return false; - } - - if (!(thread_created = pthread_create(&thread, NULL, run, NULL) == 0)) { - datetime_stop(); - return false; - } - - return true; -} - -void datetime_stop() -{ - // TODO: maybe we should assert - if (!running) return; - running = false; - - if (thread_created) pthread_join(thread, NULL); - if (display) XCloseDisplay(display); -} - -void *run(__attribute__((unused)) void *const arg) -{ - while (running) { - time_t curr_time; - time(&curr_time); - - const struct tm *const curr_tm = localtime(&curr_time); - - char buffer[256]; - strftime(buffer, sizeof(buffer), "%a, %e %b %Y, %H:%M:%S", curr_tm); - - XStoreName(display, DefaultRootWindow(display), buffer); - XSync(display, False); - - sleep(1); - } - - return NULL; -} diff --git a/datetime.h b/datetime.h deleted file mode 100644 index 86e35e9..0000000 --- a/datetime.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef _DATETIME_H -#define _DATETIME_H - -#include - -bool datetime_start(); -void datetime_stop(); - -#endif // _DATETIME_H diff --git a/main.c b/main.c index 98da2b6..ae4db95 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,4 @@ -#include "datetime.h" +#include "status.h" #include #include @@ -24,12 +24,12 @@ int main() exit(EXIT_FAILURE); } - datetime_start(); + status_start(); int wm_status; waitpid(wm_pid, &wm_status, 0); - datetime_stop(); + status_stop(); exit(WEXITSTATUS(wm_status)); } diff --git a/status.c b/status.c new file mode 100644 index 0000000..22a814a --- /dev/null +++ b/status.c @@ -0,0 +1,44 @@ +#include "status.h" + +#include +#include +#include +#include +#include +#include + +static bool running = false; +static pid_t pid = 0; + +bool status_start() +{ + // TODO: maybe we should assert + if (running) return false; + running = true; + + pid = fork(); + + if (pid == -1) { + status_stop(); + return false; + } + + if (pid == 0) { + char *const args[] = { "slstatus", NULL }; + execvp(args[0], args); + perror("polytree-session: slstatus exec"); + exit(EXIT_FAILURE); + } + + return true; +} + +void status_stop() +{ + // TODO: maybe we should assert + if (!running) return; + running = false; + + kill(pid, SIGKILL); + waitpid(pid, NULL, 0); +} diff --git a/status.h b/status.h new file mode 100644 index 0000000..0ee6d09 --- /dev/null +++ b/status.h @@ -0,0 +1,9 @@ +#ifndef _STATUS_H +#define _STATUS_H + +#include + +bool status_start(); +void status_stop(); + +#endif // _STATUS_H