1
0
Fork 0

Use slstatus

This commit is contained in:
Alex Kotov 2021-11-16 03:48:21 +05:00
parent 99230304c2
commit b008879c87
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
6 changed files with 58 additions and 75 deletions

View File

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

View File

@ -1,61 +0,0 @@
#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;
}

View File

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

6
main.c
View File

@ -1,4 +1,4 @@
#include "datetime.h"
#include "status.h"
#include <stdbool.h>
#include <stddef.h>
@ -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));
}

44
status.c Normal file
View File

@ -0,0 +1,44 @@
#include "status.h"
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
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);
}

9
status.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef _STATUS_H
#define _STATUS_H
#include <stdbool.h>
bool status_start();
void status_stop();
#endif // _STATUS_H