~ubuntu-branches/ubuntu/vivid/mago/vivid

« back to all changes in this revision

Viewing changes to mago/xlib/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2011-02-08 13:32:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110208133213-m1og7ey0m990chg6
Tags: 0.3+bzr20-0ubuntu1
* debian/rules:
  - updated to debhelper 7
  - use dh_python2 instead of python-central
* debian/pycompat:
  - removed, no longer needed
* debian/control:
  - dropped cdbs and python-central dependencies
* bzr snapshot of the current trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010-2011 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Import pywo library and define additional extensions"""
 
18
 
 
19
from xlib import *
 
20
from time import sleep
 
21
 
 
22
def window_from_pid(pid, command = None):
 
23
    """ Return the first XID matching a PID
 
24
 
 
25
    :param pid: Process ID
 
26
    :param command: Command used to launch the application. Used as fallback 
 
27
                    to find the window using WM_COMMAND
 
28
    """
 
29
    WM.flush()
 
30
 
 
31
    # Lets try 5 secs before aborting
 
32
    for i in range(0,10):
 
33
        for w in WM.windows():
 
34
            try:
 
35
                wm_pid = w.get_property('_NET_WM_PID')
 
36
                if wm_pid and wm_pid.value[0] == pid:
 
37
                    return w
 
38
            # Workaround
 
39
            # BadWindow: <class 'Xlib.error.BadWindow'>: code = 3
 
40
            except xlib.error.BadWindow:
 
41
                pass
 
42
 
 
43
        # Failed with PID try with command
 
44
        if command:
 
45
            for w in WM.windows():
 
46
                try:
 
47
                    wm_cmd = w.get_property('WM_COMMAND')
 
48
                    if wm_cmd and command in wm_cmd.value:
 
49
                        return w
 
50
                # Workaround
 
51
                # BadWindow: <class 'Xlib.error.BadWindow'>: code = 3
 
52
                except xlib.error.BadWindow:
 
53
                    pass
 
54
 
 
55
        sleep(.5)
 
56
    return None