Archived
2
0

with comments for if stucts are needed later

This commit is contained in:
mbattista 2021-04-10 23:17:05 +00:00
parent be3453c37d
commit 79e3e153bd
3 changed files with 69 additions and 48 deletions

View File

@ -9,14 +9,14 @@ static int DIRTY = 0;
struct linked_list struct linked_list
{ {
unsigned long number; unsigned long number;
int keycode; KeyCode keycode;
struct linked_list *next; struct linked_list *next;
}; };
typedef struct linked_list node; typedef struct linked_list node;
node *head=NULL, *last=NULL; node *head = NULL, *last = NULL;
void insertAtLast(unsigned long value, int keycode) { void insertAtLast(unsigned long value, KeyCode keycode) {
node *temp_node; node *temp_node;
temp_node = (node *) malloc(sizeof(node)); temp_node = (node *) malloc(sizeof(node));
@ -25,7 +25,7 @@ void insertAtLast(unsigned long value, int keycode) {
temp_node->next = NULL; temp_node->next = NULL;
//For the 1st element //For the 1st element
if(head == NULL) { if(!head) {
head = temp_node; head = temp_node;
last = temp_node; last = temp_node;
} else { } else {
@ -34,17 +34,17 @@ void insertAtLast(unsigned long value, int keycode) {
} }
} }
void deleteItem(int value) { void deleteItem(unsigned long value) {
node *myNode = head, *previous=NULL; node *myNode = head, *previous = NULL;
while(myNode!=NULL) { while(myNode) {
if(myNode->number==value) { if(myNode->number == value) {
if(previous==NULL) if(!previous)
head = myNode->next; head = myNode->next;
else else
previous->next = myNode->next; previous->next = myNode->next;
free(myNode); //need to free up the memory to prevent memory leak free(myNode);
break; break;
} }
@ -53,20 +53,18 @@ void deleteItem(int value) {
} }
} }
int searchItem(int value) { node *searchItemNode(unsigned long value) {
node *searchNode = head; node *searchNode = head;
int flag = 0;
while(searchNode != NULL) { while(searchNode) {
if(searchNode->number==value) { if(searchNode->number == value) {
flag = searchNode->keycode;
break; break;
} else { } else {
searchNode = searchNode->next; searchNode = searchNode->next;
} }
} }
return flag; return searchNode;
} }
Display *getXDisplay(void) { Display *getXDisplay(void) {
@ -189,21 +187,18 @@ KeyCode XkbKeysymToKeycode(KeySym keysym) {
return keycode; return keycode;
} }
void XKey(unsigned long key, int down) { int XKey(unsigned long key, int down) {
if (key != 0) {
Display *display = getXDisplay(); Display *display = getXDisplay();
// KeyCode code = XKeysymToKeycode(display, key); KeyCode code = -2;
// node *compareNode;
int code; // compareNode = searchItemNode(key);
if (down) { if (down) {
code = searchItem(key); // if (compareNode && compareNode != last) {
if (code == 0) { // code = compareNode->keycode;
// XKeysymToKeycode() doesn't respect state, so we have to use // } else {
// something slightly more complex
code = XkbKeysymToKeycode(key); code = XkbKeysymToKeycode(key);
// Map non-existing keysyms to new keycodes if (!code) {
if(code == 0) {
int min, max, numcodes; int min, max, numcodes;
XDisplayKeycodes(display, &min, &max); XDisplayKeycodes(display, &min, &max);
XGetKeyboardMapping(display, min, max-min, &numcodes); XGetKeyboardMapping(display, min, max-min, &numcodes);
@ -213,17 +208,37 @@ void XKey(unsigned long key, int down) {
for(int i=0;i<numcodes;i++) keysym_list[i] = key; for(int i=0;i<numcodes;i++) keysym_list[i] = key;
XChangeKeyboardMapping(display, code, numcodes, keysym_list, 1); XChangeKeyboardMapping(display, code, numcodes, keysym_list, 1);
} }
if (!code)
insertAtLast(key, code); return -1;
} // }
// insertAtLast(key, code);
XTestFakeKeyEvent(display, code, down, CurrentTime);
XSync(display, 0);
} else { } else {
code = searchItem(key); // if (compareNode) {
deleteItem(key); // code = compareNode->keycode;
code = XkbKeysymToKeycode(key);
if (!code) {
int min, max, numcodes;
XDisplayKeycodes(display, &min, &max);
XGetKeyboardMapping(display, min, max-min, &numcodes);
code = (max-min+1)*numcodes;
KeySym keysym_list[numcodes];
for(int i=0;i<numcodes;i++) keysym_list[i] = key;
XChangeKeyboardMapping(display, code, numcodes, keysym_list, 1);
} }
if (!code)
return -1;
XTestFakeKeyEvent(display, code, down, CurrentTime); XTestFakeKeyEvent(display, code, down, CurrentTime);
XSync(display, 0); XSync(display, 0);
// deleteItem(key);
// }
} }
// return code;
} }
void XClipboardSet(char *src) { void XClipboardSet(char *src) {

View File

@ -73,9 +73,12 @@ func KeyDown(code uint64) error {
return fmt.Errorf("debounced key %v", code) return fmt.Errorf("debounced key %v", code)
} }
fmt.Printf("Code-Press %v", code)
debounce_key[code] = time.Now() debounce_key[code] = time.Now()
C.XKey(C.ulong(code), C.int(1)) t := C.XKey(C.ulong(code), C.int(1))
fmt.Printf("X-Press %v", t)
return nil return nil
} }
@ -103,7 +106,10 @@ func KeyUp(code uint64) error {
delete(debounce_key, code) delete(debounce_key, code)
C.XKey(C.ulong(code), C.int(0)) fmt.Printf("Code-Release %v", code)
t := C.XKey(C.ulong(code), C.int(0))
fmt.Printf("X-Release %v", t)
return nil return nil
} }

View File

@ -27,7 +27,7 @@
void XMove(int x, int y); void XMove(int x, int y);
void XScroll(int x, int y); void XScroll(int x, int y);
void XButton(unsigned int button, int down); void XButton(unsigned int button, int down);
void XKey(unsigned long key, int down); int XKey(unsigned long key, int down);
void XClipboardSet(char *src); void XClipboardSet(char *src);
char *XClipboardGet(); char *XClipboardGet();