scx/c/xlib/key.c

128 lines
3.8 KiB
C

#include "xlib.h"
//#ifdef XLIB_RELEASE_5_OR_LATER
// I don't know if XDisplayKeycodes() was already there in X11R4.
// else: dpy->min_keycode dpy->max_keycode
s48_value scx_Display_Min_Keycode (s48_value d) {
int mink, maxk;
XDisplayKeycodes(SCX_EXTRACT_DISPLAY(d), &mink, &maxk);
return s48_enter_fixnum(mink);
}
s48_value scx_Display_Max_Keycode (s48_value d) {
int mink, maxk;
XDisplayKeycodes(SCX_EXTRACT_DISPLAY(d), &mink, &maxk);
return s48_enter_fixnum(maxk);
}
//#ifdef XLIB_RELEASE_5_OR_LATER
// I'm not sure if this works correctly in X11R4:
s48_value scx_Display_Keysyms_Per_Keycode (s48_value d) {
KeySym *ksyms;
int mink, maxk, ksyms_per_kode;
XDisplayKeycodes(SCX_EXTRACT_DISPLAY(d), &mink, &maxk);
ksyms = XGetKeyboardMapping(SCX_EXTRACT_DISPLAY(d), (KeyCode)mink,
maxk - mink + 1, &ksyms_per_kode);
XFree(ksyms);
return s48_enter_fixnum(ksyms_per_kode);
}
//#else
//static s48_value P_Display_Keysyms_Per_Keycode (d) s48_value d; {
// Check_Type (d, T_Display);
// // Force initialization:
// Disable_Interrupts;
// (void)XKeycodeToKeysym (DISPLAY(d)->dpy, DISPLAY(d)->dpy->min_keycode, 0);
// Enable_Interrupts;
// return s48_enter_fixnum (DISPLAY(d)->dpy->keysyms_per_keycode);
//}
//#endif
s48_value scx_String_To_Keysym (s48_value s) {
KeySym k = XStringToKeysym (s48_extract_string(s));
return k == NoSymbol ? S48_FALSE : s48_enter_integer ((unsigned long)k);
}
s48_value scx_Keysym_To_String (s48_value k) {
char* s = XKeysymToString ((KeySym)s48_extract_integer(k));
return s ? s48_enter_string(s) : S48_FALSE;
}
s48_value scx_Keycode_To_Keysym (s48_value d, s48_value k, s48_value index) {
KeySym ks;
//Disable_Interrupts;
ks = XKeycodeToKeysym(SCX_EXTRACT_DISPLAY(d),
(int)s48_extract_integer (k),
(int)s48_extract_integer (index));
//Enable_Interrupts;
if (ks == NoSymbol) return S48_FALSE;
else return s48_enter_integer((unsigned long)ks);
}
s48_value scx_Keysym_To_Keycode (s48_value d, s48_value k) {
KeyCode kc;
//Disable_Interrupts;
kc = XKeysymToKeycode (SCX_EXTRACT_DISPLAY(d),
(KeySym)s48_extract_integer(k));
//Enable_Interrupts;
return s48_enter_fixnum(kc);
}
s48_value scx_Lookup_String (s48_value d, s48_value k, s48_value mask) {
XKeyEvent e;
char buf[1024];
int len;
KeySym keysym_return;
XComposeStatus status_return;
e.display = SCX_EXTRACT_DISPLAY(d);
e.keycode = (int)s48_extract_integer(k);
e.state = s48_extract_integer(mask);
//Disable_Interrupts;
len = XLookupString (&e, buf, 1024, &keysym_return, &status_return);
//Enable_Interrupts;
return s48_enter_string(buf); //is there a 0 at buf[len] ?
}
s48_value scx_Rebind_Keysym (s48_value d, s48_value k, s48_value mods,
s48_value str) {
int i, n = S48_VECTOR_LENGTH(mods);
KeySym p[n];
for (i = 0; i < n; i++)
p[i] = (KeySym)s48_extract_integer(S48_VECTOR_REF(mods, i));
XRebindKeysym (SCX_EXTRACT_DISPLAY(d),
(KeySym)s48_extract_integer (k), p, n,
(unsigned char *)s48_extract_string(str),
S48_STRING_LENGTH(str));
return S48_UNSPECIFIC;
}
s48_value scx_Refresh_Keyboard_Mapping (s48_value d, s48_value w,
s48_value event) {
static XMappingEvent fake;
fake.type = MappingNotify;
fake.display = SCX_EXTRACT_DISPLAY(d);
fake.window = SCX_EXTRACT_WINDOW(w);
fake.request = s48_extract_integer(event);
XRefreshKeyboardMapping (&fake);
return S48_UNSPECIFIC;
}
scx_init_key () {
S48_EXPORT_FUNCTION(scx_Display_Min_Keycode);
S48_EXPORT_FUNCTION(scx_Display_Max_Keycode);
S48_EXPORT_FUNCTION(scx_Display_Keysyms_Per_Keycode);
S48_EXPORT_FUNCTION(scx_String_To_Keysym);
S48_EXPORT_FUNCTION(scx_Keysym_To_String);
S48_EXPORT_FUNCTION(scx_Keycode_To_Keysym);
S48_EXPORT_FUNCTION(scx_Keysym_To_Keycode);
S48_EXPORT_FUNCTION(scx_Lookup_String);
S48_EXPORT_FUNCTION(scx_Rebind_Keysym);
S48_EXPORT_FUNCTION(scx_Refresh_Keyboard_Mapping);
}