2001-10-06 14:13:28 +00:00
|
|
|
#include "CXWindowsPrimaryScreen.h"
|
|
|
|
#include "CServer.h"
|
|
|
|
#include "CThread.h"
|
2001-10-14 14:37:41 +00:00
|
|
|
#include "CLog.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <X11/X.h>
|
|
|
|
|
|
|
|
//
|
|
|
|
// CXWindowsPrimaryScreen
|
|
|
|
//
|
|
|
|
|
|
|
|
CXWindowsPrimaryScreen::CXWindowsPrimaryScreen() :
|
|
|
|
m_server(NULL),
|
2001-11-11 21:15:30 +00:00
|
|
|
m_active(false),
|
|
|
|
m_window(None)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
CXWindowsPrimaryScreen::~CXWindowsPrimaryScreen()
|
|
|
|
{
|
2001-11-11 21:15:30 +00:00
|
|
|
assert(m_window == None);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CXWindowsPrimaryScreen::open(CServer* server)
|
|
|
|
{
|
|
|
|
assert(m_server == NULL);
|
|
|
|
assert(server != NULL);
|
|
|
|
|
|
|
|
// set the server
|
|
|
|
m_server = server;
|
|
|
|
|
|
|
|
// open the display
|
2001-11-11 21:15:30 +00:00
|
|
|
openDisplay();
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CXWindowsPrimaryScreen::close()
|
|
|
|
{
|
|
|
|
assert(m_server != NULL);
|
|
|
|
|
|
|
|
// close the display
|
2001-11-11 21:15:30 +00:00
|
|
|
closeDisplay();
|
|
|
|
|
|
|
|
// done with server
|
|
|
|
m_server = NULL;
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CXWindowsPrimaryScreen::enter(SInt32 x, SInt32 y)
|
|
|
|
{
|
2001-10-14 14:37:41 +00:00
|
|
|
log((CLOG_INFO "entering primary at %d,%d", x, y));
|
2001-11-11 21:15:30 +00:00
|
|
|
assert(m_active == true);
|
|
|
|
assert(m_window != None);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2001-11-11 21:15:30 +00:00
|
|
|
CDisplayLock display(this);
|
2001-10-24 22:33:24 +00:00
|
|
|
|
2001-10-06 14:13:28 +00:00
|
|
|
// warp to requested location
|
2001-11-11 21:15:30 +00:00
|
|
|
XWarpPointer(display, None, m_window, 0, 0, 0, 0, x, y);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// unmap the grab window. this also ungrabs the mouse and keyboard.
|
2001-11-11 21:15:30 +00:00
|
|
|
XUnmapWindow(display, m_window);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// remove all input events for grab window
|
|
|
|
XEvent event;
|
2001-11-11 21:15:30 +00:00
|
|
|
while (XCheckWindowEvent(display, m_window,
|
2001-10-06 14:13:28 +00:00
|
|
|
PointerMotionMask |
|
|
|
|
ButtonPressMask | ButtonReleaseMask |
|
|
|
|
KeyPressMask | KeyReleaseMask |
|
|
|
|
KeymapStateMask,
|
|
|
|
&event)) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
// not active anymore
|
|
|
|
m_active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXWindowsPrimaryScreen::leave()
|
|
|
|
{
|
2001-10-14 14:37:41 +00:00
|
|
|
log((CLOG_INFO "leaving primary"));
|
2001-11-11 21:15:30 +00:00
|
|
|
assert(m_active == false);
|
|
|
|
assert(m_window != None);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2001-11-11 21:15:30 +00:00
|
|
|
CDisplayLock display(this);
|
2001-10-24 22:33:24 +00:00
|
|
|
|
2001-10-06 14:13:28 +00:00
|
|
|
// raise and show the input window
|
2001-11-11 21:15:30 +00:00
|
|
|
XMapRaised(display, m_window);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// grab the mouse and keyboard. keep trying until we get them.
|
|
|
|
// if we can't grab one after grabbing the other then ungrab
|
|
|
|
// and wait before retrying.
|
|
|
|
int result;
|
|
|
|
do {
|
|
|
|
// mouse first
|
|
|
|
do {
|
2001-11-11 21:15:30 +00:00
|
|
|
result = XGrabPointer(display, m_window, True, 0,
|
2001-10-06 14:13:28 +00:00
|
|
|
GrabModeAsync, GrabModeAsync,
|
|
|
|
m_window, None, CurrentTime);
|
|
|
|
assert(result != GrabNotViewable);
|
2001-10-14 14:37:41 +00:00
|
|
|
if (result != GrabSuccess) {
|
|
|
|
log((CLOG_DEBUG "waiting to grab pointer"));
|
2001-10-06 14:13:28 +00:00
|
|
|
CThread::sleep(0.25);
|
2001-10-14 14:37:41 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
} while (result != GrabSuccess);
|
2001-10-14 14:37:41 +00:00
|
|
|
log((CLOG_DEBUG "grabbed pointer"));
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// now the keyboard
|
2001-11-11 21:15:30 +00:00
|
|
|
result = XGrabKeyboard(display, m_window, True,
|
2001-10-06 14:13:28 +00:00
|
|
|
GrabModeAsync, GrabModeAsync, CurrentTime);
|
|
|
|
assert(result != GrabNotViewable);
|
|
|
|
if (result != GrabSuccess) {
|
2001-11-11 21:15:30 +00:00
|
|
|
// back off to avoid grab deadlock
|
|
|
|
XUngrabPointer(display, CurrentTime);
|
2001-10-14 14:37:41 +00:00
|
|
|
log((CLOG_DEBUG "ungrabbed pointer, waiting to grab keyboard"));
|
2001-10-06 14:13:28 +00:00
|
|
|
CThread::sleep(0.25);
|
|
|
|
}
|
|
|
|
} while (result != GrabSuccess);
|
2001-10-14 14:37:41 +00:00
|
|
|
log((CLOG_DEBUG "grabbed keyboard"));
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// move the mouse to the center of grab window
|
2001-11-11 21:15:30 +00:00
|
|
|
SInt32 w, h;
|
|
|
|
getScreenSize(&w, &h);
|
|
|
|
warpCursorNoLock(display, w >> 1, h >> 1);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// local client now active
|
|
|
|
m_active = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXWindowsPrimaryScreen::warpCursor(SInt32 x, SInt32 y)
|
|
|
|
{
|
2001-11-11 21:15:30 +00:00
|
|
|
CDisplayLock display(this);
|
|
|
|
warpCursorNoLock(display, x, y);
|
2001-10-24 22:33:24 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2001-10-24 22:33:24 +00:00
|
|
|
void CXWindowsPrimaryScreen::warpCursorNoLock(
|
2001-11-11 21:15:30 +00:00
|
|
|
Display* display, SInt32 x, SInt32 y)
|
2001-10-24 22:33:24 +00:00
|
|
|
{
|
2001-11-11 21:15:30 +00:00
|
|
|
assert(display != NULL);
|
|
|
|
assert(m_window != None);
|
|
|
|
|
2001-10-06 14:13:28 +00:00
|
|
|
// warp the mouse
|
2001-11-11 21:15:30 +00:00
|
|
|
XWarpPointer(display, None, getRoot(), 0, 0, 0, 0, x, y);
|
|
|
|
XSync(display, False);
|
2001-10-14 14:37:41 +00:00
|
|
|
log((CLOG_DEBUG "warped to %d,%d", x, y));
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// discard mouse events since we just added one we don't want
|
|
|
|
XEvent xevent;
|
2001-11-11 21:15:30 +00:00
|
|
|
while (XCheckWindowEvent(display, m_window,
|
2001-10-06 14:13:28 +00:00
|
|
|
PointerMotionMask, &xevent)) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-13 23:34:12 +00:00
|
|
|
#include <X11/Xatom.h> // FIXME
|
|
|
|
void CXWindowsPrimaryScreen::setClipboard(
|
|
|
|
const IClipboard* /*clipboard*/)
|
|
|
|
{
|
|
|
|
// FIXME -- put this in superclass?
|
|
|
|
// FIXME -- don't use CurrentTime
|
|
|
|
CDisplayLock display(this);
|
|
|
|
XSetSelectionOwner(display, XA_PRIMARY, m_window, CurrentTime);
|
|
|
|
if (XGetSelectionOwner(display, XA_PRIMARY) == m_window) {
|
|
|
|
// we got the selection
|
|
|
|
log((CLOG_DEBUG "grabbed clipboard"));
|
|
|
|
}
|
|
|
|
// FIXME -- need to copy or adopt the clipboard to serve future requests
|
|
|
|
}
|
|
|
|
|
2001-10-06 14:13:28 +00:00
|
|
|
void CXWindowsPrimaryScreen::getSize(
|
|
|
|
SInt32* width, SInt32* height) const
|
|
|
|
{
|
2001-11-11 21:15:30 +00:00
|
|
|
getScreenSize(width, height);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SInt32 CXWindowsPrimaryScreen::getJumpZoneSize() const
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-11-13 23:34:12 +00:00
|
|
|
void CXWindowsPrimaryScreen::getClipboard(
|
|
|
|
IClipboard* clipboard) const
|
|
|
|
{
|
|
|
|
// FIXME -- put this in superclass?
|
|
|
|
// FIXME -- don't use CurrentTime
|
|
|
|
getDisplayClipboard(clipboard, m_window, CurrentTime);
|
|
|
|
}
|
|
|
|
|
2001-11-11 21:15:30 +00:00
|
|
|
void CXWindowsPrimaryScreen::onOpenDisplay()
|
|
|
|
{
|
|
|
|
assert(m_window == None);
|
|
|
|
|
|
|
|
CDisplayLock display(this);
|
|
|
|
|
|
|
|
// get size of screen
|
|
|
|
SInt32 w, h;
|
|
|
|
getScreenSize(&w, &h);
|
|
|
|
|
|
|
|
// create the grab window. this window is used to capture user
|
|
|
|
// input when the user is focussed on another client. don't let
|
|
|
|
// the window manager mess with it.
|
|
|
|
XSetWindowAttributes attr;
|
|
|
|
attr.event_mask = PointerMotionMask |// PointerMotionHintMask |
|
|
|
|
ButtonPressMask | ButtonReleaseMask |
|
|
|
|
KeyPressMask | KeyReleaseMask |
|
|
|
|
KeymapStateMask;
|
|
|
|
attr.do_not_propagate_mask = 0;
|
|
|
|
attr.override_redirect = True;
|
|
|
|
attr.cursor = createBlankCursor();
|
|
|
|
m_window = XCreateWindow(display, getRoot(), 0, 0, w, h, 0, 0,
|
|
|
|
InputOnly, CopyFromParent,
|
|
|
|
CWDontPropagate | CWEventMask |
|
|
|
|
CWOverrideRedirect | CWCursor,
|
|
|
|
&attr);
|
|
|
|
|
|
|
|
// start watching for events on other windows
|
|
|
|
selectEvents(display, getRoot());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXWindowsPrimaryScreen::onCloseDisplay()
|
|
|
|
{
|
|
|
|
assert(m_window != None);
|
|
|
|
|
|
|
|
// destroy window
|
|
|
|
CDisplayLock display(this);
|
|
|
|
XDestroyWindow(display, m_window);
|
|
|
|
m_window = None;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXWindowsPrimaryScreen::selectEvents(
|
|
|
|
Display* display, Window w) const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
// we want to track the mouse everywhere on the display. to achieve
|
|
|
|
// that we select PointerMotionMask on every window. we also select
|
|
|
|
// SubstructureNotifyMask in order to get CreateNotify events so we
|
|
|
|
// select events on new windows too.
|
|
|
|
|
|
|
|
// we don't want to adjust our grab window
|
|
|
|
if (w == m_window)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// select events of interest
|
2001-11-13 23:34:12 +00:00
|
|
|
XSelectInput(display, w, PointerMotionMask | SubstructureNotifyMask |
|
|
|
|
PropertyChangeMask);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// recurse on child windows
|
|
|
|
Window rw, pw, *cw;
|
|
|
|
unsigned int nc;
|
2001-11-11 21:15:30 +00:00
|
|
|
if (XQueryTree(display, w, &rw, &pw, &cw, &nc)) {
|
2001-10-06 14:13:28 +00:00
|
|
|
for (unsigned int i = 0; i < nc; ++i)
|
2001-11-11 21:15:30 +00:00
|
|
|
selectEvents(display, cw[i]);
|
2001-10-25 22:17:17 +00:00
|
|
|
XFree(cw);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXWindowsPrimaryScreen::eventThread(void*)
|
|
|
|
{
|
|
|
|
for (;;) {
|
2001-11-11 21:15:30 +00:00
|
|
|
// wait for and get the next event
|
2001-10-06 14:13:28 +00:00
|
|
|
XEvent xevent;
|
2001-11-11 21:15:30 +00:00
|
|
|
getEvent(&xevent);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// handle event
|
|
|
|
switch (xevent.type) {
|
2001-10-24 22:33:24 +00:00
|
|
|
case CreateNotify: {
|
2001-10-06 14:13:28 +00:00
|
|
|
// select events on new window
|
2001-11-11 21:15:30 +00:00
|
|
|
CDisplayLock display(this);
|
|
|
|
selectEvents(display, xevent.xcreatewindow.window);
|
2001-10-06 14:13:28 +00:00
|
|
|
break;
|
2001-10-24 22:33:24 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
case KeyPress: {
|
2001-10-14 14:37:41 +00:00
|
|
|
log((CLOG_DEBUG "event: KeyPress code=%d, state=0x%04x", xevent.xkey.keycode, xevent.xkey.state));
|
2001-10-06 14:13:28 +00:00
|
|
|
const KeyModifierMask mask = mapModifier(xevent.xkey.state);
|
|
|
|
const KeyID key = mapKey(xevent.xkey.keycode, mask);
|
|
|
|
if (key != kKeyNone) {
|
|
|
|
m_server->onKeyDown(key, mask);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME -- simulate key repeat. X sends press/release for
|
|
|
|
// repeat. must detect auto repeat and use kKeyRepeat.
|
|
|
|
case KeyRelease: {
|
2001-10-14 14:37:41 +00:00
|
|
|
log((CLOG_DEBUG "event: KeyRelease code=%d, state=0x%04x", xevent.xkey.keycode, xevent.xkey.state));
|
2001-10-06 14:13:28 +00:00
|
|
|
const KeyModifierMask mask = mapModifier(xevent.xkey.state);
|
|
|
|
const KeyID key = mapKey(xevent.xkey.keycode, mask);
|
|
|
|
if (key != kKeyNone) {
|
|
|
|
m_server->onKeyUp(key, mask);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ButtonPress: {
|
2001-10-14 14:37:41 +00:00
|
|
|
log((CLOG_DEBUG "event: ButtonPress button=%d", xevent.xbutton.button));
|
2001-10-06 14:13:28 +00:00
|
|
|
const ButtonID button = mapButton(xevent.xbutton.button);
|
|
|
|
if (button != kButtonNone) {
|
|
|
|
m_server->onMouseDown(button);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ButtonRelease: {
|
2001-10-14 14:37:41 +00:00
|
|
|
log((CLOG_DEBUG "event: ButtonRelease button=%d", xevent.xbutton.button));
|
2001-10-06 14:13:28 +00:00
|
|
|
const ButtonID button = mapButton(xevent.xbutton.button);
|
|
|
|
if (button != kButtonNone) {
|
|
|
|
m_server->onMouseUp(button);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MotionNotify: {
|
2001-10-14 14:37:41 +00:00
|
|
|
log((CLOG_DEBUG "event: MotionNotify %d,%d", xevent.xmotion.x_root, xevent.xmotion.y_root));
|
2001-10-06 14:13:28 +00:00
|
|
|
SInt32 x, y;
|
|
|
|
if (!m_active) {
|
|
|
|
x = xevent.xmotion.x_root;
|
|
|
|
y = xevent.xmotion.y_root;
|
|
|
|
m_server->onMouseMovePrimary(x, y);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// FIXME -- slurp up all remaining motion events?
|
|
|
|
// probably not since key strokes may go to wrong place.
|
|
|
|
|
|
|
|
// get mouse deltas
|
2001-10-24 22:33:24 +00:00
|
|
|
{
|
2001-11-11 21:15:30 +00:00
|
|
|
CDisplayLock display(this);
|
2001-10-24 22:33:24 +00:00
|
|
|
Window root, window;
|
|
|
|
int xRoot, yRoot, xWindow, yWindow;
|
|
|
|
unsigned int mask;
|
2001-11-11 21:15:30 +00:00
|
|
|
if (!XQueryPointer(display, m_window, &root, &window,
|
2001-10-06 14:13:28 +00:00
|
|
|
&xRoot, &yRoot, &xWindow, &yWindow, &mask))
|
2001-10-24 22:33:24 +00:00
|
|
|
break;
|
|
|
|
|
2001-11-11 21:15:30 +00:00
|
|
|
// compute position of center of window
|
|
|
|
SInt32 w, h;
|
|
|
|
getScreenSize(&w, &h);
|
|
|
|
x = xRoot - (w >> 1);
|
|
|
|
y = yRoot - (h >> 1);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2001-10-24 22:33:24 +00:00
|
|
|
// warp mouse back to center
|
2001-11-11 21:15:30 +00:00
|
|
|
warpCursorNoLock(display, w >> 1, h >> 1);
|
2001-10-24 22:33:24 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
m_server->onMouseMoveSecondary(x, y);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
case SelectionClear:
|
|
|
|
target->XXX(xevent.xselectionclear.);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SelectionNotify:
|
|
|
|
target->XXX(xevent.xselection.);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SelectionRequest:
|
|
|
|
target->XXX(xevent.xselectionrequest.);
|
|
|
|
break;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyModifierMask CXWindowsPrimaryScreen::mapModifier(
|
|
|
|
unsigned int state) const
|
|
|
|
{
|
|
|
|
// FIXME -- should be configurable
|
|
|
|
KeyModifierMask mask = 0;
|
|
|
|
if (state & 1)
|
|
|
|
mask |= KeyModifierShift;
|
|
|
|
if (state & 2)
|
|
|
|
mask |= KeyModifierCapsLock;
|
|
|
|
if (state & 4)
|
|
|
|
mask |= KeyModifierControl;
|
|
|
|
if (state & 8)
|
|
|
|
mask |= KeyModifierAlt;
|
|
|
|
if (state & 16)
|
|
|
|
mask |= KeyModifierNumLock;
|
|
|
|
if (state & 32)
|
|
|
|
mask |= KeyModifierMeta;
|
|
|
|
if (state & 128)
|
|
|
|
mask |= KeyModifierScrollLock;
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyID CXWindowsPrimaryScreen::mapKey(
|
|
|
|
KeyCode keycode, KeyModifierMask mask) const
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
if (mask & KeyModifierShift)
|
|
|
|
index = 1;
|
|
|
|
else
|
|
|
|
index = 0;
|
2001-11-11 21:15:30 +00:00
|
|
|
CDisplayLock display(this);
|
|
|
|
return static_cast<KeyID>(XKeycodeToKeysym(display, keycode, index));
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ButtonID CXWindowsPrimaryScreen::mapButton(
|
|
|
|
unsigned int button) const
|
|
|
|
{
|
|
|
|
// FIXME -- should use button mapping?
|
|
|
|
if (button >= 1 && button <= 3)
|
|
|
|
return static_cast<ButtonID>(button);
|
|
|
|
else
|
|
|
|
return kButtonNone;
|
|
|
|
}
|