-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoll_darwin.go
140 lines (125 loc) · 3.94 KB
/
poll_darwin.go
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
// Copyright ©2023 Dan Kortschak. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"encoding/json"
"fmt"
"time"
"unsafe"
// For get_active_app.applescript
_ "embed"
"golang.org/x/sys/execabs"
watcher "github.com/kortschak/dex/cmd/watcher/api"
)
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework CoreFoundation -framework CoreGraphics -framework Cocoa
#include <CoreFoundation/CoreFoundation.h>
#include <CoreGraphics/CGWindow.h>
#include <Cocoa/Cocoa.h>
struct details {
int wid;
int pid;
const char* name;
const char* window;
double secondsAgo;
bool isAsleep;
bool isLocked;
bool onConsole;
};
void activeWindow(struct details *d)
{
d->secondsAgo = CGEventSourceSecondsSinceLastEventType(kCGEventSourceStateHIDSystemState, kCGAnyInputEventType);
d->isAsleep = CGDisplayIsAsleep(CGMainDisplayID());
CFDictionaryRef session = CGSessionCopyCurrentDictionary();
if (session != NULL) {
d->isLocked = [[(id)session objectForKey:(__bridge NSString *)CFSTR("CGSSessionScreenIsLocked")] boolValue];
d->onConsole = [[(id)session objectForKey:(NSString *)kCGSessionOnConsoleKey] boolValue];
CFRelease(session);
}
NSArray *windows = (NSArray *)CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements|kCGWindowListOptionOnScreenOnly,kCGNullWindowID);
for(NSDictionary *window in windows){
int pid = [[window objectForKey:(NSString *)kCGWindowOwnerPID] intValue];
if (pid == d->pid) {
@autoreleasepool {
d->wid = [[window objectForKey:(NSString *)kCGWindowNumber] intValue];
const char *ownerName = [[window objectForKey:(NSString *)kCGWindowOwnerName] UTF8String];
if (ownerName != NULL) {
d->name = strdup(ownerName);
}
const char *windowName = [[window objectForKey:(NSString *)kCGWindowName] UTF8String];
if (windowName != NULL) {
d->window = strdup(windowName);
}
}
if (windows != NULL) {
// An overabundance of caution; we must have been non-null to get here.
CFRelease(windows);
}
return;
}
}
d->pid = -1;
return;
}
*/
import "C"
func init() {
detailers[macosDetailer{}.strategy()] = newMacosDetailer
}
func newMacosDetailer() (detailer, error) { return macosDetailer{}, nil }
type macosDetailer struct{}
func (macosDetailer) strategy() string { return "macos" }
func (macosDetailer) details() (watcher.Details, error) {
// Do the fallback first because MacOS is garbage.
pid, name, windowName, err := activeWindowDetails()
t := C.struct_details{pid: C.int(pid)}
C.activeWindow(&t)
active := watcher.Details{
WindowID: int(t.wid), // Note that this is not reliable. MacOS is garbage.
ProcessID: pid,
Name: name,
Class: C.GoString(t.name),
WindowName: C.GoString(t.window),
LastInput: time.Now().Add(time.Duration(float64(t.secondsAgo) * float64(-time.Second))),
Locked: bool(t.isLocked || t.isAsleep || !t.onConsole),
}
if t.name != nil {
C.free(unsafe.Pointer(t.name))
}
if t.window != nil {
C.free(unsafe.Pointer(t.window))
}
if active.WindowName == "" {
active.WindowName = windowName
}
if err != nil {
// Could not get window exact details name, but return what we have.
err = warning{fmt.Errorf("failed to obtain some details: missing application or window name: %w", err)}
}
return active, err
}
//go:embed get_active_app.applescript
var getActiveApp string
func activeWindowDetails() (pid int, name, window string, err error) {
cmd := execabs.Command("osascript", "-e", getActiveApp)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
return 0, "", "", fmt.Errorf("%w: %s", err, &stderr)
}
var active struct {
PID int `json:"pid"`
Name string `json:"name"`
Window string `json:"window"`
}
err = json.Unmarshal(stdout.Bytes(), &active)
if err != nil {
err = fmt.Errorf("%w: %s", err, stdout.Bytes())
}
return active.PID, active.Name, active.Window, err
}