build.sh: use git-show instead of git-show-ref to get the commit id
[sugaredwine.git] / wineactivity.py
blobe5edccf0f6bad99005c853c6155d3f5eec63fe44
1 from sugar.activity import activity
2 import sys
3 import os
4 import gobject
5 import gtk
6 import shutil
7 import tempfile
9 class WineActivity(activity.Activity):
10 def __init__(self, handle):
11 activity.Activity.__init__(self, handle)
13 self.bundle_path = activity.get_bundle_path()
15 try:
16 activity_root = activity.get_activity_root()
17 wine_prefix = os.path.join(activity_root, 'data', 'wine')
18 except AttributeError:
19 try:
20 activity_root = os.environ['SUGAR_ACTIVITY_ROOT']
21 wine_prefix = os.path.join(activity_root, 'data', 'wine')
22 except KeyError:
23 activity_root = None
24 wine_prefix = os.path.expanduser('~/.wine')
26 try:
27 self.desktop_parent = gtk.EventBox()
28 self.desktop_parent.show()
29 self.set_canvas(self.desktop_parent)
30 except AttributeError:
31 # remove any children of the window that Sugar may have added
32 for widget in self.get_children():
33 self.remove(widget)
35 self.desktop_parent = self
37 os.environ['LD_LIBRARY_PATH'] = "%s:%s" % (os.path.join(self.bundle_path, 'lib'), os.environ.get('LD_LIBRARY_PATH', ''))
38 os.environ['PATH'] = "%s:%s" % (os.path.join(self.bundle_path, 'bin'), os.environ.get('PATH', ''))
39 os.environ['WINEPREFIX'] = wine_prefix
40 os.environ['WINELOADER'] = os.path.join(self.bundle_path, 'bin/wine')
41 os.environ['WINESERVER'] = os.path.join(self.bundle_path, 'bin/wineserver')
42 os.environ['WINEDLLPATH'] = os.path.join(self.bundle_path, 'lib/wine')
44 firstrun = not os.path.exists(wine_prefix)
46 self.setup_prefix(firstrun)
48 self.desktop_parent.connect('map', self.on_parent_map)
50 self.set_title("Wine")
52 def on_parent_map(self, widget):
53 os.environ['WINE_DESKTOP_PARENT'] = str(self.desktop_parent.window.xid)
55 gtk.gdk.flush()
57 allocation = self.desktop_parent.get_allocation()
59 width = gtk.gdk.screen_width()
60 height = gtk.gdk.screen_height()
62 self.wine_pid, stdin, stdout, stderr = gobject.spawn_async(
63 ('wine', 'explorer', '/desktop=Sugar,%sx%s' % (width, height)),
64 flags=gobject.SPAWN_SEARCH_PATH|gobject.SPAWN_DO_NOT_REAP_CHILD)
65 gobject.child_watch_add(self.wine_pid, self.on_wine_quit, None)
67 def start_wine(self, *args):
68 gobject.spawn_async(
69 ['wine', 'explorer', '/desktop=Sugar'] + [str(x) for x in args],
70 flags=gobject.SPAWN_SEARCH_PATH)
72 def get_unix_path(self, windowspath):
73 pid, stdin, stdout, stderr = gobject.spawn_async(
74 ('winepath', '--unix', windowspath),
75 flags=gobject.SPAWN_SEARCH_PATH,
76 standard_output=True)
78 result = []
79 s = os.read(stdout, 4096)
80 while s:
81 result.append(s)
82 s = os.read(stdout, 4096)
84 result = ''.join(result).rstrip('\n')
86 return result
88 def setup_prefix(self, firstrun):
89 os.system('wineboot --init')
91 if firstrun:
92 #set up desktop background
93 winini = self.get_unix_path(r'c:\windows\win.ini')
95 f = open(winini, 'w')
96 f.write("\r\n\r\n[desktop]\r\nWallPaper=c:\\windows\\landscape.bmp")
97 f.close()
99 import shutil
100 landscapebmp = self.get_unix_path(r'c:\windows\landscape.bmp')
101 shutil.copyfile(os.path.join(self.bundle_path, 'background', 'landscape.bmp'), landscapebmp)
103 def on_wine_quit(self, pid, condition, data):
104 sys.exit()
106 def read_file(self, file_path):
107 #the file is read-only and will be deleted so make a copy
108 tempdir = tempfile.mkdtemp('', '', self.get_unix_path(r'c:\windows\temp'))
109 head, tail = os.path.split(file_path)
110 exefile = os.path.join(tempdir, tail)
111 shutil.copyfile(file_path, exefile)
112 self.start_wine('start', '/unix', exefile)