Skip to content

Commit

Permalink
Add a keyboard shortcut to gracefully exit a Facedancer emulation
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinevg committed Sep 30, 2024
1 parent cfb3d58 commit 3ab8552
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
3 changes: 3 additions & 0 deletions facedancer/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from prompt_toolkit import HTML, print_formatted_text

from .core import FacedancerUSBApp
from .errors import EndEmulation
from .types import DescriptorTypes, LanguageIDs, USBStandardRequests
from .types import USBDirection, USBRequestType, USBRequestRecipient
from .types import DeviceSpeed
Expand Down Expand Up @@ -226,6 +227,8 @@ def emulate(self, *coroutines: Iterable[Coroutine]):

try:
self.run_with(*coroutines)
except EndEmulation as e:
log.info(f"{e}")
finally:
self.disconnect()

Expand Down
15 changes: 10 additions & 5 deletions facedancer/devices/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import inspect
import argparse

from ..logging import configure_default_logging
from ..errors import EndEmulation
from ..logging import configure_default_logging, log

def default_main(device_or_type, *coroutines):
""" Simple, default main for Facedancer emulation.
Expand Down Expand Up @@ -38,7 +39,11 @@ def default_main(device_or_type, *coroutines):
sys.exit(0)

# Run the relevant code, along with any added coroutines.
device.emulate(*coroutines)

if args.suggest:
device.print_suggested_additions()
log.info("Starting emulation, press 'ctrl+c' to disconnect and exit.")
try:
device.emulate(*coroutines)
except KeyboardInterrupt:
pass
finally:
if args.suggest:
device.print_suggested_additions()
3 changes: 3 additions & 0 deletions facedancer/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
class DeviceNotFoundError(IOError):
""" Error indicating a device was not found. """
pass

class EndEmulation(Exception):
""" When an EndEmulation exception is thrown the emulation will shutdown and exit. """

0 comments on commit 3ab8552

Please sign in to comment.