import dbus bus = dbus.SessionBus() class DbusBase(object): def getInterface(self, path=''): obj = bus.get_object('org.freedesktop.compiz', '/org/freedesktop/compiz%s' % path) return dbus.Interface(obj, 'org.freedesktop.compiz') class Context(DbusBase): def __init__(self): plugins_iface = self.getInterface('/core/allscreens/active_plugins') active_plugins = plugins_iface.get() plugins_iface = self.getInterface() self.__plugins = {} for plugin in plugins_iface.getPlugins(): plugin = unicode(plugin) self.__plugins[plugin] = Plugin(plugin, (plugin in active_plugins)) self.__plugins['core'] = Plugin('core', True) def __getPlugins(self): return self.__plugins plugins = property(__getPlugins, None) class Action: def __init__(self, value): self.key = value[0] self.button = value[1] self.bell = value[2] self.edge = value[3] self.edge_button = value[4] class Color: def __init__(self, value): self.red = eval('0x' + value[1:3]) self.green = eval('0x' + value[3:5]) self.blue = eval('0x' + value[5:7]) self.alpha = eval('0x' + value[7:]) class Option(DbusBase): def __init__(self, plugin, name, screen): self.__plugin = plugin self.__name = name self.__screen = screen def __getOptionInterface(self): return self.getInterface('/%s/%s/%s' % (self.__plugin, self.__screen, self.__name)) def __getName(self): return self.__name name = property(__getName, None) def __getMetadata(self): option_iface = self.__getOptionInterface() raw = option_iface.getMetadata() metadata = {} metadata['short_desc'] = unicode(raw[0]) metadata['long_desc'] = unicode(raw[1]) metadata['type'] = unicode(raw[2]) if raw[2] == 'list': metadata['list_type'] = unicode(raw[3]) return metadata metadata = property(__getMetadata, None) def __getLimits(self): option_iface = self.__getOptionInterface() metadata = option_iface.getMetadata() datatype = metadata[2] if datatype == 'list': return tuple(metadata[4:]) if datatype == 'string': return tuple(metadata[3]) return tuple(metadata[3:]) limits = property(__getLimits, None) def __getValue(self): option_iface = self.__getOptionInterface() value = option_iface.get() datatype = self.metadata['type'] if datatype == 'action': return Action(value) elif datatype == 'color': return Color(value) return value def __setValue(self, value): option_iface = self.__getOptionInterface() datatype = self.metadata['type'] if datatype == 'action': value = [value.key, value.button, value.bell, value.edge, value.edge_button] if datatype == 'color': #convert back to HTML-like string value = '#%02X%02X%02X%02X' % (value.red, value.green, value.blue, value.alpha) option_iface.set(value) value = property(__getValue, __setValue) class Plugin(DbusBase): def __init__(self, name, active): self.__name = name self.__active = active self.__options = {} if self.__active: for screen in ('allscreens', 'screen0', 'screen1', 'screen2', 'screen3'): options_iface = self.getInterface('/%s/%s' % (name, screen)) try: options = options_iface.list() except: continue if options is not None: self.__options[screen] = {} #workaround for screens with one option if not isinstance(options, tuple): options = [options,] for option in options: option = unicode(option) self.__options[screen][option] = Option(self.name, option, screen) def __getName(self): return self.__name name = property(__getName, None) def __isActive(self): return self.__active active = property(__isActive, None) def __getMetadata(self): if self.__name == 'core': metadata = {} metadata['short_desc'] = 'General' metadata['long_desc'] = 'Options for Compiz itself' return metadata meta_iface = self.getInterface() meta = meta_iface.getPluginMetadata(self.__name) metadata = {} metadata['short_desc'] = unicode(meta[1]) metadata['long_desc'] = unicode(meta[2]) metadata['compat'] = bool(meta[3]) metadata['deps'] = list(meta[4]) metadata['features'] = list(meta[5]) return metadata metadata = property(__getMetadata, None) def __getOptions(self): return self.__options options = property(__getOptions, None)