[Pommed-commits] [SCM] pommed - hotkeys handler for Apple laptops branch, master, updated. 1.28-3-g9bd8a9e

Julien BLACHE jb at jblache.org
Mon Oct 19 19:34:16 UTC 2009


The following commit has been merged in the master branch:
commit 9bd8a9e2c864e660f6575c3c104f80edbbcec7f9
Author: Julien BLACHE <jb at jblache.org>
Date:   Mon Oct 19 21:32:46 2009 +0200

    Move VT state checking code into pommed.
    
    The VT state checking code needs to open /dev/ttyN, which is not always
    possible as a user. Pommed runs as root, so moving that code into pommed
    overcomes this issue.

diff --git a/ChangeLog b/ChangeLog
index 30c46f5..5bbf371 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -4,6 +4,9 @@ ChangeLog for pommed
 version 1.29:
 	- pommed: add support for newer nvidia backlight driver which
 	changed the sysfs layout.
+	- pommed: move VT state checking to pommed, exposed over DBus.
+	- gpomme: switch to asking pommed for the state of the VT.
+	- wmpomme: switch to asking pommed for the state of the VT.
 
 version 1.28:
 	- pommed: added support for the MacBookPro5,3 (15" MacBookPro
diff --git a/client-common/dbus-client.c b/client-common/dbus-client.c
index 1744d30..8912649 100644
--- a/client-common/dbus-client.c
+++ b/client-common/dbus-client.c
@@ -1,7 +1,7 @@
 /*
  * dbus-client.c -- shared DBus client routines for pommed clients
  *
- * Copyright (C) 2006-2007 Julien BLACHE <jb at jblache.org>
+ * Copyright (C) 2006-2007, 2009 Julien BLACHE <jb at jblache.org>
  *
  * 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
@@ -238,6 +238,57 @@ mbp_call_audio_getmute(DBusPendingCallNotifyFunction cb, void *userdata)
   return 0;
 }
 
+int
+mbp_call_video_getvtstate(int vtnum, DBusPendingCallNotifyFunction cb, void *userdata)
+{
+  DBusMessage *msg;
+  DBusPendingCall *pending;
+
+  int ret;
+
+  msg = dbus_message_new_method_call("org.pommed", "/org/pommed/video",
+				     "org.pommed.video", "getVTState");
+
+  if (msg == NULL)
+    {
+      printf("Failed to create method call message\n");
+
+      return -1;
+    }
+
+  ret = dbus_message_append_args(msg,
+				 DBUS_TYPE_UINT32, &vtnum,
+				 DBUS_TYPE_INVALID);
+  if (ret == FALSE)
+    {
+      printf("Failed to add arguments\n");
+
+      dbus_message_unref(msg);
+
+      return -1;
+    }
+
+  ret = dbus_connection_send_with_reply(conn, msg, &pending, 250);
+  if (ret == FALSE)
+    {
+      printf("Could not send method call\n");
+
+      dbus_message_unref(msg);
+
+      return -1;
+    }
+
+  dbus_connection_flush(conn);
+
+  dbus_message_unref(msg);
+
+  dbus_pending_call_block(pending);
+
+  cb(pending, userdata);
+
+  return 0;
+}
+
 
 /* Error checking, mainly for replies to method calls */
 
diff --git a/client-common/dbus-client.h b/client-common/dbus-client.h
index 99c3313..27a37d6 100644
--- a/client-common/dbus-client.h
+++ b/client-common/dbus-client.h
@@ -38,6 +38,9 @@ mbp_call_audio_getvolume(DBusPendingCallNotifyFunction cb, void *userdata);
 int
 mbp_call_audio_getmute(DBusPendingCallNotifyFunction cb, void *userdata);
 
+int
+mbp_call_video_getvtstate(int vtnum, DBusPendingCallNotifyFunction cb, void *userdata);
+
 
 /* Error checking */
 int
diff --git a/client-common/video-client.c b/client-common/video-client.c
index fe9e013..40175c3 100644
--- a/client-common/video-client.c
+++ b/client-common/video-client.c
@@ -1,7 +1,7 @@
 /*
  * video-client.c -- shared video switch routines for pommed clients
  *
- * Copyright (C) 2007 Julien BLACHE <jb at jblache.org>
+ * Copyright (C) 2007, 2009 Julien BLACHE <jb at jblache.org>
  *
  * Some code below taken from GDM where noted.
  *
@@ -47,7 +47,7 @@ static char *vsw_user = NULL;
  * Get the VT number X is running on
  * (code taken from GDM, daemon/getvt.c, GPLv2+)
  */
-static int
+int
 mbp_get_x_vtnum(Display *dpy)
 {
   Atom prop;
@@ -106,73 +106,18 @@ mbp_get_x_vtnum(Display *dpy)
   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 < 0) || (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(Display *dpy)
+mbp_video_switch(void)
 {
   struct passwd *pw;
   char *vsw = NULL;
 
   int ret;
 
-  if (!mbp_frontend_is_active(dpy))
-    return;
-
   if (vsw_user == NULL)
     {
       pw = getpwuid(getuid());
diff --git a/client-common/video-client.h b/client-common/video-client.h
index 55eba0c..20bcee3 100644
--- a/client-common/video-client.h
+++ b/client-common/video-client.h
@@ -9,8 +9,11 @@
 #define VIDEO_SWITCH_USER        "/.videoswitch"
 
 
+int
+mbp_get_x_vtnum(Display *dpy);
+
 void
-mbp_video_switch(Display *dpy);
+mbp_video_switch(void);
 
 
 #endif /* !__MBP_VIDEO_CLIENT_H__ */
diff --git a/gpomme/gpomme.c b/gpomme/gpomme.c
index d496f99..c0839cd 100644
--- a/gpomme/gpomme.c
+++ b/gpomme/gpomme.c
@@ -2,7 +2,7 @@
  * gpomme - GTK application for use with pommed
  *
  * Copyright (C) 2006, 2008 Soeren SONNENBURG <debian at nn7.de>
- * Copyright (C) 2006-2008 Julien BLACHE <jb at jblache.org>
+ * Copyright (C) 2006-2009 Julien BLACHE <jb at jblache.org>
  * Copyright (C) 2007 daniel g. siegel <dgsiegel at gmail.com>
  *
  * Portions of the GTK code below were shamelessly
@@ -306,6 +306,36 @@ create_window(void)
 }
 
 
+static void
+mbp_video_getvtstate_cb(DBusPendingCall *pending, void *status)
+{
+  DBusMessage *msg;
+
+  msg = dbus_pending_call_steal_reply(pending);
+
+  if (msg == NULL)
+    {
+      fprintf(stderr, "Could not steal reply\n");
+
+      dbus_pending_call_unref(pending);
+
+      return;
+    }
+
+  dbus_pending_call_unref(pending);
+
+  if (!mbp_dbus_check_error(msg))
+    {
+      dbus_message_get_args(msg, &dbus_err,
+			    DBUS_TYPE_BOOLEAN, (int *)status,
+			    DBUS_TYPE_INVALID);
+    }
+  else
+    *(int *)status = -1;
+
+  dbus_message_unref(msg);
+}
+
 static gboolean
 mbp_dbus_reconnect(gpointer userdata);
 
@@ -384,8 +414,19 @@ mbp_dbus_listen(DBusConnection *lconn, DBusMessage *msg, gpointer userdata)
     }
   else if (dbus_message_is_signal(msg, "org.pommed.signal.videoSwitch", "videoSwitch"))
     {
+      int vtnum;
+      int vtstate;
+      int ret;
+
       dpy = GDK_WINDOW_XDISPLAY(GTK_WIDGET(mbp_w.window)->window);
-      mbp_video_switch(dpy);
+
+      vtnum = mbp_get_x_vtnum(dpy);
+
+      ret = mbp_call_video_getvtstate(vtnum, mbp_video_getvtstate_cb, &vtstate);
+      if ((ret < 0) || (vtstate < 0))
+	fprintf(stderr, "video getVTState call failed !\n");
+      else if (vtstate == 1)
+	mbp_video_switch();
     }
   else if (dbus_message_is_signal(msg, DBUS_INTERFACE_LOCAL, "Disconnected"))
     {
diff --git a/pommed/dbus.c b/pommed/dbus.c
index 5c932ab..26dc9c9 100644
--- a/pommed/dbus.c
+++ b/pommed/dbus.c
@@ -1,7 +1,7 @@
 /*
  * pommed - Apple laptops hotkeys handler daemon
  *
- * Copyright (C) 2006-2007 Julien BLACHE <jb at jblache.org>
+ * Copyright (C) 2006-2007, 2009 Julien BLACHE <jb at jblache.org>
  *
  * 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
@@ -35,6 +35,7 @@
 #include "kbd_backlight.h"
 #include "ambient.h"
 #include "audio.h"
+#include "video.h"
 #include "cd_eject.h"
 
 
@@ -603,6 +604,55 @@ process_audio_getmute_call(DBusMessage *req)
   dbus_message_unref(msg);
 }
 
+static void
+process_video_getvtstate_call(DBusMessage *req)
+{
+  DBusMessage *msg;
+
+  int vtnum;
+  int vtstate;
+  int ret;
+
+  logdebug("Got video getVTState call\n");
+
+  ret = dbus_message_get_args(req, &err, DBUS_TYPE_UINT32, &vtnum, DBUS_TYPE_INVALID);
+  if (ret == FALSE)
+    {
+      logdebug("video getVTState call with no/inappropriate arguments ?!\n");
+
+      return;
+    }
+
+  /* Check VT state */
+  vtstate = video_vt_active(vtnum);
+
+  msg = dbus_message_new_method_return(req);
+
+  ret = dbus_message_append_args(msg,
+				 DBUS_TYPE_BOOLEAN, &vtstate,
+				 DBUS_TYPE_INVALID);
+  if (ret == FALSE)
+    {
+      logdebug("Failed to add arguments\n");
+
+      dbus_message_unref(msg);
+
+      return;
+    }
+
+  ret = dbus_connection_send(conn, msg, NULL);
+  if (ret == FALSE)
+    {
+      logdebug("Could not send video getVTState reply\n");
+
+      dbus_message_unref(msg);
+
+      return;
+    }
+
+  dbus_message_unref(msg);
+}
+
 
 static void
 process_lcd_backlight_step_call(DBusMessage *req, int dir)
@@ -765,6 +815,8 @@ mbpdbus_process_requests(DBusConnection *lconn, DBusMessage *msg, void *data)
     process_audio_getvolume_call(msg);
   else if (dbus_message_is_method_call(msg, "org.pommed.audio", "getMute"))
     process_audio_getmute_call(msg);
+  else if (dbus_message_is_method_call(msg, "org.pommed.video", "getVTState"))
+    process_video_getvtstate_call(msg);
   // Set methods
   else if (dbus_message_is_method_call(msg, "org.pommed.lcdBacklight", "levelUp"))
     process_lcd_backlight_step_call(msg, STEP_UP);
diff --git a/pommed/video.c b/pommed/video.c
index 1e4a2f7..0d256f0 100644
--- a/pommed/video.c
+++ b/pommed/video.c
@@ -1,7 +1,7 @@
 /*
  * pommed - Apple laptops hotkeys handler daemon
  *
- * Copyright (C) 2007 Julien BLACHE <jb at jblache.org>
+ * Copyright (C) 2007, 2009 Julien BLACHE <jb at jblache.org>
  *
  * 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
@@ -19,6 +19,18 @@
 
 #include <stdio.h>
 
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+
+#include <errno.h>
+
+#include <linux/vt.h>
+
 #include <syslog.h>
 
 #include "pommed.h"
@@ -31,3 +43,46 @@ video_switch(void)
 {
   mbpdbus_send_video_switch();
 }
+
+int
+video_vt_active(int vt)
+{
+  int fd;
+  char buf[16];
+  struct vt_stat vtstat;
+
+  int ret;
+
+  ret = snprintf(buf, sizeof(buf), "/dev/tty%d", vt);
+  if ((ret < 0) || (ret >= sizeof(buf)))
+    return 1;
+
+  /* Try to open the VT the client's 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;
+
+  /* The VT isn't a tty, WTF?! */
+  if (!isatty(fd))
+    {
+      close(fd);
+      return 0;
+    }
+
+  /* Get VT state, includes currently-active VT number */
+  ret = ioctl(fd, VT_GETSTATE, &vtstat);
+  close(fd);
+
+  if (ret < 0)
+    return 1;
+
+  return (vt == vtstat.v_active);
+}
diff --git a/pommed/video.h b/pommed/video.h
index 9600921..f711fc2 100644
--- a/pommed/video.h
+++ b/pommed/video.h
@@ -9,6 +9,9 @@
 void
 video_switch(void);
 
+int
+video_vt_active(int vt);
+
 
 #endif /* !__VIDEO_H__ */
 
diff --git a/wmpomme/wmpomme.c b/wmpomme/wmpomme.c
index 587d387..5b6ebf3 100644
--- a/wmpomme/wmpomme.c
+++ b/wmpomme/wmpomme.c
@@ -1,7 +1,7 @@
 /*
  * wmpomme -- WindowMaker dockapp for use with pommed
  *
- * Copyright (C) 2006-2008 Julien BLACHE <jb at jblache.org>
+ * Copyright (C) 2006-2009 Julien BLACHE <jb at jblache.org>
  *
  * Based on wmwave by Carsten Schuermann <carsten at schuermann.org>
  * wmwave derived from:
@@ -129,6 +129,10 @@ wmmbp_dbus_init(void)
   return 0;
 }
 
+/* Forward */
+void
+wmmbp_video_getvtstate_cb(DBusPendingCall *pending, void *status);
+
 void
 mbp_dbus_listen(void)
 {
@@ -207,7 +211,17 @@ mbp_dbus_listen(void)
 	}
       else if (dbus_message_is_signal(msg, "org.pommed.signal.videoSwitch", "videoSwitch"))
 	{
-	  mbp_video_switch(display);
+	  int vtnum;
+	  int vtstate;
+	  int ret;
+
+	  vtnum = mbp_get_x_vtnum(display);
+
+	  ret = mbp_call_video_getvtstate(vtnum, wmmbp_video_getvtstate_cb, &vtstate);
+	  if ((ret < 0) || (vtstate < 0))
+	    fprintf(stderr, "video getVTState call failed !\n");
+	  else if (vtstate == 1)
+	    mbp_video_switch();
 	}
       else if (dbus_message_is_signal(msg, DBUS_INTERFACE_LOCAL, "Disconnected"))
 	{
@@ -384,6 +398,36 @@ wmmbp_audio_getmute_cb(DBusPendingCall *pending, void *status)
   dbus_message_unref(msg);
 }
 
+void
+wmmbp_video_getvtstate_cb(DBusPendingCall *pending, void *status)
+{
+  DBusMessage *msg;
+
+  msg = dbus_pending_call_steal_reply(pending);
+
+  if (msg == NULL)
+    {
+      fprintf(stderr, "Could not steal reply\n");
+
+      dbus_pending_call_unref(pending);
+
+      return;
+    }
+
+  dbus_pending_call_unref(pending);
+
+  if (!mbp_dbus_check_error(msg))
+    {
+      dbus_message_get_args(msg, &dbus_err,
+			    DBUS_TYPE_BOOLEAN, (int *)status,
+			    DBUS_TYPE_INVALID);
+    }
+  else
+    *(int *)status = -1;
+
+  dbus_message_unref(msg);
+}
+
 
 void
 wmmbp_get_values(void)

-- 
pommed - hotkeys handler for Apple laptops



More information about the Pommed-commits mailing list