forked from sugarlabs/Frotz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrotz.py
305 lines (252 loc) · 11 KB
/
frotz.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# Frotz.activity
# A simple front end to the classic interactive fiction interpreter frotz on the XO laptop
# http://wiki.laptop.org/go/Frotz
#
# Copyright (C) 2008 Joshua Minor
# This file is part of Frotz.activity
#
# Parts of Frotz.activity are based on code from Terminal.activity
# Copyright (C) 2007, Eduardo Silva <[email protected]>.
# Copyright (C) 2008, One Laptop Per Child
#
# Frotz.activity is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Frotz.activity is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Frotz.activity. If not, see <http://www.gnu.org/licenses/>.
import gi
gi.require_version('Gtk', '3.0')
import os
import sys
import logging
from gettext import gettext as _
from gi.repository import Gtk, Gdk,GLib
import dbus
from sugar3.activity import activity
from sugar3.activity import activityfactory
from sugar3 import env
from sugar3.graphics.toolbutton import ToolButton
from sugar3.graphics.palette import Palette
import configparser
import os.path
from gi.repository import Pango
from gi.repository import Vte
import platform, sys
from ctypes import cdll
if platform.machine().startswith('arm'):
pass # FIXME
else:
if platform.architecture()[0] == '64bit':
vte_path = "x86-64"
else:
vte_path = "x86"
vte = cdll.LoadLibrary("lib/%s/libvte.so.9" % vte_path)
sys.path.append("lib/%s" % vte_path)
class FrotzActivity(activity.Activity):
def __init__(self, handle):
activity.Activity.__init__(self, handle)
logging.debug('Starting the Frotz activity')
self.set_title(_('Frotz'))
self.connect('key-press-event', self.__key_press_cb)
toolbox = activity.ActivityToolbox(self)
self._edit_toolbar = activity.EditToolbar()
toolbox.add_toolbar(_('Edit'), self._edit_toolbar)
self._edit_toolbar.show()
self._edit_toolbar.undo.props.visible = False
self._edit_toolbar.redo.props.visible = False
self._edit_toolbar.separator.props.visible = False
self._edit_toolbar.copy.connect('clicked', self._copy_cb)
self._edit_toolbar.paste.connect('clicked', self._paste_cb)
activity_toolbar = toolbox.get_activity_toolbar()
activity_toolbar.share.props.visible = False
activity_toolbar.keep.props.visible = False
# Add a button that will send you to the ifarchive to get more games
activity_toolbar.get_games = ToolButton('activity-get-games')
activity_toolbar.get_games.set_tooltip(_('Get More Games'))
activity_toolbar.get_games.connect('clicked', self._get_games_cb)
activity_toolbar.insert(activity_toolbar.get_games, 2)
activity_toolbar.get_games.show()
self.set_toolbox(toolbox)
toolbox.show()
box = Gtk.HBox(False, 4)
self._vte = VTE()
self._vte.show()
self._vte.connect("child-exited", self._quit_cb)
scrollbar = Gtk.VScrollbar(self._vte.get_adjustment())
scrollbar.show()
box.pack_start(self._vte)
box.pack_start(scrollbar, False, False, 0)
self.set_canvas(box)
box.show()
self._vte.grab_focus()
self.game_started = False
default_game_file = os.path.join(activity.get_bundle_path(), "Advent.z5")
# when we return to the idle state, launch the default game
# if read_file is called, that will override this
GLib.idle_add(self.start_game, default_game_file)
def _quit_cb(self, foo=None):
print("Quitting...")
sys.exit(0)
def start_game(self, game_file):
if not self.game_started:
# cd to a persistent directory
# so that saved games will have a place to live
save_dir = os.path.join(os.environ["SUGAR_ACTIVITY_ROOT"], "data")
# print a welcome banner and pause for a moment
# that way the end user will have a chance to see which version of frotz we are using
# and which file we are loading
if platform.machine().startswith('arm'):
logging.error('ARM not supported, yet') # FIXME
sys.exit(0)
else:
if platform.architecture()[0] == '64bit':
self._vte.feed_child("cd '%s'; clear; frotz64|head -3 ; echo '\nLoading %s...'; sleep 2; frotz64 '%s'; exit\n" % (save_dir, os.path.basename(game_file), game_file))
else:
self._vte.feed_child("cd '%s'; clear; frotz32|head -3 ; echo '\nLoading %s...'; sleep 2; frotz32 '%s'; exit\n" % (save_dir, os.path.basename(game_file), game_file))
self.game_started = True
def read_file(self, file_path):
self.start_game(file_path)
def open_url(self, url):
"""Ask the journal to open an URL for us."""
from sugar3 import profile
from shutil import rmtree
from sugar3.datastore import datastore
from sugar3.activity.activity import show_object_in_journal
from tempfile import mkdtemp
tmpfolder = mkdtemp('.tmp', 'url', os.path.join(self.get_activity_root(), 'instance'))
tmpfilepath = os.path.join(tmpfolder, 'url')
try:
tmpfile = open(tmpfilepath, 'w')
tmpfile.write(url)
tmpfile.close()
os.chmod(tmpfolder, 0o755)
os.chmod(tmpfilepath, 0o755)
jobject = datastore.create()
metadata = {
'title': url,
'title_set_by_user': '1',
'buddies': '',
'preview': '',
'icon-color': profile.get_color().to_string(),
'mime_type': 'text/uri-list',
}
for k, v in list(metadata.items()):
jobject.metadata[k] = v # the dict.update method is missing =(
jobject.file_path = tmpfilepath
datastore.write(jobject)
show_object_in_journal(jobject.object_id)
jobject.destroy()
finally:
rmtree(tmpfilepath, ignore_errors=True) # clean up!
def _get_games_cb(self, button):
url = 'http://wiki.laptop.org/go/Frotz/Games'
#activityfactory.create_with_uri('org.laptop.WebActivity', url)
self.open_url(url)
def _copy_cb(self, button):
if self._vte.get_has_selection():
self._vte.copy_clipboard()
def _paste_cb(self, button):
self._vte.paste_clipboard()
def __key_press_cb(self, window, event):
if event.state & Gtk.gdk.CONTROL_MASK and event.state & Gtk.gdk.SHIFT_MASK:
if Gtk.gdk.keyval_name(event.keyval) == "C":
if self._vte.get_has_selection():
self._vte.copy_clipboard()
return True
elif Gtk.gdk.keyval_name(event.keyval) == "V":
self._vte.paste_clipboard()
return True
return False
class VTE(Vte.Terminal):
def __init__(self):
Vte.Terminal.__init__(self)
self._configure_vte()
#os.chdir(os.environ["HOME"])
self.fork_command()
def _configure_vte(self):
conf = configparser.ConfigParser()
conf_file = os.path.join(env.get_profile_path(), 'terminalrc')
if os.path.isfile(conf_file):
f = open(conf_file, 'r')
conf.readfp(f)
f.close()
else:
conf.add_section('terminal')
if conf.has_option('terminal', 'font'):
font = conf.get('terminal', 'font')
else:
font = 'Monospace 8'
conf.set('terminal', 'font', font)
self.set_font(Pango.FontDescription(font))
if conf.has_option('terminal', 'fg_color'):
fg_color = conf.get('terminal', 'fg_color')
else:
fg_color = '#000000'
conf.set('terminal', 'fg_color', fg_color)
if conf.has_option('terminal', 'bg_color'):
bg_color = conf.get('terminal', 'bg_color')
else:
bg_color = '#FFFFFF'
conf.set('terminal', 'bg_color', bg_color)
self.set_colors(Gtk.gdk.color_parse (fg_color),
Gtk.gdk.color_parse (bg_color),
[])
if conf.has_option('terminal', 'cursor_blink'):
blink = conf.getboolean('terminal', 'cursor_blink')
else:
blink = False
conf.set('terminal', 'cursor_blink', blink)
self.set_cursor_blinks(blink)
if conf.has_option('terminal', 'bell'):
bell = conf.getboolean('terminal', 'bell')
else:
bell = False
conf.set('terminal', 'bell', bell)
self.set_audible_bell(bell)
if conf.has_option('terminal', 'scrollback_lines'):
scrollback_lines = conf.getint('terminal', 'scrollback_lines')
else:
scrollback_lines = 1000
conf.set('terminal', 'scrollback_lines', scrollback_lines)
self.set_scrollback_lines(scrollback_lines)
self.set_allow_bold(True)
if conf.has_option('terminal', 'scroll_on_keystroke'):
scroll_key = conf.getboolean('terminal', 'scroll_on_keystroke')
else:
scroll_key = False
conf.set('terminal', 'scroll_on_keystroke', scroll_key)
self.set_scroll_on_keystroke(scroll_key)
if conf.has_option('terminal', 'scroll_on_output'):
scroll_output = conf.getboolean('terminal', 'scroll_on_output')
else:
scroll_output = False
conf.set('terminal', 'scroll_on_output', scroll_output)
self.set_scroll_on_output(scroll_output)
if conf.has_option('terminal', 'emulation'):
emulation = conf.get('terminal', 'emulation')
else:
emulation = 'xterm'
conf.set('terminal', 'emulation', emulation)
self.set_emulation(emulation)
if conf.has_option('terminal', 'visible_bell'):
visible_bell = conf.getboolean('terminal', 'visible_bell')
else:
visible_bell = False
conf.set('terminal', 'visible_bell', visible_bell)
self.set_visible_bell(visible_bell)
conf.write(open(conf_file, 'w'))
def on_gconf_notification(self, client, cnxn_id, entry, what):
self.reconfigure_vte()
def on_vte_button_press(self, term, event):
if event.button == 3:
self.do_popup(event)
return True
def on_vte_popup_menu(self, term):
pass