登录用户
- 获取登录用户名
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 |
std::string get_login_name() { std::string res; char* name = new char[512]; auto len = getlogin_r(name, 512); #ifdef DEBUG if (strcmp(name, "root") == 0) { const char* user_env = getenv("USER"); if (user_env) { std::strcpy(name, user_env); } else { std::strcpy(name, "enlink"); } } #endif res = name; delete[] name; return res; } std::string get_login_path() { auto name = get_login_name(); if (name.empty()) { return ""; } return std::string("/Users/").append(name); } |
uuid
- 需要获取硬件
UUID
,用这个UUID
拼出目标文件名。具体:
1 2 3 4 |
/Users/aet_test + /Library/Preferences/ByHost/com.apple.screensaver. + UUID + .plist |
- 获取
- 通过
IOKit
库查询“IOPlatformExpertDevice”
服务,并从中读取“IOPlatformUUID”
属性,获取的就是硬件的UUID
- 这个
UUID
是针对Mac
计算机的物理硬件的唯一标识符,通常与主板相关联
无论重新安装操作系统或更改其他硬件组件,这个UUID
都保持不变
- 通过
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 |
std::string get_hw_uuid() { std::string res; @autoreleasepool { io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice")); if (platformExpert) { CFTypeRef serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert, CFSTR("IOPlatformUUID"), kCFAllocatorDefault, 0); if (serialNumberAsCFString) { CFStringRef tmpStrRef = (CFStringRef)serialNumberAsCFString; const char* tmp_str = CFStringGetCStringPtr(tmpStrRef, kCFStringEncodingUTF8); if (tmp_str) { res = tmp_str; } else { CFIndex len = CFStringGetLength(tmpStrRef); CFIndex max = CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8) + 1; char* buf = new char[max]; if (CFStringGetCString(tmpStrRef, buf, max, kCFStringEncodingUTF8)) { res = buf; } delete[] buf; } CFRelease(serialNumberAsCFString); } IOObjectRelease(platformExpert); } } return res; } |
解析
1 2 3 4 5 6 7 8 9 |
boost::property_tree::ptree tree; do { if (parsePlist(tmp_path, tree) != Status::STATUS_SUCC) { break; } auto idle_time = tree.get<int>("idleTime", 0); return (idle_time != 0); } while (false); |
字段
idleTime
- 屏保启动时间,为
0
时,表示永不
moduleDict
moduleName
- 屏幕保护程序的名称
path
- 屏幕保护程序的路径
type
屏保类型
- 该文件记录了当前屏保类型,根据观察,可能每个屏保类型对应的不太全
1 2 3 4 |
/Users/aet_test + /Library/Preferences/ByHost/com.apple.screensaver.iLifeSlideShows. + UUID + .plist |
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
bool is_screen_protected() { auto path = get_login_path(); if (path.empty()) { return false; } auto uuid = get_hw_uuid(); if (uuid.empty()) { return false; } auto tmp_path = path.append("/Library/Preferences/ByHost/com.apple.screensaver.").append(uuid).append(".plist"); boost::property_tree::ptree tree; do { if (parsePlist(tmp_path, tree) != Status::STATUS_SUCC) { break; } auto idle_time = tree.get<int>("idleTime", 0); return (idle_time != 0); } while (false); return false; } |
本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ Macos编译x86_64相关二09/05
- ♥ Cef:介绍06/29
- ♥ Macos开发问题:aarch64架构宏不识别06/25
- ♥ Macos服务相关03/27
- ♥ Visual Studio:内存泄露AddressSanitizer(跨平台)03/14
- ♥ Macos进入Recovery界面关闭SIP09/19