Skip to content

Commit 1861540

Browse files
author
RMPR
committed
Add the possiblity to stop infinite playback
This is the first polished attempt to fix #10 Instead of executing all the capture in one pass, it's executed line-by-line with the downside that it might be interrupted when a key is pressed thus causing that key to be stuck.
1 parent a6957f2 commit 1861540

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

atbswp/control.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import time
2626
from datetime import date
2727
from pathlib import Path
28+
from threading import Event
2829
from threading import Thread
2930

3031
import pyautogui
@@ -359,10 +360,14 @@ class PlayCtrl:
359360

360361
def __init__(self):
361362
self.count = settings.CONFIG.getint('DEFAULT', 'Repeat Count')
363+
self.infinite = settings.CONFIG.getboolean('DEFAULT', 'Infinite Playback')
362364

363365
def play(self, capture, toggle_button):
364366
"""Play the loaded capture."""
365-
exec(capture)
367+
for line in capture:
368+
if self.play_thread.ended():
369+
return
370+
exec(line)
366371
btn_event = wx.CommandEvent(wx.wxEVT_TOGGLEBUTTON)
367372
btn_event.EventObject = toggle_button
368373
if self.count <= 0 and not self.infinite:
@@ -380,21 +385,17 @@ def action(self, event):
380385
toggle_button.Value = False
381386
return
382387
with open(TMP_PATH, 'r') as f:
383-
capture = f.read()
384-
if capture == HEADER:
385-
wx.LogError("Empty capture")
386-
toggle_button.Value = False
387-
return
388+
capture = f.readlines()
388389
if self.count > 0 or self.infinite:
389390
self.count = self.count - 1 if not self.infinite else self.count
390-
self.play_thread = Thread()
391+
self.play_thread = PlayThread()
391392
self.play_thread.daemon = True
392-
self.play_thread = Thread(target=self.play,
393+
self.play_thread = PlayThread(target=self.play,
393394
args=(capture, toggle_button,))
394395
self.play_thread.start()
395396
else:
397+
self.play_thread.end()
396398
self.count = settings.CONFIG.getint('DEFAULT', 'Repeat Count')
397-
self.infinite = settings.CONFIG.getboolean('DEFAULT', 'Infinite Playback')
398399
settings.save_config()
399400

400401

@@ -516,3 +517,14 @@ def action(event):
516517
url = "https://youtu.be/L0jjSgX5FYk"
517518
wx.LaunchDefaultBrowser(url, flags=0)
518519

520+
class PlayThread(Thread):
521+
"""Thread with an end method triggered by a click on the Toggle button."""
522+
def __init__(self, *args, **kwargs):
523+
super(PlayThread, self).__init__(*args, **kwargs)
524+
self._end = Event()
525+
526+
def end(self):
527+
self._end.set()
528+
529+
def ended(self):
530+
return self._end.isSet()

0 commit comments

Comments
 (0)