sugar-toolkit-gtk3/sugar/emulator.py

123 lines
3.6 KiB
Python
Raw Normal View History

2006-10-16 13:35:07 +02:00
# Copyright (C) 2006, Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import os
import socket
import sys
import gobject
from sugar import env
def get_display_number():
"""Find a free display number trying to connect to 6000+ ports"""
retries = 20
display_number = 1
display_is_free = False
while not display_is_free and retries > 0:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect(('127.0.0.1', 6000 + display_number))
s.close()
display_number += 1
retries -= 1
except:
display_is_free = True
if display_is_free:
return display_number
else:
logging.error('Cannot find a free display.')
sys.exit(0)
2006-10-16 13:35:07 +02:00
class Process:
"""Object representing one of the session processes"""
def __init__(self, command):
self._command = command
def get_name(self):
return self._command
def start(self, standard_output=False):
args = self._command.split()
flags = gobject.SPAWN_SEARCH_PATH
result = gobject.spawn_async(args, flags=flags,
standard_output=standard_output)
self.pid = result[0]
self._stdout = result[2]
2006-10-16 13:35:07 +02:00
class MatchboxProcess(Process):
def __init__(self):
kbd_config = os.path.join(env.get_data_dir(), 'kbdconfig')
options = '-kbdconfig %s ' % kbd_config
2006-10-16 13:35:07 +02:00
options += '-use_titlebar no '
options += '-theme olpc '
2006-10-16 13:35:07 +02:00
command = 'matchbox-window-manager %s ' % options
Process.__init__(self, command)
def get_name(self):
return 'Matchbox'
2006-10-16 13:35:07 +02:00
class XephyrProcess(Process):
def __init__(self, width, height, fullscreen, dpi):
self._display = get_display_number()
cmd = 'Xephyr :%d -ac ' % (self._display)
2007-01-31 17:49:56 +01:00
if fullscreen:
2007-01-31 17:49:56 +01:00
cmd += ' -fullscreen '
if width > 0 and height > 0:
cmd += ' -screen %dx%d' % (width, height)
if dpi > 0:
cmd += ' -dpi %d' % (dpi)
Process.__init__(self, cmd)
def get_name(self):
return 'Xephyr'
2007-01-16 00:33:04 +01:00
def start(self, standard_output=False):
Process.start(self)
os.environ['DISPLAY'] = ":%d" % (self._display)
os.environ['SUGAR_XEPHYR_PID'] = '%d' % self.pid
2006-10-17 01:09:44 +02:00
class Emulator(object):
"""The OLPC emulator"""
def __init__(self, width, height, fullscreen, dpi):
self._fullscreen = fullscreen
2007-01-31 17:49:56 +01:00
self._width = width
self._height = height
self._dpi = dpi
def start(self):
try:
2007-01-31 17:49:56 +01:00
process = XephyrProcess(self._width, self._height,
self._fullscreen, self._dpi)
process.start()
except:
print 'Cannot run the emulator. You need to install Xephyr'
sys.exit(0)
process = MatchboxProcess()
process.start()