source: Daodan/src/Patches/Input.c@ 1056

Last change on this file since 1056 was 1017, checked in by alloc, 10 years ago

Daodan 4.0: Added Input module, cheats bindable to keys

File size: 2.4 KB
RevLine 
[1017]1#include <stdlib.h>
2#include <stdarg.h>
3
4#include "../Daodan.h"
5#include "Input.h"
6#include "../Oni/Oni.h"
7#include "../Daodan_Patch.h"
8#include "Utility.h"
9
10#define EVENT_KEYPRESS_SECURETIME 3
11
12typedef struct {
13 uint32_t key;
14 const char* actionname;
15 ActionEventType_t eventType;
16 uint32_t keydownTimeoutTicks;
17 int64_t lastevent;
18
19 CustomActionCallback_t callback;
20 CustomActionCallbackArgument callbackArgument;
21} DD_CustomAction_t;
22
23static DD_CustomAction_t customActions[100];
24
25
26void Input_RegisterCustomAction (const char* actionname, ActionEventType_t eventType, uint32_t keydownTimeoutTicks, CustomActionCallback_t callback, CustomActionCallbackArgument callbackArgument) {
27 uint16_t i = 0;
28 DD_CustomAction_t* cur = customActions;
29
30 while ( (i < ARRAY_SIZE(customActions)) && (cur->callback != 0)) {
31 cur++;
32 i++;
33 }
34
35 if (i < ARRAY_SIZE(customActions)) {
36 cur->actionname = actionname;
37 cur->eventType = eventType;
38 cur->keydownTimeoutTicks = keydownTimeoutTicks;
39 cur->callback = callback;
40 cur->callbackArgument = callbackArgument;
41 } else {
42 STARTUPMESSAGE("Registering action %s failed, maximum actions reached", actionname);
43 }
44}
45
46
47_LIrBinding_Add Oni_LIrBinding_Add = (_LIrBinding_Add)0;
48uint16_t ONICALL DD_LIrBinding_Add(uint32_t key, const char* name) {
49 DD_CustomAction_t* cur;
50 for (cur = customActions; cur->callback != 0; cur++) {
51 if (!strcmp(name, cur->actionname)) {
52 cur->key = key;
53 return 0;
54 }
55 }
56
57 return Oni_LIrBinding_Add(key, name);
58}
59
60_LIrActionBuffer_Add Oni_LIrActionBuffer_Add = (_LIrActionBuffer_Add)0;
61void ONICALL DD_LIrActionBuffer_Add(void* unknown, LItDeviceInput* input) {
62 DD_CustomAction_t* cur;
63 for (cur = customActions; cur->callback != 0; cur++) {
64 if (cur->key == input->input) {
65 int64_t curTime = UUrMachineTime_Sixtieths();
66 if (cur->eventType == EVENT_KEYPRESS) {
67 if (cur->lastevent + EVENT_KEYPRESS_SECURETIME < curTime) {
68 cur->callback(cur->callbackArgument);
69 }
70 cur->lastevent = curTime;
71 } else if (cur->eventType == EVENT_KEYDOWN) {
72 if (cur->lastevent + cur->keydownTimeoutTicks < curTime) {
73 cur->callback(cur->callbackArgument);
74 cur->lastevent = curTime;
75 }
76 }
77 return;
78 }
79 }
80 Oni_LIrActionBuffer_Add(unknown, input);
81}
82
83void Input_PatchCode () {
84 Oni_LIrBinding_Add = DDrPatch_MakeDetour((void*)LIrBinding_Add, (void*)DD_LIrBinding_Add);
85 Oni_LIrActionBuffer_Add = DDrPatch_MakeDetour((void*)LIrActionBuffer_Add, (void*)DD_LIrActionBuffer_Add);
86}
87
Note: See TracBrowser for help on using the repository browser.