Move datetime to separate module

This commit is contained in:
Alex Kotov 2021-11-15 06:20:14 +05:00
parent c21fca1a72
commit 76eba38915
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
4 changed files with 75 additions and 66 deletions

View file

@ -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)

61
datetime.c Normal file
View file

@ -0,0 +1,61 @@
#include "datetime.h"
#include <pthread.h>
#include <unistd.h>
#include <X11/Xlib.h>
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;
}

9
datetime.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef _DATETIME_H
#define _DATETIME_H
#include <stdbool.h>
bool datetime_start();
void datetime_stop();
#endif // _DATETIME_H

66
main.c
View file

@ -1,4 +1,5 @@
#include <pthread.h>
#include "datetime.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
@ -6,16 +7,6 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <X11/Xlib.h>
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;
}