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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/*
* Copyright 2007 Simone Della Longa <simonedll@yahoo.it>
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "tasks.h"
bool taskInfo::Init (WnckWindow* window) {
if (!window) {
printf("taskInfo::Init NULL argument!\n");
return false;
}
/*First set the icon. This will work always, as wnck_window_get_icon
returns a fallbackicon if necessary, the rest may fail*/
GdkPixbuf* pb = gdk_pixbuf_copy(wnck_window_get_icon (window));
wxBitmap bmp;
bmp.SetPixbuf(pb);
if (!bmp.IsOk ()) {
return false;
}
this->icon = bmp;
enum { BUFFERSIZE = 1024 };
char processName[BUFFERSIZE];
/* Since X doesn't rely on pids there's not a clear way to get the
* pid associated with a window. We use 2 different ways to get the
* pid. None of them is 100% accurate. (aMSN!!!) */
pid = wnck_window_get_pid (window);
if (!pid) {
pid = wnck_application_get_pid(wnck_window_get_application(window));
}
if (!pid){
printf("Could not get pid for 0x%x\n",(unsigned int) wnck_window_get_xid (window));
return false;
}
if (!tasks_getProcessName(pid, &processName[0], sizeof (processName) -1))
{
printf("Could not get process name for %d\n", pid);
return false;
}
setPath(&processName[0]);
setName(&processName[0]);
return true;
}
bool taskInfo::compareWith (simImage * img) {
if (name.IsEmpty() || img->link.IsEmpty()) {
return false;
}
if (pid == img->pid) {
return true;
}
wxFileName launcherFN (img->link);
wxString currLauncher = launcherFN.GetName ();
if (launcherFN.HasExt ()) {
currLauncher = currLauncher + _T (".") + launcherFN.GetExt ();
}
if ( (currLauncher.Len() > name.Len() && currLauncher.StartsWith(name))
|| name.StartsWith(currLauncher)
|| currLauncher.CmpNoCase(name) == 0) {
return true;
}
return false;
}
void taskInfo::setName (char *processName) {
wxString s = wxString ((const char *) processName, wxConvUTF8);
wxFileName programFN (s);
name = programFN.GetName ();
if (programFN.HasExt ()) {
name = name + _T (".") + programFN.GetExt ();
}
}
void taskInfo::setPath (char *processName) {
path = wxString ((const char *) processName, wxConvUTF8);
}
int tasks_getProcessName (unsigned int pid, char *target_name, int size_targetname) {
int target_link;
char dir_name[PATH_MAX];
sprintf(dir_name, "/proc/%d/exe", pid);
/* Getting the target of the exe ie to which binary it points to */
target_link = readlink(dir_name, target_name, size_targetname);
if (target_link <= 0) {
return false;
}
target_name[target_link] = '\0';
return true;
}
static void tasks_window_closed (WnckScreen *screen, WnckWindow *window,callbackArgs* ca) {
if (!WNCK_IS_WINDOW (window) || wnck_window_is_skip_tasklist (window)) {
return;
}
ImagesArray * ImagesList = ca->ImagesList;
if (!ImagesList) {
return;
}
for (unsigned int i = 0; i < ImagesList->GetCount (); i++) {
simImage *img = (*ImagesList)[i];
if (img->hasWindow(window)) {
img->removeWindow(window);
if (img->task && img->windowCount() == 0) {
delete img;
ImagesList->RemoveAt (i);
wxGetApp().updateSize();
} else if (img->windowCount() == 0) {
// we need to fall back to the default image, if a starter
img->loadImage(img->img_link);
}
if (img->windowCount() > 0) {
taskInfo ti;
ti.Init(img->getWindow());
img->img = ti.icon.ConvertToImage();
}
wxGetApp().frame->appSize = FirstPosition(ca->settings, ImagesList);
wxGetApp().updateSize();
wxGetApp().refresh();
return;
}
}
}
static void tasks_window_opened (WnckScreen *screen, WnckWindow *window, callbackArgs* ca) {
if (!ca || !ca->ImagesList) {
printf("tasks_window_opened NULL ARGUMENT!\n");
return;
}
if (wnck_window_is_skip_tasklist (window)) {
return;
}
ImagesArray * ImagesList = ca->ImagesList;
simSettings settings = ca->settings;
taskInfo ti;
ti.Init(window);
unsigned int i;
for (i = 0; i < ImagesList->GetCount (); i++) {
simImage *img = (*ImagesList)[i];
if (ti.compareWith (img)) {
img->addWindow(window);
img->pid = ti.pid;
// refresh the icon, cause sometimes the splash-screen has not
// a valid icon, but the program does
img->img = ti.icon.ConvertToImage();
wxGetApp().refresh();
return;
}
}
if (i == ImagesList->GetCount()) {
tasks_addNewImage(window, ImagesList, settings, ti);
}
}
void tasks_window_background_change (WnckScreen *screen, WnckWindow *window, callbackArgs* ca) {
wxBitmap* backImage = getRootWallpaper();
wxGetApp().SetWallpaper(backImage);
}
void tasks_track_active_window (WnckScreen *screen, WnckWindow *window, callbackArgs* ca) {
if (!WNCK_IS_WINDOW (window)) {
return;
}
ImagesArray * ImagesList = ca->ImagesList;
if (!ImagesList) {
return;
}
if (wnck_window_is_skip_tasklist (window)) {
// note that this also catches simdock itself
return;
}
for (unsigned int i = 0; i < ImagesList->GetCount (); i++) {
simImage *img = (*ImagesList)[i];
img->active = img->hasWindow(window);
}
}
void tasks_register_signals(ImagesArray * ImagesList,simSettings settings) {
callbackArgs* ca = new callbackArgs();
ca->ImagesList = ImagesList;
ca->settings = settings;
WnckScreen *defaultScreen = wnck_screen_get_default ();
g_signal_connect (G_OBJECT(defaultScreen), "window_opened",
G_CALLBACK (tasks_window_opened),
ca);
g_signal_connect (G_OBJECT(defaultScreen), "background-changed",
G_CALLBACK (tasks_window_background_change),
ca);
g_signal_connect (G_OBJECT(defaultScreen), "window_closed",
G_CALLBACK(tasks_window_closed),
ca);
g_signal_connect (G_OBJECT(defaultScreen), "active-window-changed",
G_CALLBACK(tasks_track_active_window),
ca);
}
void tasks_addNewImage(WnckWindow *window, ImagesArray* ImagesList, simSettings settings,const taskInfo& ti) {
if (!window || !ImagesList) {
printf("addNewImage::Null argument\n");
return;
}
simImage *si = new simImage (ti.icon.ConvertToImage (), _T (" "), ti.name, ti.name, _T (" "), 1);
si->addWindow(window);
si->pid = ti.pid;
si->w = settings.ICONW;
si->h = settings.ICONH;
si->y = (settings.MAXSIZE + settings.BOTTOM_BORDER) - settings.ICONH - settings.BOTTOM_BORDER;
si->task = true;
si->blurStatus = STATUS_NONE;
si->blur = 0;
ImagesList->Add(si);
wxGetApp().frame->appSize = FirstPosition(settings, ImagesList);
wxGetApp().updateSize();
wxGetApp().refresh();
}
void tasks_fillList (ImagesArray * ImagesList, simSettings settings) {
WnckScreen *defaultScreen = wnck_screen_get_default ();
wnck_screen_force_update (defaultScreen);
GList *windowz = wnck_screen_get_windows (defaultScreen);
WnckWindow *curr_Win=NULL;
int l = g_list_length (windowz);
//printf ("%d windows found\n", l);
for (int i = 0; i < l; i++) {
curr_Win = (WnckWindow *) g_list_nth_data (windowz, i);
if (!curr_Win || wnck_window_is_skip_tasklist (curr_Win)) {
continue;
}
taskInfo ti;
bool init_ok = ti.Init(curr_Win);
if (init_ok) {
unsigned int i;
for (i = 0; i < ImagesList->GetCount (); i++) {
simImage *img = (*ImagesList)[i];
if (ti.compareWith (img)) {
img->addWindow(curr_Win);
img->pid = ti.pid;
break;
}
}
if (i == ImagesList->GetCount()) {
tasks_addNewImage(curr_Win, ImagesList, settings, ti);
}
} else {
tasks_addNewImage(curr_Win, ImagesList, settings, ti);
}
}
}
Window tasks_getFromName (const char *name, int nameLength) {
if (!name) {
return 0;
}
WnckScreen *defaultScreen = wnck_screen_get_default ();
wnck_screen_force_update (defaultScreen);
GList *windowz = wnck_screen_get_windows (defaultScreen);
WnckWindow *curr_Win;
int l = g_list_length (windowz);
for (int i = 0; i < l; i++) {
curr_Win = (WnckWindow *) g_list_nth_data (windowz, i);
WnckApplication *app = wnck_window_get_application (curr_Win);
if (strncmp (name, wnck_application_get_name (app), nameLength) == 0) {
return wnck_window_get_xid (curr_Win);
}
}
return 0;
}
Window tasks_getFromWindowName (const char *name, int nameLength) {
if (!name) {
return 0;
}
WnckScreen *defaultScreen = wnck_screen_get_default ();
wnck_screen_force_update (defaultScreen);
GList *windowz = wnck_screen_get_windows (defaultScreen);
WnckWindow *curr_Win;
int l = g_list_length (windowz);
for (int i = 0; i < l; i++) {
curr_Win = (WnckWindow *) g_list_nth_data (windowz, i);
printf("%s \n", wnck_window_get_name (curr_Win));
if (strncmp (name, wnck_window_get_name (curr_Win), nameLength) == 0) {
return wnck_window_get_xid (curr_Win);
}
}
return 0;
}
void tasks_raise (WnckWindow* win) {
if (!win) {
return;
}
wnck_window_unminimize(win, gtk_get_current_event_time());
wnck_window_activate(win, gtk_get_current_event_time());
}
void tasks_minimize (WnckWindow* win) {
if (!win) {
return;
}
wnck_window_minimize(win);
}
|