Launch matchbox and other activities, use glib to spawn processes

This commit is contained in:
Dan Williams 2006-04-25 15:21:24 -04:00
parent d5a9ca986d
commit e5cd418e7e

View File

@ -16,76 +16,17 @@ import gc
import socket
import types
import select
import string
import time
sys.path.append(os.getcwd())
sys.path.append('../shell/example-activity/')
import activity
XEPHYR_PATH = "/usr/bin/Xephyr"
MATCHBOX_PATH = "/usr/bin/matchbox-window-manager"
def getfd(filespec, readOnly = 0):
if type(filespec) == types.IntType:
return (filespec, False)
if filespec == None:
filespec = "/dev/null"
flags = os.O_RDWR | os.O_CREAT
if (readOnly):
flags = os.O_RDONLY
fd = os.open(filespec, flags, 0644)
return (fd, True)
def exec_with_redirect(cmd, argv, display, stdin=0, stdout=1, stderr=2, setpgrp=True):
cmd = os.path.abspath(cmd)
if not os.access (cmd, os.X_OK):
raise RuntimeError(cmd + " can not be run")
stdout_opened = False
stderr_opened = False
(stdin, stdin_opened) = getfd(stdin)
if stdout == stderr:
(stdout, stdout_opened) = getfd(stdout)
stderr = stdout
else:
(stdout, stdout_opened) = getfd(stdout)
(stderr, stderr_opened) = getfd(stderr)
childpid = os.fork()
if (not childpid):
# Become leader of a new process group if requested
if setpgrp:
os.setpgrp()
if stdin != 0:
os.dup2(stdin, 0)
os.close(stdin)
if stdout != 1:
os.dup2(stdout, 1)
if stdout != stderr:
os.close(stdout)
if stderr != 2:
os.dup2(stderr, 2)
os.close(stderr)
try:
if display:
os.environ['DISPLAY'] = "0:%d" % display
os.execv(cmd, argv)
except OSError, e:
print "Could not execute command '%s'. Reason: %s" % (cmd, e)
sys.exit(1)
# Close any files we may have opened
if stdin_opened:
os.close(stdin)
if stdout_opened:
os.close(stdout)
if stderr != stdout and stderr_opened:
os.close(stderr)
return childpid
class LegacyActivity(activity.Activity):
def __init__(self, args):
@ -93,38 +34,58 @@ class LegacyActivity(activity.Activity):
self._act_name = os.path.basename(args[1])
self._display = 5
self._args = args[1:]
self._act_pid = None
self._matchbox_pid = None
self._xephyr_pid = None
def _xephyr_function(self, pid, condition, data=None):
print "Xephyr: PID: %d, condition: %s" % (pid, condition)
def _matchbox_function(self, pid, condition, data=None):
print "WM: PID: %d, condition: %s" % (pid, condition)
def _act_function(self, pid, condition, data=None):
print "ACT: PID: %d, condition: %s" % (pid, condition)
if condition == 0:
self._act_pid = None
gtk.main_quit()
def __key_press_event_cb(self, widget, event):
print event
def _start(self):
cmd = XEPHYR_PATH
args = []
args.append(XEPHYR_PATH)
args.append(":%d" % self._display)
args.append("-ac")
args.append("-parent")
args.append("%d" % self._plug.get_id())
args.append("-host-cursor")
self._xephyr_pid = exec_with_redirect(cmd, args, None, None)
args = string.split("%s :%d -ac -parent %d -host-cursor" % (XEPHYR_PATH, self._display, self._plug.get_id()))
(self._xephyr_pid, a, b, c) = gobject.spawn_async(args, standard_output=sys.stdout, standard_error=sys.stderr)
self._xephyr_watch = gobject.child_watch_add(self._xephyr_pid, self._xephyr_function)
cmd = os.path.abspath(self._args[0])
args = [cmd]
envp = ["DISPLAY=:%d" % self._display]
envp.append("INPUTRC=/etc/inputrc")
envp.append("XMODIFIERS=@im=SCIM")
envp.append("GTK_IM_MODULE=scim")
try:
envp.append("LANG=%s" % os.environ['LANG'])
except:
envp.append("LANG=en_US.UTF-8")
args = string.split("%s" % MATCHBOX_PATH)
(self._matchbox_pid, a, b, c) = gobject.spawn_async(args, envp=envp, standard_output=sys.stdout, standard_error=sys.stderr)
gobject.child_watch_add(self._matchbox_pid, self._matchbox_function)
args = [os.path.abspath(self._args[0])]
for arg in self._args[1:]:
args.append(arg)
self._act_pid = exec_with_redirect(cmd, args, self._display, None)
self._act_watch = gobject.child_watch_add(self._act_pid, self._act_function)
(self._act_pid, a, b, c) = gobject.spawn_async(args, envp=envp, standard_output=sys.stdout, standard_error=sys.stderr)
gobject.child_watch_add(self._act_pid, self._act_function)
def activity_on_connected_to_shell(self):
print "act %d: in activity_on_connected_to_shell" % self.activity_get_id()
self.activity_set_tab_text(self._act_name)
self._plug = self.activity_get_gtk_plug()
self._plug.add_events(gtk.gdk.ALL_EVENTS_MASK)
self._plug.connect("key-press-event", self.__key_press_event_cb)
self._plug.show()
self._start()
self._plug.grab_focus()
def activity_on_disconnected_from_shell(self):
print "act %d: in activity_on_disconnected_from_shell"%self.activity_get_id()
@ -140,10 +101,21 @@ class LegacyActivity(activity.Activity):
def activity_on_got_focus(self):
print "act %d: in activity_on_got_focus"%self.activity_get_id()
self._plug.grab_focus()
def cleanup(self):
os.kill(self._xephyr_pid, 9)
os.kill(self._act_pid, 9)
try:
if self._act_pid:
os.kill(self._act_pid, 9)
time.sleep(0.2)
if self._xephyr_pid:
os.kill(self._xephyr_pid, 9)
time.sleep(0.2)
if self._matchbox_pid:
os.kill(self._matchbox_pid, 9)
time.sleep(0.2)
except OSError, e:
pass
def run(self):
try: