Compare commits
3 Commits
114c5f901f
...
778d75396e
Author | SHA1 | Date | |
---|---|---|---|
778d75396e | |||
162c59c7da | |||
c5b616f5e7 |
26
Makefile
26
Makefile
@ -1,5 +1,5 @@
|
||||
FLAGS = -g -Wall -Wextra -DLOG_USE_COLOR `pkg-config --cflags --libs libnotify`
|
||||
LD_FLAGS = -lsqlite3
|
||||
LD_FLAGS = -lsqlite3 -lpthread
|
||||
|
||||
|
||||
$(shell mkdir -p out)
|
||||
@ -17,21 +17,27 @@ out/%.o: src/main/%.c
|
||||
out/%.o: src/log/%.c
|
||||
$(CC) -c $(FLAGS) $^ -o $@
|
||||
|
||||
takl: out/main.o out/db.o out/tasks.o
|
||||
takl: out/main.o out/db.o out/tasks.o out/utils.o
|
||||
$(CC) $(FLAGS) $(LD_FLAGS) $^ -o $@
|
||||
|
||||
takl-daemon: out/daemon.o out/db.o out/tasks.o out/log.o
|
||||
takl-daemon: out/daemon.o out/db.o out/tasks.o out/utils.o out/log.o
|
||||
$(CC) $(FLAGS) $(LD_FLAGS) $^ -o $@
|
||||
|
||||
|
||||
install: takl
|
||||
sudo cp takl /usr/bin/takl
|
||||
install: takl takl-daemon
|
||||
sudo cp takl takl-daemon /usr/bin/
|
||||
|
||||
uninstall: takl
|
||||
sudo rm /usr/bin/takl
|
||||
uninstall:
|
||||
-sudo rm /usr/bin/takl
|
||||
-sudo rm /usr/bin/takl-daemon
|
||||
-sudo rm ~/.config/autostart/takl-daemon.desktop
|
||||
|
||||
gnome-install: install
|
||||
mkdir -p ~/.config/autostart
|
||||
cp takl-daemon.desktop ~/.config/autostart/
|
||||
|
||||
|
||||
clean:
|
||||
rm out -r
|
||||
rm ./takl
|
||||
rm ./takl-daemon
|
||||
-rm out -r
|
||||
-rm ./takl
|
||||
-rm ./takl-daemon
|
@ -15,6 +15,11 @@ make install # installation dans /usr/bin/takl
|
||||
|
||||
Vous pouvez ensuite utiliser la commande `takl` pour ajouter/ modifier des items à la todo list.
|
||||
|
||||
Pour recevoir des notifications lorsqu'une tâche arrive à échéance, il faut lancer `takl-daemon` en arrière-plan
|
||||
dans un environnement similaire à l'utilisateur (Un service systemd ou une tâche cron auront besoin de plus
|
||||
d'informations pour pouvoir envoyer des notifications).
|
||||
Pour gnome, `make gnome-install` lancera le programme à tous les prochains redémarrages.
|
||||
|
||||
|
||||
### Utilisation
|
||||
|
||||
|
178
src/daemon.c
178
src/daemon.c
@ -1,65 +1,173 @@
|
||||
/*
|
||||
* Petit daemon qui va envoyer des notifications aux horaires des tâches
|
||||
* il vérifie si la base de données change par l'intermédiaire d'un socket,
|
||||
* (implémenté par un fichier stocké dans /tmp/takl.$USER), qui contient le pid
|
||||
* de ce daemon afin de pouvoir déterminer si un autre daemon tourne.
|
||||
* (Le cas où deux daemons sont lancés en même temps n'est pas traité.)
|
||||
*/
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "main/include/colors.h"
|
||||
#include "main/include/config.h"
|
||||
#include "main/include/tasks.h"
|
||||
#include "main/include/db.h"
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/types.h>
|
||||
#include <linux/inotify.h>
|
||||
|
||||
#include "log/log.h"
|
||||
#include "main/include/db.h"
|
||||
#include "main/include/tasks.h"
|
||||
#include "main/include/utils.h"
|
||||
#include "main/include/colors.h"
|
||||
#include "main/include/config.h"
|
||||
|
||||
task_t next_task(task_list_t* tasks) {
|
||||
time_t now = time(0);
|
||||
|
||||
task_t next;
|
||||
next.due_to = 0;
|
||||
#define EVENT_SIZE ( sizeof (struct inotify_event) )
|
||||
#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
|
||||
|
||||
task_list_t* cur = tasks;
|
||||
|
||||
while (cur) {
|
||||
task_t t = cur->task;
|
||||
|
||||
if (!t.done && (difftime(now, t.due_to) <= 0 || t.due_to == 0)) { // La tâche a une échéance, qui n'est pas passée
|
||||
if (next.due_to == 0 || difftime(next.due_to, t.due_to) >= 0) {
|
||||
next = t;
|
||||
}
|
||||
bool has_changed = false;
|
||||
task_list_t* tasks;
|
||||
pthread_mutex_t tasks_lock = PTHREAD_MUTEX_INITIALIZER; // locks tasks and has_changed at the same time
|
||||
|
||||
|
||||
|
||||
void* inotify_check_changes(void* arg) {
|
||||
(void)arg;
|
||||
char buffer[EVENT_BUF_LEN];
|
||||
char* socket_path = get_socket_path();
|
||||
|
||||
while (1) { // Regarde si /tmp/takl.$USER a changé (donc si la BDD a changé et doit être rechargée)
|
||||
int fd = inotify_init();
|
||||
if (fd < 0) {
|
||||
log_fatal("Impossible d'initialiser inotify");
|
||||
free(socket_path);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
cur = cur->next;
|
||||
}
|
||||
int wd = inotify_add_watch(fd, socket_path, IN_ACCESS);
|
||||
|
||||
return next;
|
||||
int length = read(fd, buffer, EVENT_BUF_LEN);
|
||||
if (length < 0) {
|
||||
log_fatal("inotify: lecture impossible");
|
||||
free(socket_path);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
log_debug("Base de données changée");
|
||||
pthread_mutex_lock(&tasks_lock);
|
||||
free_task_list(tasks);
|
||||
tasks = get_task_list(false);
|
||||
has_changed = true;
|
||||
pthread_mutex_unlock(&tasks_lock);
|
||||
|
||||
inotify_rm_watch(fd, wd);
|
||||
close(fd);
|
||||
}
|
||||
free(socket_path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int existing_takl_daemon() {
|
||||
char* socket_path = get_socket_path();
|
||||
|
||||
if (!access(socket_path, F_OK)) { // tmp file exists
|
||||
FILE* fp = fopen(socket_path, "r+");
|
||||
int daemon_pid;
|
||||
fscanf(fp, "%d", &daemon_pid);
|
||||
fclose(fp);
|
||||
|
||||
if(!kill(daemon_pid, 0)) {
|
||||
free(socket_path);
|
||||
return true; // The process is running
|
||||
}
|
||||
}
|
||||
FILE* fp = fopen(socket_path, "w");
|
||||
fprintf(fp, "%d", getpid());
|
||||
fclose(fp);
|
||||
|
||||
free(socket_path);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
log_info("TaKl " VERSION " -- Daemon started");
|
||||
bool notified_no_change = false;
|
||||
|
||||
if (!existing_takl_daemon()) {
|
||||
log_info("TaKl " VERSION " -- Daemon started");
|
||||
} else {
|
||||
log_info("TaKl Daemon déjà en cours d'exécution. Arrêt");
|
||||
return 1;
|
||||
}
|
||||
|
||||
task_list_t* tasks = get_task_list(false);
|
||||
tasks = get_task_list(false);
|
||||
|
||||
pthread_t inotify_id = 0; // Lancement d'inotify dans un autre fil
|
||||
pthread_create(&inotify_id, NULL, inotify_check_changes, NULL);
|
||||
|
||||
while (1) {
|
||||
pthread_mutex_lock(&tasks_lock);
|
||||
task_t next = next_task(tasks);
|
||||
pthread_mutex_unlock(&tasks_lock);
|
||||
|
||||
if (next.due_to == 0) {
|
||||
log_fatal("Plus de tâches avec échéance dans la liste");
|
||||
free_task_list(tasks);
|
||||
return 1;
|
||||
if (!notified_no_change)
|
||||
log_debug("Plus de tâches avec échéance dans la liste");
|
||||
notified_no_change = true;
|
||||
|
||||
while (1) { // sleep until change
|
||||
sleep(5);
|
||||
|
||||
pthread_mutex_lock(&tasks_lock);
|
||||
if (has_changed) {
|
||||
log_debug("Changement détecté");
|
||||
has_changed = false;
|
||||
pthread_mutex_unlock(&tasks_lock);
|
||||
|
||||
break;
|
||||
}
|
||||
pthread_mutex_unlock(&tasks_lock);
|
||||
}
|
||||
} else {
|
||||
time_t now = time(0);
|
||||
|
||||
double wait = difftime(next.due_to, now) + 5;
|
||||
log_debug("Prochaine tâche dans %0.1lfs", wait);
|
||||
notified_no_change = false;
|
||||
|
||||
while (difftime(next.due_to, now) >= 5) {
|
||||
sleep(5);
|
||||
pthread_mutex_lock(&tasks_lock);
|
||||
if (has_changed) {
|
||||
log_debug("Changement détecté");
|
||||
has_changed = false;
|
||||
pthread_mutex_unlock(&tasks_lock);
|
||||
|
||||
break;
|
||||
}
|
||||
pthread_mutex_unlock(&tasks_lock);
|
||||
|
||||
now = time(0);
|
||||
}
|
||||
|
||||
if (difftime(next.due_to, now) < 5) { // Exit not du e to the break statement
|
||||
log_debug("Envoi de la notification tâche [%d]", next.id);
|
||||
desktop_notification(next);
|
||||
}
|
||||
//! si deux tâches sont à la même date à moins de 0.1s près,
|
||||
//! l'une des deux seulement sera affichée
|
||||
|
||||
sleep(3); // Attendre un peu pour ne pas reconsidérer la même tâche 1063 fois.
|
||||
}
|
||||
|
||||
time_t now = time(0);
|
||||
double wait = difftime(next.due_to, now) + 5;
|
||||
log_debug("Attente de %0.1lfs", wait);
|
||||
sleep(wait);
|
||||
|
||||
log_debug("Envoi de la notification tâche [%d]", next.id);
|
||||
desktop_notification(next);
|
||||
//! si deux tâches sont à la même date à moins de 0.1s près,
|
||||
//! l'une des deux seulement sera affichée
|
||||
}
|
||||
|
||||
pthread_join(inotify_id, NULL);
|
||||
|
||||
free_task_list(tasks);
|
||||
return 0;
|
||||
}
|
@ -11,6 +11,8 @@ Fonctions d'interaction avec la base de données
|
||||
#include "include/colors.h"
|
||||
#include "include/config.h"
|
||||
#include "include/struct.h"
|
||||
#include "include/utils.h"
|
||||
#include "include/db.h"
|
||||
|
||||
|
||||
char* get_db_path() {
|
||||
@ -75,6 +77,17 @@ sqlite3* get_db() {
|
||||
return db;
|
||||
}
|
||||
|
||||
void notify_change() {
|
||||
char* socket_path = get_socket_path();
|
||||
FILE* fp = fopen(socket_path, "r");
|
||||
|
||||
int pid;
|
||||
fscanf(fp, "%d", &pid); // Trigger IN_ACCESS
|
||||
(void)pid;
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
|
||||
int add_task(task_t t) {
|
||||
sqlite3* db = get_db();
|
||||
@ -105,6 +118,7 @@ int add_task(task_t t) {
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
sqlite3_close(db);
|
||||
notify_change();
|
||||
|
||||
if (ret != SQLITE_DONE) {
|
||||
if (ret == 19) {
|
||||
@ -147,6 +161,7 @@ int update_task(task_t t) {
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
sqlite3_close(db);
|
||||
notify_change();
|
||||
|
||||
if (ret != SQLITE_DONE) {
|
||||
printf("Return value: %d\n", ret);
|
||||
@ -175,6 +190,7 @@ void delete_task(int id) {
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
sqlite3_close(db);
|
||||
notify_change();
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,11 +1,14 @@
|
||||
#ifndef DEF_CONFIG_H
|
||||
#define DEF_CONFIG_H
|
||||
|
||||
#define VERSION "1.0.3"
|
||||
#define VERSION "1.1.0"
|
||||
|
||||
// By default, $HOME/.config/takl.sqlite3 is used. You can change this behaviour here
|
||||
//#define DB_FILE "takl.sqlite3"
|
||||
|
||||
// By default, /tmp/takl.$USER is used. You can change this behaviour here
|
||||
//#define SOCKET_FILE "takl.sock"
|
||||
|
||||
#define MAX_TASK_ID 10000 // max is set to MAX_TASK_ID-1
|
||||
#define NEW_TASK_ID_MAX_RETRIES 10000 // number of retries before giving up
|
||||
|
||||
|
@ -9,6 +9,11 @@ Arrête le programme en cas d'échec
|
||||
*/
|
||||
void create_db();
|
||||
|
||||
/*
|
||||
Open/closes the socket file to notify of db changes
|
||||
*/
|
||||
void notify_change();
|
||||
|
||||
/*
|
||||
Ajouter une tâche à la base de données.
|
||||
Le numéro de tâche ne doit pas déjà être utilisé
|
||||
|
14
src/main/include/utils.h
Normal file
14
src/main/include/utils.h
Normal file
@ -0,0 +1,14 @@
|
||||
#include <pthread.h>
|
||||
|
||||
#include "struct.h"
|
||||
|
||||
|
||||
/*
|
||||
Renvoie le chemin d'accès au socket (/tmp/takl.$USER sauf si défini différemment dans la config)
|
||||
*/
|
||||
char* get_socket_path();
|
||||
|
||||
/*
|
||||
Renvoie l'élément arrivant dans le moins de temps d'une liste de tâches.
|
||||
*/
|
||||
task_t next_task(task_list_t* tasks);
|
51
src/main/utils.c
Normal file
51
src/main/utils.c
Normal file
@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "include/config.h"
|
||||
#include "include/struct.h"
|
||||
|
||||
char* get_socket_path() {
|
||||
#ifndef SOCKET_FILE
|
||||
char* base_path = "/tmp/takl";
|
||||
|
||||
char* username = getenv("USER");
|
||||
assert(username != NULL);
|
||||
|
||||
char* socket_path = malloc(sizeof(char)*(strlen(base_path)+strlen(username)+1));
|
||||
sprintf(socket_path, "%s.%s", base_path, username);
|
||||
|
||||
return socket_path;
|
||||
#else
|
||||
char* socket_path = malloc(sizeof(char)*(strlen(SOCKET_FILE)+1));
|
||||
memcpy(socket_path, SOCKET_FILE, sizeof(char)*(strlen(SOCKET_FILE)+1));
|
||||
|
||||
return socket_path;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
task_t next_task(task_list_t* tasks) {
|
||||
time_t now = time(0);
|
||||
|
||||
task_t next;
|
||||
next.due_to = 0;
|
||||
|
||||
task_list_t* cur = tasks;
|
||||
|
||||
while (cur) {
|
||||
task_t t = cur->task;
|
||||
|
||||
if (!t.done && (difftime(now, t.due_to) <= 0 || t.due_to == 0)) { // La tâche a une échéance, qui n'est pas passée
|
||||
if (next.due_to == 0 || difftime(next.due_to, t.due_to) >= 0) {
|
||||
next = t;
|
||||
}
|
||||
}
|
||||
|
||||
cur = cur->next;
|
||||
}
|
||||
|
||||
return next;
|
||||
}
|
7
takl-daemon.desktop
Normal file
7
takl-daemon.desktop
Normal file
@ -0,0 +1,7 @@
|
||||
[Desktop Entry]
|
||||
Type=Application
|
||||
Exec=/usr/bin/takl-daemon
|
||||
Hidden=false
|
||||
Terminal=false
|
||||
X-GNOME-Autostart-enabled=true
|
||||
Name=TaKl Notifier Daemon
|
Loading…
Reference in New Issue
Block a user