annotate libervia/cli/cmd_remote_control.py @ 4305:4cd4922de876

tests: reformat tests using black.
author Goffi <goffi@goffi.org>
date Thu, 26 Sep 2024 16:11:56 +0200
parents 0d7bb4df2343
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4243
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Libervia CLI
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 import asyncio
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from functools import partial
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 import logging
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 import xml.etree.ElementTree as ET
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
25
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from prompt_toolkit.input import Input, create_input
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from prompt_toolkit.key_binding import KeyPress
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from prompt_toolkit.keys import Keys
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from prompt_toolkit.patch_stdout import patch_stdout
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from prompt_toolkit import PromptSession
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from libervia.backend.core import exceptions
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from libervia.backend.core.i18n import _
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from libervia.backend.tools.common import data_format
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from libervia.cli.constants import Const as C
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from libervia.frontends.tools import aio, jid
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from libervia.frontends.tools.webrtc_remote_control import WebRTCRemoteController
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
38
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 from . import base
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 from . import xmlui_manager
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 __commands__ = ["RemoteControl"]
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
44
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 class Send(base.CommandBase):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 def __init__(self, host):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 super(Send, self).__init__(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 host,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 "send",
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 use_progress=True,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 use_verbose=True,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 help=_("remote control another device"),
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self.remote_controller: WebRTCRemoteController | None = None
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 def add_parser_options(self):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self.parser.add_argument("jid", help=_("the destination jid"))
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
58
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 def send_key(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 self,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 key: str,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 code: str,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 ctrl_key: bool = False,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 shift_key: bool = False,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 alt_key: bool = False,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 meta_key: bool = False,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 ) -> None:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 """
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 Send the key press input.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
70
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 @param key: The key pressed.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 @param code: The code of the key pressed.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 @param ctrl_key: Whether the Ctrl key was pressed.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 @param shift_key: Whether the Shift key was pressed.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 @param alt_key: Whether the Alt key was pressed.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 @param meta_key: Whether the Meta key was pressed.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 """
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 assert self.remote_controller is not None
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 event_data = {
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 "key": key,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 "code": code,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 "ctrlKey": ctrl_key,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 "shiftKey": shift_key,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 "altKey": alt_key,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 "metaKey": meta_key,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 }
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 # we send both events as we don't distinguish them.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 for evt_type in ["keydown", "keyup"]:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 key_data = {"type": evt_type, **event_data}
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 self.remote_controller.send_input(key_data)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 print(f"Sending {key_data}")
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
92
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 def handle_key_press(self, key_press: KeyPress) -> None:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 """Handle key press event."""
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 key = key_press.key
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 if key.startswith("c-"):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 key = key[2:] # remove "c-" prefix
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 elif key.startswith("s-"):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 key = key[2:] # remove "s-" prefix
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 self.send_key(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 key=key.lower(),
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 # FIXME: handle properly code.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 code=key.lower(),
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 ctrl_key=key_press.key.startswith("c-"),
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 shift_key=key_press.key.startswith("s-") or key.isupper(),
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 # FIXME: alt-key is translated to escape + key, see
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 # https://python-prompt-toolkit.readthedocs.io/en/master/pages/advanced_topics/key_bindings.html
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 alt_key=False,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 # TODO
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 meta_key=False,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
112
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 def on_keys_ready(self, input_: Input, handle_key_fut: asyncio.Future) -> None:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 for key_press in input_.read_keys():
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 if key_press.key == Keys.ControlC:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 handle_key_fut.set_exception(KeyboardInterrupt())
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 else:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 self.handle_key_press(key_press)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
119
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 async def confirm_ctrl_c(self) -> bool:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 """Ask user if they want to send Ctrl-C event or quit."""
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 session = PromptSession()
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 with patch_stdout():
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 while True:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 response = await session.prompt_async(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 "Ctrl-C pressed. Send event (e) or quit (q)? (e/q): "
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 if response.lower() == "e":
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 return True
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 elif response.lower() == "q":
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 return False
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
132
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 async def handle_ctrl_c(self) -> None:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 """Handle Ctrl-C key press."""
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 if await self.confirm_ctrl_c():
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 self.send_key(key="c", code="c", ctrl_key=True)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 else:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 await self.host.a_quit()
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
139
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 async def _on_open(self, remote_controller: WebRTCRemoteController) -> None:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 input_ = create_input()
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 self.disp(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 "Connection with peer established. Your keyboard input will be sent to "
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 "controlled device."
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
146
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 while True:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 handle_key_fut = asyncio.Future()
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 try:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 with input_.raw_mode():
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 with input_.attach(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 partial(self.on_keys_ready, input_, handle_key_fut)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 ):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 await handle_key_fut
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 except KeyboardInterrupt:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 await self.handle_ctrl_c()
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
157
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 async def start(self):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 root_logger = logging.getLogger()
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 # we don't want any formatting for messages from webrtc
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 for handler in root_logger.handlers:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 handler.setFormatter(None)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 if self.verbosity == 0:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 root_logger.setLevel(logging.ERROR)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 if self.verbosity >= 1:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 root_logger.setLevel(logging.WARNING)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 if self.verbosity >= 2:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 root_logger.setLevel(logging.DEBUG)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
169
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 aio.install_glib_asyncio_iteration()
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 self.remote_controller = WebRTCRemoteController(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 self.host.bridge, self.profile, end_call_cb=self.host.a_quit
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 await self.remote_controller.start(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 self.args.jid, {"devices": {"keyboard": {}}}, on_open_cb=self._on_open
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
177
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
178
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 class Receive(base.CommandAnswering):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 def __init__(self, host):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 super().__init__(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 host,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 "receive",
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 use_verbose=True,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 help=_("be remote controlled by another device"),
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 self.action_callbacks = {
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 C.META_TYPE_CONFIRM: self.on_confirm_action,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 C.META_TYPE_NOT_IN_ROSTER_LEAK: self.on_confirm_action,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 C.META_TYPE_REMOTE_CONTROL: self.on_remote_control_action,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 }
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 self.receiver = None
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
193
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 def add_parser_options(self):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 self.parser.add_argument(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 "-S",
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 "--share-screen",
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 choices=["yes", "no", "auto"],
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 default="auto",
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 help=_("share the screen (default: auto)"),
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 self.parser.add_argument(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 "jids",
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 nargs="+",
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 help=_("jids accepted automatically"),
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
207
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 async def start_webrtc(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 self, from_jid: jid.JID, session_id: str, screenshare: dict, devices: dict
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 ) -> None:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 """Start the WebRTC workflown"""
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 assert self.receiver is not None
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 root_logger = logging.getLogger()
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 # we don't want any formatting for messages from webrtc
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 for handler in root_logger.handlers:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 handler.setFormatter(None)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 if self.verbosity == 0:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 root_logger.setLevel(logging.ERROR)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 if self.verbosity >= 1:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 root_logger.setLevel(logging.WARNING)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 if self.verbosity >= 2:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 root_logger.setLevel(logging.DEBUG)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 await self.receiver.start_receiving(from_jid, session_id, screenshare)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
224
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 def get_xmlui_id(self, action_data):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 # FIXME: we temporarily use ElementTree, but a real XMLUI managing module
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 # should be available in the futur
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 # TODO: XMLUI module
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 try:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 xml_ui = action_data["xmlui"]
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 except KeyError:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 self.disp(_("Action has no XMLUI"), 1)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 else:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 ui = ET.fromstring(xml_ui.encode("utf-8"))
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 xmlui_id = ui.get("submit")
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 if not xmlui_id:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 self.disp(_("Invalid XMLUI received"), error=True)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 return xmlui_id
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
239
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 async def get_confirmation(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 self, action_data: dict, pre_accepted: bool = False
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 ) -> tuple[str, jid.JID, bool]:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 """Check if action is confirmed, and ask user otherwise
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
244
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 @param action_data: Data as used in on_action_new.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
246 @return: a tuple with:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 - XMLUI ID
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 - sender JID
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 - confirmation boolean
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 """
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 xmlui_id = self.get_xmlui_id(action_data)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 if xmlui_id is None:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 self.disp("Internal ERROR: xmlui_id missing", error=True)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 raise exceptions.InternalError()
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 if (
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 action_data["type"] != C.META_TYPE_REMOTE_CONTROL
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
257 and action_data.get("subtype") != C.META_TYPE_REMOTE_CONTROL
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 ):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 self.disp(_("Ignoring confirm dialog unrelated to remote control."), 1)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
260 raise exceptions.CancelError
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 try:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 from_jid = jid.JID(action_data["from_jid"])
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 except ValueError:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 self.disp(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 _('invalid "from_jid" value received, ignoring: {value}').format(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
266 value=action_data["from_jid"]
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
267 ),
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 error=True,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 raise exceptions.DataError
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
271 except KeyError:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 self.disp(_('ignoring action without "from_jid" value'), error=True)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
273 raise exceptions.DataError
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
274
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
275 self.disp(_("Confirmation needed for request from an entity not in roster"), 1)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
276
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
277 if pre_accepted:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
278 # Session has already been accepted during pre-flight.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
279 confirmed = True
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
280 elif action_data["type"] == C.META_TYPE_CONFIRM and not self.bare_jids:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
281 # Sender is in roster, and we have an "accept all" policy.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
282 confirmed = True
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 self.disp(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 _(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 f"{from_jid} automatically confirmed due to entity being in roster"
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 " and accept all policy."
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
287 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 elif from_jid.bare in self.bare_jids:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 # If the sender is expected, we can confirm the session.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 confirmed = True
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 self.disp(_("Sender confirmed because they are explicitly expected"), 1)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
293 else:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 # Not automatically accepted, we ask authorisation to the user.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
295 xmlui = xmlui_manager.create(self.host, action_data["xmlui"])
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
296 confirmed = await self.host.confirm(xmlui.dlg.message)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
297 return xmlui_id, from_jid, confirmed
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
298
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 async def on_confirm_action(self, action_data, action_id, security_limit, profile):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
300 """Handle pre-flight remote control request"""
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
301 try:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
302 xmlui_id, from_jid, confirmed = await self.get_confirmation(action_data)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
303 except exceptions.InternalError:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
304 self.host.quit_from_signal(1)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
305 return
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
306 except (exceptions.InternalError, exceptions.DataError):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
307 return
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
308 xmlui_data = {"answer": C.bool_const(confirmed)}
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
309 await self.host.bridge.action_launch(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
310 xmlui_id, data_format.serialise(xmlui_data), profile_key=profile
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
311 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
312 if not confirmed:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
313 self.disp(_("Session refused for {from_jid}").format(from_jid=from_jid))
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
314 self.host.quit_from_signal(0)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
315
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
316 async def on_remote_control_action(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
317 self, action_data, action_id, security_limit, profile
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
318 ):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
319 """Handles actual remote control request"""
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
320 try:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 session_id = action_data["session_id"]
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 except KeyError:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
323 self.disp(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
324 f"Internal Error: Session ID is missing in action data: {action_data=}",
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
325 error=True,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
326 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
327 return
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
328 pre_accepted = action_data.get("pre_accepted", False)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
329 try:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
330 xmlui_id, from_jid, confirmed = await self.get_confirmation(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
331 action_data, pre_accepted
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
332 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
333 except exceptions.InternalError:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
334 self.host.quit_from_signal(1)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
335 return
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
336 except (exceptions.InternalError, exceptions.DataError):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
337 return
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
338 if confirmed:
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
339 await self.start_webrtc(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
340 from_jid,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
341 session_id,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
342 action_data.get("screenshare", {}),
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
343 action_data.get("devices", {}),
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
344 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
345 xmlui_data = {"answer": C.bool_const(confirmed)}
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
346 await self.host.bridge.action_launch(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
347 xmlui_id, data_format.serialise(xmlui_data), profile_key=profile
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
348 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
349
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
350 async def start(self):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
351 self.bare_jids = [jid.JID(jid_).bare for jid_ in self.args.jids]
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
352 from libervia.frontends.tools.webrtc_remote_control import (
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
353 WebRTCRemoteControlReceiver,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
354 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
355
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
356 self.receiver = WebRTCRemoteControlReceiver(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
357 self.host.bridge,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
358 self.profile,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
359 on_close_cb=self.host.a_quit,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
360 verbose=self.verbosity >= 1,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
361 )
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
362 aio.install_glib_asyncio_iteration()
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
363 # FIXME: for now AUTO always do the screen sharing, but is should be disabled and
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
364 # appropriate method should be used when no desktop environment is detected.
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
365 with_screen_sharing = self.args.share_screen in ("yes", "auto")
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4243
diff changeset
366 await self.receiver.request_remote_desktop(with_screen_sharing)
4243
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
367
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
368 self.disp(_("Waiting for controlling device…"))
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
369 await self.start_answering()
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
370
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
371
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
372 class RemoteControl(base.CommandBase):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
373 subcommands = (Send, Receive)
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
374
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
375 def __init__(self, host):
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
376 super().__init__(
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
377 host,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
378 "remote-control",
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
379 use_profile=False,
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
380 help=_("Control or be controlled by another device."),
e47e29511d57 cli (remote-control): new `remote-control` command:
Goffi <goffi@goffi.org>
parents:
diff changeset
381 )