Add option to invert single and double-click actions
authorHugo Villeneuve <hugo@hugovil.com>
Fri, 15 Feb 2019 22:16:20 +0000 (17:16 -0500)
committerHugo Villeneuve <hugo@hugovil.com>
Fri, 15 Feb 2019 22:21:27 +0000 (17:21 -0500)
src/configfile.c
src/wmnotify.c
src/wmnotify.h

index 1bd42e1..c9b9621 100644 (file)
@@ -88,6 +88,10 @@ static void CreateDefaultConfigurationFile(char *file)
                "# Mail Check Interval (in minutes, default is 5 minutes).\n");
        fprintf(fp, "#mailcheckdelay 5\n\n");
        fprintf(fp, "# Default mail client (optional).\n");
+       fprintf(fp,
+               "# Manual mail check: 0=double-click, 1=single-click (optional, default is "
+               "1).\n");
+       fprintf(fp, "mailcheck_single_click 1\n\n");
        fprintf(fp, "#mailclient sylpheed\n\n");
        fprintf(fp,
                "# Audio notification, 0=disable, 1=enable (optional, default is "
@@ -198,6 +202,7 @@ static void ParseConfigurationFile(FILE *file)
        wmnotify_infos.mail_check_interval = 60; /* 1 minute interval. */
        wmnotify_infos.audible_notification = false; /* Disabled. */
        wmnotify_infos.use_ssl = false; /* Disabled. */
+       wmnotify_infos.mailcheck_single_click = 1;
        wmnotify_infos.mail_client_argv[0] = NULL; /* No default command. */
        wmnotify_infos.audiofile[0] = '\0'; /* No default audio file. */
        wmnotify_infos.volume = 100; /* 100% volume. */
@@ -302,6 +307,20 @@ static void ParseConfigurationFile(FILE *file)
                        }
                        wmnotify_infos.mail_check_interval =
                                (unsigned int)delay * 60;
+               } else if (STREQ(token, "mailcheck_single_click")) {
+                       int number;
+
+                       token = GetArguments("mailcheck_single_click", true);
+                       number = GetNumber(token, "mailcheck_single_click");
+                       if ((number == 0) || (number == 1)) {
+                               wmnotify_infos.mailcheck_single_click = number;
+                       } else {
+                               fprintf(stderr,
+                                       "%s: Invalid value for for parameter 'mailcheck_single_click' in\n"
+                                       "configuration file (must be 0 or 1): %d\n",
+                                       PACKAGE, number);
+                               exit(EXIT_FAILURE);
+                       }
                } else if (STREQ(token, "mailclient")) {
                        /* Multiple arguments */
                        token = GetArguments("mailclient", false);
index b767f66..df37276 100644 (file)
 #include "wmnotify.h"
 
 
-/*
- * Set in DoubleClick() to stop the new mail animation when the mail client is
- * opened.
- */
+/* Set to 1 to stop the new mail animation when the mail client is opened. */
 static bool animation_stop;
 
 static int animation_image = MAILBOX_FULL;
 
 /*
- * Set in response to signal sent by SingleClick() to force mail check. Also set
+ * Set in response to single or double-click to force mail check. Also set
  * to true at startup to force initial check.
  */
 static bool manual_check = true;
@@ -58,7 +55,10 @@ static bool manual_check = true;
 /* Used to signal TimerThread to quit. Inactive for now. */
 static bool quit;
 
-static int double_click_notif;
+static int start_mail_client_notif;
+
+static int single_click_sig;
+static int double_click_sig;
 
 /* TimerThread ID */
 static pthread_t timer_thread;
@@ -161,7 +161,7 @@ static void ExecuteCommand(char *argv[])
 }
 
 
-/* single-click --> Checking mail */
+/* Single-click callback */
 static void SingleClick(void)
 {
        int status;
@@ -170,7 +170,7 @@ static void SingleClick(void)
                printf("%s: SingleClick() Entry\n", PACKAGE);
 
        /* Sending a signal to awake the TimerThread() thread. */
-       status = pthread_kill(timer_thread, SIGUSR1);
+       status = pthread_kill(timer_thread, single_click_sig);
        if (status != EXIT_SUCCESS) {
                fprintf(stderr, "%s: pthread_kill() error (%d)\n", PACKAGE,
                        status);
@@ -183,52 +183,25 @@ static void SingleClick(void)
 }
 
 
-/* Double-click --> Starting external mail client. */
+/* Double-click callback */
 static void DoubleClick(void)
 {
        int status;
 
-       if (wmnotify_infos.mail_client_argv[0] != NULL) {
-               if (wmnotify_infos.debug)
-                       printf("%s: Starting mail client\n", PACKAGE);
-
-               ExecuteCommand(wmnotify_infos.mail_client_argv);
-
-               double_click_notif = true;
-
-               /*
-                * Sending a signal to awake the TimerThread() thread. This was
-                * previously done with a mutex variable (animation_stop), but
-                * this caused a bug when the following sequence was
-                * encountered:
-                *   -The user double-click to start the external mail client
-                *   -A new E-mail is received shortly after that
-                *   -The user exit the external mail client
-                *   -The user manually check for new E-mail
-                *   -The audio notification sound is played, but no animation
-                *    image is displayed.
-                * This was because setting the mutex variable 'animation_stop'
-                * didn't awakened the TimerThread(), but single-clicking
-                * awakened it. Since the 'animation_stop' variable was still
-                * set to true, no animation occured.
-                */
-               status = pthread_kill(timer_thread, SIGUSR2);
-               if (status != EXIT_SUCCESS) {
-                       fprintf(stderr, "%s: pthread_kill() error (%d)\n",
-                               PACKAGE, status);
-                       ErrorLocation(__FILE__, __LINE__);
-                       exit(EXIT_FAILURE);
-               }
-
-               DisplayExecuteCommandNotification();
-               sleep(1);
-               DisplayClosedMailbox();
+       if (wmnotify_infos.debug)
+               printf("%s: DoubleClick() Entry\n", PACKAGE);
 
-               double_click_notif = false;
-       } else {
-               fprintf(stderr, "%s: Warning: No email-client defined.\n",
-                       PACKAGE);
+       /* Sending a signal to awake the TimerThread() thread. */
+       status = pthread_kill(timer_thread, double_click_sig);
+       if (status != EXIT_SUCCESS) {
+               fprintf(stderr, "%s: pthread_kill() error (%d)\n", PACKAGE,
+                       status);
+               ErrorLocation(__FILE__, __LINE__);
+               exit(EXIT_FAILURE);
        }
+
+       if (wmnotify_infos.debug)
+               printf("%s: DoubleClick() Exit\n", PACKAGE);
 }
 
 
@@ -257,17 +230,56 @@ static void CatchChildTerminationSignal(int signal)
        }
 }
 
+/*
+ * Sending of a signal to awake the TimerThread() thread: This was
+ * previously done with a mutex variable (animation_stop), but
+ * this caused a bug when the following sequence was
+ * encountered:
+ *   -The user double-click to start the external mail client
+ *   -A new E-mail is received shortly after that
+ *   -The user exit the external mail client
+ *   -The user manually check for new E-mail
+ *   -The audio notification sound is played, but no animation
+ *    image is displayed.
+ * This was because setting the mutex variable 'animation_stop'
+ * didn't awakened the TimerThread(), but single-clicking
+ * awakened it. Since the 'animation_stop' variable was still
+ * set to true, no animation occured.
+ */
+static void
+start_mail_client(void)
+{
+       if (wmnotify_infos.mail_client_argv[0] != NULL) {
+               if (wmnotify_infos.debug)
+                       printf("%s: Starting mail client\n", PACKAGE);
+
+               /* Starting external mail client. */
+               ExecuteCommand(wmnotify_infos.mail_client_argv);
+
+               start_mail_client_notif = true;
+
+               DisplayExecuteCommandNotification();
+               sleep(1);
+               DisplayClosedMailbox();
+
+               start_mail_client_notif = false;
+       } else {
+               fprintf(stderr, "%s: Warning: No email-client defined.\n",
+                       PACKAGE);
+       }
+}
 
 static void CatchTimerSignal(int signal)
 {
        switch (signal) {
        case SIGUSR1:
-               /* Catching the signal sent by the SingleClick() function. */
+               /* Catching the signal to manually check mail. */
                manual_check = true;
                break;
        case SIGUSR2:
-               /* Catching the signal sent by the DoubleClick() function. */
+               /* Catching the signal to start mail client. */
                animation_stop = true;
+               start_mail_client();
                break;
        default:
                fprintf(stderr,
@@ -411,7 +423,7 @@ static void *TimerThread(void *arg)
                        }
                        animation_running = false;
                        animation_stop = false;
-                       if (double_click_notif == false) {
+                       if (start_mail_client_notif == false) {
                                /*
                                 * Before exiting, be sure to put NO MAIL image
                                 * back in place.
@@ -468,6 +480,17 @@ int main(int argc, char *argv[])
         */
        (void) signal(SIGCHLD, CatchChildTerminationSignal);
 
+       if (wmnotify_infos.mailcheck_single_click)
+       {
+               single_click_sig = SIGUSR1;
+               double_click_sig = SIGUSR2;
+       }
+       else
+       {
+               single_click_sig = SIGUSR2;
+               double_click_sig = SIGUSR1;
+       }
+
        /* Initialize callback function pointers. */
        ProcessXlibEventsInit(SingleClick, DoubleClick);
 
index c701f01..989e511 100644 (file)
@@ -60,6 +60,7 @@
 struct wmnotify_t {
        bool debug;
        char *optional_config_file;
+       int mailcheck_single_click;
        char mail_client_command[512];
        char *mail_client_argv[ARGV_LIMIT];
        unsigned int mail_check_interval;       /* In seconds. */