cleanup in windows version of DFKeys

develop
Petr Mrázek 2009-12-13 04:08:42 +00:00
parent b132228f22
commit 9ea2876203
2 changed files with 61 additions and 368 deletions

@ -120,6 +120,7 @@ Window EnumerateWindows (Display *display, Window rootWindow, const char *search
{
return BadWindow;
}
for (i = 0; i < noOfChildren; i++)
{
Window tempWindow = EnumerateWindows (display, children[i], searchString);
@ -133,7 +134,7 @@ Window EnumerateWindows (Display *display, Window rootWindow, const char *search
XFree ( (char*) children);
return retWindow;
}
// END ENUMERATE WINDOWS
bool getDFWindow (Display *dpy, Window& dfWindow, Window & rootWindow)
{
// int numScreeens = ScreenCount(dpy);
@ -151,51 +152,6 @@ bool getDFWindow (Display *dpy, Window& dfWindow, Window & rootWindow)
return false;
}
// FIXME: Too much black magic.
bool setWMClientLeaderProperty (Display *dpy, Window &dfWin, Window &currentFocus)
{
static bool propertySet;
if (propertySet)
{
return true;
}
Atom leaderAtom;
Atom type;
int format, status;
unsigned long nitems = 0;
unsigned long extra = 0;
unsigned char *data = 0;
Window result = 0;
leaderAtom = XInternAtom (dpy, "WM_CLIENT_LEADER", false);
status = XGetWindowProperty (dpy, currentFocus, leaderAtom, 0L, 1L, false, XA_WINDOW, &type, &format, &nitems, &extra, (unsigned char **) & data);
if (status == Success)
{
if (data != 0)
{
result = * ( (Window*) data);
XFree (data);
}
else
{
Window curr = currentFocus;
while (data == 0)
{
Window parent;
Window root;
Window *children;
uint numChildren;
XQueryTree (dpy, curr, &root, &parent, &children, &numChildren);
XGetWindowProperty (dpy, parent, leaderAtom, 0L, 1L, false, XA_WINDOW, &type, &format, &nitems, &extra, (unsigned char **) &data);
}
result = * ( (Window*) data);
XFree (data);
}
}
XChangeProperty (dpy, dfWin, leaderAtom, XA_WINDOW, 32, PropModeReplace, (const unsigned char *) &result, 1);
propertySet = true;
return true;
}
// let's hope it works
void send_xkeyevent(Display *display, Window dfW,Window rootW, int keycode, int modstate, int is_press, useconds_t delay)
{
@ -269,7 +225,6 @@ void API::TypeSpecial (t_special command, int count, int delay)
{
mykeysym = ksTable[command];
xkeycode = XKeysymToKeycode (dpy, mykeysym);
//XTestFakeKeyEvent (dpy, XKeysymToKeycode (dpy, XStringToKeysym ("Shift_L")), false, CurrentTime);
send_xkeyevent(dpy,dfWin,rootWin,ksTable[DFHack::LEFT_SHIFT],0,false, delay * 1000);
send_xkeyevent(dpy,dfWin,rootWin,xkeycode,0,true, delay * 1000);
send_xkeyevent(dpy,dfWin,rootWin,xkeycode,0,false, delay * 1000);
@ -285,124 +240,4 @@ void API::TypeSpecial (t_special command, int count, int delay)
{
usleep (delay*1000 * count);
}
}
/*
void API::TypeStr (const char *lpszString, int delay, bool useShift)
{
ForceResume();
Display *dpy = XOpenDisplay (NULL); // null opens the display in $DISPLAY
Window dfWin;
Window rootWin;
if (getDFWindow (dpy, dfWin, rootWin))
{
XWindowAttributes currAttr;
Window currentFocus;
int currentRevert;
XGetInputFocus (dpy, &currentFocus, &currentRevert); //get current focus
setWMClientLeaderProperty (dpy, dfWin, currentFocus);
XGetWindowAttributes (dpy, dfWin, &currAttr);
if (currAttr.map_state == IsUnmapped)
{
XMapRaised (dpy, dfWin);
}
if (currAttr.map_state == IsUnviewable)
{
XRaiseWindow (dpy, dfWin);
}
XSync (dpy, false);
XSetInputFocus (dpy, dfWin, RevertToNone, CurrentTime);
XSync (dpy, false);
char cChar;
KeyCode xkeycode;
char prevKey = 0;
int sleepAmnt = 0;
while ( (cChar = *lpszString++)) // loops through chars
{
xkeycode = XKeysymToKeycode (dpy, cChar);
//HACK add an extra shift up event, this fixes the problem of the same character twice in a row being displayed in df
XTestFakeKeyEvent (dpy, XKeysymToKeycode (dpy, XStringToKeysym ("Shift_L")), false, CurrentTime);
if (useShift || cChar >= 'A' && cChar <= 'Z')
{
XTestFakeKeyEvent (dpy, XKeysymToKeycode (dpy, XStringToKeysym ("Shift_L")), true, CurrentTime);
XSync (dpy, false);
}
XTestFakeKeyEvent (dpy, xkeycode, true, CurrentTime);
XSync (dpy, false);
XTestFakeKeyEvent (dpy, xkeycode, false, CurrentTime);
XSync (dpy, false);
if (useShift || cChar >= 'A' && cChar <= 'Z')
{
XTestFakeKeyEvent (dpy, XKeysymToKeycode (dpy, XStringToKeysym ("Shift_L")), false, CurrentTime);
XSync (dpy, false);
}
usleep(10000);
}
if (currAttr.map_state == IsUnmapped)
{
// XUnmapWindow(dpy,dfWin); if I unmap the window, it is no longer on the task bar, so just lower it instead
XLowerWindow (dpy, dfWin);
}
XSetInputFocus (dpy, currentFocus, currentRevert, CurrentTime);
XSync (dpy, true);
}
usleep (delay*1000);
}
*/
/*
void API::TypeSpecial (t_special command, int count, int delay)
{
ForceResume();
if (command != WAIT)
{
KeySym mykeysym;
KeyCode xkeycode;
Display *dpy = XOpenDisplay (NULL); // null opens the display in $DISPLAY
Window dfWin;
Window rootWin;
if (getDFWindow (dpy, dfWin, rootWin))
{
XWindowAttributes currAttr;
Window currentFocus;
int currentRevert;
XGetInputFocus (dpy, &currentFocus, &currentRevert); //get current focus
setWMClientLeaderProperty (dpy, dfWin, currentFocus);
XGetWindowAttributes (dpy, dfWin, &currAttr);
if (currAttr.map_state == IsUnmapped)
{
XMapRaised (dpy, dfWin);
}
if (currAttr.map_state == IsUnviewable)
{
XRaiseWindow (dpy, dfWin);
}
XSync (dpy, false);
XSetInputFocus (dpy, dfWin, RevertToParent, CurrentTime);
for (int i = 0;i < count; i++)
{
//HACK add an extra shift up event, this fixes the problem of the same character twice in a row being displayed in df
XTestFakeKeyEvent (dpy, XKeysymToKeycode (dpy, ksTable[LEFT_SHIFT]), false, CurrentTime);
mykeysym = ksTable[command];
xkeycode = XKeysymToKeycode (dpy, mykeysym);
XTestFakeKeyEvent (dpy, xkeycode, true, CurrentTime);
XSync (dpy, true);
XTestFakeKeyEvent (dpy, xkeycode, false, CurrentTime);
XSync (dpy, true);
usleep(10000);
}
if (currAttr.map_state == IsUnmapped)
{
XLowerWindow (dpy, dfWin); // can't unmap, because you lose task bar
}
XSetInputFocus (dpy, currentFocus, currentRevert, CurrentTime);
XSync (dpy, false);
}
}
else
{
usleep (delay*1000 * count);
}
}
*/
}

@ -25,6 +25,63 @@ distribution.
#include "DFCommonInternal.h"
using namespace DFHack;
// should always reflect the enum in DFkeys.h
const static int ksTable[NUM_SPECIALS]=
{
VK_RETURN,
VK_SPACE,
VK_BACK,
VK_TAB,
VK_CAPITAL,
VK_LSHIFT,
VK_RSHIFT,
VK_LCONTROL,
VK_RCONTROL,
VK_MENU,
0, //XK_VoidSymbol, // WAIT
VK_ESCAPE,
VK_UP,
VK_DOWN,
VK_LEFT,
VK_RIGHT,
VK_F1,
VK_F2,
VK_F3,
VK_F4,
VK_F5,
VK_F6,
VK_F7,
VK_F8,
VK_F9,
VK_F10,
VK_F11,
VK_F12,
VK_PRIOR,
VK_NEXT,
VK_INSERT,
VK_DELETE,
VK_HOME,
VK_END,
VK_DIVIDE,
VK_MULTIPLY,
VK_SUBTRACT,
VK_ADD,
VK_RETURN,
VK_NUMPAD0,
VK_NUMPAD1,
VK_NUMPAD2,
VK_NUMPAD3,
VK_NUMPAD4,
VK_NUMPAD5,
VK_NUMPAD6,
VK_NUMPAD7,
VK_NUMPAD8,
VK_NUMPAD9,
VK_SEPARATOR
};
//Windows key handlers
struct window
{
@ -125,206 +182,7 @@ void API::TypeSpecial (t_special command, int count, int delay)
input[0].type = INPUT_KEYBOARD;
input[1].type = INPUT_KEYBOARD;
input[1].ki.dwFlags = KEYEVENTF_KEYUP;
switch (command)
{
case ENTER:
input[0].ki.wVk = VK_RETURN;
input[1].ki.wVk = VK_RETURN;
break;
case SPACE:
input[0].ki.wVk = VK_SPACE;
input[1].ki.wVk = VK_SPACE;
break;
case BACK_SPACE:
input[0].ki.wVk = VK_BACK;
input[1].ki.wVk = VK_BACK;
break;
case TAB:
input[0].ki.wVk = VK_TAB;
input[1].ki.wVk = VK_TAB;
break;
case CAPS_LOCK:
input[0].ki.wVk = VK_CAPITAL;
input[1].ki.wVk = VK_CAPITAL;
break;
// These are only for pressing the modifier key itself, you can't do key combinations with them, like ctrl+C
case LEFT_SHIFT: // I am not positive that this will work to distinguish the left and right..
input[0].ki.wVk = VK_LSHIFT;
input[1].ki.wVk = VK_LSHIFT;
break;
case RIGHT_SHIFT:
input[0].ki.wVk = VK_RSHIFT;
input[1].ki.wVk = VK_RSHIFT;
break;
case LEFT_CONTROL:
input[0].ki.wVk = VK_LCONTROL;
input[1].ki.wVk = VK_LCONTROL;
break;
case RIGHT_CONTROL:
input[0].ki.wVk = VK_RCONTROL;
input[1].ki.wVk = VK_RCONTROL;
break;
case ALT:
input[0].ki.wVk = VK_MENU;
input[1].ki.wVk = VK_MENU;
break;
case ESCAPE:
input[0].ki.wVk = VK_ESCAPE;
input[1].ki.wVk = VK_ESCAPE;
break;
case UP:
input[0].ki.wVk = VK_UP;
input[1].ki.wVk = VK_UP;
break;
case DOWN:
input[0].ki.wVk = VK_DOWN;
input[1].ki.wVk = VK_DOWN;
break;
case LEFT:
input[0].ki.wVk = VK_LEFT;
input[1].ki.wVk = VK_LEFT;
break;
case RIGHT:
input[0].ki.wVk = VK_RIGHT;
input[1].ki.wVk = VK_RIGHT;
break;
case F1:
input[0].ki.wVk = VK_F1;
input[1].ki.wVk = VK_F1;
break;
case F2:
input[0].ki.wVk = VK_F2;
input[1].ki.wVk = VK_F2;
break;
case F3:
input[0].ki.wVk = VK_F3;
input[1].ki.wVk = VK_F3;
break;
case F4:
input[0].ki.wVk = VK_F4;
input[1].ki.wVk = VK_F4;
break;
case F5:
input[0].ki.wVk = VK_F5;
input[1].ki.wVk = VK_F5;
break;
case F6:
input[0].ki.wVk = VK_F6;
input[1].ki.wVk = VK_F6;
break;
case F7:
input[0].ki.wVk = VK_F7;
input[1].ki.wVk = VK_F7;
break;
case F8:
input[0].ki.wVk = VK_F8;
input[1].ki.wVk = VK_F8;
break;
case F9:
input[0].ki.wVk = VK_F9;
input[1].ki.wVk = VK_F9;
break;
case F10:
input[0].ki.wVk = VK_F10;
input[1].ki.wVk = VK_F10;
break;
case F11:
input[0].ki.wVk = VK_F11;
input[1].ki.wVk = VK_F11;
break;
case F12:
input[0].ki.wVk = VK_F12;
input[1].ki.wVk = VK_F12;
break;
case PAGE_UP:
input[0].ki.wVk = VK_PRIOR;
input[1].ki.wVk = VK_PRIOR;
break;
case PAGE_DOWN:
input[0].ki.wVk = VK_NEXT;
input[1].ki.wVk = VK_NEXT;
break;
case INSERT:
input[0].ki.wVk = VK_INSERT;
input[1].ki.wVk = VK_INSERT;
break;
case KEY_DELETE:
input[0].ki.wVk = VK_DELETE;
input[1].ki.wVk = VK_DELETE;
break;
case HOME:
input[0].ki.wVk = VK_HOME;
input[1].ki.wVk = VK_HOME;
break;
case END:
input[0].ki.wVk = VK_END;
input[1].ki.wVk = VK_END;
break;
case KEYPAD_DIVIDE:
input[0].ki.wVk = VK_DIVIDE;
input[1].ki.wVk = VK_DIVIDE;
break;
case KEYPAD_MULTIPLY:
input[0].ki.wVk = VK_MULTIPLY;
input[1].ki.wVk = VK_MULTIPLY;
break;
case KEYPAD_SUBTRACT:
input[0].ki.wVk = VK_SUBTRACT;
input[1].ki.wVk = VK_SUBTRACT;
break;
case KEYPAD_ADD:
input[0].ki.wVk = VK_ADD;
input[1].ki.wVk = VK_ADD;
break;
case KEYPAD_ENTER:
input[0].ki.wVk = VK_RETURN;
input[1].ki.wVk = VK_RETURN;
break;
case KEYPAD_0:
input[0].ki.wVk = VK_NUMPAD0;
input[1].ki.wVk = VK_NUMPAD0;
break;
case KEYPAD_1:
input[0].ki.wVk = VK_NUMPAD1;
input[1].ki.wVk = VK_NUMPAD1;
break;
case KEYPAD_2:
input[0].ki.wVk = VK_NUMPAD2;
input[1].ki.wVk = VK_NUMPAD2;
break;
case KEYPAD_3:
input[0].ki.wVk = VK_NUMPAD3;
input[1].ki.wVk = VK_NUMPAD3;
break;
case KEYPAD_4:
input[0].ki.wVk = VK_NUMPAD4;
input[1].ki.wVk = VK_NUMPAD4;
break;
case KEYPAD_5:
input[0].ki.wVk = VK_NUMPAD5;
input[1].ki.wVk = VK_NUMPAD5;
break;
case KEYPAD_6:
input[0].ki.wVk = VK_NUMPAD6;
input[1].ki.wVk = VK_NUMPAD6;
break;
case KEYPAD_7:
input[0].ki.wVk = VK_NUMPAD7;
input[1].ki.wVk = VK_NUMPAD7;
break;
case KEYPAD_8:
input[0].ki.wVk = VK_NUMPAD8;
input[1].ki.wVk = VK_NUMPAD8;
break;
case KEYPAD_9:
input[0].ki.wVk = VK_NUMPAD9;
input[1].ki.wVk = VK_NUMPAD9;
break;
case KEYPAD_DECIMAL_POINT:
input[0].ki.wVk = VK_SEPARATOR;
input[1].ki.wVk = VK_SEPARATOR;
break;
}
input[0].ki.wVk = input[1].ki.wVk = ksTable[command];
for (int i = 0; i < count;i++)
{
SendInput (2, input, sizeof (input[0]));