aboutsummaryrefslogtreecommitdiffstats
path: root/quantum/process_keycode/process_unicode_common.h
blob: cab6eea6eafdc9b790e5e920ab034fa4bc627878 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* Copyright 2017 Jack Humbert
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "quantum.h"

#if defined(UNICODE_ENABLE) + defined(UNICODEMAP_ENABLE) + defined(UCIS_ENABLE) > 1
#    error "Cannot enable more than one Unicode method (UNICODE, UNICODEMAP, UCIS) at the same time"
#endif

// Keycodes used for starting Unicode input on different platforms
#ifndef UNICODE_KEY_OSX
#    define UNICODE_KEY_OSX KC_LALT
#endif
#ifndef UNICODE_KEY_LNX
#    define UNICODE_KEY_LNX LCTL(LSFT(KC_U))
#endif
#ifndef UNICODE_KEY_WINC
#    define UNICODE_KEY_WINC KC_RALT
#endif

// Comma-delimited, ordered list of input modes selected for use (e.g. in cycle)
// Example: #define UNICODE_SELECTED_MODES UC_WINC, UC_LNX
#ifndef UNICODE_SELECTED_MODES
#    define UNICODE_SELECTED_MODES -1
#endif

// Whether input mode changes in cycle should be written to EEPROM
#ifndef UNICODE_CYCLE_PERSIST
#    define UNICODE_CYCLE_PERSIST true
#endif

// Delay between starting Unicode input and sending a sequence, in ms
#ifndef UNICODE_TYPE_DELAY
#    define UNICODE_TYPE_DELAY 10
#endif

enum unicode_input_modes {
    UC_OSX,    // Mac OS X using Unicode Hex Input
    UC_LNX,    // Linux using IBus
    UC_WIN,    // Windows using EnableHexNumpad
    UC_BSD,    // BSD (not implemented)
    UC_WINC,   // Windows using WinCompose (https://github.com/samhocevar/wincompose)
    UC__COUNT  // Number of available input modes (always leave at the end)
};

typedef union {
    uint32_t raw;
    struct {
        uint8_t input_mode : 8;
    };
} unicode_config_t;

extern unicode_config_t unicode_config;
extern uint8_t          unicode_saved_mods;

void    unicode_input_mode_init(void);
uint8_t get_unicode_input_mode(void);
void    set_unicode_input_mode(uint8_t mode);
void    cycle_unicode_input_mode(uint8_t offset);
void    persist_unicode_input_mode(void);

void unicode_input_start(void);
void unicode_input_finish(void);
void unicode_input_cancel(void);

void register_hex(uint16_t hex);
void send_unicode_hex_string(const char *str);

bool process_unicode_common(uint16_t keycode, keyrecord_t *record);

#define UC_BSPC UC(0x0008)
#define UC_SPC UC(0x0020)

#define UC_EXLM UC(0x0021)
#define UC_DQUT UC(0x0022)
#define UC_HASH UC(0x0023)
#define UC_DLR UC(0x0024)
#define UC_PERC UC(0x0025)
#define UC_AMPR UC(0x0026)
#define UC_QUOT UC(0x0027)
#define UC_LPRN UC(0x0028)
#define UC_RPRN UC(0x0029)
#define UC_ASTR UC(0x002A)
#define UC_PLUS UC(0x002B)
#define UC_COMM UC(0x002C)
#define UC_DASH UC(0x002D)
#define UC_DOT UC(0x002E)
#define UC_SLSH UC(0x002F)

#define UC_0 UC(0x0030)
#define UC_1 UC(0x0031)
#define UC_2 UC(0x0032)
#define UC_3 UC(0x0033)
#define UC_4 UC(0x0034)
#define UC_5 UC(0x0035)
#define UC_6 UC(0x0036)
#define UC_7 UC(0x0037)
#define UC_8 UC(0x0038)
#define UC_9 UC(0x0039)

#define UC_COLN UC(0x003A)
#define UC_SCLN UC(0x003B)
#define UC_LT UC(0x003C)
#define UC_EQL UC(0x003D)
#define UC_GT UC(0x003E)
#define UC_QUES UC(0x003F)
#define UC_AT UC(0x0040)

#define UC_A UC(0x0041)
#define UC_B UC(0x0042)
#define UC_C UC(0x0043)
#define UC_D UC(0x0044)
#define UC_E UC(0x0045)
#define UC_F UC(0x0046)
#define UC_G UC(0x0047)
#define UC_H UC(0x0048)
#define UC_I UC(0x0049)
#define UC_J UC(0x004A)
#define UC_K UC(0x004B)
#define UC_L UC(0x004C)
#define UC_M UC(0x004D)
#define UC_N UC(0x004E)
#define UC_O UC(0x004F)
#define UC_P UC(0x0050)
#define UC_Q UC(0x0051)
#define UC_R UC(0x0052)
#define UC_S UC(0x0053)
#define UC_T UC(0x0054)
#define UC_U UC(0x0055)
#define UC_V UC(0x0056)
#define UC_W UC(0x0057)
#define UC_X UC(0x0058)
#define UC_Y UC(0x0059)
#define UC_Z UC(0x005A)

#define UC_LBRC UC(0x005B)
#define UC_BSLS UC(0x005C)
#define UC_RBRC UC(0x005D)
#define UC_CIRM UC(0x005E)
#define UC_UNDR UC(0x005F)

#define UC_GRV UC(0x0060)

#define UC_a UC(0x0061)
#define UC_b UC(0x0062)
#define UC_c UC(0x0063)
#define UC_d UC(0x0064)
#define UC_e UC(0x0065)
#define UC_f UC(0x0066)
#define UC_g UC(0x0067)
#define UC_h UC(0x0068)
#define UC_i UC(0x0069)
#define UC_j UC(0x006A)
#define UC_k UC(0x006B)
#define UC_l UC(0x006C)
#define UC_m UC(0x006D)
#define UC_n UC(0x006E)
#define UC_o UC(0x006F)
#define UC_p UC(0x0070)
#define UC_q UC(0x0071)
#define UC_r UC(0x0072)
#define UC_s UC(0x0073)
#define UC_t UC(0x0074)
#define UC_u UC(0x0075)
#define UC_v UC(0x0076)
#define UC_w UC(0x0077)
#define UC_x UC(0x0078)
#define UC_y UC(0x0079)
#define UC_z UC(0x007A)

#define UC_LCBR UC(0x007B)
#define UC_PIPE UC(0x007C)
#define UC_RCBR UC(0x007D)
#define UC_TILD UC(0x007E)
#define UC_DEL UC(0x007F)
P, &uip_all_zeroes_addr, sizeof(uip_ipaddr_t)); switch (DHCPMessageType) { case DHCP_DISCOVER: /* If no preference was made or the preferred IP is already taken, find a new address */ if (DHCPServerApp_CheckIfIPLeased(&PreferredClientIP)) DHCPServerApp_GetUnleasedIP(&PreferredClientIP); /* Create a new DHCP OFFER packet with the offered IP address */ AppDataSize += DHCPServerApp_FillDHCPHeader(AppData, DHCP_OFFER, &RemoteMACAddress, &PreferredClientIP, TransactionID); /* Add network mask and router information to the list of DHCP OFFER packet options */ AppDataSize += DHCPCommon_SetOption(AppData->Options, DHCP_OPTION_SUBNET_MASK, sizeof(uip_ipaddr_t), &Netmask); AppDataSize += DHCPCommon_SetOption(AppData->Options, DHCP_OPTION_ROUTER, sizeof(uip_ipaddr_t), &GatewayIPAddress); /* Send the DHCP OFFER packet */ uip_poll_conn(BroadcastConnection); memcpy(&uip_udp_conn->ripaddr, &uip_broadcast_addr, sizeof(uip_ipaddr_t)); uip_udp_send(AppDataSize); break; case DHCP_REQUEST: /* Check to see if the requested IP address has already been leased to a client */ if (!(DHCPServerApp_CheckIfIPLeased(&PreferredClientIP))) { /* Create a new DHCP ACK packet to accept the IP address lease */ AppDataSize += DHCPServerApp_FillDHCPHeader(AppData, DHCP_ACK, &RemoteMACAddress, &PreferredClientIP, TransactionID); /* Add network mask and router information to the list of DHCP ACK packet options */ AppDataSize += DHCPCommon_SetOption(AppData->Options, DHCP_OPTION_SUBNET_MASK, sizeof(uip_ipaddr_t), &Netmask); AppDataSize += DHCPCommon_SetOption(AppData->Options, DHCP_OPTION_ROUTER, sizeof(uip_ipaddr_t), &GatewayIPAddress); /* Mark the requested IP as leased to a client */ DHCPServerApp_LeaseIP(&PreferredClientIP); } else { /* Create a new DHCP NAK packet to reject the requested allocation */ AppDataSize += DHCPServerApp_FillDHCPHeader(AppData, DHCP_NAK, &RemoteMACAddress, &uip_all_zeroes_addr, TransactionID); } /* Send the DHCP ACK or NAK packet */ uip_poll_conn(BroadcastConnection); memcpy(&uip_udp_conn->ripaddr, &uip_broadcast_addr, sizeof(uip_ipaddr_t)); uip_udp_send(AppDataSize); break; case DHCP_RELEASE: /* Mark the IP address as released in the allocation table */ DHCPServerApp_UnleaseIP(&uip_udp_conn->ripaddr); break; } } } /** Fills the DHCP packet response with the appropriate BOOTP header for DHCP. This fills out all the required * fields, leaving only the additional DHCP options to be added to the packet before it is sent to the DHCP client. * * \param[out] DHCPHeader Location in the packet buffer where the BOOTP header should be written to * \param[in] DHCPMessageType DHCP Message type, such as DHCP_DISCOVER * \param[in] ClientHardwareAddress Client MAC address the created transaction should be directed to * \param[in] PreferredClientIP Preferred IP that should be given to the client if it is unallocated * \param[in] TransactionID Transaction ID the created transaction should be associated with * * \return Size in bytes of the created DHCP packet */ static uint16_t DHCPServerApp_FillDHCPHeader(DHCP_Header_t* const DHCPHeader, const uint8_t DHCPMessageType, const struct uip_eth_addr* const ClientHardwareAddress, const uip_ipaddr_t* const PreferredClientIP, const uint32_t TransactionID) { /* Erase existing packet data so that we start will all 0x00 DHCP header data */ memset(DHCPHeader, 0, sizeof(DHCP_Header_t)); DHCPHeader->Operation = DHCPMessageType; DHCPHeader->HardwareType = DHCP_HTYPE_ETHERNET; DHCPHeader->HardwareAddressLength = sizeof(MACAddress); DHCPHeader->Hops = 0; DHCPHeader->TransactionID = TransactionID; DHCPHeader->ElapsedSeconds = 0; DHCPHeader->Flags = 0; memcpy(&DHCPHeader->NextServerIP, &uip_hostaddr, sizeof(uip_ipaddr_t)); memcpy(&DHCPHeader->YourIP, PreferredClientIP, sizeof(uip_ipaddr_t)); memcpy(&DHCPHeader->ClientHardwareAddress, ClientHardwareAddress, sizeof(struct uip_eth_addr)); DHCPHeader->Cookie = DHCP_MAGIC_COOKIE; /* Add a DHCP message type and terminator options to the start of the DHCP options field */ DHCPHeader->Options[0] = DHCP_OPTION_MSG_TYPE; DHCPHeader->Options[1] = 1; DHCPHeader->Options[2] = DHCPMessageType; DHCPHeader->Options[3] = DHCP_OPTION_END; /* Calculate the total number of bytes added to the outgoing packet */ return (sizeof(DHCP_Header_t) + 4); } /** Checks to see if the nominated IP address has already been allocated to a client. * * \param[in] IPAddress IP Address whose lease status should be checked * * \pre The IP address must be within the same /24 subnet as the virtual webserver. * * \return Boolean \c true if the IP has already been leased to a client, \c false otherwise. */ static bool DHCPServerApp_CheckIfIPLeased(const uip_ipaddr_t* const IPAddress) { uint8_t Byte = (IPAddress->u8[3] / 8); uint8_t Mask = (1 << (IPAddress->u8[3] % 8)); /* Make sure that the requested IP address isn't already leased to the virtual server or another client */ if (IPAddress->u8[3] && !(IPAddress->u8[3] == uip_hostaddr.u8[3]) && !(LeasedIPs[Byte] & Mask)) return false; else return true; } /** Retrieves the next unleased IP in the IP address pool. * * \param[out] NewIPAddress Location where the generated IP Address should be stored */ static void DHCPServerApp_GetUnleasedIP(uip_ipaddr_t* const NewIPAddress) { uip_ipaddr_copy(NewIPAddress, &uip_hostaddr); /** Look through the current subnet, skipping the broadcast and zero IP addresses */ for (uint8_t IP = 1; IP < 254; IP++) { /* Update new IP address to lease with the current IP address to test */ NewIPAddress->u8[3] = IP; /* If we've found an unleased IP, abort with the updated IP stored for the called */ if (!(DHCPServerApp_CheckIfIPLeased(NewIPAddress))) return; } } /** Marks the given IP Address as leased in the address pool, so that it will not be * allocated to another client unless it is first released. * * \param[in] IPAddress IP Address to mark as leased * * \pre The IP address must be within the same /24 subnet as the virtual webserver. */ static void DHCPServerApp_LeaseIP(const uip_ipaddr_t* const IPAddress) { uint8_t Byte = (IPAddress->u8[3] / 8); uint8_t Mask = (1 << (IPAddress->u8[3] % 8)); /* Mark the IP address as leased in the allocation table */ LeasedIPs[Byte] |= Mask; } /** Marks the given IP Address as not leased in the address pool, so that it can be * allocated to another client upon request. * * \param[in] IPAddress IP Address to mark as not leased * * \pre The IP address must be within the same /24 subnet as the virtual webserver. */ static void DHCPServerApp_UnleaseIP(const uip_ipaddr_t* const IPAddress) { uint8_t Byte = (IPAddress->u8[3] / 8); uint8_t Mask = (1 << (IPAddress->u8[3] % 8)); /* Mark the IP address as unleased in the allocation table */ LeasedIPs[Byte] &= ~Mask; } #endif