2001-10-08 19:24:46 +00:00
|
|
|
#include "CXWindowsSecondaryScreen.h"
|
|
|
|
#include "CClient.h"
|
|
|
|
#include "CThread.h"
|
2001-10-24 22:33:24 +00:00
|
|
|
#include "CLock.h"
|
2001-10-08 19:24:46 +00:00
|
|
|
#include "TMethodJob.h"
|
2001-10-23 22:41:46 +00:00
|
|
|
#include "CLog.h"
|
2001-10-08 19:24:46 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <X11/X.h>
|
|
|
|
#include <X11/extensions/XTest.h>
|
|
|
|
|
|
|
|
//
|
|
|
|
// CXWindowsSecondaryScreen
|
|
|
|
//
|
|
|
|
|
|
|
|
CXWindowsSecondaryScreen::CXWindowsSecondaryScreen() :
|
|
|
|
m_client(NULL),
|
|
|
|
m_display(NULL),
|
|
|
|
m_window(None),
|
|
|
|
m_w(0), m_h(0)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
CXWindowsSecondaryScreen::~CXWindowsSecondaryScreen()
|
|
|
|
{
|
|
|
|
assert(m_display == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXWindowsSecondaryScreen::open(CClient* client)
|
|
|
|
{
|
|
|
|
assert(m_client == NULL);
|
|
|
|
assert(client != NULL);
|
|
|
|
|
|
|
|
// set the client
|
|
|
|
m_client = client;
|
|
|
|
|
|
|
|
// open the display
|
2001-10-23 22:41:46 +00:00
|
|
|
log((CLOG_DEBUG "XOpenDisplay(%s)", "NULL"));
|
2001-10-25 22:17:17 +00:00
|
|
|
m_display = XOpenDisplay(NULL); // FIXME -- allow non-default
|
2001-10-08 19:24:46 +00:00
|
|
|
if (m_display == NULL)
|
|
|
|
throw int(5); // FIXME -- make exception for this
|
|
|
|
|
|
|
|
// get default screen
|
|
|
|
m_screen = DefaultScreen(m_display);
|
|
|
|
Screen* screen = ScreenOfDisplay(m_display, m_screen);
|
|
|
|
|
|
|
|
// get screen size
|
|
|
|
m_w = WidthOfScreen(screen);
|
|
|
|
m_h = HeightOfScreen(screen);
|
2001-10-23 22:41:46 +00:00
|
|
|
log((CLOG_INFO "secondary display size: %dx%d", m_w, m_h));
|
2001-10-08 19:24:46 +00:00
|
|
|
|
|
|
|
// verify the availability of the XTest extension
|
|
|
|
int majorOpcode, firstEvent, firstError;
|
2001-10-25 22:17:17 +00:00
|
|
|
if (!XQueryExtension(m_display, XTestExtensionName,
|
2001-10-08 19:24:46 +00:00
|
|
|
&majorOpcode, &firstEvent, &firstError))
|
|
|
|
throw int(6); // FIXME -- make exception for this
|
|
|
|
|
2001-10-23 22:41:46 +00:00
|
|
|
// get the root window
|
|
|
|
m_root = RootWindow(m_display, m_screen);
|
|
|
|
|
|
|
|
// create the cursor hiding window. this window is used to hide the
|
|
|
|
// cursor when it's not on the screen. the window is hidden as soon
|
|
|
|
// as the cursor enters the screen or the display's real cursor is
|
|
|
|
// moved.
|
|
|
|
XSetWindowAttributes attr;
|
|
|
|
attr.event_mask = LeaveWindowMask;
|
|
|
|
attr.do_not_propagate_mask = 0;
|
|
|
|
attr.override_redirect = True;
|
|
|
|
attr.cursor = createBlankCursor();
|
2001-10-25 22:17:17 +00:00
|
|
|
m_window = XCreateWindow(m_display, m_root, 0, 0, 1, 1, 0, 0,
|
2001-10-23 22:41:46 +00:00
|
|
|
InputOnly, CopyFromParent,
|
|
|
|
CWDontPropagate | CWEventMask |
|
|
|
|
CWOverrideRedirect | CWCursor,
|
|
|
|
&attr);
|
|
|
|
|
2001-10-08 19:24:46 +00:00
|
|
|
// become impervious to server grabs
|
2001-10-25 22:17:17 +00:00
|
|
|
XTestGrabControl(m_display, True);
|
2001-10-08 19:24:46 +00:00
|
|
|
|
2001-10-23 22:41:46 +00:00
|
|
|
// hide the cursor
|
|
|
|
leave();
|
|
|
|
|
2001-10-08 19:24:46 +00:00
|
|
|
// start processing events
|
|
|
|
m_eventThread = new CThread(new TMethodJob<CXWindowsSecondaryScreen>(
|
|
|
|
this, &CXWindowsSecondaryScreen::eventThread));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXWindowsSecondaryScreen::close()
|
|
|
|
{
|
|
|
|
assert(m_client != NULL);
|
|
|
|
assert(m_eventThread != NULL);
|
|
|
|
|
|
|
|
// stop event thread
|
|
|
|
m_eventThread->cancel();
|
|
|
|
m_eventThread->wait();
|
|
|
|
delete m_eventThread;
|
|
|
|
m_eventThread = NULL;
|
|
|
|
|
|
|
|
// no longer impervious to server grabs
|
2001-10-25 22:17:17 +00:00
|
|
|
XTestGrabControl(m_display, False);
|
2001-10-08 19:24:46 +00:00
|
|
|
|
2001-10-23 22:41:46 +00:00
|
|
|
// destroy window
|
2001-10-25 22:17:17 +00:00
|
|
|
XDestroyWindow(m_display, m_window);
|
2001-10-23 22:41:46 +00:00
|
|
|
m_window = None;
|
|
|
|
|
2001-10-08 19:24:46 +00:00
|
|
|
// close the display
|
2001-10-25 22:17:17 +00:00
|
|
|
XCloseDisplay(m_display);
|
2001-10-08 19:24:46 +00:00
|
|
|
m_display = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXWindowsSecondaryScreen::enter(SInt32 x, SInt32 y)
|
|
|
|
{
|
|
|
|
assert(m_display != NULL);
|
2001-10-23 22:41:46 +00:00
|
|
|
assert(m_window != None);
|
2001-10-08 19:24:46 +00:00
|
|
|
|
2001-10-24 22:33:24 +00:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
2001-10-08 19:24:46 +00:00
|
|
|
// warp to requested location
|
2001-10-25 22:17:17 +00:00
|
|
|
XTestFakeMotionEvent(m_display, m_screen, x, y, CurrentTime);
|
|
|
|
XSync(m_display, False);
|
2001-10-08 19:24:46 +00:00
|
|
|
|
|
|
|
// show cursor
|
2001-10-25 22:17:17 +00:00
|
|
|
XUnmapWindow(m_display, m_window);
|
2001-10-08 19:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CXWindowsSecondaryScreen::leave()
|
|
|
|
{
|
|
|
|
assert(m_display != NULL);
|
2001-10-23 22:41:46 +00:00
|
|
|
assert(m_window != None);
|
2001-10-08 19:24:46 +00:00
|
|
|
|
2001-10-24 22:33:24 +00:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
2001-10-25 22:17:17 +00:00
|
|
|
// move hider window under the mouse (rather than moving the mouse
|
|
|
|
// somewhere else on the screen)
|
2001-10-25 22:09:27 +00:00
|
|
|
int x, y, dummy;
|
|
|
|
unsigned int dummyMask;
|
|
|
|
Window dummyWindow;
|
2001-10-25 22:17:17 +00:00
|
|
|
XQueryPointer(m_display, m_root, &dummyWindow, &dummyWindow,
|
2001-10-25 22:09:27 +00:00
|
|
|
&x, &y, &dummy, &dummy, &dummyMask);
|
2001-10-25 22:17:17 +00:00
|
|
|
XMoveWindow(m_display, m_window, x, y);
|
2001-10-25 22:09:27 +00:00
|
|
|
|
2001-10-23 22:41:46 +00:00
|
|
|
// raise and show the hider window
|
2001-10-25 22:17:17 +00:00
|
|
|
XMapRaised(m_display, m_window);
|
2001-10-23 22:41:46 +00:00
|
|
|
|
|
|
|
// hide cursor by moving it into the hider window
|
2001-10-25 22:17:17 +00:00
|
|
|
XWarpPointer(m_display, None, m_window, 0, 0, 0, 0, 0, 0);
|
2001-10-08 19:24:46 +00:00
|
|
|
}
|
|
|
|
|
2001-10-25 21:40:29 +00:00
|
|
|
void CXWindowsSecondaryScreen::keyDown(
|
2001-10-08 19:24:46 +00:00
|
|
|
KeyID key, KeyModifierMask mask)
|
|
|
|
{
|
|
|
|
assert(m_display != NULL);
|
|
|
|
|
2001-10-24 22:33:24 +00:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
2001-10-25 22:17:17 +00:00
|
|
|
XTestFakeKeyEvent(m_display, mapKey(key, mask), True, CurrentTime);
|
|
|
|
XSync(m_display, False);
|
2001-10-08 19:24:46 +00:00
|
|
|
}
|
|
|
|
|
2001-10-25 21:40:29 +00:00
|
|
|
void CXWindowsSecondaryScreen::keyRepeat(
|
2001-10-08 19:24:46 +00:00
|
|
|
KeyID, KeyModifierMask, SInt32)
|
|
|
|
{
|
|
|
|
assert(m_display != NULL);
|
|
|
|
|
2001-10-24 22:33:24 +00:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
2001-10-08 19:24:46 +00:00
|
|
|
// FIXME
|
|
|
|
}
|
|
|
|
|
2001-10-25 21:40:29 +00:00
|
|
|
void CXWindowsSecondaryScreen::keyUp(
|
2001-10-08 19:24:46 +00:00
|
|
|
KeyID key, KeyModifierMask mask)
|
|
|
|
{
|
|
|
|
assert(m_display != NULL);
|
|
|
|
|
2001-10-24 22:33:24 +00:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
2001-10-25 22:17:17 +00:00
|
|
|
XTestFakeKeyEvent(m_display, mapKey(key, mask), False, CurrentTime);
|
|
|
|
XSync(m_display, False);
|
2001-10-08 19:24:46 +00:00
|
|
|
}
|
|
|
|
|
2001-10-25 21:40:29 +00:00
|
|
|
void CXWindowsSecondaryScreen::mouseDown(ButtonID button)
|
2001-10-08 19:24:46 +00:00
|
|
|
{
|
|
|
|
assert(m_display != NULL);
|
|
|
|
|
2001-10-24 22:33:24 +00:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
2001-10-25 22:17:17 +00:00
|
|
|
XTestFakeButtonEvent(m_display, mapButton(button), True, CurrentTime);
|
|
|
|
XSync(m_display, False);
|
2001-10-08 19:24:46 +00:00
|
|
|
}
|
|
|
|
|
2001-10-25 21:40:29 +00:00
|
|
|
void CXWindowsSecondaryScreen::mouseUp(ButtonID button)
|
2001-10-08 19:24:46 +00:00
|
|
|
{
|
|
|
|
assert(m_display != NULL);
|
|
|
|
|
2001-10-24 22:33:24 +00:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
2001-10-25 22:17:17 +00:00
|
|
|
XTestFakeButtonEvent(m_display, mapButton(button), False, CurrentTime);
|
|
|
|
XSync(m_display, False);
|
2001-10-08 19:24:46 +00:00
|
|
|
}
|
|
|
|
|
2001-10-25 21:40:29 +00:00
|
|
|
void CXWindowsSecondaryScreen::mouseMove(SInt32 x, SInt32 y)
|
2001-10-08 19:24:46 +00:00
|
|
|
{
|
|
|
|
assert(m_display != NULL);
|
|
|
|
|
2001-10-24 22:33:24 +00:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
2001-10-25 22:17:17 +00:00
|
|
|
XTestFakeMotionEvent(m_display, m_screen, x, y, CurrentTime);
|
|
|
|
XSync(m_display, False);
|
2001-10-08 19:24:46 +00:00
|
|
|
}
|
|
|
|
|
2001-10-25 21:40:29 +00:00
|
|
|
void CXWindowsSecondaryScreen::mouseWheel(SInt32)
|
2001-10-08 19:24:46 +00:00
|
|
|
{
|
|
|
|
assert(m_display != NULL);
|
|
|
|
|
2001-10-24 22:33:24 +00:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
2001-10-08 19:24:46 +00:00
|
|
|
// FIXME
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXWindowsSecondaryScreen::getSize(
|
|
|
|
SInt32* width, SInt32* height) const
|
|
|
|
{
|
|
|
|
assert(m_display != NULL);
|
|
|
|
assert(width != NULL && height != NULL);
|
|
|
|
|
|
|
|
*width = m_w;
|
|
|
|
*height = m_h;
|
|
|
|
}
|
|
|
|
|
|
|
|
SInt32 CXWindowsSecondaryScreen::getJumpZoneSize() const
|
|
|
|
{
|
|
|
|
assert(m_display != NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-10-23 22:41:46 +00:00
|
|
|
Cursor CXWindowsSecondaryScreen::createBlankCursor()
|
|
|
|
{
|
|
|
|
// this seems just a bit more complicated than really necessary
|
|
|
|
|
|
|
|
// get the closet cursor size to 1x1
|
|
|
|
unsigned int w, h;
|
2001-10-25 22:17:17 +00:00
|
|
|
XQueryBestCursor(m_display, m_root, 1, 1, &w, &h);
|
2001-10-23 22:41:46 +00:00
|
|
|
|
|
|
|
// make bitmap data for cursor of closet size. since the cursor
|
|
|
|
// is blank we can use the same bitmap for shape and mask: all
|
|
|
|
// zeros.
|
|
|
|
const int size = ((w + 7) >> 3) * h;
|
|
|
|
char* data = new char[size];
|
|
|
|
memset(data, 0, size);
|
|
|
|
|
|
|
|
// make bitmap
|
2001-10-25 22:17:17 +00:00
|
|
|
Pixmap bitmap = XCreateBitmapFromData(m_display, m_root, data, w, h);
|
2001-10-23 22:41:46 +00:00
|
|
|
|
|
|
|
// need an arbitrary color for the cursor
|
|
|
|
XColor color;
|
|
|
|
color.pixel = 0;
|
|
|
|
color.red = color.green = color.blue = 0;
|
|
|
|
color.flags = DoRed | DoGreen | DoBlue;
|
|
|
|
|
|
|
|
// make cursor from bitmap
|
2001-10-25 22:17:17 +00:00
|
|
|
Cursor cursor = XCreatePixmapCursor(m_display, bitmap, bitmap,
|
2001-10-23 22:41:46 +00:00
|
|
|
&color, &color, 0, 0);
|
|
|
|
|
|
|
|
// don't need bitmap or the data anymore
|
|
|
|
delete[] data;
|
2001-10-25 22:17:17 +00:00
|
|
|
XFreePixmap(m_display, bitmap);
|
2001-10-23 22:41:46 +00:00
|
|
|
|
|
|
|
return cursor;
|
|
|
|
}
|
|
|
|
|
2001-10-08 19:24:46 +00:00
|
|
|
void CXWindowsSecondaryScreen::eventThread(void*)
|
|
|
|
{
|
2001-10-23 22:41:46 +00:00
|
|
|
assert(m_display != NULL);
|
|
|
|
assert(m_window != None);
|
|
|
|
|
2001-10-08 19:24:46 +00:00
|
|
|
for (;;) {
|
|
|
|
// wait for and then get the next event
|
2001-10-24 22:33:24 +00:00
|
|
|
m_mutex.lock();
|
2001-10-08 19:24:46 +00:00
|
|
|
while (XPending(m_display) == 0) {
|
2001-10-24 22:33:24 +00:00
|
|
|
m_mutex.unlock();
|
2001-10-08 19:24:46 +00:00
|
|
|
CThread::sleep(0.05);
|
2001-10-24 22:33:24 +00:00
|
|
|
m_mutex.lock();
|
2001-10-08 19:24:46 +00:00
|
|
|
}
|
|
|
|
XEvent xevent;
|
|
|
|
XNextEvent(m_display, &xevent);
|
2001-10-24 22:33:24 +00:00
|
|
|
m_mutex.unlock();
|
2001-10-08 19:24:46 +00:00
|
|
|
|
|
|
|
// handle event
|
|
|
|
switch (xevent.type) {
|
2001-10-24 22:33:24 +00:00
|
|
|
case LeaveNotify: {
|
2001-10-23 22:41:46 +00:00
|
|
|
// mouse moved out of hider window somehow. hide the window.
|
2001-10-24 22:33:24 +00:00
|
|
|
CLock lock(&m_mutex);
|
2001-10-25 22:17:17 +00:00
|
|
|
XUnmapWindow(m_display, m_window);
|
2001-10-08 19:24:46 +00:00
|
|
|
break;
|
2001-10-24 22:33:24 +00:00
|
|
|
}
|
2001-10-08 19:24:46 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
// FIXME -- handle screen resolution changes
|
|
|
|
|
|
|
|
case SelectionClear:
|
|
|
|
target->XXX(xevent.xselectionclear.);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SelectionNotify:
|
|
|
|
target->XXX(xevent.xselection.);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SelectionRequest:
|
|
|
|
target->XXX(xevent.xselectionrequest.);
|
|
|
|
break;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyCode CXWindowsSecondaryScreen::mapKey(
|
|
|
|
KeyID id, KeyModifierMask /*mask*/) const
|
|
|
|
{
|
|
|
|
// FIXME -- use mask
|
2001-10-25 22:17:17 +00:00
|
|
|
return XKeysymToKeycode(m_display, static_cast<KeySym>(id));
|
2001-10-08 19:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int CXWindowsSecondaryScreen::mapButton(
|
|
|
|
ButtonID id) const
|
|
|
|
{
|
|
|
|
// FIXME -- should use button mapping?
|
|
|
|
return static_cast<unsigned int>(id);
|
|
|
|
}
|