-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.cpccOS.cpp
executable file
·286 lines (216 loc) · 8.85 KB
/
core.cpccOS.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* *****************************************
* File: core.cpccOS.cpp
* Purpose: Portable (cross-platform), light-weight, OS functions
* *****************************************
* Library: Cross Platform C++ Classes (cpcc)
* Copyright: 2015 StarMessage software.
* License: Free for opensource projects.
* Commercial license exists for closed source projects.
* Web: http://www.StarMessageSoftware.com/cpcclibrary
* Download: https://github.com/starmessage/cpcc
* email: sales -at- starmessage.info
* *****************************************
*/
// do not compile this if added in the xcode project files.
// the .mm must be included as well
#if defined(_WIN32) || defined (IMPORTED_BY_core_cpccOS_mm)
#ifdef __APPLE__
#if !defined __OBJC__
#error File core.cpccOS.cpp must be compiled as obc++ under MAC. Did you include the core.cpccOS.cpp instead of core.cpccOS.mm ?
#endif
#endif
#include <iostream>
#include <sstream>
#include <mutex>
#include "core.cpccOS.h"
#include "core.cpccIdeMacros.h"
#ifdef _WIN32
#include "core.cpccOSWin.h"
#elif __APPLE__
#ifdef cpccTARGET_IOS
#include <Foundation/Foundation.h>
#include <UIKit/UIKit.h>
#else
#include <AppKit/AppKit.h>
#endif
#endif
/*
cpcc_string cpccOS::getOSNameVersionAndBuild(void)
{
#ifdef __APPLE__
// In the documentation: "this string is not appropriate for parsing."
NSString * operatingSystemVersionString = // @"Версія 10.10.5 (складення 14F2511)";
[[NSProcessInfo processInfo] operatingSystemVersionString];
// returns: Version 10.12.6 (Build 16G29)
// under ukrainian — 'uk_UA' locale it returns:
// Версія 10.10.5 (складення 14F2511)
if (!operatingSystemVersionString)
return "Mac OS X Version ?";
std::string result = [operatingSystemVersionString cStringUsingEncoding : NSUTF8StringEncoding];
size_t pos = result.find("10.");
if (pos!=std::string::npos)
result.erase(0, pos);
result.insert(0,"Mac OS X ");
return result; // returns: Mac OS X 10.12.6 (Build 16G29)
// todo: change the function to return just the version numbers. No build number ot other text
#else
return cpccOSWin::getWindowsNameVersionAndBuild();
#endif
}
*/
cpcc_string cpccOS::getPreferredLanguage(void)
{
#ifdef _WIN32
/*
If you're asking about "Which language the OS menus and dialogs are dispalyed in" (i.e. which MUI - Multilingual User Interface kit - is installed), use the following:
GetSystemDefaultUILanguage to get the original language of the system,
GetUserDefaultUILanguage
https://docs.microsoft.com/en-us/windows/desktop/api/winnls/nf-winnls-getuserdefaultuilanguage
to get the current user's selection,
This function returns only a language identifier.
An application can retrieve the language name using the GetUserPreferredUILanguages function.
EnumUILanguages to see which languages are available.
GetUserPreferredUILanguages
https://msdn.microsoft.com/en-us/library/windows/desktop/dd318139(v=vs.85).aspx
GetUserPreferredUILanguages(MUI_LANGUAGE_NAME
*/
// from https://msdn.microsoft.com/en-us/library/windows/apps/jj244362(v=vs.105).aspx
/*
ULONG numLanguages = 0;
WCHAR pwszLanguagesBuffer[50];
DWORD cchLanguagesBuffer = sizeof(pwszLanguagesBuffer) -1;
BOOL hr = GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numLanguages, &pwszLanguagesBuffer, &cchLanguagesBuffer);
if (hr)
return std::string(pwszLanguagesBuffer);
*/
LANGID lang_id = GetUserDefaultUILanguage();
return cpccOSWin::GetLangStringFromLangId(lang_id, true);
#elif __APPLE__
NSString* language = [[NSLocale preferredLanguages] objectAtIndex:0];
return [language cStringUsingEncoding:NSUTF8StringEncoding];
#endif
} // End getPreferredLanguage
const cpcc_string cpccOS::getUserName(void)
{
#ifdef _WIN32
cpcc_char username[200];
DWORD username_len = sizeof(username) - 1;
GetUserName(username, &username_len);
return username;
#elif __APPLE__
NSString* un = NSUserName();
return[un UTF8String];
#endif
}
// portable / cross platform C function for Windows, OSX
// returns the computer name
const cpcc_string cpccOS::getComputerName(void)
{
#ifdef _WIN32
cpcc_char name[255]; DWORD size;
size = sizeof(name) - 1;
GetComputerName(name, &size);
return name;
#endif
#ifdef __APPLE__
char name[_POSIX_HOST_NAME_MAX + 1];
if (gethostname(name, sizeof name) == -1)
return std::string("getComputerName failed.");
// remove the .local or .lan from the computer name as reported by OSX
std::string ComputerNameTrimmed(name);
util_RemoveSuffixFromString(ComputerNameTrimmed, (char*)".lan");
util_RemoveSuffixFromString(ComputerNameTrimmed, (char*)".local");
return ComputerNameTrimmed;
/*
[[NSDictionary
dictionaryWithContentsOfFile:@"/var/db/SystemConfiguration/preferences.xml"
] valueForKeyPath:@"System.System.ComputerName"];
*/
/*
[[NSProcessInfo processInfo] hostName]
*/
#endif
}
void cpccOS::getMainMonitorResolution(int& width, int& height)
{
#ifdef _WIN32
/*
Size of the primary monitor: GetSystemMetrics SM_CXSCREEN / SM_CYSCREEN (GetDeviceCaps can also be used)
Size of all monitors (combined): GetSystemMetrics SM_CX/YVIRTUALSCREEN
Size of work area (screen excluding taskbar and other docked bars) on primary monitor: SystemParametersInfo SPI_GETWORKAREA
Size of a specific monitor (work area and "screen"): GetMonitorInfo
Remember that a monitor does not always "begin" at 0x0.
MonitorFromWindow to find the monitor your window is on and then call GetMonitorInfo.
*/
width = GetSystemMetrics(SM_CXSCREEN);
height = GetSystemMetrics(SM_CYSCREEN);
#elif defined(cpccTARGET_IOS)
width = [UIScreen mainScreen].bounds.size.width;
height = [UIScreen mainScreen].bounds.size.height;
#elif defined(TARGET_OS_MAC)
NSScreen* mainScreen = [NSScreen mainScreen];
NSRect rect = [mainScreen frame];
width = rect.size.width;
height = rect.size.height;
#endif
}
#ifdef __APPLE__
std::string cpccOS::readProgramVersionByPrincipalClass(const char *aClassName)
{
NSString *aNSClassName = [NSString stringWithCString: aClassName encoding:NSASCIIStringEncoding ];
// NSString *tmpVersion = [[NSBundle bundleForClass: aNSClassName] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSString *tmpVersion = [[NSBundle bundleForClass: aNSClassName] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
std::string ver = [tmpVersion UTF8String];
// [tmpVersion release];
return ver;
}
#if !(TARGET_OS_IPHONE)
std::string cpccOS::getBundleIDfromAppName(const char *aAppName)
{
// [[NSBundle mainBundle] bundleIdentifier]
// return an application's Bundle Identifier for a named application:
NSString *appName = [NSString stringWithCString: aAppName encoding:NSASCIIStringEncoding ];
NSWorkspace * workspace = [NSWorkspace sharedWorkspace];
NSString * appPath = [workspace fullPathForApplication:appName];
std::string result;
if (appPath) {
NSBundle * appBundle = [NSBundle bundleWithPath:appPath];
NSString *bundleID = [appBundle bundleIdentifier];
result = [bundleID UTF8String];
}
return result;
}
#endif
std::string cpccOS::getBundleID(void)
{
std::string result = [[[NSBundle mainBundle] bundleIdentifier] UTF8String];
return result;
}
#endif
cpcc_string& cpccOS::readProgramVersion(void)
{
// static variable
static cpcc_string ver(_T("Version N/A"));
if (ver!= _T("Version N/A"))
return ver;
#ifdef __APPLE__
// how to get the .saver bundle:
// http://stackoverflow.com/questions/8490562/using-a-system-plug-in-saver-bundle-inside-a-cocoa-application-as-a-resource
// http://stackoverflow.com/questions/17391380/macosx-screensaver-how-to-get-the-path-to-saver-bundle
/*
cpccFileSystemMini fs;
NSString *appBundlePath = [NSString stringWithCString:fs.getAppBundlePath().c_str() encoding:NSASCIIStringEncoding];
NSBundle *bundle = [NSBundle bundleWithPath:appBundlePath ] ;
NSString *tmpVersion = [bundle objectForInfoDictionaryKey:@"CFBundleVersion"];
[bundle release];
//[appBundlePath release]; // already deallocated
*/
NSString *tmpVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
ver = [tmpVersion UTF8String];
#if !(__has_feature(objc_arc))
[tmpVersion release];
#endif
#endif
return ver;
}
#endif