Skip to content
Browse files

implemented keyboard events, optional pointer events possible

  • Loading branch information...
1 parent 2df8b19 commit e579ef8e4eeeb7de345f7fe77e5b06b1d277276a @hwhw committed Mar 5, 2012
Showing with 195 additions and 19 deletions.
  1. +141 −2 config.lua
  2. +54 −17 kindlevncviewer.c
View
143 config.lua
@@ -1,5 +1,144 @@
-print("Hello World.")
+require "keys"
+require "rfbkeys"
+-- comment out the following line on a KDX
+set_k3_keycodes()
+
+
+-- variables client_width and client_height will be available when handleInput() is called
+client_width = 0
+client_height = 0
+
+--[[
+ you have also the following API functions:
+
+ Quit( [status] )
+ will quit the application. optional: return code (must be positive)
+
+ SendKeyEvent( keyCode, pressed )
+ sends a key event to the rfb server. "pressed" is a bool value
+ telling whether the key was pressed (true) or released (false)
+
+ SendPointerEvent( x, y, buttonMask )
+ sends a pointer event to the rfb server
+]]--
+
+
+-- globals for remembering key state
+shift = false
+sym = false
+
+-- this handler will be called upon key presses (input events, actually)
function handleInput(channel, itype, code, value)
- print("input:", channel, itype, code, value)
+ --print("input:", channel, itype, code, value)
+ if itype == EV_KEY then
+ local pressed = false
+ if value == EVENT_VALUE_KEY_PRESS then
+ pressed = true
+ elseif value == EVENT_VALUE_KEY_RELEASE then
+ pressed = false
+ else
+ return -- we don't know how to handle this.
+ end
+
+ -- will toggle state
+ if code == KEY_SYM then sym = pressed
+ elseif code == KEY_SHIFT then shift = pressed
+
+ -- number keys, not present on K3
+ elseif code == KEY_1 then SendKeyEvent(XK_1, pressed)
+ elseif code == KEY_2 then SendKeyEvent(XK_2, pressed)
+ elseif code == KEY_3 then SendKeyEvent(XK_3, pressed)
+ elseif code == KEY_4 then SendKeyEvent(XK_4, pressed)
+ elseif code == KEY_5 then SendKeyEvent(XK_5, pressed)
+ elseif code == KEY_6 then SendKeyEvent(XK_6, pressed)
+ elseif code == KEY_7 then SendKeyEvent(XK_7, pressed)
+ elseif code == KEY_8 then SendKeyEvent(XK_8, pressed)
+ elseif code == KEY_9 then SendKeyEvent(XK_9, pressed)
+ elseif code == KEY_0 then SendKeyEvent(XK_0, pressed)
+
+ -- letter keys
+ elseif not shift and code == KEY_Q then SendKeyEvent(XK_q, pressed)
+ elseif not shift and code == KEY_W then SendKeyEvent(XK_w, pressed)
+ elseif not shift and code == KEY_E then SendKeyEvent(XK_e, pressed)
+ elseif not shift and code == KEY_R then SendKeyEvent(XK_r, pressed)
+ elseif not shift and code == KEY_T then SendKeyEvent(XK_t, pressed)
+ elseif not shift and code == KEY_Y then SendKeyEvent(XK_y, pressed)
+ elseif not shift and code == KEY_U then SendKeyEvent(XK_u, pressed)
+ elseif not shift and code == KEY_I then SendKeyEvent(XK_i, pressed)
+ elseif not shift and code == KEY_O then SendKeyEvent(XK_o, pressed)
+ elseif not shift and code == KEY_P then SendKeyEvent(XK_p, pressed)
+ elseif not shift and code == KEY_A then SendKeyEvent(XK_a, pressed)
+ elseif not shift and code == KEY_S then SendKeyEvent(XK_s, pressed)
+ elseif not shift and code == KEY_D then SendKeyEvent(XK_d, pressed)
+ elseif not shift and code == KEY_F then SendKeyEvent(XK_f, pressed)
+ elseif not shift and code == KEY_G then SendKeyEvent(XK_g, pressed)
+ elseif not shift and code == KEY_H then SendKeyEvent(XK_h, pressed)
+ elseif not shift and code == KEY_J then SendKeyEvent(XK_j, pressed)
+ elseif not shift and code == KEY_K then SendKeyEvent(XK_k, pressed)
+ elseif not shift and code == KEY_L then SendKeyEvent(XK_l, pressed)
+ elseif not shift and code == KEY_Z then SendKeyEvent(XK_z, pressed)
+ elseif not shift and code == KEY_X then SendKeyEvent(XK_x, pressed)
+ elseif not shift and code == KEY_C then SendKeyEvent(XK_c, pressed)
+ elseif not shift and code == KEY_V then SendKeyEvent(XK_v, pressed)
+ elseif not shift and code == KEY_B then SendKeyEvent(XK_b, pressed)
+ elseif not shift and code == KEY_N then SendKeyEvent(XK_n, pressed)
+ elseif not shift and code == KEY_M then SendKeyEvent(XK_m, pressed)
+ elseif shift and code == KEY_Q then SendKeyEvent(XK_Q, pressed)
+ elseif shift and code == KEY_W then SendKeyEvent(XK_W, pressed)
+ elseif shift and code == KEY_E then SendKeyEvent(XK_E, pressed)
+ elseif shift and code == KEY_R then SendKeyEvent(XK_R, pressed)
+ elseif shift and code == KEY_T then SendKeyEvent(XK_T, pressed)
+ elseif shift and code == KEY_Y then SendKeyEvent(XK_Y, pressed)
+ elseif shift and code == KEY_U then SendKeyEvent(XK_U, pressed)
+ elseif shift and code == KEY_I then SendKeyEvent(XK_I, pressed)
+ elseif shift and code == KEY_O then SendKeyEvent(XK_O, pressed)
+ elseif shift and code == KEY_P then SendKeyEvent(XK_P, pressed)
+ elseif shift and code == KEY_A then SendKeyEvent(XK_A, pressed)
+ elseif shift and code == KEY_S then SendKeyEvent(XK_S, pressed)
+ elseif shift and code == KEY_D then SendKeyEvent(XK_D, pressed)
+ elseif shift and code == KEY_F then SendKeyEvent(XK_F, pressed)
+ elseif shift and code == KEY_G then SendKeyEvent(XK_G, pressed)
+ elseif shift and code == KEY_H then SendKeyEvent(XK_H, pressed)
+ elseif shift and code == KEY_J then SendKeyEvent(XK_J, pressed)
+ elseif shift and code == KEY_K then SendKeyEvent(XK_K, pressed)
+ elseif shift and code == KEY_L then SendKeyEvent(XK_L, pressed)
+ elseif shift and code == KEY_Z then SendKeyEvent(XK_Z, pressed)
+ elseif shift and code == KEY_X then SendKeyEvent(XK_X, pressed)
+ elseif shift and code == KEY_C then SendKeyEvent(XK_C, pressed)
+ elseif shift and code == KEY_V then SendKeyEvent(XK_V, pressed)
+ elseif shift and code == KEY_B then SendKeyEvent(XK_B, pressed)
+ elseif shift and code == KEY_N then SendKeyEvent(XK_N, pressed)
+ elseif shift and code == KEY_M then SendKeyEvent(XK_M, pressed)
+
+ -- other keys
+ elseif not shift and code == KEY_DEL then SendKeyEvent(XK_Delete, pressed)
+ elseif shift and code == KEY_DEL then SendKeyEvent(XK_BackSpace, pressed)
+ elseif code == KEY_DOT then SendKeyEvent(XK_period, pressed)
+ elseif code == KEY_SLASH then SendKeyEvent(XK_slash, pressed)
+ elseif code == KEY_ENTER then SendKeyEvent(XK_Return, pressed)
+ elseif code == KEY_SPACE then SendKeyEvent(XK_space, pressed)
+
+ elseif code == KEY_ALT then SendKeyEvent(XK_Alt_L, pressed)
+ elseif code == KEY_AA then SendKeyEvent(XK_Control_L, pressed)
+
+ -- special keys
+ --elseif code == KEY_VPLUS then SendKeyEvent(XK_, pressed)
+ --elseif code == KEY_VMINUS then SendKeyEvent(XK_, pressed)
+ elseif code == KEY_HOME then
+ Quit()
+ elseif code == KEY_PGBCK then SendKeyEvent(XK_Escape, pressed)
+ elseif code == KEY_PGFWD then SendKeyEvent(XK_Tab, pressed)
+ -- the following two exist only on K3
+ elseif code == KEY_LPGBCK then SendKeyEvent(XK_Escape, pressed)
+ elseif code == KEY_LPGFWD then SendKeyEvent(XK_Tab, pressed)
+ --elseif code == KEY_MENU then SendKeyEvent(XK_, pressed)
+ --elseif code == KEY_BACK then SendKeyEvent(XK_, pressed)
+ elseif code == KEY_FW_LEFT then SendKeyEvent(XK_Left, pressed)
+ elseif code == KEY_FW_RIGHT then SendKeyEvent(XK_Right, pressed)
+ elseif code == KEY_FW_UP then SendKeyEvent(XK_Up, pressed)
+ elseif code == KEY_FW_DOWN then SendKeyEvent(XK_Down, pressed)
+ elseif code == KEY_FW_PRESS then SendKeyEvent(XK_Return, pressed)
+ end
+ end
end
View
71 kindlevncviewer.c
@@ -38,14 +38,15 @@ char *password = NULL;
char *config = NULL;
lua_State *L = NULL;
+rfbClient* client;
#define DELAY_REFRESH_BY_USECS 150000 // 150 msec
#define FORCE_PARTIAL_REFRESH_FOR_X_256TH_PXUP 512
#define DO_FULL_UPDATES 0
#define FULL_REFRESH_FOR_X_256TH_PXUP 256
#define ENDLESS_RECONNECT 1
-char* passwordCallback(rfbClient* client) {
+char* passwordCallback() {
if(password == NULL) {
running = -1;
fprintf(stderr,"got request for password, but no password was given on command line.\n");
@@ -54,7 +55,7 @@ char* passwordCallback(rfbClient* client) {
return strdup(password);
}
-void rfb16ToFramebuffer4(rfbClient* client, int x, int y, int w, int h) {
+void rfb16ToFramebuffer4(int x, int y, int w, int h) {
int cx, cy;
int len = (w + (x & 1)) >> 1;
@@ -139,7 +140,7 @@ void updateFromRFB(rfbClient* client, int x, int y, int w, int h) {
int cy = (y > vinfo.yres) ? vinfo.yres - 1 : y;
int cw = (x+w > vinfo.xres) ? vinfo.xres - (x+1) : w;
int ch = (y+h > vinfo.yres) ? vinfo.yres - (y+1) : h;
- rfb16ToFramebuffer4(client, cx, cy, cw, ch);
+ rfb16ToFramebuffer4(cx, cy, cw, ch);
if(rx1 > cx) rx1 = cx;
if(rx2 < cx+cw-1) rx2 = cx+cw-1;
if(ry1 > cy) ry1 = cy;
@@ -169,8 +170,32 @@ void handleInput(int chan, int fd) {
}
}
+int luaSendKeyEvent(lua_State *L) {
+ int key = luaL_checkint(L, 1);
+ int pressed = lua_toboolean(L, 2);
+
+ SendKeyEvent(client, (uint32_t) key, pressed);
+
+ return 0;
+}
+
+int luaSendPointerEvent(lua_State *L) {
+ int x = luaL_checkint(L, 1);
+ int y = luaL_checkint(L, 2);
+ int buttonMask = luaL_checkint(L, 3);
+
+ SendPointerEvent(client, x, y, buttonMask);
+
+ return 0;
+}
+
+int luaQuit(lua_State *L) {
+ running = 0 - luaL_optint(L, 1, 0);
+ return 0;
+}
+
/* adapted from libVNCclient, extended by input device file descriptors: */
-int myWaitForMessage(rfbClient* client, unsigned int usecs)
+int myWaitForMessage(unsigned int usecs)
{
fd_set fds;
struct timeval timeout;
@@ -237,7 +262,6 @@ void closeInputDevices() {
}
int main(int argc, char **argv) {
- rfbClient* cl;
int refresh_partial_force_at;
int refresh_full_at;
int i;
@@ -326,6 +350,12 @@ int main(int argc, char **argv) {
L = lua_open();
if(L) {
luaL_openlibs(L);
+ lua_pushcfunction(L, luaSendKeyEvent);
+ lua_setglobal(L, "SendKeyEvent");
+ lua_pushcfunction(L, luaSendPointerEvent);
+ lua_setglobal(L, "SendPointerEvent");
+ lua_pushcfunction(L, luaQuit);
+ lua_setglobal(L, "Quit");
if(luaL_dofile(L, config)) {
fprintf(stderr, "lua config error: %s", lua_tostring(L, -1));
lua_close(L);
@@ -339,18 +369,25 @@ int main(int argc, char **argv) {
while(running > 0) {
/* initialize rfbClient */
- cl = rfbGetClient(5,3,2); // 16bpp
- cl->GetPassword = passwordCallback;
- cl->canHandleNewFBSize = FALSE;
- cl->GotFrameBufferUpdate = updateFromRFB;
- cl->listenPort = LISTEN_PORT_OFFSET;
+ client = rfbGetClient(5,3,2); // 16bpp
+ client->GetPassword = passwordCallback;
+ client->canHandleNewFBSize = FALSE;
+ client->GotFrameBufferUpdate = updateFromRFB;
+ client->listenPort = LISTEN_PORT_OFFSET;
/* connect */
- if (!rfbInitClient(cl,&argc,argv)) {
+ if (!rfbInitClient(client,&argc,argv)) {
goto quit;
}
- refresh_full_at = ((cl->width*cl->height) >> 8) * FULL_REFRESH_FOR_X_256TH_PXUP;
- refresh_partial_force_at = ((cl->width*cl->height) >> 8) * FORCE_PARTIAL_REFRESH_FOR_X_256TH_PXUP;
+ refresh_full_at = ((client->width*client->height) >> 8) * FULL_REFRESH_FOR_X_256TH_PXUP;
+ refresh_partial_force_at = ((client->width*client->height) >> 8) * FORCE_PARTIAL_REFRESH_FOR_X_256TH_PXUP;
+
+ if(L) {
+ lua_pushinteger(L, client->width);
+ lua_setglobal(L, "client_width");
+ lua_pushinteger(L, client->height);
+ lua_setglobal(L, "client_height");
+ }
while (running > 0) {
int n;
@@ -360,19 +397,19 @@ int main(int argc, char **argv) {
rx2 = 0;
ry2 = 0;
#define LONGLOOP 5*60*1000*1000
- n = myWaitForMessage(cl,LONGLOOP);
+ n = myWaitForMessage(LONGLOOP);
if (n<0) {
fprintf(stderr,"error while waiting for RFB message.\n");
if(ENDLESS_RECONNECT) goto reconnect;
goto quit;
}
while(n > 0) {
- if(!HandleRFBServerMessage(cl)) {
+ if(!HandleRFBServerMessage(client)) {
fprintf(stderr,"error while handling RFB message.\n");
if(ENDLESS_RECONNECT) goto reconnect;
goto quit;
}
- n = myWaitForMessage(cl,DELAY_REFRESH_BY_USECS);
+ n = myWaitForMessage(DELAY_REFRESH_BY_USECS);
if (n<0) {
fprintf(stderr,"error while waiting for RFB message.\n");
if(ENDLESS_RECONNECT) goto reconnect;
@@ -391,7 +428,7 @@ int main(int argc, char **argv) {
}
}
reconnect:
- rfbClientCleanup(cl);
+ rfbClientCleanup(client);
}
quit:
closeInputDevices();

0 comments on commit e579ef8

Please sign in to comment.
Something went wrong with that request. Please try again.