Skip to content

Commit 7b4d57e

Browse files
committed
Put window switching on a thread
1 parent ccaedfc commit 7b4d57e

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

controller.py

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
wserver = Flask(__name__)
2929
dependencies = []
3030
stream_sources = ["static-images", "v4l2", "vnc-browser"]
31-
sway_socket_path = "/tmp/sway.sock"
3231
cmds = {"clock" : "humanbeans_clock",
3332
"image_viewer" : "imv",
3433
"media_player" : "mpv",
@@ -230,7 +229,7 @@ def create(self, uris):
230229
item["player"] = "system"
231230
item["play_time_s"] = self.default_play_time_s
232231
elif uri.startswith("iss-weather://"):
233-
weather = Weather(self.location)
232+
self.weather = Weather(self.location)
234233
item["num"] = n
235234
item["uri"] = uri
236235
item["player"] = "weather"
@@ -263,12 +262,12 @@ def start_player(self):
263262
x = threading.Thread(target=self.start_image_view, args=())
264263
elif item["player"] == "news":
265264
x = threading.Thread(target=self.start_news_view, args=(self.news,))
266-
elif item["system"] == "system":
265+
elif item["player"] == "system":
267266
x = threading.Thread(target=self.start_system_view, args=())
268267
elif item["player"] == "weather":
269-
x = threading.Thread(target=self.start_weather_view, args=())
268+
x = threading.Thread(target=self.start_weather_view, args=(self.weather,))
270269

271-
#x.start()
270+
x.start()
272271
if not x.is_alive():
273272
logging.error("Failed to start {}".format(item["player"]))
274273
else:
@@ -544,8 +543,11 @@ def fetch_weather(self):
544543
encoding="utf8")
545544

546545
#data = p.communicate()[0]
547-
data = requests.get(url, timeout=10)
548-
data = data.content
546+
try:
547+
data = requests.get(url, timeout=10)
548+
data = data.content
549+
except ReadTimeout as e:
550+
logging.error("weather: Timeout for request")
549551

550552
if not data:
551553
logging.error("Failed to fetch weather data")
@@ -654,6 +656,7 @@ def __init__(self, address, port, res_x=1280, res_y=1024):
654656
self.window_blacklist = list()
655657
self.screenshot_path = "/tmp"
656658
self.screenshot_file = "screenshot.png"
659+
self.socket_path = self.get_socket_path()
657660

658661
logging.info("Resolution: {} x {}"
659662
.format(self.res_x, self.res_y))
@@ -666,14 +669,30 @@ def __init__(self, address, port, res_x=1280, res_y=1024):
666669

667670
logging.info("Blacklisted {} windows".format(len(self.window_blacklist)))
668671

672+
self.x = threading.Thread(target=self.focus_next_window, args=())
673+
self.x.start()
674+
675+
def get_socket_path(self):
676+
cmd = ['/usr/bin/sway', '--get-socketpath']
677+
p = subprocess.Popen(cmd,
678+
shell=False,
679+
stdout=subprocess.PIPE,
680+
stderr=subprocess.STDOUT,
681+
encoding="utf8")
682+
683+
path = p.communicate()
684+
path = path[0].rstrip()
685+
#path = "/tmp/sway.sock"
686+
return path
687+
669688
def get_windows_whitelist(self):
670689
windows = self.get_windows(self.window_blacklist)
671690
logging.info("display: {} windows in whitelist".format(len(windows)))
672691

673692
return windows
674693

675694
def get_windows(self, blacklist=None):
676-
cmd="swaymsg -s {} -t get_tree".format(sway_socket_path)
695+
cmd="swaymsg -s {} -t get_tree".format(self.socket_path)
677696
windows = []
678697

679698
p = subprocess.Popen(cmd,
@@ -724,28 +743,25 @@ def workspace_nodes(self, workspace):
724743
return windows
725744

726745
def active_window():
727-
cmd = 'swaymsg -s {} -t get_tree) | jq ".. | select(.type?) | select(.focused==true).id"'.format(sway_socket_path)
728-
729-
async def focus_next_window(self):
730-
await asyncio.sleep(random.random() * 3)
731-
t = round(time.time() - self.start_time, 1)
732-
logging.info("Finished task: {}".format(t))
746+
cmd = 'swaymsg -s {} -t get_tree) | jq ".. | select(.type?) | select(.focused==true).id"'.format(self.socket_path)
733747

734-
if len(self.switching_windows) == 0:
735-
self.switching_windows = self.get_windows_whitelist()
748+
def focus_next_window(self):
749+
while True:
750+
logging.info("Focus change {}".format(self.switching_windows))
751+
time.sleep(3)
736752
if len(self.switching_windows) == 0:
737-
logging.warning("display: Expected windows to display but there is none")
738-
739-
return
753+
self.switching_windows = self.get_windows_whitelist()
754+
if len(self.switching_windows) == 0:
755+
logging.warning("display: Expected windows to display but there is none")
740756

741-
next_window = self.switching_windows.pop()
742-
logging.info("display: Switching focus to: ".format(next_window["id"]))
743-
cmd = "swaymsg -s sway_socket_path [con_id={}] focus".format(next_window["id"], sway_socket_path)
757+
continue
744758

745-
p = subprocess.Popen(cmd,
746-
shell=True)
759+
next_window = self.switching_windows.pop()
760+
logging.info("display: Switching focus to: ".format(next_window["id"]))
761+
cmd = "swaymsg -s {} [con_id={}] focus".format(self.socket_path, next_window["id"])
762+
p = subprocess.Popen(cmd, shell=True)
763+
p.communicate()[0]
747764

748-
p.communicate()[0]
749765

750766
async def fullscreen_next_window(self):
751767
await asyncio.sleep(random.random() * 3)
@@ -762,7 +778,7 @@ async def fullscreen_next_window(self):
762778
next_window = self.switching_windows.pop()
763779
logging.info("display: Switching focus to: ".format(next_window["id"]))
764780

765-
cmd = "swaymsg -s sway_socket_path [con_id={}] fullscreen".format(next_window["id"], sway_socket_path)
781+
cmd = "swaymsg -s {} [con_id={}] fullscreen".format(self.socket_path, next_window["id"])
766782

767783
p = subprocess.Popen(cmd,
768784
shell=True)
@@ -779,7 +795,7 @@ async def task_scheduler(self, interval_s, interval_function):
779795
)
780796

781797
def switch_workspace(self):
782-
cmd = "swaymsg -s sway_socket_path workspace {}".format(next_ws, sway_socket_path)
798+
cmd = "swaymsg -s {} workspace {}".format(self.socket_path, next_ws)
783799

784800
p = subprocess.Popen(cmd,
785801
shell=True,
@@ -979,6 +995,7 @@ def __init__(self, threads):
979995
local_ip = System.net_local_iface_address(probe_ip)
980996

981997
display = Display(local_ip, listen_port)
998+
982999
nwins = len(display.get_windows())
9831000
if nwins > 0:
9841001
logging.warning("Expected no windows but found {}"

0 commit comments

Comments
 (0)