Skip to content

Commit

Permalink
Split actions into their own modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrippa committed Mar 12, 2014
1 parent b0e3d0e commit 88096c4
Show file tree
Hide file tree
Showing 14 changed files with 574 additions and 537 deletions.
19 changes: 2 additions & 17 deletions ds4drv/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,14 @@

from threading import Thread

from .actions import (ActionLED,
ReportActionBattery,
ReportActionBinding,
ReportActionBluetoothSignal,
ReportActionDump,
ReportActionInput,
ReportActionStatus)
from .actions import ActionRegistry
from .backends import BluetoothBackend, HidrawBackend
from .config import load_options
from .daemon import Daemon
from .eventloop import EventLoop
from .exceptions import BackendError


ACTIONS = (ActionLED,
ReportActionBattery,
ReportActionBinding,
ReportActionBluetoothSignal,
ReportActionDump,
ReportActionInput,
ReportActionStatus)


class DS4Controller(object):
def __init__(self, index, options, dynamic=False):
self.index = index
Expand All @@ -35,7 +20,7 @@ def __init__(self, index, options, dynamic=False):
self.device = None
self.loop = EventLoop()

self.actions = [cls(self) for cls in ACTIONS]
self.actions = [cls(self) for cls in ActionRegistry.actions]
self.bindings = options.parent.bindings
self.current_profile = "default"
self.default_profile = options
Expand Down
76 changes: 76 additions & 0 deletions ds4drv/action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from .config import add_controller_option
from .utils import with_metaclass

from functools import wraps

BASE_CLASSES = ["Action", "ReportAction"]


class ActionRegistry(type):
def __init__(cls, name, bases, attrs):
if name not in BASE_CLASSES:
if not hasattr(ActionRegistry, "actions"):
ActionRegistry.actions = []
else:
ActionRegistry.actions.append(cls)


class Action(with_metaclass(ActionRegistry)):
"""Actions are what drives most of the functionality of ds4drv."""

@classmethod
def add_option(self, *args, **kwargs):
add_controller_option(*args, **kwargs)

def __init__(self, controller):
self.controller = controller
self.logger = controller.logger

self.register_event("device-setup", self.setup)
self.register_event("device-cleanup", self.disable)
self.register_event("load-options", self.load_options)

def create_timer(self, interval, func):
return self.controller.loop.create_timer(interval, func)

def register_event(self, event, func):
self.controller.loop.register_event(event, func)

def unregister_event(self, event, func):
self.controller.loop.unregister_event(event, func)

def setup(self, device):
pass

def enable(self):
pass

def disable(self):
pass

def load_options(self, options):
pass


class ReportAction(Action):
def __init__(self, controller):
super(ReportAction, self).__init__(controller)

self._last_report = None
self.register_event("device-report", self._handle_report)

def create_timer(self, interval, callback):
@wraps(callback)
def wrapper(*args, **kwargs):
if self._last_report:
return callback(self._last_report, *args, **kwargs)
return True

return super(ReportAction, self).create_timer(interval, wrapper)

def _handle_report(self, report):
self._last_report = report
self.handle_report(report)

def handle_report(self, report):
pass
Loading

0 comments on commit 88096c4

Please sign in to comment.