File: WindowWatcher.py

package info (click to toggle)
earcandy 0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 392 kB
  • ctags: 764
  • sloc: python: 3,106; xml: 15; makefile: 7
file content (179 lines) | stat: -rw-r--r-- 6,213 bytes parent folder | download
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
# Ear Candy - Pulseaduio sound managment tool
# Copyright (C) 2008 Jason Taylor
# 
# 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, see <http://www.gnu.org/licenses/>.

# based on original code by by RYX (aka Rico Pfaus)
# http://thegraveyard.org/files/kapager-0.0.3.py

import wnck
import gobject
import os
import gtk
from DesktopFiles import DesktopFiles

class Application():
    def __init__(self, pid, command, name, desktop_files, icon):
        self.command = os.path.basename(command.lower().strip())
        self.name = name.lower().strip()
        self.category = ""
        self.icon = icon
        self.icon_name = ""
        self.description = name

        # current application window info
        self.pid = pid
        self.window_name = ""
        self.x = 0
        self.y = 0
        self.fullscreen = False
        
        self.__match_desktop(desktop_files)
        return

    def __match_desktop(self,  desktop_files):

        #print "checking desktop files for match....", self.name
        # Try looking up the application list
        for a in desktop_files:
            ex = a.get_exec_array()[0].lower()

            # Check for pulse audio settings first...
            if a.get("X-PulseAudio-Properties"):
                self.category = a.get("X-PulseAudio-Properties")
                break

            if a.get("X-GNOME-Bugzilla-Product") == self.name or ex == self.name or ex == self.command or a.get("Name").lower() == self.name or a.get("Name").lower() == self.command or ex + ".real" == self.command :
                self.icon_name = a.get("Icon")
                self.description = a.get("Name")
                self.category = self.__desktop_categories_to_category( a.get("Categories") )
                #print "category : " + (self.category or "<unknown>")
                break

        if self.command == "skype.real":
            self.category = "phone"
            #print "category : " + self.category
            

    def __desktop_categories_to_category(self, categories):
        if categories:
            for category in categories:
                if "Telephony" in categories or "InstantMessaging" in categories:
                    return "phone"
                if "Music" in categories:
                    return "music"
                if "Video" in categories:
                    return "video"
                if "AudioVideo" in categories:
                    return "music"             
    
        return ""

class WindowWatcher():
    
    def __init__(self, callback = None, existing=False):
        self.screen = wnck.screen_get_default()
        self.screen.connect("active_window_changed", self.active_window_changed)
        self.screen.connect("window_opened", self.window_opened)
        self.callback = callback
        self.current_window = None
        self.applications = {}
        self.desktop_files = DesktopFiles().read_all()   

        for win in self.screen.get_windows():
            self.update("exists", win)

    def send_window_key_press_from_pid(self, pid, key):
        for win in self.screen.get_windows():
            app = win.get_application()
            app_name = app.get_name()
            if pid == win.get_pid():       
                
                break;


    def get_command(self, pid):
        command = str(pid)
        try:
            # Try and get command from PID
            f = open("/proc/%s/cmdline" % pid, "r")
            command = f.readline()[:-1]
            f.close()
        except:
            pass
        return command

    def window_opened(self, screen, win):
        if win and win.get_window_type() != wnck.WINDOW_DOCK: # ignore docks
            #print            
            #print "window opened"
            app = win.get_application ()
            app_name = app.get_name()
            pid = win.get_pid()
            command = self.get_command(pid)
            category = ""

            if not self.applications.has_key(command):
                a = Application(pid, command, app_name, self.desktop_files, win.get_icon ())
                self.applications[command] = a
            
            self.update("open", win)
            win.connect("geometry-changed", self.geometry_changed)

    def active_window_changed(self, screen, old_window):
        win = screen.get_active_window ()
        self.update("active", win)        

    def update(self, state, win):
        if win and win.get_window_type() != wnck.WINDOW_DOCK: # ignore docks
                
            #try:
                app = win.get_application ()
                app_name = app.get_name()
                pid = win.get_pid()

                command = self.get_command(pid)

                application = None
                application = self.applications[command]
                application.pid = pid

                geom = win.get_geometry()
                x = float(geom[1])
                y = float(geom[0])
                w = float(geom[2])
                h = float(geom[3])

                if win.get_name():
                    application.window_name =  win.get_name()

                application.x = x+h/2
                application.y = y+w/2
                application.fullscreen = win.is_fullscreen () or win.is_maximized ()
                application.icon = win.get_icon ()

                
                if self.callback:
                    gobject.idle_add(self.callback, application, state)
            #except:
            #    print "error reading window", win

    def geometry_changed(self, win):
        self.update("geo", win)


if __name__ == '__main__':
    ww = WindowWatcher()