Your phone is the terminal. Stream a Linux/Mac desktop to an iPhone over WebRTC through NAT, with audio and input. Open source, single binaries.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/network-computer.git
HTTPS
https://gitstable.com/git/askrobots/network-computer.git
SSH
ssh://git@gitstable.com:2222/askrobots/network-computer.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Dan Quellhorst Firefox policy: no WebRTC mDNS (a page using WebRTC made Firefox listen on UDP 5353) a1d92f3 1 month, 3 weeks ago History
nc-device · 8.2 KB · 234 lines
1#!/usr/bin/env python3
2"""nc-device: the desk lays itself out for the device you are using.
3
4A phone, a tablet and a desktop want different things from the same desk:
5on a phone, one window at a time, big buttons, a launcher you can hit with a
6thumb; on a tablet, touch-sized controls but room for two windows; at a
7desktop, what you set up. The client reports what it is (the host writes
8/run/nc-host/input: input=touch|pointer, device=phone|tablet|desktop), and
9this applies the matching profile to the session: panel and dock size, window
10title bars, text size, the launcher, and on a phone maximized windows. The
11dbbasic apps read the same file (their app kit's InputMode).
12
13 nc-device watch and apply (autostarted with the session)
14 nc-device set phone|tablet|desktop|auto
15 choose one yourself ("set this up for my phone"); it
16 holds until you connect from a different kind of
17 device; auto follows the client again
18 nc-device status JSON: what the client reports, any override, what is applied
19
20Your desktop settings are the baseline: saved the first time (in
21~/.config/nc/device-desktop.json) and restored whenever you are back at a
22desktop, so a layout you made yourself is never lost to a phone visit.
23"""
24import json
25import os
26import subprocess
27import sys
28import threading
29import time
30
31HOST_FILE = "/run/nc-host/input"
32# a choice made by you or voice ("set this up for my phone"): it lasts until the
33# client reports a different device (you pick up the iPad), then auto again
34OVERRIDE = os.path.expanduser("~/.config/nc/device-override")
35# the profile applied, kept across reboots (on the desk, with ~/.config), so
36# settings left by a phone visit are never taken for your desktop ones;
37# nc-launch reads it too
38CURRENT = os.path.expanduser("~/.config/nc/device-current")
39BASELINE = os.path.expanduser("~/.config/nc/device-desktop.json")
40
41# (channel, property, type): what a profile changes
42SETTINGS = [
43 ("xfce4-panel", "/panels/panel-1/size", "int"),
44 ("xfce4-panel", "/panels/panel-1/icon-size", "int"),
45 ("xfce4-panel", "/panels/panel-2/size", "int"),
46 ("xfwm4", "/general/theme", "string"),
47 ("xfwm4", "/general/title_font", "string"),
48 ("xsettings", "/Gtk/FontName", "string"),
49 ("xfce4-desktop", "/desktop-icons/icon-size", "int"),
50]
51FALLBACK = { # when a property has never been set
52 "/panels/panel-1/size": "26", "/panels/panel-1/icon-size": "16", "/panels/panel-2/size": "48",
53 "/general/theme": "Default", "/general/title_font": "Sans Bold 9", "/Gtk/FontName": "Sans 10",
54 "/desktop-icons/icon-size": "48",
55}
56# per profile: absolute values, or ("+", n) to grow a font's size by n points
57PROFILES = {
58 "tablet": {"/panels/panel-1/size": "40", "/panels/panel-1/icon-size": "24", "/panels/panel-2/size": "64",
59 "/general/theme": "Default-hdpi", "/general/title_font": ("+", 2), "/Gtk/FontName": ("+", 2),
60 "/desktop-icons/icon-size": "64"},
61 "phone": {"/panels/panel-1/size": "44", "/panels/panel-1/icon-size": "28", "/panels/panel-2/size": "64",
62 "/general/theme": "Default-xhdpi", "/general/title_font": ("+", 3), "/Gtk/FontName": ("+", 3),
63 "/desktop-icons/icon-size": "64"},
64}
65
66
67def xq(*args):
68 return subprocess.run(["xfconf-query", *args], capture_output=True, text=True)
69
70
71def get(channel, prop):
72 r = xq("-c", channel, "-p", prop)
73 return r.stdout.strip() if r.returncode == 0 else None
74
75
76def put(channel, prop, kind, value):
77 if xq("-c", channel, "-p", prop, "-s", value).returncode != 0:
78 xq("-c", channel, "-p", prop, "-n", "-t", kind, "-s", value)
79
80
81def grow(font, n):
82 """'Sans Bold 9' + 3 -> 'Sans Bold 12'."""
83 name, _, size = font.rpartition(" ")
84 try:
85 return f"{name} {int(float(size)) + n}"
86 except ValueError:
87 return font
88
89
90def save_baseline():
91 b = {p: get(c, p) or FALLBACK[p] for c, p, _ in SETTINGS}
92 os.makedirs(os.path.dirname(BASELINE), exist_ok=True)
93 with open(BASELINE, "w") as f:
94 json.dump(b, f, indent=1)
95 return b
96
97
98def baseline():
99 """Your desktop settings, as they were the last time you left a desktop."""
100 try:
101 with open(BASELINE) as f:
102 return json.load(f)
103 except (OSError, ValueError):
104 return save_baseline()
105
106
107def reported():
108 """(device, input) from the host's file."""
109 kv = {}
110 try:
111 for line in open(HOST_FILE):
112 k, _, v = line.strip().partition("=")
113 kv[k] = v
114 except OSError:
115 pass
116 d = kv.get("device") if kv.get("device") in ("phone", "tablet", "desktop") else "desktop"
117 return d, kv.get("input") or ("touch" if d != "desktop" else "pointer")
118
119
120def override():
121 try:
122 choice, _, when = open(OVERRIDE).read().strip().partition(" ")
123 except OSError:
124 return None
125 if choice not in ("phone", "tablet", "desktop"):
126 return None
127 if when != reported()[0]: # a different device since: follow it
128 try:
129 os.remove(OVERRIDE)
130 except OSError:
131 pass
132 return None
133 return choice
134
135
136def wanted():
137 return override() or reported()[0]
138
139
140def applied():
141 try:
142 return open(CURRENT).read().strip() or None
143 except OSError:
144 return None
145
146
147def apply(profile):
148 # leaving the desktop: what is set now is yours (you may have changed it)
149 # (the very first time, baseline() saves them however this began)
150 if profile != "desktop" and applied() == "desktop":
151 save_baseline()
152 base = baseline()
153 changes = PROFILES.get(profile, {})
154 for channel, prop, kind in SETTINGS:
155 v = changes.get(prop, base.get(prop) or FALLBACK[prop])
156 if isinstance(v, tuple):
157 v = grow(base.get(prop) or FALLBACK[prop], v[1])
158 put(channel, prop, kind, v)
159 os.makedirs(os.path.dirname(CURRENT), exist_ok=True)
160 with open(CURRENT, "w") as f:
161 f.write(profile + "\n")
162 if profile == "phone":
163 for wid in normal_windows():
164 maximize(wid)
165
166
167# ---------- phone: every window maximized, new ones too ----------
168
169def normal_windows():
170 r = subprocess.run(["wmctrl", "-l"], capture_output=True, text=True)
171 ids = [line.split()[0] for line in r.stdout.splitlines() if line.split() and line.split()[1] != "-1"]
172 return [w for w in ids if is_normal(w)]
173
174
175def is_normal(wid):
176 r = subprocess.run(["xprop", "-id", wid, "_NET_WM_WINDOW_TYPE", "WM_TRANSIENT_FOR"],
177 capture_output=True, text=True)
178 return "_NET_WM_WINDOW_TYPE_NORMAL" in r.stdout and "WM_TRANSIENT_FOR(WINDOW)" not in r.stdout
179
180
181def maximize(wid):
182 subprocess.run(["wmctrl", "-i", "-r", wid, "-b", "add,maximized_vert,maximized_horz"],
183 capture_output=True)
184
185
186def watch_new_windows():
187 """While on a phone, maximize each window as it opens (dialogs stay as they are)."""
188 seen = set(normal_windows())
189 p = subprocess.Popen(["xprop", "-root", "-spy", "_NET_CLIENT_LIST"], stdout=subprocess.PIPE, text=True)
190 for _ in p.stdout:
191 time.sleep(0.3) # let the new window map and get its type
192 now = set(normal_windows())
193 if applied() == "phone":
194 for wid in now - seen:
195 maximize(wid)
196 seen = now
197
198
199def watch():
200 threading.Thread(target=watch_new_windows, daemon=True).start()
201 last = None
202 while True:
203 w = wanted()
204 if w != last:
205 apply(w)
206 last = w
207 time.sleep(2)
208
209
210def main():
211 cmd = sys.argv[1] if len(sys.argv) > 1 else "watch"
212 if cmd == "watch":
213 watch()
214 elif cmd == "set" and len(sys.argv) == 3 and sys.argv[2] in ("phone", "tablet", "desktop", "auto"):
215 os.makedirs(os.path.dirname(OVERRIDE), exist_ok=True)
216 if sys.argv[2] == "auto":
217 try:
218 os.remove(OVERRIDE)
219 except OSError:
220 pass
221 else:
222 with open(OVERRIDE, "w") as f:
223 f.write(f"{sys.argv[2]} {reported()[0]}\n")
224 print(json.dumps({"ok": True, "device": wanted()}))
225 elif cmd == "status":
226 d, i = reported()
227 print(json.dumps({"ok": True, "reported": d, "input": i, "override": override(), "applied": applied()}))
228 else:
229 print(__doc__)
230 sys.exit(2)
231
232
233if __name__ == "__main__":
234 main()