#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif
#include <X11/X.h>
#include "windowstr.h"
#include "scrnintstr.h"
#include "exglobals.h"
#include "enterleave.h"
static WindowPtr PointerWindows[MAXDEVICES];
static WindowPtr FocusWindows[MAXDEVICES];
static BOOL
HasPointer(WindowPtr win)
{
int i;
for (i = 0; i < MAXDEVICES; i++)
if (PointerWindows[i] == win)
return TRUE;
return FALSE;
}
static BOOL
HasFocus(WindowPtr win)
{
int i;
for (i = 0; i < MAXDEVICES; i++)
if (FocusWindows[i] == win)
return TRUE;
return FALSE;
}
static WindowPtr
PointerWin(DeviceIntPtr dev)
{
return PointerWindows[dev->id];
}
static WindowPtr
FirstPointerChild(WindowPtr win)
{
int i;
for (i = 0; i < MAXDEVICES; i++)
{
if (PointerWindows[i] && IsParent(win, PointerWindows[i]))
return PointerWindows[i];
}
return NULL;
}
static WindowPtr
FirstFocusChild(WindowPtr win)
{
int i;
for (i = 0; i < MAXDEVICES; i++)
{
if (FocusWindows[i] && FocusWindows[i] != PointerRootWin &&
IsParent(win, FocusWindows[i]))
return FocusWindows[i];
}
return NULL;
}
void
EnterWindow(DeviceIntPtr dev, WindowPtr win, int mode)
{
PointerWindows[dev->id] = win;
}
static void
LeaveWindow(DeviceIntPtr dev, WindowPtr win, int mode)
{
PointerWindows[dev->id] = NULL;
}
void
SetFocusIn(DeviceIntPtr dev, WindowPtr win)
{
FocusWindows[dev->id] = win;
}
void
SetFocusOut(DeviceIntPtr dev, WindowPtr win)
{
FocusWindows[dev->id] = NULL;
}
WindowPtr
CommonAncestor(
WindowPtr a,
WindowPtr b)
{
for (b = b->parent; b; b = b->parent)
if (IsParent(b, a)) return b;
return NullWindow;
}
#if 0
static void
DeviceEnterNotifies(DeviceIntPtr dev,
WindowPtr ancestor,
WindowPtr child,
int mode,
int detail)
{
WindowPtr parent = child->parent;
if (ancestor == parent)
return;
DeviceEnterNotifies(dev, ancestor, parent, mode, detail);
DeviceEnterLeaveEvent(dev, DeviceEnterNotify, mode, detail, parent,
child->drawable.id);
}
#endif
static void
CoreEnterNotifies(DeviceIntPtr dev,
WindowPtr ancestor,
WindowPtr child,
int mode,
int detail)
{
WindowPtr parent = child->parent;
if (ancestor == parent)
return;
CoreEnterNotifies(dev, ancestor, parent, mode, detail);
if (!HasPointer(parent) && !FirstPointerChild(parent))
CoreEnterLeaveEvent(dev, EnterNotify, mode, detail, parent,
child->drawable.id);
}
static void
CoreLeaveNotifies(DeviceIntPtr dev,
WindowPtr child,
WindowPtr ancestor,
int mode,
int detail)
{
WindowPtr win;
if (ancestor == child)
return;
for (win = child->parent; win != ancestor; win = win->parent)
{
if (HasPointer(win) || FirstPointerChild(win))
return;
CoreEnterLeaveEvent(dev, LeaveNotify, mode, detail, win, child->drawable.id);
child = win;
}
}
#if 0
static void
DeviceLeaveNotifies(DeviceIntPtr dev,
WindowPtr child,
WindowPtr ancestor,
int mode,
int detail)
{
WindowPtr win;
if (ancestor == child)
return;
for (win = child->parent; win != ancestor; win = win->parent)
{
DeviceEnterLeaveEvent(dev, DeviceLeaveNotify, mode, detail, win,
child->drawable.id);
child = win;
}
}
#endif
static void
CoreEnterLeaveNonLinear(DeviceIntPtr dev,
WindowPtr A,
WindowPtr B,
int mode)
{
WindowPtr X = CommonAncestor(A, B);
if (!HasPointer(A))
{
WindowPtr child = FirstPointerChild(A);
if (child)
CoreEnterLeaveEvent(dev, LeaveNotify, mode, NotifyInferior, A, None);
else
CoreEnterLeaveEvent(dev, LeaveNotify, mode, NotifyNonlinear, A, None);
}
CoreLeaveNotifies(dev, A, X, mode, NotifyNonlinearVirtual);
CoreEnterNotifies(dev, X, B, mode, NotifyNonlinearVirtual);
if (!HasPointer(B))
{
WindowPtr child = FirstPointerChild(B);
if (child)
CoreEnterLeaveEvent(dev, EnterNotify, mode, NotifyInferior, B, None);
else
CoreEnterLeaveEvent(dev, EnterNotify, mode, NotifyNonlinear, B, None);
}
}
static void
CoreEnterLeaveToAncestor(DeviceIntPtr dev,
WindowPtr A,
WindowPtr B,
int mode)
{
if (!HasPointer(A))
{
WindowPtr child = FirstPointerChild(A);
if (child)
CoreEnterLeaveEvent(dev, LeaveNotify, mode, NotifyInferior, A, None);
else
CoreEnterLeaveEvent(dev, LeaveNotify, mode, NotifyAncestor, A, None);
}
CoreLeaveNotifies(dev, A, B, mode, NotifyVirtual);
if (!HasPointer(B))
CoreEnterLeaveEvent(dev, EnterNotify, mode, NotifyInferior, B, None);
}
static void
CoreEnterLeaveToDescendant(DeviceIntPtr dev,
WindowPtr A,
WindowPtr B,
int mode)
{
if (!HasPointer(A))
CoreEnterLeaveEvent(dev, LeaveNotify, mode, NotifyInferior, A, None);
CoreEnterNotifies(dev, A, B, mode, NotifyVirtual);
if (!HasPointer(B))
{
WindowPtr child = FirstPointerChild(B);
if (child)
CoreEnterLeaveEvent(dev, EnterNotify, mode, NotifyInferior, B, None);
else
CoreEnterLeaveEvent(dev, EnterNotify, mode, NotifyAncestor, B, None);
}
}
static void
CoreEnterLeaveEvents(DeviceIntPtr dev,
WindowPtr from,
WindowPtr to,
int mode)
{
if (!dev->isMaster)
return;
LeaveWindow(dev, from, mode);
if (IsParent(from, to))
CoreEnterLeaveToDescendant(dev, from, to, mode);
else if (IsParent(to, from))
CoreEnterLeaveToAncestor(dev, from, to, mode);
else
CoreEnterLeaveNonLinear(dev, from, to, mode);
EnterWindow(dev, to, mode);
}
#if 0
static void
DeviceEnterLeaveEvents(DeviceIntPtr dev,
WindowPtr from,
WindowPtr to,
int mode)
{
if (IsParent(from, to))
{
DeviceEnterLeaveEvent(dev, DeviceLeaveNotify, mode, NotifyInferior, from, None);
DeviceEnterNotifies(dev, from, to, mode, NotifyVirtual);
DeviceEnterLeaveEvent(dev, DeviceEnterNotify, mode, NotifyAncestor, to, None);
}
else if (IsParent(to, from))
{
DeviceEnterLeaveEvent(dev, DeviceLeaveNotify, mode, NotifyAncestor, from, None);
DeviceLeaveNotifies(dev, from, to, mode, NotifyVirtual);
DeviceEnterLeaveEvent(dev, DeviceEnterNotify, mode, NotifyInferior, to, None);
}
else
{
WindowPtr common = CommonAncestor(to, from);
DeviceEnterLeaveEvent(dev, DeviceLeaveNotify, mode, NotifyNonlinear, from, None);
DeviceLeaveNotifies(dev, from, common, mode, NotifyNonlinearVirtual);
DeviceEnterNotifies(dev, common, to, mode, NotifyNonlinearVirtual);
DeviceEnterLeaveEvent(dev, DeviceEnterNotify, mode, NotifyNonlinear, to, None);
}
}
#endif
void
DoEnterLeaveEvents(DeviceIntPtr pDev,
WindowPtr fromWin,
WindowPtr toWin,
int mode)
{
if (!IsPointerDevice(pDev))
return;
if (fromWin == toWin)
return;
CoreEnterLeaveEvents(pDev, fromWin, toWin, mode);
#if 0
DeviceEnterLeaveEvents(pDev, fromWin, toWin, mode);
#endif
}
static void
DeviceFocusOutEvents(DeviceIntPtr dev,
WindowPtr child,
WindowPtr ancestor,
int mode,
int detail)
{
WindowPtr win;
if (ancestor == child)
return;
for (win = child->parent; win != ancestor; win = win->parent)
DeviceFocusEvent(dev, DeviceFocusOut, mode, detail, win);
}
static void
DeviceFocusInEvents(DeviceIntPtr dev,
WindowPtr ancestor,
WindowPtr child,
int mode,
int detail)
{
WindowPtr parent = child->parent;
if (ancestor == parent || !parent)
return;
DeviceFocusInEvents(dev, ancestor, parent, mode, detail);
DeviceFocusEvent(dev, DeviceFocusIn, mode, detail, parent);
}
static void
CoreFocusInEvents(DeviceIntPtr dev,
WindowPtr ancestor,
WindowPtr child,
int mode,
int detail)
{
WindowPtr parent = child->parent;
if (ancestor == parent)
return;
CoreFocusInEvents(dev, ancestor, parent, mode, detail);
if (!HasFocus(parent) && !FirstFocusChild(parent))
CoreFocusEvent(dev, FocusIn, mode, detail, parent);
}
static void
CoreFocusOutEvents(DeviceIntPtr dev,
WindowPtr child,
WindowPtr ancestor,
int mode,
int detail)
{
WindowPtr win;
if (ancestor == child)
return;
for (win = child->parent; win != ancestor; win = win->parent)
{
if (HasFocus(win) || FirstFocusChild(win))
return;
CoreFocusEvent(dev, FocusOut, mode, detail, win);
}
}
static void
CoreFocusOutNotifyPointerEvents(DeviceIntPtr dev,
WindowPtr pwin_parent,
WindowPtr exclude,
int mode,
int inclusive)
{
WindowPtr P, stopAt;
P = PointerWin(GetPairedDevice(dev));
if (!P)
return;
if (!IsParent(pwin_parent, P))
if (!(pwin_parent == P && inclusive))
return;
if (exclude != None && exclude != PointerRootWin &&
(IsParent(exclude, P) || IsParent(P, exclude)))
return;
stopAt = (inclusive) ? pwin_parent->parent : pwin_parent;
for (; P && P != stopAt; P = P->parent)
CoreFocusEvent(dev, FocusOut, mode, NotifyPointer, P);
}
static void
CoreFocusInRecurse(DeviceIntPtr dev,
WindowPtr win,
WindowPtr stopAt,
int mode,
int inclusive)
{
if ((!inclusive && win == stopAt) || !win)
return;
CoreFocusInRecurse(dev, win->parent, stopAt, mode, inclusive);
CoreFocusEvent(dev, FocusIn, mode, NotifyPointer, win);
}
static void
CoreFocusInNotifyPointerEvents(DeviceIntPtr dev,
WindowPtr pwin_parent,
WindowPtr exclude,
int mode,
int inclusive)
{
WindowPtr P;
P = PointerWin(GetPairedDevice(dev));
if (!P || P == exclude || (pwin_parent != P && !IsParent(pwin_parent, P)))
return;
if (exclude != None && (IsParent(exclude, P) || IsParent(P, exclude)))
return;
CoreFocusInRecurse(dev, P, pwin_parent, mode, inclusive);
}
static void
CoreFocusNonLinear(DeviceIntPtr dev,
WindowPtr A,
WindowPtr B,
int mode)
{
WindowPtr X = CommonAncestor(A, B);
if (!HasFocus(A))
{
WindowPtr child = FirstFocusChild(A);
if (child)
{
CoreFocusOutNotifyPointerEvents(dev, A, child, mode, FALSE);
CoreFocusEvent(dev, FocusOut, mode, NotifyInferior, A);
} else
{
CoreFocusOutNotifyPointerEvents(dev, A, None, mode, FALSE);
CoreFocusEvent(dev, FocusOut, mode, NotifyNonlinear, A);
}
}
CoreFocusOutEvents(dev, A, X, mode, NotifyNonlinearVirtual);
CoreFocusInEvents(dev, X, B, mode, NotifyNonlinearVirtual);
if (!HasFocus(B))
{
WindowPtr child = FirstFocusChild(B);
if (child)
{
CoreFocusEvent(dev, FocusIn, mode, NotifyInferior, B);
CoreFocusInNotifyPointerEvents(dev, B, child, mode, FALSE);
} else {
CoreFocusEvent(dev, FocusIn, mode, NotifyNonlinear, B);
CoreFocusInNotifyPointerEvents(dev, B, None, mode, FALSE);
}
}
}
static void
CoreFocusToAncestor(DeviceIntPtr dev,
WindowPtr A,
WindowPtr B,
int mode)
{
if (!HasFocus(A))
{
WindowPtr child = FirstFocusChild(A);
if (child)
{
CoreFocusOutNotifyPointerEvents(dev, A, child, mode, FALSE);
CoreFocusEvent(dev, FocusOut, mode, NotifyInferior, A);
} else
CoreFocusEvent(dev, FocusOut, mode, NotifyAncestor, A);
}
CoreFocusOutEvents(dev, A, B, mode, NotifyVirtual);
if (!HasFocus(B))
{
CoreFocusEvent(dev, FocusIn, mode, NotifyInferior, B);
CoreFocusInNotifyPointerEvents(dev, B, A, mode, FALSE);
}
}
static void
CoreFocusToDescendant(DeviceIntPtr dev,
WindowPtr A,
WindowPtr B,
int mode)
{
if (!HasFocus(A))
{
CoreFocusOutNotifyPointerEvents(dev, A, B, mode, FALSE);
CoreFocusEvent(dev, FocusOut, mode, NotifyInferior, A);
}
CoreFocusInEvents(dev, A, B, mode, NotifyVirtual);
if (!HasFocus(B))
{
WindowPtr child = FirstFocusChild(B);
if (child)
{
CoreFocusEvent(dev, FocusIn, mode, NotifyInferior, B);
CoreFocusInNotifyPointerEvents(dev, B, child, mode, FALSE);
} else
CoreFocusEvent(dev, FocusIn, mode, NotifyAncestor, B);
}
}
static BOOL
HasOtherPointer(WindowPtr win, DeviceIntPtr exclude)
{
int i;
for (i = 0; i < MAXDEVICES; i++)
if (i != exclude->id && PointerWindows[i] == win)
return TRUE;
return FALSE;
}
static void
CoreFocusPointerRootNoneSwitch(DeviceIntPtr dev,
WindowPtr A,
WindowPtr B,
int mode)
{
WindowPtr root;
int i;
int nscreens = screenInfo.numScreens;
#ifdef PANORAMIX
if (!noPanoramiXExtension)
nscreens = 1;
#endif
for (i = 0; i < nscreens; i++)
{
root = WindowTable[i];
if (!HasOtherPointer(root, GetPairedDevice(dev)) && !FirstFocusChild(root))
{
if (dev->focus && dev->focus->win == PointerRootWin &&
B != PointerRootWin)
{
WindowPtr ptrwin = PointerWin(GetPairedDevice(dev));
if (ptrwin && IsParent(root, ptrwin))
CoreFocusOutNotifyPointerEvents(dev, root, None, mode, TRUE);
}
CoreFocusEvent(dev, FocusOut, mode, ((int)A) ? NotifyPointerRoot : NotifyDetailNone, root);
CoreFocusEvent(dev, FocusIn, mode, ((int)B) ? NotifyPointerRoot : NotifyDetailNone, root);
if (B == PointerRootWin)
CoreFocusInNotifyPointerEvents(dev, root, None, mode, TRUE);
}
}
}
static void
CoreFocusToPointerRootOrNone(DeviceIntPtr dev,
WindowPtr A,
WindowPtr B,
int mode)
{
WindowPtr root;
int i;
int nscreens = screenInfo.numScreens;
#ifdef PANORAMIX
if (!noPanoramiXExtension)
nscreens = 1;
#endif
if (!HasFocus(A))
{
WindowPtr child = FirstFocusChild(A);
if (child)
{
CoreFocusOutNotifyPointerEvents(dev, A, B, mode, FALSE);
CoreFocusEvent(dev, FocusOut, mode, NotifyInferior, A);
} else {
CoreFocusOutNotifyPointerEvents(dev, A, None, mode, FALSE);
CoreFocusEvent(dev, FocusOut, mode, NotifyNonlinear, A);
}
}
CoreFocusOutEvents(dev, A, NullWindow, mode, NotifyNonlinearVirtual);
for (i = 0; i < nscreens; i++)
{
root = WindowTable[i];
if (!HasFocus(root) && !FirstFocusChild(root))
{
CoreFocusEvent(dev, FocusIn, mode, ((int)B) ? NotifyPointerRoot : NotifyDetailNone, root);
if (B == PointerRootWin)
CoreFocusInNotifyPointerEvents(dev, root, None, mode, TRUE);
}
}
}
static void
CoreFocusFromPointerRootOrNone(DeviceIntPtr dev,
WindowPtr A,
WindowPtr B,
int mode)
{
WindowPtr root;
int i;
int nscreens = screenInfo.numScreens;
#ifdef PANORAMIX
if (!noPanoramiXExtension)
nscreens = 1;
#endif
for (i = 0; i < nscreens; i++)
{
root = WindowTable[i];
if (!HasFocus(root) && !FirstFocusChild(root))
{
if (dev->focus && dev->focus->win == PointerRootWin &&
B != PointerRootWin)
{
WindowPtr ptrwin = PointerWin(GetPairedDevice(dev));
if (ptrwin)
CoreFocusOutNotifyPointerEvents(dev, root, None, mode, TRUE);
}
CoreFocusEvent(dev, FocusOut, mode, ((int)A) ? NotifyPointerRoot : NotifyDetailNone, root);
}
}
root = B;
while(root->parent)
root = root->parent;
if (B != root)
{
CoreFocusEvent(dev, FocusIn, mode, NotifyNonlinearVirtual, root);
CoreFocusInEvents(dev, root, B, mode, NotifyNonlinearVirtual);
}
if (!HasFocus(B))
{
WindowPtr child = FirstFocusChild(B);
if (child)
{
CoreFocusEvent(dev, FocusIn, mode, NotifyInferior, B);
CoreFocusInNotifyPointerEvents(dev, B, child, mode, FALSE);
} else {
CoreFocusEvent(dev, FocusIn, mode, NotifyNonlinear, B);
CoreFocusInNotifyPointerEvents(dev, B, None, mode, FALSE);
}
}
}
static void
CoreFocusEvents(DeviceIntPtr dev,
WindowPtr from,
WindowPtr to,
int mode)
{
if (!dev->isMaster)
return;
SetFocusOut(dev, from);
if (((to == NullWindow) || (to == PointerRootWin)) &&
((from == NullWindow) || (from == PointerRootWin)))
CoreFocusPointerRootNoneSwitch(dev, from, to, mode);
else if ((to == NullWindow) || (to == PointerRootWin))
CoreFocusToPointerRootOrNone(dev, from, to, mode);
else if ((from == NullWindow) || (from == PointerRootWin))
CoreFocusFromPointerRootOrNone(dev, from, to, mode);
else if (IsParent(from, to))
CoreFocusToDescendant(dev, from, to, mode);
else if (IsParent(to, from))
CoreFocusToAncestor(dev, from, to, mode);
else
CoreFocusNonLinear(dev, from, to, mode);
SetFocusIn(dev, to);
}
#define RootWindow(dev) dev->spriteInfo->sprite->spriteTrace[0]
static void
DeviceFocusEvents(DeviceIntPtr dev,
WindowPtr from,
WindowPtr to,
int mode)
{
int out, in;
int i;
int nscreens = screenInfo.numScreens;
SpritePtr sprite = dev->spriteInfo->sprite;
if (from == to)
return;
out = (from == NoneWin) ? NotifyDetailNone : NotifyPointerRoot;
in = (to == NoneWin) ? NotifyDetailNone : NotifyPointerRoot;
#ifdef PANORAMIX
if (!noPanoramiXExtension)
nscreens = 1;
#endif
if ((to == NullWindow) || (to == PointerRootWin))
{
if ((from == NullWindow) || (from == PointerRootWin))
{
if (from == PointerRootWin)
DeviceFocusOutEvents(dev, sprite->win, RootWindow(dev), mode,
NotifyPointer);
for (i = 0; i < nscreens; i++)
DeviceFocusEvent(dev, FocusOut, mode, out, WindowTable[i]);
}
else
{
if (IsParent(from, sprite->win))
DeviceFocusOutEvents(dev, sprite->win, from, mode,
NotifyPointer);
DeviceFocusEvent(dev, FocusOut, mode, NotifyNonlinear, from);
DeviceFocusOutEvents(dev, from->parent, NullWindow, mode,
NotifyNonlinearVirtual);
}
for (i = 0; i < nscreens; i++)
DeviceFocusEvent(dev, FocusIn, mode, in, WindowTable[i]);
if (to == PointerRootWin)
DeviceFocusInEvents(dev, RootWindow(dev), sprite->win, mode, NotifyPointer);
}
else
{
if ((from == NullWindow) || (from == PointerRootWin))
{
if (from == PointerRootWin)
DeviceFocusOutEvents(dev, sprite->win, RootWindow(dev), mode,
NotifyPointer);
for (i = 0; i < nscreens; i++)
DeviceFocusEvent(dev, FocusOut, mode, out, WindowTable[i]);
if (to->parent != NullWindow)
DeviceFocusInEvents(dev, RootWindow(dev), to, mode, NotifyNonlinearVirtual);
DeviceFocusEvent(dev, FocusIn, mode, NotifyNonlinear, to);
if (IsParent(to, sprite->win))
DeviceFocusInEvents(dev, to, sprite->win, mode, NotifyPointer);
}
else
{
if (IsParent(to, from))
{
DeviceFocusEvent(dev, FocusOut, mode, NotifyAncestor, from);
DeviceFocusOutEvents(dev, from->parent, to, mode,
NotifyVirtual);
DeviceFocusEvent(dev, FocusIn, mode, NotifyInferior, to);
if ((IsParent(to, sprite->win)) &&
(sprite->win != from) &&
(!IsParent(from, sprite->win)) &&
(!IsParent(sprite->win, from)))
DeviceFocusInEvents(dev, to, sprite->win, mode, NotifyPointer);
}
else
if (IsParent(from, to))
{
if ((IsParent(from, sprite->win)) &&
(sprite->win != from) &&
(!IsParent(to, sprite->win)) &&
(!IsParent(sprite->win, to)))
DeviceFocusOutEvents(dev, sprite->win, from, mode,
NotifyPointer);
DeviceFocusEvent(dev, FocusOut, mode, NotifyInferior, from);
DeviceFocusInEvents(dev, from, to, mode, NotifyVirtual);
DeviceFocusEvent(dev, FocusIn, mode, NotifyAncestor, to);
}
else
{
WindowPtr common = CommonAncestor(to, from);
if (IsParent(from, sprite->win))
DeviceFocusOutEvents(dev, sprite->win, from, mode,
NotifyPointer);
DeviceFocusEvent(dev, FocusOut, mode, NotifyNonlinear, from);
if (from->parent != NullWindow)
DeviceFocusOutEvents(dev, from->parent, common, mode,
NotifyNonlinearVirtual);
if (to->parent != NullWindow)
DeviceFocusInEvents(dev, common, to, mode, NotifyNonlinearVirtual);
DeviceFocusEvent(dev, FocusIn, mode, NotifyNonlinear, to);
if (IsParent(to, sprite->win))
DeviceFocusInEvents(dev, to, sprite->win, mode, NotifyPointer);
}
}
}
}
void
DoFocusEvents(DeviceIntPtr pDev,
WindowPtr from,
WindowPtr to,
int mode)
{
if (!IsKeyboardDevice(pDev))
return;
if (from == to)
return;
CoreFocusEvents(pDev, from, to, mode);
DeviceFocusEvents(pDev, from, to, mode);
}