From bf3bce0ab2d6d5fc8448821fbfa29b48a1bb5994 Mon Sep 17 00:00:00 2001 From: Frederic Culot Date: Fri, 2 Jan 2009 22:28:53 +0000 Subject: code cleanup --- src/notify.c | 297 ++++++++++++++++++++++++++++------------------------------- 1 file changed, 140 insertions(+), 157 deletions(-) (limited to 'src/notify.c') diff --git a/src/notify.c b/src/notify.c index 5f03a6d..df42edc 100755 --- a/src/notify.c +++ b/src/notify.c @@ -1,8 +1,8 @@ -/* $calcurse: notify.c,v 1.33 2008/12/28 13:13:59 culot Exp $ */ +/* $calcurse: notify.c,v 1.34 2009/01/02 22:28:54 culot Exp $ */ /* * Calcurse - text-based organizer - * Copyright (c) 2004-2008 Frederic Culot + * Copyright (c) 2004-2009 Frederic Culot * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,10 +40,10 @@ #include "mem.h" #include "notify.h" -static struct notify_vars_s *notify = NULL; -static struct notify_app_s *notify_app = NULL; -static pthread_attr_t detached_thread_attr; -static pthread_t notify_t_main; +static struct notify_vars_s notify; +static struct notify_app_s notify_app; +static pthread_attr_t detached_thread_attr; +static pthread_t notify_t_main; /* Return 1 if we need to display the notify-bar, else 0. */ int @@ -51,9 +51,9 @@ notify_bar (void) { int display_bar = 0; - pthread_mutex_lock (&nbar->mutex); - display_bar = (nbar->show) ? 1 : 0; - pthread_mutex_unlock (&nbar->mutex); + pthread_mutex_lock (&nbar.mutex); + display_bar = (nbar.show) ? 1 : 0; + pthread_mutex_unlock (&nbar.mutex); return (display_bar); } @@ -66,28 +66,21 @@ notify_init_vars (void) char *date_format = "%a %F"; char *cmd = "printf '\\a'"; - nbar = (struct nbar_s *) mem_malloc (sizeof (struct nbar_s)); - pthread_mutex_init (&nbar->mutex, NULL); - nbar->show = 1; - nbar->cntdwn = 300; - (void)strncpy (nbar->datefmt, date_format, strlen (date_format) + 1); - (void)strncpy (nbar->timefmt, time_format, strlen (time_format) + 1); - (void)strncpy (nbar->cmd, cmd, strlen (cmd) + 1); + pthread_mutex_init (&nbar.mutex, NULL); + nbar.show = 1; + nbar.cntdwn = 300; + (void)strncpy (nbar.datefmt, date_format, strlen (date_format) + 1); + (void)strncpy (nbar.timefmt, time_format, strlen (time_format) + 1); + (void)strncpy (nbar.cmd, cmd, strlen (cmd) + 1); - if ((nbar->shell = getenv ("SHELL")) == NULL) - nbar->shell = "/bin/sh"; + if ((nbar.shell = getenv ("SHELL")) == NULL) + nbar.shell = "/bin/sh"; (void)pthread_attr_init (&detached_thread_attr); (void)pthread_attr_setdetachstate (&detached_thread_attr, PTHREAD_CREATE_DETACHED); } -void -notify_free_vars (void) -{ - mem_free (nbar); -} - /* Extract the appointment file name from the complete file path. */ static void extract_aptsfile (void) @@ -96,11 +89,11 @@ extract_aptsfile (void) file = strrchr (path_apts, '/'); if (!file) - notify->apts_file = path_apts; + notify.apts_file = path_apts; else { - notify->apts_file = file; - notify->apts_file++; + notify.apts_file = file; + notify.apts_file++; } } @@ -112,28 +105,18 @@ extract_aptsfile (void) void notify_init_bar (void) { - notify = (struct notify_vars_s *) mem_malloc (sizeof (struct notify_vars_s)); - notify_app = (struct notify_app_s *) mem_malloc (sizeof (struct notify_app_s)); - pthread_mutex_init (¬ify->mutex, NULL); - pthread_mutex_init (¬ify_app->mutex, NULL); - notify_app->got_app = 0; - notify->win = newwin (win[NOT].h, win[NOT].w, win[NOT].y, win[NOT].x); + pthread_mutex_init (¬ify.mutex, NULL); + pthread_mutex_init (¬ify_app.mutex, NULL); + notify_app.got_app = 0; + notify.win = newwin (win[NOT].h, win[NOT].w, win[NOT].y, win[NOT].x); extract_aptsfile (); } -void -notify_free_bar (void) -{ - mem_free (notify_app); - mem_free (notify); -} - /* Stop the notify-bar main thread. */ void notify_stop_main_thread (void) { pthread_cancel (notify_t_main); - return; } /* @@ -143,8 +126,8 @@ notify_stop_main_thread (void) void notify_reinit_bar (void) { - delwin (notify->win); - notify->win = newwin (win[NOT].h, win[NOT].w, win[NOT].y, win[NOT].x); + delwin (notify.win); + notify.win = newwin (win[NOT].h, win[NOT].w, win[NOT].y, win[NOT].x); } /* Launch user defined command as a notification. */ @@ -176,74 +159,74 @@ notify_update_bar (void) char buf[BUFSIZ]; date_pos = space; - pthread_mutex_lock (¬ify->mutex); + pthread_mutex_lock (¬ify.mutex); - file_pos = strlen (notify->date) + strlen (notify->time) + 7 + 2 * space; - app_pos = file_pos + strlen (notify->apts_file) + 2 + space; + file_pos = strlen (notify.date) + strlen (notify.time) + 7 + 2 * space; + app_pos = file_pos + strlen (notify.apts_file) + 2 + space; txt_max_len = col - (app_pos + 12 + space); - custom_apply_attr (notify->win, ATTR_HIGHEST); - wattron (notify->win, A_UNDERLINE | A_REVERSE); - mvwhline (notify->win, 0, 0, ACS_HLINE, col); - mvwprintw (notify->win, 0, date_pos, "[ %s | %s ]", - notify->date, notify->time); - mvwprintw (notify->win, 0, file_pos, "(%s)", notify->apts_file); + custom_apply_attr (notify.win, ATTR_HIGHEST); + wattron (notify.win, A_UNDERLINE | A_REVERSE); + mvwhline (notify.win, 0, 0, ACS_HLINE, col); + mvwprintw (notify.win, 0, date_pos, "[ %s | %s ]", + notify.date, notify.time); + mvwprintw (notify.win, 0, file_pos, "(%s)", notify.apts_file); - pthread_mutex_lock (¬ify_app->mutex); - if (notify_app->got_app) + pthread_mutex_lock (¬ify_app.mutex); + if (notify_app.got_app) { - if (strlen (notify_app->txt) > txt_max_len) + if (strlen (notify_app.txt) > txt_max_len) { too_long = 1; - (void)strncpy (buf, notify_app->txt, txt_max_len - 3); + (void)strncpy (buf, notify_app.txt, txt_max_len - 3); buf[txt_max_len - 3] = '\0'; } - time_left = notify_app->time - notify->time_in_sec; + time_left = notify_app.time - notify.time_in_sec; if (time_left > 0) { hours_left = (time_left / HOURINSEC); minutes_left = (time_left - hours_left * HOURINSEC) / MININSEC; - pthread_mutex_lock (&nbar->mutex); + pthread_mutex_lock (&nbar.mutex); - if (time_left < nbar->cntdwn && (notify_app->state & APOINT_NOTIFY)) + if (time_left < nbar.cntdwn && (notify_app.state & APOINT_NOTIFY)) blinking = 1; else blinking = 0; if (blinking) - wattron (notify->win, A_BLINK); + wattron (notify.win, A_BLINK); if (too_long) - mvwprintw (notify->win, 0, app_pos, "> %02d:%02d :: %s.. <", + mvwprintw (notify.win, 0, app_pos, "> %02d:%02d :: %s.. <", hours_left, minutes_left, buf); else - mvwprintw (notify->win, 0, app_pos, "> %02d:%02d :: %s <", - hours_left, minutes_left, notify_app->txt); + mvwprintw (notify.win, 0, app_pos, "> %02d:%02d :: %s <", + hours_left, minutes_left, notify_app.txt); if (blinking) - wattroff (notify->win, A_BLINK); + wattroff (notify.win, A_BLINK); - if (blinking && !(notify_app->state & APOINT_NOTIFIED)) + if (blinking && !(notify_app.state & APOINT_NOTIFIED)) { - notify_app->state |= APOINT_NOTIFIED; - launch_cmd (nbar->cmd, nbar->shell); + notify_app.state |= APOINT_NOTIFIED; + launch_cmd (nbar.cmd, nbar.shell); } - pthread_mutex_unlock (&nbar->mutex); + pthread_mutex_unlock (&nbar.mutex); } else { - notify_app->got_app = 0; - pthread_mutex_unlock (¬ify_app->mutex); - pthread_mutex_unlock (¬ify->mutex); + notify_app.got_app = 0; + pthread_mutex_unlock (¬ify_app.mutex); + pthread_mutex_unlock (¬ify.mutex); notify_check_next_app (); return; } } - pthread_mutex_unlock (¬ify_app->mutex); + pthread_mutex_unlock (¬ify_app.mutex); - wattroff (notify->win, A_UNDERLINE | A_REVERSE); - custom_remove_attr (notify->win, ATTR_HIGHEST); - wrefresh (notify->win); + wattroff (notify.win, A_UNDERLINE | A_REVERSE); + custom_remove_attr (notify.win, ATTR_HIGHEST); + wrefresh (notify.win); - pthread_mutex_unlock (¬ify->mutex); + pthread_mutex_unlock (¬ify.mutex); } /* Update the notication bar content */ @@ -264,22 +247,22 @@ notify_main_thread (void *arg) { ntimer = time (NULL); ntime = localtime (&ntimer); - pthread_mutex_lock (¬ify->mutex); - notify->time_in_sec = ntimer; - pthread_mutex_lock (&nbar->mutex); - strftime (notify->time, NOTIFY_FIELD_LENGTH, nbar->timefmt, ntime); - strftime (notify->date, NOTIFY_FIELD_LENGTH, nbar->datefmt, ntime); - pthread_mutex_unlock (&nbar->mutex); - pthread_mutex_unlock (¬ify->mutex); + pthread_mutex_lock (¬ify.mutex); + notify.time_in_sec = ntimer; + pthread_mutex_lock (&nbar.mutex); + strftime (notify.time, NOTIFY_FIELD_LENGTH, nbar.timefmt, ntime); + strftime (notify.date, NOTIFY_FIELD_LENGTH, nbar.datefmt, ntime); + pthread_mutex_unlock (&nbar.mutex); + pthread_mutex_unlock (¬ify.mutex); notify_update_bar (); (void)sleep (thread_sleep); elapse += thread_sleep; if (elapse >= check_app) { elapse = 0; - pthread_mutex_lock (¬ify_app->mutex); - got_app = notify_app->got_app; - pthread_mutex_unlock (¬ify_app->mutex); + pthread_mutex_lock (¬ify_app.mutex); + got_app = notify_app.got_app; + pthread_mutex_unlock (¬ify_app.mutex); if (!got_app) notify_check_next_app (); } @@ -305,19 +288,19 @@ notify_thread_app (void *arg) tmp_app = *recur_apoint_check_next (&tmp_app, current_time, get_today ()); tmp_app = *apoint_check_next (&tmp_app, current_time); - pthread_mutex_lock (¬ify_app->mutex); + pthread_mutex_lock (¬ify_app.mutex); if (tmp_app.got_app) { - notify_app->got_app = 1; - notify_app->time = tmp_app.time; - notify_app->txt = strdup (tmp_app.txt); - notify_app->state = tmp_app.state; + notify_app.got_app = 1; + notify_app.time = tmp_app.time; + notify_app.txt = strdup (tmp_app.txt); + notify_app.state = tmp_app.state; } else { - notify_app->got_app = 0; + notify_app.got_app = 0; } - pthread_mutex_unlock (¬ify_app->mutex); + pthread_mutex_unlock (¬ify_app.mutex); if (tmp_app.txt != NULL) mem_free (tmp_app.txt); @@ -346,28 +329,28 @@ notify_check_added (char *mesg, long start, char state) long gap; current_time = time (NULL); - pthread_mutex_lock (¬ify_app->mutex); - if (!notify_app->got_app) + pthread_mutex_lock (¬ify_app.mutex); + if (!notify_app.got_app) { gap = start - current_time; if (gap >= 0 && gap <= DAYINSEC) update_notify = 1; } - else if (start < notify_app->time && start >= current_time) + else if (start < notify_app.time && start >= current_time) { update_notify = 1; } - else if (start == notify_app->time && state != notify_app->state) + else if (start == notify_app.time && state != notify_app.state) update_notify = 1; if (update_notify) { - notify_app->got_app = 1; - notify_app->time = start; - notify_app->txt = strdup (mesg); - notify_app->state = state; + notify_app.got_app = 1; + notify_app.time = start; + notify_app.txt = strdup (mesg); + notify_app.state = state; } - pthread_mutex_unlock (¬ify_app->mutex); + pthread_mutex_unlock (¬ify_app.mutex); notify_update_bar (); } @@ -380,33 +363,33 @@ notify_check_repeated (recur_apoint_llist_node_t *i) time_t current_time; current_time = time (NULL); - pthread_mutex_lock (¬ify_app->mutex); + pthread_mutex_lock (¬ify_app.mutex); if ((real_app_time = recur_item_inday (i->start, i->exc, i->rpt->type, i->rpt->freq, i->rpt->until, get_today ()) > current_time)) { - if (!notify_app->got_app) + if (!notify_app.got_app) { if (real_app_time - current_time <= DAYINSEC) update_notify = 1; } - else if (real_app_time < notify_app->time && + else if (real_app_time < notify_app.time && real_app_time >= current_time) { update_notify = 1; } - else if (real_app_time == notify_app->time && - i->state != notify_app->state) + else if (real_app_time == notify_app.time && + i->state != notify_app.state) update_notify = 1; } if (update_notify) { - notify_app->got_app = 1; - notify_app->time = real_app_time; - notify_app->txt = strdup (i->mesg); - notify_app->state = i->state; + notify_app.got_app = 1; + notify_app.time = real_app_time; + notify_app.txt = strdup (i->mesg); + notify_app.state = i->state; } - pthread_mutex_unlock (¬ify_app->mutex); + pthread_mutex_unlock (¬ify_app.mutex); notify_update_bar (); } @@ -415,12 +398,12 @@ notify_same_item (long time) { int same = 0; - pthread_mutex_lock (&(notify_app->mutex)); - if (notify_app->got_app && notify_app->time == time) + pthread_mutex_lock (&(notify_app.mutex)); + if (notify_app.got_app && notify_app.time == time) same = 1; - pthread_mutex_unlock (&(notify_app->mutex)); + pthread_mutex_unlock (&(notify_app.mutex)); - return (same); + return same; } int @@ -431,12 +414,12 @@ notify_same_recur_item (recur_apoint_llist_node_t *i) item_start = recur_item_inday (i->start, i->exc, i->rpt->type, i->rpt->freq, i->rpt->until, get_today ()); - pthread_mutex_lock (¬ify_app->mutex); - if (notify_app->got_app && item_start == notify_app->time) + pthread_mutex_lock (¬ify_app.mutex); + if (notify_app.got_app && item_start == notify_app.time) same = 1; - pthread_mutex_unlock (&(notify_app->mutex)); + pthread_mutex_unlock (&(notify_app.mutex)); - return (same); + return same; } /* Launch the notify-bar main thread. */ @@ -445,7 +428,6 @@ notify_start_main_thread (void) { pthread_create (¬ify_t_main, NULL, notify_main_thread, NULL); notify_check_next_app (); - return; } /* Print options related to the notify-bar. */ @@ -493,18 +475,18 @@ notify_print_options (WINDOW *optwin, int col) _("(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); - (void)strncpy (opt[CLOCK].value, nbar->timefmt, BUFSIZ); - (void)snprintf (opt[WARN].value, BUFSIZ, "%d", nbar->cntdwn); - (void)strncpy (opt[CMD].value, nbar->cmd, BUFSIZ); + (void)strncpy (opt[DATE].value, nbar.datefmt, BUFSIZ); + (void)strncpy (opt[CLOCK].value, nbar.timefmt, BUFSIZ); + (void)snprintf (opt[WARN].value, BUFSIZ, "%d", nbar.cntdwn); + (void)strncpy (opt[CMD].value, nbar.cmd, BUFSIZ); l = strlen (opt[SHOW].name); x = x_pos + x_offset + l; mvwprintw (optwin, y_pos, x_pos, "[1] %s", opt[SHOW].name); erase_window_part (optwin, x, y_pos, maxcol, y_pos); - print_bool_option_incolor (optwin, nbar->show, y_pos, x); + print_bool_option_incolor (optwin, nbar.show, y_pos, x); mvwprintw (optwin, y_pos + 1, x_pos, opt[SHOW].desc); for (i = 1; i < NB_OPT; i++) @@ -529,7 +511,7 @@ notify_print_options (WINDOW *optwin, int col) mvwprintw (optwin, y + 1, x_pos, opt[i].desc); } - pthread_mutex_unlock (&nbar->mutex); + pthread_mutex_unlock (&nbar.mutex); wmove (win[STA].p, 1, 0); wnoutrefresh (optwin); doupdate (); @@ -553,9 +535,11 @@ notify_config_bar (void) char *cmd_str = _("Enter the notification command "); int ch = 0, change_win = 1; - buf = (char *) mem_malloc (BUFSIZ); + buf = mem_malloc (BUFSIZ); (void)snprintf (label, BUFSIZ, _("CalCurse %s | notify-bar options"), VERSION); + + conf_win.p = 0; custom_confwin_init (&conf_win, label); while (ch != 'q') @@ -577,66 +561,65 @@ notify_config_bar (void) custom_confwin_init (&conf_win, label); break; case '1': - pthread_mutex_lock (&nbar->mutex); - nbar->show = !nbar->show; - pthread_mutex_unlock (&nbar->mutex); + pthread_mutex_lock (&nbar.mutex); + nbar.show = !nbar.show; + pthread_mutex_unlock (&nbar.mutex); if (notify_bar ()) notify_start_main_thread (); else notify_stop_main_thread (); - delwin (conf_win.p); change_win = 1; break; case '2': status_mesg (date_str, ""); - pthread_mutex_lock (&nbar->mutex); - (void)strncpy (buf, nbar->datefmt, strlen (nbar->datefmt) + 1); - pthread_mutex_unlock (&nbar->mutex); + pthread_mutex_lock (&nbar.mutex); + (void)strncpy (buf, nbar.datefmt, strlen (nbar.datefmt) + 1); + pthread_mutex_unlock (&nbar.mutex); if (updatestring (win[STA].p, &buf, 0, 1) == 0) { - pthread_mutex_lock (&nbar->mutex); - (void)strncpy (nbar->datefmt, buf, strlen (buf) + 1); - pthread_mutex_unlock (&nbar->mutex); + pthread_mutex_lock (&nbar.mutex); + (void)strncpy (nbar.datefmt, buf, strlen (buf) + 1); + pthread_mutex_unlock (&nbar.mutex); } change_win = 0; break; case '3': status_mesg (time_str, ""); - pthread_mutex_lock (&nbar->mutex); - (void)strncpy (buf, nbar->timefmt, strlen (nbar->timefmt) + 1); - pthread_mutex_unlock (&nbar->mutex); + pthread_mutex_lock (&nbar.mutex); + (void)strncpy (buf, nbar.timefmt, strlen (nbar.timefmt) + 1); + pthread_mutex_unlock (&nbar.mutex); if (updatestring (win[STA].p, &buf, 0, 1) == 0) { - pthread_mutex_lock (&nbar->mutex); - (void)strncpy (nbar->timefmt, buf, strlen (buf) + 1); - pthread_mutex_unlock (&nbar->mutex); + pthread_mutex_lock (&nbar.mutex); + (void)strncpy (nbar.timefmt, buf, strlen (buf) + 1); + pthread_mutex_unlock (&nbar.mutex); } change_win = 0; break; case '4': status_mesg (count_str, ""); - pthread_mutex_lock (&nbar->mutex); - printf (buf, "%d", nbar->cntdwn); - pthread_mutex_unlock (&nbar->mutex); + pthread_mutex_lock (&nbar.mutex); + printf (buf, "%d", nbar.cntdwn); + pthread_mutex_unlock (&nbar.mutex); if (updatestring (win[STA].p, &buf, 0, 1) == 0 && is_all_digit (buf) && atoi (buf) >= 0 && atoi (buf) <= DAYINSEC) { - pthread_mutex_lock (&nbar->mutex); - nbar->cntdwn = atoi (buf); - pthread_mutex_unlock (&nbar->mutex); + pthread_mutex_lock (&nbar.mutex); + nbar.cntdwn = atoi (buf); + pthread_mutex_unlock (&nbar.mutex); } change_win = 0; break; case '5': status_mesg (cmd_str, ""); - pthread_mutex_lock (&nbar->mutex); - (void)strncpy (buf, nbar->cmd, strlen (nbar->cmd) + 1); - pthread_mutex_unlock (&nbar->mutex); + pthread_mutex_lock (&nbar.mutex); + (void)strncpy (buf, nbar.cmd, strlen (nbar.cmd) + 1); + pthread_mutex_unlock (&nbar.mutex); if (updatestring (win[STA].p, &buf, 0, 1) == 0) { - pthread_mutex_lock (&nbar->mutex); - (void)strncpy (nbar->cmd, buf, strlen (buf) + 1); - pthread_mutex_unlock (&nbar->mutex); + pthread_mutex_lock (&nbar.mutex); + (void)strncpy (nbar.cmd, buf, strlen (buf) + 1); + pthread_mutex_unlock (&nbar.mutex); } change_win = 0; break; -- cgit v1.2.3-54-g00ecf