Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.accessibility/windows/native/jabswitch/jabswitch.cpp
40957 views
1
/*
2
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
#include <stdio.h>
27
#include <string.h>
28
#include <stdlib.h>
29
#include <ctype.h>
30
#include <Windows.h>
31
#include <tchar.h>
32
33
// This is the default buffer size used for RegQueryValue values.
34
#define DEFAULT_ALLOC MAX_PATH
35
// only allocate a buffer as big as MAX_ALLOC for RegQueryValue values.
36
#define MAX_ALLOC 262144
37
38
static LPCTSTR ACCESSIBILITY_USER_KEY =
39
_T("Software\\Microsoft\\Windows NT\\CurrentVersion\\Accessibility");
40
static LPCTSTR ACCESSIBILITY_SYSTEM_KEY =
41
_T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Accessibility\\Session");
42
static LPCTSTR ACCESSIBILITY_CONFIG =
43
_T("Configuration");
44
static LPCTSTR STR_ACCESSBRIDGE =
45
_T("oracle_javaaccessbridge");
46
47
// Note: There are senarios where more than one extension can be specified on the
48
// asssistive_technologies=
49
// line but this code only deals with the case of
50
// assistive_technologies=com.sun.java.accessibility.AccessBridge
51
// assuming that if additional extensions are desired the user knows how edit the file.
52
53
FILE* origFile;
54
FILE* tempFile;
55
56
bool isXP()
57
{
58
static bool isXPFlag = false;
59
OSVERSIONINFO osvi;
60
61
// Initialize the OSVERSIONINFO structure.
62
ZeroMemory( &osvi, sizeof( osvi ) );
63
osvi.dwOSVersionInfoSize = sizeof( osvi );
64
65
GetVersionEx( &osvi );
66
67
if ( osvi.dwMajorVersion == 5 ) // For Windows XP and Windows 2000
68
isXPFlag = true;
69
70
return isXPFlag ;
71
}
72
73
void enableJAB() {
74
// Copy lines from orig to temp modifying the line containing
75
// assistive_technologies=
76
// There are various scenarios:
77
// 1) If the line exists exactly as
78
// #assistive_technologies=com.sun.java.accessibility.AccessBridge
79
// replace it with
80
// assistive_technologies=com.sun.java.accessibility.AccessBridge
81
// 2) else if the line exists exactly as
82
// assistive_technologies=com.sun.java.accessibility.AccessBridge
83
// use it as is
84
// 3) else if a line containing "assistive_technologies" exits
85
// a) if it's already commented out, us it as is (jab will be enabled in step 4)
86
// b) else if it's not commented out, comment it out and add a new line with
87
// assistive_technologies=com.sun.java.accessibility.AccessBridge
88
// 4) If the line doesn't exist (or case 3a), add
89
// assistive_technologies=com.sun.java.accessibility.AccessBridge
90
// Do the same for screen_magnifier_present=
91
char line[512];
92
char commentLine[512] = "#";
93
char jabLine[] = "assistive_technologies=com.sun.java.accessibility.AccessBridge\n";
94
char magLine[] = "screen_magnifier_present=true\n";
95
bool foundJabLine = false;
96
bool foundMagLine = false;
97
while (!feof(origFile)) {
98
if (fgets(line, 512, origFile) != NULL) {
99
if (_stricmp(line, "#assistive_technologies=com.sun.java.accessibility.AccessBridge\n") == 0) {
100
fputs(jabLine, tempFile);
101
foundJabLine = true;
102
} else if (_stricmp(line, jabLine) == 0) {
103
fputs(line, tempFile);
104
foundJabLine = true;
105
} else if (strstr(line, "assistive_technologies") != NULL) {
106
char* context;
107
char* firstNonSpaceChar = strtok_s(line, " ", &context);
108
if (*firstNonSpaceChar == '#') {
109
fputs(line, tempFile);
110
} else {
111
strcat_s(commentLine, line);
112
fputs(commentLine, tempFile);
113
fputs(jabLine, tempFile);
114
foundJabLine = true;
115
}
116
} else if (_stricmp(line, "#screen_magnifier_present=true\n") == 0) {
117
fputs(magLine, tempFile);
118
foundMagLine = true;
119
} else if (_stricmp(line, magLine) == 0) {
120
fputs(line, tempFile);
121
foundMagLine = true;
122
} else if (strstr(line, "screen_magnifier_present") != NULL) {
123
char* context;
124
char* firstNonSpaceChar = strtok_s(line, " ", &context);
125
if (*firstNonSpaceChar == '#') {
126
fputs(line, tempFile);
127
} else {
128
strcat_s(commentLine, line);
129
fputs(commentLine, tempFile);
130
fputs(magLine, tempFile);
131
foundMagLine = true;
132
}
133
} else {
134
fputs(line, tempFile);
135
}
136
}
137
}
138
if (!foundJabLine) {
139
fputs(jabLine, tempFile);
140
}
141
if (!foundMagLine) {
142
fputs(magLine, tempFile);
143
}
144
}
145
146
void disableJAB() {
147
// Copy lines from orig to temp modifying the line containing
148
// assistive_technologies=
149
// There are various scenarios:
150
// 1) If the uncommented line exists, comment it out
151
// 2) If the line exists but is preceeded by a #, nothing to do
152
// 3) If the line doesn't exist, nothing to do
153
// Do the same for screen_magnifier_present=
154
char line[512];
155
char commentLine[512];
156
while (!feof(origFile)) {
157
if (fgets(line, 512, origFile) != NULL) {
158
if (strstr(line, "assistive_technologies") != NULL) {
159
char* context;
160
char* firstNonSpaceChar = strtok_s(line, " ", &context);
161
if (*firstNonSpaceChar != '#') {
162
strcpy_s(commentLine, "#");
163
strcat_s(commentLine, line);
164
fputs(commentLine, tempFile);
165
} else {
166
fputs(line, tempFile);
167
}
168
} else if (strstr(line, "screen_magnifier_present") != NULL) {
169
char* context;
170
char* firstNonSpaceChar = strtok_s(line, " ", &context);
171
if (*firstNonSpaceChar != '#') {
172
strcpy_s(commentLine, "#");
173
strcat_s(commentLine, line);
174
fputs(commentLine, tempFile);
175
} else {
176
fputs(line, tempFile);
177
}
178
} else {
179
fputs(line, tempFile);
180
}
181
}
182
}
183
}
184
185
int modify(bool enable) {
186
errno_t error = 0;
187
char path[_MAX_PATH];
188
char tempPath[_MAX_PATH];
189
// Get the path for %USERPROFILE%
190
char *profilePath;
191
size_t len;
192
error = _dupenv_s(&profilePath, &len, "USERPROFILE" );
193
if (error) {
194
printf("Error fetching USERPROFILE.\n");
195
perror("Error");
196
return error;
197
}
198
const char acc_props1[] = "\\.accessibility.properties";
199
const char acc_props2[] = "\\.acce$$ibility.properties";
200
// len must be 234 or less (233 characters)
201
// sizeof(path) is 260 (room for 259 characters)
202
// sizeof(acc_props1) is 27 (26 characters)
203
// path will hold 233 path characters plus 26 file characters plus 1 null character)
204
// if len - 1 > 233 then error
205
if ( len - 1 > sizeof(path) - sizeof(acc_props1) ||
206
len - 1 > sizeof(tempPath) - sizeof(acc_props2) ) {
207
printf("The USERPROFILE environment variable is too long.\n");
208
printf("It must be no longer than 233 characters.\n");
209
free(profilePath);
210
return 123;
211
}
212
path[0] = 0;
213
strcat_s(path, _MAX_PATH, profilePath);
214
strcat_s(path, acc_props1);
215
tempPath[0] = 0;
216
strcat_s(tempPath, _MAX_PATH, profilePath);
217
strcat_s(tempPath, acc_props2);
218
free(profilePath);
219
profilePath = 0;
220
// Open the original file. If it doesn't exist and this is an enable request then create it.
221
error = fopen_s(&origFile, path, "r");
222
if (error) {
223
if (enable) {
224
error = fopen_s(&origFile, path, "w");
225
if (error) {
226
printf("Couldn't create file: %s\n", path);
227
perror("Error");
228
} else {
229
char str[100] = "assistive_technologies=com.sun.java.accessibility.AccessBridge\n";
230
strcat_s(str, "screen_magnifier_present=true\n");
231
fprintf(origFile, str);
232
fclose(origFile);
233
}
234
} else {
235
// It's OK if the file isn't there for a -disable
236
error = 0;
237
}
238
} else {
239
// open a temp file
240
error = fopen_s(&tempFile, tempPath, "w");
241
if (error) {
242
printf("Couldn't open temp file: %s\n", tempPath);
243
perror("Error");
244
return error;
245
}
246
if (enable) {
247
enableJAB();
248
} else {
249
disableJAB();
250
}
251
fclose(origFile);
252
fclose(tempFile);
253
// delete the orig file and rename the temp file
254
if (remove(path) != 0) {
255
printf("Couldn't remove file: %s\n", path);
256
perror("Error");
257
return errno;
258
}
259
if (rename(tempPath, path) != 0) {
260
printf("Couldn't rename %s to %s.\n", tempPath, path);
261
perror("Error");
262
return errno;
263
}
264
}
265
return error;
266
}
267
268
void printUsage() {
269
printf("\njabswitch [/enable | /disable | /version | /?]\n\n");
270
printf("Description:\n");
271
printf(" jabswitch enables or disables the Java Access Bridge.\n\n");
272
printf("Parameters:\n");
273
printf(" /enable Enable the Java Accessibility Bridge.\n");
274
printf(" /disable Disable the Java Accessibility Bridge.\n");
275
printf(" /version Display the version.\n");
276
printf(" /? Display this usage information.\n");
277
printf("\nNote:\n");
278
printf(" The Java Access Bridge can also be enabled with the\n");
279
printf(" Windows Ease of Access control panel (which can be\n");
280
printf(" activated by pressing Windows + U). The Ease of Access\n");
281
printf(" control panel has a Java Access Bridge checkbox. Please\n");
282
printf(" be aware that unchecking the checkbox has no effect and\n");
283
printf(" in order to disable the Java Access Bridge you must run\n");
284
printf(" jabswitch.exe from the command line.\n");
285
}
286
287
void printVersion() {
288
TCHAR executableFileName[_MAX_PATH];
289
if (!GetModuleFileName(0, executableFileName, _MAX_PATH)) {
290
printf("Unable to get executable file name.\n");
291
return;
292
}
293
DWORD nParam;
294
DWORD nVersionSize = GetFileVersionInfoSize(executableFileName, &nParam);
295
if (!nVersionSize) {
296
printf("Unable to get version info size.\n");
297
return;
298
}
299
char* pVersionData = new char[nVersionSize];
300
if (!GetFileVersionInfo(executableFileName, 0, nVersionSize, pVersionData)) {
301
printf("Unable to get version info.\n");
302
return;
303
}
304
LPVOID pVersionInfo;
305
UINT nSize;
306
if (!VerQueryValue(pVersionData, _T("\\"), &pVersionInfo, &nSize)) {
307
printf("Unable to query version value.\n");
308
return;
309
}
310
VS_FIXEDFILEINFO *pVSInfo = (VS_FIXEDFILEINFO *)pVersionInfo;
311
char versionString[100];
312
sprintf_s( versionString, "version %i.%i.%i.%i",
313
pVSInfo->dwProductVersionMS >> 16,
314
pVSInfo->dwProductVersionMS & 0xFFFF,
315
pVSInfo->dwProductVersionLS >> 16,
316
pVSInfo->dwProductVersionLS & 0xFFFF );
317
char outputString[100];
318
strcpy_s(outputString, "jabswitch ");
319
strcat_s(outputString, versionString);
320
strcat_s(outputString, "\njabswitch enables or disables the Java Access Bridge.\n");
321
printf(outputString);
322
}
323
324
int regEnable() {
325
HKEY hKey;
326
DWORD retval = -1;
327
LSTATUS err;
328
err = RegOpenKeyEx(HKEY_CURRENT_USER, ACCESSIBILITY_USER_KEY, NULL, KEY_READ|KEY_WRITE, &hKey);
329
if (err == ERROR_SUCCESS) {
330
DWORD dataType = REG_SZ;
331
DWORD dataLength = DEFAULT_ALLOC;
332
TCHAR dataBuffer[DEFAULT_ALLOC];
333
TCHAR *data = dataBuffer;
334
bool freeData = false;
335
err = RegQueryValueEx(hKey, ACCESSIBILITY_CONFIG, 0, &dataType, (BYTE *)data, &dataLength);
336
if (err == ERROR_MORE_DATA) {
337
if (dataLength > 0 && dataLength < MAX_ALLOC) {
338
data = new TCHAR[dataLength];
339
err = RegQueryValueEx(hKey, ACCESSIBILITY_CONFIG, 0, &dataType, (BYTE *)data, &dataLength);
340
}
341
}
342
if (err == ERROR_SUCCESS) {
343
err = _tcslwr_s(dataBuffer, DEFAULT_ALLOC);
344
if (err) {
345
return -1;
346
}
347
if (_tcsstr(dataBuffer, STR_ACCESSBRIDGE) != NULL) {
348
return 0; // This is OK, e.g. ran enable twice and the value is there.
349
} else {
350
// add oracle_javaaccessbridge to Config key for HKCU
351
dataLength = dataLength + (_tcslen(STR_ACCESSBRIDGE) + 1) * sizeof(TCHAR);
352
TCHAR *newStr = new TCHAR[dataLength];
353
if (newStr != NULL) {
354
wsprintf(newStr, L"%s,%s", dataBuffer, STR_ACCESSBRIDGE);
355
RegSetValueEx(hKey, ACCESSIBILITY_CONFIG, 0, REG_SZ, (BYTE *)newStr, dataLength);
356
}
357
}
358
}
359
RegCloseKey(hKey);
360
}
361
return err;
362
}
363
364
int regDeleteValue(HKEY hFamilyKey, LPCWSTR lpSubKey)
365
{
366
HKEY hKey;
367
DWORD retval = -1;
368
LSTATUS err;
369
err = RegOpenKeyEx(hFamilyKey, lpSubKey, NULL, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, &hKey);
370
if (err != ERROR_SUCCESS)
371
err = RegOpenKeyEx(hFamilyKey, lpSubKey, NULL, KEY_READ|KEY_WRITE, &hKey);
372
373
if (err == ERROR_SUCCESS) {
374
DWORD dataType = REG_SZ;
375
DWORD dataLength = DEFAULT_ALLOC;
376
TCHAR dataBuffer[DEFAULT_ALLOC];
377
TCHAR searchBuffer[DEFAULT_ALLOC];
378
TCHAR *data = dataBuffer;
379
bool freeData = false;
380
err = RegQueryValueEx(hKey, ACCESSIBILITY_CONFIG, 0, &dataType, (BYTE *)data, &dataLength);
381
if (err == ERROR_MORE_DATA) {
382
if (dataLength > 0 && dataLength < MAX_ALLOC) {
383
data = new TCHAR[dataLength];
384
err = RegQueryValueEx(hKey, ACCESSIBILITY_CONFIG, 0, &dataType, (BYTE *)data, &dataLength);
385
}
386
}
387
if (err == ERROR_SUCCESS) {
388
err = _tcslwr_s(dataBuffer, DEFAULT_ALLOC);
389
if (err) {
390
return -1;
391
}
392
if (_tcsstr(dataBuffer, STR_ACCESSBRIDGE) == NULL) {
393
return 0; // This is OK, e.g. ran disable twice and the value is not there.
394
} else {
395
// remove oracle_javaaccessbridge from Config key
396
TCHAR *newStr = new TCHAR[dataLength];
397
TCHAR *nextToken;
398
LPTSTR tok, beg1 = dataBuffer;
399
bool first = true;
400
_tcscpy_s(newStr, dataLength, L"");
401
tok = _tcstok_s(beg1, L",", &nextToken);
402
while (tok != NULL) {
403
_tcscpy_s(searchBuffer, DEFAULT_ALLOC, tok);
404
err = _tcslwr_s(searchBuffer, DEFAULT_ALLOC);
405
if (err) {
406
return -1;
407
}
408
if (_tcsstr(searchBuffer, STR_ACCESSBRIDGE) == NULL) {
409
if (!first) {
410
_tcscat_s(newStr, dataLength, L",");
411
}
412
first = false;
413
_tcscat_s(newStr, dataLength, tok);
414
}
415
tok = _tcstok_s(NULL, L",", &nextToken);
416
}
417
dataLength = (_tcslen(newStr) + 1) * sizeof(TCHAR);
418
RegSetValueEx(hKey, ACCESSIBILITY_CONFIG, 0, REG_SZ, (BYTE *)newStr, dataLength);
419
}
420
}
421
RegCloseKey(hKey);
422
}
423
return err;
424
}
425
426
int regDisable()
427
{
428
LSTATUS err;
429
// Update value for HKCU
430
err=regDeleteValue(HKEY_CURRENT_USER, ACCESSIBILITY_USER_KEY);
431
// Update value for HKLM for Session
432
TCHAR dataBuffer[DEFAULT_ALLOC];
433
DWORD dwSessionId ;
434
ProcessIdToSessionId(GetCurrentProcessId(),&dwSessionId ) ;
435
if( dwSessionId >= 0 )
436
{
437
wsprintf(dataBuffer, L"%s%d", ACCESSIBILITY_SYSTEM_KEY, dwSessionId);
438
err=regDeleteValue(HKEY_LOCAL_MACHINE, dataBuffer);
439
}
440
return err;
441
}
442
443
void main(int argc, char* argv[]) {
444
bool enableWasRequested = false;
445
bool disableWasRequested = false;
446
bool badParams = true;
447
int error = 0;
448
if (argc == 2) {
449
if (_stricmp(argv[1], "-?") == 0 || _stricmp(argv[1], "/?") == 0) {
450
printUsage();
451
badParams = false;
452
} else if (_stricmp(argv[1], "-version") == 0 || _stricmp(argv[1], "/version") == 0) {
453
printVersion();
454
badParams = false;
455
} else {
456
if (_stricmp(argv[1], "-enable") == 0 || _stricmp(argv[1], "/enable") == 0) {
457
badParams = false;
458
enableWasRequested = true;
459
error = modify(true);
460
if (error == 0) {
461
if( !isXP() )
462
regEnable();
463
}
464
} else if (_stricmp(argv[1], "-disable") == 0 || _stricmp(argv[1], "/disable") == 0) {
465
badParams = false;
466
disableWasRequested = true;
467
error = modify(false);
468
if (error == 0) {
469
if( !isXP() )
470
regDisable();
471
}
472
}
473
}
474
}
475
if (badParams) {
476
printUsage();
477
} else if (enableWasRequested || disableWasRequested) {
478
if (error != 0) {
479
printf("There was an error.\n\n");
480
}
481
printf("The Java Access Bridge has ");
482
if (error != 0) {
483
printf("not ");
484
}
485
printf("been ");
486
if (enableWasRequested) {
487
printf("enabled.\n");
488
} else {
489
printf("disabled.\n");
490
}
491
// Use exit so test case can sense for error.
492
if (error != 0) {
493
exit(error);
494
}
495
}
496
}
497
498