-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScreenKeyboard.cpp
175 lines (139 loc) · 4.85 KB
/
ScreenKeyboard.cpp
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
/*
Copyright (C) 2021 Christian Strauch
This file is part of ScreenKeyboard.
ScreenKeyboard 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 3 of the License, or
(at your option) any later version.
ScreenKeyboard 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 ScreenKeyboard. If not, see <https://www.gnu.org/licenses/>
*/
#pragma comment(lib, "ComCtl32.lib")
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#undef WIN32_LEAN_AND_MEAN
#include <windowsx.h>
#include <CommCtrl.h>
#include <Shlobj.h>
#include <iostream>
#include <map>
#include <string>
#include <sstream>
#include <filesystem>
#include <fstream>
#include <stdint.h>
#include "targetver.h"
#include "Resource.h"
#include "Config.h"
#include "Keyboard.h"
using namespace std;
HINSTANCE instance = NULL;
vector<Config> configs;
bool ReadUserConfigFiles()
{
bool loaded_any_custom_layouts = false;
wchar_t* documents_path_c = NULL;
HRESULT res = SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &documents_path_c);
wstring documents_path_str(documents_path_c);
CoTaskMemFree((void*)documents_path_c);
try
{
filesystem::path config_path(documents_path_str);
config_path /= "ScreenKeyboard";
error_code err;
if (!filesystem::is_directory(config_path, err))
return false;
filesystem::directory_iterator dir_iter(config_path);
for (auto& path_iter : dir_iter)
{
filesystem::path path = path_iter.path();
try
{
if (path_iter.is_regular_file() or path_iter.is_symlink())
{
if (path.extension() == ".json")
{
Config cfg;
LoadConfig(cfg, path);
configs.push_back(cfg);
loaded_any_custom_layouts = true;
}
}
}
catch (exception& e)
{
stringstream text;
text << "The layout file '" << path << "' could not be loaded: " << e.what() << "\n\nPress OK to continue.";
MessageBoxA(NULL, text.str().c_str(), "One of the user layout files could not be loaded", MB_OK | MB_ICONWARNING);
}
}
}
catch (exception& e)
{
stringstream text;
text << "Could not load user layouts: " << e.what() << "\n";
MessageBoxA(NULL, text.str().c_str(), "Error while loading user layout files", MB_OK | MB_ICONERROR);
}
return loaded_any_custom_layouts;
}
bool LoadFileInResource(int name, int type, DWORD& size, const char*& data)
{
HMODULE handle = GetModuleHandle(NULL);
HRSRC rc = FindResource(handle, MAKEINTRESOURCE(name), MAKEINTRESOURCE(type));
if (rc == 0)
return false;
HGLOBAL rcData = LoadResource(handle, rc);
if (rcData == 0)
return false;
size = SizeofResource(handle, rc);
data = static_cast<const char*>(LockResource(rcData));
return true;
}
void TryLoadEmbeddedConfigString(int name, int type)
{
// load embedded default layout
DWORD size = 0;
const char* data = 0;
if (!LoadFileInResource(name, type, size, data))
{
MessageBoxA(NULL, "Error loading embedded default config!", "FATAL ERROR", MB_OK | MB_ICONERROR);
return;
}
char* resstr = new char[size + 1L];
memcpy(resstr, data, size);
resstr[size] = 0;
string str = resstr;
delete[] resstr;
try
{
Config cfg;
LoadConfig(cfg, str.c_str());
configs.push_back(cfg);
}
catch (exception& e)
{
stringstream text;
text << "Exception while loading embedded config: " << e.what() << "\n";
MessageBoxA(NULL, text.str().c_str(), "FATAL ERROR", MB_OK | MB_ICONERROR);
}
}
int APIENTRY wWinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
{
instance=hInst;
configs.reserve(2);
TryLoadEmbeddedConfigString(IDR_JSON_DEFAULT_ENGLISH, RESTYPE_JSON_FILE);
TryLoadEmbeddedConfigString(IDR_JSON_OLD_ENGLISH, RESTYPE_JSON_FILE);
ReadUserConfigFiles();
InitCommonControls();
if (configs.size() < 1)
configs.emplace_back();
int ret = Keyboard::CreateKeyboard(instance, configs, 0);
return ret;
}