Keyboard macros: Difference between revisions
Content added Content deleted
(Added Wren) |
|||
Line 1,885: | Line 1,885: | ||
Key_Delete("Ctrl-Shft-N")</lang> |
Key_Delete("Ctrl-Shft-N")</lang> |
||
=={{header|Wren}}== |
|||
{{trans|C}} |
|||
{{libheader|Xlib}} |
|||
<br> |
|||
As it's not currently possible for Wren-cli to access Xlib directly, we embed a Wren script in a C application to complete this task. |
|||
<lang ecmascript>/* keyboard_macros.wren */ |
|||
var GrabModeAsync = 1 |
|||
var Mod1Mask = 1 << 3 |
|||
var KeyPress = 2 |
|||
var XK_F6 = 0xffc3 |
|||
var XK_F7 = 0xffc4 |
|||
foreign class XEvent { |
|||
construct new() {} |
|||
foreign eventType |
|||
} |
|||
foreign class XDisplay { |
|||
construct openDisplay(displayName) {} |
|||
foreign defaultRootWindow() |
|||
foreign grabKey(keycode, modifiers, grabWindow, ownerEvents, pointerMode, keyboardMode) |
|||
foreign ungrabKey(keycode, modifiers, grabWindow) |
|||
foreign keysymToKeycode(keysym) |
|||
foreign closeDisplay() |
|||
foreign nextEvent(eventReturn) |
|||
} |
|||
class X { |
|||
foreign static stringToKeysym(string) |
|||
foreign static lookupKeysym(keyEvent, index) |
|||
} |
|||
var xd = XDisplay.openDisplay("") |
|||
if (xd == 0) { |
|||
System.print("Cannot open display.") |
|||
return |
|||
} |
|||
var drw = xd.defaultRootWindow() |
|||
xd.grabKey(xd.keysymToKeycode(X.stringToKeysym("F7")), Mod1Mask, drw, true, GrabModeAsync, GrabModeAsync) |
|||
xd.grabKey(xd.keysymToKeycode(X.stringToKeysym("F6")), Mod1Mask, drw, true, GrabModeAsync, GrabModeAsync) |
|||
var e = XEvent.new() |
|||
while (true) { |
|||
xd.nextEvent(e) |
|||
if (e.eventType == KeyPress) { |
|||
var s = X.lookupKeysym(e, 0) |
|||
if (s == XK_F7) { |
|||
System.print("something's happened.") |
|||
} else if (s == XK_F6) { |
|||
break |
|||
} |
|||
} |
|||
} |
|||
xd.ungrabKey(xd.keysymToKeycode(X.stringToKeysym("F7")), Mod1Mask, drw) |
|||
xd.ungrabKey(xd.keysymToKeycode(X.stringToKeysym("F6")), Mod1Mask, drw) |
|||
xd.closeDisplay()</lang> |
|||
<br> |
|||
We now embed this Wren script in the following C program, compile and run it. |
|||
<lang c>#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <X11/Xlib.h> |
|||
#include "wren.h" |
|||
/* C <=> Wren interface functions */ |
|||
void C_displayAllocate(WrenVM* vm) { |
|||
Display** pdisplay = (Display**)wrenSetSlotNewForeign(vm, 0, 0, sizeof(Display*)); |
|||
const char *displayName = wrenGetSlotString(vm, 1); |
|||
if (displayName == "") { |
|||
*pdisplay = XOpenDisplay(NULL); |
|||
} else { |
|||
*pdisplay = XOpenDisplay(displayName); |
|||
} |
|||
} |
|||
void C_eventAllocate(WrenVM* vm) { |
|||
wrenSetSlotNewForeign(vm, 0, 0, sizeof(XEvent)); |
|||
} |
|||
void C_eventType(WrenVM* vm) { |
|||
XEvent e = *(XEvent *)wrenGetSlotForeign(vm, 0); |
|||
wrenSetSlotDouble(vm, 0, (double)e.type); |
|||
} |
|||
void C_defaultRootWindow(WrenVM* vm) { |
|||
Display* display = *(Display**)wrenGetSlotForeign(vm, 0); |
|||
Window w = DefaultRootWindow(display); |
|||
wrenSetSlotDouble(vm, 0, (double)w); |
|||
} |
|||
void C_grabKey(WrenVM* vm) { |
|||
Display* display = *(Display**)wrenGetSlotForeign(vm, 0); |
|||
int keycode = (int)wrenGetSlotDouble(vm, 1); |
|||
unsigned int modifiers = (unsigned int)wrenGetSlotDouble(vm, 2); |
|||
Window w = (Window)wrenGetSlotDouble(vm, 3); |
|||
Bool owner_events = (Bool)wrenGetSlotBool(vm, 4); |
|||
int pointer_mode = (int)wrenGetSlotDouble(vm, 5); |
|||
int keyboard_mode = (int)wrenGetSlotDouble(vm, 6); |
|||
XGrabKey(display, keycode, modifiers, w, owner_events, pointer_mode, keyboard_mode); |
|||
} |
|||
void C_ungrabKey(WrenVM* vm) { |
|||
Display* display = *(Display**)wrenGetSlotForeign(vm, 0); |
|||
int keycode = (int)wrenGetSlotDouble(vm, 1); |
|||
unsigned int modifiers = (unsigned int)wrenGetSlotDouble(vm, 2); |
|||
Window w = (Window)wrenGetSlotDouble(vm, 3); |
|||
XUngrabKey(display, keycode, modifiers, w); |
|||
} |
|||
void C_keysymToKeycode(WrenVM* vm) { |
|||
Display* display = *(Display**)wrenGetSlotForeign(vm, 0); |
|||
KeySym k = (KeySym)wrenGetSlotDouble(vm, 1); |
|||
KeyCode code = XKeysymToKeycode(display, k); |
|||
wrenSetSlotDouble(vm, 0, (double)code); |
|||
} |
|||
void C_closeDisplay(WrenVM* vm) { |
|||
Display* display = *(Display**)wrenGetSlotForeign(vm, 0); |
|||
XCloseDisplay(display); |
|||
} |
|||
void C_nextEvent(WrenVM* vm) { |
|||
Display* display = *(Display**)wrenGetSlotForeign(vm, 0); |
|||
XEvent* pe = (XEvent*)wrenGetSlotForeign(vm, 1); |
|||
XNextEvent(display, pe); |
|||
} |
|||
void C_stringToKeysym(WrenVM* vm) { |
|||
char *string = (char *)wrenGetSlotString(vm, 1); |
|||
KeySym k = XStringToKeysym(string); |
|||
wrenSetSlotDouble(vm, 0, (double)k); |
|||
} |
|||
void C_lookupKeysym(WrenVM* vm) { |
|||
XKeyEvent *pke = (XKeyEvent*)wrenGetSlotForeign(vm, 1); |
|||
int index = (int)wrenGetSlotDouble(vm, 2); |
|||
KeySym k = XLookupKeysym(pke, index); |
|||
wrenSetSlotDouble(vm, 0, (double)k); |
|||
} |
|||
WrenForeignClassMethods bindForeignClass(WrenVM* vm, const char* module, const char* className) { |
|||
WrenForeignClassMethods methods; |
|||
methods.finalize = NULL; |
|||
if (strcmp(className, "XDisplay") == 0) { |
|||
methods.allocate = C_displayAllocate; |
|||
} else if (strcmp(className, "XEvent") == 0) { |
|||
methods.allocate = C_eventAllocate; |
|||
} else { |
|||
methods.allocate = NULL; |
|||
} |
|||
return methods; |
|||
} |
|||
WrenForeignMethodFn bindForeignMethod( |
|||
WrenVM* vm, |
|||
const char* module, |
|||
const char* className, |
|||
bool isStatic, |
|||
const char* signature) { |
|||
if (strcmp(module, "main") == 0) { |
|||
if (strcmp(className, "XEvent") == 0) { |
|||
if (!isStatic && strcmp(signature, "eventType") == 0) return C_eventType; |
|||
} else if (strcmp(className, "XDisplay") == 0) { |
|||
if (!isStatic && strcmp(signature, "defaultRootWindow()") == 0) return C_defaultRootWindow; |
|||
if (!isStatic && strcmp(signature, "grabKey(_,_,_,_,_,_)") == 0) return C_grabKey; |
|||
if (!isStatic && strcmp(signature, "ungrabKey(_,_,_)") == 0) return C_ungrabKey; |
|||
if (!isStatic && strcmp(signature, "keysymToKeycode(_)") == 0) return C_keysymToKeycode; |
|||
if (!isStatic && strcmp(signature, "closeDisplay()") == 0) return C_closeDisplay; |
|||
if (!isStatic && strcmp(signature, "nextEvent(_)") == 0) return C_nextEvent; |
|||
} else if (strcmp(className, "X") == 0) { |
|||
if (isStatic && strcmp(signature, "stringToKeysym(_)") == 0) return C_stringToKeysym; |
|||
if (isStatic && strcmp(signature, "lookupKeysym(_,_)") == 0) return C_lookupKeysym; |
|||
} |
|||
} |
|||
return NULL; |
|||
} |
|||
static void writeFn(WrenVM* vm, const char* text) { |
|||
printf("%s", text); |
|||
} |
|||
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) { |
|||
switch (errorType) { |
|||
case WREN_ERROR_COMPILE: |
|||
printf("[%s line %d] [Error] %s\n", module, line, msg); |
|||
break; |
|||
case WREN_ERROR_STACK_TRACE: |
|||
printf("[%s line %d] in %s\n", module, line, msg); |
|||
break; |
|||
case WREN_ERROR_RUNTIME: |
|||
printf("[Runtime Error] %s\n", msg); |
|||
break; |
|||
} |
|||
} |
|||
char *readFile(const char *fileName) { |
|||
FILE *f = fopen(fileName, "r"); |
|||
fseek(f, 0, SEEK_END); |
|||
long fsize = ftell(f); |
|||
rewind(f); |
|||
char *script = malloc(fsize + 1); |
|||
fread(script, 1, fsize, f); |
|||
fclose(f); |
|||
script[fsize] = 0; |
|||
return script; |
|||
} |
|||
int main(int argc, char **argv) { |
|||
WrenConfiguration config; |
|||
wrenInitConfiguration(&config); |
|||
config.writeFn = &writeFn; |
|||
config.errorFn = &errorFn; |
|||
config.bindForeignClassFn = &bindForeignClass; |
|||
config.bindForeignMethodFn = &bindForeignMethod; |
|||
WrenVM* vm = wrenNewVM(&config); |
|||
const char* module = "main"; |
|||
const char* fileName = "keyboard_macros.wren"; |
|||
char *script = readFile(fileName); |
|||
WrenInterpretResult result = wrenInterpret(vm, module, script); |
|||
switch (result) { |
|||
case WREN_RESULT_COMPILE_ERROR: |
|||
printf("Compile Error!\n"); |
|||
break; |
|||
case WREN_RESULT_RUNTIME_ERROR: |
|||
printf("Runtime Error!\n"); |
|||
break; |
|||
case WREN_RESULT_SUCCESS: |
|||
break; |
|||
} |
|||
wrenFreeVM(vm); |
|||
free(script); |
|||
return 0; |
|||
}</lang> |
|||
{{omit from|ACL2}} |
{{omit from|ACL2}} |