Skip to content

Commit ea34961

Browse files
committed
Experimenting with Window manipulation.
1 parent 29a293d commit ea34961

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/robotjs.cc

+41
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <node.h>
22
#include <v8.h>
3+
#include <vector>
34
#include "mouse.h"
45
#include "deadbeef_rand.h"
56
#include "screen.h"
@@ -125,6 +126,43 @@ Handle<Value> captureScreen(const Arguments& args)
125126
//return scope.Close(String::New("1"));
126127
}
127128

129+
Handle<Value> getWindows(const Arguments& args)
130+
{
131+
HandleScope scope;
132+
133+
CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
134+
CFIndex windowNum = CFArrayGetCount(windowList);
135+
136+
std::vector<Object> windows;
137+
Local<Object> obj = Object::New();
138+
139+
140+
for (int i = 0; i < (int)windowNum; i++)
141+
{
142+
CFDictionaryRef info = (CFDictionaryRef)CFArrayGetValueAtIndex(windowList, i);
143+
CFNumberRef currentPID = (CFNumberRef)CFDictionaryGetValue(info, kCGWindowOwnerPID);
144+
CFNumberRef currentWindowNumber = (CFNumberRef)CFDictionaryGetValue(info, kCGWindowNumber);
145+
CFStringRef currentTitle = (CFStringRef)CFDictionaryGetValue(info, kCGWindowName);
146+
CFIndex length = CFStringGetLength(currentTitle);
147+
CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
148+
char *buffer = (char *)malloc(maxSize);
149+
CFStringGetCString(currentTitle, buffer, maxSize, kCFStringEncodingUTF8);
150+
obj->Set(String::NewSymbol("title"), String::New(buffer));
151+
obj->Set(String::NewSymbol("id"), Number::New(*(int *)currentWindowNumber));
152+
printf(buffer);
153+
printf("\n");
154+
//windows.push_back(obj);
155+
156+
}
157+
158+
159+
160+
//return scope.Close(String::New("1"));
161+
162+
return scope.Close(String::New("1"));
163+
}
164+
165+
128166
void init(Handle<Object> target)
129167
{
130168
target->Set(String::NewSymbol("moveMouse"),
@@ -144,6 +182,9 @@ void init(Handle<Object> target)
144182

145183
target->Set(String::NewSymbol("captureScreen"),
146184
FunctionTemplate::New(captureScreen)->GetFunction());
185+
186+
target->Set(String::NewSymbol("getWindows"),
187+
FunctionTemplate::New(getWindows)->GetFunction());
147188
}
148189

149190
NODE_MODULE(robotjs, init)

0 commit comments

Comments
 (0)