-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
85 lines (69 loc) · 2.25 KB
/
main.py
File metadata and controls
85 lines (69 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import subprocess
import sys
import decky_plugin
# append py_modules to PYTHONPATH
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/py_modules")
PLUGIN_BIN_DIR: str = decky_plugin.DECKY_PLUGIN_DIR + "/bin"
# Create a subprocess environment with an empty LD_LIBRARY_PATH
env: dict[str, str] = os.environ.copy()
del env["LD_LIBRARY_PATH"]
# Return umtprd pid if running, or 0 otherwise
def get_umtprd_pid() -> int:
pid = ""
try:
with subprocess.Popen(
["pgrep", "--full", "--oldest", "umtprd"], stdout=subprocess.PIPE, env=env
) as p:
assert p.stdout is not None
pid = p.stdout.read().strip()
except Exception:
return 0
if not pid:
return 0
return int(pid)
# Check if umtprd is running
def is_running() -> bool:
pid = get_umtprd_pid()
if pid != 0:
return True
else:
return False
class Plugin:
# Asyncio-compatible long-running code, executed in a task when the plugin is loaded
async def _main(self):
pass
# Function called first during the unload process,
# utilize this to handle your plugin being removed
async def _unload(self):
await self.stop_mtp()
pass
# Check if umtprd is running
async def is_running(self) -> bool:
return is_running()
# Check if Dual-Role Device is enabled in BIOS
async def is_drd_enabled(self) -> bool:
try:
with subprocess.Popen(
"lsmod | grep dwc3", shell=True, stdout=subprocess.PIPE, env=env
) as p:
assert p.stdout is not None
result = p.stdout.read().strip()
except Exception:
return False
if not result:
return False
else:
return True
# Toggle MTP
async def toggle_mtp(self) -> bool:
if not is_running():
_ = subprocess.run("./start.sh", cwd=PLUGIN_BIN_DIR, shell=True, env=env)
else:
_ = subprocess.run("./stop.sh", cwd=PLUGIN_BIN_DIR, shell=True, env=env)
return is_running()
# Stop MTP
async def stop_mtp(self):
if not is_running():
return
_ = subprocess.run("./stop.sh", cwd=PLUGIN_BIN_DIR, shell=True, env=env)