[Pommed-commits] r294 - trunk/gpomme

Julien Blache jblache at alioth.debian.org
Wed Mar 7 21:04:28 CET 2007


Author: jblache
Date: 2007-03-07 20:04:27 +0000 (Wed, 07 Mar 2007)
New Revision: 294

Added:
   trunk/gpomme/inotify-syscalls.h
Modified:
   trunk/gpomme/conffile.c
   trunk/gpomme/conffile.h
   trunk/gpomme/gpomme.c
   trunk/gpomme/gpomme.h
   trunk/gpomme/theme.c
Log:
Use inotify to watch the config file and reload the configuration.


Modified: trunk/gpomme/conffile.c
===================================================================
--- trunk/gpomme/conffile.c	2007-03-06 21:01:36 UTC (rev 293)
+++ trunk/gpomme/conffile.c	2007-03-07 20:04:27 UTC (rev 294)
@@ -24,11 +24,17 @@
 
 #include <errno.h>
 #include <unistd.h>
+#include <fcntl.h>
+
 #include <sys/types.h>
 #include <pwd.h>
 
+#include <linux/inotify.h>
+#include "inotify-syscalls.h"
+
 #include <confuse.h>
 
+#include "conffile.h"
 #include "gpomme.h"
 #include "theme.h"
 
@@ -42,8 +48,8 @@
   };
 
 
-cfg_t *cfg;
-static char *conffile;
+cfg_t *cfg = NULL;
+static char *conffile = NULL;
 
 
 static int
@@ -82,24 +88,30 @@
 
   int ret;
 
-  pw = getpwuid(getuid());
-  if (pw == NULL)
+  if (conffile == NULL)
     {
-      fprintf(stderr, "Could not get user information\n");
+      pw = getpwuid(getuid());
+      if (pw == NULL)
+	{
+	  fprintf(stderr, "Could not get user information\n");
 
-      return -1;
-    }
+	  return -1;
+	}
 
-  conffile = (char *) malloc(strlen(pw->pw_dir) + strlen(CONFFILE) + 1);
-  if (conffile == NULL)
-    {
-      fprintf(stderr, "Could not allocate memory\n");
+      conffile = (char *) malloc(strlen(pw->pw_dir) + strlen(CONFFILE) + 1);
+      if (conffile == NULL)
+	{
+	  fprintf(stderr, "Could not allocate memory\n");
 
-      return -1;
+	  return -1;
+	}
+
+      strncpy(conffile, pw->pw_dir, strlen(pw->pw_dir) + 1);
+      strncat(conffile, CONFFILE, strlen(CONFFILE));
     }
 
-  strncpy(conffile, pw->pw_dir, strlen(pw->pw_dir) + 1);
-  strncat(conffile, CONFFILE, strlen(CONFFILE));
+  if (cfg != NULL)
+    cfg_free(cfg);
 
   cfg = cfg_init(cfg_opts, CFGF_NONE);
 
@@ -180,3 +192,51 @@
 
   return 0;
 }
+
+
+int
+config_monitor(void)
+{
+  int fd;
+  int ret;
+
+  fd = inotify_init();
+  if (fd < 0)
+    {
+      fprintf(stderr, "Error: could not initialize inotify instance: %s\n", strerror(errno));
+
+      return -1;
+    }
+
+  ret = fcntl(fd, F_GETFL);
+  if (ret < 0)
+    {
+      close(fd);
+
+      fprintf(stderr, "Error: failed to get inotify fd flags: %s\n", strerror(errno));
+
+      return -1;
+    }
+
+  ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
+  if (ret < 0)
+    {
+      close(fd);
+
+      fprintf(stderr, "Error: failed to set inotify fd flags: %s\n", strerror(errno));
+
+      return -1;
+    }
+
+  ret = inotify_add_watch(fd, conffile, IN_CLOSE_WRITE);
+  if (ret < 0)
+    {
+      close(fd);
+
+      fprintf(stderr, "Error: could not add inotify watch: %s\n", strerror(errno));
+
+      return -1;
+    }
+
+  return fd;
+}

Modified: trunk/gpomme/conffile.h
===================================================================
--- trunk/gpomme/conffile.h	2007-03-06 21:01:36 UTC (rev 293)
+++ trunk/gpomme/conffile.h	2007-03-07 20:04:27 UTC (rev 294)
@@ -11,4 +11,7 @@
 int
 config_write(void);
 
+int
+config_monitor(void);
+
 #endif /* !__CONFFILE_H__ */

Modified: trunk/gpomme/gpomme.c
===================================================================
--- trunk/gpomme/gpomme.c	2007-03-06 21:01:36 UTC (rev 293)
+++ trunk/gpomme/gpomme.c	2007-03-07 20:04:27 UTC (rev 294)
@@ -29,6 +29,9 @@
 #include <unistd.h>
 #include <signal.h>
 
+#include <linux/inotify.h>
+#include "inotify-syscalls.h"
+
 #include <libintl.h>
 
 #include <gtk/gtk.h>
@@ -48,7 +51,6 @@
 
 struct _mbp_w mbp_w;
 
-
 struct
 {
   int muted;
@@ -57,7 +59,10 @@
 DBusError dbus_err;
 DBusConnection *conn;
 
+/* inotify fd (config file monitoring) */
+int cfg_ifd = -1;
 
+
 /* Timer callback */
 gboolean
 hide_window(gpointer userdata)
@@ -293,7 +298,7 @@
 }
 
 
-gboolean
+void
 mbp_dbus_listen(gpointer userdata)
 {
   DBusMessage *msg;
@@ -312,7 +317,7 @@
       ret = mbp_dbus_connect();
 
       if (ret < 0)
-	return TRUE;
+	return;
     }
 
   while (1)
@@ -322,7 +327,7 @@
       msg = dbus_connection_pop_message(conn);
 
       if (msg == NULL)
-	return TRUE;
+	return;
 
       if (dbus_message_is_signal(msg, "org.pommed.signal.lcdBacklight", "lcdBacklight"))
 	{
@@ -398,7 +403,41 @@
 
       dbus_message_unref(msg);
     }
+}
 
+void
+mbp_check_config(gpointer userdata)
+{
+  struct inotify_event ie;
+
+  int ret;
+
+  if (cfg_ifd < 0)
+    return;
+
+  ret = read(cfg_ifd, &ie, sizeof(struct inotify_event));
+  if (ret > 0)
+    {
+      ret = config_load();
+      if (ret < 0)
+	{
+	  fprintf(stderr, "Failed to reload config file, exiting\n");
+
+	  gtk_main_quit();
+	}
+
+      close(cfg_ifd);
+
+      cfg_ifd = config_monitor();
+    }
+}
+
+gboolean
+mbp_check_events(gpointer userdata)
+{
+  mbp_check_config(userdata);
+  mbp_dbus_listen(userdata);
+
   return TRUE;
 }
 
@@ -456,6 +495,8 @@
 
   mbp_dbus_connect();
 
+  cfg_ifd = config_monitor();
+
   signal(SIGINT, sig_int_term_handler);
   signal(SIGTERM, sig_int_term_handler);
 
@@ -468,10 +509,13 @@
 
   create_window();
 
-  g_timeout_add(100, mbp_dbus_listen, NULL);
+  g_timeout_add(100, mbp_check_events, NULL);
 
   gtk_main();
 
+
+  close(cfg_ifd);
+
   audio_command(AUDIO_COMMAND_QUIT);
   audio_cleanup();
 

Modified: trunk/gpomme/gpomme.h
===================================================================
--- trunk/gpomme/gpomme.h	2007-03-06 21:01:36 UTC (rev 293)
+++ trunk/gpomme/gpomme.h	2007-03-07 20:04:27 UTC (rev 294)
@@ -9,7 +9,7 @@
 
 #define THEME_BASE    "/usr/share/gpomme/themes"
 
-#define M_VERSION     "0.5"
+#define M_VERSION     "0.6"
 
 
 struct _mbp_w

Added: trunk/gpomme/inotify-syscalls.h
===================================================================
--- trunk/gpomme/inotify-syscalls.h	2007-03-06 21:01:36 UTC (rev 293)
+++ trunk/gpomme/inotify-syscalls.h	2007-03-07 20:04:27 UTC (rev 294)
@@ -0,0 +1,98 @@
+/*
+ * Inotify syscall numbers
+ * Taken from the Linux kernel source tree
+ *
+ * Licensed under the terms of the GNU General Public License Version 2.
+ *
+ * Copyright (c) 2006 Tobias Klauser <tklauser at distanz.ch>
+ */
+
+#ifndef _LINUX_INOTIFY_SYSCALLS_H
+#define _LINUX_INOTIFY_SYSCALLS_H
+
+#include <sys/syscall.h>
+
+#if defined(__i386__)
+# define __NR_inotify_init	291
+# define __NR_inotify_add_watch	292
+# define __NR_inotify_rm_watch	293
+#elif defined(__x86_64__)
+# define __NR_inotify_init	253
+# define __NR_inotify_add_watch	254
+# define __NR_inotify_rm_watch	255
+#elif defined(__powerpc__) || defined(__powerpc64__)
+# define __NR_inotify_init	275
+# define __NR_inotify_add_watch	276
+# define __NR_inotify_rm_watch	277
+#elif defined (__ia64__)
+# define __NR_inotify_init	1277
+# define __NR_inotify_add_watch	1278
+# define __NR_inotify_rm_watch	1279
+#elif defined (__s390__)
+# define __NR_inotify_init	284
+# define __NR_inotify_add_watch	285
+# define __NR_inotify_rm_watch	286
+#elif defined (__alpha__)
+# define __NR_inotify_init	444
+# define __NR_inotify_add_watch	445
+# define __NR_inotify_rm_watch	446
+#elif defined (__sparc__) || defined (__sparc64__)
+# define __NR_inotify_init	151
+# define __NR_inotify_add_watch	152
+# define __NR_inotify_rm_watch	156
+#elif defined (__arm__)
+# define __NR_OABI_SYSCALL_BASE	0x900000
+# if defined(__thumb__) || defined(__ARM_EABI__)
+#  define __NR_SYSCALL_BASE	0
+# else
+#  define __NR_SYSCALL_BASE	__NR_OABI_SYSCALL_BASE
+# endif
+# define __NR_inotify_init	(__NR_SYSCALL_BASE + 316)
+# define __NR_inotify_add_watch	(__NR_SYSCALL_BASE + 317)
+# define __NR_inotify_rm_watch	(__NR_SYSCALL_BASE + 318)
+#elif defined (__sh__)
+# define __NR_inotify_init	290
+# define __NR_inotify_add_watch	291
+# define __NR_inotify_rm_watch	292
+#elif defined (__hppa__)
+# define __NR_inotify_init	269
+# define __NR_inotify_add_watch	270
+# define __NR_inotify_rm_watch	271
+#elif defined (__mips__)
+# include <sgidefs.h>
+# if _MIPS_SIM == _MIPS_SIM_ABI32
+#  define __NR_Linux			4000
+#  define __NR_inotify_init		(__NR_Linux + 284)
+#  define __NR_inotify_add_watch	(__NR_Linux + 285)
+#  define __NR_inotify_rm_watch		(__NR_Linux + 286)
+# elif _MIPS_SIM == _MIPS_SIM_ABI64
+#  define __NR_Linux			5000
+#  define __NR_inotify_init		(__NR_Linux + 243)
+#  define __NR_inotify_add_watch	(__NR_Linux + 244)
+#  define __NR_inotify_rm_watch		(__NR_Linux + 245)
+# elif _MIPS_SIM == _MIPS_SIM_NABI32
+#  define __NR_Linux			6000
+#  define __NR_inotify_init		(__NR_Linux + 247)
+#  define __NR_inotify_add_watch	(__NR_Linux + 248)
+#  define __NR_inotify_rm_watch		(__NR_Linux + 249)
+# endif
+#else
+# error "inotify not supported on this architecture!"
+#endif
+
+static inline int inotify_init (void)
+{
+	return syscall (__NR_inotify_init);
+}
+
+static inline int inotify_add_watch (int fd, const char *name, __u32 mask)
+{
+	return syscall (__NR_inotify_add_watch, fd, name, mask);
+}
+
+static inline int inotify_rm_watch (int fd, __u32 wd)
+{
+	return syscall (__NR_inotify_rm_watch, fd, wd);
+}
+
+#endif /* _LINUX_INOTIFY_SYSCALLS_H */

Modified: trunk/gpomme/theme.c
===================================================================
--- trunk/gpomme/theme.c	2007-03-06 21:01:36 UTC (rev 293)
+++ trunk/gpomme/theme.c	2007-03-07 20:04:27 UTC (rev 294)
@@ -68,12 +68,16 @@
   GError *error = NULL;
 
   char file[PATH_MAX];
+  int i;
   int ret;
 
   ret = snprintf(file, PATH_MAX, "%s/%s/background.png", THEME_BASE, name);
   if (ret >= PATH_MAX)
     return -1;
 
+  if (theme.background)
+    g_object_unref(G_OBJECT(theme.background));
+
   theme.background = gdk_pixbuf_new_from_file(file, &error);
 
   if (error != NULL)
@@ -92,23 +96,24 @@
    * the images by itself when we start adding/removing them
    * to/from a GtkContainer.
    */
+
+  for (i = 0; i < IMG_NIMG; i++)
+    {
+      if (theme.images[i])
+	g_object_unref(G_OBJECT(theme.images[i]));
+    }
+
   theme.images[IMG_LCD_BCK] = load_image(name, "brightness.png");
-  g_object_ref(G_OBJECT(theme.images[IMG_LCD_BCK]));
-
   theme.images[IMG_KBD_BCK] = load_image(name, "kbdlight.png");
-  g_object_ref(G_OBJECT(theme.images[IMG_KBD_BCK]));
-
   theme.images[IMG_AUDIO_VOL_ON] = load_image(name, "volume.png");
-  g_object_ref(G_OBJECT(theme.images[IMG_AUDIO_VOL_ON]));
-
   theme.images[IMG_AUDIO_VOL_OFF] = load_image(name, "mute.png");
-  g_object_ref(G_OBJECT(theme.images[IMG_AUDIO_VOL_OFF]));
-
   theme.images[IMG_AUDIO_MUTE] = load_image(name, "noaudio.png");
-  g_object_ref(G_OBJECT(theme.images[IMG_AUDIO_MUTE]));
-
   theme.images[IMG_CD_EJECT] = load_image(name, "cdrom.png");
-  g_object_ref(G_OBJECT(theme.images[IMG_CD_EJECT]));
 
+  for (i = 0; i < IMG_NIMG; i++)
+    {
+      g_object_ref(G_OBJECT(theme.images[i]));
+    }
+
   return 0;
 }




More information about the Pommed-commits mailing list