2006-07-12 14:02:29 +02:00
|
|
|
import os
|
2006-07-15 15:32:05 +02:00
|
|
|
import socket
|
2006-07-16 14:22:10 +02:00
|
|
|
import sys
|
2006-07-12 14:02:29 +02:00
|
|
|
|
2006-08-11 11:37:35 +02:00
|
|
|
from session.Process import Process
|
2006-08-22 10:37:24 +02:00
|
|
|
import sugar.env
|
2006-07-12 14:02:29 +02:00
|
|
|
|
2006-07-16 14:22:10 +02:00
|
|
|
def get_display_number():
|
|
|
|
"""Find a free display number trying to connect to 6000+ ports"""
|
|
|
|
retries = 20
|
|
|
|
display_number = 1
|
2006-08-11 13:05:33 +02:00
|
|
|
display_is_free = False
|
|
|
|
|
2006-07-16 14:22:10 +02:00
|
|
|
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-07-12 14:02:29 +02:00
|
|
|
class XephyrProcess(Process):
|
|
|
|
def __init__(self):
|
2006-07-16 14:22:10 +02:00
|
|
|
self._display = get_display_number()
|
2006-08-16 20:34:33 +02:00
|
|
|
cmd = 'Xephyr :%d -ac -screen 800x600' % (self._display)
|
2006-07-12 14:02:29 +02:00
|
|
|
Process.__init__(self, cmd)
|
2006-07-15 15:32:05 +02:00
|
|
|
|
2006-07-16 14:22:10 +02:00
|
|
|
def get_name(self):
|
|
|
|
return 'Xephyr'
|
2006-07-15 15:32:05 +02:00
|
|
|
|
2006-07-16 14:22:10 +02:00
|
|
|
def start(self):
|
|
|
|
Process.start(self)
|
|
|
|
os.environ['DISPLAY'] = ":%d" % (self._display)
|
2006-07-15 15:32:05 +02:00
|
|
|
|
2006-07-16 14:22:10 +02:00
|
|
|
class XnestProcess(Process):
|
|
|
|
def __init__(self):
|
|
|
|
self._display = get_display_number()
|
2006-08-16 20:34:33 +02:00
|
|
|
cmd = 'Xnest :%d -ac -geometry 800x600' % (self._display)
|
2006-07-16 14:22:10 +02:00
|
|
|
Process.__init__(self, cmd)
|
2006-07-15 15:32:05 +02:00
|
|
|
|
2006-07-12 14:02:29 +02:00
|
|
|
def get_name(self):
|
2006-07-16 14:22:10 +02:00
|
|
|
return 'Xnest'
|
2006-07-12 14:02:29 +02:00
|
|
|
|
|
|
|
def start(self):
|
2006-07-16 14:22:10 +02:00
|
|
|
Process.start(self)
|
|
|
|
os.environ['DISPLAY'] = ":%d" % (self._display)
|
2006-07-12 14:02:29 +02:00
|
|
|
|
2006-08-22 10:37:24 +02:00
|
|
|
class DbusProcess(Process):
|
|
|
|
def __init__(self):
|
|
|
|
config = sugar.env.get_dbus_config()
|
|
|
|
cmd = "dbus-daemon --print-address --config-file %s" % config
|
|
|
|
Process.__init__(self, cmd)
|
|
|
|
|
|
|
|
def get_name(self):
|
|
|
|
return 'Dbus'
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
Process.start(self, True)
|
|
|
|
dbus_file = os.fdopen(self._stdout)
|
|
|
|
addr = dbus_file.readline().strip()
|
|
|
|
dbus_file.close()
|
|
|
|
os.environ["DBUS_SESSION_BUS_ADDRESS"] = addr
|
|
|
|
|
2006-07-12 14:02:29 +02:00
|
|
|
class Emulator:
|
|
|
|
"""The OLPC emulator"""
|
|
|
|
def start(self):
|
2006-07-16 14:22:10 +02:00
|
|
|
try:
|
|
|
|
process = XephyrProcess()
|
|
|
|
process.start()
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
process = XnestProcess()
|
|
|
|
process.start()
|
|
|
|
except:
|
2006-08-22 10:37:24 +02:00
|
|
|
print 'Cannot run the emulator. You need to install \
|
|
|
|
Xephyr or Xnest.'
|
2006-07-16 14:22:10 +02:00
|
|
|
sys.exit(0)
|
2006-08-22 10:37:24 +02:00
|
|
|
|
|
|
|
process = DbusProcess()
|
|
|
|
process.start()
|