Newer
Older
Sam Spilsbury
committed
/*
* Copyright © 2011 Canonical Ltd.
*
* 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 <sam.spilsbury@canonical.com>
*/
#include "privatestackdebugger.h"
#include "privatescreen.h"
Sam Spilsbury
committed
#include "privatewindow.h"
Sam Spilsbury
committed
namespace
{
StackDebugger * gStackDebugger = NULL;
}
StackDebugger *
StackDebugger::Default ()
{
return gStackDebugger;
}
void
StackDebugger::SetDefault (StackDebugger *dbg)
{
if (gStackDebugger)
delete gStackDebugger;
gStackDebugger = dbg;
}
StackDebugger::StackDebugger (Display *dpy, Window root, FetchXEventInterface *fetchXEvent) :
Sam Spilsbury
committed
mServerNChildren (0),
mServerChildren (NULL),
mWindowsChanged (false),
mServerWindowsChanged (false),
Sam Spilsbury
committed
mRoot (root),
mFetchXEvent (fetchXEvent),
mTimeoutRequired (false)
Sam Spilsbury
committed
{
}
StackDebugger::~StackDebugger ()
{
if (mServerChildren)
{
XFree (mServerChildren);
mServerChildren = NULL;
mServerNChildren = 0;
}
}
bool
StackDebugger::timedOut ()
{
return mTimeoutRequired;
}
void
StackDebugger::removeServerWindow (Window id)
{
/* Find the toplevel window in the list and remove it */
for (CompWindowList::iterator it = mLastServerWindows.begin ();
it != mLastServerWindows.end ();
{
if ((*it)->id () == id)
{
mLastServerWindows.erase (it);
break;
}
}
}
Sam Spilsbury
committed
void
StackDebugger::overrideRedirectRestack (Window toplevel, Window sibling)
{
CompWindow *tl = screen->findWindow (toplevel);
removeServerWindow (toplevel);
/* Find the sibling of this window and insert above it or at
* the bottom if the sibling is 0 */
if (sibling)
{
for (CompWindowList::iterator it = mLastServerWindows.begin ();
it != mLastServerWindows.end ();
{
if (sibling == (*it)->id () ||
sibling == (*it)->frame ())
{
mLastServerWindows.insert (++it, tl);
break;
}
}
}
else
mLastServerWindows.push_front (tl);
}
bool
StackDebugger::getNextEvent (XEvent &ev)
{
if (mEvents.empty ())
return false;
ev = mEvents.front ();
mEvents.pop_front ();
return true;
}
void
StackDebugger::loadStack (CompWindowList &serverWindows, bool wait)
Sam Spilsbury
committed
{
Window rootRet, parentRet;
if (mServerChildren)
XFree (mServerChildren);
XSync (mDpy, FALSE);
XGrabServer (mDpy);
Sam Spilsbury
committed
XQueryTree (mDpy, mRoot, &rootRet, &parentRet,
&mServerChildren, &mServerNChildren);
unsigned int n = XEventsQueued (mDpy, QueuedAfterFlush);
mEvents.clear ();
mEvents.resize (n);
while (it != mEvents.end ())
{
mFetchXEvent->getNextXEvent ((*it));
XSync (mDpy, FALSE);
/* It is possible that X might not be keeping up with us, so
* we should give it about 300 ms in case the stacks are out of sync
* in order to deliver any more events that might be pending */
mTimeoutRequired = false;
mLastServerWindows = serverWindows;
if (mServerNChildren != serverWindows.size () && wait)
{
struct pollfd pfd;
pfd.events = POLLIN;
pfd.revents = 0;
pfd.fd = ConnectionNumber (mDpy);
poll (&pfd, 1, 300);
while (mFetchXEvent->getNextXEvent (e))
mEvents.push_back (e);
Sam Spilsbury
committed
mDestroyedFrames.clear ();
XUngrabServer (mDpy);
XSync (mDpy, FALSE);
Sam Spilsbury
committed
}
void
StackDebugger::addDestroyedFrame (Window f)
{
mDestroyedFrames.push_back (f);
}
Sam Spilsbury
committed
void
StackDebugger::removeDestroyedFrame (Window f)
{
mDestroyedFrames.remove (f);
}
Sam Spilsbury
committed
bool
StackDebugger::cmpStack (CompWindowList &windows,
CompWindowList &serverWindows,
bool verbose)
{
std::vector <Window> serverSideWindows;
CompWindowList::reverse_iterator lrrit = windows.rbegin ();
CompWindowList::reverse_iterator lsrit = mLastServerWindows.rbegin ();
Sam Spilsbury
committed
unsigned int i = 0;
bool err = false;
for (unsigned int n = 0; n < mServerNChildren; n++)
{
if (std::find (mDestroyedFrames.begin (),
mDestroyedFrames.end (), mServerChildren[n])
== mDestroyedFrames.end ())
serverSideWindows.push_back (mServerChildren[n]);
}
if (verbose)
compLogMessage ("core", CompLogLevelDebug, "sent | recv | server |");
Sam Spilsbury
committed
for (;(lrrit != windows.rend () ||
Sam Spilsbury
committed
i != serverSideWindows.size ());)
{
Window lrXid = 0;
Window lsXid = 0;
Window sXid = 0;
if (lrrit != windows.rend ())
Sam Spilsbury
committed
lrXid = (*lrrit)->priv->frame ? (*lrrit)->priv->frame : (*lrrit)->id ();
Sam Spilsbury
committed
Sam Spilsbury
committed
lsXid = (*lsrit)->priv->frame ? (*lsrit)->priv->frame : (*lsrit)->id ();
Sam Spilsbury
committed
if (i != serverSideWindows.size ())
sXid = serverSideWindows[serverSideWindows.size () - (i + 1)];
if (verbose)
compLogMessage ("core", CompLogLevelDebug, "id 0x%x id 0x%x id 0x%x %s",
Sam Spilsbury
committed
(unsigned int) lsXid, (unsigned int) lrXid,
(unsigned int) sXid, (lrXid != sXid) ? " /!\\ " : "");
Sam Spilsbury
committed
Sam Spilsbury
committed
err = true;
if (lrrit != windows.rend ())
Sam Spilsbury
committed
Sam Spilsbury
committed
if (i != serverSideWindows.size ())
i++;
}
return err;
}
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
/* Checks the sanity of the list of windows last sent to the server.
*
* There are a few stacking "layers" here. From top to bottom:
* - 1) Docks stacked above toplevel windows which are stacked
* above fullscreen windows
* - 2) "Keep above" toplevel windows above fullscreen windows
* where a toplevel is in focus
* - 3) Toplevel windows in focus above fullscreen windows
* - 4) Fullscreen windows
* - 5) Dock windows
* - 6) Keep above windows
* - 7) Toplevel windows
* - 8) Docks which are marked "Keep Below"
* - 9) "Keep Below" windows
* - 10) Desktop windows
*
* There are also a few rules which apply here:
* - 1) Dock windows should always be above normal windows
* except if marked keep below on any layer.
* - 2) Dock windows should ONLY be on one layer at a time,
* eg if they are on layer 1 then there cannot
* also be dock windows on layer 5 (except in the
* case of below dock windows on layer 8)
* - 3) Fullscreen windows must always be above docks when in
* focus, no matter if there is another window with "Keep Above"
* - 4) Focused windows take priority over fullscreen windows and
* docks must always be above them (see rule 1)
*
* As we pass through each layer, this function flags each one from
* lowest being the most bits set to highest being the least bits
* set. If a window violates this it raises a warning */
#define DOCKS_ABOVE_TOPLEVELS_ABOVE_FULLSCREEN 0xffffffff >> 1
#define KEEP_ABOVE_TOPLEVELS_ABOVE_FULLSCREEN 0xffffffff >> 2
#define TOPLEVELS_ABOVE_FULLSCREEN 0xffffffff >> 3
#define FULLSCREEN 0xffffffff >> 4
#define DOCKS 0xffffffff >> 5
#define KEEP_ABOVE 0xffffffff >> 6
#define TOPLEVELS 0xffffffff >> 7
#define DOCKS_BELOW 0xffffffff >> 8
#define KEEP_BELOW 0xffffffff >> 9
#define DESKTOP 0xffffffff >> 10
namespace
{
bool setCurrentLayer (Window requestingWindow, int layer, int ¤t)
{
bool ret = false;
/* Only allow steps down */
if ((current & layer) != layer)
{
ret = true;
compLogMessage ("stackdebugger", CompLogLevelWarn, "0x%x requested invalid layer 0x%x",
requestingWindow, layer, current);
}
current = layer;
return ret;
}
}
bool
StackDebugger::checkSanity (CompWindowList &serverWindows, bool verbose)
{
int current = 0xffffffff;
int oldCurrent = current;
bool err = false;
if (verbose)
compLogMessage ("stackdebugger", CompLogLevelDebug, "processing new stack --------");
/* go backwards down the stack */
for (CompWindowList::reverse_iterator rit = serverWindows.rbegin ();
{
CompWindow *w = (*rit);
/* Override redirect windows set all kinds
* of crazy stuff and are required to stack
* themselves so skip those */
if (w->overrideRedirect ())
continue;
/* ignore non-override redirect unmanaged windows */
if (!w->managed ())
continue;
Sam Spilsbury
committed
/* ignore any windows that just got created */
if (!w->mapNum ())
continue;
/* determine the current layer */
if (w->type () == CompWindowTypeDockMask)
{
if ((current & DOCKS_ABOVE_TOPLEVELS_ABOVE_FULLSCREEN) ==
DOCKS_ABOVE_TOPLEVELS_ABOVE_FULLSCREEN)
{
bool fullscreenWindow = false;
/* search down the stack to check if there is a fullscreen
* window, otherwise we are not on the fullscreen layer */
Sam Spilsbury
committed
for (CompWindow *rw = w->prev; rw; rw = rw->prev)
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
{
if (rw->type () & CompWindowTypeFullscreenMask)
{
fullscreenWindow = true;
break;
}
}
/* if there is no fullscreen window, change the layer */
if (!fullscreenWindow)
err |= setCurrentLayer (w->id (), DOCKS, current);
else
err |= setCurrentLayer (w->id (), DOCKS_ABOVE_TOPLEVELS_ABOVE_FULLSCREEN, current);
}
else if (w->state () & CompWindowStateBelowMask)
err |= setCurrentLayer (w->id (), DOCKS_BELOW, current);
else
err |= setCurrentLayer (w->id (), DOCKS, current);
}
else if (w->type () == CompWindowTypeFullscreenMask)
{
err |= setCurrentLayer (w->id (), FULLSCREEN, current);
}
else if (w->type () == CompWindowTypeDesktopMask)
{
err |= setCurrentLayer (w->id (), DESKTOP, current);
}
/* everything else that is not a fullscreen window or a desktop */
else
{
if (w->state () & CompWindowStateAboveMask)
{
if ((current & KEEP_ABOVE_TOPLEVELS_ABOVE_FULLSCREEN) ==
KEEP_ABOVE_TOPLEVELS_ABOVE_FULLSCREEN)
{
bool fullscreenWindow = false;
/* search down the stack to check if there is a fullscreen
* window, otherwise we are not on the fullscreen layer */
Sam Spilsbury
committed
for (CompWindow *rw = w->prev; rw; rw = rw->prev)
Sam Spilsbury
committed
if (rw->type () & CompWindowTypeFullscreenMask)
{
fullscreenWindow = true;
break;
}
}
if (!fullscreenWindow)
err |= setCurrentLayer (w->id (), KEEP_ABOVE, current);
else
err |= setCurrentLayer (w->id (), KEEP_ABOVE_TOPLEVELS_ABOVE_FULLSCREEN, current);
}
else
err |= setCurrentLayer (w->id (), KEEP_ABOVE, current);
}
else if (w->state () & CompWindowStateBelowMask)
err |= setCurrentLayer (w->id (), KEEP_BELOW, current);
else
{
if ((current & TOPLEVELS_ABOVE_FULLSCREEN) ==
TOPLEVELS_ABOVE_FULLSCREEN)
{
bool fullscreenWindow = false;
/* search down the stack to check if there is a fullscreen
* window, otherwise we are not on the fullscreen layer */
Sam Spilsbury
committed
for (CompWindow *rw = w->prev; rw; rw = rw->prev)
Sam Spilsbury
committed
if (rw->type () & CompWindowTypeFullscreenMask)
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
{
fullscreenWindow = true;
break;
}
}
if (!fullscreenWindow)
err |= setCurrentLayer (w->id (), TOPLEVELS, current);
else
err |= setCurrentLayer (w->id (), TOPLEVELS_ABOVE_FULLSCREEN, current);
}
else
err |= setCurrentLayer (w->id (), TOPLEVELS, current);
}
}
if ((current & DOCKS_ABOVE_TOPLEVELS_ABOVE_FULLSCREEN) ==
DOCKS_ABOVE_TOPLEVELS_ABOVE_FULLSCREEN)
{
if (verbose && current != oldCurrent)
compLogMessage ("stackdebugger", CompLogLevelDebug, "on layer DOCKS_ABOVE_TOPLEVELS_ABOVE_FULLSCREEN");
}
else if ((current & KEEP_ABOVE_TOPLEVELS_ABOVE_FULLSCREEN) ==
KEEP_ABOVE_TOPLEVELS_ABOVE_FULLSCREEN)
{
if (verbose && current != oldCurrent)
compLogMessage ("stackdebugger", CompLogLevelDebug, "on layer KEEP_ABOVE_TOPLEVELS_ABOVE_FULLSCREEN");
}
else if ((current & TOPLEVELS_ABOVE_FULLSCREEN) == TOPLEVELS_ABOVE_FULLSCREEN)
{
if (verbose && current != oldCurrent)
compLogMessage ("stackdebugger", CompLogLevelDebug, "on layer TOPLEVELS_ABOVE_FULLSCREEN");
}
else if ((current & FULLSCREEN) == FULLSCREEN)
{
if (verbose && current != oldCurrent)
compLogMessage ("stackdebugger", CompLogLevelDebug, "on layer FULLSCREEN");
}
else if ((current & DOCKS) == DOCKS)
{
if (verbose && current != oldCurrent)
compLogMessage ("stackdebugger", CompLogLevelDebug, "on layer DOCKS");
}
else if ((current & KEEP_ABOVE) == KEEP_ABOVE)
{
if (verbose && current != oldCurrent)
compLogMessage ("stackdebugger", CompLogLevelDebug, "on layer KEEP_ABOVE");
}
else if ((current & TOPLEVELS) == TOPLEVELS)
{
if (verbose && current != oldCurrent)
compLogMessage ("stackdebugger", CompLogLevelDebug, "on layer TOPLEVELS");
}
else if ((current & DOCKS_BELOW) == DOCKS_BELOW)
{
if (verbose && current != oldCurrent)
compLogMessage ("stackdebugger", CompLogLevelDebug, "on layer DOCKS_BELOW");
}
else if ((current & KEEP_BELOW) == KEEP_BELOW)
{
if (verbose && current != oldCurrent)
compLogMessage ("stackdebugger", CompLogLevelDebug, "on layer KEEP_BELOW");
}
else if ((current & DESKTOP) == DESKTOP)
{
if (verbose && current != oldCurrent)
compLogMessage ("stackdebugger", CompLogLevelDebug, "on layer DESKTOP");
}
oldCurrent = current;
}
return err;
}