~alexlauni/do-plugins/bansheeplugin

« back to all changes in this revision

Viewing changes to WindowManager/src/Util.cs

  • Committer: Alex Launi
  • Date: 2009-02-28 17:40:30 UTC
  • mfrom: (276.37.54 trunk)
  • Revision ID: alex.launi@gmail.com-20090228174030-vj9z2b96smsodmmb
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
        
30
30
        public static class Util
31
31
        {
 
32
                static IEnumerable<string> BadPrefixes {
 
33
                        get {
 
34
                                yield return "gksu";
 
35
                                yield return "sudo";
 
36
                                yield return "java";
 
37
                                yield return "mono";
 
38
                                yield return "ruby";
 
39
                                yield return "padsp";
 
40
                                yield return "aoss";
 
41
                                yield return "python";
 
42
                                yield return "python2.4";
 
43
                                yield return "python2.5";
 
44
                        }
 
45
                }
 
46
                
 
47
                static List<Application> application_list;
 
48
                static bool application_list_update_needed;
 
49
                
 
50
                static Dictionary<int, string> exec_lines = new Dictionary<int, string> ();
 
51
                static DateTime last_update = new DateTime (0);
 
52
                
 
53
                static Util ()
 
54
                {
 
55
                        Wnck.Screen.Default.WindowClosed += delegate {
 
56
                                application_list_update_needed = true;
 
57
                        };
 
58
                        
 
59
                        Wnck.Screen.Default.WindowOpened += delegate {
 
60
                                application_list_update_needed = true;
 
61
                        };
 
62
                        
 
63
                        Wnck.Screen.Default.ApplicationOpened += delegate {
 
64
                                application_list_update_needed = true;
 
65
                        };
 
66
                        
 
67
                        Wnck.Screen.Default.ApplicationClosed += delegate {
 
68
                                application_list_update_needed = true;
 
69
                        };
 
70
                }
 
71
                
32
72
                /// <summary>
33
 
                /// Returns a list of applications that match an exec string
 
73
                /// Returns a list of all applications on the default screen
34
74
                /// </summary>
35
 
                /// <param name="exec">
36
 
                /// A <see cref="System.String"/>
37
 
                /// </param>
38
75
                /// <returns>
39
 
                /// A <see cref="List"/>
 
76
                /// A <see cref="Application"/> array
40
77
                /// </returns>
41
 
                public static List<Application> GetApplicationList (string exec)
 
78
                public static List<Application> GetApplications ()
42
79
                {
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")) {
47
 
                                int pid;
48
 
                                out_app = null;
49
 
                                try { pid = Convert.ToInt32 (dir.Substring (6)); } 
50
 
                                catch { continue; }
51
 
                                
52
 
                                string exec_line = CmdLineForPid (pid);
53
 
                                if (string.IsNullOrEmpty (exec_line))
54
 
                                        continue;
55
 
                                
56
 
                                if (exec_line.Contains (exec)) {
57
 
                                        foreach (Application app in GetApplications ()) {
58
 
                                                if (app.Pid == pid) {
59
 
                                                        if (app.Windows.Select (win => !win.IsSkipTasklist).Any ())
60
 
                                                                out_app = app;
61
 
                                                        break;
62
 
                                                }
63
 
                                        }
 
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);
64
85
                                }
65
 
                                
66
 
                                if (out_app != null)
67
 
                                        apps.Add (out_app);
68
86
                        }
69
 
                        return apps;
 
87
                        return application_list;
70
88
                }
71
89
                
72
90
                /// <summary>
81
99
                public static string CmdLineForPid (int pid)
82
100
                {
83
101
                        StreamReader reader;
84
 
                        string cmdline;
 
102
                        string cmdline = null;
 
103
                        
85
104
                        try {
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), ' ');
89
108
                                reader.Close ();
90
109
                                reader.Dispose ();
91
 
                        } catch { return null; }
 
110
                        } catch { }
92
111
                        
93
112
                        return cmdline;
94
113
                }
95
114
                
96
115
                /// <summary>
97
 
                /// Returns a list of all applications on the default screen
 
116
                /// Returns a list of applications that match an exec string
98
117
                /// </summary>
 
118
                /// <param name="exec">
 
119
                /// A <see cref="System.String"/>
 
120
                /// </param>
99
121
                /// <returns>
100
 
                /// A <see cref="Application"/> array
 
122
                /// A <see cref="List"/>
101
123
                /// </returns>
102
 
                public static Application[] GetApplications ()
 
124
                public static List<Application> GetApplicationList (string exec)
103
125
                {
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);
108
 
                        }
109
 
                        return apps.ToArray ();
 
127
                        if (string.IsNullOrEmpty (exec))
 
128
                                return apps;
 
129
                        
 
130
                        exec = ProcessExecString (exec);
 
131
                        
 
132
                        UpdateExecList ();
 
133
 
 
134
                        foreach (KeyValuePair<int, string> kvp in exec_lines) {
 
135
                                if (kvp.Value != null && kvp.Value.Contains (exec)) {
 
136
                                        foreach (Application app in GetApplications ()) {
 
137
                                                if (app == null)
 
138
                                                        continue;
 
139
                                                
 
140
                                                if (app.Pid == kvp.Key || app.Windows.Any (w => w.Pid == kvp.Key)) {
 
141
                                                        if (app.Windows.Any (win => !win.IsSkipTasklist))
 
142
                                                                apps.Add (app);
 
143
                                                        break;
 
144
                                                }
 
145
                                        }
 
146
                                }
 
147
                        }
 
148
                        return apps;
 
149
                }
 
150
                
 
151
                static void UpdateExecList ()
 
152
                {
 
153
                        if ((DateTime.UtcNow - last_update).TotalMilliseconds < 200) return;
 
154
                        
 
155
                        exec_lines.Clear ();
 
156
                        
 
157
                        foreach (string dir in Directory.GetDirectories ("/proc")) {
 
158
                                int pid;
 
159
                                try { pid = Convert.ToInt32 (Path.GetFileName (dir)); } 
 
160
                                catch { continue; }
 
161
                                
 
162
                                string exec_line = CmdLineForPid (pid);
 
163
                                if (string.IsNullOrEmpty (exec_line))
 
164
                                        continue;
 
165
 
 
166
                                exec_line = ProcessExecString (exec_line);
 
167
                                
 
168
                                exec_lines [pid] = exec_line;
 
169
                        }
 
170
                        
 
171
                        last_update = DateTime.UtcNow;
 
172
                }
 
173
 
 
174
                public static string ProcessExecString (string exec)
 
175
                {
 
176
                        string [] parts = exec.Split (' ');
 
177
                        for (int i = 0; i < parts.Length; i++) {
 
178
                                if (parts [i].StartsWith ("-"))
 
179
                                        continue;
 
180
                                
 
181
                                if (parts [i].Contains ("/"))
 
182
                                        parts [i] = parts [i].Split ('/').Last ();
 
183
                                
 
184
                                foreach (string prefix in BadPrefixes) {
 
185
                                        if (parts [i] == prefix)
 
186
                                                parts [i] = null;
 
187
                                }
 
188
                                
 
189
                                if (!string.IsNullOrEmpty (parts [i])) {
 
190
                                        return parts [i].ToLower ();
 
191
                                }
 
192
                        }
 
193
                        return null;
110
194
                }
111
195
        }
112
196
}