Skip to content

Instantly share code, notes, and snippets.

@fukusaka
Created May 26, 2010 14:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fukusaka/414559 to your computer and use it in GitHub Desktop.
Save fukusaka/414559 to your computer and use it in GitHub Desktop.
#import <stdio.h>
#import <stdlib.h>
#import <unistd.h>
#import <Cocoa/Cocoa.h>
#import "CGSPrivate.h"
char *param_window;
char *param;
int show_pid;
int show_geometry;
int show_system;
void
list_window()
{
CGSConnection cid = _CGSDefaultConnection();
CGSWindow wid = kCGNullWindowID;
CFArrayRef windowList
= CGWindowListCopyWindowInfo(kCGWindowListOptionAll,
wid);
for (NSMutableDictionary* entry in (NSArray*)windowList)
{
NSInteger windowNumber
= [[entry objectForKey:(id)kCGWindowNumber] integerValue];
NSString* ownerName
= [entry objectForKey:(id)kCGWindowOwnerName];
NSInteger ownerPID
= [[entry objectForKey:(id)kCGWindowOwnerPID] integerValue];
NSInteger workspace
= [[entry objectForKey:(id)kCGWindowWorkspace] integerValue];
CGRect rect;
CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)[entry objectForKey:(id)kCGWindowBounds], &rect);
if (!show_system && workspace == 0) continue;
printf ("0x%08x %2d ",
(int)windowNumber,
(int)workspace);
if (show_pid)
printf ("%-8d ",
(int)ownerPID);
if (show_geometry)
printf ("%-6g %-6g %-6g %-6g ",
rect.origin.x, rect.origin.y,
rect.size.width, rect.size.height);
printf ("%s\n",
[ownerName UTF8String]);
}
CFRelease(windowList);
}
void
current_desktop()
{
CGSConnection cid = _CGSDefaultConnection();
int workspace = 0;
CGSGetWorkspace(cid,&workspace);
printf("%d\n",workspace);
}
int
move_workspace(int workspace)
{
CGSConnection cid = _CGSDefaultConnection();
int handle = 0;
CGSTransitionSpec spec
= { 0, CGSCube, CGSLeft, 0, NULL};
CGSNewTransition(cid, &spec, &handle);
CGSSetWorkspace(cid, workspace);
CGSInvokeTransition(cid, handle, 1);
sleep(1);
CGSReleaseTransition(cid,handle);
}
int
move_workspace_window(int wid_,int workspace)
{
CGSConnection cid = _CGSDefaultConnection();
CGSWindow wid = wid_;
CGSMoveWorkspaceWindowList(cid, &wid, 1, workspace);
}
int
main(int argc, char * argv[])
{
int opt;
int action = '?';
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
while ((opt = getopt(argc,argv,"dlr:t:pGs")) != -1) {
switch (opt) {
case 'd': case 'l':
action = opt;
break;
case 'r':
param_window = optarg;
break;
case 't':
param = optarg;
move_workspace_window(strtol(param_window,NULL,0),
strtol(param,NULL,0));
break;
case 'p':
show_pid = 1;
break;
case 'G':
show_geometry = 1;
break;
case 's':
show_system = 1;
break;
case '?':
default:
action = '?';
break;
}
}
switch (action) {
case 'd':
current_desktop();
break;
case 'l':
list_window();
break;
case '?':
fprintf(stderr,"cgsutils -d \n");
fprintf(stderr,"cgsutils -l -spG\n");
fprintf(stderr,"cgsutils -r wid -t workspace\n");
break;
}
[pool drain];
return 0;
}
/*
* Local Variables:
* compile-command: "gcc -framework Cocoa -o cgsutils cgsutils.m"
* End:
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment