Skip to content

Commit

Permalink
Merge pull request #78 from poconbhui/exit-gracefully
Browse files Browse the repository at this point in the history
Added SIGINT handling
  • Loading branch information
Ape authored Jun 10, 2016
2 parents 3032fb1 + 63ff992 commit 9c561e8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
34 changes: 29 additions & 5 deletions ds4drv/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import signal

from threading import Thread

Expand Down Expand Up @@ -108,26 +109,50 @@ def read_report(self):
def run(self):
self.loop.run()

def exit(self, *args):
def exit(self, *args, error = True):
if self.device:
self.cleanup_device()

self.logger.error(*args)
self.error = True
if error == True:
self.logger.error(*args)
self.error = True
else:
self.logger.info(*args)


def create_controller_thread(index, controller_options, dynamic=False):
controller = DS4Controller(index, controller_options, dynamic=dynamic)

thread = Thread(target=controller.run)
thread.daemon = True
thread.controller = controller
thread.start()

return thread


class SigintHandler(object):
def __init__(self, threads):
self.threads = threads

def cleanup_controller_threads(self):
for thread in self.threads:
thread.controller.exit("Cleaning up...", error=False)
thread.controller.loop.stop()
thread.join()

def __call__(self, signum, frame):
signal.signal(signum, signal.SIG_DFL)

self.cleanup_controller_threads()
sys.exit(0)


def main():
threads = []

sigint_handler = SigintHandler(threads)
signal.signal(signal.SIGINT, sigint_handler)

try:
options = load_options()
except ValueError as err:
Expand All @@ -146,7 +171,6 @@ def main():
if options.daemon:
Daemon.fork(options.daemon_log, options.daemon_pid)

threads = []
for index, controller_options in enumerate(options.controllers):
thread = create_controller_thread(index + 1, controller_options)
threads.append(thread)
Expand Down
6 changes: 5 additions & 1 deletion ds4drv/eventloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class EventLoop(object):
def __init__(self):
self.stop()

# Timeout value well over the expected controller poll time, but
# short enough for ds4drv to shut down in a reasonable time.
self.epoll_timeout = 1

def create_timer(self, interval, callback):
"""Creates a timer."""

Expand Down Expand Up @@ -95,7 +99,7 @@ def run(self):
"""Starts the loop."""
self.running = True
while self.running:
for fd, event in self.epoll.poll():
for fd, event in self.epoll.poll(self.epoll_timeout):
callback = self.callbacks.get(fd)
if callback:
callback()
Expand Down

0 comments on commit 9c561e8

Please sign in to comment.