• attachment:compiz-send.py of Plugins/Dbus
  • Immutable Page
  • Info
  • Attachments

Attachment 'compiz-send.py'

Download

   1 #!/usr/bin/env python
   2 
   3 import sys, dbus, subprocess
   4 
   5 METHOD_ACTIVATE, METHOD_LIST = range(2)
   6 
   7 booldict = {'true': True, 'false': False}
   8 
   9 
  10 def destrify(s):
  11     '''Attempt to turn a string into an int, float, or bool'''
  12     value = None
  13     try:
  14         i = int(s, 0)
  15     except ValueError:
  16         try:
  17             f = float(s)
  18         except ValueError:
  19             value = booldict.get(s.lower(), s)
  20         else:
  21             value = f
  22     else:
  23         value = i
  24     return value
  25 
  26 
  27 # Getting root window ID
  28 
  29 try:
  30     rootwin = subprocess.Popen(['xwininfo', '-root'],
  31                                stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
  32 except OSError:
  33     raise SystemExit('Error: xwininfo not present')
  34 
  35 try:
  36     rootwin_id = int(rootwin.split()[3], 0)
  37 except IndexError:
  38     raise SystemExit('Error: unexpectedly short output from xwininfo')
  39 except ValueError:
  40     raise SystemExit('Error: unable to convert "%s" to int', rootwin.split()[3])
  41 
  42 # dbus call setup
  43 service = interface = 'org.freedesktop.compiz'
  44 session_bus = dbus.SessionBus()
  45 
  46 # Argument parsing
  47 if len(sys.argv) < 3:
  48     if len(sys.argv) == 2:
  49         method = METHOD_LIST
  50     else:
  51         proxy = session_bus.get_object(service, "/org/freedesktop/compiz")
  52         print "Available methods:"
  53         print proxy.Introspect().replace("<node", "\n<node")
  54         sys.exit(0)
  55 else:
  56     method = METHOD_ACTIVATE
  57 
  58 plugin = sys.argv[1]
  59 if method == METHOD_ACTIVATE:
  60     action = sys.argv[2]
  61 
  62 args = ['root', rootwin_id]
  63 
  64 extra_len = len(sys.argv[3:])
  65 if extra_len >= 2 and extra_len % 2 == 0:
  66     for k, v in zip(sys.argv[3::2], sys.argv[4::2]):
  67         args.append(k)
  68         args.append(destrify(v))
  69 
  70 
  71 # D-Bus call
  72 
  73 if method == METHOD_ACTIVATE:
  74     proxy = session_bus.get_object(
  75         service, '/org/freedesktop/compiz/%s/allscreens/%s' %(plugin, action))
  76     obj = dbus.Interface(proxy, interface)
  77     obj.activate(*args)
  78 elif method == METHOD_LIST:
  79     proxy = session_bus.get_object(
  80         service, '/org/freedesktop/compiz/%s/allscreens' %(plugin))
  81     obj = dbus.Interface(proxy, interface)
  82     print '\n'.join(obj.list())

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2010-01-16 15:52:46, 2.1 KB) [[attachment:compiz-send.py]]

You are not allowed to attach a file to this page.