Refactoring.

Leonardo Robol [2011-05-07 16:13]
Refactoring.
Filename
audio_switcher.c
diff --git a/audio_switcher.c b/audio_switcher.c
index 566b7fd..ff00888 100644
--- a/audio_switcher.c
+++ b/audio_switcher.c
@@ -4,30 +4,64 @@
 #include <pulse/pulseaudio.h>
 #include <libnotify/notify.h>

-/* Global variable to keep track of the main pulse audio loop */
+/**
+ * Global variable to keep track of the main pulse audio loop, that
+ * will be terminated on the success or failure of the switch. */
 pa_mainloop* mainloop;

+/**
+ * @brief Callback called by toggle_mute when the switch operation
+ * is completed (with success or failure). It terminates the mainloop
+ * and exit the program.
+ *
+ * @param c the Pulseaudio context.
+ * @param success 0 if the switch has gone well, not zero othewise.
+ * @param new_output The description of the new active output.
+ */
 void
 success_callback(pa_context *c, int success, char* new_output)
 {
   /* Notify to the user that the switch was successful */
   GError* error = NULL;
-  char body[255];
-  if (strlen(new_output) < 200)
-    sprintf(body, "New active output: %s", new_output);
+  char *title, *body;
+
+  body = (char*) malloc(sizeof(char) * 255);
+
+  /* Set messages of the notification based on the success
+   * of the switching operation */
+  if (!success)
+    {
+      title = "Audio switch failure";
+      body = "Audio switching operation failed.";
+    }
   else
-    strncpy(body, new_output, 255);
-  NotifyNotification *notification = notify_notification_new ("Audio switched",
+    {
+      title = "Audio switched";
+      if (strlen(new_output) < 235)
+	sprintf(body, "New active output: %s", new_output);
+      else
+	strncpy(body, new_output, 255);
+    }
+
+  /* Display the notification */
+  NotifyNotification *notification = notify_notification_new (title,
 							      body,
 							      "gnome-mixer");
   notify_notification_show (notification,
 			    &error);

+  /* Free vars */
+  free (body);
+
   /* Exit the pulseaudio mainloop */
   pa_mainloop_quit(mainloop, success);
 }

-
+/**
+ * @brief Called for every sink output of pulse audio.
+ * It toggles mute cycling through the available output
+ * ports.
+ */
 void
 toggle_mute(pa_context *c, pa_sink_info *i, void* user_data)
 {
@@ -48,6 +82,10 @@ toggle_mute(pa_context *c, pa_sink_info *i, void* user_data)
 }


+/**
+ * @brief Callback called on the phasis of the connection
+ * to the default Pulseaudio server.
+ */
 void
 context_connected_cb(pa_context *c, void* user_data)
 {
@@ -58,19 +96,26 @@ context_connected_cb(pa_context *c, void* user_data)
   }
 }

+/**
+ * @brief main routine
+ */
 int
 main()
 {
-  mainloop = pa_mainloop_new();
-  pa_mainloop_api *api = pa_mainloop_get_api (mainloop);
+  pa_mainloop_api *api;
   pa_context *c;
-  pa_operation* operation;
-  pa_operation_state_t state = PA_OPERATION_RUNNING;
   int retval;
   pa_proplist *proplist;

+  /* Create a new pulseaudio mainloop */
+  mainloop = pa_mainloop_new();
+  api = pa_mainloop_get_api(mainloop);
+
+  /* Init notification system */
   notify_init ("AudioSwitch");

+  /* Create a new Pualseaudio context with the propery
+   * dictionary */
   proplist = pa_proplist_new();
   pa_proplist_sets(proplist, PA_PROP_DEVICE_API, "alsa");
   c = pa_context_new_with_proplist(api, "Audio Switcher",
@@ -82,7 +127,8 @@ main()
   pa_context_connect(c, NULL, PA_CONTEXT_NOFLAGS, NULL);
   pa_context_set_state_callback(c, (pa_context_notify_cb_t) context_connected_cb, mainloop);

-  /* Loop :) */
+  /* Loop until quit is explictly require by the success_callback */
   pa_mainloop_run(mainloop, &retval);
+
   return retval;
 }
ViewGit