Skip to content

Commit ccaedfc

Browse files
committed
Use swaymsg with socket_path
1 parent cd35496 commit ccaedfc

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

controller.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
wserver = Flask(__name__)
2929
dependencies = []
3030
stream_sources = ["static-images", "v4l2", "vnc-browser"]
31+
sway_socket_path = "/tmp/sway.sock"
3132
cmds = {"clock" : "humanbeans_clock",
3233
"image_viewer" : "imv",
3334
"media_player" : "mpv",
@@ -76,7 +77,7 @@ def display_screenshot():
7677
fn = display.screenshot()
7778
if not os.path.isfile(fn):
7879
logging.error("screenshot: File {} does not exist".format(fn))
79-
return False
80+
#return False
8081
return send_file(fn, mimetype='image/png')
8182

8283
def which(cmd):
@@ -380,28 +381,28 @@ def __init__(self, stream_source):
380381
if stream_source == "v4l2":
381382
# Start streaming
382383
logging.info("Setting up source")
383-
stream_create_v4l2_src(stream_source_device)
384+
self.stream_create_v4l2_src(stream_source_device)
384385
logging.info("Setting up stream")
385386
time.sleep(3)
386-
stream_v4l2_ffmpeg()
387-
#gst = stream_setup_gstreamer(stream_source,
387+
self.stream_v4l2_ffmpeg()
388+
#gst = self.stream_setup_gstreamer(stream_source,
388389
# stream_source_device,
389390
# local_ip,
390391
# listen_port)
391392
#gst.stdin.close()
392393
#gst.wait()
393394

394395
elif stream_source == "static-images":
395-
gst = stream_setup_gstreamer(stream_source,
396+
gst = self.stream_setup_gstreamer(stream_source,
396397
stream_source_device,
397398
local_ip,
398399
listen_port)
399400

400-
gst_stream_images(gst, img_path)
401+
self.gst_stream_images(gst, img_path)
401402
gst.stdin.close()
402403
gst.wait()
403404

404-
def stream_setup_gstreamer(stream_source, source_device, ip, port):
405+
def stream_setup_gstreamer(self, stream_source, source_device, ip, port):
405406
if stream_source == "static-images":
406407
gstreamer = subprocess.Popen([
407408
'gst-launch-1.0', '-v', '-e',
@@ -427,16 +428,16 @@ def stream_setup_gstreamer(stream_source, source_device, ip, port):
427428

428429
return gstreamer
429430

430-
def gst_stream_images(gstreamer, img_path, debug=False):
431+
def gst_stream_images(self, gstreamer, img_path, debug=False):
431432
t0 = int(round(time.time() * 1000))
432433
n = -1
433434

434435
while True:
435-
filename = img_path + '/image_' + str(0).zfill(4) + '.png'
436+
#filename = img_path + '/image_' + str(0).zfill(4) + '.png'
437+
filename = '/tmp/screenshot.png'
436438
t1 = int(round(time.time() * 1000))
437439

438-
if debug:
439-
logging.debug(filename + ": " + str(t1 - t0) + " ms")
440+
logging.debug(filename + ": " + str(t1 - t0) + " ms")
440441

441442
t0 = t1
442443

@@ -460,7 +461,7 @@ def gst_stream_images(gstreamer, img_path, debug=False):
460461
else:
461462
n += 1
462463

463-
def stream_create_v4l2_src(device):
464+
def stream_create_v4l2_src(self, device):
464465
# Check if device is an existing character device
465466
if not stat.S_ISCHR(os.lstat(device)[stat.ST_MODE]):
466467
logging.error(device + " does not exist, aborting..")
@@ -484,16 +485,12 @@ def stream_create_v4l2_src(device):
484485

485486
return True
486487

487-
488-
def stream_v4l2_ffmpeg():
488+
def stream_v4l2_ffmpeg(self):
489489
ffmpeg = subprocess.Popen([
490490
'ffmpeg', '-f', 'v4l2', '-i', '/dev/video0',
491491
'-codec', 'copy',
492-
'-f', 'mpegts', 'udp:localhost:6000'
493-
],
494-
stdin=subprocess.PIPE,
495-
start_new_session=True,
496-
close_fds=False)
492+
'-f', 'mpegts', 'udp:0.0.0.0:6000'
493+
])
497494

498495

499496
class News:
@@ -676,7 +673,7 @@ def get_windows_whitelist(self):
676673
return windows
677674

678675
def get_windows(self, blacklist=None):
679-
cmd="swaymsg -t get_tree"
676+
cmd="swaymsg -s {} -t get_tree".format(sway_socket_path)
680677
windows = []
681678

682679
p = subprocess.Popen(cmd,
@@ -727,7 +724,7 @@ def workspace_nodes(self, workspace):
727724
return windows
728725

729726
def active_window():
730-
cmd = 'swaymsg -t get_tree) | jq ".. | select(.type?) | select(.focused==true).id"'
727+
cmd = 'swaymsg -s {} -t get_tree) | jq ".. | select(.type?) | select(.focused==true).id"'.format(sway_socket_path)
731728

732729
async def focus_next_window(self):
733730
await asyncio.sleep(random.random() * 3)
@@ -743,7 +740,7 @@ async def focus_next_window(self):
743740

744741
next_window = self.switching_windows.pop()
745742
logging.info("display: Switching focus to: ".format(next_window["id"]))
746-
cmd = "swaymsg [con_id={}] focus".format(next_window["id"])
743+
cmd = "swaymsg -s sway_socket_path [con_id={}] focus".format(next_window["id"], sway_socket_path)
747744

748745
p = subprocess.Popen(cmd,
749746
shell=True)
@@ -765,7 +762,7 @@ async def fullscreen_next_window(self):
765762
next_window = self.switching_windows.pop()
766763
logging.info("display: Switching focus to: ".format(next_window["id"]))
767764

768-
cmd = "swaymsg [con_id={}] fullscreen".format(next_window["id"])
765+
cmd = "swaymsg -s sway_socket_path [con_id={}] fullscreen".format(next_window["id"], sway_socket_path)
769766

770767
p = subprocess.Popen(cmd,
771768
shell=True)
@@ -782,7 +779,7 @@ async def task_scheduler(self, interval_s, interval_function):
782779
)
783780

784781
def switch_workspace(self):
785-
cmd = "swaymsg workspace {}".format(next_ws)
782+
cmd = "swaymsg -s sway_socket_path workspace {}".format(next_ws, sway_socket_path)
786783

787784
p = subprocess.Popen(cmd,
788785
shell=True,
@@ -976,7 +973,7 @@ def __init__(self, threads):
976973
sources_str = " ".join(str(x) for x in stream_sources)
977974
logging.error("Invalid source, aborting..")
978975
logging.info("Possible choices are: " + sources_str)
979-
sys.exit(1)
976+
#sys.exit(1)
980977

981978
hostname = socket.gethostname()
982979
local_ip = System.net_local_iface_address(probe_ip)

0 commit comments

Comments
 (0)