Compare commits
No commits in common. "778d75396ec25d531fd8e824d318c2429362cc4c" and "114c5f901fd23b44038cae12e2eb37a695328673" have entirely different histories.
778d75396e
...
114c5f901f
26
Makefile
26
Makefile
@ -1,5 +1,5 @@
|
|||||||
FLAGS = -g -Wall -Wextra -DLOG_USE_COLOR `pkg-config --cflags --libs libnotify`
|
FLAGS = -g -Wall -Wextra -DLOG_USE_COLOR `pkg-config --cflags --libs libnotify`
|
||||||
LD_FLAGS = -lsqlite3 -lpthread
|
LD_FLAGS = -lsqlite3
|
||||||
|
|
||||||
|
|
||||||
$(shell mkdir -p out)
|
$(shell mkdir -p out)
|
||||||
@ -17,27 +17,21 @@ out/%.o: src/main/%.c
|
|||||||
out/%.o: src/log/%.c
|
out/%.o: src/log/%.c
|
||||||
$(CC) -c $(FLAGS) $^ -o $@
|
$(CC) -c $(FLAGS) $^ -o $@
|
||||||
|
|
||||||
takl: out/main.o out/db.o out/tasks.o out/utils.o
|
takl: out/main.o out/db.o out/tasks.o
|
||||||
$(CC) $(FLAGS) $(LD_FLAGS) $^ -o $@
|
$(CC) $(FLAGS) $(LD_FLAGS) $^ -o $@
|
||||||
|
|
||||||
takl-daemon: out/daemon.o out/db.o out/tasks.o out/utils.o out/log.o
|
takl-daemon: out/daemon.o out/db.o out/tasks.o out/log.o
|
||||||
$(CC) $(FLAGS) $(LD_FLAGS) $^ -o $@
|
$(CC) $(FLAGS) $(LD_FLAGS) $^ -o $@
|
||||||
|
|
||||||
|
|
||||||
install: takl takl-daemon
|
install: takl
|
||||||
sudo cp takl takl-daemon /usr/bin/
|
sudo cp takl /usr/bin/takl
|
||||||
|
|
||||||
uninstall:
|
uninstall: takl
|
||||||
-sudo rm /usr/bin/takl
|
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:
|
clean:
|
||||||
-rm out -r
|
rm out -r
|
||||||
-rm ./takl
|
rm ./takl
|
||||||
-rm ./takl-daemon
|
rm ./takl-daemon
|
@ -15,11 +15,6 @@ make install # installation dans /usr/bin/takl
|
|||||||
|
|
||||||
Vous pouvez ensuite utiliser la commande `takl` pour ajouter/ modifier des items à la todo list.
|
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
|
### Utilisation
|
||||||
|
|
||||||
|
164
src/daemon.c
164
src/daemon.c
@ -1,172 +1,64 @@
|
|||||||
/*
|
|
||||||
* 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 <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <assert.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <signal.h>
|
#include <time.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/colors.h"
|
||||||
#include "main/include/config.h"
|
#include "main/include/config.h"
|
||||||
|
#include "main/include/tasks.h"
|
||||||
|
#include "main/include/db.h"
|
||||||
|
|
||||||
|
#include "log/log.h"
|
||||||
|
|
||||||
#define EVENT_SIZE ( sizeof (struct inotify_event) )
|
task_t next_task(task_list_t* tasks) {
|
||||||
#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
|
time_t now = time(0);
|
||||||
|
|
||||||
|
task_t next;
|
||||||
|
next.due_to = 0;
|
||||||
|
|
||||||
|
task_list_t* cur = tasks;
|
||||||
|
|
||||||
bool has_changed = false;
|
while (cur) {
|
||||||
task_list_t* tasks;
|
task_t t = cur->task;
|
||||||
pthread_mutex_t tasks_lock = PTHREAD_MUTEX_INITIALIZER; // locks tasks and has_changed at the same time
|
|
||||||
|
|
||||||
|
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) {
|
||||||
void* inotify_check_changes(void* arg) {
|
next = t;
|
||||||
(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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int wd = inotify_add_watch(fd, socket_path, IN_ACCESS);
|
cur = cur->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");
|
return next;
|
||||||
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() {
|
int main() {
|
||||||
bool notified_no_change = false;
|
|
||||||
|
|
||||||
if (!existing_takl_daemon()) {
|
|
||||||
log_info("TaKl " VERSION " -- Daemon started");
|
log_info("TaKl " VERSION " -- Daemon started");
|
||||||
} else {
|
|
||||||
log_info("TaKl Daemon déjà en cours d'exécution. Arrêt");
|
task_list_t* tasks = get_task_list(false);
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
task_t next = next_task(tasks);
|
||||||
|
|
||||||
|
if (next.due_to == 0) {
|
||||||
|
log_fatal("Plus de tâches avec échéance dans la liste");
|
||||||
|
free_task_list(tasks);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
|
||||||
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);
|
time_t now = time(0);
|
||||||
|
|
||||||
double wait = difftime(next.due_to, now) + 5;
|
double wait = difftime(next.due_to, now) + 5;
|
||||||
log_debug("Prochaine tâche dans %0.1lfs", wait);
|
log_debug("Attente de %0.1lfs", wait);
|
||||||
notified_no_change = false;
|
sleep(wait);
|
||||||
|
|
||||||
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);
|
log_debug("Envoi de la notification tâche [%d]", next.id);
|
||||||
desktop_notification(next);
|
desktop_notification(next);
|
||||||
}
|
|
||||||
//! si deux tâches sont à la même date à moins de 0.1s près,
|
//! si deux tâches sont à la même date à moins de 0.1s près,
|
||||||
//! l'une des deux seulement sera affichée
|
//! 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.
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pthread_join(inotify_id, NULL);
|
|
||||||
|
|
||||||
free_task_list(tasks);
|
free_task_list(tasks);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -11,8 +11,6 @@ Fonctions d'interaction avec la base de données
|
|||||||
#include "include/colors.h"
|
#include "include/colors.h"
|
||||||
#include "include/config.h"
|
#include "include/config.h"
|
||||||
#include "include/struct.h"
|
#include "include/struct.h"
|
||||||
#include "include/utils.h"
|
|
||||||
#include "include/db.h"
|
|
||||||
|
|
||||||
|
|
||||||
char* get_db_path() {
|
char* get_db_path() {
|
||||||
@ -77,17 +75,6 @@ sqlite3* get_db() {
|
|||||||
return 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) {
|
int add_task(task_t t) {
|
||||||
sqlite3* db = get_db();
|
sqlite3* db = get_db();
|
||||||
@ -118,7 +105,6 @@ int add_task(task_t t) {
|
|||||||
|
|
||||||
sqlite3_finalize(stmt);
|
sqlite3_finalize(stmt);
|
||||||
sqlite3_close(db);
|
sqlite3_close(db);
|
||||||
notify_change();
|
|
||||||
|
|
||||||
if (ret != SQLITE_DONE) {
|
if (ret != SQLITE_DONE) {
|
||||||
if (ret == 19) {
|
if (ret == 19) {
|
||||||
@ -161,7 +147,6 @@ int update_task(task_t t) {
|
|||||||
|
|
||||||
sqlite3_finalize(stmt);
|
sqlite3_finalize(stmt);
|
||||||
sqlite3_close(db);
|
sqlite3_close(db);
|
||||||
notify_change();
|
|
||||||
|
|
||||||
if (ret != SQLITE_DONE) {
|
if (ret != SQLITE_DONE) {
|
||||||
printf("Return value: %d\n", ret);
|
printf("Return value: %d\n", ret);
|
||||||
@ -190,7 +175,6 @@ void delete_task(int id) {
|
|||||||
|
|
||||||
sqlite3_finalize(stmt);
|
sqlite3_finalize(stmt);
|
||||||
sqlite3_close(db);
|
sqlite3_close(db);
|
||||||
notify_change();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,14 +1,11 @@
|
|||||||
#ifndef DEF_CONFIG_H
|
#ifndef DEF_CONFIG_H
|
||||||
#define DEF_CONFIG_H
|
#define DEF_CONFIG_H
|
||||||
|
|
||||||
#define VERSION "1.1.0"
|
#define VERSION "1.0.3"
|
||||||
|
|
||||||
// By default, $HOME/.config/takl.sqlite3 is used. You can change this behaviour here
|
// By default, $HOME/.config/takl.sqlite3 is used. You can change this behaviour here
|
||||||
//#define DB_FILE "takl.sqlite3"
|
//#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 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
|
#define NEW_TASK_ID_MAX_RETRIES 10000 // number of retries before giving up
|
||||||
|
|
||||||
|
@ -9,11 +9,6 @@ Arrête le programme en cas d'échec
|
|||||||
*/
|
*/
|
||||||
void create_db();
|
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.
|
Ajouter une tâche à la base de données.
|
||||||
Le numéro de tâche ne doit pas déjà être utilisé
|
Le numéro de tâche ne doit pas déjà être utilisé
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
#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);
|
|
@ -1,51 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
[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