summaryrefslogtreecommitdiff
path: root/CompizConnection.py
blob: 4fa32e2d2399269334a4e97d6ddae5a67a9e3270 (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
#
# Copyright Eyemagnet Limited
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
# 
# This program 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
# General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
# 
# Author: Kristian F. Erikson <kristian@erikson.dk>
#

import dbus
import threading

_compizAddress = 'org.freedesktop.compiz'   # Listed here in case they change
_compizInterface = 'org.freedesktop.compiz'
_compizObject = '/org/freedesktop/compiz'
_screen0 = '/screen0'
_allscreens = '/allscreens'

        
class CompizConnection( object ):   # Handles everything going back and fourth with compiz
    """
        This class is intended for use mainly within this module, by the classes CompizManager
        and CompizPlugin, to handle the sending and receiving over the Dbus.
    """
    
    def __init__(self):
        self.bus = dbus.SessionBus()
                          
    def getPlugins(self):
        remote_object = self.bus.get_object(_compizAddress, _compizObject)
        return remote_object.getPlugins(_compizInterface)
        
    def get(self, objectLocation):
        remote_object = self.bus.get_object(_compizAddress, _compizObject + objectLocation)
        return remote_object.get(_compizInterface)
        
    def setOption(self, optionLocation, optionValue):
        remote_object = self.bus.get_object(_compizAddress, _compizObject + optionLocation)
        remote_object.set(optionValue, _compizInterface)
        
    def setActivePlugins(self, pluginNames):
        class transmitThread(threading.Thread):
            def run(self):  
                try:
                    threadBus = dbus.SessionBus()   # Note - if this causes issues revert to using dbus-send with os.popen()
                    activePlugins = threadBus.get_object(_compizAddress, _compizObject + '/core/allscreens/active_plugins')
                    activePlugins.set(pluginNames, dbus_interface = _compizInterface)
                except dbus.exceptions.DBusException:   # Catch the exception of compiz not responding
                    pass
        # Start a new thread to send the data so program does not hang
        thread = transmitThread()
        thread.start()
    
    def getList(self, location):
        remote_object = self.bus.get_object(_compizAddress, _compizObject + location)
        return remote_object.list(dbus_interface = _compizInterface)

    def doAction(self, location, *params):
        remote_object = self.bus.get_object(_compizAddress, _compizObject + location)
        
        # Assuming no more that 6 args and always in pairs (I'm not sure this implementation is very good)
        # Perhaps it should be replaced by an impletation using dbus-send via popen or something.
        # Get the arguments and make the call
        if len(params) == 0:
            remote_object.activate(dbus_interface = _compizInterface)
        elif len(params) == 2:
            arg1,arg2 = params
            remote_object.activate(arg1,arg2,dbus_interface = _compizInterface)
        elif len(params) == 4:
            arg1,arg2,arg3,arg4 = params
            remote_object.activate(arg1,arg2,arg3,arg4,dbus_interface = _compizInterface)
        elif len(params) == 6:
            arg1,arg2,arg3,arg4,arg5,arg6 = params
            remote_object.activate(arg1,arg2,arg3,arg4,arg5,arg6,dbus_interface = _compizInterface)
        elif len(params) == 8:
            arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8 = params
            remote_object.activate(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,dbus_interface = _compizInterface)
        elif len(params) == 10:
            arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10 = params
            remote_object.activate(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,dbus_interface = _compizInterface)
        elif len(params) == 12:
            arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11,arg12 = params
            remote_object.activate(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11,arg12,dbus_interface = _compizInterface)

        self.lastSentString = location + ' ' + str(params)

    def getLastSentString(self):        # Used for testing
        return self.lastSentString