robotgo/event/goEvent.h

252 lines
6.9 KiB
C
Raw Permalink Normal View History

2016-11-10 06:34:13 +00:00
// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
2016-11-26 16:12:38 +00:00
// https://github.com/go-vgo/robotgo/blob/master/LICENSE
2016-11-10 06:34:13 +00:00
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2016-10-30 13:22:06 +00:00
#ifdef HAVE_CONFIG_H
2016-12-17 08:50:57 +00:00
#include <config.h>
2016-10-30 13:22:06 +00:00
#endif
2017-11-13 15:30:00 +00:00
#include "pub.h"
2016-10-30 13:22:06 +00:00
2016-12-17 08:47:03 +00:00
void dispatch_proc(iohook_event * const event) {
2016-10-30 13:22:06 +00:00
char buffer[256] = { 0 };
size_t length = snprintf(buffer, sizeof(buffer),
"id=%i,when=%" PRIu64 ",mask=0x%X",
event->type, event->time, event->mask);
switch (event->type) {
case EVENT_KEY_PRESSED:
// If the escape key is pressed, naturally terminate the program.
if (event->data.keyboard.keycode == VC_ESCAPE) {
2018-01-27 10:36:16 +00:00
// int status = hook_stop();
// switch (status) {
// // System level errors.
// case IOHOOK_ERROR_OUT_OF_MEMORY:
// loggerProc(LOG_LEVEL_ERROR, "Failed to allocate memory. (%#X)", status);
// break;
2016-10-30 13:22:06 +00:00
2018-01-27 10:36:16 +00:00
// case IOHOOK_ERROR_X_RECORD_GET_CONTEXT:
// // NOTE This is the only platform specific error that occurs on hook_stop().
// loggerProc(LOG_LEVEL_ERROR, "Failed to get XRecord context. (%#X)", status);
// break;
2016-10-30 13:22:06 +00:00
2018-01-27 10:36:16 +00:00
// // Default error.
// case IOHOOK_FAILURE:
// default:
// loggerProc(LOG_LEVEL_ERROR, "An unknown hook error occurred. (%#X)", status);
// break;
// }
2016-10-30 13:22:06 +00:00
}
case EVENT_KEY_RELEASED:
snprintf(buffer + length, sizeof(buffer) - length,
",keycode=%u,rawcode=0x%X",
event->data.keyboard.keycode, event->data.keyboard.rawcode);
2017-12-29 12:24:47 +00:00
int akeyCode = (uint16_t) event->data.keyboard.keycode;
2018-01-27 10:36:16 +00:00
if (event->data.keyboard.keycode == VC_ESCAPE
&& atoi(cevent) == 11) {
int stopEvent = stop_event();
// printf("stop_event%d\n", stopEvent);
cstatus = 0;
}
2017-01-02 11:01:10 +00:00
// printf("atoi(str)---%d\n", atoi(cevent));
2018-07-07 15:01:55 +00:00
if (akeyCode == atoi(cevent)) {
2017-12-29 12:18:28 +00:00
int stopEvent = stop_event();
// printf("%d\n", stopEvent);
2017-05-09 13:56:22 +00:00
cstatus = 0;
2017-01-02 11:01:10 +00:00
}
2016-10-30 13:22:06 +00:00
break;
case EVENT_KEY_TYPED:
snprintf(buffer + length, sizeof(buffer) - length,
",keychar=%lc,rawcode=%u",
2017-01-24 15:56:57 +00:00
(uint16_t) event->data.keyboard.keychar,
2016-10-30 13:22:06 +00:00
event->data.keyboard.rawcode);
#ifdef WE_REALLY_WANT_A_POINTER
char *buf = malloc (6);
#else
char buf[6];
#endif
2017-05-10 13:04:55 +00:00
sprintf(buf, "%lc", (uint16_t) event->data.keyboard.keychar);
2016-10-30 13:22:06 +00:00
#ifdef WE_REALLY_WANT_A_POINTER
free (buf);
#endif
2018-07-07 15:01:55 +00:00
if (strcmp(buf, cevent) == 0) {
2017-12-29 12:18:28 +00:00
int stopEvent = stop_event();
// printf("%d\n", stopEvent);
2017-05-09 13:56:22 +00:00
cstatus = 0;
2016-10-30 13:22:06 +00:00
}
// return (char*) event->data.keyboard.keychar;
break;
case EVENT_MOUSE_PRESSED:
case EVENT_MOUSE_RELEASED:
case EVENT_MOUSE_CLICKED:
case EVENT_MOUSE_MOVED:
case EVENT_MOUSE_DRAGGED:
snprintf(buffer + length, sizeof(buffer) - length,
",x=%i,y=%i,button=%i,clicks=%i",
event->data.mouse.x, event->data.mouse.y,
event->data.mouse.button, event->data.mouse.clicks);
2017-05-09 13:56:22 +00:00
int abutton = event->data.mouse.button;
int aclicks = event->data.mouse.clicks;
int amouse = -1;
2018-07-07 15:01:55 +00:00
if (strcmp(cevent, "mleft") == 0) {
2017-05-09 13:56:22 +00:00
amouse = 1;
2016-10-30 13:22:06 +00:00
}
2018-07-07 15:01:55 +00:00
if (strcmp(cevent, "mright") == 0) {
2017-05-09 13:56:22 +00:00
amouse = 2;
2016-10-30 13:22:06 +00:00
}
2018-07-07 15:01:55 +00:00
if (strcmp(cevent, "wheelDown") == 0) {
2017-05-09 13:56:22 +00:00
amouse = 4;
2016-10-30 13:22:06 +00:00
}
2018-07-07 15:01:55 +00:00
if (strcmp(cevent, "wheelUp") == 0) {
2017-05-09 13:56:22 +00:00
amouse = 5;
2016-10-30 13:22:06 +00:00
}
2018-07-07 15:01:55 +00:00
if (strcmp(cevent, "wheelLeft") == 0) {
2017-05-09 13:56:22 +00:00
amouse = 6;
2016-10-30 13:22:06 +00:00
}
2018-07-07 15:01:55 +00:00
if (strcmp(cevent, "wheelRight") == 0) {
2017-05-09 13:56:22 +00:00
amouse = 7;
2016-10-30 13:22:06 +00:00
}
2018-07-07 15:01:55 +00:00
if (abutton == amouse && aclicks == 1) {
2017-12-29 12:18:28 +00:00
int stopEvent = stop_event();
2017-05-09 13:56:22 +00:00
cstatus = 0;
2016-10-30 13:22:06 +00:00
}
break;
case EVENT_MOUSE_WHEEL:
snprintf(buffer + length, sizeof(buffer) - length,
",type=%i,amount=%i,rotation=%i",
event->data.wheel.type, event->data.wheel.amount,
event->data.wheel.rotation);
break;
default:
break;
}
// fprintf(stdout, "----%s\n", buffer);
}
2018-09-04 18:02:27 +00:00
int add_event(char *key_event) {
2016-10-30 13:22:06 +00:00
// (uint16_t *)
2018-09-04 18:02:27 +00:00
cevent = key_event;
2016-10-30 13:22:06 +00:00
// Set the logger callback for library output.
2017-01-19 16:35:11 +00:00
hookSetlogger(&loggerProc);
2016-10-30 13:22:06 +00:00
2016-12-17 08:47:03 +00:00
// Set the event callback for IOhook events.
2016-10-30 13:22:06 +00:00
hook_set_dispatch_proc(&dispatch_proc);
// Start the hook and block.
// NOTE If EVENT_HOOK_ENABLED was delivered, the status will always succeed.
int status = hook_run();
switch (status) {
2016-12-17 08:47:03 +00:00
case IOHOOK_SUCCESS:
2016-10-30 13:22:06 +00:00
// Everything is ok.
break;
// System level errors.
2016-12-17 08:47:03 +00:00
case IOHOOK_ERROR_OUT_OF_MEMORY:
2017-01-19 16:35:11 +00:00
loggerProc(LOG_LEVEL_ERROR, "Failed to allocate memory. (%#X)", status);
2016-10-30 13:22:06 +00:00
break;
// X11 specific errors.
2016-12-17 08:47:03 +00:00
case IOHOOK_ERROR_X_OPEN_DISPLAY:
2017-01-19 16:35:11 +00:00
loggerProc(LOG_LEVEL_ERROR, "Failed to open X11 display. (%#X)", status);
2016-10-30 13:22:06 +00:00
break;
2016-12-17 08:47:03 +00:00
case IOHOOK_ERROR_X_RECORD_NOT_FOUND:
2017-01-19 16:35:11 +00:00
loggerProc(LOG_LEVEL_ERROR, "Unable to locate XRecord extension. (%#X)", status);
2016-10-30 13:22:06 +00:00
break;
2016-12-17 08:47:03 +00:00
case IOHOOK_ERROR_X_RECORD_ALLOC_RANGE:
2017-01-19 16:35:11 +00:00
loggerProc(LOG_LEVEL_ERROR, "Unable to allocate XRecord range. (%#X)", status);
2016-10-30 13:22:06 +00:00
break;
2016-12-17 08:47:03 +00:00
case IOHOOK_ERROR_X_RECORD_CREATE_CONTEXT:
2017-01-19 16:35:11 +00:00
loggerProc(LOG_LEVEL_ERROR, "Unable to allocate XRecord context. (%#X)", status);
2016-10-30 13:22:06 +00:00
break;
2016-12-17 08:47:03 +00:00
case IOHOOK_ERROR_X_RECORD_ENABLE_CONTEXT:
2017-01-19 16:35:11 +00:00
loggerProc(LOG_LEVEL_ERROR, "Failed to enable XRecord context. (%#X)", status);
2016-10-30 13:22:06 +00:00
break;
// Windows specific errors.
2016-12-17 08:47:03 +00:00
case IOHOOK_ERROR_SET_WINDOWS_HOOK_EX:
2017-01-19 16:35:11 +00:00
loggerProc(LOG_LEVEL_ERROR, "Failed to register low level windows hook. (%#X)", status);
2016-10-30 13:22:06 +00:00
break;
// Darwin specific errors.
2016-12-17 08:47:03 +00:00
case IOHOOK_ERROR_AXAPI_DISABLED:
2017-01-19 16:35:11 +00:00
loggerProc(LOG_LEVEL_ERROR, "Failed to enable access for assistive devices. (%#X)", status);
2016-10-30 13:22:06 +00:00
break;
2016-12-17 08:47:03 +00:00
case IOHOOK_ERROR_CREATE_EVENT_PORT:
2017-01-19 16:35:11 +00:00
loggerProc(LOG_LEVEL_ERROR, "Failed to create apple event port. (%#X)", status);
2016-10-30 13:22:06 +00:00
break;
2016-12-17 08:47:03 +00:00
case IOHOOK_ERROR_CREATE_RUN_LOOP_SOURCE:
2017-01-19 16:35:11 +00:00
loggerProc(LOG_LEVEL_ERROR, "Failed to create apple run loop source. (%#X)", status);
2016-10-30 13:22:06 +00:00
break;
2016-12-17 08:47:03 +00:00
case IOHOOK_ERROR_GET_RUNLOOP:
2017-01-19 16:35:11 +00:00
loggerProc(LOG_LEVEL_ERROR, "Failed to acquire apple run loop. (%#X)", status);
2016-10-30 13:22:06 +00:00
break;
2016-12-17 08:47:03 +00:00
case IOHOOK_ERROR_CREATE_OBSERVER:
2017-01-19 16:35:11 +00:00
loggerProc(LOG_LEVEL_ERROR, "Failed to create apple run loop observer. (%#X)", status);
2016-10-30 13:22:06 +00:00
break;
// Default error.
2016-12-17 08:47:03 +00:00
case IOHOOK_FAILURE:
2016-10-30 13:22:06 +00:00
default:
2017-01-19 16:35:11 +00:00
loggerProc(LOG_LEVEL_ERROR, "An unknown hook error occurred. (%#X)", status);
2016-10-30 13:22:06 +00:00
break;
}
// return status;
2017-11-23 15:44:41 +00:00
// printf("%d\n", status);
2016-10-30 13:22:06 +00:00
return cstatus;
}
2017-12-29 12:18:28 +00:00
int stop_event(){
2016-10-30 13:22:06 +00:00
int status = hook_stop();
switch (status) {
// System level errors.
2016-12-17 08:47:03 +00:00
case IOHOOK_ERROR_OUT_OF_MEMORY:
2017-01-19 16:35:11 +00:00
loggerProc(LOG_LEVEL_ERROR, "Failed to allocate memory. (%#X)", status);
2016-10-30 13:22:06 +00:00
break;
2016-12-17 08:47:03 +00:00
case IOHOOK_ERROR_X_RECORD_GET_CONTEXT:
2016-10-30 13:22:06 +00:00
// NOTE This is the only platform specific error that occurs on hook_stop().
2017-01-19 16:35:11 +00:00
loggerProc(LOG_LEVEL_ERROR, "Failed to get XRecord context. (%#X)", status);
2016-10-30 13:22:06 +00:00
break;
// Default error.
2016-12-17 08:47:03 +00:00
case IOHOOK_FAILURE:
2016-10-30 13:22:06 +00:00
default:
2017-01-19 16:35:11 +00:00
// loggerProc(LOG_LEVEL_ERROR, "An unknown hook error occurred. (%#X)", status);
2016-10-30 13:22:06 +00:00
break;
}
return status;
}