They just enumerate all child windows, these events are not frequent enough for this to be a performance bottleneck. The check is something along the lines of
Win* GetChildAt(Win* w, int x, int y)
{
size_t i;
if (x >= w->ScreenX1 && y >= w->ScreenY1 &&
x <= w->ScreenX2 && y <= w->ScreenY2) {
for (i=0; i < w->ChildCount; i++) {
Win* child = GetChildAt(w->Child[i], x, y);
if (child) return child;
}
return w;
}
return NULL;
}
Call this on the root window and you have the deepest child at the given coordinates.
(though there is usually a bit extra logic for handling, e.g., invisible and disabled windows)
How is the click propagated recursively through every component and is the position compared repeatedly at every step?