-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path__init__.py
More file actions
281 lines (208 loc) · 9.48 KB
/
__init__.py
File metadata and controls
281 lines (208 loc) · 9.48 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# -*- coding: utf-8; -*-
# Based on original Joystick Gremlin work by Lionel Ott and other contributors - Joystick Gremlin Ex is (C) EMCS 2025
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
from PySide6 import QtWidgets, QtCore
from lxml import etree as ElementTree
import gremlin.base_profile
import gremlin.config
from gremlin.input_types import InputType
import gremlin.ui.dialogs
import gremlin.ui.input_item
import gremlin.ui.ui_common
import gremlin.util
from gremlin.util import safe_read, safe_format
import subprocess
import logging
syslog = logging.getLogger("system")
class RunProcessWidget(gremlin.ui.input_item.AbstractActionWidget):
"""Widget which allows the configuration of TTS actions."""
def __init__(self, action_data, parent=None):
super().__init__(action_data, parent=parent)
assert isinstance(action_data, RunProcess)
def _create_ui(self):
self.process_widget = gremlin.ui.ui_common.QPathLineItem("Process:",self.action_data.process, self.action_data)
self.process_widget.pathChanged.connect(self._process_changed_cb)
self.process_widget.open.connect(self._process_open_cb)
self.process_widget.installEventFilter(self)
self.args_widget = QtWidgets.QPlainTextEdit()
self.args_widget.setPlainText(self.action_data.arguments)
self.args_widget.textChanged.connect(self._args_changed_cb)
self.args_widget.installEventFilter(self)
self.run_widget = QtWidgets.QPushButton("Test")
self.run_widget.setIcon(gremlin.util.load_icon("fa6s.play",qta_color = gremlin.ui.ui_common.Color.activeColor()))
self.run_widget.setToolTip("Runs the process")
self.run_widget.clicked.connect(self._run_process)
self.chkb_exec_on_release = QtWidgets.QCheckBox("Exec on release")
self.chkb_exec_on_release.setChecked(self.action_data.exec_on_release)
self.chkb_exec_on_release.setToolTip("Execute the command on input release instead of input press")
self.chkb_exec_on_release.clicked.connect(self._exec_on_release_changed)
self.options_container_widget = QtWidgets.QWidget()
self.options_container_widget.setContentsMargins(0,0,0,0)
self.options_container_layout = QtWidgets.QHBoxLayout(self.options_container_widget)
self.options_container_layout.setContentsMargins(0,0,0,0)
self.args_by_line_widget = QtWidgets.QCheckBox("Argument per line")
self.args_by_line_widget.setChecked(self.action_data.args_per_line)
self.args_by_line_widget.setToolTip("When enabled, each line in the argument list will be passed as a separate argument to the process")
self.args_by_line_widget.clicked.connect(self._args_per_line_changed)
self.options_container_layout.addWidget(QtWidgets.QLabel("Arguments:"))
self.options_container_layout.addStretch()
self.options_container_layout.addWidget(self.args_by_line_widget)
self.container_widget = QtWidgets.QWidget()
self.container_layout = QtWidgets.QHBoxLayout(self.container_widget)
self.container_layout.addWidget(self.run_widget)
self.container_layout.addWidget(self.chkb_exec_on_release)
self.container_layout.addStretch()
self.main_layout.addWidget(self.process_widget)
self.main_layout.addWidget(self.options_container_widget)
self.main_layout.addWidget(self.args_widget)
self.main_layout.addWidget(self.container_widget)
self._update_ui()
def _process_open_cb(self, widget):
''' opens the process executable '''
fname = widget.data.process
self.executable_dialog = gremlin.ui.dialogs.ProcessWindow(fname)
self.executable_dialog.setWindowModality(QtCore.Qt.ApplicationModal)
self.executable_dialog.data = widget
self.executable_dialog.process_selected.connect(self._select_executable)
self.executable_dialog.show()
def _select_executable(self, fname):
"""Adds the provided executable to the list of configurations.
:param fname the executable for which to add a mapping
"""
widget = self.sender()
w = widget.data
item = w.data
item.process = fname
with QtCore.QSignalBlocker(w):
w.setText(fname)
self.action_data.process = fname
self._update_ui()
@QtCore.Slot(bool)
def _args_per_line_changed(self, checked):
self.action_data.args_per_line = checked
def _exec_on_release_changed(self, checked):
self.action_data.exec_on_release = checked
@QtCore.Slot(object, str)
def _process_changed_cb(self, widget, text):
self.action_data.process = text
self._update_ui()
def eventFilter(self, widget, event):
t = event.type()
if t == QtCore.QEvent.Type.FocusOut:
if widget == self.args_widget:
self.action_data.arguments = self.args_widget.toPlainText()
elif widget == self.process_widget:
self._update_ui()
return False
def _update_ui(self):
text = self.process_widget.text()
if "\"" or "\\" in text:
# convert windows format to python format
text = text.replace("\"","").replace("\\","/")
with QtCore.QSignalBlocker(self.process_widget):
self.process_widget.setText(text)
self.action_data.process = text
enabled = os.path.isfile(self.action_data.process)
self.run_widget.setEnabled(enabled)
@QtCore.Slot()
def _args_changed_cb(self):
self.action_data.arguments = self.args_widget.toPlainText()
def _populate_ui(self):
pass
@QtCore.Slot()
def _run_process(self):
self.action_data.execute()
class RunProcessFunctor(gremlin.base_profile.AbstractFunctor):
def __init__(self, action, parent = None):
super().__init__(action, parent)
self.action_data = action
def process_event(self, event, value, extra_data = None):
execute = False
if self.action_data.exec_on_release and not event.is_pressed:
execute = True
elif event.is_pressed:
execute = True
if execute:
self.action_data.execute()
return True
class RunProcess(gremlin.base_profile.AbstractAction):
"""Action representing a single TTS entry."""
name = "Run Process"
tag = "run-process"
default_button_activation = (True, False)
# override default allowed inputs here
# input_types = [
# InputType.JoystickAxis,
# InputType.JoystickButton,
# InputType.JoystickHat,
# InputType.Keyboard
# ]
functor = RunProcessFunctor
widget = RunProcessWidget
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.process = "" # process to run
self.arguments = "" # args to send
self.args_per_line = True # one arg per line
self.exec_on_release = False # exec on release vs press
def display_name(self):
''' returns a display string for the current configuration '''
return f"Run Process: [{self.process}] Args: [{self.arguments}]"
def icon(self):
return "fa6s.bolt"
def requires_virtual_button(self):
return self.get_input_type() in [
InputType.JoystickAxis,
InputType.JoystickHat
]
def _parse_xml(self, node, data = None):
if "process" in node.attrib:
self.process = node.get("process")
if "args" in node.attrib:
self.arguments = node.get("args")
self.args_per_line = safe_read(node,"line_per_arg",bool, True)
self.exec_on_release = safe_read(node,"exec_on_release",bool, False)
def _generate_xml(self):
node = ElementTree.Element(self.tag)
node.set("process", self.process)
node.set("args", self.arguments)
node.set("line_per_arg", str(self.args_per_line))
node.set("exec_on_release", str(self.exec_on_release))
return node
def _is_valid(self):
return len(self.process) and os.path.isfile(self.process)
def execute(self):
''' executes the process '''
try:
if self.args_per_line:
args = self.arguments.splitlines()
else:
args = self.arguments
cmd_list = [self.process]
cmd_list.extend(arg for arg in args)
process = subprocess.Popen(cmd_list,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
syslog.info(f"PROC: execute process: {self.process} {args}")
if out:
syslog.info(f"\toutput: {out.decode()}")
if err:
syslog.warning(f"\terror: {err.decode()}")
except:
pass
version = 1
name = "run-process"
create = RunProcess