summaryrefslogtreecommitdiff
path: root/src/workspaces.c
blob: 0e316ca41aff1a79ef64a99ba35528e534eac6c9 (plain)
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
/*
        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; You may only use version 2 of the License,
        you have no option to use any other 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 
        oroborus - (c) 2001 Ken Lynch
        xfwm4    - (c) 2002-2003 Olivier Fourdan
 
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xmd.h>
#include <gtk/gtk.h>
#include <glib.h>
#include "main.h"
#include "workspaces.h"
#include "settings.h"
#include "client.h"
#include "hints.h"
#include "debug.h"

void workspaceSwitch(int new_ws, Client * c2)
{
    Client *c, *f = NULL;
    Client *previous;
    unsigned long data[1];
    int i;
    XEvent an_event;

    DBG("entering workspaceSwitch\n");

    if((new_ws < 0) && params.wrap_workspaces)
    {
        new_ws = params.workspace_count - 1;
    }
    if((new_ws > params.workspace_count - 1) && params.wrap_workspaces)
    {
        new_ws = 0;
    }
    if(new_ws == workspace)
    {
        return;
    }

    previous = clientGetFocus();
    if(previous)
    {
        CLIENT_FLAG_SET(previous, CLIENT_FLAG_FOCUS);
        if(CLIENT_FLAG_TEST(previous, CLIENT_FLAG_STICKY))
        {
            f = previous;
        }
    }

    if(c2)
    {
        clientSetWorkspace(c2, new_ws, FALSE);
    }

    workspace = new_ws;

    /* First pass */
    for(c = clients->prev, i = 0; i < client_count; c = c->prev, i++)
    {
        if(CLIENT_FLAG_TEST_AND_NOT(c, CLIENT_FLAG_VISIBLE, CLIENT_FLAG_STICKY) && !(c->transient_for) && ((c->win_workspace != new_ws)))
        {
            clientHide(c, False);
        }
    }
    /* Second pass */
    for(c = clients, i = 0; i < client_count; c = c->next, i++)
    {
        if(CLIENT_FLAG_TEST(c, CLIENT_FLAG_STICKY))
        {
            clientSetWorkspace(c, new_ws, TRUE);
        }
        else
        {
            if((c->win_workspace == new_ws) && !(c->transient_for) && !CLIENT_FLAG_TEST(c, CLIENT_FLAG_HIDDEN))
            {
                clientShow(c, False);
                if((!f) && CLIENT_FLAG_TEST(c, CLIENT_FLAG_FOCUS))
                {
                    f = c;
                }
                CLIENT_FLAG_UNSET(c, CLIENT_FLAG_FOCUS);
            }
        }
    }

    setGnomeHint(dpy, root, win_workspace, new_ws);
    data[0] = new_ws;
    XChangeProperty(dpy, root, net_current_desktop, XA_CARDINAL, 32, PropModeReplace, (unsigned char *)data, 1);
    workspaceUpdateArea(margins, gnome_margins);
    /* Just get rid of EnterNotify events when using focus follow mouse */
    XSync(dpy, 0);
    if(!params.click_to_focus)
    {
        while(XCheckTypedEvent(dpy, EnterNotify, &an_event))
            ;
    }
    clientSetFocus(f, True);
}

void workspaceSetCount(int count)
{
    Client *c;
    int i;

    DBG("entering workspaceSetCount\n");

    if(count < 1)
    {
        count = 1;
    }
    if(count == params.workspace_count)
    {
        return;
    }

    setGnomeHint(dpy, root, win_workspace_count, count);
    setNetHint(dpy, root, net_number_of_desktops, count);
    params.workspace_count = count;

    for(c = clients, i = 0; i < client_count; c = c->next, i++)
    {
        if(c->win_workspace > count - 1)
        {
            clientSetWorkspace(c, count - 1, TRUE);
        }
    }
    if(workspace > count - 1)
    {
        workspaceSwitch(count - 1, NULL);
    }
    set_net_workarea(dpy, root, params.workspace_count, margins);
}

void workspaceUpdateArea(CARD32 * margins, CARD32 * gnome_margins)
{
    Client *c;
    int i;
    int prev_top = margins[MARGIN_TOP];
    int prev_left = margins[MARGIN_LEFT];
    int prev_right = margins[MARGIN_RIGHT];
    int prev_bottom = margins[MARGIN_BOTTOM];

    DBG("entering workspaceSetCount\n");

    for(i = 0; i < 4; i++)
    {
        margins[i] = gnome_margins[i];
    }
    for(c = clients, i = 0; i < client_count; c = c->next, i++)
    {
        if(CLIENT_FLAG_TEST_ALL(c, CLIENT_FLAG_HAS_STRUTS | CLIENT_FLAG_VISIBLE))
        {
            margins[MARGIN_TOP] = MAX(margins[MARGIN_TOP], c->struts[MARGIN_TOP]);
            margins[MARGIN_LEFT] = MAX(margins[MARGIN_LEFT], c->struts[MARGIN_LEFT]);
            margins[MARGIN_RIGHT] = MAX(margins[MARGIN_RIGHT], c->struts[MARGIN_RIGHT]);
            margins[MARGIN_BOTTOM] = MAX(margins[MARGIN_BOTTOM], c->struts[MARGIN_BOTTOM]);
        }
    }
    DBG("Desktop area computed : (%d,%d,%d,%d)\n", margins[0], margins[1], margins[2], margins[3]);
    if((prev_top != margins[MARGIN_TOP]) || (prev_left != margins[MARGIN_LEFT]) || (prev_right != margins[MARGIN_RIGHT]) || (prev_bottom != margins[MARGIN_BOTTOM]))
    {
        DBG("Margins have changed, updating net_workarea\n");
        set_net_workarea(dpy, screen, params.workspace_count, margins);
    }
}