-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs-uae.py
executable file
·163 lines (137 loc) · 5.45 KB
/
fs-uae.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python3
import subprocess
import os
import sys
import fsuae
from fsuae.luashell import LuaShellError
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtCore import QProcess, QTimer
class ExtendedEmu(fsuae.Emu):
def __init__(self):
super().__init__()
def read_config(self, opt):
try:
result = self.sh.execute("=uae.read_config('{0}')".format(opt), 1)[0]
except LuaShellError:
result = None
except TypeError:
result = None
return result
class Floppy(QtWidgets.QMenu):
def __init__(self, num_drives, shell, floppylist):
QtWidgets.QMenu.__init__(self, parent=None)
self.shell = shell
self.num_drives = num_drives
for n in range(self.num_drives):
floppy = self.shell.getFloppyImagePath(n)
if not floppy:
floppy = '(empty)'
action = self.addAction('DF{0}: {1}'.format(n, floppy))
action.setData((n, None))
self.triggered.connect(self.menuitem_clicked)
if len(floppylist) > 0:
self.add_floppylist_menu(floppylist)
def menuitem_clicked(self, action):
drive_no, image = action.data()
self.insert(drive_no, image)
def insert(self, drive_no, image):
if image is None:
image = self.select_image()
if image is '':
return
self.shell.setFloppyImagePath(drive_no, image)
def select_image(self):
image, filter = QtWidgets.QFileDialog.getOpenFileName(
parent=self, caption='Select a disk image', filter=(
'Floppy disk images (*.adf *.dms *.ipf *.adz);;All Files (*)'))
return image
def add_floppylist_menu(self, floppylist):
self.addSeparator()
submenu = self.addMenu('Floppy List')
for floppy in floppylist:
subsubmenu = submenu.addMenu(floppy)
for n in range(self.num_drives):
action = subsubmenu.addAction('Into DF{0}:'.format(n))
action.setData((n, floppy))
subsubmenu.triggered.connect(self.menuitem_clicked)
class Eject(QtWidgets.QMenu):
def __init__(self, num_drives, shell):
QtWidgets.QMenu.__init__(self, parent=None)
self.shell = shell
for n in range(num_drives):
if self.shell.getFloppyImagePath(n):
action = self.addAction('Eject DF{0}'.format(n))
action.setData(n)
self.triggered.connect(self.menuitem_clicked)
def menuitem_clicked(self, action):
drive_no = action.data()
self.eject(drive_no)
def eject(self, drive_no):
self.shell.setFloppyImagePath(drive_no, '')
class FSUAEtray(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.style = QtWidgets.QApplication.style()
self.icon = QtGui.QIcon(self.style.standardIcon(
QtWidgets.QStyle.SP_DriveFDIcon))
self.systray = QtWidgets.QSystemTrayIcon(self.icon, parent=self)
self.systray.show()
self.shell = ExtendedEmu()
self.num_tries = 0
self.timer = QTimer()
self.timer.setInterval(2000)
self.timer.timeout.connect(self.connect_shell)
self.run_emulator()
self.systray.activated.connect(self.on_icon_clicked)
def run_emulator(self):
self.proc = QProcess(self)
self.proc.started.connect(self.timer.start)
self.proc.finished.connect(self.exit_app)
self.proc.error.connect(self.on_error)
self.proc.setProcessChannelMode(QProcess.ForwardedChannels)
self.proc.start('fs-uae', ['--lua_shell=1'] + sys.argv[1:])
def on_icon_clicked(self, reason):
if reason == QtWidgets.QSystemTrayIcon.Context:
self.menu = Floppy(self.num_drives, self.shell, self.floppylist)
else:
self.menu = Eject(self.num_drives, self.shell)
if not self.menu.isEmpty():
self.menu.exec_(QtGui.QCursor.pos())
def on_error(self, error):
if error == QProcess.FailedToStart:
QtWidgets.QMessageBox.critical(
None, 'Error', 'Failed to start FS-UAE',
QtWidgets.QMessageBox.Ok)
self.exit_app(127)
def exit_app(self, returncode, status=None):
QtWidgets.QApplication.instance().exit(returncode)
def connect_shell(self):
if self.shell.isConnected():
print('connection established.')
self.timer.stop()
self.num_drives = self.shell.getNumFloppyDrives()
self.floppylist = self.get_floppylist()
elif self.num_tries > 4:
print('lua shell connection error')
self.timer.stop()
error = self.shell.getError()
sys.stderr.write('{0}\n'.format(error))
self.systray.showMessage('Lua Shell connection error', error)
self.num_drives = 0
self.floppylist = []
else:
print('connecting...')
self.shell.connect()
self.num_tries += 1
def get_floppylist(self):
floppylist = set()
for n in range(20):
image = self.shell.read_config('diskimage{0}'.format(n))
if image:
floppylist.add(image)
return sorted(list(floppylist))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
tray = FSUAEtray()
sys.exit(app.exec_())