-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.cpccOS.h
146 lines (110 loc) · 4.11 KB
/
core.cpccOS.h
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
/* *****************************************
* File: core.cpccOS.h
* 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
* *****************************************
*/
#pragma once
#ifdef _WIN32
#include <Windows.h>
#endif
#ifdef __APPLE__
#include <TargetConditionals.h>
#include <sys/sysctl.h> // for sysctlbyname
#endif
#include <vector>
#include <string>
#include "cpccUnicodeSupport.h"
// /////////////////////////////
//
// class cpccOS declaration
//
// /////////////////////////////
class cpccOS
{
private:
#ifdef __APPLE__
static void util_RemoveSuffixFromString(std::string &str, const char* suffixToRemove);
#endif
public:
// todo: move it to core.cpccHardware.h
static void getMainMonitorResolution(int &width, int &height);
static cpcc_string getMainMonitorResolutionAsText(void);
// portable / cross platform C function for Windows, OSX returns the computer name
static const cpcc_string getComputerName(void);
static const cpcc_string getUserName(void);
static cpcc_string& readProgramVersion(void);
#ifdef __APPLE__
static std::string readProgramVersionByPrincipalClass(const char *aClassName);
static std::string getBundleID(void);
#if TARGET_OS_IPHONE != 1
static std::string getBundleIDfromAppName(const char *aAppName);
#endif
// Determine Whether Your App Is Running as a Translated Binary under MacOS Rosetta
// https://developer.apple.com/documentation/apple_silicon/about_the_rosetta_translation_environment
// Return: 0 = native run, 1 = Rosetta, -1 = Don't know, error occured
static int appRunsUnderRosetta(void);
#endif
static cpcc_string getPreferredLanguage(void);
#ifdef _WIN32
static const HWND getWindowHandleOfProgram(const TCHAR *aClassName);
#endif
};
// /////////////////////////////
//
// class cpccOS implementation
//
// /////////////////////////////
inline cpcc_string cpccOS::getMainMonitorResolutionAsText(void)
{
cpcc_stringstream result;
int mw, mh;
getMainMonitorResolution(mw, mh);
result << mw;
result << _T("x");
result << mh;
return result.str();
}
#ifdef __APPLE__
inline void cpccOS::util_RemoveSuffixFromString(std::string &str, const char* suffixToRemove)
{
// std::cout << "to remove " << suffixToRemove << " from " << str << std::endl;
const size_t pos = str.rfind(suffixToRemove);
if (pos == std::string::npos)
return; // not found
// std::cout << "Found " << suffixToRemove << " in " << str << std::endl;
if (pos != str.length() - strlen(suffixToRemove) )
return; // text found but not at the end of the string
str.erase(pos , std::string::npos );
}
inline int cpccOS::appRunsUnderRosetta(void)
{
/* On Apple silicon, a universal binary may run either natively or as a translated binary.
The system runs the native version whenever possible, but the user might opt to run the
code using Rosetta to support older plug-ins.
*/
int ret = 0;
size_t size = sizeof(ret);
if (sysctlbyname("sysctl.proc_translated", &ret, &size, NULL, 0) == -1)
{
if (errno == ENOENT)
return 0;
return -1;
}
return ret;
}
#endif
#ifdef _WIN32
inline const HWND cpccOS::getWindowHandleOfProgram(const TCHAR* aClassName)
{
// e.g. FindWindow("Notepad", "Untitled - Notepad");
return FindWindow(aClassName, NULL);
}
#endif