diff --git a/Makefile b/Makefile index 858dbd8..8407c7b 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,8 @@ include config.mk -SRC = main.c +SRC = datetime.c main.c +HDR = datetime.h OBJ = $(SRC:.c=.o) all: options polytree-session @@ -16,7 +17,7 @@ options: %.o: %.c $(CC) -c $< -o $@ $(CFLAGS) -OBJ: config.mk +OBJ: config.mk $(HDR) polytree-session: $(OBJ) $(CC) -o $@ $(OBJ) $(LDFLAGS) diff --git a/datetime.c b/datetime.c new file mode 100644 index 0000000..9ba3888 --- /dev/null +++ b/datetime.c @@ -0,0 +1,61 @@ +#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 new file mode 100644 index 0000000..86e35e9 --- /dev/null +++ b/datetime.h @@ -0,0 +1,9 @@ +#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 b0d353b..98da2b6 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,5 @@ -#include +#include "datetime.h" + #include #include #include @@ -6,16 +7,6 @@ #include #include #include -#include - -static bool datetime_running = false; -static Display *datetime_display = NULL; -static pthread_t datetime_thread; -static bool datetime_thread_created = false; - -static bool datetime_start(); -static void datetime_stop(); -static void *datetime_run(void *arg); int main() { @@ -42,56 +33,3 @@ int main() exit(WEXITSTATUS(wm_status)); } - -bool datetime_start() -{ - // TODO: maybe we should assert - if (datetime_running) return false; - datetime_running = true; - - if ((datetime_display = XOpenDisplay(NULL)) == NULL) { - datetime_stop(); - return false; - } - - if (!(datetime_thread_created = - pthread_create(&datetime_thread, NULL, datetime_run, NULL) == 0)) - { - datetime_stop(); - return false; - } - - return true; -} - -void datetime_stop() -{ - // TODO: maybe we should assert - if (!datetime_running) return; - datetime_running = false; - - if (datetime_thread_created) pthread_join(datetime_thread, NULL); - if (datetime_display) XCloseDisplay(datetime_display); -} - -void *datetime_run(__attribute__((unused)) void *const arg) -{ - while (datetime_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(datetime_display, - DefaultRootWindow(datetime_display), - buffer); - XSync(datetime_display, False); - - sleep(1); - } - - return NULL; -}