Mercurial > minori
annotate dep/animone/src/util/osx.cc @ 268:382b50754fe4
dep/animone: make osx code a bit less hacky
it would be nice if macos actually provided a real API for getting
window titles (outside of the accessibility api). the accessibility
API is a real mess to work with; the user has to give permission to
access it under newer versions.
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Fri, 12 Apr 2024 05:21:45 -0400 |
parents | 862d0d8619f6 |
children |
rev | line source |
---|---|
258 | 1 #include "animone/util/osx.h" |
2 | |
3 #include <memory> | |
4 #include <string> | |
5 | |
6 #include <libproc.h> | |
7 #include <sys/sysctl.h> | |
8 | |
9 namespace animone::internal::osx::util { | |
10 | |
268
382b50754fe4
dep/animone: make osx code a bit less hacky
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
11 static bool GetProcessNameFromProcPidPath(pid_t pid, std::string& result) { |
382b50754fe4
dep/animone: make osx code a bit less hacky
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
12 result.assign(PROC_PIDPATHINFO_MAXSIZE, '\0'); |
258 | 13 |
268
382b50754fe4
dep/animone: make osx code a bit less hacky
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
14 int ret = proc_pidpath(pid, result.data(), result.size() * sizeof(char)); |
382b50754fe4
dep/animone: make osx code a bit less hacky
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
15 if (ret <= 0) |
258 | 16 return false; |
17 | |
268
382b50754fe4
dep/animone: make osx code a bit less hacky
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
18 /* find the last slash, if there's none, we're done here */ |
382b50754fe4
dep/animone: make osx code a bit less hacky
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
19 size_t last_slash = result.rfind('/'); |
382b50754fe4
dep/animone: make osx code a bit less hacky
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
20 if (last_slash == std::string::npos) |
382b50754fe4
dep/animone: make osx code a bit less hacky
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
21 return true; |
258 | 22 |
268
382b50754fe4
dep/animone: make osx code a bit less hacky
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
23 result.erase(0, last_slash + 1); |
258 | 24 return true; |
25 } | |
26 | |
268
382b50754fe4
dep/animone: make osx code a bit less hacky
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
27 static bool GetProcessNameFromProcName(pid_t pid, std::string& result) { |
382b50754fe4
dep/animone: make osx code a bit less hacky
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
28 result.assign(2 * MAXCOMLEN, '\0'); |
258 | 29 |
30 int size = proc_name(pid, &result.front(), result.length()); | |
268
382b50754fe4
dep/animone: make osx code a bit less hacky
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
31 |
382b50754fe4
dep/animone: make osx code a bit less hacky
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
32 /* if size is MAXCOMLEN or 2 * MAXCOMLEN, assume |
382b50754fe4
dep/animone: make osx code a bit less hacky
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
33 * this method won't work and our result is truncated */ |
382b50754fe4
dep/animone: make osx code a bit less hacky
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
34 if (size <= 0 || size == MAXCOMLEN || size == 2 * MAXCOMLEN) |
258 | 35 return false; |
36 | |
37 result.resize(size); | |
38 return true; | |
39 } | |
40 | |
41 bool GetProcessName(pid_t pid, std::string& result) { | |
268
382b50754fe4
dep/animone: make osx code a bit less hacky
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
42 if (GetProcessNameFromProcName(pid, result)) |
258 | 43 return true; |
44 | |
268
382b50754fe4
dep/animone: make osx code a bit less hacky
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
45 if (GetProcessNameFromProcPidPath(pid, result)) |
258 | 46 return true; |
47 | |
48 return false; | |
49 } | |
50 | |
51 } // namespace animone::internal::osx::util |