From cb47f17b31dde57823d46d974e983aa7fe7cad98 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Fri, 11 Aug 2006 09:37:35 +0000 Subject: Move session stuff to his own package --- (limited to 'shell/session') diff --git a/shell/session/Emulator.py b/shell/session/Emulator.py new file mode 100644 index 0000000..d70059d --- /dev/null +++ b/shell/session/Emulator.py @@ -0,0 +1,75 @@ +import logging +import os +import socket +import sys + +from session.Process import Process + +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)) + logging.info('Display %d is already in use. Trying next.' % (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) + +class XephyrProcess(Process): + def __init__(self): + self._display = get_display_number() + cmd = 'Xephyr :%d -ac -screen 640x480' % (self._display) + Process.__init__(self, cmd) + + def get_name(self): + return 'Xephyr' + + def start(self): + Process.start(self) + os.environ['DISPLAY'] = ":%d" % (self._display) + +class XnestProcess(Process): + def __init__(self): + self._display = get_display_number() + cmd = 'Xnest :%d -ac -geometry 640x480' % (self._display) + Process.__init__(self, cmd) + + def get_name(self): + return 'Xnest' + + def start(self): + Process.start(self) + os.environ['DISPLAY'] = ":%d" % (self._display) + +class Emulator: + """The OLPC emulator""" + + def __init__(self): + pass + + def start(self): + try: + process = XephyrProcess() + process.start() + except: + try: + process = XnestProcess() + process.start() + except: + logging.error('Cannot run the emulator. You need to install \ + Xephyr or Xnest.') + sys.exit(0) diff --git a/shell/session/Makefile.am b/shell/session/Makefile.am new file mode 100644 index 0000000..c95c6cb --- /dev/null +++ b/shell/session/Makefile.am @@ -0,0 +1,6 @@ +sugardir = $(pkgdatadir)/shell/session +sugar_PYTHON = \ + __init__.py \ + Emulator.py \ + Process.py \ + Session.py diff --git a/shell/session/Process.py b/shell/session/Process.py new file mode 100644 index 0000000..09d93a7 --- /dev/null +++ b/shell/session/Process.py @@ -0,0 +1,19 @@ +import logging + +import gobject + +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._stdout = result[2] diff --git a/shell/session/Session.py b/shell/session/Session.py new file mode 100644 index 0000000..8131c1d --- /dev/null +++ b/shell/session/Session.py @@ -0,0 +1,82 @@ +import os +import gtk +import gobject +import time +import dbus +import dbus.dbus_bindings + +from sugar.presence import PresenceService +from Shell import Shell +from session.Process import Process +import sugar.env + +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() + addr = addr.strip() + dbus_file.close() + os.environ["DBUS_SESSION_BUS_ADDRESS"] = addr + +class MatchboxProcess(Process): + def __init__(self): + options = '-use_titlebar no' + + kbd_config = os.path.join(sugar.env.get_data_dir(), 'kbdconfig') + options += ' -kbdconfig %s' % kbd_config + + command = 'matchbox-window-manager %s' % options + print command + Process.__init__(self, command) + + def get_name(self): + return 'Matchbox' + +class PresenceServiceProcess(Process): + def __init__(self): + Process.__init__(self, "sugar-presence-service") + + def get_name(self): + return "PresenceService" + + def start(self): + Process.start(self) + bus = dbus.Bus() + ret = False + # Wait for the presence service to start up + while not ret: + ret = dbus.dbus_bindings.bus_name_has_owner(bus._connection, PresenceService.DBUS_SERVICE) + time.sleep(0.2) + +class Session: + """Takes care of running the shell and all the sugar processes""" + def __init__(self, registry): + self._registry = registry + + def start(self): + """Start the session""" + process = DbusProcess() + process.start() + + process = MatchboxProcess() + process.start() + + process = PresenceServiceProcess() + process.start() + + shell = Shell(self._registry) + shell.start() + + try: + gtk.main() + except KeyboardInterrupt: + print 'Ctrl+C pressed, exiting...' diff --git a/shell/session/__init__.py b/shell/session/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/shell/session/__init__.py -- cgit v0.9.1