Index: xfce4-session/xfsm-shutdown-helper-sudo.c =================================================================== --- xfce4-session/xfsm-shutdown-helper-sudo.c (revision 22728) +++ xfce4-session/xfsm-shutdown-helper-sudo.c (working copy) @@ -1,6 +1,6 @@ /* $Id$ */ /*- - * Copyright (c) 2003-2004 Benedikt Meurer + * Copyright (c) 2003-2006 Benedikt Meurer * All rights reserved. * * This program is free software; you can redistribute it and/or modify @@ -53,6 +53,10 @@ #include #endif +#ifdef HAVE_DBUS +#include +#endif + #include #include @@ -65,9 +69,87 @@ FILE *infile; FILE *outfile; gboolean need_password; +#ifdef HAVE_DBUS + gboolean using_hal; +#endif }; +#ifdef HAVE_DBUS +static gboolean +hal_ping (void) +{ + DBusConnection *connection; + DBusMessage *message; + DBusMessage *result; + DBusError derror; + + dbus_error_init (&derror); + + /* connect to the system bus */ + connection = dbus_bus_get (DBUS_BUS_SYSTEM, &derror); + if (G_UNLIKELY (connection == NULL)) + return FALSE; + + /* ping HAL */ + message = dbus_message_new_method_call ("org.freedesktop.Hal", + "/org/freedesktop/Hal/devices/computer", + "org.freedesktop.DBus.Peer", + "Ping"); + result = dbus_connection_send_with_reply_and_block (connection, message, 2000, &derror); + dbus_message_unref (message); + + /* check if HAL is available */ + if (result == NULL) + { + dbus_error_free (&derror); + return FALSE; + } + + /* HAL is available */ + dbus_message_unref (result); + return TRUE; +} + + +static gboolean +hal_send_command (const gchar *method_name) +{ + DBusConnection *connection; + DBusMessage *message; + DBusMessage *result; + DBusError derror; + + dbus_error_init (&derror); + + /* connect to the system bus */ + connection = dbus_bus_get (DBUS_BUS_SYSTEM, &derror); + if (G_UNLIKELY (connection == NULL)) + return FALSE; + + /* send the command to HAL */ + message = dbus_message_new_method_call ("org.freedesktop.Hal", + "/org/freedesktop/Hal/devices/computer", + "org.freedesktop.Hal.Device.SystemPowerManagement", + method_name); + result = dbus_connection_send_with_reply_and_block (connection, message, 2000, &derror); + dbus_message_unref (message); + + /* check if the command succeed */ + if (result == NULL) + { + g_warning ("Failed to send command \"%s\" to HAL: %s", method_name, derror.message); + dbus_error_free (&derror); + return FALSE; + } + + /* the command succeed */ + dbus_message_unref (result); + return TRUE; +} +#endif + + XfsmShutdownHelper* xfsm_shutdown_helper_spawn (void) { @@ -80,6 +162,21 @@ int result; int n; +#ifdef HAVE_DBUS + /* check if we can use HAL first */ + if (hal_ping ()) + { + helper = g_new0 (XfsmShutdownHelper, 1); + helper->using_hal = TRUE; + return helper; + } + else + { + g_warning ("HAL is not available, falling back to sudo " + "to shutdown the system from within Xfce."); + } +#endif + sudo = g_find_program_in_path ("sudo"); if (G_UNLIKELY (sudo == NULL)) { @@ -279,6 +376,18 @@ g_return_val_if_fail (helper != NULL, FALSE); g_return_val_if_fail (!helper->need_password, FALSE); +#ifdef HAVE_DBUS + /* check if we're using HAL */ + if (helper->using_hal) + { + /* send the command to HAL */ + if (command == XFSM_SHUTDOWN_REBOOT) + return hal_send_command ("Reboot"); + else + return hal_send_command ("Shutdown"); + } +#endif + fprintf (helper->outfile, "%s\n", command_table[command]); fflush (helper->outfile); Index: xfce4-session/Makefile.am =================================================================== --- xfce4-session/Makefile.am (revision 22728) +++ xfce4-session/Makefile.am (working copy) @@ -1,5 +1,6 @@ INCLUDES = \ - -I$(top_srcdir) + -I$(top_srcdir) \ + -DDBUS_API_SUBJECT_TO_CHANGE man_MANS = xfce4-session.1 @@ -42,6 +43,7 @@ xfsm-startup.h xfce4_session_CFLAGS = \ + @DBUS_CFLAGS@ \ @GNOME_CFLAGS@ \ @LIBSM_CFLAGS@ \ @LIBX11_CFLAGS@ \ @@ -60,6 +62,7 @@ @LIBX11_LIBS@ \ @LIBXFCE4MCS_CLIENT_LIBS@ \ @LIBXFCEGUI4_LIBS@ \ + @DBUS_LIBS@ \ @GNOME_LIBS@ xfce4_session_DEPENDENCIES = \ Index: configure.in.in =================================================================== --- configure.in.in (revision 22728) +++ configure.in.in (working copy) @@ -36,7 +36,7 @@ AC_MINIX() AM_CONDITIONAL([HAVE_OS_CYGWIN], [test x"`uname | grep \"CYGWIN\"`" != x""]) if test x"`uname | grep \"CYGWIN\"`" != x""; then - AC_DEFINE([HAVE_OS_CYGWIN], [1], [Compiling under cygwin]) + AC_DEFINE([HAVE_OS_CYGWIN], [1], [Compiling under cygwin]) fi dnl check for basic programs @@ -75,6 +75,10 @@ dnl Configure the MCS plugins XDT_XFCE_MCS_PLUGIN([XFCE_MCS_MANAGER], [4.3.90.2]) +dnl Check for DBUS support +XDT_CHECK_OPTIONAL_PACKAGE([DBUS], [dbus-1], [0.34], [dbus], + [D-Bus support (for HAL)], [yes]) + dnl Check for gnome support XDT_CHECK_OPTIONAL_PACKAGE([GNOME], [libgnome-2.0], [2.4.0], [gnome], [Assistive technologies support], [no])