PDA

View Full Version : [HOWTO] Remote-access compiz over dbus using python


RYX
July 30th, 2007, 04:49 PM
This is a small example of accessing compiz over dbus. I wrote it a while ago and just stepped across it on my hdd. Maybe it is of help to someone.

#!/usr/bin/env python

# A little test of connecting compiz from python over dbus ...
# Should be a good base for creating a simple settings-manager in python.
#
# by RYX (Rico Pfaus) 2007 - can be used freely (as in free beer)
#

import dbus

# init service/interface
COMPIZ_DBUS_SERVICE = 'org.freedesktop.compiz'
COMPIZ_DBUS_INTERFACE = 'org.freedesktop.compiz'
COMPIZ_DBUS_OBJECT = '/org/freedesktop/compiz'

# get session bus
bus = dbus.SessionBus()

# utility function to call methods in compiz
def compiz_call(obj_path, func_name, *args):
"""Call method in obj_path in compiz-service over dbus. The compiz-object's
path does not need to be passed."""
path = COMPIZ_DBUS_OBJECT
if obj_path:
path += '/' + obj_path
obj = bus.get_object(COMPIZ_DBUS_SERVICE, path)
iface = dbus.Interface(obj, COMPIZ_DBUS_INTERFACE)
func = getattr(iface, func_name)
if func:
try:
return func(*args)
except Exception, ex:
print "compiz_call(): Dbus-error: " + str(ex)
return None

# test getting various things
# get list of plugins
print "/org/freedesktop/compiz.getPlugins:"
print compiz_call('', 'getPlugins')

# get metadata for a specific plugin
print "\n/org/freedesktop/compiz.getPluginMetadata('cube'):"
print compiz_call('', 'getPluginMetadata', 'cube')

# get all core-options that apply to all screens
print "\n/org/freedesktop/compiz/core/allscreens.list:"
print compiz_call('core/allscreens', 'list')

# get metadata for a specific option
print "\n/org/freedesktop/compiz/core/screen0/lighting.getMetadata:"
print compiz_call('core/screen0/lighting', 'getMetadata')

# get the value of an option
print "\n/org/freedesktop/compiz/core/screen0/lighting.get:"
print compiz_call('core/screen0/lighting', 'get')

# get all options for a plugin
print "\n/org/freedesktop/compiz/decoration/allscreens.list:"
print compiz_call('decoration/allscreens', 'list')

# get the value of a plugin's option
print "\n/org/freedesktop/compiz/decoration/allscreens/shadow_color.get:"
print compiz_call('decoration/allscreens/shadow_color', 'get')


:)

Kristian
August 18th, 2007, 02:17 AM
The "license" doesn't allow anyone to actually modify it and release the modifications, which one would assume is the goal of a skeleton like this. Can you be convinced to release this under a proper license, GPL, MIT, or any other fully free license?

By the way, you may want to include an activation example, like:

compiz_call('ezoom/allscreens/pan_left','activate','root',0x13a)

As how arguments are treated and such is not always as intuitive as one would think with DBus.