diff --git a/config.mk b/config.mk index 4a8f635..bad2ce3 100644 --- a/config.mk +++ b/config.mk @@ -7,8 +7,14 @@ SYSPREFIX = /usr ICONSPREFIX = $(SYSPREFIX)/share/icons XSESSIONSPREFIX = $(SYSPREFIX)/share/xsessions -INCS = -LIBS = +X11INC = /usr/X11R6/include +X11LIB = /usr/X11R6/lib +# FreeBSD (uncomment) +#X11INC = /usr/local/include +#X11LIB = /usr/local/lib + +INCS = -I${X11INC} +LIBS = -L${X11LIB} -lX11 -lpthread CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L -DVERSION=\"$(VERSION)\" CFLAGS = -std=c99 -pedantic -Wall -Wextra -Os $(INCS) $(CPPFLAGS) diff --git a/main.c b/main.c index b84e168..b0d353b 100644 --- a/main.c +++ b/main.c @@ -1,11 +1,21 @@ +#include +#include #include #include #include #include #include #include +#include -#define WM_STARTUP_TIME_SEC 3 +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() { @@ -23,10 +33,65 @@ int main() exit(EXIT_FAILURE); } - for (unsigned int i = 0; i < WM_STARTUP_TIME_SEC; ++i) sleep(1); + datetime_start(); int wm_status; waitpid(wm_pid, &wm_status, 0); + datetime_stop(); + 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; +}