3
//GNOME Do is the legal property of its developers. Please refer to the
4
//COPYRIGHT file distributed with this
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.
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.
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
24
using System.Collections.Generic;
25
using System.Diagnostics;
26
using System.Threading;
31
namespace WindowManager
33
public class WindowListItems
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;
41
public static Window CurrentWindow {
47
public static Window PreviousWindow {
49
return previousWindow;
53
public static List<Window> CurrentApplication {
58
pid = CurrentWindow.Pid;
59
outList = new List<Window> ();
61
foreach (Window w in scrn.WindowsStacked) {
62
if (w.Pid == pid && !w.IsSkipTasklist)
70
public static List<Window> PreviousApplication {
75
pid = PreviousWindow.Pid;
76
outList = new List<Window> ();
78
foreach (Window w in scrn.WindowsStacked) {
79
if (w.Pid == pid && !w.IsSkipTasklist)
87
static WindowListItems ()
89
Gtk.Application.Init (); //why do i HAVE to do this?
91
windowList = new Dictionary<string,List<Window>> ();
93
scrn = Screen.Default;
94
scrn.WindowOpened += OnWindowOpened;
95
scrn.WindowClosed += OnWindowClosed;
96
scrn.ActiveWindowChanged += OnActiveWindowChanged;
99
public static void GetList (out Dictionary<string, List<Window>> winList)
101
winList = new Dictionary<string,List<Window>> ();
103
foreach (KeyValuePair<string, List<Window>> kvp in windowList) {
104
winList.Add (kvp.Key, kvp.Value);
108
public static List<Window> GetApplication (string application)
110
List<Window> windows;
112
windowList.TryGetValue (application, out windows);
117
private static void OnActiveWindowChanged (object o, ActiveWindowChangedArgs args)
120
if (!scrn.ActiveWindow.IsSkipTasklist) {
121
previousWindow = currentWindow;
122
currentWindow = scrn.ActiveWindow;
130
private static void OnWindowOpened (object o, WindowOpenedArgs args)
132
if (args.Window.IsSkipTasklist) return;
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));
141
updateRunner.Start ();
146
private static void OnWindowClosed (object o, WindowClosedArgs args)
148
if (args.Window.IsSkipTasklist) return;
150
// there is a reason, see above
151
GLib.Timeout.Add (2000, delegate {
152
Thread updateRunner = new Thread (new ThreadStart (UpdateList));
154
updateRunner.Start ();
159
private static void UpdateList ()
161
if (!Monitor.TryEnter (listLock))
166
ProcessStartInfo st = new ProcessStartInfo ("ps");
167
st.RedirectStandardOutput = true;
168
st.UseShellExecute = false;
173
foreach (Window w in scrn.WindowsStacked) {
174
if (w.Pid == 0) continue;
176
if (w.IsSkipTasklist) continue;
178
st.Arguments = "c -o cmd --no-headers " + w.Pid;
180
process = Process.Start (st);
182
process.WaitForExit ();
183
if (process.ExitCode != 0) continue;
185
processName = process.StandardOutput.ReadLine ();
186
if (windowList.ContainsKey (processName)) {
187
List<Window> winList;
188
windowList.TryGetValue (processName, out winList);
191
List<Window> winList = new List<Window> ();
193
windowList.Add (processName, winList);
198
Monitor.Exit (listLock);
201
public static event ListUpdatedDelegate ListUpdated;
203
public delegate void ListUpdatedDelegate ();