~ci-train-bot/compiz/compiz-ubuntu-xenial-2841

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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * Copyright © 2012 Sam Spilsbury
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Canonical Ltd. not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * Canonical Ltd. makes no representations about the suitability of this
 * software for any purpose. It is provided "as is" without express or
 * implied warranty.
 *
 * CANONICAL, LTD. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL CANONICAL, LTD. BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Authored by: Sam Spilsbury <smspillaz@gmail.com>
 */
#include <cstdio>
#include <cassert>
#include <boost/foreach.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/bind.hpp>
#include "asyncserverwindow.h"
#include "configurerequestbuffer-impl.h"

#ifndef foreach
#define foreach BOOST_FOREACH
#endif

namespace crb = compiz::window::configure_buffers;
namespace cw = compiz::window;

class crb::ConfigureRequestBuffer::Private
{
    public:

	typedef crb::Lockable::Weak LockObserver;

	Private (cw::AsyncServerWindow                          *asyncServerWindow,
		 cw::SyncServerWindow                           *syncServerWindow,
		 const crb::ConfigureRequestBuffer::LockFactory &lockFactory) :
	    clientChangeMask (0),
	    wrapperChangeMask (0),
	    frameChangeMask (0),
	    sendSyntheticConfigure (false),
	    lockCount (0),
	    asyncServerWindow (asyncServerWindow),
	    syncServerWindow (syncServerWindow),
	    lockFactory (lockFactory)
	{
	}

	void dispatchConfigure (bool force = false);

	XWindowChanges clientChanges;
	unsigned int   clientChangeMask;

	XWindowChanges wrapperChanges;
	unsigned int   wrapperChangeMask;

	XWindowChanges frameChanges;
	unsigned int   frameChangeMask;

	bool           sendSyntheticConfigure;

	unsigned int   lockCount;

	cw::AsyncServerWindow *asyncServerWindow;
	cw::SyncServerWindow  *syncServerWindow;

	crb::ConfigureRequestBuffer::LockFactory lockFactory;
	std::vector <LockObserver>               locks;
};

void
crb::ConfigureRequestBuffer::Private::dispatchConfigure (bool force)
{
    const unsigned int allEventMasks = 0x7f;
    bool immediate = frameChangeMask & (CWStackMode | CWSibling);

    /* This is a stop-gap solution for not having a plugin API to
     * query the window shape. Once we have that, we can safely
     * remove these and require the queue to be unlocked when that
     * happens. Its a separate unit of work for improving resize
     * performance anyways.
     */
    immediate |= (frameChangeMask & (CWWidth | CWHeight | CWBorderWidth));
    immediate |= (wrapperChangeMask & (CWWidth | CWHeight | CWBorderWidth | CWX | CWY));
    immediate |= (clientChangeMask & (CWWidth | CWHeight | CWBorderWidth | CWX | CWY));
    immediate |= force;

    bool clientDispatch = (clientChangeMask & allEventMasks);
    bool wrapperDispatch = (wrapperChangeMask & allEventMasks);
    bool frameDispatch  = (frameChangeMask & allEventMasks);

    bool dispatch = !lockCount && (clientDispatch ||
				   wrapperDispatch ||
				   frameDispatch ||
				   sendSyntheticConfigure);

    if (dispatch || immediate)
    {
	if (frameDispatch)
	{
	    asyncServerWindow->requestConfigureOnFrame (frameChanges,
							frameChangeMask);
	    frameChangeMask = 0;
	}

	if (wrapperDispatch)
	{
	    asyncServerWindow->requestConfigureOnWrapper (wrapperChanges,
							  wrapperChangeMask);
	    wrapperChangeMask = 0;
	}

	if (clientDispatch)
	{
	    asyncServerWindow->requestConfigureOnClient (clientChanges,
							 clientChangeMask);
	    clientChangeMask = 0;
	}

	if (sendSyntheticConfigure)
	{
	    asyncServerWindow->sendSyntheticConfigureNotify ();
	    sendSyntheticConfigure = false;
	}

	foreach (const LockObserver &lock, locks)
	{
	    crb::Lockable::Ptr strongLock (lock.lock ());

	    /* We might be in a lock's destructor so check
	     * if this can really be re-locked, if not, its
	     * no big deal as the lock is going away anyways
	     */
	    if (strongLock)
		strongLock->lock ();
	}
    }
}

void
crb::ConfigureRequestBuffer::freeze ()
{
    priv->lockCount++;

    assert (priv->lockCount <= priv->locks.size ());
}

void
crb::ConfigureRequestBuffer::release ()
{
    assert (priv->lockCount);

    priv->lockCount--;

    priv->dispatchConfigure ();
}

namespace
{
void applyChangeToXWC (const XWindowChanges &from,
		       XWindowChanges       &to,
		       unsigned int         mask)
{
    if (mask & CWX)
	to.x = from.x;

    if (mask & CWY)
	to.y = from.y;

    if (mask & CWWidth)
	to.width = from.width;

    if (mask & CWHeight)
	to.height = from.height;

    if (mask & CWBorderWidth)
	to.border_width = from.border_width;

    if (mask & CWSibling)
	to.sibling = from.sibling;

    if (mask & CWStackMode)
	to.stack_mode = from.stack_mode;
}
}

void
crb::ConfigureRequestBuffer::pushClientRequest (const XWindowChanges &xwc,
						unsigned int         mask)
{
    applyChangeToXWC (xwc, priv->clientChanges, mask);
    priv->clientChangeMask |= mask;

    priv->dispatchConfigure ();
}

void
crb::ConfigureRequestBuffer::pushWrapperRequest (const XWindowChanges &xwc,
						 unsigned int         mask)
{
    applyChangeToXWC (xwc, priv->wrapperChanges, mask);
    priv->wrapperChangeMask |= mask;

    priv->dispatchConfigure ();
}

void
crb::ConfigureRequestBuffer::pushFrameRequest (const XWindowChanges &xwc,
					       unsigned int         mask)
{
    applyChangeToXWC (xwc, priv->frameChanges, mask);
    priv->frameChangeMask |= mask;

    priv->dispatchConfigure ();
}

void
crb::ConfigureRequestBuffer::pushSyntheticConfigureNotify ()
{
    priv->sendSyntheticConfigure = true;

    priv->dispatchConfigure ();
}

crb::Releasable::Ptr
crb::ConfigureRequestBuffer::obtainLock ()
{
    crb::BufferLock::Ptr lock (priv->lockFactory (this));

    priv->locks.push_back (crb::Lockable::Weak (lock));
    lock->lock ();

    return lock;
}

namespace
{
bool isLock (const crb::Lockable::Weak &lockable,
	     crb::BufferLock           *lock)
{
    crb::Lockable::Ptr strongLockable (lockable.lock ());

    /* Asserting that the lock did not go away without telling
     * us first */
    assert (strongLockable.get ());

    if (strongLockable.get () == lock)
	return true;

    return false;
}
}

void
crb::ConfigureRequestBuffer::untrackLock (crb::BufferLock *lock)
{
    std::remove_if (priv->locks.begin (),
		    priv->locks.end (),
		    boost::bind (isLock, _1, lock));
}

bool crb::ConfigureRequestBuffer::queryAttributes (XWindowAttributes &attrib)
{
    priv->dispatchConfigure (true);
    return priv->syncServerWindow->queryAttributes (attrib);
}

bool crb::ConfigureRequestBuffer::queryFrameAttributes (XWindowAttributes &attrib)
{
    priv->dispatchConfigure (true);
    return priv->syncServerWindow->queryFrameAttributes (attrib);
}

/* This is more or less of a stop-gap for the fact that
 * when resizing window we re-query the window shape
 * and apply that to the frame. That's a separate unit of
 * work and should be dealt with separately. For now, force
 * a release of the queue whenever we do that so that
 * XShapeGetRectangles doesn't return an unexpected value
 */
XRectangle *
crb::ConfigureRequestBuffer::queryShapeRectangles (int kind,
						   int *count,
						   int *ordering)
{
    priv->dispatchConfigure (true);
    return priv->syncServerWindow->queryShapeRectangles (kind, count, ordering);
}

void crb::ConfigureRequestBuffer::forceRelease ()
{
    priv->dispatchConfigure (true);
}

crb::ConfigureRequestBuffer::ConfigureRequestBuffer (AsyncServerWindow                          *asyncServerWindow,
						     SyncServerWindow                           *syncServerWindow,
						     const crb::ConfigureRequestBuffer::LockFactory &factory) :
    priv (new crb::ConfigureRequestBuffer::Private (asyncServerWindow, syncServerWindow, factory))
{
}

compiz::window::configure_buffers::Buffer::Ptr
crb::ConfigureRequestBuffer::Create (AsyncServerWindow *asyncServerWindow,
				     SyncServerWindow  *syncServerWindow,
				     const LockFactory &factory)
{
    return crb::Buffer::Ptr (new crb::ConfigureRequestBuffer (asyncServerWindow,
							      syncServerWindow,
							      factory));
}

class crb::ConfigureBufferLock::Private
{
    public:

	Private (crb::CountedFreeze *freezable) :
	    freezable (freezable),
	    armed (false)
	{
	}

	crb::CountedFreeze *freezable;
	bool               armed;
};

crb::ConfigureBufferLock::ConfigureBufferLock (crb::CountedFreeze *freezable) :
    priv (new crb::ConfigureBufferLock::Private (freezable))
{
}

crb::ConfigureBufferLock::~ConfigureBufferLock ()
{
    release ();
}

void
crb::ConfigureBufferLock::lock ()
{
    if (!priv->armed)
	priv->freezable->freeze ();

    priv->armed = true;
}

void
crb::ConfigureBufferLock::release ()
{
    if (priv->armed)
	priv->freezable->release ();

    priv->armed = false;
}