-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlibxkbswitchwin.c
255 lines (218 loc) · 7.74 KB
/
libxkbswitchwin.c
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* xkb-switch-lib API port to Win32/Win64
* Copyright (C) 2013 Dmitry Hrabrov a.k.a. DeXPeriX
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "libxkbswitchwin.h"
char lName[LBUF+1];
char aName[LBUF+1];
char keybuf[KBUF_SIZE+1];
char stringBuf[BIGBUF+1];
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
lName[0] = 0;
lName[1] = 0;
lName[2] = 0;
return TRUE; // succesful
}
DX_EXPORT
char dtolower(char c){
if( (c>='A') && (c<='Z') ) c = c - 'A' + 'a';
return c;
}
DX_EXPORT
int dxGetLayout(long a){
unsigned int x;
HWND hwnd;
DWORD threadId;
HKL currentLayout;
hwnd = GetForegroundWindow();
if( hwnd ){
threadId = GetWindowThreadProcessId(hwnd, NULL);
currentLayout = GetKeyboardLayout(threadId);
x = (unsigned int)currentLayout & 0x0000FFFF;
return x;
} else return -1;
}
DX_EXPORT
const char * Xkb_Switch_getXkbLayout( const char * param /* unused */ ){
unsigned int x;
HWND hwnd;
DWORD threadId;
HKL currentLayout;
LCID localez;
hwnd = GetForegroundWindow();
if( hwnd ){
threadId = GetWindowThreadProcessId(hwnd, NULL);
currentLayout = GetKeyboardLayout(threadId);
x = (unsigned int)currentLayout & 0x0000FFFF;
localez = MAKELCID( (LANGID)x, SORT_DEFAULT);
GetLocaleInfo(localez, LOCALE_SISO3166CTRYNAME, lName, LBUF);
lName[0] = dtolower(lName[0]);
lName[1] = dtolower(lName[1]);
lName[2] = 0;
return lName;
} else return NULL;
}
DX_EXPORT
const char * Xkb_Switch_setXkbLayout( const char * newgrp ){
unsigned int i, n, Lid;
HKL lpList[MAX_LAYS];
HKL currentLayout;
HWND hwnd;
LCID localez;
if ( newgrp == NULL || newgrp[ 0 ] == '\0' ) return NULL;
n = GetKeyboardLayoutList(0, NULL);
n = GetKeyboardLayoutList(n, lpList);
hwnd = GetForegroundWindow();
for(i=0; i<n; i++){
Lid = ((unsigned int)lpList[i]) & 0x0000FFFF; /* bottom 16 bit */
localez = MAKELCID( (LANGID)Lid, SORT_DEFAULT);
GetLocaleInfo(localez, LOCALE_SISO3166CTRYNAME, lName, LBUF);
if(
(hwnd) &&
( dtolower(newgrp[0]) == dtolower(lName[0]) ) &&
( dtolower(newgrp[1]) == dtolower(lName[1]) )
){
currentLayout = lpList[i];
PostMessage(hwnd,WM_INPUTLANGCHANGEREQUEST,0,(LPARAM)(currentLayout));
return lName;
}
}
return NULL;
}
/*
DX_EXPORT
char dxGetLocalizedCharByUS(char c, int layout){
unsigned int i, n, Lid;
HKL lpList[MAX_LAYS];
HKL currentLayout;
short key, topkey;
char keyboardState[KBUF_SIZE];
unsigned long wbuf = 0;
int canConvert = 0;
n = GetKeyboardLayoutList(0, NULL);
n = GetKeyboardLayoutList(n, lpList);
key = VkKeyScanEx(c, 1033); //get key from us-layout
topkey = (key & 0xFF00) >> 8;
for(i=0; i< ( sizeof(keyboardState)/sizeof(keyboardState[0]) ); i++) keyboardState[i] = 0;
if( topkey & 1 ) keyboardState[VK_SHIFT] = 0xFF;
if( topkey & 2 ) keyboardState[VK_CONTROL] = 0xFF;
if( topkey & 4 ) keyboardState[VK_MENU] = 0xFF;
for(i=0; i<n; i++){
Lid = ((unsigned int)lpList[i]) & 0x0000FFFF;
currentLayout = lpList[i];
if( (Lid == layout) || (lpList[i] == layout) ){
//canConvert = ToUnicodeEx( key, 0, keyboardState, keybuf, KBUF_SIZE, 0, currentLayout );
canConvert = ToAsciiEx( key, 0, keyboardState, &wbuf, 0, currentLayout );
if( canConvert > 0 ){
return wbuf;
}
}
}
return 0;
}*/
DX_EXPORT
const char * Xkb_Switch_getLocalizedCharByUS( char c, const char * grp, int isUTF ){
unsigned int i, j, n, Lid;
HKL lpList[MAX_LAYS];
HKL currentLayout;
LCID localez;
UINT CodePage = CP_UTF8;
short key, topkey, topw;
unsigned char keyboardState[KBUF_SIZE];
wchar_t wstr[KBUF_SIZE];
unsigned long wbuf = 0;
int canConvert = 0;
n = GetKeyboardLayoutList(0, NULL);
n = GetKeyboardLayoutList(n, lpList);
//key = VkKeyScan(c);
key = VkKeyScanEx(c, 1033); //get key from us-layout
topkey = (key & 0xFF00) >> 8;
keybuf[0] = 0;
keybuf[1] = 0;
keybuf[2] = 0;
for(i=0; i< ( sizeof(keyboardState)/sizeof(keyboardState[0]) ); i++) keyboardState[i] = 0;
if( topkey & 1 ) keyboardState[VK_SHIFT] = 0xFF;
if( topkey & 2 ) keyboardState[VK_CONTROL] = 0xFF;
if( topkey & 4 ) keyboardState[VK_MENU] = 0xFF;
for(j=0; j<n; j++){
Lid = ((unsigned int)lpList[j]) & 0x0000FFFF; /* bottom 16 bit */
currentLayout = lpList[j];
localez = MAKELCID( (LANGID)Lid, SORT_DEFAULT);
GetLocaleInfo(localez, LOCALE_SISO3166CTRYNAME, aName, LBUF);
if(
( dtolower(grp[0]) == dtolower(aName[0]) ) &&
( dtolower(grp[1]) == dtolower(aName[1]) )
){
/*if( isUTF ){*/
CodePage = CP_UTF8;
canConvert = ToUnicodeEx( key, 0, keyboardState, wstr, KBUF_SIZE, 0, currentLayout );
/*} else {
CodePage = CP_ACP;
canConvert = ToAsciiEx( key, 0, keyboardState, &wbuf, 0, currentLayout );
wstr[0] = wbuf & 0x00FF;
wstr[1] = wbuf & 0xFF00;
}*/
if( !isUTF ) CodePage = CP_ACP;
WideCharToMultiByte ( CodePage, // utf8/ansi code page
0, // Check
&wstr, // Source Unicode string
canConvert, // -1 means string is zero-terminated
&keybuf, // Destination char string
KBUF_SIZE, // Size of buffer
NULL, // No default character
NULL ); // Don't care about this
}
}
return keybuf;
}
DX_EXPORT
const char * Xkb_Switch_getLocalizedCharByUSutf( char c, const char * grp ){
return Xkb_Switch_getLocalizedCharByUS(c, grp, 1);
}
DX_EXPORT
const char * Xkb_Switch_getLocalizedCharByUSansi( char c, const char * grp ){
return Xkb_Switch_getLocalizedCharByUS(c, grp, 0);
}
DX_EXPORT
const char * Xkb_Switch_getCurrentCharByUSutf(const char * curChar){
char c = curChar[0];
return Xkb_Switch_getLocalizedCharByUS( c, Xkb_Switch_getXkbLayout(NULL), 1 );
}
DX_EXPORT
const char * Xkb_Switch_getCurrentCharByUSansi(const char * curChar){
char c = curChar[0];
return Xkb_Switch_getLocalizedCharByUS( c, Xkb_Switch_getXkbLayout(NULL), 0 );
}
DX_EXPORT
const char * Xkb_Switch_getCurrentStringByUS(const char * curString){
char* retbuf;
//int curLayout;
int i;
for(i=0;i<BIGBUF-1; i++) stringBuf[i] = 0;
//curLayout = dxGetLayout(1033);//
Xkb_Switch_getXkbLayout(NULL);
i=0;
while( curString[i] != 0 ){
retbuf = Xkb_Switch_getLocalizedCharByUSutf( curString[i], lName );
stringBuf[i] = retbuf[0];
//stringBuf[i] = dxGetLocalizedCharByUS( curString[i], curLayout );
i++;
}
stringBuf[i] = 0;
return stringBuf;
}