Compare commits

..
7 changed files with 54 additions and 183 deletions

No files matched your search

@@ -7,7 +7,6 @@
#include "droiddeck-ahb-v1-server-protocol.h"
#include <android/hardware_buffer.h>
#include <errno.h>
#include <linux/sync_file.h>
#include <fcntl.h>
#include <poll.h>
#include <pthread.h>
@@ -146,12 +145,6 @@ static void handle_released(uint64_t id, int fd) {
if (poll(&p, 1, 0) > 0) {
close(fd);
} else {
if (ab->fence_src) {
struct sync_merge_data m = {.name = "droiddeck-release", .fd2 = fd};
if (ioctl(ab->fence_fd, SYNC_IOC_MERGE, &m) == 0) { close(fd); fd = m.fence; }
wl_event_source_remove(ab->fence_src);
close(ab->fence_fd);
}
ab->fence_fd = fd;
ab->fence_src = wl_event_loop_add_fd(g_loop, fd, WL_EVENT_READABLE, on_fence_readable, ab);
if (!ab->fence_src) { close(fd); ab->fence_fd = -1; }
@@ -159,7 +152,6 @@ static void handle_released(uint64_t id, int fd) {
}
}
}
if (ab->fence_src) return;
send_deferred_release(ab);
maybe_free(ab);
}
@@ -1000,6 +1000,11 @@ static void take_shm(struct surface *s, struct wl_shm_buffer *shm, struct wl_res
s->buf_alpha = wl_shm_buffer_get_format(shm) == WL_SHM_FORMAT_ARGB8888;
s->black_fill = 0;
s->has_content = s->shm_img != NULL;
if (s->dst_set && !s->src_set && (s->dst[0] != w || s->dst[1] != h)) {
s->dst_set = 0;
s->dst[0] = -1;
s->dst[1] = -1;
}
g_stat_shm++;
struct client_info *ci = client_info_of(wl_resource_get_client(s->resource));
if (ci && ci->asked_feedback) ci->shm_frames++;
@@ -1083,6 +1088,11 @@ static void take_dmabuf(struct surface *s, struct dmabuf_buffer *b, struct wl_re
s->buf_h = b->height;
s->buf_alpha = (b->format & 0xff) == 'A';
s->has_content = b->img != NULL;
if (s->dst_set && !s->src_set && (s->dst[0] != b->width || s->dst[1] != b->height)) {
s->dst_set = 0;
s->dst[0] = -1;
s->dst[1] = -1;
}
if (!repeat) g_stat_dmabuf++;
const int announced_now = !s->announced_vulkan;
if (!s->announced_vulkan) {
@@ -992,22 +992,11 @@ def steam_clear_pending_interstitial(agent_dir, app_id):
if (typeof apps?.GetActiveGameActions !== 'function' || typeof apps.ContinueGameAction !== 'function') return false;
const active = await apps.GetActiveGameActions() || [];
for (const action of active) {
const gameid = String(action?.strAppID ?? action?.gameid ?? action?.nAppID ?? '');
if (!/^[0-9]+$/.test(gameid)) continue;
const numericId = BigInt(gameid);
const appid = String(numericId > 0xffffffffn ? numericId >> 32n : numericId);
if (appid !== %s || action?.strActionName !== 'LaunchApp' || action?.bWaitingForUI !== true) continue;
const task = action.strTaskName;
let response;
if (task === 'ShowLaunchOption') {
response = String(Number.isInteger(action.nLaunchOption) && action.nLaunchOption >= 0 ? action.nLaunchOption : 0);
} else if (task === 'ShowInterstitials' || task === 'CreatingProcess') {
response = task;
} else {
continue;
const appid = String(action?.strAppID ?? action?.gameid ?? action?.nAppID ?? '');
if (appid === %s && action?.strTaskName === 'ShowInterstitials') {
apps.ContinueGameAction(action.nGameActionID, appid, 'ShowInterstitials');
return true;
}
apps.ContinueGameAction(action.nGameActionID, response);
return true;
}
return false;
})()""" % json.dumps(app_id)
@@ -1308,9 +1308,9 @@ case $mode in
done < <(xprop -spy -root "${atoms[@]}" 2>/dev/null)
) &
steam_hold_launch() {
local game=$1 appid=$1 shortcut= waited=0 status=0
local game=$1 shortcut= waited=0 status=0
_dd_focused() { xprop -root GAMESCOPE_FOCUSED_APP 2>/dev/null | sed -n 's/.*= *//p'; }
_dd_starting() { pgrep -f "reaper SteamLaunch AppId=$appid( |$)" >/dev/null 2>&1; }
_dd_starting() { pgrep -f "reaper SteamLaunch AppId=$(((game >> 32) & 0xffffffff))( |$)" >/dev/null 2>&1; }
_dd_hand_off() {
printf '%s\n' "$game" > "$DD_LAUNCH_DIR/steam-game.tmp" && mv -f "$DD_LAUNCH_DIR/steam-game.tmp" "$DD_LAUNCH_DIR/steam-game"
}
@@ -1318,10 +1318,7 @@ case $mode in
until [ "$(_dd_focused)" = 769 ] || [ "$SECONDS" -ge "$_dd_end" ]; do
sleep 1
done
if [ "${#game}" -gt 10 ]; then
shortcut=1
appid=$(((game >> 32) & 0xffffffff))
fi
[ "${#game}" -gt 10 ] && shortcut=1
_dd_ready_end=$((SECONDS + 30))
while [ "$waited" -lt 30 ] && [ "$((_dd_ready_end - SECONDS))" -gt 0 ]; do
timeout "$((_dd_ready_end - SECONDS))" /usr/local/bin/droiddeck-agent --steam-ready >/dev/null 2>&1 && break
@@ -1332,36 +1329,30 @@ case $mode in
waited=0
echo "== steam: interface up; launching the requested game $game"
_dd_hand_off
while [ "$(_dd_focused)" = 769 ] && [ "$waited" -lt 120 ]; do
if /usr/local/bin/droiddeck-agent --steam-clear-pending-interstitial "$appid" >/dev/null 2>&1; then
echo "== steam: continued a pending launch step for $game"
fi
[ -n "$shortcut" ] || return 0
while [ "$(_dd_focused)" = 769 ] && [ "$waited" -lt 20 ]; do
sleep 1
waited=$((waited + 1))
if [ -n "$shortcut" ] && [ "$waited" -eq 20 ]; then
if _dd_starting; then
echo "== steam: the requested game is already starting; not launching again"
return 0
fi
echo "== steam: the game is not in front after about 20 s; waiting for the shortcut overview"
/usr/local/bin/droiddeck-agent --wait-steam-shortcut-overview "$game" 45 || status=$?
if [ "$status" -eq 3 ]; then
echo "== steam: not a shortcut CGameID; not launching again"
elif _dd_starting; then
echo "== steam: the requested game started while waiting; not launching again"
return 0
elif [ "$(_dd_focused)" = 769 ]; then
echo "== steam: clearing the pending launch interstitial for $game (overview status $status)"
if ! /usr/local/bin/droiddeck-agent --steam-clear-pending-interstitial "$appid" >/dev/null 2>&1; then
echo "== steam: launching the requested game again $game"
_dd_hand_off
fi
else
echo "== steam: the game took focus while waiting; not launching again"
return 0
fi
fi
done
[ "$(_dd_focused)" = 769 ] || return 0
if _dd_starting; then
echo "== steam: the requested game is already starting; not launching again"
return 0
fi
echo "== steam: the game is not in front after about 20 s; waiting for the shortcut overview"
/usr/local/bin/droiddeck-agent --wait-steam-shortcut-overview "$game" 45 || status=$?
if [ "$status" -eq 3 ]; then
echo "== steam: not a shortcut CGameID; not launching again"
elif _dd_starting; then
echo "== steam: the requested game started while waiting; not launching again"
elif [ "$(_dd_focused)" = 769 ]; then
echo "== steam: clearing the pending launch interstitial for $game (overview status $status)"
/usr/local/bin/droiddeck-agent --steam-clear-pending-interstitial "$(((game >> 32) & 0xffffffff))" >/dev/null 2>&1 && return 0
echo "== steam: launching the requested game again $game"
_dd_hand_off
else
echo "== steam: the game took focus while waiting; not launching again"
fi
}
if [ -n "${DD_LAUNCH_DIR:-}" ] && [ -z "${DD_DESKTOP:-}" ]; then
_dd_held=
-38
View File
@@ -532,44 +532,6 @@ class AgentTest(unittest.TestCase):
with self.assertRaises(agent.AgentError):
agent.steam_clear_pending_interstitial(self.dir, "0")
@unittest.skipUnless(shutil.which("node"), "node is not installed")
def test_pending_launch_responses_match_the_waiting_task(self):
with patch.object(agent, "cdp_eval", return_value={"value": False}) as evaluate:
agent.steam_clear_pending_interstitial(self.dir, "413150")
expression = evaluate.call_args.args[1]
cases = [
({"strTaskName": "ShowInterstitials"}, [[7, "ShowInterstitials"]]),
({"strTaskName": "ShowInterstitials", "gameid": str((413150 << 32) | 0x02000000)}, [[7, "ShowInterstitials"]]),
({"strTaskName": "ShowInterstitials", "gameid": "invalid"}, []),
({"strTaskName": "CreatingProcess"}, [[7, "CreatingProcess"]]),
({"strTaskName": "ShowLaunchOption", "nLaunchOption": 2}, [[7, "2"]]),
({"strTaskName": "ShowLaunchOption", "nLaunchOption": -1}, [[7, "0"]]),
({"strTaskName": "ShowLaunchOption", "nLaunchOption": 0}, [[7, "0"]]),
({"strTaskName": "SynchronizingCloud"}, []),
({"strTaskName": "ShowEula"}, []),
({"strTaskName": "DownloadingDepots"}, []),
({"strTaskName": "ShowInterstitials", "bWaitingForUI": False}, []),
({"strTaskName": "ShowInterstitials", "strActionName": "VerifyApp"}, []),
({"strTaskName": "ShowInterstitials", "gameid": "123"}, []),
]
for changes, expected in cases:
action = dict(nGameActionID=7, gameid="413150", strActionName="LaunchApp", bWaitingForUI=True)
action.update(changes)
harness = """
const calls = [];
globalThis.SteamClient = {Apps: {
GetActiveGameActions: async () => %s,
ContinueGameAction: (...args) => calls.push(args)
}};
(async () => {
const result = await %s;
process.stdout.write(JSON.stringify({result, calls}));
})().catch(error => { console.error(error); process.exit(1); });
""" % (json.dumps([action]), expression)
with self.subTest(changes=changes):
result = subprocess.run(["node", "-e", harness], text=True, capture_output=True, check=True, timeout=5)
self.assertEqual({"result": bool(expected), "calls": expected}, json.loads(result.stdout))
def test_asleep_command_reports_steams_own_sleep_state(self):
with patch.dict(os.environ, {"DD_LAUNCH_DIR": str(self.dir.parent)}):
for value, code in (("yes", 0), ("no", 5), ("unknown", 1), (None, 1)):
+10 -18
View File
@@ -32,7 +32,6 @@ class SteamColdLaunchTest(unittest.TestCase):
xprop.write_text(
"#!/bin/sh\n"
"n=$(cat \"$FOCUS_COUNT\" 2>/dev/null || echo 0); n=$((n + 1)); echo $n > \"$FOCUS_COUNT\"\n"
"[ ! -f \"$HOME/overview-focus\" ] || { echo \"GAMESCOPE_FOCUSED_APP(CARDINAL) = $(cat \"$HOME/overview-focus\")\"; exit; }\n"
"i=0; last=769\n"
"for f in $FOCUS; do i=$((i + 1)); last=$f; [ \"$i\" -eq \"$n\" ] && break; done\n"
"echo \"GAMESCOPE_FOCUSED_APP(CARDINAL) = $last\"\n")
@@ -43,7 +42,6 @@ class SteamColdLaunchTest(unittest.TestCase):
"printf '%s\n' \"$@\" > \"$AGENT_ARGS\"\n"
"n=$(cat \"$AGENT_COUNT\" 2>/dev/null || echo 0); n=$((n + 1)); echo $n > \"$AGENT_COUNT\"\n"
"[ \"$1\" = --steam-ready ] && exit \"${READY_STATUS:-0}\"\n"
"if [ \"$1\" = --wait-steam-shortcut-overview ] && [ -n \"$OVERVIEW_FOCUS\" ]; then echo \"$OVERVIEW_FOCUS\" > \"$HOME/overview-focus\"; fi\n"
"exit \"$AGENT_STATUS\"\n")
agent.chmod(0o755)
sleep = self.bin / "sleep"
@@ -58,13 +56,13 @@ class SteamColdLaunchTest(unittest.TestCase):
pgrep.chmod(0o755)
self.block = LAUNCH_BLOCK.replace("/usr/local/bin/droiddeck-agent", str(agent))
def run_launch(self, target, focus="769", helper_status=0, running_after=999, ready_status=0, overview_focus=""):
def run_launch(self, target, focus="769", helper_status=0, running_after=999, ready_status=0):
env = dict(os.environ, HOME=str(self.root), DD_LAUNCH_DIR=str(self.launch_dir),
PATH=str(self.bin) + os.pathsep + os.environ["PATH"],
AGENT_ARGS=str(self.agent_args), SLEEP_ARGS=str(self.sleep_args),
AGENT_COUNT=str(self.agent_count), AGENT_STATUS=str(helper_status),
FOCUS=focus, FOCUS_COUNT=str(self.focus_count), RUNNING_AFTER=str(running_after),
READY_STATUS=str(ready_status), OVERVIEW_FOCUS=overview_focus)
READY_STATUS=str(ready_status))
script = self.block + "\nprintf 'ARG:%s\n' \"$@\"\nwait\n"
url = target if target.startswith("steam://") else "steam://rungameid/" + target
result = subprocess.run(["bash", "-c", script, "cold-launch", url],
@@ -87,7 +85,7 @@ class SteamColdLaunchTest(unittest.TestCase):
self.assertEqual(1, len(self.hand_offs(result)), result.stdout)
self.assertIn("clearing the pending launch interstitial", result.stdout)
self.assertEqual(["--steam-clear-pending-interstitial", "3551182975"], self.agent_args.read_text().splitlines())
self.assertEqual(["1"] * 120, self.sleep_args.read_text().split())
self.assertEqual(["1"] * 20, self.sleep_args.read_text().split())
def test_a_timed_out_overview_still_gets_one_more_hand_off(self):
result = self.run_launch(SHORTCUT, focus="769", helper_status=1)
@@ -99,7 +97,7 @@ class SteamColdLaunchTest(unittest.TestCase):
result = self.run_launch("413150", ready_status=1)
self.assertEqual("413150", (self.launch_dir / "steam-game").read_text().strip())
self.assertEqual(1, len(self.hand_offs(result)), result.stdout)
self.assertEqual(["1"] * 150, self.sleep_args.read_text().split())
self.assertEqual(["1"] * 30, self.sleep_args.read_text().split())
def test_a_focus_change_does_not_end_the_readiness_wait(self):
result = self.run_launch("413150", focus="769 4242", ready_status=1)
@@ -109,7 +107,7 @@ class SteamColdLaunchTest(unittest.TestCase):
def test_a_game_still_starting_without_a_window_is_not_launched_twice(self):
result = self.run_launch(SHORTCUT, running_after=1)
self.assertEqual(1, len(self.hand_offs(result)), result.stdout)
self.assertEqual(["--steam-clear-pending-interstitial", "3551182975"], self.agent_args.read_text().splitlines())
self.assertIn("--steam-ready", self.agent_args.read_text())
self.assertEqual(["-f", "reaper SteamLaunch AppId=3551182975( |$)"],
(self.root / "pgrep-args").read_text().splitlines())
self.assertIn("already starting", result.stdout)
@@ -121,24 +119,18 @@ class SteamColdLaunchTest(unittest.TestCase):
self.assertIn("started while waiting", result.stdout)
def test_a_game_that_takes_focus_during_the_overview_wait_is_not_started_twice(self):
result = self.run_launch(SHORTCUT, helper_status=0, overview_focus="4242")
focus = " ".join(["769"] * 23) + " 4242"
result = self.run_launch(SHORTCUT, focus=focus, helper_status=0)
self.assertEqual(1, len(self.hand_offs(result)), result.stdout)
self.assertNotIn("clearing the pending launch interstitial", result.stdout)
self.assertNotIn("launching the requested game again", result.stdout)
def test_native_app_id_recovers_pending_steps_without_relaunching(self):
def test_native_app_id_keeps_the_short_delay_and_is_handed_over_once(self):
result = self.run_launch("413150")
self.assertEqual("413150", (self.launch_dir / "steam-game").read_text().strip())
self.assertEqual(1, len(self.hand_offs(result)), result.stdout)
self.assertEqual(["1"] * 120, self.sleep_args.read_text().split())
self.assertEqual(["--steam-clear-pending-interstitial", "413150"], self.agent_args.read_text().splitlines())
self.assertEqual(121, int(self.agent_count.read_text()))
def test_native_app_recovery_does_not_stop_for_an_install_script_or_repeat_a_failed_launch(self):
result = self.run_launch("413150", helper_status=1, running_after=1)
self.assertEqual(1, len(self.hand_offs(result)), result.stdout)
self.assertEqual(121, int(self.agent_count.read_text()))
self.assertEqual(["--steam-clear-pending-interstitial", "413150"], self.agent_args.read_text().splitlines())
self.assertFalse(self.sleep_args.exists())
self.assertIn("--steam-ready", self.agent_args.read_text())
def test_a_restart_for_a_game_uses_the_same_hand_off(self):
loop = SOURCE[SOURCE.index(' if [ -s "$HOME/.dd-restart-game" ]; then'):SOURCE.index(' if [ -f "$HOME/.dd-restart-once" ]; then')]
+6 -71
View File
@@ -35,21 +35,13 @@ STRUCT
static struct wl_list g_bufs;
static struct wl_event_loop *g_loop;
static int g_import_failed_logged;
static int import_ok, imports, imported_flags, deferred_releases, frees, sources, removes, merge_ok, merges;
static struct wl_event_source g_sources[4];
static struct wl_event_source *g_removed;
static int import_ok, imports, imported_flags, deferred_releases, frees, sources;
static struct wl_event_source g_source;
void wl_list_init(struct wl_list *l) { l->prev = l; l->next = l; }
void wl_list_insert(struct wl_list *l, struct wl_list *e) {
e->next = l->next; e->prev = l; l->next->prev = e; l->next = e;
}
static int fake_ioctl(int fd, unsigned long req, void *arg) {
if (req == SYNC_IOC_MERGE) {
struct sync_merge_data *m = arg;
assert(fd >= 0 && m->fd2 >= 0 && fd != m->fd2);
++merges;
if (!merge_ok) { errno = ENOTTY; return -1; }
m->fence = dup(m->fd2); return 0;
}
struct droiddeck_dma_buf_sync_file *f = arg;
(void)fd;
assert(req == DROIDDECK_DMA_BUF_IOCTL_IMPORT_SYNC_FILE);
@@ -62,17 +54,13 @@ static int droiddeck_dmabuf_fd(struct dmabuf_buffer *b) { return b->fd; }
static void droiddeck_log(const char *tag, const char *fmt, ...) { (void)tag; (void)fmt; }
static struct wl_event_source *wl_event_loop_add_fd(struct wl_event_loop *l, int fd, uint32_t m,
int (*cb)(int, uint32_t, void *), void *d) {
(void)l; (void)fd; (void)m; (void)cb; (void)d; return &g_sources[sources++];
(void)l; (void)fd; (void)m; (void)cb; (void)d; ++sources; return &g_source;
}
static void wl_event_source_remove(struct wl_event_source *s) { assert(s); ++removes; g_removed = s; }
static int on_fence_readable(int fd, uint32_t mask, void *data) { (void)fd; (void)mask; (void)data; return 0; }
static void send_deferred_release(struct ahb_buf *ab) { ++deferred_releases; ab->release_pending = 0; }
static void maybe_free(struct ahb_buf *ab) { (void)ab; ++frees; }
FUNCTIONS
static void reset(void) {
import_ok = imports = imported_flags = deferred_releases = frees = sources = removes = merge_ok = merges = 0;
g_removed = NULL;
}
static void reset(void) { import_ok = imports = imported_flags = deferred_releases = frees = sources = 0; }
static void run(int display, int can_import, int fence_signalled) {
reset();
wl_list_init(&g_bufs);
@@ -90,48 +78,12 @@ static void run(int display, int can_import, int fence_signalled) {
assert(deferred_releases == 1 && frees == 1 && !ab.fence_src && ab.fence_fd == -1);
errno = 0; assert(fcntl(fds[0], F_GETFD) == -1 && errno == EBADF);
} else {
assert(deferred_releases == 0 && sources == 1 && ab.fence_src == &g_sources[0] && ab.fence_fd == fds[0]);
assert(deferred_releases == 0 && sources == 1 && ab.fence_src == &g_source && ab.fence_fd == fds[0]);
close(fds[0]);
}
close(fds[1]);
}
static int closed(int fd) { errno = 0; return fcntl(fd, F_GETFD) == -1 && errno == EBADF; }
static void rerelease(int can_merge, int second_import, int second_signalled) {
reset();
wl_list_init(&g_bufs);
struct dmabuf_buffer b = {.fd = 7};
struct ahb_buf ab;
memset(&ab, 0, sizeof(ab));
ab.id = 42; ab.b = &b; ab.on_layer = 1; ab.release_pending = 1; ab.fence_fd = -1;
wl_list_insert(&g_bufs, &ab.link);
int first[2], second[2];
assert(pipe(first) == 0 && pipe(second) == 0);
handle_released(42, first[0]);
assert(ab.fence_src == &g_sources[0] && ab.fence_fd == first[0] && deferred_releases == 0);
ab.on_layer = 1;
if (second_signalled) assert(write(second[1], "x", 1) == 1);
import_ok = second_import; merge_ok = can_merge;
handle_released(42, second[0]);
assert(!ab.on_layer && deferred_releases == 0 && frees == 0);
if (second_import || second_signalled) {
assert(removes == 0 && merges == 0 && sources == 1 && ab.fence_src == &g_sources[0] && ab.fence_fd == first[0]);
assert(closed(second[0]));
close(first[0]);
} else {
assert(merges == 1 && removes == 1 && g_removed == &g_sources[0]);
assert(sources == 2 && ab.fence_src == &g_sources[1] && ab.fence_fd >= 0);
assert(closed(first[0]));
if (can_merge) assert(closed(second[0]) && ab.fence_fd != second[0]);
else assert(ab.fence_fd == second[0]);
close(ab.fence_fd);
}
close(first[1]); close(second[1]);
}
int main(int argc, char **argv) {
if (argc == 5 && !strcmp(argv[1], "rerelease")) {
rerelease(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
return 0;
}
assert(argc == 4);
run(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
return 0;
@@ -151,7 +103,7 @@ class ZeroCopyReleaseAtLatchTest(unittest.TestCase):
ahb, re.M))
struct = re.search(r'struct ahb_buf \{.*?\n\};', ahb, re.S).group()
functions = '\n'.join(function(ahb, n) for n in ('find_buf', 'handle_released'))
source = HARNESS.replace('DEFINES', '#include <fcntl.h>\n#include <linux/sync_file.h>\n#include <sys/ioctl.h>\n' + defines)
source = HARNESS.replace('DEFINES', '#include <fcntl.h>\n#include <sys/ioctl.h>\n' + defines)
source = source.replace('STRUCT', struct).replace('FUNCTIONS', functions)
cls.tmp = tempfile.TemporaryDirectory(prefix='droiddeck-release-latch-')
cls.addClassCleanup(cls.tmp.cleanup)
@@ -184,23 +136,6 @@ class ZeroCopyReleaseAtLatchTest(unittest.TestCase):
def test_signalled_fence_releases_without_waiting(self):
self.scenario(1, 0, 1)
def rerelease(self, can_merge, second_import, second_signalled):
result = subprocess.run([str(self.binary), 'rerelease', str(can_merge), str(second_import), str(second_signalled)],
capture_output=True, text=True, timeout=10)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_second_unsignalled_fence_merges_into_the_pending_wait(self):
self.rerelease(1, 0, 0)
def test_second_unsignalled_fence_replaces_the_wait_when_merge_fails(self):
self.rerelease(0, 0, 0)
def test_second_imported_fence_keeps_the_pending_wait(self):
self.rerelease(1, 1, 0)
def test_second_signalled_fence_keeps_the_pending_wait(self):
self.rerelease(1, 0, 1)
if __name__ == '__main__':
unittest.main()