minor simplification.
[AROS.git] / workbench / libs / kms / kms_library.c
blobf6c2487c91921dd8bc142d3db4f265c8bed34e89
1 #include <aros/debug.h>
2 #include <aros/symbolsets.h>
3 #include <devices/input.h>
4 #include <devices/rawkeycodes.h>
5 #include <libraries/kms.h>
6 #include <proto/exec.h>
7 #include <proto/keymap.h>
9 #include "kms_intern.h"
11 /* Global base pointer, for patch functions */
12 static struct kms_base *kmsbase;
14 AROS_LH4(WORD, patch_MapRawKey,
15 AROS_LHA(struct InputEvent *, event, A0),
16 AROS_LHA(STRPTR , buffer, A1),
17 AROS_LHA(LONG , length, D1),
18 AROS_LHA(struct KeyMap *, keyMap, A2),
19 struct Library *, KeymapBase, 7, Kms)
21 AROS_LIBFUNC_INIT
24 * Just pass the call on if we are disabled.
25 * Otherwise we can stick with alternate keymap
26 * and no way to switch back to default one.
28 if (kmsbase->pub.kms_SwitchQual != KMS_QUAL_DISABLE)
30 if (kmsbase->active)
31 keyMap = kmsbase->pub.kms_AltKeymap;
34 return AROS_CALL4(WORD, kmsbase->rom_MapRawKey,
35 AROS_LCA(struct InputEvent *, event, A0),
36 AROS_LCA(STRPTR , buffer, A1),
37 AROS_LCA(LONG , length, D1),
38 AROS_LCA(struct KeyMap *, keyMap, A2),
39 struct Library *, KeymapBase);
41 AROS_LIBFUNC_EXIT
44 AROS_LH5(LONG, patch_MapANSI,
45 AROS_LHA(STRPTR , string, A0),
46 AROS_LHA(LONG , count, D0),
47 AROS_LHA(STRPTR , buffer, A1),
48 AROS_LHA(LONG , length, D1),
49 AROS_LHA(struct KeyMap *, keyMap, A2),
50 struct Library *, KeymapBase, 8, Kms)
52 AROS_LIBFUNC_INIT
54 if (kmsbase->pub.kms_SwitchQual != KMS_QUAL_DISABLE)
56 if (kmsbase->active)
57 keyMap = kmsbase->pub.kms_AltKeymap;
60 return AROS_CALL5(LONG, kmsbase->rom_MapANSI,
61 AROS_LCA(STRPTR , string, A0),
62 AROS_LCA(LONG , count, D0),
63 AROS_LCA(STRPTR , buffer, A1),
64 AROS_LCA(LONG , length, D1),
65 AROS_LCA(struct KeyMap *, keyMap, A2),
66 struct Library *, KeymapBase);
68 AROS_LIBFUNC_EXIT
71 AROS_UFH2(static struct InputEvent *, kms_InputHandler,
72 AROS_UFHA(struct InputEvent * ,event, A0),
73 AROS_UFHA(struct kms_base *, KMSBase, A1))
75 AROS_USERFUNC_INIT
77 if (event->ie_Class == IECLASS_RAWKEY)
79 D(bug("[KMS] RAWKEY: qualifier 0x%04X, code 0x%04X\n", event->ie_Qualifier, event->ie_Code));
81 /* 0x03FF masks out all mouse qualifiers. I'm too lazy to type them all by names. */
82 if ((event->ie_Qualifier & 0x03FF) == kmsbase->pub.kms_SwitchQual)
85 * Qualifiers match. Now switch happens if:
86 * a) key codes match;
87 * b) kms_SwitchCode contains KMS_CODE_NOKEY and event code is any qualifier key.
88 * Without (b) sequence of pressing qualifier keys would matter, for example
89 * 'lshift + rshift' would be different from 'rshift + lshift',
91 if (((kmsbase->pub.kms_SwitchCode == KMS_CODE_NOKEY) &&
92 (event->ie_Code >= RAWKEY_LSHIFT) && (event->ie_Code <= RAWKEY_RAMIGA)) ||
93 (event->ie_Code == kmsbase->pub.kms_SwitchCode))
95 /* Switch keymap and swallow the event */
96 kmsbase->active = !kmsbase->active;
97 return NULL;
102 return event;
104 AROS_USERFUNC_EXIT
107 static ULONG KMS_Init(struct kms_base *KMSBase)
109 struct MsgPort *mp;
110 struct IOStdReq *ioreq;
111 LONG error = TRUE;
113 KMSBase->kmr = OpenResource("keymap.resource");
114 if (!KMSBase->kmr)
115 return FALSE;
117 mp = CreateMsgPort();
118 if (!mp)
119 return FALSE;
121 ioreq = CreateIORequest(mp, sizeof(struct IOStdReq));
122 if (ioreq)
124 error = OpenDevice("input.device", 0, (struct IORequest *)ioreq, 0);
125 if (!error)
128 * Keymap switcher should work no matter what.
129 * So we use the highest possible priority.
131 KMSBase->input_Int.is_Node.ln_Name = "Keymap switcher";
132 KMSBase->input_Int.is_Node.ln_Pri = 127;
133 KMSBase->input_Int.is_Code = (void (*)())kms_InputHandler;
134 KMSBase->input_Int.is_Data = KMSBase;
136 ioreq->io_Command = IND_ADDHANDLER;
137 ioreq->io_Data = &KMSBase->input_Int;
139 DoIO((struct IORequest *)ioreq);
141 D(bug("[KMS] Installed input handler\n"));
143 CloseDevice((struct IORequest *)ioreq);
146 DeleteIORequest((struct IORequest *)ioreq);
148 DeleteMsgPort(mp);
150 if (error)
151 return FALSE;
153 KMSBase->rom_MapRawKey = SetFunction(KeymapBase, (LONG)(-7 * LIB_VECTSIZE), AROS_SLIB_ENTRY(patch_MapRawKey, Kms, 7));
154 KMSBase->rom_MapANSI = SetFunction(KeymapBase, (LONG)(-8 * LIB_VECTSIZE), AROS_SLIB_ENTRY(patch_MapANSI, Kms, 8));
156 KMSBase->pub.kms_SwitchQual = KMS_QUAL_DISABLE;
158 kmsbase = KMSBase;
159 return TRUE;
162 ADD2INITLIB(KMS_Init, 0)