Set datetime
This commit is contained in:
parent
a26b6e2b33
commit
c21fca1a72
2 changed files with 75 additions and 4 deletions
10
config.mk
10
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)
|
||||
|
|
69
main.c
69
main.c
|
@ -1,11 +1,21 @@
|
|||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <X11/Xlib.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue