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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1 #include "animone/util/osx.h"
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
3 #include <memory>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
4 #include <string>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 #include <libproc.h>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7 #include <sys/sysctl.h>
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
9 namespace animone::internal::osx::util {
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
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
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
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
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
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
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
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
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
24 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
25 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
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
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
29
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
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
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
35 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
36
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
37 result.resize(size);
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
38 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
39 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
40
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
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
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
43 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
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
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
46 return true;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
47
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
48 return false;
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
49 }
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
50
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
51 } // namespace animone::internal::osx::util