30
30
public static class Util
32
static IEnumerable<string> BadPrefixes {
41
yield return "python";
42
yield return "python2.4";
43
yield return "python2.5";
47
static List<Application> application_list;
48
static bool application_list_update_needed;
50
static Dictionary<int, string> exec_lines = new Dictionary<int, string> ();
51
static DateTime last_update = new DateTime (0);
55
Wnck.Screen.Default.WindowClosed += delegate {
56
application_list_update_needed = true;
59
Wnck.Screen.Default.WindowOpened += delegate {
60
application_list_update_needed = true;
63
Wnck.Screen.Default.ApplicationOpened += delegate {
64
application_list_update_needed = true;
67
Wnck.Screen.Default.ApplicationClosed += delegate {
68
application_list_update_needed = true;
33
/// Returns a list of applications that match an exec string
73
/// Returns a list of all applications on the default screen
35
/// <param name="exec">
36
/// A <see cref="System.String"/>
39
/// A <see cref="List"/>
76
/// A <see cref="Application"/> array
41
public static List<Application> GetApplicationList (string exec)
78
public static List<Application> GetApplications ()
43
exec = exec.Split (' ')[0];
44
List<Application> apps = new List<Application> ();
45
Application out_app = null;
46
foreach (string dir in Directory.GetDirectories ("/proc")) {
49
try { pid = Convert.ToInt32 (dir.Substring (6)); }
52
string exec_line = CmdLineForPid (pid);
53
if (string.IsNullOrEmpty (exec_line))
56
if (exec_line.Contains (exec)) {
57
foreach (Application app in GetApplications ()) {
59
if (app.Windows.Select (win => !win.IsSkipTasklist).Any ())
80
if (application_list == null || application_list_update_needed) {
81
application_list = new List<Application> ();
82
foreach (Window w in Wnck.Screen.Default.Windows) {
83
if (!application_list.Contains (w.Application))
84
application_list.Add (w.Application);
87
return application_list;
81
99
public static string CmdLineForPid (int pid)
83
101
StreamReader reader;
102
string cmdline = null;
86
105
string procPath = new [] { "/proc", pid.ToString (), "cmdline" }.Aggregate (Path.Combine);
87
106
reader = new StreamReader (procPath);
88
cmdline = reader.ReadLine ();
107
cmdline = reader.ReadLine ().Replace (Convert.ToChar (0x0), ' ');
90
109
reader.Dispose ();
91
} catch { return null; }
97
/// Returns a list of all applications on the default screen
116
/// Returns a list of applications that match an exec string
118
/// <param name="exec">
119
/// A <see cref="System.String"/>
100
/// A <see cref="Application"/> array
122
/// A <see cref="List"/>
102
public static Application[] GetApplications ()
124
public static List<Application> GetApplicationList (string exec)
104
126
List<Application> apps = new List<Application> ();
105
foreach (Window w in Wnck.Screen.Default.Windows) {
106
if (!apps.Contains (w.Application))
107
apps.Add (w.Application);
109
return apps.ToArray ();
127
if (string.IsNullOrEmpty (exec))
130
exec = ProcessExecString (exec);
134
foreach (KeyValuePair<int, string> kvp in exec_lines) {
135
if (kvp.Value != null && kvp.Value.Contains (exec)) {
136
foreach (Application app in GetApplications ()) {
140
if (app.Pid == kvp.Key || app.Windows.Any (w => w.Pid == kvp.Key)) {
141
if (app.Windows.Any (win => !win.IsSkipTasklist))
151
static void UpdateExecList ()
153
if ((DateTime.UtcNow - last_update).TotalMilliseconds < 200) return;
157
foreach (string dir in Directory.GetDirectories ("/proc")) {
159
try { pid = Convert.ToInt32 (Path.GetFileName (dir)); }
162
string exec_line = CmdLineForPid (pid);
163
if (string.IsNullOrEmpty (exec_line))
166
exec_line = ProcessExecString (exec_line);
168
exec_lines [pid] = exec_line;
171
last_update = DateTime.UtcNow;
174
public static string ProcessExecString (string exec)
176
string [] parts = exec.Split (' ');
177
for (int i = 0; i < parts.Length; i++) {
178
if (parts [i].StartsWith ("-"))
181
if (parts [i].Contains ("/"))
182
parts [i] = parts [i].Split ('/').Last ();
184
foreach (string prefix in BadPrefixes) {
185
if (parts [i] == prefix)
189
if (!string.IsNullOrEmpty (parts [i])) {
190
return parts [i].ToLower ();