2009-02-19 11:12:29 +03:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Evoke, head honcho of everything
|
|
|
|
* Part of Equinox Desktop Environment (EDE).
|
|
|
|
* Copyright (c) 2007-2009 EDE Authors.
|
|
|
|
*
|
|
|
|
* This program is licensed under terms of the
|
|
|
|
* GNU General Public License version 2 or newer.
|
|
|
|
* See COPYING for details.
|
|
|
|
*/
|
|
|
|
|
2009-10-13 14:41:08 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2009-02-19 11:12:29 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <edelib/File.h>
|
2009-04-16 15:32:31 +04:00
|
|
|
#include <edelib/FileTest.h>
|
2009-02-19 11:12:29 +03:00
|
|
|
#include <edelib/Debug.h>
|
|
|
|
#include <edelib/Config.h>
|
|
|
|
#include <edelib/Resource.h>
|
|
|
|
#include <edelib/StrUtil.h>
|
|
|
|
#include <edelib/MessageBox.h>
|
|
|
|
#include <edelib/Nls.h>
|
2009-02-24 19:27:38 +03:00
|
|
|
#include <edelib/Run.h>
|
2012-09-18 21:49:48 +04:00
|
|
|
|
2014-06-20 13:29:18 +04:00
|
|
|
#include <FL/Fl.H>
|
|
|
|
|
2012-09-18 21:49:48 +04:00
|
|
|
#if EDELIB_HAVE_DBUS
|
2009-02-26 15:04:29 +03:00
|
|
|
#include <edelib/EdbusMessage.h>
|
|
|
|
#include <edelib/EdbusConnection.h>
|
2012-09-18 21:49:48 +04:00
|
|
|
#endif
|
2009-02-19 11:12:29 +03:00
|
|
|
|
|
|
|
#include "EvokeService.h"
|
|
|
|
#include "Splash.h"
|
|
|
|
#include "Logout.h"
|
|
|
|
#include "Xsm.h"
|
2009-02-25 18:03:37 +03:00
|
|
|
#include "Xshutdown.h"
|
2009-02-19 11:12:29 +03:00
|
|
|
|
|
|
|
EDELIB_NS_USING(Config)
|
|
|
|
EDELIB_NS_USING(Resource)
|
|
|
|
EDELIB_NS_USING(file_remove)
|
2009-04-16 15:32:31 +04:00
|
|
|
EDELIB_NS_USING(file_test)
|
2009-02-19 11:12:29 +03:00
|
|
|
EDELIB_NS_USING(str_trim)
|
2009-10-13 14:41:08 +04:00
|
|
|
EDELIB_NS_USING(run_sync)
|
|
|
|
EDELIB_NS_USING(alert)
|
2011-11-21 03:38:14 +04:00
|
|
|
EDELIB_NS_USING(ask)
|
2009-04-16 15:32:31 +04:00
|
|
|
EDELIB_NS_USING(FILE_TEST_IS_REGULAR)
|
2009-02-19 11:12:29 +03:00
|
|
|
|
2012-09-18 21:49:48 +04:00
|
|
|
#if EDELIB_HAVE_DBUS
|
|
|
|
EDELIB_NS_USING(EdbusMessage)
|
|
|
|
EDELIB_NS_USING(EdbusConnection)
|
|
|
|
EDELIB_NS_USING(EdbusError)
|
|
|
|
EDELIB_NS_USING(EDBUS_SESSION)
|
|
|
|
EDELIB_NS_USING(EDBUS_SYSTEM)
|
|
|
|
#endif
|
|
|
|
|
2015-12-17 03:05:32 +03:00
|
|
|
#define CONFIG_GET_STRVAL(object, section, key, buff) object.get(section, key, buff, sizeof(buff))
|
2009-10-13 14:41:08 +04:00
|
|
|
|
|
|
|
/* stolen from xfce's xfsm-shutdown-helper */
|
2015-12-16 20:05:02 +03:00
|
|
|
#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__)
|
2009-10-13 14:41:08 +04:00
|
|
|
# define POWEROFF_CMD "/sbin/shutdown -p now"
|
|
|
|
# define REBOOT_CMD "/sbin/shutdown -r now"
|
2015-12-16 20:05:02 +03:00
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
# define POWEROFF_CMD "/sbin/shutdown -hp now"
|
|
|
|
# define REBOOT_CMD "/sbin/shutdown -r now"
|
2009-10-13 14:41:08 +04:00
|
|
|
#elif defined(sun) || defined(__sun)
|
|
|
|
# define POWEROFF_CMD "/usr/sbin/shutdown -i 5 -g 0 -y"
|
|
|
|
# define REBOOT_CMD "/usr/sbin/shutdown -i 6 -g 0 -y"
|
|
|
|
#else
|
|
|
|
# define POWEROFF_CMD "/sbin/shutdown -h now"
|
|
|
|
# define REBOOT_CMD "/sbin/shutdown -r now"
|
2009-02-19 11:12:29 +03:00
|
|
|
#endif
|
|
|
|
|
2009-03-03 14:47:30 +03:00
|
|
|
static Atom XA_EDE_EVOKE_SHUTDOWN_ALL;
|
|
|
|
static Atom XA_EDE_EVOKE_QUIT;
|
2009-02-19 11:12:29 +03:00
|
|
|
|
|
|
|
static int get_int_property_value(Atom at) {
|
|
|
|
Atom real;
|
|
|
|
int format;
|
|
|
|
unsigned long n, extra;
|
|
|
|
unsigned char* prop;
|
|
|
|
|
|
|
|
int status = XGetWindowProperty(fl_display, RootWindow(fl_display, fl_screen),
|
|
|
|
at, 0L, 0x7fffffff, False, XA_CARDINAL, &real, &format, &n, &extra,
|
|
|
|
(unsigned char**)&prop);
|
|
|
|
int ret = -1;
|
|
|
|
if(status != Success && !prop)
|
|
|
|
return ret;
|
|
|
|
ret = int(*(long*)prop);
|
|
|
|
XFree(prop);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-02-26 15:04:29 +03:00
|
|
|
static void send_dbus_ede_quit(void) {
|
2012-09-18 21:49:48 +04:00
|
|
|
#ifdef EDELIB_HAVE_DBUS
|
2009-02-26 15:04:29 +03:00
|
|
|
EdbusConnection c;
|
|
|
|
E_RETURN_IF_FAIL(c.connect(EDBUS_SESSION));
|
|
|
|
|
|
|
|
EdbusMessage msg;
|
2009-07-07 15:57:45 +04:00
|
|
|
msg.create_signal("/org/equinoxproject/Shutdown", "org.equinoxproject.Shutdown", "Shutdown");
|
2009-02-26 15:04:29 +03:00
|
|
|
c.send(msg);
|
2012-09-18 21:49:48 +04:00
|
|
|
#endif
|
2009-02-26 15:04:29 +03:00
|
|
|
}
|
|
|
|
|
2012-11-26 23:38:24 +04:00
|
|
|
#define CONSOLEKIT_SERVICE "org.freedesktop.ConsoleKit"
|
|
|
|
#define CONSOLEKIT_PATH "/org/freedesktop/ConsoleKit/Manager"
|
|
|
|
#define CONSOLEKIT_INTERFACE "org.freedesktop.ConsoleKit.Manager"
|
|
|
|
|
2013-01-05 07:39:09 +04:00
|
|
|
static bool do_shutdown_or_restart(bool restart, void (*x_shutdown_func)(void)) {
|
2011-11-21 03:38:14 +04:00
|
|
|
const char *action;
|
2012-09-18 21:49:48 +04:00
|
|
|
int r = 1;
|
2011-11-21 03:38:14 +04:00
|
|
|
|
2012-09-18 21:49:48 +04:00
|
|
|
#ifdef EDELIB_HAVE_DBUS
|
2012-11-26 23:38:24 +04:00
|
|
|
const char *can_do_action;
|
|
|
|
|
2009-10-13 14:41:08 +04:00
|
|
|
EdbusConnection c;
|
|
|
|
if(!c.connect(EDBUS_SYSTEM)) {
|
2012-11-26 23:38:24 +04:00
|
|
|
alert(_("Unable to connect to DBus daemon. Make sure system DBus daemon is running and have permissions to access it"));
|
2009-10-13 14:41:08 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-21 03:38:14 +04:00
|
|
|
/* determine what to check first; ConsoleKit shutdown action calls 'Stop' */
|
2012-11-26 23:38:24 +04:00
|
|
|
can_do_action = restart ? "CanRestart" : "CanStop";
|
|
|
|
action = restart ? "Restart" : "Stop";
|
2009-10-13 14:41:08 +04:00
|
|
|
|
2011-11-21 03:38:14 +04:00
|
|
|
EdbusMessage msg, ret;
|
2012-11-26 23:38:24 +04:00
|
|
|
msg.create_method_call(CONSOLEKIT_SERVICE,
|
|
|
|
CONSOLEKIT_PATH,
|
|
|
|
CONSOLEKIT_INTERFACE,
|
|
|
|
can_do_action);
|
2009-10-13 14:41:08 +04:00
|
|
|
|
|
|
|
if(!c.send_with_reply_and_block(msg, 100, ret)) {
|
2011-11-21 03:38:14 +04:00
|
|
|
EdbusError *err = c.error();
|
2012-11-26 23:38:24 +04:00
|
|
|
alert(_("Unable to communicate with DBus properly. Got '%s' (%s)"), err->name(), err->message());
|
2011-11-21 03:38:14 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ret.size() != 1) {
|
|
|
|
alert(_("Bad reply size. Expected '1' element, but got '%i' instead"), ret.size());
|
2009-10-13 14:41:08 +04:00
|
|
|
return false;
|
|
|
|
}
|
2011-11-21 03:38:14 +04:00
|
|
|
|
|
|
|
EdbusMessage::iterator it = ret.begin();
|
2012-11-26 23:38:24 +04:00
|
|
|
if(it->is_bool() && it->to_bool() == true) {
|
2013-01-05 07:39:09 +04:00
|
|
|
/* first close all X children */
|
|
|
|
x_shutdown_func();
|
|
|
|
|
2012-11-26 23:38:24 +04:00
|
|
|
/* call action */
|
|
|
|
msg.create_method_call(CONSOLEKIT_SERVICE,
|
|
|
|
CONSOLEKIT_PATH,
|
|
|
|
CONSOLEKIT_INTERFACE,
|
|
|
|
action);
|
|
|
|
c.send(msg);
|
2011-11-21 03:38:14 +04:00
|
|
|
return true;
|
2012-11-26 23:38:24 +04:00
|
|
|
}
|
2011-11-21 03:38:14 +04:00
|
|
|
|
2012-09-18 21:49:48 +04:00
|
|
|
r = ask(_("You are not allowed to execute this command. Please consult ConsoleKit documentation on how to allow privileged actions. "
|
|
|
|
"Would you like to try to execute system commands?"));
|
|
|
|
#endif /* EDELIB_HAVE_DBUS */
|
2011-11-21 03:38:14 +04:00
|
|
|
|
|
|
|
/* try to do things manually */
|
|
|
|
if(!r) return false;
|
2012-11-26 23:38:24 +04:00
|
|
|
action = _("restart");
|
2009-10-13 14:41:08 +04:00
|
|
|
|
|
|
|
if(restart) {
|
2011-11-21 03:38:14 +04:00
|
|
|
r = run_sync(REBOOT_CMD);
|
2009-10-13 14:41:08 +04:00
|
|
|
} else {
|
2011-11-21 03:38:14 +04:00
|
|
|
r = run_sync(POWEROFF_CMD);
|
2012-11-26 23:38:24 +04:00
|
|
|
action = _("shutdown");
|
2009-10-13 14:41:08 +04:00
|
|
|
}
|
|
|
|
|
2011-11-21 03:38:14 +04:00
|
|
|
if(r) {
|
2012-11-26 23:38:24 +04:00
|
|
|
alert(_("Unable to %s the computer. Probably you do not have enough permissions to do that"), action);
|
2009-10-13 14:41:08 +04:00
|
|
|
return false;
|
|
|
|
}
|
2011-11-21 03:38:14 +04:00
|
|
|
|
2009-10-13 14:41:08 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-19 11:12:29 +03:00
|
|
|
EvokeService::EvokeService() : lock_name(NULL), xsm(NULL), is_running(false) {
|
|
|
|
/* TODO: or add setup_atoms() function */
|
|
|
|
XA_EDE_EVOKE_SHUTDOWN_ALL = XInternAtom(fl_display, "_EDE_EVOKE_SHUTDOWN_ALL", False);
|
|
|
|
XA_EDE_EVOKE_QUIT = XInternAtom(fl_display, "_EDE_EVOKE_QUIT", False);
|
|
|
|
}
|
|
|
|
|
|
|
|
EvokeService::~EvokeService() {
|
|
|
|
clear_startup_items();
|
|
|
|
stop_xsettings_manager(true);
|
|
|
|
remove_lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
EvokeService* EvokeService::instance(void) {
|
|
|
|
static EvokeService es;
|
|
|
|
return &es;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EvokeService::setup_lock(const char* name) {
|
2013-01-05 07:39:09 +04:00
|
|
|
E_RETURN_VAL_IF_FAIL(file_test(name, FILE_TEST_IS_REGULAR) == false, false);
|
2009-02-19 11:12:29 +03:00
|
|
|
|
|
|
|
FILE* f = fopen(name, "w");
|
2013-01-05 07:39:09 +04:00
|
|
|
E_RETURN_VAL_IF_FAIL(f != NULL, false);
|
|
|
|
|
2009-02-19 11:12:29 +03:00
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
lock_name = strdup(name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EvokeService::remove_lock(void) {
|
2013-01-05 07:39:09 +04:00
|
|
|
E_RETURN_IF_FAIL(lock_name != NULL);
|
2009-02-19 11:12:29 +03:00
|
|
|
|
|
|
|
file_remove(lock_name);
|
|
|
|
free(lock_name);
|
|
|
|
lock_name = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EvokeService::clear_startup_items(void) {
|
2013-01-05 07:39:09 +04:00
|
|
|
E_RETURN_IF_FAIL(startup_items.size() > 0);
|
2009-02-19 11:12:29 +03:00
|
|
|
|
2013-01-05 07:39:09 +04:00
|
|
|
StartupItemListIter it = startup_items.begin(), ite = startup_items.end();
|
|
|
|
while(it != ite) {
|
2009-02-19 11:12:29 +03:00
|
|
|
delete *it;
|
2013-01-05 07:39:09 +04:00
|
|
|
it = startup_items.erase(it);
|
|
|
|
}
|
2009-02-19 11:12:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void EvokeService::read_startup(void) {
|
|
|
|
#ifdef USE_LOCAL_CONFIG
|
|
|
|
/*
|
|
|
|
* this will load SETTINGS_FILENAME only from local directory;
|
|
|
|
* intended for development and testing only
|
|
|
|
*/
|
|
|
|
Config c;
|
|
|
|
int ret = c.load("ede-startup.conf");
|
|
|
|
#else
|
|
|
|
/* only system resource will be loaded; use ede-startup will be skipped */
|
|
|
|
Resource c;
|
2009-02-23 17:48:53 +03:00
|
|
|
int ret = c.load("ede-startup");
|
2009-02-19 11:12:29 +03:00
|
|
|
#endif
|
|
|
|
if(!ret) {
|
|
|
|
E_WARNING(E_STRLOC ": Unable to load EDE startup file\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-05 07:39:09 +04:00
|
|
|
char tokbuf[128], buf[128];
|
2009-02-19 11:12:29 +03:00
|
|
|
/* if nothing found, exit */
|
2013-01-05 07:39:09 +04:00
|
|
|
E_RETURN_IF_FAIL(CONFIG_GET_STRVAL(c, "Startup", "start_order", tokbuf));
|
2009-02-19 11:12:29 +03:00
|
|
|
|
2013-01-05 07:39:09 +04:00
|
|
|
if(CONFIG_GET_STRVAL(c, "Startup", "splash_theme", buf))
|
|
|
|
splash_theme = buf;
|
2009-02-19 11:12:29 +03:00
|
|
|
|
2013-01-05 07:39:09 +04:00
|
|
|
for(const char* sect = strtok(tokbuf, ","); sect; sect = strtok(NULL, ",")) {
|
2009-02-19 11:12:29 +03:00
|
|
|
/* remove leading/ending spaces, if exists */
|
2013-01-05 07:39:09 +04:00
|
|
|
str_trim(buf);
|
2009-02-19 11:12:29 +03:00
|
|
|
|
|
|
|
/* assure each startup item has 'exec' key */
|
2013-01-05 07:39:09 +04:00
|
|
|
if(!CONFIG_GET_STRVAL(c, sect, "exec", buf)) {
|
2009-02-19 11:12:29 +03:00
|
|
|
E_WARNING(E_STRLOC ": Startup item '%s' does not have anything to execute. Skipping...\n", sect);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
StartupItem *s = new StartupItem;
|
2013-01-05 07:39:09 +04:00
|
|
|
s->exec = buf;
|
2009-02-19 11:12:29 +03:00
|
|
|
|
2013-01-05 07:39:09 +04:00
|
|
|
if(CONFIG_GET_STRVAL(c, sect, "icon", buf))
|
|
|
|
s->icon = buf;
|
|
|
|
if(CONFIG_GET_STRVAL(c, sect, "description", buf))
|
|
|
|
s->description = buf;
|
2009-02-19 11:12:29 +03:00
|
|
|
|
|
|
|
startup_items.push_back(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EvokeService::run_startup(bool splash, bool dryrun) {
|
2013-01-05 07:39:09 +04:00
|
|
|
E_RETURN_IF_FAIL(startup_items.size() > 0);
|
2009-02-19 11:12:29 +03:00
|
|
|
|
|
|
|
Splash s(startup_items, splash_theme, splash, dryrun);
|
|
|
|
s.run();
|
|
|
|
clear_startup_items();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EvokeService::start_xsettings_manager(void) {
|
|
|
|
xsm = new Xsm;
|
|
|
|
|
|
|
|
if(Xsm::manager_running(fl_display, fl_screen)) {
|
2013-01-05 07:39:09 +04:00
|
|
|
int ret = ask(_("XSETTINGS manager already running on this screen. Would you like to replace it?"));
|
2009-02-19 11:12:29 +03:00
|
|
|
if(ret < 1) {
|
|
|
|
stop_xsettings_manager(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!xsm->init(fl_display, fl_screen)) {
|
2013-01-05 07:39:09 +04:00
|
|
|
alert(_("Unable to load XSETTINGS manager properly"));
|
2009-02-19 11:12:29 +03:00
|
|
|
stop_xsettings_manager(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(xsm->load_serialized())
|
|
|
|
xsm->notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EvokeService::stop_xsettings_manager(bool serialize) {
|
2013-01-05 07:39:09 +04:00
|
|
|
E_RETURN_IF_FAIL(xsm != NULL);
|
2009-02-19 11:12:29 +03:00
|
|
|
|
|
|
|
if(serialize)
|
|
|
|
xsm->save_serialized();
|
|
|
|
|
|
|
|
delete xsm;
|
|
|
|
xsm = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int EvokeService::handle(const XEvent* xev) {
|
|
|
|
if(xsm && xsm->should_terminate(xev)) {
|
|
|
|
E_DEBUG(E_STRLOC ": Terminating XSETTINGS manager on request by X event\n");
|
|
|
|
stop_xsettings_manager(true);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(xev->xproperty.atom == XA_EDE_EVOKE_QUIT) {
|
|
|
|
int val = get_int_property_value(XA_EDE_EVOKE_QUIT);
|
|
|
|
if(val == 1)
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(xev->xproperty.atom == XA_EDE_EVOKE_SHUTDOWN_ALL) {
|
|
|
|
int val = get_int_property_value(XA_EDE_EVOKE_SHUTDOWN_ALL);
|
|
|
|
if(val == 1) {
|
2013-01-05 07:39:09 +04:00
|
|
|
int dw, dh, ret;
|
|
|
|
|
|
|
|
dw = DisplayWidth(fl_display, fl_screen);
|
|
|
|
dh = DisplayHeight(fl_display, fl_screen);
|
2009-02-19 11:12:29 +03:00
|
|
|
|
2013-01-05 07:39:09 +04:00
|
|
|
ret = logout_dialog_show(dw, dh, LOGOUT_OPT_SHUTDOWN | LOGOUT_OPT_RESTART);
|
2009-10-13 14:41:08 +04:00
|
|
|
if(ret == LOGOUT_RET_CANCEL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
send_dbus_ede_quit();
|
|
|
|
|
|
|
|
switch(ret) {
|
|
|
|
case LOGOUT_RET_RESTART:
|
2013-01-05 07:39:09 +04:00
|
|
|
if(!do_shutdown_or_restart(true, x_shutdown)) return 1;
|
2009-10-13 14:41:08 +04:00
|
|
|
break;
|
|
|
|
case LOGOUT_RET_SHUTDOWN:
|
2013-01-05 07:39:09 +04:00
|
|
|
if(!do_shutdown_or_restart(false, x_shutdown)) return 1;
|
2009-10-13 14:41:08 +04:00
|
|
|
break;
|
|
|
|
case LOGOUT_RET_LOGOUT:
|
|
|
|
default:
|
|
|
|
x_shutdown();
|
|
|
|
break;
|
2009-02-25 18:03:37 +03:00
|
|
|
}
|
2009-10-13 14:41:08 +04:00
|
|
|
|
|
|
|
stop();
|
2009-02-19 11:12:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|