More work on implementing the daemon.

This commit is contained in:
Frederic Culot 2009-08-01 13:31:20 +00:00
parent 5181d90b01
commit 71d6cfda92
6 changed files with 174 additions and 94 deletions

View File

@ -1,3 +1,11 @@
2009-08-01 Frederic Culot <frederic@culot.org>
* src/vars.c: new variable to store daemon configuration
* src/notify.c (print_option, notify_app_txt): new functions
notification configuration menu updated with daemon-related
variables
2009-07-29 Frederic Culot <frederic@culot.org> 2009-07-29 Frederic Culot <frederic@culot.org>
* src/custom.c (custom_load_conf): use a larger buffer to read * src/custom.c (custom_load_conf): use a larger buffer to read
@ -6,7 +14,11 @@
* src/io.c (io_extract_data): do not read past the maximum length * src/io.c (io_extract_data): do not read past the maximum length
* src/dmon.c (dmon_start): avoid multiple notifications of the * src/dmon.c (dmon_start): avoid multiple notifications of the
same appointment same appointment, and logs added
* src/notify.c (notify_launch_cmd): return an error code
* src/notify.c (notify_get_next_bckg): new function
2009-07-27 Frederic Culot <frederic@culot.org> 2009-07-27 Frederic Culot <frederic@culot.org>

View File

@ -1,4 +1,4 @@
/* $calcurse: dmon.c,v 1.8 2009/07/29 19:11:58 culot Exp $ */ /* $calcurse: dmon.c,v 1.9 2009/08/01 13:31:20 culot Exp $ */
/* /*
* Calcurse - text-based organizer * Calcurse - text-based organizer
@ -161,7 +161,6 @@ daemonize (int status)
void void
dmon_start (int parent_exit_status) dmon_start (int parent_exit_status)
{ {
struct notify_app_s next;
conf_t conf; conf_t conf;
if (!daemonize (parent_exit_status)) if (!daemonize (parent_exit_status))
@ -181,31 +180,23 @@ dmon_start (int parent_exit_status)
apoint_llist_init (); apoint_llist_init ();
recur_apoint_llist_init (); recur_apoint_llist_init ();
io_load_app (); io_load_app ();
data_loaded = 1; data_loaded = 1;
next.got_app = 0;
for (;;) for (;;)
{ {
if (!next.got_app) int left;
(void)notify_get_next (&next);
if (next.got_app) if (!notify_get_next_bkgd ())
DMON_ABRT (_("error loading next appointment\n"));
left = notify_time_left ();
if (left < nbar.cntdwn)
{ {
int left; DMON_LOG (_("launching notification at %s for: \"%s\"\n"),
nowstr (), notify_app_txt ());
notify_update_app (next.time, next.state, next.txt); if (!notify_launch_cmd ())
left = notify_time_left (); DMON_LOG (_("error while sending notification\n"));
if (left < nbar.cntdwn)
{
notify_launch_cmd ();
next.got_app = 0;
}
} }
if (next.txt)
mem_free (next.txt);
DMON_LOG (_("sleeping at %s for %d seconds\n"), nowstr (), DMON_LOG (_("sleeping at %s for %d seconds\n"), nowstr (),
DMON_SLEEP_TIME); DMON_SLEEP_TIME);
psleep (DMON_SLEEP_TIME); psleep (DMON_SLEEP_TIME);

View File

@ -1,4 +1,4 @@
/* $calcurse: notify.c,v 1.41 2009/07/20 19:45:26 culot Exp $ */ /* $calcurse: notify.c,v 1.42 2009/08/01 13:31:20 culot Exp $ */
/* /*
* Calcurse - text-based organizer * Calcurse - text-based organizer
@ -189,20 +189,23 @@ notify_reinit_bar (void)
} }
/* Launch user defined command as a notification. */ /* Launch user defined command as a notification. */
void unsigned
notify_launch_cmd (void) notify_launch_cmd (void)
{ {
int pid; int pid;
if (notify_app.state & APOINT_NOTIFIED) if (notify_app.state & APOINT_NOTIFIED)
return; return 1;
notify_app.state |= APOINT_NOTIFIED; notify_app.state |= APOINT_NOTIFIED;
pid = fork (); pid = fork ();
if (pid < 0) if (pid < 0)
ERROR_MSG (_("error while launching command: could not fork")); {
ERROR_MSG (_("error while launching command: could not fork"));
return 0;
}
else if (pid == 0) else if (pid == 0)
{ {
/* Child: launch user defined command */ /* Child: launch user defined command */
@ -213,6 +216,8 @@ notify_launch_cmd (void)
} }
_exit (0); _exit (0);
} }
return 1;
} }
/* /*
@ -276,7 +281,7 @@ notify_update_bar (void)
wattroff (notify.win, A_BLINK); wattroff (notify.win, A_BLINK);
if (blinking) if (blinking)
notify_launch_cmd (); (void)notify_launch_cmd ();
pthread_mutex_unlock (&nbar.mutex); pthread_mutex_unlock (&nbar.mutex);
} }
else else
@ -358,6 +363,32 @@ notify_get_next (struct notify_app_s *a)
return 1; return 1;
} }
unsigned
notify_get_next_bkgd (void)
{
struct notify_app_s a;
if (!notify_app.got_app)
{
if (!notify_get_next (&a))
return 0;
if (a.got_app)
notify_update_app (a.time, a.state, a.txt);
}
return 1;
}
/* Return the description of next appointment to be notified. */
char *
notify_app_txt (void)
{
if (notify_app.got_app)
return notify_app.txt;
else
return (char *)0;
}
/* Look for the next appointment within the next 24 hours. */ /* Look for the next appointment within the next 24 hours. */
/* ARGSUSED0 */ /* ARGSUSED0 */
static void * static void *
@ -501,88 +532,114 @@ notify_start_main_thread (void)
notify_check_next_app (); notify_check_next_app ();
} }
/*
* Print an option in the configuration menu.
* Specific treatment is needed depending on if the option is of type boolean
* (either YES or NO), or an option holding a string value.
*/
static void
print_option (WINDOW *win, unsigned x, unsigned y, char *name,
char *valstr, unsigned valbool, char *desc, unsigned num)
{
const int XOFF = 4;
const int MAXCOL = col - 2;
int x_opt, len;
x_opt = x + XOFF + strlen (name);
mvwprintw (win, y, x, "[%u] %s", num, name);
erase_window_part (win, x_opt, y, MAXCOL, y);
if ((len = strlen (valstr)) != 0)
{
unsigned maxlen;
maxlen = MAXCOL - x_opt - 2;
custom_apply_attr (win, ATTR_HIGHEST);
if (len < maxlen)
mvwprintw (win, y, x_opt, "%s", valstr);
else
{
char buf[BUFSIZ];
(void)strncpy (buf, valstr, maxlen - 1);
buf[maxlen - 1] = '\0';
mvwprintw (win, y, x_opt, "%s...", buf);
}
custom_remove_attr (win, ATTR_HIGHEST);
}
else
print_bool_option_incolor (win, valbool, y, x_opt);
mvwprintw (win, y + 1, x, desc);
}
/* Print options related to the notify-bar. */ /* Print options related to the notify-bar. */
static void static void
notify_print_options (WINDOW *optwin, int col) notify_print_options (WINDOW *optwin, int col)
{ {
const int XORIG = 3;
const int YORIG = 4;
const int YOFF = 3;
enum enum
{ SHOW, DATE, CLOCK, WARN, CMD, NB_OPT }; { SHOW, DATE, CLOCK, WARN, CMD, DMON, DMON_LOG, NB_OPT };
struct opt_s struct opt_s
{ {
char name[BUFSIZ]; char *name;
char desc[BUFSIZ]; char *desc;
char value[BUFSIZ]; char valstr[BUFSIZ];
} unsigned valnum;
opt[NB_OPT]; } opt[NB_OPT];
int i, y, x, l, x_pos, y_pos, x_offset, y_offset, maxcol, maxlen; int i;
char buf[BUFSIZ];
x_pos = 3; opt[SHOW].name = _("notify-bar_show = ");
x_offset = 4; opt[SHOW].desc = _("(if set to YES, notify-bar will be displayed)");
y_pos = 4;
y_offset = 3; opt[DATE].name = _("notify-bar_date = ");
maxcol = col - 2; opt[DATE].desc = _("(Format of the date to be displayed inside notify-bar)");
(void)strncpy (opt[SHOW].name, _("notify-bar_show = "), BUFSIZ); opt[CLOCK].name = _("notify-bar_clock = ");
(void)strncpy (opt[DATE].name, _("notify-bar_date = "), BUFSIZ); opt[CLOCK].desc = _("(Format of the time to be displayed inside notify-bar)");
(void)strncpy (opt[CLOCK].name, _("notify-bar_clock = "), BUFSIZ);
(void)strncpy (opt[WARN].name, _("notify-bar_warning = "), BUFSIZ); opt[WARN].name = _("notify-bar_warning = ");
(void)strncpy (opt[CMD].name, _("notify-bar_command = "), BUFSIZ); opt[WARN].desc = _("(Warn user if an appointment is within next "
"'notify-bar_warning' seconds)");
(void)strncpy (opt[SHOW].desc,
_("(if set to YES, notify-bar will be displayed)"), BUFSIZ); opt[CMD].name = _("notify-bar_command = ");
(void)strncpy (opt[DATE].desc, opt[CMD].desc = _("(Command used to notify user of an upcoming appointment)");
_("(Format of the date to be displayed inside notify-bar)"),
BUFSIZ); opt[DMON].name = _("notify-daemon_enable = ");
(void)strncpy (opt[CLOCK].desc, opt[DMON].desc = _("(Run in background to get notifications after exiting)");
_("(Format of the time to be displayed inside notify-bar)"),
BUFSIZ); opt[DMON_LOG].name = _("notify-daemon_log = ");
(void)strncpy (opt[WARN].desc, opt[DMON_LOG].desc = _("(Log activity when running in background)");
_("(Warn user if an appointment is within next "
"'notify-bar_warning' seconds)"), BUFSIZ);
(void)strncpy (opt[CMD].desc,
_("(Command used to notify user of an upcoming appointment)"),
BUFSIZ);
pthread_mutex_lock (&nbar.mutex); pthread_mutex_lock (&nbar.mutex);
(void)strncpy (opt[DATE].value, nbar.datefmt, BUFSIZ); /* String value options */
(void)strncpy (opt[CLOCK].value, nbar.timefmt, BUFSIZ); (void)strncpy (opt[DATE].valstr, nbar.datefmt, BUFSIZ);
(void)snprintf (opt[WARN].value, BUFSIZ, "%d", nbar.cntdwn); (void)strncpy (opt[CLOCK].valstr, nbar.timefmt, BUFSIZ);
(void)strncpy (opt[CMD].value, nbar.cmd, BUFSIZ); (void)snprintf (opt[WARN].valstr, BUFSIZ, "%d", nbar.cntdwn);
(void)strncpy (opt[CMD].valstr, nbar.cmd, BUFSIZ);
l = strlen (opt[SHOW].name); /* Boolean options */
x = x_pos + x_offset + l; opt[SHOW].valnum = nbar.show;
mvwprintw (optwin, y_pos, x_pos, "[1] %s", opt[SHOW].name); pthread_mutex_unlock (&nbar.mutex);
erase_window_part (optwin, x, y_pos, maxcol, y_pos);
print_bool_option_incolor (optwin, nbar.show, y_pos, x); opt[DMON].valnum = dmon.enable;
mvwprintw (optwin, y_pos + 1, x_pos, opt[SHOW].desc); opt[DMON_LOG].valnum = dmon.log;
for (i = 1; i < NB_OPT; i++) opt[SHOW].valstr[0] = opt[DMON].valstr[0] = opt[DMON_LOG].valstr[0] = '\0';
for (i = 0; i < NB_OPT; i++)
{ {
l = strlen (opt[i].name); int y;
y = y_pos + i * y_offset;
x = x_pos + x_offset + l; y = YORIG + i * YOFF;
maxlen = maxcol - x - 2; print_option (optwin, XORIG, y, opt[i].name, opt[i].valstr,
opt[i].valnum, opt[i].desc, i + 1);
mvwprintw (optwin, y, x_pos, "[%d] %s", i + 1, opt[i].name);
erase_window_part (optwin, x, y, maxcol, y);
custom_apply_attr (optwin, ATTR_HIGHEST);
if (strlen (opt[i].value) < maxlen)
mvwprintw (optwin, y, x, "%s", opt[i].value);
else
{
(void)strncpy (buf, opt[i].value, maxlen - 1);
buf[maxlen - 1] = '\0';
mvwprintw (optwin, y, x, "%s...", buf);
}
custom_remove_attr (optwin, ATTR_HIGHEST);
mvwprintw (optwin, y + 1, x_pos, opt[i].desc);
} }
pthread_mutex_unlock (&nbar.mutex);
wmove (win[STA].p, 1, 0); wmove (win[STA].p, 1, 0);
wnoutrefresh (optwin); wnoutrefresh (optwin);
doupdate (); doupdate ();
@ -693,6 +750,14 @@ notify_config_bar (void)
} }
change_win = 0; change_win = 0;
break; break;
case '6':
dmon.enable = !dmon.enable;
change_win = 1;
break;
case '7':
dmon.log = !dmon.log;
change_win = 1;
break;
} }
} }
mem_free (buf); mem_free (buf);

View File

@ -1,4 +1,4 @@
/* $calcurse: notify.h,v 1.19 2009/07/20 19:45:26 culot Exp $ */ /* $calcurse: notify.h,v 1.20 2009/08/01 13:31:21 culot Exp $ */
/* /*
* Calcurse - text-based organizer * Calcurse - text-based organizer
@ -72,9 +72,11 @@ void notify_free_app (void);
void notify_start_main_thread (void); void notify_start_main_thread (void);
void notify_stop_main_thread (void); void notify_stop_main_thread (void);
void notify_reinit_bar (void); void notify_reinit_bar (void);
void notify_launch_cmd (void); unsigned notify_launch_cmd (void);
void notify_update_bar (void); void notify_update_bar (void);
unsigned notify_get_next (struct notify_app_s *); unsigned notify_get_next (struct notify_app_s *);
unsigned notify_get_next_bkgd (void);
char *notify_app_txt (void);
void notify_check_next_app (void); void notify_check_next_app (void);
void notify_check_added (char *, long, char); void notify_check_added (char *, long, char);
void notify_check_repeated (recur_apoint_llist_node_t *); void notify_check_repeated (recur_apoint_llist_node_t *);

View File

@ -1,4 +1,4 @@
/* $calcurse: vars.c,v 1.19 2009/07/26 12:47:16 culot Exp $ */ /* $calcurse: vars.c,v 1.20 2009/08/01 13:31:21 culot Exp $ */
/* /*
* Calcurse - text-based organizer * Calcurse - text-based organizer
@ -112,6 +112,9 @@ struct pad_s apad;
/* Variable to store notify-bar settings. */ /* Variable to store notify-bar settings. */
struct nbar_s nbar; struct nbar_s nbar;
/* Variable to store daemon configuration. */
struct dmon_conf_s dmon;
/* /*
* Variables init * Variables init
*/ */

View File

@ -1,4 +1,4 @@
/* $calcurse: vars.h,v 1.36 2009/07/26 12:47:17 culot Exp $ */ /* $calcurse: vars.h,v 1.37 2009/08/01 13:31:21 culot Exp $ */
/* /*
* Calcurse - text-based organizer * Calcurse - text-based organizer
@ -135,6 +135,12 @@ struct nbar_s
pthread_mutex_t mutex; pthread_mutex_t mutex;
}; };
struct dmon_conf_s
{
unsigned enable; /* launch daemon automatically when exiting */
unsigned log; /* log daemon activity */
};
/* General configuration variables */ /* General configuration variables */
typedef struct typedef struct
{ {
@ -169,6 +175,7 @@ extern char path_dmon_log[BUFSIZ];
extern struct pad_s apad; extern struct pad_s apad;
extern struct nbar_s nbar; extern struct nbar_s nbar;
extern struct dmon_conf_s dmon;
void vars_init (conf_t *); void vars_init (conf_t *);