From 0aa697b13d18140defc473b32663a938e00e97f6 Mon Sep 17 00:00:00 2001 From: ache Date: Fri, 8 Feb 2019 06:26:44 +0100 Subject: Refesh from inotify (fuck yeah !) --- main.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- sms.c | 4 +++- sms.h | 4 +--- 3 files changed, 67 insertions(+), 14 deletions(-) diff --git a/main.c b/main.c index 1cab1f8..77981eb 100644 --- a/main.c +++ b/main.c @@ -1,29 +1,27 @@ #include "main.h" #include "readline.h" - #include #include #include #include -#include - - +#include +#include +#include +#include #include #include #include #include "wind.h" +#include "sms.h" /* * Entry point of mesms and event loop */ - #ifdef DEBUG - #include - #endif const int color[] = { @@ -31,8 +29,9 @@ const int color[] = { COLOR_GREEN, COLOR_YELLOW, COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE}; - #define MAX_PATH 1024 +#define EVENT_SIZE ( sizeof (struct inotify_event) ) +#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) ) extern char* msg_win_str; extern conversation* currentConv; @@ -185,12 +184,61 @@ int main(int argc, char* argv[]){ init_pair( i+1, i, COLOR_BLACK); } + // inotify stuff + int fd = inotify_init(), wd_in = -1, wd_out = -1; + char buffer[EVENT_BUF_LEN]; + char sms_filename[200]; + + /*checking for error*/ +#ifdef DEBUG + if ( fd < 0 ) { + fprintf( stderr, "inotify_init init error"); + } +#endif + + if( fd >= 0 ) { + fcntl (fd, F_SETFL, fcntl (fd, F_GETFL) | O_NONBLOCK); + if( *config.SMS_SENTBOX ) { + wd_out = inotify_add_watch( fd, config.SMS_SENTBOX, IN_MOVED_TO); + } + if( *config.SMS_INBOX ) { + wd_in = inotify_add_watch( fd, config.SMS_INBOX, IN_CREATE ); + // From SMS_OUTBOX to SMS_SENTBOX + } + } + /* Premier affichage */ resize(); int tabed = 0; /* Event loop */ while( c = getch() ) { + + int ret = read( fd, buffer, EVENT_BUF_LEN ); + int i = 0; + while( ret > 0 ) { +#ifdef DEBUG + fprintf(stderr, "%d\n", ret); +#endif + struct inotify_event *event = (struct inotify_event*) (buffer+i); + ret -= EVENT_SIZE + event->len; + i += EVENT_SIZE + event->len; + if ( event->len && (event->mask & IN_CREATE || event->mask & IN_MOVED_TO) && !( event->mask & IN_ISDIR ) ) { +#ifdef DEBUG + fprintf(stderr, "New SMS (%s)\n", event->name); +#endif + if( strncmp(event->name, "IN", 2) == 0 ) { + sprintf(sms_filename, "%s%s", config.SMS_INBOX, event->name); + fprintf(stderr, "%s\n", sms_filename); + readSMS(sms_filename, 0); + } else { + sprintf(sms_filename, "%s%s", config.SMS_SENTBOX, event->name); + fprintf(stderr, "%s\n", sms_filename); + readSMS(sms_filename, 1); + } + } + } + if (c == KEY_RESIZE) { resize(); refresh(); @@ -377,10 +425,15 @@ edit: } - refresh(); + // inotify stuff + if( wd_in >= 0 ) { + inotify_rm_watch( fd, wd_in ); + } + if( wd_out >= 0 ) { + inotify_rm_watch( fd, wd_out ); + } end: endwin(); - return 0; } diff --git a/sms.c b/sms.c index bf7fc72..df48780 100644 --- a/sms.c +++ b/sms.c @@ -157,8 +157,10 @@ int readSMS(char* filename, int inbox_outbox) { getSms.len = sms_len; int r = insertSMS(&getSms); - if( r ) + if( r ) { + fclose(file); return 2; + } fclose(file); }else{ diff --git a/sms.h b/sms.h index a1a4ea5..7ab9281 100644 --- a/sms.h +++ b/sms.h @@ -38,9 +38,7 @@ typedef struct conversation { size_t nbSMS; } conversation; - - - +int readSMS(char*, int); int listSMS(char*,int); int loadConv(void); -- cgit v1.2.3