Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/emulator.py
blob: 664e3795df65ba0ca6ab76ca3694ef549dd852bd (plain)
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
# 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)

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]

class MatchboxProcess(Process):
    def __init__(self):
        kbd_config = os.path.join(env.get_data_dir(), 'kbdconfig')
        options = '-kbdconfig %s ' % kbd_config

        options += '-use_titlebar no '
        options += '-theme olpc '

        command = 'matchbox-window-manager %s ' % options
        Process.__init__(self, command)
    
    def get_name(self):
        return 'Matchbox'

class XephyrProcess(Process):
    def __init__(self, fullscreen):
        self._display = get_display_number()
        cmd = 'Xephyr :%d -ac ' % (self._display)
        if fullscreen:
            cmd += '-fullscreen '
        else:
            cmd += '-screen 1200x900'
        Process.__init__(self, cmd)
        
    def get_name(self):
        return 'Xephyr'

    def start(self, standard_output=False):
        Process.start(self)
        os.environ['DISPLAY'] = ":%d" % (self._display)
        os.environ['SUGAR_XEPHYR_PID'] = '%d' % self.pid


class XnestProcess(Process):
    def __init__(self):
        self._display = get_display_number()
        cmd = 'Xnest :%d -ac -geometry 800x600' % (self._display) 
        Process.__init__(self, cmd)
        
    def get_name(self):
        return 'Xnest'

    def start(self, standard_output=False):
        Process.start(self)
        os.environ['DISPLAY'] = ":%d" % (self._display)

class Emulator(object):
    """The OLPC emulator"""
    def __init__(self, fullscreen):
        self._fullscreen = fullscreen

    def start(self):
        try:
            process = XephyrProcess(self._fullscreen)
            process.start()
        except:
            try:
                process = XnestProcess()
                process.start()
            except:
                print 'Cannot run the emulator. You need to install \
                       Xephyr or Xnest.'
                sys.exit(0)

        process = MatchboxProcess()
        process.start()