Added Shutdown() call to D-Bus interface, properly catch SIGINT and SIGTERM in Daemon

This commit is contained in:
Ingo Ruhnke 2011-03-05 15:47:57 +01:00
parent 6bb8a4dc8e
commit bb10adb20d
7 changed files with 82 additions and 20 deletions

View file

@ -15,5 +15,11 @@
<method name="SetConfig">
<arg name="config" type="i" direction="in" />
</method>
<!--
rumble_enable SLOT
rumble_disable SLOT
set_rumble_gain SLOT GAIN
-->
</interface>
</node>

View file

@ -39,6 +39,8 @@
#include "xboxdrv_daemon_glue.hpp"
#include "xboxdrv_controller_glue.hpp"
XboxdrvDaemon* XboxdrvDaemon::s_current = 0;
namespace {
bool get_usb_id(udev_device* device, uint16_t* vendor_id, uint16_t* product_id)
@ -100,12 +102,18 @@ XboxdrvDaemon::XboxdrvDaemon(const Options& opts) :
m_monitor(0),
m_controller_slots(),
m_inactive_threads(),
m_uinput()
m_uinput(),
m_gmain()
{
assert(!s_current);
s_current = this;
}
XboxdrvDaemon::~XboxdrvDaemon()
{
assert(s_current);
s_current = 0;
for(ControllerSlots::iterator i = m_controller_slots.begin(); i != m_controller_slots.end(); ++i)
{
(*i)->disconnect();
@ -377,17 +385,19 @@ XboxdrvDaemon::run(const Options& opts)
// we don't use glib threads, but we still need to init the thread
// system to make glib thread safe
g_thread_init(NULL);
GMainLoop* gmain = g_main_loop_new(NULL, false);
signal(SIGINT, &XboxdrvDaemon::on_sigint);
signal(SIGTERM, &XboxdrvDaemon::on_sigint);
m_gmain = g_main_loop_new(NULL, false);
init_g_udev();
init_g_dbus();
log_info("launching into glib main loop");
g_main_loop_run(gmain);
g_main_loop_run(m_gmain);
log_info("glib main loop finished");
g_main_loop_unref(gmain);
signal(SIGINT, 0);
g_main_loop_unref(m_gmain);
}
catch(const std::exception& err)
{
@ -908,4 +918,17 @@ XboxdrvDaemon::status()
return out.str();
}
void
XboxdrvDaemon::shutdown()
{
assert(m_gmain);
g_main_loop_quit(m_gmain);
}
void
XboxdrvDaemon::on_sigint(int)
{
XboxdrvDaemon::current()->shutdown();
}
/* EOF */

View file

@ -34,6 +34,8 @@ struct XPadDevice;
class XboxdrvDaemon
{
private:
static XboxdrvDaemon* s_current;
const Options& m_opts;
struct udev* m_udev;
struct udev_monitor* m_monitor;
@ -45,6 +47,11 @@ private:
Threads m_inactive_threads;
std::auto_ptr<UInput> m_uinput;
GMainLoop* m_gmain;
private:
static void on_sigint(int);
static XboxdrvDaemon* current() { return s_current; }
public:
XboxdrvDaemon(const Options& opts);
@ -53,6 +60,7 @@ public:
void run(const Options& opts);
std::string status();
void shutdown();
private:
void create_pid_file(const Options& opts);

View file

@ -6,22 +6,11 @@
<method name="Status">
<arg type="s" direction="out" />
</method>
<method name="Shutdown" />
<!--
<method name="SetLed">
<arg name="status" type="i" direction="in" />
</method>
<method name="SetRumble">
<arg name="strong" type="i" direction="in" />
<arg name="weak" type="i" direction="in" />
</method>
reset_leds
shutdown, quit, exit
disconnect SLOT
rumble_enable SLOT
rumble_disable SLOT
set_rumble_gain SLOT GAIN
-->
</interface>
</node>

View file

@ -18,6 +18,7 @@
#include "xboxdrv_g_daemon.hpp"
#include "log.hpp"
#include "xboxdrv_daemon.hpp"
/* will create xboxdrv_g_daemon_get_type and set xboxdrv_g_daemon_parent_class */
@ -57,8 +58,18 @@ xboxdrv_g_daemon_new(XboxdrvDaemon* daemon)
gboolean
xboxdrv_g_daemon_status(XboxdrvGDaemon* self, gchar** ret, GError** error)
{
log_info("D-Bus: xboxdrv_g_daemon_status(" << self << ")");
*ret = g_strdup(self->daemon->status().c_str());
return TRUE;
}
gboolean xboxdrv_g_daemon_shutdown(XboxdrvGDaemon* self, GError** error)
{
log_info("D-Bus: xboxdrv_g_daemon_shutdown(" << self << ")");
self->daemon->shutdown();
return TRUE;
}
/* EOF */

View file

@ -51,6 +51,7 @@ GType xboxdrv_g_daemon_get_type();
XboxdrvGDaemon* xboxdrv_g_daemon_new(XboxdrvDaemon* daemon);
gboolean xboxdrv_g_daemon_status(XboxdrvGDaemon* self, gchar** ret, GError** error);
gboolean xboxdrv_g_daemon_shutdown(XboxdrvGDaemon* self, GError** error);
#endif

View file

@ -1,11 +1,28 @@
#!/usr/bin/env python
## Xbox360 USB Gamepad Userspace Driver
## Copyright (C) 2011 Ingo Ruhnke <grumbel@gmx.de>
##
## 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, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
import dbus
import sys
import re
from optparse import OptionParser, OptionGroup
parser = OptionParser("Usage: %prog [OPTIONS]")
parser = OptionParser("Usage: %prog [OPTIONS]\n"
"Remote control application for the xboxdrv daemon")
group = OptionGroup(parser, "D-Bus Options")
group.add_option("-b", "--bus", metavar="BUS",
@ -38,6 +55,10 @@ group.add_option("-c", "--config", metavar="NUM", type="int",
dest="config",
help="switches to controller configuration NUM")
group.add_option("--shutdown", action="store_true",
dest="shutdown",
help="shuts down the daemon")
parser.add_option_group(group)
(options, args) = parser.parse_args()
@ -47,6 +68,9 @@ bus = dbus.SessionBus()
if options.status:
daemon = bus.get_object("org.seul.Xboxdrv", '/org/seul/Xboxdrv/Daemon')
sys.stdout.write(daemon.Status())
elif options.shutdown:
daemon = bus.get_object("org.seul.Xboxdrv", '/org/seul/Xboxdrv/Daemon')
daemon.Shutdown()
else:
if (options.led or options.rumble or options.config) and options.slot == None:
print "Error: --slot argument required"