wmail: only one DEBUG macro is ever defined.
[dockapps.git] / wmappkill / wmAppKill.c
blob2a5e219a9998a0dbda42fc4f7904912aeed11f08
2 /*
3 * wmAppKill v0.2 - S.Rozange
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program (see the file COPYING); if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
28 #include <X11/Xlib.h>
29 #include <X11/xpm.h>
30 #include <X11/Xutil.h>
31 #include <X11/extensions/shape.h>
33 #include <sys/types.h>
34 #include <sys/time.h>
35 #include <sys/resource.h>
36 #include <sys/wait.h>
38 #include <glibtop.h>
39 #include <glibtop/proclist.h>
40 #include <glibtop/procstate.h>
42 #include <time.h>
43 #include <signal.h>
45 #include "fond.xbm"
46 #include "wmAppKill.xpm"
47 #include "wmAppKill.h"
49 Display *dpy; /* xlib global vars */
50 GC gc;
51 Window win;
52 Window iconWin;
53 Pixmap XPM;
54 int screen;
56 _zone *fZone = NULL;
57 _desc *pList = NULL; /* double linked list */
58 _desc *posProc = NULL; /* lower proc showed on the dock */
59 pid_t tabNoProc[NB_LINE]; /* array containing each line's pid */
60 pid_t lastProcPid; /* oldest proc before refreshment */
61 char *procBaseName = PROC_DEF; /* procBase is NULL if we want all the proc to be listed */
62 int procBasePos = 0; /* procbase's pos in linked list */
63 pid_t procBasePid = 1; /* procbase's pid */
64 unsigned int gNbProc = 0; /* proc number starting from procBase */
65 unsigned int gNbProcTotal = 0; /* total proc number */
66 struct timeval timev; /* to detect double-click */
68 void ZoneCreate(int x, int y, int width, int height, char no)
70 _zone *last;
72 if (!fZone) {
73 fZone = (_zone *)malloc(sizeof(_zone));
74 last = fZone;
75 } else {
76 for (last = fZone; last -> next; last = last -> next);
77 last -> next = (_zone *)malloc(sizeof(_zone));
78 last = last -> next;
81 last -> x = x;
82 last -> y = y;
83 last -> width = width;
84 last -> height = height;
85 last -> no = no;
86 last -> next = NULL;
89 char CheckZone(void)
91 int x, y,popo;
92 unsigned int mask;
93 Window root_ret, child_ret;
94 _zone *curseur = fZone;
96 XQueryPointer(dpy, iconWin, &root_ret, &child_ret, &popo, &popo, &x, &y, &mask); /* mouse position */
98 do {
99 if ((x >= curseur -> x) && (x <= curseur -> x + curseur -> width) && (y >= curseur -> y) && (y <= curseur -> y + curseur -> height))
100 return curseur -> no;
103 while ((curseur = curseur -> next));
105 return 0;
108 void GarbageCollector(_desc *garb)
110 _desc *next;
112 while (garb) {
113 next = garb -> next;
114 free(garb);
115 garb = next;
119 int CheckProc(pid_t pid)
121 glibtop_proclist bof;
122 unsigned int *n;
124 if ((n = glibtop_get_proclist (&bof, GLIBTOP_KERN_PROC_PID , (int64_t)pid)) == NULL) {
125 g_free(n);
126 return -1;
129 g_free(n);
130 return 0;
133 _desc *GetProcList(void) /* create a double linked list */
135 glibtop_proclist buf;
136 unsigned int *n;
137 unsigned int nbPr;
138 int i;
139 _desc *lastOne;
140 _desc *glump;
141 _desc *res;
143 if ((n = glibtop_get_proclist (&buf, GLIBTOP_KERN_PROC_UID, (int64_t)getuid())) == NULL) {
144 fprintf(stderr, "Problem using libgtop\n");
145 exit(1);
148 nbPr = (int)buf.number;
150 glump = (_desc *)malloc(sizeof(_desc));
151 res = glump;
152 lastOne = NULL;
154 for (i = nbPr; i; i--){
155 char *bof;
156 glibtop_proc_state buf;
157 glump -> previous = lastOne;
158 glump -> next = (_desc *)malloc(sizeof(_desc));
160 glibtop_get_proc_state(&buf, glump -> pid = n[i - 1]);
161 strcpy(glump -> name, bof = buf.cmd);
162 if (strlen(glump -> name) > MAX_CHAR)
163 glump -> name[MAX_CHAR] = 0;
165 lastOne = glump;
166 glump = glump -> next;
168 if (procBaseName && !strcmp(bof, procBaseName)) {
169 procBasePos = i - 1;
170 procBasePid = n[i - 1];
171 break;
175 lastOne -> next = NULL;
176 lastProcPid = n[nbPr - 1];
177 g_free(n);
179 if (procBaseName && i) gNbProc = nbPr - i + 1; /* procBase has been found */
180 else { /* procBaseName is null or hasn't been found */
181 procBaseName = NULL;
182 procBasePos = 0;
183 procBasePid = n[0];
184 gNbProc = nbPr;
187 gNbProcTotal = nbPr;
189 return res;
192 int CheckProcToRemove(unsigned int *procList, unsigned int procListSize)
194 _desc *curseur = pList, *temp;
195 int nbProcRemoved = 0, i;
197 while (curseur) {
198 for (i = procListSize; i; i--)
199 if (curseur -> pid == procList[i - 1]) break;
201 temp = curseur;
202 curseur = curseur -> next;
204 if (!i) { /* we didn't find it in proclist, let's remove it */
205 RemoveProc(temp);
206 gNbProc--;
207 nbProcRemoved++;
211 return nbProcRemoved;
214 int CheckProcToAdd(int pos, unsigned int *procList, unsigned int procListSize)
216 _desc *glump;
217 int i, compteur = 0;
218 glibtop_proc_state buf;
220 for (i = pos; i < procListSize ; i++){
222 compteur++;
223 glump = (_desc *)malloc(sizeof(_desc));
224 usleep(20000); /* libgtop seems to need a little bit of time */
225 if (CheckProc(procList[i])) continue; /* checking if the process isn't already dead */
227 glibtop_get_proc_state(&buf, glump -> pid = procList[i]);
228 strcpy(glump -> name, buf.cmd);
229 if (strlen(glump -> name) > MAX_CHAR)
230 glump -> name[MAX_CHAR] = 0;
232 pList -> previous = glump;
233 glump -> next = pList;
234 glump -> previous = NULL;
235 if (posProc == pList) posProc = glump;
236 pList = glump;
237 gNbProc++;
238 gNbProcTotal++;
240 lastProcPid = glump -> pid;
243 return compteur;
246 int CheckProcChange(void)
248 glibtop_proclist buf;
249 unsigned int *n;
250 unsigned int nbPr;
251 int diffNbProc;
253 if ((n = glibtop_get_proclist (&buf, GLIBTOP_KERN_PROC_UID, (int64_t)getuid())) == NULL) return -1;
254 nbPr = (int)buf.number;
256 if ((nbPr == gNbProcTotal) && (n[nbPr - 1] == lastProcPid)) return 0; /* nothing changed */
258 if (procBaseName && (n[procBasePos] != procBasePid)) /* some proc killed before the baseproc (=oldest proc) */
260 if (CheckProc(procBasePid)) { /* baseproc doesn't exist anymore */
261 GarbageCollector(pList);
262 pList = GetProcList(); /* so we create a whole new list */
263 posProc = pList;
264 return 1;
266 else
267 while (n[--procBasePos] != procBasePid); /* here we find what's the new pos. of baseproc */
271 diffNbProc = (nbPr - procBasePos) - gNbProc; /* nb of changes after baseproc */
273 if (diffNbProc == 0 && (n[nbPr - 1] == lastProcPid)){ /* only changes before baseproc */
274 gNbProcTotal = nbPr;
275 g_free(n);
276 return 0;
279 if (diffNbProc > 0 && n[nbPr - diffNbProc - 1] == lastProcPid) /* only proc to add */
280 CheckProcToAdd(nbPr - diffNbProc, n, nbPr);
282 else { /* to remove [and to add] */
283 int nb;
284 nb = CheckProcToRemove(n, nbPr);
285 if (nb != -diffNbProc)
286 CheckProcToAdd(nbPr - diffNbProc - 1, n, nbPr);
289 g_free(n);
290 return 1;
293 void RemoveProc(_desc *cible)
295 _desc *temp1, *temp2;
297 _desc *curseur;
298 int i;
300 temp1 = cible -> previous;
301 temp2 = cible -> next;
303 for (curseur = cible, i = 0; curseur && i != NB_LINE + 1; curseur = curseur -> next, ++i);
305 if (!(gNbProc - 1 < NB_LINE) && (i == 2 || i == 3)) { /* the killed proc is near the start of the list */
306 for (--i, curseur = posProc; i && curseur -> previous; curseur = curseur -> previous, --i);
307 posProc = curseur;
309 else if ((cible == posProc) && (cible -> previous)) {
310 posProc = cible -> previous;
312 else if ((cible == posProc) && (cible -> next)) {
313 posProc = cible -> next;
316 if (temp1) temp1 -> next = temp2;
317 else {
318 pList = temp2;
319 temp2 -> previous = NULL;
320 gNbProcTotal--;
321 lastProcPid = temp2 -> pid;
322 free(cible);
323 return;
326 if (temp2) temp2 -> previous = temp1;
327 else
328 temp1 -> next = NULL;
330 free(cible);
331 gNbProcTotal--;
335 void ShowString (int x, int y, char *doudou)
337 int i = 0;
338 char c;
340 while ((c = tolower(doudou[i++]))){
341 if (c >= 'a' && c <= 'z') {
342 XCopyArea(dpy,XPM, win,gc, 1 + (c - 'a') * 6, 10, 5, 8, x, y) ;
343 XCopyArea(dpy,XPM, iconWin,gc, 1 + (c - 'a') * 6, 10, 5, 8, x, y) ;
344 x += 6;
349 void DoExp(){
351 XClearWindow(dpy, win);
352 XClearWindow(dpy, iconWin);
354 XCopyArea(dpy, XPM ,win, gc, 1 + (26) * 6, 10, 6, 8, 5, 51);
355 XCopyArea(dpy, XPM, iconWin, gc, 1 + (26) * 6, 10, 6, 8, 5, 51);
356 XCopyArea(dpy, XPM, win, gc, 1 + (27) * 6, 10, 6, 8, 53, 51);
357 XCopyArea(dpy, XPM, iconWin, gc, 1 + (27) * 6, 10, 6, 8, 53, 51);
359 XCopyArea(dpy, XPM, win, gc, 106, 0, 28, 8, 17, 50);
360 XCopyArea(dpy, XPM, iconWin, gc, 106, 0, 28, 8, 17, 50);
362 DoExpose();
365 void DoExpose() {
367 int i;
368 _desc *curseur;
370 for (i = NB_LINE; i; i--){
371 XCopyArea(dpy, XPM, win, gc, 2, 22, 53, 9, X_PROC + 1, Y_PROC + ((i - 1) * 10));
372 XCopyArea(dpy, XPM, iconWin, gc, 2, 22, 53, 9, X_PROC + 1, Y_PROC + ((i - 1) * 10));
375 if (gNbProc < NB_LINE) {
376 int y;
377 i = gNbProc;
378 for (y = NB_LINE; y != gNbProc; y--) tabNoProc[y - 1] = -1;
380 else i = NB_LINE;
382 for (curseur = posProc; i; curseur = curseur -> next, i--)
384 ShowString(X_PROC + 1, Y_PROC + (i - 1) * 10, curseur -> name);
385 tabNoProc[i - 1] = curseur -> pid;
389 void DoClick(XEvent ev)
391 unsigned char doubleClick = 0;
392 static unsigned char firstClick = 0;
393 _desc *curseur;
394 char zone, i;
396 zone = CheckZone();
398 if (ev.xbutton.button == CLICK_TWO) {
399 DoExpose() ;
402 /* Mouse wheel patch by Mathieu Cuny */
404 if (ev.xbutton.button == WHEEL_UP && gNbProc > NB_LINE) {
405 for (i = NB_LINE, curseur = posProc; i; curseur = curseur -> next, i--);
406 if (curseur) posProc = posProc -> next;
407 DoExpose();
410 if (ev.xbutton.button == WHEEL_DOWN && posProc -> previous && gNbProc > NB_LINE) {
411 posProc = posProc -> previous;
412 DoExpose();
415 /* Mouse wheel patch end */
417 if (ev.xbutton.button == CLICK_ONE) {
419 struct timeval temp;
420 long long nms1;
422 gettimeofday(&temp, NULL);
423 nms1 = temp.tv_sec - timev.tv_sec; /* nb sec since last click */
425 if ((!nms1 || nms1 == 1)){
426 long long yop = (nms1 * 1000000L) + (temp.tv_usec - timev.tv_usec); /* nb mlsec since last click */
427 if (firstClick && (yop < DOUBLE_CLICK_DELAY)){ /* we got double click */
428 doubleClick = 1;
429 firstClick = 0;
430 } else firstClick = 1;
431 } else firstClick = 1;
433 timev = temp;
435 if (zone == UP && !doubleClick && gNbProc > NB_LINE)
437 for (i = NB_LINE, curseur = posProc; i; curseur = curseur -> next, i--);
438 if (curseur) posProc = posProc -> next;
439 DoExpose();
442 else if (zone == DOWN && posProc -> previous && !doubleClick && gNbProc > NB_LINE)
444 posProc = posProc -> previous;
445 DoExpose();
448 else if (zone == UP && doubleClick && gNbProc > NB_LINE)
451 for (curseur = pList; curseur -> next; curseur = curseur -> next); /* curseur = end of list */
453 for (i = NB_LINE - 1; i; curseur = curseur -> previous, i--);
454 posProc = curseur;
455 DoExpose();
458 else if (zone == DOWN && doubleClick && gNbProc > NB_LINE)
460 posProc = pList;
461 DoExpose();
464 else if (zone > 0 && zone <= NB_LINE && doubleClick && tabNoProc[zone - 1] != -1)
466 kill(tabNoProc[zone - 1], SIGKILL); /* let's kill the mofo */
467 waitpid(tabNoProc[zone - 1], NULL, 0);
470 if (doubleClick) doubleClick = 0;
475 void DoEvents() {
477 unsigned long long compteur = 0;
479 XEvent ev ;
481 for (;;){
482 if (!compteur){
483 if (CheckProcChange()) DoExpose();
484 compteur = UPDATE_NB * DELAY;
486 while(XPending(dpy)){
487 XNextEvent(dpy,&ev);
488 switch(ev.type) {
489 case Expose : DoExp(); break;
490 case ButtonPress : DoClick(ev); break;
493 usleep(DELAY);
494 compteur -= DELAY;
498 void PrintUsage(void)
500 printf("usage: wmAppKill [-a] [-n <name>] [-h]\n");
501 printf("\t-a\t\tSelect all processes\n");
502 printf("\t-n <name>\tDo not select processes older than <name> (default: wmaker)\n");
503 printf("\t-h\t\tDisplay help screen\n");
506 void GetArg(int argc, char *argv[])
508 if (argc == 1) return;
510 else if (argc == 3 && !strcmp(argv[1], "-n") && argv[2][0] != '-')
511 procBaseName = strdup(argv[2]);
513 else if (argc == 2 && !strcmp(argv[1], "-a"))
514 procBaseName = NULL;
516 else if (argc == 2 && !strcmp(argv[1], "-h"))
518 PrintUsage();
519 exit(1);
521 else {
522 PrintUsage();
523 exit(1);
527 void CreateDock(int argc, char *argv[]) /* this part comes from http://www.linuxmag-france.org/ */
529 Window root;
530 XWMHints wmHints;
531 XSizeHints sizeHints;
532 XClassHint classHint;
533 Pixmap pixmask;
534 unsigned long p_blanc;
535 unsigned long p_noir;
536 unsigned int borderWidth = 2;
537 char *wname = argv[0] ;
539 dpy = XOpenDisplay(NULL) ;
541 if(dpy == NULL)
543 fprintf(stderr, "Can't open display\n") ;
544 exit(1) ;
547 root = RootWindow(dpy,screen);
548 p_blanc = WhitePixel(dpy,screen) ;
549 p_noir = BlackPixel(dpy,screen) ;
550 gc = XDefaultGC(dpy,screen) ;
551 XSetForeground(dpy, gc, p_noir);
552 XSetBackground(dpy, gc,p_noir);
554 sizeHints.x = 0 ;
555 sizeHints.y = 0 ;
556 sizeHints.width = 64 ;
557 sizeHints.height = 64 ;
559 win = XCreateSimpleWindow(dpy,root,sizeHints.x,sizeHints.y , sizeHints.width, sizeHints.height, borderWidth, p_noir,p_noir) ;
560 iconWin = XCreateSimpleWindow(dpy,root,sizeHints.x,sizeHints.y,sizeHints.width, sizeHints.height, borderWidth, p_noir,p_noir ) ;
562 sizeHints.flags = USSize | USPosition ;
563 XSetWMNormalHints(dpy,win,&sizeHints) ;
564 wmHints.initial_state = WithdrawnState ;
565 wmHints.icon_window = iconWin ;
566 wmHints.icon_x = sizeHints.x ;
567 wmHints.icon_y = sizeHints.y ;
568 wmHints.window_group = win ;
569 wmHints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint ;
570 XSetWMHints(dpy, win, &wmHints) ;
571 classHint.res_name = wname ;
572 classHint.res_class = wname ;
574 XSetClassHint(dpy, win, &classHint) ;
575 XSetCommand(dpy,win, argv, argc) ;
577 pixmask = XCreateBitmapFromData(dpy,win,fond_bits, fond_width, fond_height) ;
578 XShapeCombineMask(dpy,win,ShapeBounding,0,0,pixmask,ShapeSet) ;
579 XShapeCombineMask(dpy,iconWin,ShapeBounding, 0, 0, pixmask, ShapeSet) ;
580 XpmCreatePixmapFromData(dpy,root,wmAppKill_xpm, &XPM, NULL,NULL) ;
582 XSelectInput(dpy,win, ExposureMask | ButtonPressMask) ;
583 XSelectInput(dpy,iconWin, ExposureMask | ButtonPressMask) ;
585 XMapWindow(dpy,win) ;
588 int main(int argc, char *argv[])
590 int i;
592 GetArg(argc, argv);
593 glibtop_init();
594 CreateDock(argc, argv);
595 gettimeofday(&timev, NULL);
597 pList = GetProcList();
598 posProc = pList;
600 ZoneCreate(4, 50, 8, 8, UP);
601 ZoneCreate(54, 50, 8, 8, DOWN);
603 for (i = NB_LINE; i; i--)
604 ZoneCreate(X_PROC + 1, Y_PROC + (i - 1) * 10 + 2, 48, 7, i);
606 DoEvents();
608 return 0;