WINGs: fix memory leak in getStreamObjects (Coverity #50119)
[wmaker-crm.git] / src / switchpanel.c
blob16083658e18d92fe982abf1c88b555ab2b483739
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2004 Alfredo K. Kojima
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "wconfig.h"
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/time.h>
28 #include "WindowMaker.h"
29 #include "screen.h"
30 #include "framewin.h"
31 #include "icon.h"
32 #include "window.h"
33 #include "defaults.h"
34 #include "switchpanel.h"
35 #include "misc.h"
36 #include "xinerama.h"
39 #ifdef USE_XSHAPE
40 #include <X11/extensions/shape.h>
41 #endif
43 struct SwitchPanel {
44 WScreen *scr;
45 WMWindow *win;
46 WMFrame *iconBox;
48 WMArray *icons;
49 WMArray *images;
50 WMArray *windows;
51 WMArray *flags;
52 RImage *bg;
53 int current;
54 int firstVisible;
55 int visibleCount;
57 WMLabel *label;
59 RImage *tileTmp;
60 RImage *tile;
62 WMFont *font;
63 WMColor *white;
66 #define BORDER_SPACE 10
67 #define ICON_SIZE 48
68 #define ICON_TILE_SIZE 64
69 #define LABEL_HEIGHT 25
70 #define SCREEN_BORDER_SPACING 2*20
71 #define SCROLL_STEPS (ICON_TILE_SIZE/2)
73 #define ICON_SELECTED (1<<1)
74 #define ICON_DIM (1<<2)
76 static int canReceiveFocus(WWindow *wwin)
78 if (wwin->frame->workspace != w_global.workspace.current)
79 return 0;
81 if (wPreferences.cycle_active_head_only &&
82 wGetHeadForWindow(wwin) != wGetHeadForPointerLocation(wwin->screen_ptr))
83 return 0;
85 if (WFLAGP(wwin, no_focusable))
86 return 0;
88 if (!wwin->flags.mapped) {
89 if (!wwin->flags.shaded && !wwin->flags.miniaturized && !wwin->flags.hidden)
90 return 0;
91 else
92 return -1;
95 return 1;
98 static Bool sameWindowClass(WWindow *wwin, WWindow *curwin)
100 if (!wwin->wm_class || !curwin->wm_class)
101 return False;
102 if (strcmp(wwin->wm_class, curwin->wm_class))
103 return False;
105 return True;
108 static void changeImage(WSwitchPanel *panel, int idecks, int selected, Bool dim, Bool force)
110 WMFrame *icon = NULL;
111 RImage *image = NULL;
112 int flags;
113 int desired = 0;
115 /* This whole function is a no-op if we aren't drawing the panel */
116 if (!wPreferences.swtileImage)
117 return;
119 icon = WMGetFromArray(panel->icons, idecks);
120 image = WMGetFromArray(panel->images, idecks);
121 flags = (int) (uintptr_t) WMGetFromArray(panel->flags, idecks);
123 if (selected)
124 desired |= ICON_SELECTED;
125 if (dim)
126 desired |= ICON_DIM;
128 if (flags == desired && !force)
129 return;
131 WMReplaceInArray(panel->flags, idecks, (void *) (uintptr_t) desired);
133 if (!panel->bg && !panel->tile && !selected)
134 WMSetFrameRelief(icon, WRFlat);
136 if (image && icon) {
137 RImage *back;
138 int opaq = (dim) ? 75 : 255;
139 RImage *tile;
140 WMPoint pos;
141 Pixmap p;
143 if (canReceiveFocus(WMGetFromArray(panel->windows, idecks)) < 0)
144 opaq = 50;
146 pos = WMGetViewPosition(WMWidgetView(icon));
147 back = panel->tileTmp;
148 if (panel->bg) {
149 RCopyArea(back, panel->bg,
150 BORDER_SPACE + pos.x - panel->firstVisible * ICON_TILE_SIZE,
151 BORDER_SPACE + pos.y, back->width, back->height, 0, 0);
152 } else {
153 RColor color;
154 WMScreen *wscr = WMWidgetScreen(icon);
155 color.red = 255;
156 color.red = WMRedComponentOfColor(WMGrayColor(wscr)) >> 8;
157 color.green = WMGreenComponentOfColor(WMGrayColor(wscr)) >> 8;
158 color.blue = WMBlueComponentOfColor(WMGrayColor(wscr)) >> 8;
159 RFillImage(back, &color);
162 if (selected) {
163 tile = panel->tile;
164 RCombineArea(back, tile, 0, 0, tile->width, tile->height,
165 (back->width - tile->width) / 2, (back->height - tile->height) / 2);
168 RCombineAreaWithOpaqueness(back, image, 0, 0, image->width, image->height,
169 (back->width - image->width) / 2, (back->height - image->height) / 2,
170 opaq);
172 RConvertImage(panel->scr->rcontext, back, &p);
173 XSetWindowBackgroundPixmap(dpy, WMWidgetXID(icon), p);
174 XClearWindow(dpy, WMWidgetXID(icon));
175 XFreePixmap(dpy, p);
178 if (!panel->bg && !panel->tile && selected)
179 WMSetFrameRelief(icon, WRSimple);
182 static void addIconForWindow(WSwitchPanel *panel, WMWidget *parent, WWindow *wwin, int x, int y)
184 WMFrame *icon = WMCreateFrame(parent);
185 RImage *image = NULL;
187 WMSetFrameRelief(icon, WRFlat);
188 WMResizeWidget(icon, ICON_TILE_SIZE, ICON_TILE_SIZE);
189 WMMoveWidget(icon, x, y);
191 if (!WFLAGP(wwin, always_user_icon) && wwin->net_icon_image)
192 image = RRetainImage(wwin->net_icon_image);
194 /* get_icon_image() includes the default icon image */
195 if (!image)
196 image = get_icon_image(panel->scr, wwin->wm_instance, wwin->wm_class, ICON_TILE_SIZE);
198 /* We must resize the icon size (~64) to the switchpanel icon size (~48) */
199 image = wIconValidateIconSize(image, ICON_SIZE);
201 WMAddToArray(panel->images, image);
202 WMAddToArray(panel->icons, icon);
205 static void scrollIcons(WSwitchPanel *panel, int delta)
207 int nfirst = panel->firstVisible + delta;
208 int i;
209 int count = WMGetArrayItemCount(panel->windows);
210 Bool dim;
212 if (count <= panel->visibleCount)
213 return;
215 if (nfirst < 0)
216 nfirst = 0;
217 else if (nfirst >= count - panel->visibleCount)
218 nfirst = count - panel->visibleCount;
220 if (nfirst == panel->firstVisible)
221 return;
223 WMMoveWidget(panel->iconBox, -nfirst * ICON_TILE_SIZE, 0);
225 panel->firstVisible = nfirst;
227 for (i = panel->firstVisible; i < panel->firstVisible + panel->visibleCount; i++) {
228 if (i == panel->current)
229 continue;
230 dim = ((int) (uintptr_t) WMGetFromArray(panel->flags, i) & ICON_DIM);
231 changeImage(panel, i, 0, dim, True);
236 * 0 1 2
237 * 3 4 5
238 * 6 7 8
240 static RImage *assemblePuzzleImage(RImage **images, int width, int height)
242 RImage *img = RCreateImage(width, height, 1);
243 RImage *tmp;
244 int tw, th;
245 RColor color;
246 if (!img)
247 return NULL;
249 color.red = 0;
250 color.green = 0;
251 color.blue = 0;
252 color.alpha = 255;
254 RFillImage(img, &color);
256 tw = width - images[0]->width - images[2]->width;
257 th = height - images[0]->height - images[6]->height;
259 if (tw <= 0 || th <= 0)
260 return NULL;
262 /* top */
263 if (tw > 0) {
264 tmp = RSmoothScaleImage(images[1], tw, images[1]->height);
265 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[0]->width, 0);
266 RReleaseImage(tmp);
268 /* bottom */
269 if (tw > 0) {
270 tmp = RSmoothScaleImage(images[7], tw, images[7]->height);
271 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[6]->width, height - images[6]->height);
272 RReleaseImage(tmp);
274 /* left */
275 if (th > 0) {
276 tmp = RSmoothScaleImage(images[3], images[3]->width, th);
277 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, 0, images[0]->height);
278 RReleaseImage(tmp);
280 /* right */
281 if (th > 0) {
282 tmp = RSmoothScaleImage(images[5], images[5]->width, th);
283 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, width - images[5]->width, images[2]->height);
284 RReleaseImage(tmp);
286 /* center */
287 if (tw > 0 && th > 0) {
288 tmp = RSmoothScaleImage(images[4], tw, th);
289 RCopyArea(img, tmp, 0, 0, tmp->width, tmp->height, images[0]->width, images[0]->height);
290 RReleaseImage(tmp);
293 /* corners */
294 RCopyArea(img, images[0], 0, 0, images[0]->width, images[0]->height, 0, 0);
295 RCopyArea(img, images[2], 0, 0, images[2]->width, images[2]->height, width - images[2]->width, 0);
296 RCopyArea(img, images[6], 0, 0, images[6]->width, images[6]->height, 0, height - images[6]->height);
297 RCopyArea(img, images[8], 0, 0, images[8]->width, images[8]->height,
298 width - images[8]->width, height - images[8]->height);
300 return img;
303 static RImage *createBackImage(int width, int height)
305 return assemblePuzzleImage(wPreferences.swbackImage, width, height);
308 static RImage *getTile(void)
310 RImage *stile;
312 if (!wPreferences.swtileImage)
313 return NULL;
315 stile = RScaleImage(wPreferences.swtileImage, ICON_TILE_SIZE, ICON_TILE_SIZE);
316 if (!stile)
317 return wPreferences.swtileImage;
319 return stile;
322 static void drawTitle(WSwitchPanel *panel, int idecks, const char *title)
324 char *ntitle;
325 int width = WMWidgetWidth(panel->win);
326 int x;
328 if (title)
329 ntitle = ShrinkString(panel->font, title, width - 2 * BORDER_SPACE);
330 else
331 ntitle = NULL;
333 if (panel->bg) {
334 if (ntitle) {
335 if (strcmp(ntitle, title) != 0) {
336 x = BORDER_SPACE;
337 } else {
338 int w = WMWidthOfString(panel->font, ntitle, strlen(ntitle));
340 x = BORDER_SPACE + (idecks - panel->firstVisible) * ICON_TILE_SIZE +
341 ICON_TILE_SIZE / 2 - w / 2;
342 if (x < BORDER_SPACE)
343 x = BORDER_SPACE;
344 else if (x + w > width - BORDER_SPACE)
345 x = width - BORDER_SPACE - w;
349 XClearWindow(dpy, WMWidgetXID(panel->win));
350 if (ntitle)
351 WMDrawString(panel->scr->wmscreen,
352 WMWidgetXID(panel->win),
353 panel->white, panel->font,
355 WMWidgetHeight(panel->win) - BORDER_SPACE - LABEL_HEIGHT +
356 WMFontHeight(panel->font) / 2, ntitle, strlen(ntitle));
357 } else {
358 if (ntitle)
359 WMSetLabelText(panel->label, ntitle);
362 if (ntitle)
363 free(ntitle);
366 static WMArray *makeWindowListArray(WWindow *curwin, int include_unmapped, Bool class_only)
368 WMArray *windows = WMCreateArray(10);
369 int fl;
370 WWindow *wwin;
372 for (fl = 0; fl < 2; fl++) {
373 for (wwin = curwin; wwin; wwin = wwin->prev) {
374 if (((!fl && canReceiveFocus(wwin) > 0) || (fl && canReceiveFocus(wwin) < 0)) &&
375 (wwin->flags.mapped || include_unmapped)) {
376 if (class_only)
377 if (!sameWindowClass(wwin, curwin))
378 continue;
380 if (!WFLAGP(wwin, skip_switchpanel))
381 WMAddToArray(windows, wwin);
384 wwin = curwin;
385 /* start over from the beginning of the list */
386 while (wwin->next)
387 wwin = wwin->next;
389 for (wwin = curwin; wwin && wwin != curwin; wwin = wwin->prev) {
390 if (((!fl && canReceiveFocus(wwin) > 0) || (fl && canReceiveFocus(wwin) < 0)) &&
391 (wwin->flags.mapped || include_unmapped)) {
392 if (class_only)
393 if (!sameWindowClass(wwin, curwin))
394 continue;
395 if (!WFLAGP(wwin, skip_switchpanel))
396 WMAddToArray(windows, wwin);
401 return windows;
404 static WMArray *makeWindowFlagsArray(int count)
406 WMArray *flags = WMCreateArray(count);
407 int i;
409 for (i = 0; i < count; i++)
410 WMAddToArray(flags, (void *) 0);
412 return flags;
415 WSwitchPanel *wInitSwitchPanel(WScreen *scr, WWindow *curwin, Bool class_only)
417 WWindow *wwin;
418 WSwitchPanel *panel = wmalloc(sizeof(WSwitchPanel));
419 WMFrame *viewport;
420 int i, width, height, iconsThatFitCount, count;
421 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
423 panel->scr = scr;
424 panel->windows = makeWindowListArray(curwin, wPreferences.swtileImage != NULL, class_only);
425 count = WMGetArrayItemCount(panel->windows);
426 if (count)
427 panel->flags = makeWindowFlagsArray(count);
429 if (count == 0) {
430 WMFreeArray(panel->windows);
431 wfree(panel);
432 return NULL;
435 width = ICON_TILE_SIZE * count;
436 iconsThatFitCount = count;
438 if (width > rect.size.width) {
439 iconsThatFitCount = (rect.size.width - SCREEN_BORDER_SPACING) / ICON_TILE_SIZE;
440 width = iconsThatFitCount * ICON_TILE_SIZE;
443 panel->visibleCount = iconsThatFitCount;
445 if (!wPreferences.swtileImage)
446 return panel;
448 height = LABEL_HEIGHT + ICON_TILE_SIZE;
450 panel->tileTmp = RCreateImage(ICON_TILE_SIZE, ICON_TILE_SIZE, 1);
451 panel->tile = getTile();
452 if (panel->tile && wPreferences.swbackImage[8])
453 panel->bg = createBackImage(width + 2 * BORDER_SPACE, height + 2 * BORDER_SPACE);
455 if (!panel->tileTmp || !panel->tile) {
456 if (panel->bg)
457 RReleaseImage(panel->bg);
458 panel->bg = NULL;
459 if (panel->tile)
460 RReleaseImage(panel->tile);
461 panel->tile = NULL;
462 if (panel->tileTmp)
463 RReleaseImage(panel->tileTmp);
464 panel->tileTmp = NULL;
467 panel->white = WMWhiteColor(scr->wmscreen);
468 panel->font = WMBoldSystemFontOfSize(scr->wmscreen, 12);
469 panel->icons = WMCreateArray(count);
470 panel->images = WMCreateArray(count);
472 panel->win = WMCreateWindow(scr->wmscreen, "");
474 if (!panel->bg) {
475 WMFrame *frame = WMCreateFrame(panel->win);
476 WMColor *darkGray = WMDarkGrayColor(scr->wmscreen);
477 WMSetFrameRelief(frame, WRSimple);
478 WMSetViewExpandsToParent(WMWidgetView(frame), 0, 0, 0, 0);
480 panel->label = WMCreateLabel(panel->win);
481 WMResizeWidget(panel->label, width, LABEL_HEIGHT);
482 WMMoveWidget(panel->label, BORDER_SPACE, BORDER_SPACE + ICON_TILE_SIZE + 5);
483 WMSetLabelRelief(panel->label, WRSimple);
484 WMSetWidgetBackgroundColor(panel->label, darkGray);
485 WMSetLabelFont(panel->label, panel->font);
486 WMSetLabelTextColor(panel->label, panel->white);
488 WMReleaseColor(darkGray);
489 height += 5;
492 WMResizeWidget(panel->win, width + 2 * BORDER_SPACE, height + 2 * BORDER_SPACE);
494 viewport = WMCreateFrame(panel->win);
495 WMResizeWidget(viewport, width, ICON_TILE_SIZE);
496 WMMoveWidget(viewport, BORDER_SPACE, BORDER_SPACE);
497 WMSetFrameRelief(viewport, WRFlat);
499 panel->iconBox = WMCreateFrame(viewport);
500 WMMoveWidget(panel->iconBox, 0, 0);
501 WMResizeWidget(panel->iconBox, ICON_TILE_SIZE * count, ICON_TILE_SIZE);
502 WMSetFrameRelief(panel->iconBox, WRFlat);
504 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
505 addIconForWindow(panel, panel->iconBox, wwin, i * ICON_TILE_SIZE, 0);
508 WMMapSubwidgets(panel->win);
509 WMRealizeWidget(panel->win);
511 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
512 changeImage(panel, i, 0, False, True);
515 if (panel->bg) {
516 Pixmap pixmap, mask;
518 RConvertImageMask(scr->rcontext, panel->bg, &pixmap, &mask, 250);
520 XSetWindowBackgroundPixmap(dpy, WMWidgetXID(panel->win), pixmap);
522 #ifdef USE_XSHAPE
523 if (mask && w_global.xext.shape.supported)
524 XShapeCombineMask(dpy, WMWidgetXID(panel->win), ShapeBounding, 0, 0, mask, ShapeSet);
525 #endif
526 if (pixmap)
527 XFreePixmap(dpy, pixmap);
529 if (mask)
530 XFreePixmap(dpy, mask);
534 WMPoint center;
535 center = wGetPointToCenterRectInHead(scr, wGetHeadForPointerLocation(scr),
536 width + 2 * BORDER_SPACE, height + 2 * BORDER_SPACE);
537 WMMoveWidget(panel->win, center.x, center.y);
540 panel->current = WMGetFirstInArray(panel->windows, curwin);
541 if (panel->current >= 0)
542 changeImage(panel, panel->current, 1, False, False);
544 WMMapWidget(panel->win);
546 return panel;
549 void wSwitchPanelDestroy(WSwitchPanel *panel)
551 int i;
552 RImage *image;
554 if (panel->win) {
555 Window info_win = panel->scr->info_window;
556 XEvent ev;
557 ev.xclient.type = ClientMessage;
558 ev.xclient.message_type = w_global.atom.wm.ignore_focus_events;
559 ev.xclient.format = 32;
560 ev.xclient.data.l[0] = True;
562 XSendEvent(dpy, info_win, True, EnterWindowMask, &ev);
563 WMUnmapWidget(panel->win);
565 ev.xclient.data.l[0] = False;
566 XSendEvent(dpy, info_win, True, EnterWindowMask, &ev);
569 if (panel->images) {
570 WM_ITERATE_ARRAY(panel->images, image, i) {
571 if (image)
572 RReleaseImage(image);
574 WMFreeArray(panel->images);
577 if (panel->win)
578 WMDestroyWidget(panel->win);
580 if (panel->icons)
581 WMFreeArray(panel->icons);
583 if (panel->flags)
584 WMFreeArray(panel->flags);
586 WMFreeArray(panel->windows);
588 if (panel->tile)
589 RReleaseImage(panel->tile);
591 if (panel->tileTmp)
592 RReleaseImage(panel->tileTmp);
594 if (panel->bg)
595 RReleaseImage(panel->bg);
597 if (panel->font)
598 WMReleaseFont(panel->font);
600 if (panel->white)
601 WMReleaseColor(panel->white);
603 wfree(panel);
606 WWindow *wSwitchPanelSelectNext(WSwitchPanel *panel, int back, int ignore_minimized, Bool class_only)
608 WWindow *wwin, *curwin, *tmpwin;
609 int count = WMGetArrayItemCount(panel->windows);
610 int orig = panel->current;
611 int i;
612 Bool dim = False;
614 if (count == 0)
615 return NULL;
617 if (!wPreferences.cycle_ignore_minimized)
618 ignore_minimized = False;
620 if (ignore_minimized && canReceiveFocus(WMGetFromArray(panel->windows, (count + panel->current) % count)) < 0)
621 ignore_minimized = False;
623 curwin = WMGetFromArray(panel->windows, orig);
624 do {
625 do {
626 if (back)
627 panel->current--;
628 else
629 panel->current++;
631 panel->current= (count + panel->current) % count;
632 wwin = WMGetFromArray(panel->windows, panel->current);
634 if (!class_only)
635 break;
636 if (panel->current == orig)
637 break;
638 } while (!sameWindowClass(wwin, curwin));
639 } while (ignore_minimized && panel->current != orig && canReceiveFocus(wwin) < 0);
641 WM_ITERATE_ARRAY(panel->windows, tmpwin, i) {
642 if (i == panel->current)
643 continue;
644 if (!class_only || sameWindowClass(tmpwin, curwin))
645 changeImage(panel, i, 0, False, False);
646 else {
647 if (i == orig)
648 dim = True;
649 changeImage(panel, i, 0, True, False);
654 if (panel->current < panel->firstVisible)
655 scrollIcons(panel, panel->current - panel->firstVisible);
656 else if (panel->current - panel->firstVisible >= panel->visibleCount)
657 scrollIcons(panel, panel->current - panel->firstVisible - panel->visibleCount + 1);
659 if (panel->win) {
660 drawTitle(panel, panel->current, wwin->frame->title);
661 if (panel->current != orig)
662 changeImage(panel, orig, 0, dim, False);
663 changeImage(panel, panel->current, 1, False, False);
666 return wwin;
669 WWindow *wSwitchPanelSelectFirst(WSwitchPanel *panel, int back)
671 WWindow *wwin;
672 int count = WMGetArrayItemCount(panel->windows);
673 char *title;
674 int i;
676 if (count == 0)
677 return NULL;
679 if (back) {
680 panel->current = count - 1;
681 scrollIcons(panel, count);
682 } else {
683 panel->current = 0;
684 scrollIcons(panel, -count);
687 wwin = WMGetFromArray(panel->windows, panel->current);
688 title = wwin->frame->title;
690 if (panel->win) {
691 for (WMArrayFirst(panel->windows, &(i)); (i) != WANotFound; WMArrayNext(panel->windows, &(i)))
692 changeImage(panel, i, i == panel->current, False, False);
694 drawTitle(panel, panel->current, title);
697 return wwin;
700 WWindow *wSwitchPanelHandleEvent(WSwitchPanel *panel, XEvent *event)
702 WMFrame *icon;
703 int i;
704 int focus = -1;
706 if (!panel->win)
707 return NULL;
709 if (event->type == MotionNotify) {
710 WM_ITERATE_ARRAY(panel->icons, icon, i) {
711 if (WMWidgetXID(icon) == event->xmotion.window) {
712 focus = i;
713 break;
718 if (focus >= 0 && panel->current != focus) {
719 WWindow *wwin;
721 WM_ITERATE_ARRAY(panel->windows, wwin, i) {
722 changeImage(panel, i, i == focus, False, False);
724 panel->current = focus;
726 wwin = WMGetFromArray(panel->windows, focus);
728 drawTitle(panel, panel->current, wwin->frame->title);
730 return wwin;
733 return NULL;
736 Window wSwitchPanelGetWindow(WSwitchPanel *swpanel)
738 if (!swpanel->win)
739 return None;
741 return WMWidgetXID(swpanel->win);