~cszikszoy/do-plugins/pastebin

« back to all changes in this revision

Viewing changes to WindowManager/src/WindowListItems.cs

  • Committer: David Siegel
  • Date: 2008-06-02 16:09:11 UTC
  • Revision ID: dave@x-20080602160911-5bc56q3okv27pg7y
Added GNOME Terminal plugin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// WindowListItems.cs
 
2
//
 
3
//GNOME Do is the legal property of its developers. Please refer to the
 
4
//COPYRIGHT file distributed with this
 
5
//source distribution.
 
6
//
 
7
// This program is free software; you can redistribute it and/or modify
 
8
// it under the terms of the GNU General Public License as published by
 
9
// the Free Software Foundation; either version 2 of the License, or
 
10
// (at your option) any later version.
 
11
//
 
12
// This program is distributed in the hope that it will be useful,
 
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
15
// GNU General Public License for more details.
 
16
// 
 
17
// You should have received a copy of the GNU General Public License
 
18
// along with this program; if not, write to the Free Software
 
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
20
//
 
21
//
 
22
 
 
23
using System;
 
24
using System.Collections.Generic;
 
25
using System.Diagnostics;
 
26
using System.Threading;
 
27
 
 
28
using Do.Universe;
 
29
using Wnck;
 
30
 
 
31
namespace WindowManager
 
32
{
 
33
        public class WindowListItems
 
34
        {
 
35
                private static Dictionary<string, List<Window>> windowList;
 
36
                private static Wnck.Screen scrn;
 
37
                private static object listLock = new object ();
 
38
                private static Window currentWindow;
 
39
                private static Window previousWindow;
 
40
                
 
41
                public static Window CurrentWindow {
 
42
                        get {
 
43
                                return currentWindow;
 
44
                        }
 
45
                }
 
46
                
 
47
                public static Window PreviousWindow {
 
48
                        get {
 
49
                                return previousWindow;
 
50
                        }
 
51
                }
 
52
                
 
53
                public static List<Window> CurrentApplication {
 
54
                        get {
 
55
                                int pid;
 
56
                                List<Window> outList;
 
57
                                
 
58
                                pid = CurrentWindow.Pid;
 
59
                                outList = new List<Window> ();
 
60
                                
 
61
                                foreach (Window w in scrn.WindowsStacked) {
 
62
                                        if (w.Pid == pid && !w.IsSkipTasklist)
 
63
                                                outList.Add(w);
 
64
                                }
 
65
                                
 
66
                                return outList;
 
67
                        }
 
68
                }
 
69
                
 
70
                public static List<Window> PreviousApplication {
 
71
                        get {
 
72
                                int pid;
 
73
                                List<Window> outList;
 
74
                                
 
75
                                pid = PreviousWindow.Pid;
 
76
                                outList = new List<Window> ();
 
77
                                
 
78
                                foreach (Window w in scrn.WindowsStacked) {
 
79
                                        if (w.Pid == pid && !w.IsSkipTasklist)
 
80
                                                outList.Add(w);
 
81
                                }
 
82
                                
 
83
                                return outList;
 
84
                        }
 
85
                }
 
86
                
 
87
                static WindowListItems ()
 
88
                {
 
89
                        Gtk.Application.Init (); //why do i HAVE to do this?
 
90
                        
 
91
                        windowList = new Dictionary<string,List<Window>> ();
 
92
                        
 
93
                        scrn = Screen.Default;
 
94
                        scrn.WindowOpened        += OnWindowOpened;
 
95
                        scrn.WindowClosed        += OnWindowClosed;
 
96
                        scrn.ActiveWindowChanged += OnActiveWindowChanged;
 
97
                }
 
98
                        
 
99
                public static void GetList (out Dictionary<string, List<Window>> winList)
 
100
                {
 
101
                        winList = new Dictionary<string,List<Window>> ();
 
102
                        
 
103
                        foreach (KeyValuePair<string, List<Window>> kvp in windowList) {
 
104
                                winList.Add (kvp.Key, kvp.Value);
 
105
                        }
 
106
                }
 
107
                
 
108
                public static List<Window> GetApplication (string application)
 
109
                {
 
110
                        List<Window> windows;
 
111
                        
 
112
                        windowList.TryGetValue (application, out windows);
 
113
                        
 
114
                        return windows;
 
115
                }
 
116
                
 
117
                private static void OnActiveWindowChanged (object o, ActiveWindowChangedArgs args)
 
118
                {
 
119
                        try {
 
120
                                if (!scrn.ActiveWindow.IsSkipTasklist) {
 
121
                                        previousWindow = currentWindow;
 
122
                                        currentWindow = scrn.ActiveWindow;
 
123
                                }
 
124
                        }
 
125
                        catch {
 
126
                                
 
127
                        }
 
128
                }
 
129
                
 
130
                private static void OnWindowOpened (object o, WindowOpenedArgs args)
 
131
                {
 
132
                        if (args.Window.IsSkipTasklist) return;
 
133
 
 
134
                        // there is a reason, but its stupid and unimportant.  Wnck reports a window
 
135
                        // open/close event if things minimize/restore really fast.  The window also gets
 
136
                        // temporarily dropped from the list for some reason.  To account for this, a 2 second
 
137
                        // lag on our updates lets Wnck unfuck itself.
 
138
                        GLib.Timeout.Add (2000, delegate {
 
139
                                Thread updateRunner = new Thread (new ThreadStart (UpdateList));
 
140
                                
 
141
                                updateRunner.Start ();
 
142
                                return false;
 
143
                        });
 
144
                }
 
145
                
 
146
                private static void OnWindowClosed (object o, WindowClosedArgs args)
 
147
                {
 
148
                        if (args.Window.IsSkipTasklist) return;
 
149
 
 
150
                        // there is a reason, see above
 
151
                        GLib.Timeout.Add (2000, delegate {
 
152
                                Thread updateRunner = new Thread (new ThreadStart (UpdateList));
 
153
                                
 
154
                                updateRunner.Start ();
 
155
                                return false;
 
156
                        });
 
157
                }
 
158
                
 
159
                private static void UpdateList ()
 
160
                {
 
161
                        if (!Monitor.TryEnter (listLock))
 
162
                                return;
 
163
                        
 
164
                        windowList.Clear ();
 
165
                        
 
166
                        ProcessStartInfo st = new ProcessStartInfo ("ps");
 
167
                        st.RedirectStandardOutput = true;
 
168
                        st.UseShellExecute = false;
 
169
                        
 
170
                        Process process;
 
171
                        string processName;
 
172
                        
 
173
                        foreach (Window w in scrn.WindowsStacked) {
 
174
                                if (w.Pid == 0) continue;
 
175
                                
 
176
                                if (w.IsSkipTasklist) continue;
 
177
                                
 
178
                                st.Arguments = "c -o cmd --no-headers " + w.Pid;
 
179
                                
 
180
                                process = Process.Start (st);
 
181
                                
 
182
                                process.WaitForExit ();
 
183
                                if (process.ExitCode != 0) continue;
 
184
                                
 
185
                                processName = process.StandardOutput.ReadLine ();
 
186
                                if (windowList.ContainsKey (processName)) {
 
187
                                        List<Window> winList;
 
188
                                        windowList.TryGetValue (processName, out winList);
 
189
                                        winList.Add (w);
 
190
                                } else {
 
191
                                        List<Window> winList = new List<Window> ();
 
192
                                        winList.Add (w);
 
193
                                        windowList.Add (processName, winList);
 
194
                                }
 
195
                        }
 
196
                        
 
197
                        ListUpdated ();
 
198
                        Monitor.Exit (listLock);
 
199
                }
 
200
                
 
201
                public static event ListUpdatedDelegate ListUpdated;
 
202
                
 
203
                public delegate void ListUpdatedDelegate ();
 
204
        }
 
205
}