2010-07-21 22:17:34 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# Terminator by Chris Jones <cmsj@tenshu.net>
|
|
|
|
# GPL v2 only
|
|
|
|
"""ipc.py - DBus server and API calls"""
|
|
|
|
|
|
|
|
import gtk
|
|
|
|
import dbus.service
|
|
|
|
from dbus.exceptions import DBusException
|
|
|
|
import dbus.glib
|
|
|
|
from borg import Borg
|
|
|
|
from terminator import Terminator
|
|
|
|
from config import Config
|
2010-07-22 08:24:47 +00:00
|
|
|
from util import dbg
|
2010-07-21 22:17:34 +00:00
|
|
|
|
|
|
|
CONFIG = Config()
|
|
|
|
if not CONFIG['dbus']:
|
|
|
|
# The config says we are not to load dbus, so pretend like we can't
|
2010-07-22 08:24:47 +00:00
|
|
|
dbg('dbus disabled')
|
2010-07-21 22:17:34 +00:00
|
|
|
raise ImportError
|
|
|
|
|
|
|
|
BUS_BASE = 'net.tenshu.Terminator'
|
|
|
|
BUS_PATH = '/net/tenshu/Terminator'
|
|
|
|
try:
|
|
|
|
# Try and include the X11 display name in the dbus bus name
|
2010-07-22 08:24:47 +00:00
|
|
|
DISPLAY = hex(hash(gtk.gdk.get_display())).replace('-', '_')
|
2010-07-21 22:17:34 +00:00
|
|
|
BUS_NAME = '%s%s' % (BUS_BASE, DISPLAY)
|
|
|
|
except:
|
|
|
|
BUS_NAME = BUS_BASE
|
|
|
|
|
|
|
|
class DBusService(Borg, dbus.service.Object):
|
|
|
|
"""DBus Server class. This is implemented as a Borg"""
|
|
|
|
bus_name = None
|
2011-08-25 21:10:04 +00:00
|
|
|
bus_path = None
|
2010-07-21 22:17:34 +00:00
|
|
|
terminator = None
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Class initialiser"""
|
2010-07-22 08:24:47 +00:00
|
|
|
Borg.__init__(self, self.__class__.__name__)
|
2010-07-21 22:17:34 +00:00
|
|
|
self.prepare_attributes()
|
|
|
|
dbus.service.Object.__init__(self, self.bus_name, BUS_PATH)
|
|
|
|
|
|
|
|
def prepare_attributes(self):
|
|
|
|
"""Ensure we are populated"""
|
|
|
|
if not self.bus_name:
|
2010-07-22 08:24:47 +00:00
|
|
|
dbg('Checking for bus name availability: %s' % BUS_NAME)
|
2010-07-21 22:17:34 +00:00
|
|
|
bus = dbus.SessionBus()
|
|
|
|
proxy = bus.get_object('org.freedesktop.DBus',
|
|
|
|
'/org/freedesktop/DBus')
|
|
|
|
flags = 1 | 4 # allow replacement | do not queue
|
|
|
|
if not proxy.RequestName(BUS_NAME, dbus.UInt32(flags)) in (1, 4):
|
2010-07-22 08:24:47 +00:00
|
|
|
dbg('bus name unavailable: %s' % BUS_NAME)
|
2010-07-21 22:17:34 +00:00
|
|
|
raise dbus.exceptions.DBusException(
|
|
|
|
"Couldn't get DBus name %s: Name exists" % BUS_NAME)
|
|
|
|
self.bus_name = dbus.service.BusName(BUS_NAME,
|
|
|
|
bus=dbus.SessionBus())
|
2011-08-25 21:10:04 +00:00
|
|
|
if not self.bus_path:
|
|
|
|
self.bus_path = BUS_PATH
|
2010-07-21 22:17:34 +00:00
|
|
|
if not self.terminator:
|
|
|
|
self.terminator = Terminator()
|
|
|
|
|
2011-12-29 20:39:09 +00:00
|
|
|
@dbus.service.method(BUS_NAME, in_signature='a{ss}')
|
|
|
|
def new_window(self, options=dbus.Dictionary()):
|
2010-07-21 22:17:34 +00:00
|
|
|
"""Create a new Window"""
|
2011-12-29 20:39:09 +00:00
|
|
|
dbg('dbus method called: new_window with parameters %s'%(options))
|
|
|
|
oldopts = self.terminator.config.options_get()
|
|
|
|
oldopts.__dict__ = options
|
|
|
|
self.terminator.config.options_set(oldopts)
|
|
|
|
self.terminator.create_layout(oldopts.layout)
|
2010-07-21 22:17:34 +00:00
|
|
|
self.terminator.layout_done()
|
2011-12-29 19:02:11 +00:00
|
|
|
|
2010-07-21 22:17:34 +00:00
|
|
|
|
2011-08-24 21:39:22 +00:00
|
|
|
@dbus.service.method(BUS_NAME)
|
|
|
|
def terminal_hsplit(self, uuid=None):
|
|
|
|
"""Split a terminal horizontally, by UUID"""
|
2011-08-25 21:10:04 +00:00
|
|
|
return self.terminal_split(uuid, True)
|
2011-08-24 21:39:22 +00:00
|
|
|
|
|
|
|
@dbus.service.method(BUS_NAME)
|
|
|
|
def terminal_vsplit(self, uuid=None):
|
|
|
|
"""Split a terminal vertically, by UUID"""
|
2011-08-25 21:10:04 +00:00
|
|
|
return self.terminal_split(uuid, False)
|
2011-08-24 21:39:22 +00:00
|
|
|
|
|
|
|
def terminal_split(self, uuid, horiz):
|
|
|
|
"""Split a terminal horizontally or vertically, by UUID"""
|
|
|
|
dbg('dbus method called: terminal_hsplit')
|
|
|
|
if not uuid:
|
|
|
|
return "ERROR: No UUID specified"
|
|
|
|
terminal = self.terminator.find_terminal_by_uuid(uuid)
|
|
|
|
if not terminal:
|
|
|
|
return "ERROR: Terminal with supplied UUID not found"
|
|
|
|
if horiz:
|
|
|
|
terminal.key_split_horiz()
|
|
|
|
else:
|
|
|
|
terminal.key_split_vert()
|
|
|
|
|
2011-08-25 21:10:04 +00:00
|
|
|
@dbus.service.method(BUS_NAME)
|
|
|
|
def get_terminals(self, uuid):
|
|
|
|
"""Return a list of all the terminals"""
|
|
|
|
return [x.uuid.urn for x in self.terminator.terminals]
|
|
|
|
|
2010-07-21 22:17:34 +00:00
|
|
|
def with_proxy(func):
|
|
|
|
"""Decorator function to connect to the session dbus bus"""
|
2010-07-22 08:24:47 +00:00
|
|
|
dbg('dbus client call: %s' % func.func_name)
|
2010-07-21 22:17:34 +00:00
|
|
|
def _exec(*args, **argd):
|
|
|
|
bus = dbus.SessionBus()
|
|
|
|
proxy = bus.get_object(BUS_NAME, BUS_PATH)
|
|
|
|
func(proxy, *args, **argd)
|
|
|
|
return _exec
|
|
|
|
|
|
|
|
@with_proxy
|
2011-12-29 20:39:09 +00:00
|
|
|
def new_window(session, options):
|
2010-07-21 22:17:34 +00:00
|
|
|
"""Call the dbus method to open a new window"""
|
2011-12-29 20:39:09 +00:00
|
|
|
session.new_window(options)
|
2010-07-21 22:17:34 +00:00
|
|
|
|
2011-08-24 21:39:22 +00:00
|
|
|
@with_proxy
|
|
|
|
def terminal_hsplit(session, uuid):
|
|
|
|
"""Call the dbus method to horizontally split a terminal"""
|
|
|
|
session.terminal_hsplit(uuid)
|
|
|
|
|
|
|
|
@with_proxy
|
|
|
|
def terminal_vsplit(session, uuid):
|
|
|
|
"""Call the dbus method to vertically split a terminal"""
|
2011-08-25 21:10:04 +00:00
|
|
|
print session.terminal_vsplit(uuid)
|
|
|
|
|
|
|
|
@with_proxy
|
|
|
|
def get_terminals(session, uuid):
|
|
|
|
"""Call the dbus method to return a list of all terminals"""
|
|
|
|
print '\n'.join(session.get_terminals(uuid))
|
2011-08-24 21:39:22 +00:00
|
|
|
|