[Pommed-commits] r413 - in trunk: client-common gpomme wmpomme

jblache at alioth.debian.org jblache at alioth.debian.org
Tue Dec 4 11:15:51 UTC 2007


Author: jblache
Date: 2007-12-04 11:15:51 +0000 (Tue, 04 Dec 2007)
New Revision: 413

Modified:
   trunk/client-common/video-client.c
   trunk/client-common/video-client.h
   trunk/gpomme/gpomme.c
   trunk/wmpomme/wmpomme.c
Log:
Check that the pommed frontend is active before running the video switch script.

"active" means the X session the frontend is running in is the active session (its VT is the active VT).


Modified: trunk/client-common/video-client.c
===================================================================
--- trunk/client-common/video-client.c	2007-12-01 22:19:08 UTC (rev 412)
+++ trunk/client-common/video-client.c	2007-12-04 11:15:51 UTC (rev 413)
@@ -5,6 +5,8 @@
  *
  * Copyright (C) 2007 Julien BLACHE <jb at jblache.org>
  *
+ * Some code below taken from GDM where noted.
+ *
  * 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
  * the Free Software Foundation; version 2 of the License.
@@ -23,28 +25,156 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
+#include <stdint.h>
 #include <pwd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
 
 #include <errno.h>
 
+#include <linux/vt.h>
+
+#include <X11/Xlib.h>
+#include <X11/Xatom.h>
+
 #include "video-client.h"
 
 
 static char *vsw_user = NULL;
 
+
 /*
+ * Get the VT number X is running on
+ * (code taken from GDM, daemon/getvt.c, GPLv2+)
+ */
+static int
+mbp_get_x_vtnum(Display *dpy)
+{
+  Atom prop;
+  Atom actualtype;
+  int actualformat;
+  unsigned long nitems;
+  unsigned long bytes_after;
+  unsigned char *buf;
+  int num;
+
+  prop = XInternAtom (dpy, "XFree86_VT", False);
+  if (prop == None)
+      return -1;
+
+  if (XGetWindowProperty (dpy, DefaultRootWindow (dpy), prop, 0, 1,
+			  False, AnyPropertyType, &actualtype, &actualformat,
+			  &nitems, &bytes_after, &buf))
+    {
+      return -1;
+    }
+
+  if (nitems != 1)
+    {
+      XFree (buf);
+      return -1;
+    }
+
+  switch (actualtype)
+    {
+      case XA_CARDINAL:
+      case XA_INTEGER:
+      case XA_WINDOW:
+	switch (actualformat)
+	  {
+	    case 8:
+	      num = (*(uint8_t  *)(void *)buf);
+	      break;
+	    case 16:
+	      num = (*(uint16_t *)(void *)buf);
+	      break;
+	    case 32:
+	      num = (*(uint32_t *)(void *)buf);
+	      break;
+	    default:
+	      XFree (buf);
+	      return -1;
+	  }
+	break;
+      default:
+	XFree (buf);
+	return -1;
+    }
+
+  XFree (buf);
+
+  return num;
+}
+
+/*
+ * Determine whether the frontend is running on the active X session
+ * aka the X session associated to the active VT.
+ *
+ * On error, return 1 by default, 0 when the error could mean we're not safe.
+ */
+static int
+mbp_frontend_is_active(Display *dpy)
+{
+  int vt;
+  int fd;
+  char buf[16];
+  struct vt_stat vtstat;
+
+  int ret;
+
+  vt = mbp_get_x_vtnum(dpy);
+
+  ret = snprintf(buf, sizeof(buf), "/dev/tty%d", vt);
+  if (ret >= sizeof(buf))
+    return 1;
+
+  /* Try to open the VT our X session is running on */
+  fd = open(buf, O_RDWR);
+
+  if ((fd < 0) && (errno == EACCES))
+    fd = open(buf, O_RDONLY);
+
+  if ((fd < 0) && (errno == EACCES))
+    fd = open(buf, O_WRONLY);
+
+  /* Can't open the VT, this shouldn't happen; maybe X is remote? */
+  if (fd < 0)
+    return 0;
+
+  /* Our VT isn't a tty, WTF?! */
+  if (!isatty(fd))
+    {
+      close(fd);
+      return 0;
+    }
+
+  /* Get VT state, includes active VT */
+  ret = ioctl(fd, VT_GETSTATE, &vtstat);
+  close(fd);
+
+  if (ret < 0)
+    return 1;
+
+  return (vt == vtstat.v_active);
+}
+
+
+/*
  * NOTE: you MUST install a SIGCHLD handler if you use this function
  */
 void
-mbp_video_switch(void)
+mbp_video_switch(Display *dpy)
 {
   struct passwd *pw;
   char *vsw = NULL;
 
   int ret;
 
+  if (!mbp_frontend_is_active(dpy))
+    return;
+
   if (vsw_user == NULL)
     {
       pw = getpwuid(getuid());

Modified: trunk/client-common/video-client.h
===================================================================
--- trunk/client-common/video-client.h	2007-12-01 22:19:08 UTC (rev 412)
+++ trunk/client-common/video-client.h	2007-12-04 11:15:51 UTC (rev 413)
@@ -10,7 +10,7 @@
 
 
 void
-mbp_video_switch(void);
+mbp_video_switch(Display *dpy);
 
 
 #endif /* !__MBP_VIDEO_CLIENT_H__ */

Modified: trunk/gpomme/gpomme.c
===================================================================
--- trunk/gpomme/gpomme.c	2007-12-01 22:19:08 UTC (rev 412)
+++ trunk/gpomme/gpomme.c	2007-12-04 11:15:51 UTC (rev 413)
@@ -43,7 +43,10 @@
 #include <libintl.h>
 
 #include <gtk/gtk.h>
+#include <gdk/gdkx.h>
 
+#include <X11/Xlib.h>
+
 #include <dbus/dbus.h>
 
 #include "gpomme.h"
@@ -319,6 +322,8 @@
   int who;
   double ratio;
 
+  Display *dpy;
+
   /* Disconnected, try to reconnect */
   if (conn == NULL)
     {
@@ -401,7 +406,8 @@
 	}
       else if (dbus_message_is_signal(msg, "org.pommed.signal.videoSwitch", "videoSwitch"))
 	{
-	  mbp_video_switch();
+	  dpy = GDK_WINDOW_XDISPLAY(GTK_WIDGET(mbp_w.window)->window);
+	  mbp_video_switch(dpy);
 	}
       else if (dbus_message_is_signal(msg, DBUS_INTERFACE_LOCAL, "Disconnected"))
 	{

Modified: trunk/wmpomme/wmpomme.c
===================================================================
--- trunk/wmpomme/wmpomme.c	2007-12-01 22:19:08 UTC (rev 412)
+++ trunk/wmpomme/wmpomme.c	2007-12-04 11:15:51 UTC (rev 413)
@@ -38,6 +38,7 @@
 
 #include <signal.h>
 
+#include <X11/Xlib.h>
 #include <X11/xpm.h>
 
 #include <dbus/dbus.h>
@@ -182,7 +183,7 @@
 	}
       else if (dbus_message_is_signal(msg, "org.pommed.signal.videoSwitch", "videoSwitch"))
 	{
-	  mbp_video_switch();
+	  mbp_video_switch(display);
 	}
       else if (dbus_message_is_signal(msg, DBUS_INTERFACE_LOCAL, "Disconnected"))
 	{




More information about the Pommed-commits mailing list