summaryrefslogtreecommitdiff
path: root/src/FloorControl.cpp
blob: 42511fb6c86238154b8aa170137218b0b25c4c82 (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
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
/* $Id: FloorControl.cpp,v 1.9 2007/01/07 05:00:56 whot Exp $ */

/*--
 --*/

#include "FloorControl.h"

FloorControl::FloorControl(Manager* manager, WMWindow* client, XConn* x11)
{
    this->manager = manager;
    this->client = client;
    this->x11 = x11;
}

FloorControl::~FloorControl()
{
    XDestroyWindow(x11->dpy, root->fcwin);
    freeSubNodes(root);
    delete root;
}

/**
 * Frees all subnodes of a node (recursively). 
 * Does NOT release the memory for the node itself.
 */
void FloorControl::freeSubNodes(WinTreeNodePtr node)
{
    vector<WinTreeNodePtr>::iterator it = node->children.begin();
    while(it != node->children.end())
    {
        freeSubNodes(*it);
        delete (*it);
        it++;
    }
}

/**
 * Queries a WinTreeNode's window for the subchildren and fills the data into
 * the node. This works recursively.
 */
bool FloorControl::getChildren(WinTreeNodePtr node)
{
    Window root_return, parent_return;
    Window* children;
    unsigned int no_children;
    unsigned int i;

    XQueryTree(x11->dpy, node->win, &root_return, &parent_return, &children, &no_children);

    if (!no_children)
    {
        node->children.clear();
        return false;
    }

    i = 0;
    while (i < no_children)
    {
        WinTreeNodePtr kidnode;
        kidnode = new WinTreeNode;
        memset(kidnode, 0, sizeof(WinTreeNode));
        kidnode->win = children[i];
        kidnode->parent = node;

        XGetWindowAttributes(x11->dpy, kidnode->win, &kidnode->attr);

        node->children.push_back(kidnode);
        i++;
    }
    XFree(children);
    return true;
}

/**
 * Initialises internal structures. This means querying the client window for
 * child windows and building up the hierarchy.
 */

void FloorControl::init()
{
    WinTreeNodePtr node = NULL;

    // list of windows that have not been queried for children yet.
    deque<WinTreeNodePtr> unqueried;

    root = new WinTreeNode;
    root->win = client->getClientWindow();
    root->parent = NULL;

    XGetWindowAttributes(x11->dpy, root->win, &root->attr);

    unqueried.push_back(root);

    do {
        node = unqueried[0];
        unqueried.pop_front();

        getChildren(node);

        if (!node->children.empty())
        {
            vector<WinTreeNodePtr>::iterator it = node->children.begin();
            while(it != node->children.end())
            {
                unqueried.push_back(*it);
                it++;
            }
        }
    } while(!unqueried.empty());
}


/**
 * For each window in the hierarchy starting with the given parent window do
 * the following:
 *  - create a new window in exactly the same place.
 *  - set the window to listen to button events.
 *  - call the method for all childs.
 *  This builds up a simple rebuilt GUI of the underlying client.
 */
void FloorControl::buildNodeGUI(WinTreePtr node, Window parent)
{
    if (node->attr.map_state != IsViewable)
        return;
    
    node->fcwin = XCreateSimpleWindow(x11->dpy, parent, node->attr.x, node->attr.y,
                                      node->attr.width - 2, node->attr.height - 2, 1,
                                      BlackPixel(x11->dpy, 0), WhitePixel(x11->dpy, 0));

    TRACE("Floor control creating window %x\n", (int)node->fcwin);

    XSelectInput(x11->dpy, node->fcwin, EnterWindowMask | LeaveWindowMask);
    manager->setButtonPressMask(node->fcwin);
    XMapWindow(x11->dpy, node->fcwin);
    XFlush(x11->dpy);

    vector<WinTreePtr>::iterator it = node->children.begin();

    while(it != node->children.end())
    {
        buildNodeGUI(*it, node->fcwin);
        it++;
    }
}



/**
 * Display floor control overlay windows.
 */
void FloorControl::display()
{
    buildNodeGUI(root, client->getContainer());
    XFlush(x11->dpy);
}


/**
 * Returns true if the floor control window has the given window.
 */
bool FloorControl::hasWindow(Window win)
{
    return nodeHasWindow(root, win);
}

/**
 * Returns true if the node or any of its subnodes contains the given window.
 */
bool FloorControl::nodeHasWindow(WinTreeNodePtr node, Window win)
{
    if (node->fcwin == win)
        return true;

    vector<WinTreeNodePtr>::iterator it = node->children.begin();

    while(it != node->children.end())
    {
        if (nodeHasWindow(*it, win))
            return true;
        it++;
    }
    return false;
}

/**
 * Handles enter/leave events. These events occur when a mouse moves over one
 * of the floor control windows.
 */
void FloorControl::handleEnterLeave(XCrossingEvent* ev)
{
    WinTreeNodePtr node = getNodeByWindow(ev->window);
    if (!node)
        return;

    XSetWindowAttributes attr;

    if (ev->type == EnterNotify)
        attr.background_pixel = BlackPixel(x11->dpy, 0);
    else
        attr.background_pixel = WhitePixel(x11->dpy, 0);


    XChangeWindowAttributes(x11->dpy, node->fcwin, CWBackPixel, &attr);
    XClearWindow(x11->dpy, node->fcwin);
    XFlush(x11->dpy);
}


WinTreeNodePtr FloorControl::getNodeByWindow(Window win)
{
    return findNodeForWindow(root, win);
}

WinTreeNodePtr FloorControl::findNodeForWindow(WinTreeNodePtr node, Window win)
{
    if (node->fcwin == win)
        return node;

    vector<WinTreeNodePtr>::iterator it = node->children.begin();
    while(it != node->children.end())
    {
        WinTreeNodePtr kidnode;
        kidnode = findNodeForWindow(*it, win);
        if (kidnode)
            return kidnode;
        it++;
    }

    return NULL;
}

void FloorControl::handleButtonPress(XDeviceButtonEvent* ev, PointerDevice* dev)
{
    WinTreeNodePtr node = getNodeByWindow(ev->window);
    int id = dev->getID();

    if (node)
    {
        XID *permdevs, 
             *denydevs;
        int nperm, ndeny, rule;

        XQueryWindowAccess(x11->dpy, client->getClientWindow(), &rule, 
                           &permdevs, &nperm, &denydevs, &ndeny);

        if (ev->state & Button1Mask)
        {
            int i;
            for (i = 0; i < nperm; i++)
            {
                if (permdevs[i] == (XID)id)
                    break;
            }

            if (i >= nperm)
            {
                permdevs = (XID*)realloc(permdevs, (ndeny + 1) * sizeof(XID));
                denydevs[ndeny++] = (char)id;
                XPermitDevices(x11->dpy, client->getClientWindow(), permdevs, nperm);
            }
        }
        else if (ev->state & Button3Mask)
        {
            for (int i = 0; i < nperm; i++)
            {
                if (permdevs[i] == (XID)id)
                {
                    if (i + 1 < nperm)
                        memmove(&permdevs[i], &permdevs[i+1], nperm - i - 1);
                    nperm--;
                    XPermitDevices(x11->dpy, client->getClientWindow(), permdevs, nperm);
                    break;
                }
            }
        }


        free(permdevs);
        free(denydevs);
    }
}