int main (int argc, char **argv) { DBusGConnection *bus; GError *error = NULL; DBusGProxy *proxy; gboolean run = FALSE; gboolean quit = FALSE; gboolean config = FALSE; gboolean version = FALSE; gboolean reload = FALSE; gboolean no_daemon = FALSE; gboolean debug = FALSE; gboolean dump = FALSE; gchar *client_id = NULL; GOptionEntry option_entries[] = { { "run",'r', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &run, NULL, NULL }, { "no-daemon",'\0' , G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &no_daemon, N_("Do not daemonize"), NULL }, { "debug",'\0' , G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &debug, N_("Enable debugging"), NULL }, { "dump",'\0' , G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &dump, N_("Dump all information"), NULL }, { "restart", '\0', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &reload, N_("Restart the running instance of Xfce power manager"), NULL}, { "customize", 'c', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &config, N_("Show the configuration dialog"), NULL }, { "quit", 'q', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &quit, N_("Quit any running xfce power manager"), NULL }, { "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &version, N_("Version information"), NULL }, { "sm-client-id", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &client_id, NULL, NULL }, { NULL, }, }; #if !GLIB_CHECK_VERSION (2, 32, 0) if ( !g_thread_supported () ) g_thread_init (NULL); #endif dbus_g_thread_init (); xfce_textdomain (GETTEXT_PACKAGE, LOCALEDIR, "UTF-8"); g_set_application_name (PACKAGE_NAME); if (!gtk_init_with_args (&argc, &argv, (gchar *)"", option_entries, (gchar *)PACKAGE, &error)) { if (G_LIKELY (error)) { g_printerr ("%s: %s.\n", G_LOG_DOMAIN, error->message); g_printerr (_("Type '%s --help' for usage."), G_LOG_DOMAIN); g_printerr ("\n"); g_error_free (error); } else { g_error ("Unable to open display."); } return EXIT_FAILURE; } if ( version ) { show_version (); } xfpm_debug_init (debug); if ( dump == FALSE && debug == FALSE && no_daemon == FALSE && daemon(0,0) ) { g_critical ("Could not daemonize"); } bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error); if ( error ) { xfce_dialog_show_error (NULL, error, "%s", _("Unable to get connection to the message bus session")); g_error ("%s: \n", error->message); } if ( quit ) { if (!xfpm_dbus_name_has_owner (dbus_g_connection_get_connection(bus), "org.xfce.PowerManager") ) { g_print (_("Xfce power manager is not running")); g_print ("\n"); return EXIT_SUCCESS; } else { proxy = dbus_g_proxy_new_for_name (bus, "org.xfce.PowerManager", "/org/xfce/PowerManager", "org.xfce.Power.Manager"); if ( !proxy ) { g_critical ("Failed to get proxy"); dbus_g_connection_unref(bus); return EXIT_FAILURE; } xfpm_manager_dbus_client_quit (proxy , &error); g_object_unref (proxy); if ( error) { g_critical ("Failed to send quit message %s:\n", error->message); g_error_free (error); } } return EXIT_SUCCESS; } if ( config ) { g_spawn_command_line_async ("xfce4-power-manager-settings", &error); if ( error ) { g_critical ("Failed to execute xfce4-power-manager-settings: %s", error->message); g_error_free (error); return EXIT_FAILURE; } return EXIT_SUCCESS; } if ( reload ) { if (!xfpm_dbus_name_has_owner (dbus_g_connection_get_connection (bus), "org.xfce.PowerManager") && !xfpm_dbus_name_has_owner (dbus_g_connection_get_connection (bus), "org.freedesktop.PowerManagement")) { g_print ("Xfce power manager is not running\n"); xfpm_start (bus, client_id, dump); } proxy = dbus_g_proxy_new_for_name (bus, "org.xfce.PowerManager", "/org/xfce/PowerManager", "org.xfce.Power.Manager"); if ( !proxy ) { g_critical ("Failed to get proxy"); dbus_g_connection_unref (bus); return EXIT_FAILURE; } if ( !xfpm_manager_dbus_client_restart (proxy, NULL) ) { g_critical ("Unable to send reload message"); g_object_unref (proxy); dbus_g_connection_unref (bus); return EXIT_SUCCESS; } return EXIT_SUCCESS; } if (dump) { if (xfpm_dbus_name_has_owner (dbus_g_connection_get_connection (bus), "org.xfce.PowerManager")) { xfpm_dump_remote (bus); return EXIT_SUCCESS; } } if (xfpm_dbus_name_has_owner (dbus_g_connection_get_connection (bus), "org.freedesktop.PowerManagement") ) { g_print ("%s: %s\n", _("Xfce Power Manager"), _("Another power manager is already running")); } else if (xfpm_dbus_name_has_owner (dbus_g_connection_get_connection(bus), "org.xfce.PowerManager")) { g_print (_("Xfce power manager is already running")); g_print ("\n"); return EXIT_SUCCESS; } else { xfpm_start (bus, client_id, dump); } return EXIT_SUCCESS; }
int main (int argc, char **argv) { GDBusConnection *bus; GError *error = NULL; XfpmPowerManager *proxy; GOptionContext *octx; gboolean run = FALSE; gboolean quit = FALSE; gboolean config = FALSE; gboolean version = FALSE; gboolean reload = FALSE; gboolean no_daemon = FALSE; gboolean debug = FALSE; gboolean dump = FALSE; gchar *client_id = NULL; GOptionEntry option_entries[] = { { "run",'r', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &run, NULL, NULL }, { "no-daemon",'\0' , G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &no_daemon, N_("Do not daemonize"), NULL }, { "debug",'\0' , G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &debug, N_("Enable debugging"), NULL }, { "dump",'\0' , G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &dump, N_("Dump all information"), NULL }, { "restart", '\0', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &reload, N_("Restart the running instance of Xfce power manager"), NULL}, { "customize", 'c', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &config, N_("Show the configuration dialog"), NULL }, { "quit", 'q', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &quit, N_("Quit any running xfce power manager"), NULL }, { "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &version, N_("Version information"), NULL }, { "sm-client-id", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &client_id, NULL, NULL }, { NULL, }, }; /* Parse the options */ octx = g_option_context_new(""); g_option_context_set_ignore_unknown_options(octx, TRUE); g_option_context_add_main_entries(octx, option_entries, NULL); g_option_context_add_group(octx, xfce_sm_client_get_option_group(argc, argv)); /* We can't add the following command because it will invoke gtk_init before we have a chance to fork. g_option_context_add_group(octx, gtk_get_option_group(TRUE)); */ if(!g_option_context_parse(octx, &argc, &argv, &error)) { g_printerr(_("Failed to parse arguments: %s\n"), error->message); g_option_context_free(octx); g_error_free(error); return EXIT_FAILURE; } g_option_context_free(octx); if ( version ) show_version (); /* Fork if needed */ if ( dump == FALSE && debug == FALSE && no_daemon == FALSE && daemon(0,0) ) { g_critical ("Could not daemonize"); } /* Initialize */ xfce_textdomain (GETTEXT_PACKAGE, LOCALEDIR, "UTF-8"); g_set_application_name (PACKAGE_NAME); if (!gtk_init_check (&argc, &argv)) { if (G_LIKELY (error)) { g_printerr ("%s: %s.\n", G_LOG_DOMAIN, error->message); g_printerr (_("Type '%s --help' for usage."), G_LOG_DOMAIN); g_printerr ("\n"); g_error_free (error); } else { g_error ("Unable to open display."); } return EXIT_FAILURE; } xfpm_debug_init (debug); bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error); if ( error ) { xfce_dialog_show_error (NULL, error, "%s", _("Unable to get connection to the message bus session")); g_error ("%s: \n", error->message); } if ( quit ) { if (!xfpm_dbus_name_has_owner (bus, "org.xfce.PowerManager") ) { g_print (_("Xfce power manager is not running")); g_print ("\n"); return EXIT_SUCCESS; } else { proxy = xfpm_power_manager_proxy_new_sync (bus, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, "org.xfce.PowerManager", "/org/xfce/PowerManager", NULL, NULL); if ( !proxy ) { g_critical ("Failed to get proxy"); g_object_unref(bus); return EXIT_FAILURE; } xfpm_power_manager_call_quit_sync (proxy, NULL, &error); g_object_unref (proxy); if ( error) { g_critical ("Failed to send quit message %s:\n", error->message); g_error_free (error); } } return EXIT_SUCCESS; } if ( config ) { g_spawn_command_line_async ("xfce4-power-manager-settings", &error); if ( error ) { g_critical ("Failed to execute xfce4-power-manager-settings: %s", error->message); g_error_free (error); return EXIT_FAILURE; } return EXIT_SUCCESS; } if ( reload ) { if (!xfpm_dbus_name_has_owner (bus, "org.xfce.PowerManager") && !xfpm_dbus_name_has_owner (bus, "org.freedesktop.PowerManagement")) { g_print ("Xfce power manager is not running\n"); xfpm_start (bus, client_id, dump); } proxy = xfpm_power_manager_proxy_new_sync (bus, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, "org.xfce.PowerManager", "/org/xfce/PowerManager", NULL, NULL); if ( !proxy ) { g_critical ("Failed to get proxy"); g_object_unref (bus); return EXIT_FAILURE; } if ( !xfpm_power_manager_call_restart_sync (proxy, NULL, NULL) ) { g_critical ("Unable to send reload message"); g_object_unref (proxy); g_object_unref (bus); return EXIT_SUCCESS; } return EXIT_SUCCESS; } if (dump) { if (xfpm_dbus_name_has_owner (bus, "org.xfce.PowerManager")) { xfpm_dump_remote (bus); return EXIT_SUCCESS; } } if (xfpm_dbus_name_has_owner (bus, "org.freedesktop.PowerManagement") ) { g_print ("%s: %s\n", _("Xfce Power Manager"), _("Another power manager is already running")); } else if (xfpm_dbus_name_has_owner (bus, "org.xfce.PowerManager")) { g_print (_("Xfce power manager is already running")); g_print ("\n"); return EXIT_SUCCESS; } else { xfpm_start (bus, client_id, dump); } return EXIT_SUCCESS; }
int main (int argc, char **argv) { GtkWidget *dialog; XfpmUnique *unique; GError *error = NULL; DBusGConnection *bus; GHashTable *config_hash; gboolean has_battery; gboolean auth_suspend; gboolean auth_hibernate; gboolean can_suspend; gboolean can_hibernate; gboolean can_shutdown; gboolean has_lcd_brightness; gboolean has_sleep_button; gboolean has_hibernate_button; gboolean has_power_button; gboolean has_lid; gboolean start_xfpm_if_not_running; gboolean debug = FALSE; GdkNativeWindow socket_id = 0; gchar *device_id = NULL; XfconfChannel *channel; DBusGProxy *proxy; GOptionEntry option_entries[] = { { "socket-id", 's', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT, &socket_id, N_("Settings manager socket"), N_("SOCKET ID") }, { "device-id", 'd', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &device_id, N_("Display a specific device by UpDevice object path"), N_("UpDevice object path") }, { "debug", '\0', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &debug, N_("Enable debugging"), NULL }, { NULL, }, }; xfce_textdomain(GETTEXT_PACKAGE, LOCALEDIR, "UTF-8"); if( !gtk_init_with_args (&argc, &argv, (gchar *)"", option_entries, (gchar *)PACKAGE, &error)) { if( error) { g_printerr("%s: %s.\n", G_LOG_DOMAIN, error->message); g_printerr(_("Type '%s --help' for usage."), G_LOG_DOMAIN); g_printerr("\n"); g_error_free(error); } else { g_error("Unable to open display."); } return EXIT_FAILURE; } bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error); if ( error ) { g_error ("%s\n",error->message); } while ( !xfpm_dbus_name_has_owner (dbus_g_connection_get_connection(bus), "org.xfce.PowerManager") ) { g_print(_("Xfce power manager is not running")); g_print("\n"); start_xfpm_if_not_running = xfce_dialog_confirm (NULL, GTK_STOCK_EXECUTE, _("Run"), NULL, _("Xfce4 Power Manager is not running, do you want to launch it now?")); if ( start_xfpm_if_not_running ) { g_spawn_command_line_async("xfce4-power-manager",NULL); /* wait 2 seconds for xfpm to startup */ g_usleep ( 2 * 1000000 ); } else { /* continue without starting xfpm, this will probably error out */ break; } } unique = xfpm_unique_new ("org.xfce.PowerManager.Config"); if ( !xfpm_unique_app_is_running (unique) ) { if ( !xfconf_init(&error) ) { g_critical("xfconf init failed: %s using default settings\n", error->message); xfce_dialog_show_warning (NULL, _("Xfce Power Manager"), "%s", _("Failed to load power manager configuration, using defaults")); g_error_free (error); error = NULL; return EXIT_FAILURE; } #if !GLIB_CHECK_VERSION (2, 32, 0) if ( !g_thread_supported () ) g_thread_init (NULL); #endif dbus_g_thread_init (); channel = xfconf_channel_new(XFPM_CHANNEL_CFG); proxy = dbus_g_proxy_new_for_name(bus, "org.xfce.PowerManager", "/org/xfce/PowerManager", "org.xfce.Power.Manager"); xfpm_manager_dbus_client_get_config (proxy, &config_hash, &error); if ( error ) { g_critical ("Unable to get configuration information from xfce power manager: %s", error->message); xfce_dialog_show_error (NULL, error, "%s", _("Unable to connect to Xfce Power Manager")); g_error_free (error); return EXIT_FAILURE; } xfpm_debug_init (debug); has_battery = xfpm_string_to_bool (g_hash_table_lookup (config_hash, "has-battery")); has_lid = xfpm_string_to_bool (g_hash_table_lookup (config_hash, "has-lid")); can_suspend = xfpm_string_to_bool (g_hash_table_lookup (config_hash, "can-suspend")); can_hibernate = xfpm_string_to_bool (g_hash_table_lookup (config_hash, "can-hibernate")); auth_suspend = xfpm_string_to_bool (g_hash_table_lookup (config_hash, "auth-suspend")); auth_hibernate = xfpm_string_to_bool (g_hash_table_lookup (config_hash, "auth-hibernate")); has_lcd_brightness = xfpm_string_to_bool (g_hash_table_lookup (config_hash, "has-brightness")); has_sleep_button = xfpm_string_to_bool (g_hash_table_lookup (config_hash, "sleep-button")); has_power_button = xfpm_string_to_bool (g_hash_table_lookup (config_hash, "power-button")); has_hibernate_button = xfpm_string_to_bool (g_hash_table_lookup (config_hash, "hibernate-button")); can_shutdown = xfpm_string_to_bool (g_hash_table_lookup (config_hash, "can-shutdown")); g_hash_table_destroy (config_hash); dialog = xfpm_settings_dialog_new (channel, auth_suspend, auth_hibernate, can_suspend, can_hibernate, can_shutdown, has_battery, has_lcd_brightness, has_lid, has_sleep_button, has_hibernate_button, has_power_button, socket_id, device_id); g_signal_connect_swapped (unique, "ping-received", G_CALLBACK (gtk_window_present), dialog); gtk_main(); xfpm_dbus_release_name(dbus_g_connection_get_connection(bus), "org.xfce.PowerManager.Config"); dbus_g_connection_unref (bus); g_object_unref (proxy); g_object_unref (unique); return EXIT_SUCCESS; } return EXIT_SUCCESS; }