Files
wmaker/src/workspace.c
T

981 lines
26 KiB
C
Raw Normal View History

1998-09-29 22:36:29 +00:00
/* workspace.c- Workspace management
*
1998-10-21 14:43:47 +00:00
* Window Maker window manager
*
2003-01-16 23:30:45 +00:00
* Copyright (c) 1997-2003 Alfredo K. Kojima
*
1998-09-29 22:36:29 +00:00
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1998-09-29 22:36:29 +00:00
*/
#include "wconfig.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef USE_XSHAPE
1999-04-17 18:24:23 +00:00
#include <X11/extensions/shape.h>
#endif
1998-09-29 22:36:29 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
1998-09-29 22:36:29 +00:00
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
1998-09-29 22:36:29 +00:00
#include "WindowMaker.h"
#include "framewin.h"
#include "window.h"
#include "icon.h"
2013-04-14 23:13:26 +02:00
#include "misc.h"
1998-09-29 22:36:29 +00:00
#include "menu.h"
#include "application.h"
#include "dock.h"
#include "actions.h"
#include "workspace.h"
#include "appicon.h"
#include "wmspec.h"
#include "xinerama.h"
2013-05-15 13:07:52 +02:00
#include "event.h"
2014-12-04 13:37:36 +07:00
#include "wsmap.h"
#include "dialog.h"
1998-09-29 22:36:29 +00:00
2013-03-27 13:05:03 +00:00
#define MC_DESTROY_LAST 1
#define MC_LAST_USED 2
2013-03-27 13:05:03 +00:00
/* index of the first workspace menu entry */
#define MC_WORKSPACE1 3
2013-03-27 13:05:03 +00:00
2009-09-14 13:28:37 +01:00
#define WORKSPACE_NAME_DISPLAY_PADDING 32
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
static WMPropList *dWorkspaces = NULL;
static WMPropList *dClip, *dName;
1998-09-29 22:36:29 +00:00
2010-04-12 23:31:59 +02:00
static void make_keys(void)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
if (dWorkspaces != NULL)
return;
2009-08-20 00:59:40 +02:00
dWorkspaces = WMCreatePLString("Workspaces");
dName = WMCreatePLString("Name");
dClip = WMCreatePLString("Clip");
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
void wWorkspaceMake(WScreen * scr, int count)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
while (count > 0) {
wWorkspaceNew(scr);
count--;
}
1998-09-29 22:36:29 +00:00
}
2013-03-20 04:02:15 +01:00
int wWorkspaceNew(WScreen *scr)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
WWorkspace *wspace, **list;
int i;
if (scr->workspace_count < MAX_WORKSPACES) {
scr->workspace_count++;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
wspace = wmalloc(sizeof(WWorkspace));
wspace->name = NULL;
2013-03-20 04:02:19 +01:00
wspace->clip = NULL;
1999-01-06 15:22:33 +00:00
2009-08-20 00:59:40 +02:00
if (!wspace->name) {
2013-11-10 17:41:08 +01:00
static const char *new_name = NULL;
static size_t name_length;
if (new_name == NULL) {
new_name = _("Workspace %i");
name_length = strlen(new_name) + 8;
}
wspace->name = wmalloc(name_length);
snprintf(wspace->name, name_length, new_name, scr->workspace_count);
2009-08-20 00:59:40 +02:00
}
1999-01-06 15:22:33 +00:00
2013-03-20 04:02:19 +01:00
if (!wPreferences.flags.noclip)
2013-04-12 01:42:41 +02:00
wspace->clip = wDockCreate(scr, WM_CLIP, NULL);
1998-09-29 22:36:29 +00:00
list = wmalloc(sizeof(WWorkspace *) * scr->workspace_count);
1998-09-29 22:36:29 +00:00
for (i = 0; i < scr->workspace_count - 1; i++)
list[i] = scr->workspaces[i];
2013-03-20 04:02:15 +01:00
2009-08-20 00:59:40 +02:00
list[i] = wspace;
if (scr->workspaces)
wfree(scr->workspaces);
2013-03-20 04:02:15 +01:00
scr->workspaces = list;
1999-01-06 15:22:33 +00:00
wWorkspaceMenuUpdate(scr, scr->workspace_menu);
wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
2009-08-20 00:59:40 +02:00
wNETWMUpdateDesktop(scr);
WMPostNotificationName(WMNWorkspaceCreated, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
2009-08-20 00:59:40 +02:00
XFlush(dpy);
1999-01-06 15:22:33 +00:00
return scr->workspace_count - 1;
2009-08-20 00:59:40 +02:00
}
2009-08-20 00:59:40 +02:00
return -1;
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
Bool wWorkspaceDelete(WScreen * scr, int workspace)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
WWindow *tmp;
WWorkspace **list;
int i, j;
if (workspace <= 0)
return False;
/* verify if workspace is in use by some window */
tmp = scr->focused_window;
while (tmp) {
if (!IS_OMNIPRESENT(tmp) && tmp->frame->workspace == workspace) {
char buf[256];
snprintf(buf, sizeof(buf), _("Workspace \"%s\" in use; cannot delete"),
scr->workspaces[workspace]->name);
wMessageDialog(scr, _("Error"), buf, _("OK"), NULL, NULL);
2009-08-20 00:59:40 +02:00
return False;
}
2009-08-20 00:59:40 +02:00
tmp = tmp->prev;
}
if (!wPreferences.flags.noclip) {
wDockDestroy(scr->workspaces[workspace]->clip);
scr->workspaces[workspace]->clip = NULL;
2009-08-20 00:59:40 +02:00
}
list = wmalloc(sizeof(WWorkspace *) * (scr->workspace_count - 1));
2009-08-20 00:59:40 +02:00
j = 0;
for (i = 0; i < scr->workspace_count; i++) {
2009-08-20 00:59:40 +02:00
if (i != workspace) {
list[j++] = scr->workspaces[i];
2009-08-20 00:59:40 +02:00
} else {
if (scr->workspaces[i]->name)
wfree(scr->workspaces[i]->name);
2014-12-04 13:37:36 +07:00
if (scr->workspaces[i]->map)
RReleaseImage(scr->workspaces[i]->map);
wfree(scr->workspaces[i]);
2009-08-20 00:59:40 +02:00
}
}
wfree(scr->workspaces);
scr->workspaces = list;
2009-08-20 00:59:40 +02:00
scr->workspace_count--;
2009-08-20 00:59:40 +02:00
/* update menu */
wWorkspaceMenuUpdate(scr, scr->workspace_menu);
2009-08-20 00:59:40 +02:00
/* clip workspace menu */
wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
2009-08-20 00:59:40 +02:00
/* update also window menu */
if (scr->workspace_submenu) {
WMenu *menu = scr->workspace_submenu;
2009-08-20 00:59:40 +02:00
i = menu->entry_no;
while (i > scr->workspace_count)
2009-08-20 00:59:40 +02:00
wMenuRemoveItem(menu, --i);
wMenuRealize(menu);
}
/* and clip menu */
if (scr->clip_submenu) {
WMenu *menu = scr->clip_submenu;
2009-08-20 00:59:40 +02:00
i = menu->entry_no;
while (i > scr->workspace_count)
2009-08-20 00:59:40 +02:00
wMenuRemoveItem(menu, --i);
wMenuRealize(menu);
}
wNETWMUpdateDesktop(scr);
WMPostNotificationName(WMNWorkspaceDestroyed, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
1999-01-06 15:22:33 +00:00
if (scr->current_workspace >= scr->workspace_count)
wWorkspaceChange(scr, scr->workspace_count - 1);
if (scr->last_workspace >= scr->workspace_count)
scr->last_workspace = 0;
1999-01-06 15:22:33 +00:00
2009-08-20 00:59:40 +02:00
return True;
1998-09-29 22:36:29 +00:00
}
1999-04-17 18:24:23 +00:00
typedef struct WorkspaceNameData {
2009-08-20 00:59:40 +02:00
int count;
RImage *back;
RImage *text;
time_t timeout;
1999-04-17 18:24:23 +00:00
} WorkspaceNameData;
2009-08-20 00:59:40 +02:00
static void hideWorkspaceName(void *data)
1999-04-17 18:24:23 +00:00
{
2009-08-20 00:59:40 +02:00
WScreen *scr = (WScreen *) data;
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
|| time(NULL) > scr->workspace_name_data->timeout) {
XUnmapWindow(dpy, scr->workspace_name);
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
if (scr->workspace_name_data) {
RReleaseImage(scr->workspace_name_data->back);
RReleaseImage(scr->workspace_name_data->text);
wfree(scr->workspace_name_data);
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
scr->workspace_name_data = NULL;
}
scr->workspace_name_timer = NULL;
} else {
RImage *img = RCloneImage(scr->workspace_name_data->back);
Pixmap pix;
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
1999-04-17 23:11:34 +00:00
2009-08-20 00:59:40 +02:00
RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
scr->workspace_name_data->count * 255 / 10);
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
RConvertImage(scr->rcontext, img, &pix);
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
RReleaseImage(img);
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
XClearWindow(dpy, scr->workspace_name);
XFreePixmap(dpy, pix);
XFlush(dpy);
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
scr->workspace_name_data->count--;
}
1999-04-17 18:24:23 +00:00
}
2009-08-20 00:59:40 +02:00
static void showWorkspaceName(WScreen * scr, int workspace)
1999-04-17 18:24:23 +00:00
{
2009-08-20 00:59:40 +02:00
WorkspaceNameData *data;
RXImage *ximg;
Pixmap text, mask;
int w, h;
int px, py;
char *name = scr->workspaces[workspace]->name;
2009-08-20 00:59:40 +02:00
int len = strlen(name);
int x, y;
#ifdef USE_XINERAMA
int head;
WMRect rect;
int xx, yy;
#endif
2009-08-20 00:59:40 +02:00
if (wPreferences.workspace_name_display_position == WD_NONE || scr->workspace_count < 2)
2009-08-20 00:59:40 +02:00
return;
if (scr->workspace_name_timer) {
WMDeleteTimerHandler(scr->workspace_name_timer);
XUnmapWindow(dpy, scr->workspace_name);
XFlush(dpy);
}
scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY, hideWorkspaceName, scr);
if (scr->workspace_name_data) {
RReleaseImage(scr->workspace_name_data->back);
RReleaseImage(scr->workspace_name_data->text);
wfree(scr->workspace_name_data);
}
data = wmalloc(sizeof(WorkspaceNameData));
data->back = NULL;
w = WMWidthOfString(scr->workspace_name_font, name, len);
h = WMFontHeight(scr->workspace_name_font);
2009-08-20 00:59:40 +02:00
#ifdef USE_XINERAMA
head = wGetHeadForPointerLocation(scr);
rect = wGetRectForHead(scr, head);
if (scr->xine_info.count) {
2009-09-14 13:28:37 +01:00
xx = rect.pos.x + (scr->xine_info.screens[head].size.width - (w + 4)) / 2;
yy = rect.pos.y + (scr->xine_info.screens[head].size.height - (h + 4)) / 2;
}
else {
2009-09-14 13:28:37 +01:00
xx = (scr->scr_width - (w + 4)) / 2;
yy = (scr->scr_height - (h + 4)) / 2;
}
#endif
2009-08-20 00:59:40 +02:00
switch (wPreferences.workspace_name_display_position) {
case WD_TOP:
#ifdef USE_XINERAMA
px = xx;
#else
2009-08-20 00:59:40 +02:00
px = (scr->scr_width - (w + 4)) / 2;
#endif
2009-09-14 13:28:37 +01:00
py = WORKSPACE_NAME_DISPLAY_PADDING;
2009-08-20 00:59:40 +02:00
break;
case WD_BOTTOM:
#ifdef USE_XINERAMA
px = xx;
#else
2009-08-20 00:59:40 +02:00
px = (scr->scr_width - (w + 4)) / 2;
#endif
2009-09-14 13:28:37 +01:00
py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
2009-08-20 00:59:40 +02:00
break;
case WD_TOPLEFT:
2009-09-14 13:28:37 +01:00
px = WORKSPACE_NAME_DISPLAY_PADDING;
py = WORKSPACE_NAME_DISPLAY_PADDING;
2009-08-20 00:59:40 +02:00
break;
case WD_TOPRIGHT:
2009-09-14 13:28:37 +01:00
px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
py = WORKSPACE_NAME_DISPLAY_PADDING;
2009-08-20 00:59:40 +02:00
break;
case WD_BOTTOMLEFT:
2009-09-14 13:28:37 +01:00
px = WORKSPACE_NAME_DISPLAY_PADDING;
py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
2009-08-20 00:59:40 +02:00
break;
case WD_BOTTOMRIGHT:
2009-09-14 13:28:37 +01:00
px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
2009-08-20 00:59:40 +02:00
break;
case WD_CENTER:
default:
#ifdef USE_XINERAMA
px = xx;
py = yy;
#else
2009-08-20 00:59:40 +02:00
px = (scr->scr_width - (w + 4)) / 2;
py = (scr->scr_height - (h + 4)) / 2;
#endif
2009-08-20 00:59:40 +02:00
break;
}
XResizeWindow(dpy, scr->workspace_name, w + 4, h + 4);
XMoveWindow(dpy, scr->workspace_name, px, py);
text = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, scr->w_depth);
mask = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, 1);
/*XSetForeground(dpy, scr->mono_gc, 0);
XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
2013-10-08 00:56:28 +02:00
for (x = 0; x <= 4; x++)
for (y = 0; y <= 4; y++)
WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, x, y, name, len);
2009-08-20 00:59:40 +02:00
XSetForeground(dpy, scr->mono_gc, 1);
XSetBackground(dpy, scr->mono_gc, 0);
XCopyPlane(dpy, text, mask, scr->mono_gc, 0, 0, w + 4, h + 4, 0, 0, 1 << (scr->w_depth - 1));
/*XSetForeground(dpy, scr->mono_gc, 1); */
XSetBackground(dpy, scr->mono_gc, 1);
XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, 2, 2, name, len);
1999-04-17 18:24:23 +00:00
#ifdef USE_XSHAPE
if (w_global.xext.shape.supported)
XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask, ShapeSet);
1999-04-17 18:24:23 +00:00
#endif
2009-08-20 00:59:40 +02:00
XSetWindowBackgroundPixmap(dpy, scr->workspace_name, text);
XClearWindow(dpy, scr->workspace_name);
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
data->text = RCreateImageFromDrawable(scr->rcontext, text, None);
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
XFreePixmap(dpy, text);
XFreePixmap(dpy, mask);
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
if (!data->text) {
XMapRaised(dpy, scr->workspace_name);
XFlush(dpy);
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
goto erro;
}
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
ximg = RGetXImage(scr->rcontext, scr->root_win, px, py, data->text->width, data->text->height);
if (!ximg)
2009-08-20 00:59:40 +02:00
goto erro;
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
XMapRaised(dpy, scr->workspace_name);
XFlush(dpy);
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
data->back = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
RDestroyXImage(scr->rcontext, ximg);
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
if (!data->back) {
goto erro;
}
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
data->count = 10;
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
/* set a timeout for the effect */
data->timeout = time(NULL) + 2 + (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY * data->count) / 1000;
2009-08-20 00:59:40 +02:00
scr->workspace_name_data = data;
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
return;
2009-08-20 00:59:40 +02:00
erro:
if (scr->workspace_name_timer)
WMDeleteTimerHandler(scr->workspace_name_timer);
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
if (data->text)
RReleaseImage(data->text);
if (data->back)
RReleaseImage(data->back);
wfree(data);
2009-08-20 00:59:40 +02:00
scr->workspace_name_data = NULL;
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY +
10 * WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
1999-04-17 18:24:23 +00:00
}
2009-09-15 20:06:04 +02:00
void wWorkspaceChange(WScreen *scr, int workspace)
1998-09-29 22:36:29 +00:00
{
2009-03-29 04:14:36 +04:00
if (scr->flags.startup || scr->flags.startup2 || scr->flags.ignore_focus_events)
2009-08-20 00:59:40 +02:00
return;
if (workspace != scr->current_workspace)
2009-08-20 00:59:40 +02:00
wWorkspaceForceChange(scr, workspace);
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
void wWorkspaceRelativeChange(WScreen * scr, int amount)
1999-01-06 15:22:33 +00:00
{
2009-08-20 00:59:40 +02:00
int w;
/* While the deiconify animation is going on the window is
* still "flying" to its final position and we don't want to
* change workspace before the animation finishes, otherwise
* the window will land in the new workspace */
if (w_global.ignore_workspace_change)
return;
w = scr->current_workspace + amount;
2009-08-20 00:59:40 +02:00
if (amount < 0) {
if (w >= 0) {
wWorkspaceChange(scr, w);
} else if (wPreferences.ws_cycle) {
wWorkspaceChange(scr, scr->workspace_count + w);
2009-08-20 00:59:40 +02:00
}
} else if (amount > 0) {
if (w < scr->workspace_count) {
2009-08-20 00:59:40 +02:00
wWorkspaceChange(scr, w);
} else if (wPreferences.ws_advance) {
wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES - 1));
} else if (wPreferences.ws_cycle) {
wWorkspaceChange(scr, w % scr->workspace_count);
2009-08-20 00:59:40 +02:00
}
}
1999-01-06 15:22:33 +00:00
}
2009-08-20 00:59:40 +02:00
void wWorkspaceForceChange(WScreen * scr, int workspace)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
WWindow *tmp, *foc = NULL, *foc2 = NULL;
if (workspace >= MAX_WORKSPACES || workspace < 0)
return;
2015-07-25 14:23:46 -03:00
if (wPreferences.enable_workspace_pager && !w_global.process_workspacemap_event)
2014-12-04 13:37:36 +07:00
wWorkspaceMapUpdate(scr);
2009-08-20 00:59:40 +02:00
SendHelperMessage(scr, 'C', workspace + 1, NULL);
if (workspace > scr->workspace_count - 1)
wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
2009-08-20 00:59:40 +02:00
wClipUpdateForWorkspaceChange(scr, workspace);
scr->last_workspace = scr->current_workspace;
scr->current_workspace = workspace;
2009-08-20 00:59:40 +02:00
wWorkspaceMenuUpdate(scr, scr->workspace_menu);
2009-08-20 00:59:40 +02:00
wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
2009-08-20 00:59:40 +02:00
tmp = scr->focused_window;
if (tmp != NULL) {
WWindow **toUnmap;
int toUnmapSize, toUnmapCount;
2009-08-20 00:59:40 +02:00
if ((IS_OMNIPRESENT(tmp) && (tmp->flags.mapped || tmp->flags.shaded) &&
!WFLAGP(tmp, no_focusable)) || tmp->flags.changing_workspace) {
foc = tmp;
}
toUnmapSize = 16;
toUnmapCount = 0;
toUnmap = wmalloc(toUnmapSize * sizeof(WWindow *));
2009-08-20 00:59:40 +02:00
/* foc2 = tmp; will fix annoyance with gnome panel
* but will create annoyance for every other application
*/
while (tmp) {
if (tmp->frame->workspace != workspace && !tmp->flags.selected) {
/* unmap windows not on this workspace */
if ((tmp->flags.mapped || tmp->flags.shaded) &&
!IS_OMNIPRESENT(tmp) && !tmp->flags.changing_workspace) {
if (toUnmapCount == toUnmapSize)
{
toUnmapSize *= 2;
toUnmap = wrealloc(toUnmap, toUnmapSize * sizeof(WWindow *));
}
toUnmap[toUnmapCount++] = tmp;
2009-08-20 00:59:40 +02:00
}
/* also unmap miniwindows not on this workspace */
if (!wPreferences.sticky_icons && tmp->flags.miniaturized &&
tmp->icon && !IS_OMNIPRESENT(tmp)) {
XUnmapWindow(dpy, tmp->icon->core->window);
tmp->icon->mapped = 0;
}
/* update current workspace of omnipresent windows */
if (IS_OMNIPRESENT(tmp)) {
WApplication *wapp = wApplicationOf(tmp->main_window);
tmp->frame->workspace = workspace;
if (wapp) {
wapp->last_workspace = workspace;
}
if (!foc2 && (tmp->flags.mapped || tmp->flags.shaded)) {
foc2 = tmp;
}
}
} else {
/* change selected windows' workspace */
if (tmp->flags.selected) {
wWindowChangeWorkspace(tmp, workspace);
if (!tmp->flags.miniaturized && !foc) {
foc = tmp;
}
} else {
if (!tmp->flags.hidden) {
if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
/* remap windows that are on this workspace */
wWindowMap(tmp);
if (!foc && !WFLAGP(tmp, no_focusable)) {
foc = tmp;
}
}
/* Also map miniwindow if not omnipresent */
if (!wPreferences.sticky_icons &&
tmp->flags.miniaturized && !IS_OMNIPRESENT(tmp) && tmp->icon) {
tmp->icon->mapped = 1;
XMapWindow(dpy, tmp->icon->core->window);
}
}
}
}
tmp = tmp->prev;
}
while (toUnmapCount > 0)
{
wWindowUnmap(toUnmap[--toUnmapCount]);
}
wfree(toUnmap);
2009-08-20 00:59:40 +02:00
/* Gobble up events unleashed by our mapping & unmapping.
* These may trigger various grab-initiated focus &
* crossing events. However, we don't care about them,
* and ignore their focus implications altogether to avoid
* flicker.
*/
scr->flags.ignore_focus_events = 1;
ProcessPendingEvents();
scr->flags.ignore_focus_events = 0;
if (!foc)
foc = foc2;
/*
* Check that the window we want to focus still exists, because the application owning it
* could decide to unmap/destroy it in response to unmap any of its other window following
* the workspace change, this happening during our 'ProcessPendingEvents' loop.
*/
if (foc != NULL) {
WWindow *parse;
Bool found;
found = False;
for (parse = scr->focused_window; parse != NULL; parse = parse->prev) {
if (parse == foc) {
found = True;
break;
}
}
if (!found)
foc = NULL;
}
2009-08-20 00:59:40 +02:00
if (scr->focused_window->flags.mapped && !foc) {
foc = scr->focused_window;
}
if (wPreferences.focus_mode == WKF_CLICK) {
wSetFocusTo(scr, foc);
} else {
unsigned int mask;
int foo;
Window bar, win;
WWindow *tmp;
tmp = NULL;
if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask)) {
tmp = wWindowFor(win);
}
/* If there's a window under the pointer, focus it.
* (we ate all other focus events above, so it's
* certainly not focused). Otherwise focus last
* focused, or the root (depending on sloppiness)
*/
if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
wSetFocusTo(scr, foc);
} else {
wSetFocusTo(scr, tmp);
}
}
}
/* We need to always arrange icons when changing workspace, even if
* no autoarrange icons, because else the icons in different workspaces
* can be superposed.
* This can be avoided if appicons are also workspace specific.
*/
if (!wPreferences.sticky_icons)
wArrangeIcons(scr, False);
if (scr->dock)
2012-11-10 20:25:14 +01:00
wAppIconPaint(scr->dock->icon_array[0]);
2009-08-20 00:59:40 +02:00
if (!wPreferences.flags.noclip && (scr->workspaces[workspace]->clip->auto_collapse ||
scr->workspaces[workspace]->clip->auto_raise_lower)) {
/* to handle enter notify. This will also */
XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
XMapWindow(dpy, scr->clip_icon->icon->core->window);
}
else if (scr->clip_icon != NULL) {
wClipIconPaint(scr->clip_icon);
2009-08-20 00:59:40 +02:00
}
wScreenUpdateUsableArea(scr);
wNETWMUpdateDesktop(scr);
showWorkspaceName(scr, workspace);
1999-04-17 18:24:23 +00:00
2009-08-20 00:59:40 +02:00
WMPostNotificationName(WMNWorkspaceChanged, scr, (void *)(uintptr_t) workspace);
2009-08-20 00:59:40 +02:00
/* XSync(dpy, False); */
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
1998-09-29 22:36:29 +00:00
}
2013-10-08 00:56:28 +02:00
static void lastWSCommand(WMenu *menu, WMenuEntry *entry)
{
/* Parameter not used, but tell the compiler that it is ok */
(void) entry;
wWorkspaceChange(menu->frame->screen_ptr, menu->frame->screen_ptr->last_workspace);
}
2013-10-08 00:56:28 +02:00
static void deleteWSCommand(WMenu *menu, WMenuEntry *entry)
1998-09-29 22:36:29 +00:00
{
/* Parameter not used, but tell the compiler that it is ok */
(void) entry;
wWorkspaceDelete(menu->frame->screen_ptr, menu->frame->screen_ptr->workspace_count - 1);
1998-09-29 22:36:29 +00:00
}
2013-03-20 04:02:15 +01:00
static void newWSCommand(WMenu *menu, WMenuEntry *foo)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
int ws;
/* Parameter not used, but tell the compiler that it is ok */
(void) foo;
2009-08-20 00:59:40 +02:00
ws = wWorkspaceNew(menu->frame->screen_ptr);
2013-03-20 04:02:15 +01:00
2009-08-20 00:59:40 +02:00
/* autochange workspace */
if (ws >= 0)
wWorkspaceChange(menu->frame->screen_ptr, ws);
1998-09-29 22:36:29 +00:00
}
void wWorkspaceRename(WScreen *scr, int workspace, const char *name)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
char buf[MAX_WORKSPACENAME_WIDTH + 1];
char *tmp;
if (workspace >= scr->workspace_count)
2009-08-20 00:59:40 +02:00
return;
/* trim white spaces */
tmp = wtrimspace(name);
2009-08-20 00:59:40 +02:00
if (strlen(tmp) == 0) {
snprintf(buf, sizeof(buf), _("Workspace %i"), workspace + 1);
} else {
strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
}
buf[MAX_WORKSPACENAME_WIDTH] = 0;
2012-05-05 11:48:00 +02:00
wfree(tmp);
2009-08-20 00:59:40 +02:00
/* update workspace */
wfree(scr->workspaces[workspace]->name);
scr->workspaces[workspace]->name = wstrdup(buf);
if (scr->clip_ws_menu) {
if (strcmp(scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text, buf) != 0) {
wfree(scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text);
scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text = wstrdup(buf);
wMenuRealize(scr->clip_ws_menu);
2009-08-20 00:59:40 +02:00
}
}
if (scr->workspace_menu) {
if (strcmp(scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text, buf) != 0) {
wfree(scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text);
scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text = wstrdup(buf);
wMenuRealize(scr->workspace_menu);
2009-08-20 00:59:40 +02:00
}
}
if (scr->clip_icon)
wClipIconPaint(scr->clip_icon);
2009-08-20 00:59:40 +02:00
WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) workspace);
1998-09-29 22:36:29 +00:00
}
/* callback for when menu entry is edited */
2009-08-20 00:59:40 +02:00
static void onMenuEntryEdited(WMenu * menu, WMenuEntry * entry)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
char *tmp;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
tmp = entry->text;
wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
1998-09-29 22:36:29 +00:00
}
2009-08-20 00:59:40 +02:00
WMenu *wWorkspaceMenuMake(WScreen * scr, Bool titled)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
WMenu *wsmenu;
WMenuEntry *entry;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
if (!wsmenu) {
wwarning(_("could not create Workspace menu"));
return NULL;
}
2009-08-20 00:59:40 +02:00
/* callback to be called when an entry is edited */
wsmenu->on_edit = onMenuEntryEdited;
2009-08-20 00:59:40 +02:00
wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
1998-09-29 22:36:29 +00:00
entry = wMenuAddCallback(wsmenu, _("Last Used"), lastWSCommand, NULL);
entry->rtext = GetShortcutKey(wKeyBindings[WKBD_LASTWORKSPACE]);
2009-08-20 00:59:40 +02:00
return wsmenu;
1998-09-29 22:36:29 +00:00
}
void wWorkspaceMenuUpdate(WScreen * scr, WMenu * menu)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
int i;
long ws;
char title[MAX_WORKSPACENAME_WIDTH + 1];
WMenuEntry *entry;
int tmp;
if (!menu)
return;
if (menu->entry_no < scr->workspace_count + MC_WORKSPACE1) {
2009-08-20 00:59:40 +02:00
/* new workspace(s) added */
i = scr->workspace_count - (menu->entry_no - MC_WORKSPACE1);
2013-03-27 13:05:03 +00:00
ws = menu->entry_no - MC_WORKSPACE1;
2009-08-20 00:59:40 +02:00
while (i > 0) {
wstrlcpy(title, scr->workspaces[ws]->name, MAX_WORKSPACENAME_WIDTH);
2009-08-20 00:59:40 +02:00
entry = wMenuAddCallback(menu, title, switchWSCommand, (void *)ws);
entry->flags.indicator = 1;
entry->flags.editable = 1;
i--;
ws++;
}
} else if (menu->entry_no > scr->workspace_count + MC_WORKSPACE1) {
2009-08-20 00:59:40 +02:00
/* removed workspace(s) */
for (i = menu->entry_no - 1; i >= scr->workspace_count + MC_WORKSPACE1; i--)
2009-08-20 00:59:40 +02:00
wMenuRemoveItem(menu, i);
}
for (i = 0; i < scr->workspace_count; i++) {
/* workspace shortcut labels */
if (i / 10 == scr->current_workspace / 10)
menu->entries[i + MC_WORKSPACE1]->rtext = GetShortcutKey(wKeyBindings[WKBD_WORKSPACE1 + (i % 10)]);
else
menu->entries[i + MC_WORKSPACE1]->rtext = NULL;
2013-03-27 13:05:03 +00:00
menu->entries[i + MC_WORKSPACE1]->flags.indicator_on = 0;
2009-08-20 00:59:40 +02:00
}
menu->entries[scr->current_workspace + MC_WORKSPACE1]->flags.indicator_on = 1;
wMenuRealize(menu);
2009-08-20 00:59:40 +02:00
/* don't let user destroy current workspace */
if (scr->current_workspace == scr->workspace_count - 1)
2013-03-27 13:05:03 +00:00
wMenuSetEnabled(menu, MC_DESTROY_LAST, False);
2013-10-08 00:56:28 +02:00
else
2013-03-27 13:05:03 +00:00
wMenuSetEnabled(menu, MC_DESTROY_LAST, True);
2009-08-20 00:59:40 +02:00
/* back to last workspace */
if (scr->workspace_count && scr->last_workspace != scr->current_workspace)
wMenuSetEnabled(menu, MC_LAST_USED, True);
else
wMenuSetEnabled(menu, MC_LAST_USED, False);
2009-08-20 00:59:40 +02:00
tmp = menu->frame->top_width + 5;
/* if menu got unreachable, bring it to a visible place */
if (menu->frame_x < tmp - (int)menu->frame->core->width)
wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
wMenuPaint(menu);
1998-09-29 22:36:29 +00:00
}
void wWorkspaceSaveState(WScreen * scr, WMPropList * old_state)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
WMPropList *parr, *pstr, *wks_state, *old_wks_state, *foo, *bar;
int i;
make_keys();
old_wks_state = WMGetFromPLDictionary(old_state, dWorkspaces);
parr = WMCreatePLArray(NULL);
for (i = 0; i < scr->workspace_count; i++) {
pstr = WMCreatePLString(scr->workspaces[i]->name);
2009-08-20 00:59:40 +02:00
wks_state = WMCreatePLDictionary(dName, pstr, NULL);
WMReleasePropList(pstr);
if (!wPreferences.flags.noclip) {
pstr = wClipSaveWorkspaceState(scr, i);
2009-08-20 00:59:40 +02:00
WMPutInPLDictionary(wks_state, dClip, pstr);
WMReleasePropList(pstr);
} else if (old_wks_state != NULL) {
foo = WMGetFromPLArray(old_wks_state, i);
if (foo != NULL) {
bar = WMGetFromPLDictionary(foo, dClip);
if (bar != NULL)
2009-08-20 00:59:40 +02:00
WMPutInPLDictionary(wks_state, dClip, bar);
}
}
WMAddToPLArray(parr, wks_state);
WMReleasePropList(wks_state);
}
WMPutInPLDictionary(scr->session_state, dWorkspaces, parr);
2009-08-20 00:59:40 +02:00
WMReleasePropList(parr);
1998-09-29 22:36:29 +00:00
}
2013-03-20 04:02:15 +01:00
void wWorkspaceRestoreState(WScreen *scr)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
WMPropList *parr, *pstr, *wks_state, *clip_state;
2011-03-24 16:07:20 +01:00
int i, j;
2009-08-20 00:59:40 +02:00
make_keys();
if (scr->session_state == NULL)
2009-08-20 00:59:40 +02:00
return;
parr = WMGetFromPLDictionary(scr->session_state, dWorkspaces);
2009-08-20 00:59:40 +02:00
if (!parr)
return;
for (i = 0; i < WMIN(WMGetPropListItemCount(parr), MAX_WORKSPACES); i++) {
wks_state = WMGetFromPLArray(parr, i);
if (WMIsPLDictionary(wks_state))
pstr = WMGetFromPLDictionary(wks_state, dName);
else
pstr = wks_state;
2013-03-20 04:02:15 +01:00
if (i >= scr->workspace_count)
2009-08-20 00:59:40 +02:00
wWorkspaceNew(scr);
2013-03-20 04:02:15 +01:00
if (scr->workspace_menu) {
wfree(scr->workspace_menu->entries[i + MC_WORKSPACE1]->text);
scr->workspace_menu->entries[i + MC_WORKSPACE1]->text = wstrdup(WMGetFromPLString(pstr));
scr->workspace_menu->flags.realized = 0;
2009-08-20 00:59:40 +02:00
}
2013-03-20 04:02:15 +01:00
wfree(scr->workspaces[i]->name);
scr->workspaces[i]->name = wstrdup(WMGetFromPLString(pstr));
2009-08-20 00:59:40 +02:00
if (!wPreferences.flags.noclip) {
2009-09-02 00:05:46 +02:00
int added_omnipresent_icons = 0;
2013-03-20 04:02:15 +01:00
2009-08-20 00:59:40 +02:00
clip_state = WMGetFromPLDictionary(wks_state, dClip);
if (scr->workspaces[i]->clip)
wDockDestroy(scr->workspaces[i]->clip);
2013-03-20 04:02:15 +01:00
scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state, WM_CLIP);
2009-08-20 00:59:40 +02:00
if (i > 0)
wDockHideIcons(scr->workspaces[i]->clip);
2009-08-20 00:59:40 +02:00
/* We set the global icons here, because scr->workspaces[i]->clip
* was not valid in wDockRestoreState().
* There we only set icon->omnipresent to know which icons we
* need to set here.
*/
for (j = 0; j < scr->workspaces[i]->clip->max_icons; j++) {
WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
2009-09-02 00:05:46 +02:00
int k;
if (!aicon || !aicon->omnipresent)
continue;
aicon->omnipresent = 0;
if (wClipMakeIconOmnipresent(aicon, True) != WO_SUCCESS)
continue;
if (i == 0)
continue;
/* Move this appicon from workspace i to workspace 0 */
scr->workspaces[i]->clip->icon_array[j] = NULL;
scr->workspaces[i]->clip->icon_count--;
2009-09-02 00:05:46 +02:00
added_omnipresent_icons++;
/* If there are too many omnipresent appicons, we are in trouble */
assert(scr->workspaces[0]->clip->icon_count + added_omnipresent_icons
<= scr->workspaces[0]->clip->max_icons);
2009-09-02 00:05:46 +02:00
/* Find first free spot on workspace 0 */
for (k = 0; k < scr->workspaces[0]->clip->max_icons; k++)
if (scr->workspaces[0]->clip->icon_array[k] == NULL)
2009-09-02 00:05:46 +02:00
break;
scr->workspaces[0]->clip->icon_array[k] = aicon;
aicon->dock = scr->workspaces[0]->clip;
2009-08-20 00:59:40 +02:00
}
scr->workspaces[0]->clip->icon_count += added_omnipresent_icons;
2009-08-20 00:59:40 +02:00
}
WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) i);
}
1998-09-29 22:36:29 +00:00
}
/* Returns the workspace number for a given workspace name */
int wGetWorkspaceNumber(WScreen *scr, const char *value)
{
int w, i;
if (sscanf(value, "%i", &w) != 1) {
w = -1;
for (i = 0; i < scr->workspace_count; i++) {
if (strcmp(scr->workspaces[i]->name, value) == 0) {
w = i;
break;
}
}
} else {
w--;
}
return w;
}