Improve datetime service

This commit is contained in:
Alex Kotov 2021-11-12 14:09:03 +05:00
parent b6b15a328c
commit b17580b4ae
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 45 additions and 30 deletions

View File

@ -14,34 +14,63 @@ static void *run(void *vargp);
static const char *const default_text = "date & time";
static pthread_mutex_t mutex;
static bool running = false;
static Display *display = None;
static Atoms atoms = NULL;
static bool running = true;
static pthread_t thread;
static pthread_mutex_t mutex;
static bool thread_created = false;
static char buffer[BUFFER_SIZE];
void datetime_lock()
{
pthread_mutex_lock(&mutex);
}
void datetime_unlock()
{
pthread_mutex_unlock(&mutex);
}
const char *datetime_get()
{
return buffer;
}
bool datetime_start()
{
datetime_lock();
if (running) return false;
running = true;
strcpy(buffer, default_text);
if ((display = XOpenDisplay(NULL)) == NULL) {
return false;
}
if ((display = XOpenDisplay(NULL)) == NULL) return false;
if ((atoms = atoms_create(display)) == NULL) return false;
atoms = atoms_create(display);
thread_created = pthread_create(&thread, NULL, run, NULL) == 0;
pthread_create(&thread, NULL, run, NULL);
datetime_unlock();
return true;
return thread_created;
}
void datetime_stop()
{
datetime_lock();
if (!running) return;
running = false;
atoms_destroy(atoms);
XCloseDisplay(display);
pthread_join(thread, NULL);
if (thread_created) pthread_join(thread, NULL);
if (atoms != NULL) atoms_destroy(atoms);
if (display != None) XCloseDisplay(display);
datetime_unlock();
}
void *run(void *vargp)
@ -79,18 +108,3 @@ void *run(void *vargp)
return NULL;
}
void datetime_lock()
{
pthread_mutex_lock(&mutex);
}
const char *datetime_get()
{
return buffer;
}
void datetime_unlock()
{
pthread_mutex_unlock(&mutex);
}

View File

@ -5,9 +5,8 @@
bool datetime_start();
void datetime_stop();
void datetime_lock();
const char *datetime_get();
void datetime_unlock();
const char *datetime_get();
#endif // _DATETIME_H

6
dwm.c
View File

@ -2482,8 +2482,10 @@ main(int argc, char *argv[])
if (!(dpy = XOpenDisplay(NULL)))
die("dwm: cannot open display");
checkotherwm();
if (!datetime_start())
if (!datetime_start()) {
datetime_stop();
die("dwm: cannot start datetime service");
}
setup();
#ifdef __OpenBSD__
if (pledge("stdio rpath proc exec", NULL) == -1)
@ -2491,8 +2493,8 @@ main(int argc, char *argv[])
#endif /* __OpenBSD__ */
scan();
run();
datetime_stop();
cleanup();
datetime_stop();
XCloseDisplay(dpy);
return EXIT_SUCCESS;
}