| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# gcdemu: Gtk/AppIndicator CDEmu GUI |
| 4 |
# Copyright (C) 2006-2026 Rok Mandeljc |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation; either version 2 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along |
| 17 |
# with this program; if not, write to the Free Software Foundation, Inc., |
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
|
| 20 |
import argparse |
| 21 |
import datetime |
| 22 |
import gettext |
| 23 |
import os |
| 24 |
import signal |
| 25 |
import subprocess |
| 26 |
import sys |
| 27 |
import weakref |
| 28 |
|
| 29 |
import gi |
| 30 |
|
| 31 |
# Put under if block to avoid triggering E402 warnings in subsequent imports... |
| 32 |
if True: |
| 33 |
gi.require_version('GLib', '2.0') |
| 34 |
gi.require_version('GObject', '2.0') |
| 35 |
gi.require_version('Gio', '2.0') |
| 36 |
gi.require_version('Gtk', '3.0') |
| 37 |
gi.require_version('GdkPixbuf', '2.0') |
| 38 |
gi.require_version('Notify', '0.7') |
| 39 |
|
| 40 |
from gi.repository import GLib, GObject, Gio |
| 41 |
from gi.repository import Gtk, GdkPixbuf |
| 42 |
from gi.repository import Notify |
| 43 |
|
| 44 |
# AppIndicator |
| 45 |
try: |
| 46 |
gi.require_version('AppIndicator3', '0.1') |
| 47 |
|
| 48 |
from gi.repository import AppIndicator3 as AppIndicator |
| 49 |
have_app_indicator = True |
| 50 |
except Exception: |
| 51 |
try: |
| 52 |
gi.require_version('AyatanaAppIndicator3', '0.1') |
| 53 |
|
| 54 |
from gi.repository import AyatanaAppIndicator3 as AppIndicator |
| 55 |
have_app_indicator = True |
| 56 |
except Exception: |
| 57 |
have_app_indicator = False |
| 58 |
|
| 59 |
|
| 60 |
# *** Globals *** |
| 61 |
app_name = "gcdemu" |
| 62 |
app_version = "3.3.0" |
| 63 |
supported_daemon_interface_version = (7, 0) |
| 64 |
|
| 65 |
# I18n |
| 66 |
gettext.install(app_name) |
| 67 |
|
| 68 |
# Set process name |
| 69 |
if sys.platform == "linux": |
| 70 |
try: |
| 71 |
import ctypes |
| 72 |
libc = ctypes.CDLL("libc.so.6") |
| 73 |
libc.prctl(15, app_name.encode('utf-8'), 0, 0, 0) # 15 = PR_SET_NAME |
| 74 |
except Exception: |
| 75 |
pass |
| 76 |
|
| 77 |
|
| 78 |
######################################################################## |
| 79 |
# CDEmuDaemonProxy: Daemon proxy object # |
| 80 |
######################################################################## |
| 81 |
class CDEmuDaemonProxy(GObject.GObject): |
| 82 |
_name = 'net.sf.cdemu.CDEmuDaemon' |
| 83 |
_object_path = '/Daemon' |
| 84 |
|
| 85 |
def __init__(self): |
| 86 |
super().__init__() |
| 87 |
|
| 88 |
self.watcher_id = -1 |
| 89 |
self.proxy_handler_ids = [] |
| 90 |
self.proxy = None |
| 91 |
|
| 92 |
def cleanup(self): |
| 93 |
# Cancel watcher |
| 94 |
if self.watcher_id != -1: |
| 95 |
Gio.bus_unwatch_name(self.watcher_id) |
| 96 |
self.watcher_id = -1 |
| 97 |
|
| 98 |
# Clean up the proxy |
| 99 |
if self.proxy: |
| 100 |
for handler in self.proxy_handler_ids: |
| 101 |
self.proxy.disconnect(handler) |
| 102 |
self.proxy_handler_ids = [] |
| 103 |
self.proxy = None |
| 104 |
|
| 105 |
def connect_to_bus(self, use_system, autostart_daemon): |
| 106 |
# Cleanup |
| 107 |
self.cleanup() |
| 108 |
|
| 109 |
# Get the bus |
| 110 |
bus = Gio.bus_get_sync(Gio.BusType.SYSTEM if use_system else Gio.BusType.SESSION, None) |
| 111 |
|
| 112 |
# Create proxy on our interface |
| 113 |
self.proxy = Gio.DBusProxy.new_sync( |
| 114 |
bus, |
| 115 |
0, |
| 116 |
None, |
| 117 |
self._name, |
| 118 |
self._object_path, |
| 119 |
'net.sf.cdemu.CDEmuDaemon', |
| 120 |
None, |
| 121 |
) |
| 122 |
|
| 123 |
# Connect signal: g-signal |
| 124 |
handler_id = self.proxy.connect("g-signal", self.on_g_signal) |
| 125 |
self.proxy_handler_ids.append(handler_id) |
| 126 |
|
| 127 |
# Set up name watch; we do not try to autostart the daemon here |
| 128 |
self.watcher_id = Gio.bus_watch_name_on_connection( |
| 129 |
bus, |
| 130 |
self._name, |
| 131 |
0, |
| 132 |
lambda c, n, no: self.emit("DaemonStarted"), |
| 133 |
lambda c, n: self.emit("DaemonStopped"), |
| 134 |
) |
| 135 |
|
| 136 |
# Try to autostart daemon by pinging it |
| 137 |
if autostart_daemon: |
| 138 |
bus.call_sync( |
| 139 |
self._name, |
| 140 |
self._object_path, |
| 141 |
"org.freedesktop.DBus.Peer", |
| 142 |
"Ping", |
| 143 |
None, |
| 144 |
None, |
| 145 |
0, |
| 146 |
-1, |
| 147 |
None, |
| 148 |
) |
| 149 |
|
| 150 |
def on_g_signal(self, proxy, sender, signal, params): |
| 151 |
if signal == "DeviceStatusChanged": |
| 152 |
self.emit(signal, params[0]) |
| 153 |
elif signal == "DeviceOptionChanged": |
| 154 |
self.emit(signal, params[0], params[1]) |
| 155 |
elif signal == "DeviceMappingReady": |
| 156 |
self.emit(signal, params[0]) |
| 157 |
elif signal == "DeviceAdded": |
| 158 |
self.emit(signal) |
| 159 |
elif signal == "DeviceRemoved": |
| 160 |
self.emit(signal) |
| 161 |
else: |
| 162 |
print(f"Unknown signal: sender={sender!r}, signal={signal!r}, params={params!r}") |
| 163 |
|
| 164 |
def GetDaemonVersion(self): |
| 165 |
return self.proxy.GetDaemonVersion() |
| 166 |
|
| 167 |
def GetLibraryVersion(self): |
| 168 |
return self.proxy.GetLibraryVersion() |
| 169 |
|
| 170 |
def GetDaemonInterfaceVersion2(self): |
| 171 |
return self.proxy.GetDaemonInterfaceVersion2() |
| 172 |
|
| 173 |
def EnumDaemonDebugMasks(self): |
| 174 |
return self.proxy.EnumDaemonDebugMasks() |
| 175 |
|
| 176 |
def EnumLibraryDebugMasks(self): |
| 177 |
return self.proxy.EnumLibraryDebugMasks() |
| 178 |
|
| 179 |
def EnumSupportedParsers(self): |
| 180 |
return self.proxy.EnumSupportedParsers() |
| 181 |
|
| 182 |
def EnumSupportedWriters(self): |
| 183 |
return self.proxy.EnumSupportedWriters() |
| 184 |
|
| 185 |
def EnumSupportedFilterStreams(self): |
| 186 |
return self.proxy.EnumSupportedFilterStreams() |
| 187 |
|
| 188 |
def EnumWriterParameters(self, writer_id): |
| 189 |
return self.proxy.EnumWriterParameters('(s)', writer_id) |
| 190 |
|
| 191 |
def GetNumberOfDevices(self): |
| 192 |
return self.proxy.GetNumberOfDevices() |
| 193 |
|
| 194 |
def DeviceGetMapping(self, device_number): |
| 195 |
return self.proxy.DeviceGetMapping('(i)', device_number) |
| 196 |
|
| 197 |
def DeviceGetStatus(self, device_number): |
| 198 |
return self.proxy.DeviceGetStatus('(i)', device_number) |
| 199 |
|
| 200 |
def DeviceLoad(self, device_number, filenames, parameters): |
| 201 |
return self.proxy.DeviceLoad('(iasa{sv})', device_number, filenames, parameters) |
| 202 |
|
| 203 |
def DeviceCreateBlank(self, device_number, filename, parameters): |
| 204 |
return self.proxy.DeviceCreateBlank('(isa{sv})', device_number, filename, parameters) |
| 205 |
|
| 206 |
def DeviceUnload(self, device_number): |
| 207 |
return self.proxy.DeviceUnload('(i)', device_number) |
| 208 |
|
| 209 |
def DeviceGetOption(self, device_number, option_name): |
| 210 |
return self.proxy.DeviceGetOption('(is)', device_number, option_name) |
| 211 |
|
| 212 |
def DeviceSetOption(self, device_number, option_name, option_value): |
| 213 |
return self.proxy.DeviceSetOption('(isv)', device_number, option_name, option_value) |
| 214 |
|
| 215 |
def AddDevice(self): |
| 216 |
return self.proxy.AddDevice() |
| 217 |
|
| 218 |
def RemoveDevice(self): |
| 219 |
return self.proxy.RemoveDevice() |
| 220 |
|
| 221 |
|
| 222 |
GObject.type_register(CDEmuDaemonProxy) |
| 223 |
|
| 224 |
GObject.signal_new( |
| 225 |
"DaemonStarted", |
| 226 |
CDEmuDaemonProxy, |
| 227 |
GObject.SignalFlags.RUN_FIRST, |
| 228 |
None, |
| 229 |
(), |
| 230 |
) |
| 231 |
|
| 232 |
GObject.signal_new( |
| 233 |
"DaemonStopped", |
| 234 |
CDEmuDaemonProxy, |
| 235 |
GObject.SignalFlags.RUN_FIRST, |
| 236 |
None, |
| 237 |
(), |
| 238 |
) |
| 239 |
|
| 240 |
GObject.signal_new( |
| 241 |
"DeviceStatusChanged", |
| 242 |
CDEmuDaemonProxy, |
| 243 |
GObject.SignalFlags.RUN_FIRST, |
| 244 |
None, |
| 245 |
(GObject.TYPE_INT, ), |
| 246 |
) |
| 247 |
|
| 248 |
GObject.signal_new( |
| 249 |
"DeviceOptionChanged", |
| 250 |
CDEmuDaemonProxy, |
| 251 |
GObject.SignalFlags.RUN_FIRST, |
| 252 |
None, |
| 253 |
(GObject.TYPE_INT, GObject.TYPE_STRING, ), |
| 254 |
) |
| 255 |
|
| 256 |
GObject.signal_new( |
| 257 |
"DeviceMappingReady", |
| 258 |
CDEmuDaemonProxy, |
| 259 |
GObject.SignalFlags.RUN_FIRST, |
| 260 |
None, |
| 261 |
(GObject.TYPE_INT, ), |
| 262 |
) |
| 263 |
|
| 264 |
GObject.signal_new( |
| 265 |
"DeviceAdded", |
| 266 |
CDEmuDaemonProxy, |
| 267 |
GObject.SignalFlags.RUN_FIRST, |
| 268 |
None, |
| 269 |
(), |
| 270 |
) |
| 271 |
|
| 272 |
GObject.signal_new( |
| 273 |
"DeviceRemoved", |
| 274 |
CDEmuDaemonProxy, |
| 275 |
GObject.SignalFlags.RUN_FIRST, |
| 276 |
None, |
| 277 |
(), |
| 278 |
) |
| 279 |
|
| 280 |
|
| 281 |
######################################################################## |
| 282 |
# CDEmu: D-BUS interface object # |
| 283 |
######################################################################## |
| 284 |
class CDEmu(GObject.GObject): |
| 285 |
def __init__(self): |
| 286 |
super().__init__() |
| 287 |
|
| 288 |
self.daemon_proxy = CDEmuDaemonProxy() |
| 289 |
self.daemon_proxy.connect("DaemonStarted", lambda o: self.on_daemon_started()) |
| 290 |
self.daemon_proxy.connect("DaemonStopped", lambda o: self.on_daemon_stopped()) |
| 291 |
|
| 292 |
self.daemon_proxy.connect("DeviceAdded", lambda o: self.on_device_added()) |
| 293 |
self.daemon_proxy.connect("DeviceRemoved", lambda o: self.on_device_removed()) |
| 294 |
|
| 295 |
self.initial = True |
| 296 |
self.devices = [] |
| 297 |
|
| 298 |
def cleanup(self): |
| 299 |
# Cleanup the devices |
| 300 |
self.devices = [] |
| 301 |
|
| 302 |
def on_daemon_started(self): |
| 303 |
if not self.initial: |
| 304 |
self.emit("daemon-started") |
| 305 |
|
| 306 |
# Establish connection to daemon |
| 307 |
self.establish_connection() |
| 308 |
|
| 309 |
# Reset the initial run flag |
| 310 |
self.initial = False |
| 311 |
|
| 312 |
def on_daemon_stopped(self): |
| 313 |
if not self.initial: |
| 314 |
self.emit("daemon-stopped") |
| 315 |
|
| 316 |
# Cleanup |
| 317 |
self.emit("connection-lost") |
| 318 |
self.cleanup() |
| 319 |
|
| 320 |
# Reset the initial run flag |
| 321 |
self.initial = False |
| 322 |
|
| 323 |
def connect_to_bus(self, use_system, autostart_daemon): |
| 324 |
# Disconnect |
| 325 |
self.emit("connection-lost") |
| 326 |
|
| 327 |
# Cleanup |
| 328 |
self.cleanup() |
| 329 |
|
| 330 |
# Connect |
| 331 |
self.initial = True |
| 332 |
try: |
| 333 |
self.daemon_proxy.connect_to_bus(use_system, autostart_daemon) |
| 334 |
except GLib.Error as e: |
| 335 |
self.emit( |
| 336 |
"error", |
| 337 |
_("Daemon autostart error"), |
| 338 |
_("Daemon autostart failed. Error:\n%s") % (e), |
| 339 |
) |
| 340 |
|
| 341 |
def establish_connection(self): |
| 342 |
# Get the daemon interface version |
| 343 |
self.interface_version = self.daemon_proxy.GetDaemonInterfaceVersion2() |
| 344 |
|
| 345 |
# Validate the daemon interface version |
| 346 |
if ( |
| 347 |
self.interface_version[0] != supported_daemon_interface_version[0] or |
| 348 |
self.interface_version[1] < supported_daemon_interface_version[1] |
| 349 |
): |
| 350 |
self.emit( |
| 351 |
"error", |
| 352 |
_("Incompatible daemon interface"), |
| 353 |
_("CDEmu daemon interface version %i.%i detected, but version %i.%i is required!") % |
| 354 |
( |
| 355 |
self.interface_version[0], |
| 356 |
self.interface_version[1], |
| 357 |
supported_daemon_interface_version[0], |
| 358 |
supported_daemon_interface_version[1], |
| 359 |
), |
| 360 |
) |
| 361 |
|
| 362 |
# Cleanup |
| 363 |
self.cleanup() |
| 364 |
return |
| 365 |
|
| 366 |
# Get daemon version |
| 367 |
self.daemon_version = self.daemon_proxy.GetDaemonVersion() |
| 368 |
|
| 369 |
# Get library version |
| 370 |
self.library_version = self.daemon_proxy.GetLibraryVersion() |
| 371 |
|
| 372 |
# Get daemon debug masks |
| 373 |
self.daemon_debug_masks = self.daemon_proxy.EnumDaemonDebugMasks() |
| 374 |
|
| 375 |
# Get library debug masks |
| 376 |
self.library_debug_masks = self.daemon_proxy.EnumLibraryDebugMasks() |
| 377 |
|
| 378 |
# Get supported parsers |
| 379 |
self.supported_parsers = self.daemon_proxy.EnumSupportedParsers() |
| 380 |
|
| 381 |
# Get supported writers |
| 382 |
self.supported_writers = [] |
| 383 |
writers = self.daemon_proxy.EnumSupportedWriters() |
| 384 |
for writer in writers: |
| 385 |
writer_id = writer[0] |
| 386 |
writer_name = writer[1] |
| 387 |
parameter_sheet = self.daemon_proxy.EnumWriterParameters(writer_id) |
| 388 |
self.supported_writers.append((writer_id, writer_name, parameter_sheet)) |
| 389 |
|
| 390 |
# Get supported filter streams |
| 391 |
self.supported_filter_streams = self.daemon_proxy.EnumSupportedFilterStreams() |
| 392 |
|
| 393 |
# Get number of devices |
| 394 |
num_devices = self.daemon_proxy.GetNumberOfDevices() |
| 395 |
|
| 396 |
# Create devices |
| 397 |
for i in range(num_devices): |
| 398 |
self.devices.append(CDEmuDevice(i, self.daemon_proxy)) |
| 399 |
|
| 400 |
# Emit signal |
| 401 |
self.emit("connection-established", num_devices) |
| 402 |
|
| 403 |
def on_device_added(self): |
| 404 |
# Create device object and append it to list |
| 405 |
num = len(self.devices) |
| 406 |
self.devices.append(CDEmuDevice(num, self.daemon_proxy)) |
| 407 |
self.emit("device-added", num) |
| 408 |
|
| 409 |
def on_device_removed(self): |
| 410 |
# Remove from list |
| 411 |
self.devices.pop() |
| 412 |
self.emit("device-removed", len(self.devices)) |
| 413 |
|
| 414 |
def add_device(self): |
| 415 |
try: |
| 416 |
self.daemon_proxy.AddDevice() |
| 417 |
except GLib.Error as e: |
| 418 |
self.emit( |
| 419 |
"error", |
| 420 |
_("Failed to add device"), |
| 421 |
_("Failed to add new device. Error:\n%s") % (e), |
| 422 |
) |
| 423 |
|
| 424 |
def remove_device(self): |
| 425 |
try: |
| 426 |
self.daemon_proxy.RemoveDevice() |
| 427 |
except GLib.Error as e: |
| 428 |
self.emit( |
| 429 |
"error", |
| 430 |
_("Failed to remove device"), |
| 431 |
_("Failed to remove device. Error:\n%s") % (e), |
| 432 |
) |
| 433 |
|
| 434 |
|
| 435 |
GObject.type_register(CDEmu) |
| 436 |
|
| 437 |
GObject.signal_new( |
| 438 |
"error", |
| 439 |
CDEmu, |
| 440 |
GObject.SignalFlags.RUN_FIRST, |
| 441 |
None, |
| 442 |
(GObject.TYPE_STRING, GObject.TYPE_STRING, ), |
| 443 |
) |
| 444 |
|
| 445 |
GObject.signal_new( |
| 446 |
"daemon-started", |
| 447 |
CDEmu, |
| 448 |
GObject.SignalFlags.RUN_FIRST, |
| 449 |
None, |
| 450 |
(), |
| 451 |
) |
| 452 |
|
| 453 |
GObject.signal_new( |
| 454 |
"daemon-stopped", |
| 455 |
CDEmu, |
| 456 |
GObject.SignalFlags.RUN_FIRST, |
| 457 |
None, |
| 458 |
(), |
| 459 |
) |
| 460 |
|
| 461 |
GObject.signal_new( |
| 462 |
"connection-established", |
| 463 |
CDEmu, |
| 464 |
GObject.SignalFlags.RUN_FIRST, |
| 465 |
None, |
| 466 |
(GObject.TYPE_INT, ), |
| 467 |
) |
| 468 |
|
| 469 |
GObject.signal_new( |
| 470 |
"connection-lost", |
| 471 |
CDEmu, |
| 472 |
GObject.SignalFlags.RUN_FIRST, |
| 473 |
None, |
| 474 |
(), |
| 475 |
) |
| 476 |
|
| 477 |
GObject.signal_new( |
| 478 |
"device-added", |
| 479 |
CDEmu, |
| 480 |
GObject.SignalFlags.RUN_FIRST, |
| 481 |
None, |
| 482 |
(GObject.TYPE_INT, ), |
| 483 |
) |
| 484 |
|
| 485 |
GObject.signal_new( |
| 486 |
"device-removed", |
| 487 |
CDEmu, |
| 488 |
GObject.SignalFlags.RUN_FIRST, |
| 489 |
None, |
| 490 |
(GObject.TYPE_INT, ), |
| 491 |
) |
| 492 |
|
| 493 |
|
| 494 |
######################################################################## |
| 495 |
# CDEmuDevice: device representation # |
| 496 |
######################################################################## |
| 497 |
class CDEmuNeedPassword(Exception): |
| 498 |
pass |
| 499 |
|
| 500 |
|
| 501 |
class CDEmuDevice(GObject.GObject): |
| 502 |
def __init__(self, device_number, proxy): |
| 503 |
GObject.GObject.__init__(self) |
| 504 |
|
| 505 |
# Device number |
| 506 |
self.number = device_number |
| 507 |
|
| 508 |
# Daemon proxy |
| 509 |
self.proxy = proxy |
| 510 |
|
| 511 |
# Device status |
| 512 |
self.loaded, self.filenames = self.proxy.DeviceGetStatus(self.number) |
| 513 |
|
| 514 |
# Device mapping |
| 515 |
self.sr_path, self.sg_path = self.proxy.DeviceGetMapping(self.number) |
| 516 |
|
| 517 |
# Get device options |
| 518 |
self.dpm_emulation = self.proxy.DeviceGetOption(self.number, "dpm-emulation") |
| 519 |
self.tr_emulation = self.proxy.DeviceGetOption(self.number, "tr-emulation") |
| 520 |
self.bad_sector_emulation = self.proxy.DeviceGetOption(self.number, "bad-sector-emulation") |
| 521 |
self.dvd_report_css = self.proxy.DeviceGetOption(self.number, "dvd-report-css") |
| 522 |
self.device_id = self.proxy.DeviceGetOption(self.number, "device-id") |
| 523 |
self.daemon_debug_mask = self.proxy.DeviceGetOption(self.number, "daemon-debug-mask") |
| 524 |
self.library_debug_mask = self.proxy.DeviceGetOption(self.number, "library-debug-mask") |
| 525 |
|
| 526 |
# Note: we connect the following using weakref, otherwise we get circular references |
| 527 |
# and devices never get destroyed |
| 528 |
self.proxy.connect( |
| 529 |
"DeviceStatusChanged", |
| 530 |
lambda p, d, obj=weakref.ref(self): obj().on_status_changed(d) if obj() else None, |
| 531 |
) |
| 532 |
self.proxy.connect( |
| 533 |
"DeviceOptionChanged", |
| 534 |
lambda p, d, o, obj=weakref.ref(self): obj().on_option_changed(d, o) if obj() else None, |
| 535 |
) |
| 536 |
self.proxy.connect( |
| 537 |
"DeviceMappingReady", |
| 538 |
lambda p, d, obj=weakref.ref(self): obj().on_mapping_ready(d) if obj() else None, |
| 539 |
) |
| 540 |
|
| 541 |
def on_status_changed(self, device_number): |
| 542 |
if device_number != self.number: |
| 543 |
return |
| 544 |
|
| 545 |
# We may fail to retrieve status when a device was removed |
| 546 |
try: |
| 547 |
self.loaded, self.filenames = self.proxy.DeviceGetStatus(self.number) |
| 548 |
self.emit("status-changed") |
| 549 |
except Exception: |
| 550 |
pass |
| 551 |
|
| 552 |
def on_option_changed(self, device_number, option): |
| 553 |
if device_number != self.number: |
| 554 |
return |
| 555 |
|
| 556 |
if option == "device-id": |
| 557 |
self.device_id = self.proxy.DeviceGetOption(self.number, "device-id") |
| 558 |
self.emit("device-id-changed") |
| 559 |
elif option == "dpm-emulation": |
| 560 |
self.dpm_emulation = self.proxy.DeviceGetOption(self.number, "dpm-emulation") |
| 561 |
self.emit("dpm-emulation-changed") |
| 562 |
elif option == "tr-emulation": |
| 563 |
self.tr_emulation = self.proxy.DeviceGetOption(self.number, "tr-emulation") |
| 564 |
self.emit("tr-emulation-changed") |
| 565 |
elif option == "bad-sector-emulation": |
| 566 |
self.bad_sector_emulation = self.proxy.DeviceGetOption(self.number, "bad-sector-emulation") |
| 567 |
self.emit("bad-sector-emulation-changed") |
| 568 |
elif option == "dvd-report-css": |
| 569 |
self.dvd_report_css = self.proxy.DeviceGetOption(self.number, "dvd-report-css") |
| 570 |
self.emit("dvd-report-css-changed") |
| 571 |
elif option == "daemon-debug-mask": |
| 572 |
self.daemon_debug_mask = self.proxy.DeviceGetOption(self.number, "daemon-debug-mask") |
| 573 |
self.emit("daemon-debug-mask-changed") |
| 574 |
elif option == "library-debug-mask": |
| 575 |
self.library_debug_mask = self.proxy.DeviceGetOption(self.number, "library-debug-mask") |
| 576 |
self.emit("library-debug-mask-changed") |
| 577 |
else: |
| 578 |
print(f"Unknown option: {option}!") |
| 579 |
|
| 580 |
def on_mapping_ready(self, device_number): |
| 581 |
if device_number != self.number: |
| 582 |
return |
| 583 |
|
| 584 |
# Check if mappings really changed |
| 585 |
changed = False |
| 586 |
sr_path, sg_path = self.proxy.DeviceGetMapping(self.number) |
| 587 |
|
| 588 |
if sr_path != self.sr_path: |
| 589 |
self.sr_path = sr_path |
| 590 |
changed = True |
| 591 |
|
| 592 |
if sg_path != self.sg_path: |
| 593 |
self.sg_path = sg_path |
| 594 |
changed = True |
| 595 |
|
| 596 |
if changed: |
| 597 |
self.emit("mapping-changed") |
| 598 |
|
| 599 |
def unload_device(self): |
| 600 |
try: |
| 601 |
self.proxy.DeviceUnload(self.number) |
| 602 |
except GLib.Error as e: |
| 603 |
self.emit("error", _("Failed to unload device #%02d:\n%s") % (self.number, e)) |
| 604 |
|
| 605 |
def load_device(self, filenames, params): |
| 606 |
try: |
| 607 |
self.proxy.DeviceLoad(self.number, filenames, params) |
| 608 |
except GLib.Error as e: |
| 609 |
if "net.sf.cdemu.CDEmuDaemon.errorMirage.EncryptedImage" in str(e): |
| 610 |
# Need password, raise proper exception... |
| 611 |
raise CDEmuNeedPassword() |
| 612 |
else: |
| 613 |
self.emit( |
| 614 |
"error", |
| 615 |
_("Failed to load image %s to device #%02d:\n%s") % |
| 616 |
(";".join(filenames), self.number, e), |
| 617 |
) |
| 618 |
|
| 619 |
def create_blank_disc(self, filename, params): |
| 620 |
try: |
| 621 |
self.proxy.DeviceCreateBlank(self.number, filename, params) |
| 622 |
except GLib.Error as e: |
| 623 |
self.emit( |
| 624 |
"error", |
| 625 |
_("Failed to create blank disc on device #%02d:\n%s") % |
| 626 |
(self.number, e), |
| 627 |
) |
| 628 |
|
| 629 |
def set_device_id(self, value): |
| 630 |
if self.device_id == value: |
| 631 |
return |
| 632 |
|
| 633 |
try: |
| 634 |
self.proxy.DeviceSetOption(self.number, "device-id", GLib.Variant('(ssss)', value)) |
| 635 |
except GLib.Error as e: |
| 636 |
self.emit( |
| 637 |
"error", |
| 638 |
_("Failed to set device ID for device #%02d to %s:\n%s") % |
| 639 |
(self.number, value, e), |
| 640 |
) |
| 641 |
|
| 642 |
def set_dpm_emulation(self, value): |
| 643 |
if self.dpm_emulation == value: |
| 644 |
return |
| 645 |
|
| 646 |
try: |
| 647 |
self.proxy.DeviceSetOption(self.number, "dpm-emulation", GLib.Variant('b', value)) |
| 648 |
except GLib.Error as e: |
| 649 |
self.emit( |
| 650 |
"error", |
| 651 |
_("Failed to set DPM emulation for device #%02d to %i:\n%s") % |
| 652 |
(self.number, value, e), |
| 653 |
) |
| 654 |
|
| 655 |
def set_tr_emulation(self, value): |
| 656 |
if self.tr_emulation == value: |
| 657 |
return |
| 658 |
|
| 659 |
try: |
| 660 |
self.proxy.DeviceSetOption(self.number, "tr-emulation", GLib.Variant('b', value)) |
| 661 |
except GLib.Error as e: |
| 662 |
self.emit( |
| 663 |
"error", |
| 664 |
_("Failed to set TR emulation for device #%02d to %i:\n%s") % |
| 665 |
(self.number, value, e), |
| 666 |
) |
| 667 |
|
| 668 |
def set_bad_sector_emulation(self, value): |
| 669 |
if self.bad_sector_emulation == value: |
| 670 |
return |
| 671 |
|
| 672 |
try: |
| 673 |
self.proxy.DeviceSetOption(self.number, "bad-sector-emulation", GLib.Variant('b', value)) |
| 674 |
except GLib.Error as e: |
| 675 |
self.emit( |
| 676 |
"error", |
| 677 |
_("Failed to set bad sector emulation for device #%02d to %i:\n%s") % |
| 678 |
(self.number, value, e), |
| 679 |
) |
| 680 |
|
| 681 |
def set_dvd_report_css(self, value): |
| 682 |
if self.dvd_report_css == value: |
| 683 |
return |
| 684 |
|
| 685 |
try: |
| 686 |
self.proxy.DeviceSetOption(self.number, "dvd-report-css", GLib.Variant('b', value)) |
| 687 |
except GLib.Error as e: |
| 688 |
self.emit( |
| 689 |
"error", |
| 690 |
_("Failed to set DVD report CSS/CPPM for device #%02d to %i:\n%s") % |
| 691 |
(self.number, value, e), |
| 692 |
) |
| 693 |
|
| 694 |
def set_daemon_debug_mask(self, value): |
| 695 |
if self.daemon_debug_mask == value: |
| 696 |
return |
| 697 |
|
| 698 |
try: |
| 699 |
self.proxy.DeviceSetOption(self.number, "daemon-debug-mask", GLib.Variant('i', value)) |
| 700 |
except GLib.Error as e: |
| 701 |
self.emit( |
| 702 |
"error", |
| 703 |
_("Failed to set daemon debug mask for device #%02d to 0x%X:\n%s") % |
| 704 |
(self.number, value, e), |
| 705 |
) |
| 706 |
|
| 707 |
def set_library_debug_mask(self, value): |
| 708 |
if self.library_debug_mask == value: |
| 709 |
return |
| 710 |
|
| 711 |
try: |
| 712 |
self.proxy.DeviceSetOption(self.number, "library-debug-mask", GLib.Variant('i', value)) |
| 713 |
except GLib.Error as e: |
| 714 |
self.emit( |
| 715 |
"error", |
| 716 |
_("Failed to set library debug mask for device #%02d to 0x%X:\n%s") % |
| 717 |
(self.number, value, e), |
| 718 |
) |
| 719 |
|
| 720 |
|
| 721 |
GObject.type_register(CDEmuDevice) |
| 722 |
|
| 723 |
GObject.signal_new( |
| 724 |
"mapping-changed", |
| 725 |
CDEmuDevice, |
| 726 |
GObject.SignalFlags.RUN_FIRST, |
| 727 |
None, |
| 728 |
(), |
| 729 |
) |
| 730 |
|
| 731 |
GObject.signal_new( |
| 732 |
"status-changed", |
| 733 |
CDEmuDevice, |
| 734 |
GObject.SignalFlags.RUN_FIRST, |
| 735 |
None, |
| 736 |
(), |
| 737 |
) |
| 738 |
|
| 739 |
GObject.signal_new( |
| 740 |
"device-id-changed", |
| 741 |
CDEmuDevice, |
| 742 |
GObject.SignalFlags.RUN_FIRST, |
| 743 |
None, |
| 744 |
(), |
| 745 |
) |
| 746 |
|
| 747 |
GObject.signal_new( |
| 748 |
"dpm-emulation-changed", |
| 749 |
CDEmuDevice, |
| 750 |
GObject.SignalFlags.RUN_FIRST, |
| 751 |
None, |
| 752 |
(), |
| 753 |
) |
| 754 |
|
| 755 |
GObject.signal_new( |
| 756 |
"tr-emulation-changed", |
| 757 |
CDEmuDevice, |
| 758 |
GObject.SignalFlags.RUN_FIRST, |
| 759 |
None, |
| 760 |
(), |
| 761 |
) |
| 762 |
|
| 763 |
GObject.signal_new( |
| 764 |
"bad-sector-emulation-changed", |
| 765 |
CDEmuDevice, |
| 766 |
GObject.SignalFlags.RUN_FIRST, |
| 767 |
None, |
| 768 |
(), |
| 769 |
) |
| 770 |
|
| 771 |
GObject.signal_new( |
| 772 |
"dvd-report-css-changed", |
| 773 |
CDEmuDevice, |
| 774 |
GObject.SignalFlags.RUN_FIRST, |
| 775 |
None, |
| 776 |
(), |
| 777 |
) |
| 778 |
|
| 779 |
GObject.signal_new( |
| 780 |
"daemon-debug-mask-changed", |
| 781 |
CDEmuDevice, |
| 782 |
GObject.SignalFlags.RUN_FIRST, |
| 783 |
None, |
| 784 |
(), |
| 785 |
) |
| 786 |
|
| 787 |
GObject.signal_new( |
| 788 |
"library-debug-mask-changed", |
| 789 |
CDEmuDevice, |
| 790 |
GObject.SignalFlags.RUN_FIRST, |
| 791 |
None, |
| 792 |
(), |
| 793 |
) |
| 794 |
|
| 795 |
GObject.signal_new( |
| 796 |
"error", |
| 797 |
CDEmuDevice, |
| 798 |
GObject.SignalFlags.RUN_FIRST, |
| 799 |
None, |
| 800 |
(GObject.TYPE_STRING, ), |
| 801 |
) |
| 802 |
|
| 803 |
|
| 804 |
######################################################################## |
| 805 |
# gCDEmuDeviceStatusPage: device status page # |
| 806 |
######################################################################## |
| 807 |
class gCDEmuDeviceStatusPage(Gtk.Grid): |
| 808 |
def __init__(self): |
| 809 |
super().__init__() |
| 810 |
self.set_orientation(Gtk.Orientation.VERTICAL) |
| 811 |
self.set_row_spacing(5) |
| 812 |
self.set_column_spacing(5) |
| 813 |
|
| 814 |
# *** Frame: status *** |
| 815 |
frame = Gtk.Frame.new(_("Status")) |
| 816 |
frame.set_label_align(0.50, 0.50) |
| 817 |
frame.set_border_width(2) |
| 818 |
self.add(frame) |
| 819 |
|
| 820 |
grid = Gtk.Grid.new() |
| 821 |
grid.set_border_width(5) |
| 822 |
grid.set_column_spacing(5) |
| 823 |
grid.set_row_spacing(5) |
| 824 |
|
| 825 |
frame.add(grid) |
| 826 |
|
| 827 |
# Loaded |
| 828 |
label = Gtk.Label.new(_("Loaded: ")) |
| 829 |
label.set_hexpand(True) |
| 830 |
grid.attach(label, 0, 0, 1, 1) |
| 831 |
|
| 832 |
label2 = Gtk.Label.new() |
| 833 |
label2.set_hexpand(True) |
| 834 |
grid.attach_next_to(label2, label, Gtk.PositionType.RIGHT, 1, 1) |
| 835 |
self.label_loaded = label2 |
| 836 |
|
| 837 |
# Filename(s) |
| 838 |
label = Gtk.Label.new(_("File name(s): ")) |
| 839 |
label.set_hexpand(True) |
| 840 |
grid.attach(label, 0, 1, 1, 1) |
| 841 |
|
| 842 |
label2 = Gtk.Label.new() |
| 843 |
label2.set_justify(Gtk.Justification.CENTER) |
| 844 |
label2.set_hexpand(True) |
| 845 |
grid.attach_next_to(label2, label, Gtk.PositionType.RIGHT, 1, 1) |
| 846 |
self.label_filename = label2 |
| 847 |
|
| 848 |
# Separator |
| 849 |
separator = Gtk.Separator.new(Gtk.Orientation.HORIZONTAL) |
| 850 |
separator.set_hexpand(True) |
| 851 |
grid.attach(separator, 0, 2, 2, 1) |
| 852 |
|
| 853 |
# Load/create blank buttons |
| 854 |
button = Gtk.Button.new_with_label(_("Load")) |
| 855 |
button.set_hexpand(True) |
| 856 |
grid.attach(button, 0, 3, 1, 1) |
| 857 |
button.connect("clicked", lambda b: self.emit("load-unload-device")) |
| 858 |
self.button_load = button |
| 859 |
|
| 860 |
button = Gtk.Button.new_with_label(_("Create blank")) |
| 861 |
button.set_hexpand(True) |
| 862 |
grid.attach_next_to(button, self.button_load, Gtk.PositionType.RIGHT, 1, 1) |
| 863 |
button.connect("clicked", lambda b: self.emit("create-blank-disc")) |
| 864 |
self.button_create_blank = button |
| 865 |
|
| 866 |
# *** Frame: mapping *** |
| 867 |
frame = Gtk.Frame.new(_("Device mapping")) |
| 868 |
frame.set_label_align(0.50, 0.50) |
| 869 |
frame.set_border_width(2) |
| 870 |
self.add(frame) |
| 871 |
|
| 872 |
grid = Gtk.Grid.new() |
| 873 |
frame.add(grid) |
| 874 |
grid.set_border_width(5) |
| 875 |
grid.set_column_spacing(5) |
| 876 |
grid.set_row_spacing(2) |
| 877 |
|
| 878 |
# SCSI CD-ROM device |
| 879 |
label = Gtk.Label.new(_("SCSI CD-ROM device: ")) |
| 880 |
label.set_hexpand(True) |
| 881 |
grid.attach(label, 0, 0, 1, 1) |
| 882 |
|
| 883 |
label2 = Gtk.Label.new() |
| 884 |
label2.set_hexpand(True) |
| 885 |
grid.attach_next_to(label2, label, Gtk.PositionType.RIGHT, 1, 1) |
| 886 |
self.label_dev_sr = label2 |
| 887 |
|
| 888 |
# SCSI generic device |
| 889 |
label = Gtk.Label.new(_("SCSI generic device: ")) |
| 890 |
label.set_hexpand(True) |
| 891 |
grid.attach(label, 0, 1, 1, 1) |
| 892 |
|
| 893 |
label2 = Gtk.Label.new() |
| 894 |
grid.attach_next_to(label2, label, Gtk.PositionType.RIGHT, 1, 1) |
| 895 |
self.label_dev_sg = label2 |
| 896 |
|
| 897 |
# "Remove device" button |
| 898 |
# Center horizontally, put 5 pixels above the bottom (and make |
| 899 |
# sure it expands vertically) |
| 900 |
button = Gtk.Button.new_with_label(_("Remove device")) |
| 901 |
self.add(button) |
| 902 |
button.set_vexpand(True) |
| 903 |
button.set_valign(Gtk.Align.END) |
| 904 |
button.set_halign(Gtk.Align.CENTER) |
| 905 |
button.set_margin_bottom(5) |
| 906 |
button.connect("clicked", lambda b: self.emit("remove-device")) |
| 907 |
self.button_remove = button |
| 908 |
|
| 909 |
self.show_all() |
| 910 |
|
| 911 |
def set_device_mapping(self, sg_path, sr_path): |
| 912 |
self.label_dev_sr.set_text(sr_path) |
| 913 |
self.label_dev_sg.set_text(sg_path) |
| 914 |
|
| 915 |
def set_status(self, loaded, filenames): |
| 916 |
if loaded: |
| 917 |
text = "\n".join(os.path.basename(filename) for filename in filenames) |
| 918 |
|
| 919 |
self.label_loaded.set_label(_("Yes")) |
| 920 |
self.label_filename.set_label(text) |
| 921 |
self.button_load.set_label(_("Unload")) |
| 922 |
|
| 923 |
self.button_create_blank.set_sensitive(False) |
| 924 |
else: |
| 925 |
self.label_loaded.set_label(_("No")) |
| 926 |
self.label_filename.set_label("") |
| 927 |
self.button_load.set_label(_("Load")) |
| 928 |
|
| 929 |
self.button_create_blank.set_sensitive(True) |
| 930 |
|
| 931 |
def set_removable(self, value): |
| 932 |
self.button_remove.set_sensitive(value) |
| 933 |
|
| 934 |
|
| 935 |
GObject.type_register(gCDEmuDeviceStatusPage) |
| 936 |
|
| 937 |
GObject.signal_new( |
| 938 |
"load-unload-device", |
| 939 |
gCDEmuDeviceStatusPage, |
| 940 |
GObject.SignalFlags.RUN_FIRST, |
| 941 |
None, |
| 942 |
(), |
| 943 |
) |
| 944 |
|
| 945 |
GObject.signal_new( |
| 946 |
"create-blank-disc", |
| 947 |
gCDEmuDeviceStatusPage, |
| 948 |
GObject.SignalFlags.RUN_FIRST, |
| 949 |
None, |
| 950 |
(), |
| 951 |
) |
| 952 |
|
| 953 |
GObject.signal_new( |
| 954 |
"remove-device", |
| 955 |
gCDEmuDeviceStatusPage, |
| 956 |
GObject.SignalFlags.RUN_FIRST, |
| 957 |
None, |
| 958 |
(), |
| 959 |
) |
| 960 |
|
| 961 |
|
| 962 |
######################################################################## |
| 963 |
# gCDEmuDeviceOptionsPage: device options page # |
| 964 |
######################################################################## |
| 965 |
class gCDEmuDeviceOptionsPage(Gtk.Grid): |
| 966 |
def __init__(self): |
| 967 |
super().__init__() |
| 968 |
self.set_orientation(Gtk.Orientation.VERTICAL) |
| 969 |
self.set_row_spacing(5) |
| 970 |
|
| 971 |
# *** Device ID *** |
| 972 |
# Device ID |
| 973 |
frame = Gtk.Frame.new(_("Device ID")) |
| 974 |
frame.set_label_align(0.50, 0.50) |
| 975 |
frame.set_border_width(2) |
| 976 |
self.add(frame) |
| 977 |
|
| 978 |
grid = Gtk.Grid.new() |
| 979 |
grid.set_border_width(5) |
| 980 |
grid.set_column_spacing(5) |
| 981 |
grid.set_row_spacing(5) |
| 982 |
|
| 983 |
frame.add(grid) |
| 984 |
|
| 985 |
# Vendor ID |
| 986 |
label = Gtk.Label.new(_("Vendor ID: ")) |
| 987 |
label.set_hexpand(True) |
| 988 |
grid.attach(label, 0, 0, 1, 1) |
| 989 |
|
| 990 |
entry = Gtk.Entry.new() |
| 991 |
entry.set_max_length(8) |
| 992 |
entry.set_hexpand(True) |
| 993 |
grid.attach_next_to(entry, label, Gtk.PositionType.RIGHT, 1, 1) |
| 994 |
self.entry_vendor_id = entry |
| 995 |
|
| 996 |
# Product ID |
| 997 |
label = Gtk.Label.new(_("Product ID: ")) |
| 998 |
label.set_hexpand(True) |
| 999 |
grid.attach(label, 0, 1, 1, 1) |
| 1000 |
|
| 1001 |
entry = Gtk.Entry.new() |
| 1002 |
entry.set_max_length(16) |
| 1003 |
entry.set_hexpand(True) |
| 1004 |
grid.attach_next_to(entry, label, Gtk.PositionType.RIGHT, 1, 1) |
| 1005 |
self.entry_product_id = entry |
| 1006 |
|
| 1007 |
# Revision |
| 1008 |
label = Gtk.Label.new(_("Revision: ")) |
| 1009 |
label.set_hexpand(True) |
| 1010 |
grid.attach(label, 0, 2, 1, 1) |
| 1011 |
|
| 1012 |
entry = Gtk.Entry.new() |
| 1013 |
entry.set_max_length(4) |
| 1014 |
entry.set_hexpand(True) |
| 1015 |
grid.attach_next_to(entry, label, Gtk.PositionType.RIGHT, 1, 1) |
| 1016 |
self.entry_revision = entry |
| 1017 |
|
| 1018 |
# Vendor-specific |
| 1019 |
label = Gtk.Label.new(_("Vendor-specific: ")) |
| 1020 |
label.set_hexpand(True) |
| 1021 |
grid.attach(label, 0, 3, 1, 1) |
| 1022 |
|
| 1023 |
entry = Gtk.Entry.new() |
| 1024 |
entry.set_max_length(20) |
| 1025 |
entry.set_hexpand(True) |
| 1026 |
grid.attach_next_to(entry, label, Gtk.PositionType.RIGHT, 1, 1) |
| 1027 |
self.entry_vendor_specific = entry |
| 1028 |
|
| 1029 |
# Separator |
| 1030 |
separator = Gtk.Separator.new(Gtk.Orientation.HORIZONTAL) |
| 1031 |
grid.attach(separator, 0, 4, 2, 1) |
| 1032 |
|
| 1033 |
# Button |
| 1034 |
button = Gtk.Button.new_with_label(_("Set device ID")) |
| 1035 |
button.set_hexpand(True) |
| 1036 |
grid.attach(button, 0, 5, 2, 1) |
| 1037 |
button.connect("clicked", self.on_set_device_id_clicked) |
| 1038 |
|
| 1039 |
# *** DPM emulation *** |
| 1040 |
checkbutton = Gtk.CheckButton.new_with_label(_("DPM emulation")) |
| 1041 |
checkbutton.connect("toggled", lambda t: self.emit("dpm-emulation-changed", t.get_active())) |
| 1042 |
self.add(checkbutton) |
| 1043 |
self.checkbutton_dpm = checkbutton |
| 1044 |
|
| 1045 |
# *** Transfer rate emulation *** |
| 1046 |
checkbutton = Gtk.CheckButton.new_with_label(_("Transfer rate emulation")) |
| 1047 |
checkbutton.connect("toggled", lambda t: self.emit("tr-emulation-changed", t.get_active())) |
| 1048 |
self.add(checkbutton) |
| 1049 |
self.checkbutton_tr = checkbutton |
| 1050 |
|
| 1051 |
# *** Bad sector emulation *** |
| 1052 |
checkbutton = Gtk.CheckButton.new_with_label(_("Bad sector emulation")) |
| 1053 |
checkbutton.connect("toggled", lambda t: self.emit("bad-sector-emulation-changed", t.get_active())) |
| 1054 |
self.add(checkbutton) |
| 1055 |
self.checkbutton_bad_sector = checkbutton |
| 1056 |
|
| 1057 |
# *** DVD report CSS *** |
| 1058 |
checkbutton = Gtk.CheckButton.new_with_label(_("DVD report CSS/CPPM")) |
| 1059 |
checkbutton.connect("toggled", lambda t: self.emit("dvd-report-css-changed", t.get_active())) |
| 1060 |
self.add(checkbutton) |
| 1061 |
self.checkbutton_dvd_report_css = checkbutton |
| 1062 |
|
| 1063 |
self.show_all() |
| 1064 |
|
| 1065 |
def set_device_id(self, device_id): |
| 1066 |
self.entry_vendor_id.set_text(device_id[0]) |
| 1067 |
self.entry_product_id.set_text(device_id[1]) |
| 1068 |
self.entry_revision.set_text(device_id[2]) |
| 1069 |
self.entry_vendor_specific.set_text(device_id[3]) |
| 1070 |
|
| 1071 |
def on_set_device_id_clicked(self, button): |
| 1072 |
identity = ( |
| 1073 |
self.entry_vendor_id.get_text(), |
| 1074 |
self.entry_product_id.get_text(), |
| 1075 |
self.entry_revision.get_text(), |
| 1076 |
self.entry_vendor_specific.get_text() |
| 1077 |
) |
| 1078 |
self.emit("device-id-changed", identity) |
| 1079 |
|
| 1080 |
def set_dpm_emulation(self, value): |
| 1081 |
self.checkbutton_dpm.set_active(value) |
| 1082 |
|
| 1083 |
def set_tr_emulation(self, value): |
| 1084 |
self.checkbutton_tr.set_active(value) |
| 1085 |
|
| 1086 |
def set_bad_sector_emulation(self, value): |
| 1087 |
self.checkbutton_bad_sector.set_active(value) |
| 1088 |
|
| 1089 |
def set_dvd_report_css(self, value): |
| 1090 |
self.checkbutton_dvd_report_css.set_active(value) |
| 1091 |
|
| 1092 |
|
| 1093 |
GObject.type_register(gCDEmuDeviceOptionsPage) |
| 1094 |
|
| 1095 |
GObject.signal_new( |
| 1096 |
"device-id-changed", |
| 1097 |
gCDEmuDeviceOptionsPage, |
| 1098 |
GObject.SignalFlags.RUN_FIRST, |
| 1099 |
None, |
| 1100 |
(GObject.TYPE_PYOBJECT, ), |
| 1101 |
) |
| 1102 |
|
| 1103 |
GObject.signal_new( |
| 1104 |
"dpm-emulation-changed", |
| 1105 |
gCDEmuDeviceOptionsPage, |
| 1106 |
GObject.SignalFlags.RUN_FIRST, |
| 1107 |
None, |
| 1108 |
(GObject.TYPE_BOOLEAN, ), |
| 1109 |
) |
| 1110 |
|
| 1111 |
GObject.signal_new( |
| 1112 |
"tr-emulation-changed", |
| 1113 |
gCDEmuDeviceOptionsPage, |
| 1114 |
GObject.SignalFlags.RUN_FIRST, |
| 1115 |
None, |
| 1116 |
(GObject.TYPE_BOOLEAN, ), |
| 1117 |
) |
| 1118 |
|
| 1119 |
GObject.signal_new( |
| 1120 |
"bad-sector-emulation-changed", |
| 1121 |
gCDEmuDeviceOptionsPage, |
| 1122 |
GObject.SignalFlags.RUN_FIRST, |
| 1123 |
None, |
| 1124 |
(GObject.TYPE_BOOLEAN, ), |
| 1125 |
) |
| 1126 |
|
| 1127 |
GObject.signal_new( |
| 1128 |
"dvd-report-css-changed", |
| 1129 |
gCDEmuDeviceOptionsPage, |
| 1130 |
GObject.SignalFlags.RUN_FIRST, |
| 1131 |
None, |
| 1132 |
(GObject.TYPE_BOOLEAN, ), |
| 1133 |
) |
| 1134 |
|
| 1135 |
|
| 1136 |
######################################################################## |
| 1137 |
# gCDEmuDeviceDebugMaskPage: debug mask page # |
| 1138 |
######################################################################## |
| 1139 |
class gCDEmuDeviceDebugMaskPage(Gtk.Frame): |
| 1140 |
def __init__(self, name, masks_list): |
| 1141 |
super().__init__() |
| 1142 |
self.set_label_align(0.50, 0.50) |
| 1143 |
|
| 1144 |
self.set_label(name) |
| 1145 |
self.entries = [] |
| 1146 |
|
| 1147 |
grid = Gtk.Grid.new() |
| 1148 |
grid.set_orientation(Gtk.Orientation.VERTICAL) |
| 1149 |
grid.set_border_width(5) |
| 1150 |
grid.set_row_spacing(2) |
| 1151 |
self.add(grid) |
| 1152 |
|
| 1153 |
# Create checkboxes |
| 1154 |
for mask in masks_list: |
| 1155 |
checkbutton = Gtk.CheckButton.new_with_label(mask[0]) |
| 1156 |
checkbutton.weight = mask[1] |
| 1157 |
checkbutton.set_hexpand(True) |
| 1158 |
grid.add(checkbutton) |
| 1159 |
|
| 1160 |
self.entries.append(checkbutton) |
| 1161 |
|
| 1162 |
# Separator |
| 1163 |
separator = Gtk.Separator.new(Gtk.Orientation.HORIZONTAL) |
| 1164 |
separator.set_hexpand(True) |
| 1165 |
separator.set_property("margin-bottom", 5) |
| 1166 |
separator.set_property("margin-top", 5) |
| 1167 |
grid.add(separator) |
| 1168 |
|
| 1169 |
# Button |
| 1170 |
button = Gtk.Button.new_with_label(_("Set debug mask")) |
| 1171 |
button.set_hexpand(True) |
| 1172 |
grid.add(button) |
| 1173 |
button.connect("clicked", self.on_set_debug_mask_clicked) |
| 1174 |
|
| 1175 |
self.show_all() |
| 1176 |
|
| 1177 |
def set_debug_mask(self, value): |
| 1178 |
for entry in self.entries: |
| 1179 |
entry.set_active(value & entry.weight) |
| 1180 |
|
| 1181 |
def on_set_debug_mask_clicked(self, button): |
| 1182 |
# Get value |
| 1183 |
value = 0 |
| 1184 |
for entry in self.entries: |
| 1185 |
value |= entry.weight * entry.get_active() |
| 1186 |
|
| 1187 |
self.emit("debug-mask-changed", value) |
| 1188 |
|
| 1189 |
|
| 1190 |
GObject.type_register(gCDEmuDeviceDebugMaskPage) |
| 1191 |
|
| 1192 |
GObject.signal_new( |
| 1193 |
"debug-mask-changed", |
| 1194 |
gCDEmuDeviceDebugMaskPage, |
| 1195 |
GObject.SignalFlags.RUN_FIRST, |
| 1196 |
None, |
| 1197 |
(GObject.TYPE_UINT, ), |
| 1198 |
) |
| 1199 |
|
| 1200 |
|
| 1201 |
######################################################################## |
| 1202 |
# gCDEMuFileChooserDialog: file chooser dialog # |
| 1203 |
######################################################################## |
| 1204 |
class gCDEMuFileChooserDialog(Gtk.FileChooserDialog): |
| 1205 |
def __init__(self, parsers, filter_streams): |
| 1206 |
super().__init__(title=_("Open file"), action=Gtk.FileChooserAction.OPEN) |
| 1207 |
self.add_buttons( |
| 1208 |
_("Cancel"), Gtk.ResponseType.CANCEL, |
| 1209 |
_("Open"), Gtk.ResponseType.ACCEPT, |
| 1210 |
) |
| 1211 |
|
| 1212 |
self.set_select_multiple(True) |
| 1213 |
self.set_local_only(False) |
| 1214 |
|
| 1215 |
# Set filters based on supported parser information |
| 1216 |
filter_entry = Gtk.FileFilter.new() |
| 1217 |
filter_entry.set_name(_("All files")) |
| 1218 |
filter_entry.add_pattern("*") |
| 1219 |
self.add_filter(filter_entry) |
| 1220 |
|
| 1221 |
all_images = Gtk.FileFilter.new() |
| 1222 |
all_images.set_name(_("All image files")) |
| 1223 |
self.add_filter(all_images) |
| 1224 |
|
| 1225 |
for info in parsers: |
| 1226 |
for description, mime_type in info[2]: |
| 1227 |
filter_entry = Gtk.FileFilter.new() |
| 1228 |
|
| 1229 |
filter_entry.set_name(description) |
| 1230 |
filter_entry.add_mime_type(mime_type) |
| 1231 |
|
| 1232 |
all_images.add_mime_type(mime_type) |
| 1233 |
|
| 1234 |
self.add_filter(filter_entry) |
| 1235 |
|
| 1236 |
for info in filter_streams: |
| 1237 |
for description, mime_type in info[3]: |
| 1238 |
filter_entry = Gtk.FileFilter.new() |
| 1239 |
|
| 1240 |
filter_entry.set_name(description) |
| 1241 |
filter_entry.add_mime_type(mime_type) |
| 1242 |
|
| 1243 |
all_images.add_mime_type(mime_type) |
| 1244 |
|
| 1245 |
self.add_filter(filter_entry) |
| 1246 |
|
| 1247 |
# Extra options |
| 1248 |
widget = self.create_extra_options_widget() |
| 1249 |
self.set_extra_widget(widget) |
| 1250 |
|
| 1251 |
def run(self): |
| 1252 |
# Clear parameters |
| 1253 |
self.combobox_encoding.set_active(0) |
| 1254 |
self.entry_password.set_text("") |
| 1255 |
|
| 1256 |
# Run the dialog |
| 1257 |
return super().run() |
| 1258 |
|
| 1259 |
def get_parameters(self): |
| 1260 |
parameters = {} |
| 1261 |
|
| 1262 |
# Encoding |
| 1263 |
i = self.combobox_encoding.get_active_iter() |
| 1264 |
encoding = self.combobox_encoding.get_model().get_value(i, 1) |
| 1265 |
if encoding: |
| 1266 |
parameters["encoding"] = GLib.Variant("s", encoding) |
| 1267 |
|
| 1268 |
# Password |
| 1269 |
password = self.entry_password.get_text() |
| 1270 |
if password: |
| 1271 |
parameters["password"] = GLib.Variant("s", password) |
| 1272 |
|
| 1273 |
return parameters |
| 1274 |
|
| 1275 |
def create_extra_options_widget(self): |
| 1276 |
# Expander with grid |
| 1277 |
expander = Gtk.Expander.new(_("Extra Options")) |
| 1278 |
|
| 1279 |
grid = Gtk.Grid.new() |
| 1280 |
grid.set_column_spacing(5) |
| 1281 |
grid.set_row_spacing(5) |
| 1282 |
expander.add(grid) |
| 1283 |
|
| 1284 |
# *** Encoding *** |
| 1285 |
# Label |
| 1286 |
label = Gtk.Label.new(_("Encoding: ")) |
| 1287 |
grid.attach(label, 0, 0, 1, 1) |
| 1288 |
|
| 1289 |
# Combo box |
| 1290 |
liststore = self.get_encodings_list() |
| 1291 |
combobox = Gtk.ComboBox.new_with_model_and_entry(liststore) |
| 1292 |
combobox.set_entry_text_column(0) |
| 1293 |
grid.attach_next_to(combobox, label, Gtk.PositionType.RIGHT, 1, 1) |
| 1294 |
self.combobox_encoding = combobox |
| 1295 |
|
| 1296 |
# Autocompletion |
| 1297 |
completion = Gtk.EntryCompletion.new() |
| 1298 |
completion.set_model(liststore) |
| 1299 |
completion.set_text_column(0) |
| 1300 |
combobox.get_child().set_completion(completion) |
| 1301 |
|
| 1302 |
# *** Password *** |
| 1303 |
# Label |
| 1304 |
label = Gtk.Label.new(_("Password: ")) |
| 1305 |
grid.attach(label, 0, 1, 1, 1) |
| 1306 |
|
| 1307 |
# Entry |
| 1308 |
entry = Gtk.Entry.new() |
| 1309 |
entry.set_visibility(False) |
| 1310 |
grid.attach_next_to(entry, label, Gtk.PositionType.RIGHT, 1, 1) |
| 1311 |
self.entry_password = entry |
| 1312 |
|
| 1313 |
expander.show_all() |
| 1314 |
|
| 1315 |
return expander |
| 1316 |
|
| 1317 |
def get_encodings_list(self): |
| 1318 |
# Get list of encodings supported by iconv (since that is what |
| 1319 |
# GLib and consequently CDEmu/libMirage uses) |
| 1320 |
encodings = [] |
| 1321 |
try: |
| 1322 |
iconv_lines = subprocess.run( |
| 1323 |
['iconv', '--list'], |
| 1324 |
env={'LANG': 'C'}, |
| 1325 |
stdout=subprocess.PIPE, |
| 1326 |
stdin=subprocess.DEVNULL, |
| 1327 |
stderr=subprocess.DEVNULL, |
| 1328 |
text=True, |
| 1329 |
check=True, |
| 1330 |
).stdout |
| 1331 |
encodings = [ |
| 1332 |
line.strip('/').lower() |
| 1333 |
for line in iconv_lines.splitlines() |
| 1334 |
] |
| 1335 |
except Exception: |
| 1336 |
# Probably due to iconv missing; not a big deal, we just won't |
| 1337 |
# display encodings list |
| 1338 |
pass |
| 1339 |
|
| 1340 |
# Create ListStore |
| 1341 |
liststore = Gtk.ListStore.new([GObject.TYPE_STRING, GObject.TYPE_STRING]) |
| 1342 |
liststore.append(("Autodetect", "")) |
| 1343 |
for encoding in encodings: |
| 1344 |
liststore.append((encoding, encoding)) |
| 1345 |
|
| 1346 |
return liststore |
| 1347 |
|
| 1348 |
|
| 1349 |
######################################################################## |
| 1350 |
# gCDEMuPasswordDialog: password dialog # |
| 1351 |
######################################################################## |
| 1352 |
class gCDEMuPasswordDialog(Gtk.Dialog): |
| 1353 |
def __init__(self, *args, **kwargs): |
| 1354 |
super().__init__(*args, **kwargs) |
| 1355 |
self.set_title(_("Enter password")) |
| 1356 |
self.add_buttons( |
| 1357 |
_("OK"), Gtk.ResponseType.OK, |
| 1358 |
_("Cancel"), Gtk.ResponseType.REJECT, |
| 1359 |
) |
| 1360 |
self.set_default_response(Gtk.ResponseType.OK) |
| 1361 |
|
| 1362 |
# Grid |
| 1363 |
grid = Gtk.Grid.new() |
| 1364 |
grid.set_border_width(5) |
| 1365 |
grid.set_row_spacing(5) |
| 1366 |
grid.set_column_spacing(5) |
| 1367 |
self.vbox.add(grid) |
| 1368 |
|
| 1369 |
# Label |
| 1370 |
label = Gtk.Label.new(_("The image you are trying to load is encrypted.")) |
| 1371 |
grid.attach(label, 0, 0, 2, 1) |
| 1372 |
|
| 1373 |
# Label & text entry |
| 1374 |
label = Gtk.Label.new(_("Password: ")) |
| 1375 |
grid.attach(label, 0, 1, 1, 1) |
| 1376 |
|
| 1377 |
entry = Gtk.Entry.new() |
| 1378 |
entry.set_hexpand(True) |
| 1379 |
grid.attach_next_to(entry, label, Gtk.PositionType.RIGHT, 1, 1) |
| 1380 |
entry.set_visibility(False) |
| 1381 |
entry.set_activates_default(True) # Activate default action (= accept) on ENTER key |
| 1382 |
self.entry = entry |
| 1383 |
|
| 1384 |
self.vbox.show_all() |
| 1385 |
|
| 1386 |
def get_password(self): |
| 1387 |
return self.entry.get_text() |
| 1388 |
|
| 1389 |
def run(self): |
| 1390 |
# Clear the password field |
| 1391 |
self.entry.set_text("") |
| 1392 |
|
| 1393 |
# Chain to parent |
| 1394 |
return super().run() |
| 1395 |
|
| 1396 |
|
| 1397 |
######################################################################## |
| 1398 |
# gCDEMuCreateBlankImageDialog: blank disc image creation dialog # |
| 1399 |
######################################################################## |
| 1400 |
class gCDEMuCreateBlankImageDialog(Gtk.Dialog): |
| 1401 |
class ValidationError(Exception): |
| 1402 |
pass |
| 1403 |
|
| 1404 |
def __init__(self, writers): |
| 1405 |
super().__init__() |
| 1406 |
self.set_title(_("Create blank disc image")) |
| 1407 |
self.add_buttons( |
| 1408 |
_("OK"), Gtk.ResponseType.ACCEPT, |
| 1409 |
_("Cancel"), Gtk.ResponseType.REJECT, |
| 1410 |
) |
| 1411 |
|
| 1412 |
self.writers = writers |
| 1413 |
|
| 1414 |
# Frame: image settings |
| 1415 |
frame = Gtk.Frame.new(_("Image")) |
| 1416 |
frame.set_label_align(0.50, 0.50) |
| 1417 |
frame.set_border_width(2) |
| 1418 |
self.vbox.add(frame) |
| 1419 |
|
| 1420 |
grid = Gtk.Grid.new() |
| 1421 |
grid.set_border_width(5) |
| 1422 |
grid.set_column_spacing(5) |
| 1423 |
grid.set_row_spacing(5) |
| 1424 |
frame.add(grid) |
| 1425 |
|
| 1426 |
# Filename |
| 1427 |
label = Gtk.Label.new(_("Filename: ")) |
| 1428 |
grid.attach(label, 0, 0, 1, 1) |
| 1429 |
|
| 1430 |
entry = Gtk.Entry.new() |
| 1431 |
entry.set_hexpand(True) |
| 1432 |
grid.attach_next_to(entry, label, Gtk.PositionType.RIGHT, 1, 1) |
| 1433 |
self.entry_filename = entry |
| 1434 |
|
| 1435 |
button = Gtk.Button.new_with_label(_("Choose")) |
| 1436 |
grid.attach_next_to(button, entry, Gtk.PositionType.RIGHT, 1, 1) |
| 1437 |
button.connect("clicked", lambda b: self.choose_file()) |
| 1438 |
|
| 1439 |
# Medium type |
| 1440 |
label = Gtk.Label.new(_("Medium type: ")) |
| 1441 |
grid.attach(label, 0, 1, 1, 1) |
| 1442 |
|
| 1443 |
liststore = Gtk.ListStore.new([GObject.TYPE_STRING, GObject.TYPE_STRING]) |
| 1444 |
liststore.append(("CD-R 74 min (650 MB)", "cdr74")) |
| 1445 |
liststore.append(("CD-R 80 min (700 MB)", "cdr80")) |
| 1446 |
liststore.append(("CD-R 90 min (800 MB)", "cdr90")) |
| 1447 |
liststore.append(("CD-R 99 min (900 MB)", "cdr99")) |
| 1448 |
liststore.append(("DVD+R SL", "dvd+r")) |
| 1449 |
liststore.append(("BD-R SL", "bdr")) |
| 1450 |
|
| 1451 |
combobox = Gtk.ComboBox.new_with_model_and_entry(liststore) |
| 1452 |
combobox.set_entry_text_column(0) |
| 1453 |
combobox.set_active(0) |
| 1454 |
combobox.set_hexpand(True) |
| 1455 |
grid.attach_next_to(combobox, label, Gtk.PositionType.RIGHT, 2, 1) |
| 1456 |
self.combobox_medium = combobox |
| 1457 |
|
| 1458 |
completion = Gtk.EntryCompletion.new() |
| 1459 |
completion.set_model(liststore) |
| 1460 |
completion.set_text_column(0) |
| 1461 |
combobox.get_child().set_completion(completion) |
| 1462 |
|
| 1463 |
# Writer |
| 1464 |
label = Gtk.Label.new(_("Writer: ")) |
| 1465 |
grid.attach(label, 0, 2, 1, 1) |
| 1466 |
|
| 1467 |
liststore = Gtk.ListStore.new([GObject.TYPE_STRING, GObject.TYPE_INT]) |
| 1468 |
for i, writer in enumerate(self.writers): |
| 1469 |
liststore.append((writer[0], i)) |
| 1470 |
|
| 1471 |
combobox = Gtk.ComboBox.new_with_model_and_entry(liststore) |
| 1472 |
combobox.set_entry_text_column(0) |
| 1473 |
combobox.set_hexpand(True) |
| 1474 |
grid.attach_next_to(combobox, label, Gtk.PositionType.RIGHT, 2, 1) |
| 1475 |
combobox.connect("changed", lambda c: self.build_writer_options()) |
| 1476 |
self.combobox_writer = combobox |
| 1477 |
|
| 1478 |
completion = Gtk.EntryCompletion.new() |
| 1479 |
completion.set_model(liststore) |
| 1480 |
completion.set_text_column(0) |
| 1481 |
combobox.get_child().set_completion(completion) |
| 1482 |
|
| 1483 |
# Frame: writer options |
| 1484 |
frame = Gtk.Frame.new(_("Writer options")) |
| 1485 |
frame.set_label_align(0.50, 0.50) |
| 1486 |
frame.set_border_width(2) |
| 1487 |
self.vbox.add(frame) |
| 1488 |
|
| 1489 |
self.frame_writer = frame |
| 1490 |
|
| 1491 |
self.writer_options_ui = None |
| 1492 |
self.writer_options_widgets = None |
| 1493 |
|
| 1494 |
self.vbox.show_all() |
| 1495 |
self.frame_writer.hide() |
| 1496 |
|
| 1497 |
def choose_file(self): |
| 1498 |
dialog = Gtk.FileChooserDialog( |
| 1499 |
title=_("Choose file"), |
| 1500 |
parent=self, |
| 1501 |
action=Gtk.FileChooserAction.SAVE, |
| 1502 |
) |
| 1503 |
dialog.add_buttons( |
| 1504 |
_("Cancel"), Gtk.ResponseType.CANCEL, |
| 1505 |
_("Choose"), Gtk.ResponseType.ACCEPT, |
| 1506 |
) |
| 1507 |
dialog.set_local_only(False) |
| 1508 |
dialog.set_do_overwrite_confirmation(True) |
| 1509 |
|
| 1510 |
if dialog.run() == Gtk.ResponseType.ACCEPT: |
| 1511 |
self.entry_filename.set_text(dialog.get_filename()) |
| 1512 |
|
| 1513 |
dialog.destroy() |
| 1514 |
|
| 1515 |
def build_writer_options(self): |
| 1516 |
# Destroy old GUI and widgets map |
| 1517 |
if self.writer_options_ui is not None: |
| 1518 |
self.writer_options_ui.destroy() |
| 1519 |
self.writer_options_ui = None |
| 1520 |
|
| 1521 |
if self.writer_options_widgets is not None: |
| 1522 |
self.writer_options_widgets = None |
| 1523 |
|
| 1524 |
# Hide Writer frame |
| 1525 |
self.frame_writer.hide() |
| 1526 |
|
| 1527 |
# Get selected writer |
| 1528 |
i = self.combobox_writer.get_active_iter() |
| 1529 |
if i is None: |
| 1530 |
return |
| 1531 |
writer_idx = self.combobox_writer.get_model().get_value(i, 1) |
| 1532 |
writer = self.writers[writer_idx] |
| 1533 |
|
| 1534 |
parameter_sheet = writer[2] |
| 1535 |
|
| 1536 |
# Create GUI |
| 1537 |
grid = Gtk.Grid.new() |
| 1538 |
grid.set_border_width(5) |
| 1539 |
grid.set_column_spacing(5) |
| 1540 |
grid.set_row_spacing(5) |
| 1541 |
self.frame_writer.add(grid) |
| 1542 |
|
| 1543 |
self.writer_options_ui = grid # Store so we can destroy it |
| 1544 |
self.writer_options_widgets = {} # ID -> widget map |
| 1545 |
|
| 1546 |
row = 0 |
| 1547 |
|
| 1548 |
for parameter_entry in parameter_sheet: |
| 1549 |
parameter_id = parameter_entry[0] |
| 1550 |
parameter_name = parameter_entry[1] |
| 1551 |
parameter_description = parameter_entry[2] |
| 1552 |
parameter_default = parameter_entry[3] |
| 1553 |
parameter_enum = parameter_entry[4] |
| 1554 |
|
| 1555 |
needs_label = True |
| 1556 |
|
| 1557 |
if len(parameter_enum): |
| 1558 |
# Enum; create a combo box |
| 1559 |
# Store |
| 1560 |
liststore = Gtk.ListStore.new([GObject.TYPE_STRING, GObject.TYPE_STRING]) |
| 1561 |
for enum_value in parameter_enum: |
| 1562 |
liststore.append((enum_value, enum_value)) |
| 1563 |
|
| 1564 |
# Combo box |
| 1565 |
widget = Gtk.ComboBox.new_with_model_and_entry(liststore) |
| 1566 |
widget.set_entry_text_column(0) |
| 1567 |
|
| 1568 |
completion = Gtk.EntryCompletion.new() |
| 1569 |
completion.set_model(liststore) |
| 1570 |
completion.set_text_column(0) |
| 1571 |
widget.get_child().set_completion(completion) |
| 1572 |
|
| 1573 |
# Default value |
| 1574 |
for entry in liststore: |
| 1575 |
if entry[1] == parameter_default: |
| 1576 |
widget.set_active_iter(entry.iter) |
| 1577 |
break |
| 1578 |
elif isinstance(parameter_default, str): |
| 1579 |
# String; create a text entry |
| 1580 |
widget = Gtk.Entry.new() |
| 1581 |
# Default value |
| 1582 |
widget.set_text(parameter_default) |
| 1583 |
elif isinstance(parameter_default, bool): |
| 1584 |
# Boolean; create a check button |
| 1585 |
widget = Gtk.CheckButton.new_with_label(parameter_name) |
| 1586 |
widget.set_tooltip_text(parameter_description) |
| 1587 |
needs_label = False |
| 1588 |
# Default value |
| 1589 |
widget.set_active(parameter_default) |
| 1590 |
elif isinstance(parameter_default, int): |
| 1591 |
# Integer; create a pin button |
| 1592 |
widget = Gtk.SpinButton.new() |
| 1593 |
# Default value |
| 1594 |
widget.set_value(parameter_default) |
| 1595 |
else: |
| 1596 |
continue |
| 1597 |
|
| 1598 |
if needs_label: |
| 1599 |
label = Gtk.Label.new(f"{parameter_name:s}: ") |
| 1600 |
label.set_tooltip_text(parameter_description) |
| 1601 |
grid.attach(label, 0, row, 1, 1) |
| 1602 |
|
| 1603 |
widget.set_hexpand(True) |
| 1604 |
grid.attach_next_to(widget, label, Gtk.PositionType.RIGHT, 1, 1) |
| 1605 |
else: |
| 1606 |
grid.attach(widget, 0, row, 2, 1) |
| 1607 |
|
| 1608 |
self.writer_options_widgets[parameter_id] = widget |
| 1609 |
|
| 1610 |
row += 1 |
| 1611 |
|
| 1612 |
# Show Writer frame |
| 1613 |
self.frame_writer.show_all() |
| 1614 |
|
| 1615 |
def get_filename(self): |
| 1616 |
return self.entry_filename.get_text() |
| 1617 |
|
| 1618 |
def get_writer_parameters(self): |
| 1619 |
parameters = {} |
| 1620 |
|
| 1621 |
# Writer ID |
| 1622 |
i = self.combobox_writer.get_active_iter() |
| 1623 |
writer_idx = self.combobox_writer.get_model().get_value(i, 1) |
| 1624 |
parameters["writer-id"] = GLib.Variant("s", self.writers[writer_idx][0]) |
| 1625 |
|
| 1626 |
# Medium type |
| 1627 |
i = self.combobox_medium.get_active_iter() |
| 1628 |
parameters["medium-type"] = GLib.Variant("s", self.combobox_medium.get_model().get_value(i, 1)) |
| 1629 |
|
| 1630 |
# Writer parameters |
| 1631 |
# NOTE: we do not need to wrap items() in list(), because we do |
| 1632 |
# not modify the source dictionary during iteration |
| 1633 |
for parameter_id, widget in self.writer_options_widgets.items(): |
| 1634 |
if isinstance(widget, Gtk.Entry): |
| 1635 |
# Text entry |
| 1636 |
value = GLib.Variant("s", widget.get_text()) |
| 1637 |
elif isinstance(widget, Gtk.SpinButton): |
| 1638 |
# Spin button |
| 1639 |
value = GLib.Variant("i", widget.get_value()) |
| 1640 |
elif isinstance(widget, Gtk.CheckButton): |
| 1641 |
# Check button |
| 1642 |
value = GLib.Variant("b", widget.get_active()) |
| 1643 |
elif isinstance(widget, Gtk.ComboBox): |
| 1644 |
# Combo box |
| 1645 |
i = widget.get_active_iter() |
| 1646 |
value = GLib.Variant("s", widget.get_model().get_value(i, 1)) |
| 1647 |
else: |
| 1648 |
continue |
| 1649 |
|
| 1650 |
parameters[parameter_id] = value |
| 1651 |
|
| 1652 |
return parameters |
| 1653 |
|
| 1654 |
def run(self): |
| 1655 |
# Run in loop and validate when user clicks OK |
| 1656 |
while True: |
| 1657 |
# Chain to parent |
| 1658 |
result = super().run() |
| 1659 |
if result == Gtk.ResponseType.ACCEPT: |
| 1660 |
# Validate |
| 1661 |
try: |
| 1662 |
# Image filename must be set |
| 1663 |
if self.entry_filename.get_text() == "": |
| 1664 |
raise self.ValidationError(_("Image filename/basename not set!")) |
| 1665 |
# Writer must be chosen |
| 1666 |
if self.combobox_writer.get_active_iter() is None: |
| 1667 |
raise self.ValidationError(_("No image writer is chosen!")) |
| 1668 |
break |
| 1669 |
except self.ValidationError as e: |
| 1670 |
# Show error dialog |
| 1671 |
message = Gtk.MessageDialog( |
| 1672 |
parent=self, |
| 1673 |
message_type=Gtk.MessageType.ERROR, |
| 1674 |
buttons=Gtk.ButtonsType.CLOSE, |
| 1675 |
text=e, |
| 1676 |
) |
| 1677 |
message.set_title(_("Invalid option")) |
| 1678 |
message.run() |
| 1679 |
message.destroy() |
| 1680 |
else: |
| 1681 |
break |
| 1682 |
|
| 1683 |
return result |
| 1684 |
|
| 1685 |
|
| 1686 |
######################################################################## |
| 1687 |
# gCDEmuDevice # |
| 1688 |
######################################################################## |
| 1689 |
class gCDEmuDevice(GObject.GObject): |
| 1690 |
def connect_signal(self, obj, sig, callback): |
| 1691 |
signal = obj.connect(sig, callback) |
| 1692 |
self.signals.append((obj, signal)) |
| 1693 |
|
| 1694 |
def __init__(self, cdemu, device_number): |
| 1695 |
super().__init__() |
| 1696 |
|
| 1697 |
self.signals = [] |
| 1698 |
|
| 1699 |
self.number = device_number |
| 1700 |
self.device = cdemu.devices[self.number] |
| 1701 |
|
| 1702 |
# Create menu item |
| 1703 |
self.menu_item = Gtk.MenuItem() |
| 1704 |
self.menu_item.show() |
| 1705 |
# Note: signal for menu item is connected in the child class, |
| 1706 |
# because Gtk and AppIndicator versions handle different signals |
| 1707 |
|
| 1708 |
# Create file chooser dialog |
| 1709 |
self.file_chooser = gCDEMuFileChooserDialog( |
| 1710 |
cdemu.supported_parsers, |
| 1711 |
cdemu.supported_filter_streams, |
| 1712 |
) |
| 1713 |
|
| 1714 |
# Create blank image creation dialog |
| 1715 |
self.blank_disc_creation_dialog = gCDEMuCreateBlankImageDialog( |
| 1716 |
cdemu.supported_writers, |
| 1717 |
) |
| 1718 |
|
| 1719 |
# Create password dialog |
| 1720 |
self.password_dialog = gCDEMuPasswordDialog() |
| 1721 |
|
| 1722 |
# *** GUI *** |
| 1723 |
# Create dialog |
| 1724 |
self.dialog = Gtk.Dialog() |
| 1725 |
self.dialog.set_title(_("Device #%02d: properties") % self.number) |
| 1726 |
self.dialog.add_buttons(_("Close"), Gtk.ResponseType.CLOSE) |
| 1727 |
self.dialog.set_border_width(5) |
| 1728 |
self.dialog.vbox.set_border_width(5) |
| 1729 |
# Hide the dialog when closed |
| 1730 |
self.connect_signal( |
| 1731 |
self.dialog, |
| 1732 |
"delete-event", |
| 1733 |
lambda w, e: w.hide() or True, |
| 1734 |
) |
| 1735 |
self.connect_signal( |
| 1736 |
self.dialog, |
| 1737 |
"response", |
| 1738 |
lambda w, e: w.hide() or True, |
| 1739 |
) |
| 1740 |
|
| 1741 |
# Label |
| 1742 |
label = Gtk.Label(label="<b><big>" + (_("Device #%02d") % (self.number)) + "</big></b>") |
| 1743 |
label.show() |
| 1744 |
label.set_use_markup(True) |
| 1745 |
self.dialog.vbox.add(label) |
| 1746 |
|
| 1747 |
# Notebook |
| 1748 |
notebook = Gtk.Notebook() |
| 1749 |
notebook.show() |
| 1750 |
notebook.set_tab_pos(Gtk.PositionType.LEFT) |
| 1751 |
notebook.set_border_width(5) |
| 1752 |
self.dialog.vbox.add(notebook) |
| 1753 |
|
| 1754 |
# Page: status |
| 1755 |
self.page_status = gCDEmuDeviceStatusPage() |
| 1756 |
self.page_status.set_device_mapping(self.device.sg_path, self.device.sr_path) |
| 1757 |
notebook.append_page(self.page_status, Gtk.Label(label=_("Status"))) |
| 1758 |
|
| 1759 |
self.connect_signal( |
| 1760 |
self.page_status, |
| 1761 |
"load-unload-device", |
| 1762 |
lambda w: self.load_unload_device(), |
| 1763 |
) |
| 1764 |
self.connect_signal( |
| 1765 |
self.page_status, |
| 1766 |
"create-blank-disc", |
| 1767 |
lambda w: self.create_blank_disc(), |
| 1768 |
) |
| 1769 |
self.connect_signal( |
| 1770 |
self.page_status, |
| 1771 |
"remove-device", |
| 1772 |
lambda w: cdemu.remove_device(), |
| 1773 |
) |
| 1774 |
|
| 1775 |
# Page: options |
| 1776 |
self.page_options = gCDEmuDeviceOptionsPage() |
| 1777 |
notebook.append_page(self.page_options, Gtk.Label(label=_("Options"))) |
| 1778 |
self.connect_signal( |
| 1779 |
self.page_options, |
| 1780 |
"device-id-changed", |
| 1781 |
lambda w, id: self.device.set_device_id(id), |
| 1782 |
) |
| 1783 |
self.connect_signal( |
| 1784 |
self.page_options, |
| 1785 |
"dpm-emulation-changed", |
| 1786 |
lambda w, v: self.device.set_dpm_emulation(v), |
| 1787 |
) |
| 1788 |
self.connect_signal( |
| 1789 |
self.page_options, |
| 1790 |
"tr-emulation-changed", |
| 1791 |
lambda w, v: self.device.set_tr_emulation(v), |
| 1792 |
) |
| 1793 |
self.connect_signal( |
| 1794 |
self.page_options, |
| 1795 |
"bad-sector-emulation-changed", |
| 1796 |
lambda w, v: self.device.set_bad_sector_emulation(v), |
| 1797 |
) |
| 1798 |
self.connect_signal( |
| 1799 |
self.page_options, |
| 1800 |
"dvd-report-css-changed", |
| 1801 |
lambda w, v: self.device.set_dvd_report_css(v), |
| 1802 |
) |
| 1803 |
|
| 1804 |
# Page: daemon debug mask |
| 1805 |
self.page_daemon = gCDEmuDeviceDebugMaskPage(_("Daemon debug mask"), cdemu.daemon_debug_masks) |
| 1806 |
notebook.append_page(self.page_daemon, Gtk.Label(label=_("Daemon"))) |
| 1807 |
self.connect_signal( |
| 1808 |
self.page_daemon, |
| 1809 |
"debug-mask-changed", |
| 1810 |
lambda w, v: self.device.set_daemon_debug_mask(v), |
| 1811 |
) |
| 1812 |
|
| 1813 |
# Page: library debug mask |
| 1814 |
self.page_library = gCDEmuDeviceDebugMaskPage(_("Library debug mask"), cdemu.library_debug_masks) |
| 1815 |
notebook.append_page(self.page_library, Gtk.Label(label=_("Library"))) |
| 1816 |
self.connect_signal( |
| 1817 |
self.page_library, |
| 1818 |
"debug-mask-changed", |
| 1819 |
lambda w, v: self.device.set_library_debug_mask(v), |
| 1820 |
) |
| 1821 |
|
| 1822 |
# Connect device signals |
| 1823 |
self.connect_signal( |
| 1824 |
self.device, |
| 1825 |
"mapping-changed", |
| 1826 |
lambda w: self.page_status.set_device_mapping(self.device.sg_path, self.device.sr_path), |
| 1827 |
) |
| 1828 |
self.connect_signal( |
| 1829 |
self.device, |
| 1830 |
"status-changed", |
| 1831 |
lambda w: self.update_status(True), |
| 1832 |
) |
| 1833 |
self.connect_signal( |
| 1834 |
self.device, |
| 1835 |
"device-id-changed", |
| 1836 |
lambda w: self.update_device_id(True), |
| 1837 |
) |
| 1838 |
self.connect_signal( |
| 1839 |
self.device, |
| 1840 |
"dpm-emulation-changed", |
| 1841 |
lambda w: self.update_dpm_emulation(True), |
| 1842 |
) |
| 1843 |
self.connect_signal( |
| 1844 |
self.device, |
| 1845 |
"tr-emulation-changed", |
| 1846 |
lambda w: self.update_tr_emulation(True), |
| 1847 |
) |
| 1848 |
self.connect_signal( |
| 1849 |
self.device, |
| 1850 |
"bad-sector-emulation-changed", |
| 1851 |
lambda w: self.update_bad_sector_emulation(True), |
| 1852 |
) |
| 1853 |
self.connect_signal( |
| 1854 |
self.device, |
| 1855 |
"dvd-report-css-changed", |
| 1856 |
lambda w: self.update_dvd_report_css(True), |
| 1857 |
) |
| 1858 |
self.connect_signal( |
| 1859 |
self.device, |
| 1860 |
"daemon-debug-mask-changed", |
| 1861 |
lambda w: self.update_daemon_debug_mask(True), |
| 1862 |
) |
| 1863 |
self.connect_signal( |
| 1864 |
self.device, |
| 1865 |
"library-debug-mask-changed", |
| 1866 |
lambda w: self.update_library_debug_mask(True), |
| 1867 |
) |
| 1868 |
self.connect_signal( |
| 1869 |
self.device, |
| 1870 |
"error", |
| 1871 |
lambda w, t: self.show_error(t), |
| 1872 |
) |
| 1873 |
|
| 1874 |
# Some pages require refresh when they are shown... |
| 1875 |
self.connect_signal( |
| 1876 |
notebook, |
| 1877 |
"switch-page", |
| 1878 |
lambda n, p, pp: self.refresh_pages(), |
| 1879 |
) |
| 1880 |
self.connect_signal( |
| 1881 |
self.dialog, |
| 1882 |
"show", |
| 1883 |
lambda w: self.refresh_pages(), |
| 1884 |
) |
| 1885 |
|
| 1886 |
# Manually perform the initial update... |
| 1887 |
self.update_status(False) |
| 1888 |
self.update_device_id(False) |
| 1889 |
self.update_dpm_emulation(False) |
| 1890 |
self.update_tr_emulation(False) |
| 1891 |
self.update_bad_sector_emulation(False) |
| 1892 |
self.update_dvd_report_css(False) |
| 1893 |
self.update_daemon_debug_mask(False) |
| 1894 |
self.update_library_debug_mask(False) |
| 1895 |
|
| 1896 |
def cleanup(self): |
| 1897 |
# Release reference to device |
| 1898 |
self.device = None |
| 1899 |
|
| 1900 |
# Cleanup signal handlers |
| 1901 |
for obj, handler in self.signals: |
| 1902 |
obj.disconnect(handler) |
| 1903 |
|
| 1904 |
# Explicitly destroy dialogs, because they might still be running |
| 1905 |
self.dialog.destroy() |
| 1906 |
self.file_chooser.destroy() |
| 1907 |
self.menu_item.destroy() |
| 1908 |
self.password_dialog.destroy() |
| 1909 |
|
| 1910 |
def update_status(self, notify): |
| 1911 |
# Menu item |
| 1912 |
if self.device.loaded: |
| 1913 |
images = os.path.basename(self.device.filenames[0]) # Make it short |
| 1914 |
if len(self.device.filenames) > 1: |
| 1915 |
images += ", ..." # Indicate there's more than one file |
| 1916 |
self.menu_item.set_label("%s #%02d: %s" % (_("Device"), self.device.number, images)) |
| 1917 |
else: |
| 1918 |
self.menu_item.set_label("%s #%02d: %s" % (_("Device"), self.device.number, _("Empty"))) |
| 1919 |
|
| 1920 |
# Status page in the device dialog |
| 1921 |
self.page_status.set_status(self.device.loaded, self.device.filenames) |
| 1922 |
|
| 1923 |
# Notification |
| 1924 |
if notify: |
| 1925 |
if self.device.loaded: |
| 1926 |
self.emit( |
| 1927 |
"device-notification", |
| 1928 |
_("Device status change"), |
| 1929 |
_("Device #%02d has been loaded.") % (self.device.number), |
| 1930 |
) |
| 1931 |
else: |
| 1932 |
self.emit( |
| 1933 |
"device-notification", |
| 1934 |
_("Device status change"), |
| 1935 |
_("Device #%02d has been emptied.") % (self.device.number), |
| 1936 |
) |
| 1937 |
|
| 1938 |
def update_device_id(self, notify): |
| 1939 |
self.page_options.set_device_id(self.device.device_id) |
| 1940 |
if notify: |
| 1941 |
self.emit( |
| 1942 |
"device-notification", |
| 1943 |
_("Device option change"), |
| 1944 |
_("Device #%02d has had its device ID changed:\n Vendor ID: '%s'\n Product ID: '%s'\n Revision: '%s'\n Vendor-specific: '%s'") % # noqa: E501 |
| 1945 |
( |
| 1946 |
self.device.number, |
| 1947 |
self.device.device_id[0], |
| 1948 |
self.device.device_id[1], |
| 1949 |
self.device.device_id[2], |
| 1950 |
self.device.device_id[3], |
| 1951 |
), |
| 1952 |
) |
| 1953 |
|
| 1954 |
def update_dpm_emulation(self, notify): |
| 1955 |
self.page_options.set_dpm_emulation(self.device.dpm_emulation) |
| 1956 |
if notify: |
| 1957 |
self.emit( |
| 1958 |
"device-notification", |
| 1959 |
_("Device option change"), |
| 1960 |
_("Device #%02d has had its DPM emulation option changed. New value: %s") % |
| 1961 |
(self.device.number, self.device.dpm_emulation), |
| 1962 |
) |
| 1963 |
|
| 1964 |
def update_tr_emulation(self, notify): |
| 1965 |
self.page_options.set_tr_emulation(self.device.tr_emulation) |
| 1966 |
if notify: |
| 1967 |
self.emit( |
| 1968 |
"device-notification", |
| 1969 |
_("Device option change"), |
| 1970 |
_("Device #%02d has had its TR emulation option changed. New value: %s") % |
| 1971 |
(self.device.number, self.device.tr_emulation), |
| 1972 |
) |
| 1973 |
|
| 1974 |
def update_bad_sector_emulation(self, notify): |
| 1975 |
self.page_options.set_bad_sector_emulation(self.device.bad_sector_emulation) |
| 1976 |
if notify: |
| 1977 |
self.emit( |
| 1978 |
"device-notification", |
| 1979 |
_("Device option change"), |
| 1980 |
_("Device #%02d has had its bad sector emulation option changed. New value: %s") % |
| 1981 |
(self.device.number, self.device.bad_sector_emulation), |
| 1982 |
) |
| 1983 |
|
| 1984 |
def update_dvd_report_css(self, notify): |
| 1985 |
self.page_options.set_dvd_report_css(self.device.dvd_report_css) |
| 1986 |
if notify: |
| 1987 |
self.emit( |
| 1988 |
"device-notification", |
| 1989 |
_("Device option change"), |
| 1990 |
_("Device #%02d has had its DVD report CSS/CPPM option changed. New value: %s") % |
| 1991 |
(self.device.number, self.device.dvd_report_css), |
| 1992 |
) |
| 1993 |
|
| 1994 |
def update_daemon_debug_mask(self, notify): |
| 1995 |
self.page_daemon.set_debug_mask(self.device.daemon_debug_mask) |
| 1996 |
if notify: |
| 1997 |
self.emit( |
| 1998 |
"device-notification", |
| 1999 |
_("Device option change"), |
| 2000 |
_("Device #%02d has had its daemon debug mask changed. New value: 0x%X") % |
| 2001 |
(self.device.number, self.device.daemon_debug_mask), |
| 2002 |
) |
| 2003 |
|
| 2004 |
def update_library_debug_mask(self, notify): |
| 2005 |
self.page_library.set_debug_mask(self.device.library_debug_mask) |
| 2006 |
if notify: |
| 2007 |
self.emit( |
| 2008 |
"device-notification", |
| 2009 |
_("Device option change"), |
| 2010 |
_("Device #%02d has had its library debug mask changed. New value: 0x%X") % |
| 2011 |
(self.device.number, self.device.library_debug_mask), |
| 2012 |
) |
| 2013 |
|
| 2014 |
def load_unload_device(self): |
| 2015 |
if self.device.loaded: |
| 2016 |
self.device.unload_device() |
| 2017 |
else: |
| 2018 |
result = self.file_chooser.run() |
| 2019 |
self.file_chooser.hide() |
| 2020 |
if result == Gtk.ResponseType.ACCEPT: |
| 2021 |
# Get filename and parameters |
| 2022 |
filenames = self.file_chooser.get_filenames() |
| 2023 |
parameters = self.file_chooser.get_parameters() |
| 2024 |
|
| 2025 |
# Try loading |
| 2026 |
try: |
| 2027 |
self.device.load_device(filenames, parameters) |
| 2028 |
except CDEmuNeedPassword: |
| 2029 |
# Get password |
| 2030 |
result = self.password_dialog.run() |
| 2031 |
self.password_dialog.hide() |
| 2032 |
if result == Gtk.ResponseType.OK: |
| 2033 |
password = self.password_dialog.get_password() |
| 2034 |
# Try loading with overriden password |
| 2035 |
parameters["password"] = GLib.Variant("s", password) |
| 2036 |
self.device.load_device(filenames, parameters) |
| 2037 |
|
| 2038 |
def create_blank_disc(self): |
| 2039 |
result = self.blank_disc_creation_dialog.run() |
| 2040 |
self.blank_disc_creation_dialog.hide() |
| 2041 |
if result == Gtk.ResponseType.ACCEPT: |
| 2042 |
filename = self.blank_disc_creation_dialog.get_filename() |
| 2043 |
writer_parameters = self.blank_disc_creation_dialog.get_writer_parameters() |
| 2044 |
self.device.create_blank_disc(filename, writer_parameters) |
| 2045 |
|
| 2046 |
def refresh_pages(self): |
| 2047 |
# Some pages have fields that can be altered without applying the |
| 2048 |
# change. We want to reset those changes when a page is changed |
| 2049 |
# or when the dialog is shown... |
| 2050 |
self.update_device_id(False) |
| 2051 |
self.update_daemon_debug_mask(False) |
| 2052 |
self.update_library_debug_mask(False) |
| 2053 |
|
| 2054 |
def show_error(self, text): |
| 2055 |
# Show error dialog |
| 2056 |
message = Gtk.MessageDialog( |
| 2057 |
parent=None, |
| 2058 |
message_type=Gtk.MessageType.ERROR, |
| 2059 |
buttons=Gtk.ButtonsType.CLOSE, |
| 2060 |
text=text, |
| 2061 |
) |
| 2062 |
message.set_title(_("Device error")) |
| 2063 |
message.run() |
| 2064 |
message.destroy() |
| 2065 |
|
| 2066 |
def set_removable(self, value): |
| 2067 |
# Forward to status page |
| 2068 |
self.page_status.set_removable(value) |
| 2069 |
|
| 2070 |
|
| 2071 |
GObject.type_register(gCDEmuDevice) |
| 2072 |
|
| 2073 |
GObject.signal_new( |
| 2074 |
"device-notification", |
| 2075 |
gCDEmuDevice, |
| 2076 |
GObject.SignalFlags.RUN_FIRST, |
| 2077 |
None, |
| 2078 |
(GObject.TYPE_STRING, GObject.TYPE_STRING, ), |
| 2079 |
) |
| 2080 |
|
| 2081 |
|
| 2082 |
class gCDEmuDevice_Gtk(gCDEmuDevice): |
| 2083 |
def __init__(self, cdemu, device_number): |
| 2084 |
super().__init__(cdemu, device_number) |
| 2085 |
|
| 2086 |
self.connect_signal( |
| 2087 |
self.menu_item, |
| 2088 |
"button-press-event", |
| 2089 |
self.on_menu_item_button_press_event, |
| 2090 |
) |
| 2091 |
|
| 2092 |
def on_menu_item_button_press_event(self, widget, event): |
| 2093 |
if event.button == 1: |
| 2094 |
# Left click: device dialog |
| 2095 |
self.dialog.present() # Make sure the window is always on top |
| 2096 |
# Don't "run" the dialog, because we want it to be non-modal |
| 2097 |
# self.dialog.run() |
| 2098 |
# self.dialog.hide() |
| 2099 |
else: |
| 2100 |
# Right click: quick load/unload |
| 2101 |
self.load_unload_device() |
| 2102 |
|
| 2103 |
def update_status(self, notify): |
| 2104 |
gCDEmuDevice.update_status(self, notify) |
| 2105 |
|
| 2106 |
if self.device.loaded: |
| 2107 |
self.menu_item.set_tooltip_text(_("Left click for Property Dialog, right click to unload.")) |
| 2108 |
else: |
| 2109 |
self.menu_item.set_tooltip_text(_("Left click for Property Dialog, right click to load.")) |
| 2110 |
|
| 2111 |
|
| 2112 |
class gCDEmuDevice_Indicator(gCDEmuDevice): |
| 2113 |
def __init__(self, cdemu, device_number): |
| 2114 |
super().__init__(cdemu, device_number) |
| 2115 |
|
| 2116 |
self.menu_item.set_tooltip_text(_("Click for Property Dialog")) |
| 2117 |
|
| 2118 |
self.connect_signal( |
| 2119 |
self.menu_item, |
| 2120 |
"activate", |
| 2121 |
self.on_menu_item_activate, |
| 2122 |
) |
| 2123 |
|
| 2124 |
def on_menu_item_activate(self, widget): |
| 2125 |
# Left click: device dialog |
| 2126 |
self.dialog.present() # Make sure the window is always on top |
| 2127 |
# Don't "run" the dialog, because we want it to be non-modal |
| 2128 |
# self.dialog.run() |
| 2129 |
# self.dialog.hide() |
| 2130 |
|
| 2131 |
|
| 2132 |
######################################################################## |
| 2133 |
# gCDEmu # |
| 2134 |
######################################################################## |
| 2135 |
class gCDEmuTray(GObject.GObject): |
| 2136 |
def load_logo(self): |
| 2137 |
# Lookup "icon" in standard paths |
| 2138 |
icon_name = "gcdemu" |
| 2139 |
icon_info = Gtk.IconTheme().lookup_icon(icon_name, 0, 0) |
| 2140 |
if icon_info is None: |
| 2141 |
print(f"Pixmap {icon_name!r} not found in standard pixmap paths!") |
| 2142 |
self.logo = None |
| 2143 |
return |
| 2144 |
logo_filename = icon_info.get_filename() |
| 2145 |
if logo_filename is None: |
| 2146 |
print("Failed to get icon filename; Do you have the librsvg2 package?") |
| 2147 |
self.logo = None |
| 2148 |
return |
| 2149 |
|
| 2150 |
# Load |
| 2151 |
self.logo = GdkPixbuf.Pixbuf.new_from_file_at_size(logo_filename, 156, 156) |
| 2152 |
|
| 2153 |
def cleanup(self): |
| 2154 |
# Default state: disconnected |
| 2155 |
self.connected = False |
| 2156 |
self.update_icon() |
| 2157 |
|
| 2158 |
self.item_new_device.set_sensitive(False) |
| 2159 |
|
| 2160 |
# Remove devices |
| 2161 |
num_devices = len(self.devices) |
| 2162 |
for i in reversed(range(num_devices)): |
| 2163 |
self.remove_device(i) |
| 2164 |
|
| 2165 |
def connect_to_daemon(self): |
| 2166 |
# Cleanup |
| 2167 |
self.cleanup() |
| 2168 |
|
| 2169 |
# Establish connection |
| 2170 |
self.cdemu.connect_to_bus(self.use_system_bus, self.daemon_autostart) |
| 2171 |
|
| 2172 |
def __init__(self): |
| 2173 |
super().__init__() |
| 2174 |
|
| 2175 |
self.connected = False |
| 2176 |
self.devices = [] |
| 2177 |
|
| 2178 |
# *** Configuration *** |
| 2179 |
self.settings = Gio.Settings.new("net.sf.cdemu.gcdemu") |
| 2180 |
|
| 2181 |
# Watch over our settings |
| 2182 |
self.settings.connect("changed", lambda s, k: self.on_settings_changed(k)) |
| 2183 |
|
| 2184 |
# Notifications |
| 2185 |
if Notify.init(app_name): |
| 2186 |
self.show_notifications = self.settings.get_boolean("show-notifications") |
| 2187 |
else: |
| 2188 |
# Disable because pynotify initialization failed |
| 2189 |
self.show_notifications = False |
| 2190 |
|
| 2191 |
# Which bus to use |
| 2192 |
self.use_system_bus = self.settings.get_boolean("use-system-bus") |
| 2193 |
|
| 2194 |
# Daemon autostart |
| 2195 |
self.daemon_autostart = self.settings.get_boolean("daemon-autostart") |
| 2196 |
|
| 2197 |
# Icon policy |
| 2198 |
self.icon_policy = self.settings.get_string("icon-policy") |
| 2199 |
|
| 2200 |
# Icon names |
| 2201 |
self.icon_connected = self.settings.get_string("icon-connected") |
| 2202 |
self.icon_disconnected = self.settings.get_string("icon-disconnected") |
| 2203 |
|
| 2204 |
# Load logo |
| 2205 |
self.load_logo() |
| 2206 |
|
| 2207 |
# *** The About dialog *** |
| 2208 |
self.about = Gtk.AboutDialog() |
| 2209 |
self.about.set_name(app_name) |
| 2210 |
self.about.set_version(app_version) |
| 2211 |
self.about.set_copyright("Copyright (C) 2006-%d Rok Mandeljc" % (datetime.date.today().year)) |
| 2212 |
self.about.set_comments(_("A GUI for controlling CDEmu devices.")) |
| 2213 |
self.about.set_website("http://cdemu.sf.net") |
| 2214 |
self.about.set_website_label(_("The CDEmu project website")) |
| 2215 |
self.about.set_authors(["Rok Mandeljc <[email protected]>"]) |
| 2216 |
self.about.set_artists(["Rômulo Fernandes <[email protected]>"]) |
| 2217 |
self.about.set_translator_credits(_("translator-credits")) |
| 2218 |
self.about.set_logo(self.logo) |
| 2219 |
|
| 2220 |
# *** Menu *** |
| 2221 |
self.menu = Gtk.Menu() |
| 2222 |
|
| 2223 |
# Devices |
| 2224 |
item = Gtk.MenuItem.new_with_label(_("Devices")) |
| 2225 |
item.set_sensitive(False) |
| 2226 |
self.menu.append(item) |
| 2227 |
|
| 2228 |
item = Gtk.MenuItem.new_with_label(_("New device...")) |
| 2229 |
item.set_sensitive(False) |
| 2230 |
item.connect("activate", self.on_new_device_activate) |
| 2231 |
self.menu.append(item) |
| 2232 |
self.item_new_device = item |
| 2233 |
|
| 2234 |
# Separator |
| 2235 |
self.menu.append(Gtk.SeparatorMenuItem()) |
| 2236 |
|
| 2237 |
# Use system bus |
| 2238 |
if False: |
| 2239 |
item = Gtk.CheckMenuItem.new_with_mnemonic(_("Use _system bus")) |
| 2240 |
item.set_active(self.use_system_bus) |
| 2241 |
item.connect("toggled", self.on_use_system_bus_toggled) |
| 2242 |
self.menu.append(item) |
| 2243 |
self.item_use_system_bus = item |
| 2244 |
|
| 2245 |
# Show notifications |
| 2246 |
item = Gtk.CheckMenuItem.new_with_mnemonic(_("Show _notifications")) |
| 2247 |
item.set_active(self.show_notifications) |
| 2248 |
item.connect("toggled", self.on_show_notifications_toggled) |
| 2249 |
self.menu.append(item) |
| 2250 |
self.item_show_notifications = item |
| 2251 |
|
| 2252 |
# Separator |
| 2253 |
self.menu.append(Gtk.SeparatorMenuItem()) |
| 2254 |
|
| 2255 |
# About |
| 2256 |
item = Gtk.MenuItem.new_with_label(_("About")) |
| 2257 |
item.connect("activate", self.on_about_activated) |
| 2258 |
self.menu.append(item) |
| 2259 |
|
| 2260 |
# Separator |
| 2261 |
self.menu.append(Gtk.SeparatorMenuItem()) |
| 2262 |
|
| 2263 |
# Quit |
| 2264 |
item = Gtk.MenuItem.new_with_label(_("Quit")) |
| 2265 |
item.connect("activate", self.on_quit_activated) |
| 2266 |
self.menu.append(item) |
| 2267 |
|
| 2268 |
self.menu.show_all() |
| 2269 |
|
| 2270 |
# *** Create the CDEmu object *** |
| 2271 |
self.cdemu = CDEmu() |
| 2272 |
self.cdemu.connect("daemon-started", self.on_daemon_started) |
| 2273 |
self.cdemu.connect("daemon-stopped", self.on_daemon_stopped) |
| 2274 |
self.cdemu.connect("connection-established", self.on_connection_established) |
| 2275 |
self.cdemu.connect("connection-lost", self.on_connection_lost) |
| 2276 |
self.cdemu.connect("error", lambda c, t, m: self.show_error(t, m)) |
| 2277 |
self.cdemu.connect("device-added", lambda c, d: self.on_device_added(d)) |
| 2278 |
self.cdemu.connect("device-removed", lambda c, d: self.on_device_removed(d)) |
| 2279 |
|
| 2280 |
def on_settings_changed(self, key): |
| 2281 |
if key == "use-system-bus": |
| 2282 |
self.use_system_bus = self.settings.get_boolean(key) |
| 2283 |
print(f"New setting: use system bus: {self.use_system_bus}") |
| 2284 |
self.item_use_system_bus.set_active(self.use_system_bus) |
| 2285 |
self.cdemu.connect_to_bus(self.use_system_bus, self.daemon_autostart) |
| 2286 |
elif key == "show-notifications": |
| 2287 |
self.show_notifications = self.settings.get_boolean(key) |
| 2288 |
self.item_show_notifications.set_active(self.show_notifications) |
| 2289 |
print(f"New setting: show notifications: {self.show_notifications}") |
| 2290 |
elif key == "icon-connected": |
| 2291 |
self.icon_connected = self.settings.get_string(key) |
| 2292 |
self.update_icon() |
| 2293 |
elif key == "icon-disconnected": |
| 2294 |
self.icon_disconnected = self.settings.get_string(key) |
| 2295 |
self.update_icon() |
| 2296 |
elif key == "daemon-autostart": |
| 2297 |
self.daemon_autostart = self.settings.get_boolean(key) |
| 2298 |
print(f"New setting: daemon autostart: {self.daemon_autostart}") |
| 2299 |
elif key == "icon-policy": |
| 2300 |
self.icon_policy = self.settings.get_string(key) |
| 2301 |
self.update_icon() |
| 2302 |
else: |
| 2303 |
print(f"Unknown setting key: {key}!") |
| 2304 |
|
| 2305 |
def on_about_activated(self, menuitem): |
| 2306 |
self.about.run() |
| 2307 |
self.about.hide() |
| 2308 |
|
| 2309 |
def on_quit_activated(self, menuitem): |
| 2310 |
self.emit("quit") |
| 2311 |
|
| 2312 |
def on_use_system_bus_toggled(self, checkmenuitem): |
| 2313 |
self.settings.set_boolean("use-system-bus", checkmenuitem.get_active()) |
| 2314 |
|
| 2315 |
def on_show_notifications_toggled(self, checkmenuitem): |
| 2316 |
self.settings.set_boolean("show-notifications", checkmenuitem.get_active()) |
| 2317 |
|
| 2318 |
def on_connection_established(self, cdemu, num_devices): |
| 2319 |
self.connected = True |
| 2320 |
self.update_icon() |
| 2321 |
|
| 2322 |
self.item_new_device.set_sensitive(True) |
| 2323 |
|
| 2324 |
# Create devices GUI |
| 2325 |
for i in range(num_devices): |
| 2326 |
# Add device |
| 2327 |
self.add_device(i) |
| 2328 |
|
| 2329 |
def on_connection_lost(self, cdemu): |
| 2330 |
self.cleanup() |
| 2331 |
|
| 2332 |
def on_daemon_started(self, cdemu): |
| 2333 |
self.show_notification( |
| 2334 |
_("Daemon started"), |
| 2335 |
_("CDEmu daemon has been started."), |
| 2336 |
"dialog-information", |
| 2337 |
) |
| 2338 |
|
| 2339 |
def on_daemon_stopped(self, cdemu): |
| 2340 |
self.show_notification( |
| 2341 |
_("Daemon stopped"), |
| 2342 |
_("CDEmu daemon has been stopped."), |
| 2343 |
"dialog-information", |
| 2344 |
) |
| 2345 |
|
| 2346 |
def on_device_notification(self, device, summary, body): |
| 2347 |
self.show_notification( |
| 2348 |
summary, |
| 2349 |
body, |
| 2350 |
"dialog-information", |
| 2351 |
) |
| 2352 |
|
| 2353 |
def show_notification(self, summary, body, icon): |
| 2354 |
if self.show_notifications: |
| 2355 |
notification = Notify.Notification.new(summary, body, icon) |
| 2356 |
notification.show() |
| 2357 |
|
| 2358 |
def show_error(self, title, text): |
| 2359 |
# Show error dialog |
| 2360 |
message = Gtk.MessageDialog( |
| 2361 |
parent=None, |
| 2362 |
message_type=Gtk.MessageType.ERROR, |
| 2363 |
buttons=Gtk.ButtonsType.CLOSE, |
| 2364 |
text=text, |
| 2365 |
) |
| 2366 |
message.set_title(title) |
| 2367 |
message.run() |
| 2368 |
message.destroy() |
| 2369 |
|
| 2370 |
def on_new_device_activate(self, menuitem): |
| 2371 |
self.cdemu.add_device() |
| 2372 |
|
| 2373 |
def on_device_added(self, device_number): |
| 2374 |
self.add_device(device_number) |
| 2375 |
self.show_notification( |
| 2376 |
_("Device added"), |
| 2377 |
_("Device #%02d has been created.") % device_number, |
| 2378 |
"dialog-information", |
| 2379 |
) |
| 2380 |
|
| 2381 |
def on_device_removed(self, device_number): |
| 2382 |
self.remove_device(device_number) |
| 2383 |
self.show_notification( |
| 2384 |
_("Device removed"), |
| 2385 |
_("Device #%02d has been removed.") % device_number, |
| 2386 |
"dialog-information", |
| 2387 |
) |
| 2388 |
|
| 2389 |
def add_device(self, device_number): |
| 2390 |
# Create device dialog |
| 2391 |
device = self.create_device(self.cdemu, device_number) |
| 2392 |
self.devices.append(device) |
| 2393 |
|
| 2394 |
# Add device's menu item to the devices menu |
| 2395 |
self.menu.insert(device.menu_item, 1 + device_number) |
| 2396 |
# Connect signal |
| 2397 |
device.connect("device-notification", self.on_device_notification) |
| 2398 |
|
| 2399 |
# Set the just-added device as "removable", and mark the one |
| 2400 |
# before it as "non-removable"... |
| 2401 |
self.devices[-1].set_removable(True) |
| 2402 |
if len(self.devices) > 1: |
| 2403 |
self.devices[-2].set_removable(False) |
| 2404 |
|
| 2405 |
def remove_device(self, device_number): |
| 2406 |
# Remove from list |
| 2407 |
device = self.devices.pop(device_number) |
| 2408 |
# Cleanup the device |
| 2409 |
device.cleanup() |
| 2410 |
|
| 2411 |
# Mark the last device in the list as "removable" |
| 2412 |
if len(self.devices) > 0: |
| 2413 |
self.devices[-1].set_removable(True) |
| 2414 |
|
| 2415 |
|
| 2416 |
GObject.type_register(gCDEmuTray) |
| 2417 |
|
| 2418 |
GObject.signal_new( |
| 2419 |
"quit", |
| 2420 |
gCDEmuTray, |
| 2421 |
GObject.SignalFlags.RUN_FIRST, |
| 2422 |
None, |
| 2423 |
(), |
| 2424 |
) |
| 2425 |
|
| 2426 |
|
| 2427 |
class gCDEmuTray_Gtk(gCDEmuTray): |
| 2428 |
def __init__(self): |
| 2429 |
super().__init__() |
| 2430 |
|
| 2431 |
# *** The status icon *** |
| 2432 |
self.tray_icon = Gtk.StatusIcon() |
| 2433 |
|
| 2434 |
self.tray_icon.set_visible(True) |
| 2435 |
self.tray_icon.connect('activate', self.on_activate) |
| 2436 |
self.tray_icon.connect('popup-menu', self.on_popup_menu) |
| 2437 |
|
| 2438 |
# Update icon |
| 2439 |
self.update_icon() |
| 2440 |
|
| 2441 |
def create_device(self, cdemu, device_number): |
| 2442 |
return gCDEmuDevice_Gtk(cdemu, device_number) |
| 2443 |
|
| 2444 |
def on_activate(self, status_icon): |
| 2445 |
# Popup the devices menu (simulate the 1st button click) |
| 2446 |
self.menu.popup( |
| 2447 |
None, |
| 2448 |
None, |
| 2449 |
status_icon.position_menu, |
| 2450 |
self.tray_icon, |
| 2451 |
1, |
| 2452 |
Gtk.get_current_event_time(), |
| 2453 |
) |
| 2454 |
|
| 2455 |
def on_popup_menu(self, status_icon, button, activate_time): |
| 2456 |
# Popup the menu |
| 2457 |
self.menu.popup( |
| 2458 |
None, |
| 2459 |
None, |
| 2460 |
status_icon.position_menu, |
| 2461 |
self.tray_icon, |
| 2462 |
button, |
| 2463 |
activate_time, |
| 2464 |
) |
| 2465 |
|
| 2466 |
def update_icon(self): |
| 2467 |
# Show/hide icon |
| 2468 |
if self.icon_policy == "always": |
| 2469 |
visible = True |
| 2470 |
elif self.icon_policy == "never": |
| 2471 |
visible = False |
| 2472 |
elif self.icon_policy in {"when_connected", "when-connected"}: |
| 2473 |
if self.connected: |
| 2474 |
visible = True |
| 2475 |
else: |
| 2476 |
visible = False |
| 2477 |
else: |
| 2478 |
# Fix invalid setting... |
| 2479 |
print(f"Unknown icon policy {self.icon_policy!r}! Using 'always' by default!") |
| 2480 |
self.icon_policy = "always" |
| 2481 |
visible = True |
| 2482 |
self.tray_icon.set_visible(visible) |
| 2483 |
|
| 2484 |
# Set icon |
| 2485 |
if self.connected: |
| 2486 |
icon_name = self.icon_connected |
| 2487 |
tooltip_text = _('gCDEmu - connected') |
| 2488 |
else: |
| 2489 |
icon_name = self.icon_disconnected |
| 2490 |
tooltip_text = _('gCDEmu - disconnected') |
| 2491 |
|
| 2492 |
if icon_name is None: |
| 2493 |
icon_name = "image-missing" # Fallback icon |
| 2494 |
|
| 2495 |
self.tray_icon.set_from_icon_name(icon_name) |
| 2496 |
self.tray_icon.set_tooltip_text(tooltip_text) |
| 2497 |
|
| 2498 |
|
| 2499 |
class gCDEmuTray_Indicator(gCDEmuTray): |
| 2500 |
def __init__(self): |
| 2501 |
super().__init__() |
| 2502 |
|
| 2503 |
# AppIndicator |
| 2504 |
self.indicator = AppIndicator.Indicator.new( |
| 2505 |
app_name, |
| 2506 |
"gcdemu", |
| 2507 |
AppIndicator.IndicatorCategory.HARDWARE, |
| 2508 |
) |
| 2509 |
self.indicator.set_status(AppIndicator.IndicatorStatus.ACTIVE) |
| 2510 |
self.indicator.set_menu(self.menu) |
| 2511 |
|
| 2512 |
# Update icon |
| 2513 |
self.update_icon() |
| 2514 |
|
| 2515 |
def create_device(self, cdemu, device_number): |
| 2516 |
return gCDEmuDevice_Indicator(cdemu, device_number) |
| 2517 |
|
| 2518 |
def update_icon(self): |
| 2519 |
# Set icons |
| 2520 |
self.indicator.set_icon_full(self.icon_connected, "Connected") |
| 2521 |
self.indicator.set_attention_icon_full(self.icon_disconnected, "Disconnected") |
| 2522 |
|
| 2523 |
# Show/hide icon |
| 2524 |
if self.icon_policy == "always": |
| 2525 |
visible = True |
| 2526 |
elif self.icon_policy == "never": |
| 2527 |
visible = False |
| 2528 |
elif self.icon_policy in {"when_connected", "when-connected"}: |
| 2529 |
if self.connected: |
| 2530 |
visible = True |
| 2531 |
else: |
| 2532 |
visible = False |
| 2533 |
else: |
| 2534 |
# Fix invalid setting... |
| 2535 |
print(f"Unknown icon policy {self.icon_policy!r}! Using 'always' by default!") |
| 2536 |
self.icon_policy = "always" |
| 2537 |
visible = True |
| 2538 |
|
| 2539 |
# Set the status (change icon) |
| 2540 |
if visible: |
| 2541 |
if self.connected: |
| 2542 |
self.indicator.set_status(AppIndicator.IndicatorStatus.ACTIVE) |
| 2543 |
else: |
| 2544 |
self.indicator.set_status(AppIndicator.IndicatorStatus.ATTENTION) |
| 2545 |
else: |
| 2546 |
self.indicator.set_status(AppIndicator.IndicatorStatus.PASSIVE) |
| 2547 |
|
| 2548 |
|
| 2549 |
######################################################################## |
| 2550 |
# Main # |
| 2551 |
######################################################################## |
| 2552 |
class gCDEmuApplication(Gtk.Application): |
| 2553 |
def __init__(self): |
| 2554 |
super().__init__( |
| 2555 |
application_id="net.sf.cdemu.GCDEmu", |
| 2556 |
flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE, |
| 2557 |
) |
| 2558 |
|
| 2559 |
self.arguments = None |
| 2560 |
self.tray = None |
| 2561 |
|
| 2562 |
def do_activate(self): |
| 2563 |
if self.tray: |
| 2564 |
# If tray already exists, do nothing... |
| 2565 |
return |
| 2566 |
|
| 2567 |
# Create hidden window to keep GtkApplication happy |
| 2568 |
window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL) |
| 2569 |
window.set_title("gCDEmu") |
| 2570 |
self.add_window(window) |
| 2571 |
|
| 2572 |
# Create tray |
| 2573 |
if self.args.tray_mode == "gtk": |
| 2574 |
# Gtk tray icon |
| 2575 |
self.tray = gCDEmuTray_Gtk() |
| 2576 |
elif self.args.tray_mode == "indicator": |
| 2577 |
# AppIndicator |
| 2578 |
if not have_app_indicator: |
| 2579 |
message = Gtk.MessageDialog( |
| 2580 |
parent=None, |
| 2581 |
message_type=Gtk.MessageType.ERROR, |
| 2582 |
buttons=Gtk.ButtonsType.CLOSE, |
| 2583 |
text=_("Failed to load AppIndicator library!"), |
| 2584 |
) |
| 2585 |
message.set_title(_("AppIndicator not available")) |
| 2586 |
message.run() |
| 2587 |
message.destroy() |
| 2588 |
sys.exit(-1) |
| 2589 |
|
| 2590 |
self.tray = gCDEmuTray_Indicator() |
| 2591 |
else: |
| 2592 |
# Autodetect: if AppIndicator is available, prefer it over |
| 2593 |
# Gtk tray |
| 2594 |
if have_app_indicator: |
| 2595 |
print("AppIndicator tray icon mode") |
| 2596 |
self.tray = gCDEmuTray_Indicator() |
| 2597 |
else: |
| 2598 |
print("Gtk tray icon mode") |
| 2599 |
self.tray = gCDEmuTray_Gtk() |
| 2600 |
|
| 2601 |
self.tray.connect("quit", lambda o: self.quit()) |
| 2602 |
|
| 2603 |
# Connect to daemon |
| 2604 |
self.tray.connect_to_daemon() |
| 2605 |
|
| 2606 |
def do_command_line(self, args): |
| 2607 |
# Default command-line handler |
| 2608 |
Gtk.Application.do_command_line(self, args) |
| 2609 |
|
| 2610 |
# We parse arguments only if they are local, as it would be very |
| 2611 |
# inconvenient for command-line parser to blow up in our face for |
| 2612 |
# a secondary-instance (which does nothing in our case, anyway) |
| 2613 |
# and bring down the primary instance as well. Now, in an ideal |
| 2614 |
# world, we would take care of this in do_local_command_line(), |
| 2615 |
# however as of GIO 2.38, overriding that one causes a segfault... |
| 2616 |
if not args.get_is_remote(): |
| 2617 |
# Command-line parser |
| 2618 |
parser = argparse.ArgumentParser( |
| 2619 |
prog="gcdemu", |
| 2620 |
formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 2621 |
) |
| 2622 |
parser.add_argument( |
| 2623 |
"--tray-mode", |
| 2624 |
type=str, |
| 2625 |
nargs="?", |
| 2626 |
choices=["gtk", "indicator", "auto"], |
| 2627 |
default="auto", |
| 2628 |
help=_("gCDEmu tray mode"), |
| 2629 |
) |
| 2630 |
|
| 2631 |
self.args = parser.parse_args(args.get_arguments()[1:]) |
| 2632 |
|
| 2633 |
# Activate the app |
| 2634 |
self.do_activate() |
| 2635 |
|
| 2636 |
return False |
| 2637 |
|
| 2638 |
|
| 2639 |
if __name__ == "__main__": |
| 2640 |
app = gCDEmuApplication() |
| 2641 |
signal.signal(signal.SIGINT, signal.SIG_DFL) # Make Ctrl+C work |
| 2642 |
status = app.run(sys.argv) |
| 2643 |
sys.exit(status) |