forked from atoxx/DockMarker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspaces.c
executable file
·93 lines (81 loc) · 2.5 KB
/
spaces.c
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
//
// spaces.c
// Change Space
//
// Created by Stephen Sykes on 30/8/11.
// Copyright (c) 2011 Switchstep. All rights reserved.
//
// Derived from https://gist.github.com/1129406
#include <unistd.h>
#include <CoreServices/CoreServices.h>
#include <ApplicationServices/ApplicationServices.h>
#include "spaces.h"
int get_space_id(void)
{
int space = -1;
CFArrayRef windows = CGWindowListCopyWindowInfo( kCGWindowListOptionOnScreenOnly, kCGNullWindowID );
CFIndex i, n;
for (i = 0, n = CFArrayGetCount(windows); i < n; i++) {
CFDictionaryRef windict = CFArrayGetValueAtIndex(windows, i);
CFNumberRef spacenum = CFDictionaryGetValue(windict, kCGWindowWorkspace);
if (spacenum) {
CFNumberGetValue(spacenum, kCFNumberIntType, &space);
}
}
CFRelease(windows);
return space;
}
int total_spaces(void)
{
int rows, cols;
CoreDockGetWorkspacesCount(&rows, &cols);
return cols;
}
void set_space_by_index(int space)
{
CFNotificationCenterRef nc = CFNotificationCenterGetDistributedCenter();
CFStringRef numstr = CFStringCreateWithFormat(NULL, nil, CFSTR("%d"), space);
CFNotificationCenterPostNotification(nc, CFSTR("com.apple.switchSpaces"), numstr, NULL, TRUE);
CFRelease(numstr);
}
int get_front_window_pid(void)
{
int pid;
CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
CFIndex i, n;
for (i = 0, n = CFArrayGetCount(windows); i < n; i++) {
CFDictionaryRef windict = CFArrayGetValueAtIndex(windows, i);
CFNumberRef layernum = CFDictionaryGetValue(windict, kCGWindowLayer);
CFNumberRef pidnum = CFDictionaryGetValue(windict, kCGWindowOwnerPID);
if (layernum && pidnum) {
int layer;
CFNumberGetValue(layernum, kCFNumberIntType, &layer);
if (layer == 0) {
CFNumberGetValue(pidnum, kCFNumberIntType, &pid);
CFRelease(windows);
return pid;
}
}
}
CFRelease(windows);
return -1;
}
int is_full_screen(void)
{
CFArrayRef windows = CGWindowListCopyWindowInfo( kCGWindowListOptionOnScreenOnly, kCGNullWindowID );
CFIndex i, n;
for (i = 0, n = CFArrayGetCount(windows); i < n; i++) {
CFDictionaryRef windict = CFArrayGetValueAtIndex(windows, i);
CFNumberRef layernum = CFDictionaryGetValue(windict, kCGWindowLayer);
if (layernum) {
int layer;
CFNumberGetValue(layernum, kCFNumberIntType, &layer);
if (layer == -1) {
CFRelease(windows);
return 1;
}
}
}
CFRelease(windows);
return 0;
}