Exemplo n.º 1
0
static CGSSurfaceID attach_gl_context_to_window(CGLContextObj glCtx,
   CGSWindowID wid, int *width, int *height)
{
    CFArrayRef wins;
    CFDictionaryRef win, bnd;
    GLint params = 0;
    Float64 w = 0, h = 0;
    CGSSurfaceID sid = 0;
    CGSConnectionID cid = CGSMainConnectionID();

    printf("cid:%d wid:%d\n", cid, wid);
 
    /* determine window size */
    /* FIXME/TODO - CGWindowListCopyWindowInfo was introduced on OSX 10.5, 
     * find alternative for lower versions. */
    wins = CGWindowListCopyWindowInfo(kCGWindowListOptionIncludingWindow, wid); /* expect one result only */
    win = (CFDictionaryRef)CFArrayGetValueAtIndex(wins, 0);
    bnd = (CFDictionaryRef)CFDictionaryGetValue(win, kCGWindowBounds);
    CFNumberGetValue((CFNumberRef)CFDictionaryGetValue((CFDictionaryRef)bnd, CFSTR("Width")),
       kCFNumberFloat64Type, &w);
    CFNumberGetValue((CFNumberRef)CFDictionaryGetValue(bnd, CFSTR("Height")),
       kCFNumberFloat64Type, &h);
    CFRelease(wins);
 
    /* create a surface. */
    if(CGSAddSurface(cid, wid, &sid) != kCGErrorSuccess)
    {
       printf("ERR: no surface\n");
    }
    printf("sid:%d\n", sid);
 
    /* set surface size, and order it frontmost */
    if(CGSSetSurfaceBounds(cid, wid, sid, CGRectMake(0, 0, w, h)) != kCGErrorSuccess)
       printf("ERR: cant set bounds\n");
    if(CGSOrderSurface(cid, wid, sid, 1, 0) != kCGErrorSuccess)
       printf("ERR: cant order front\n");
 
    /* attach context to the surface */
    if(CGLSetSurface(glCtx, cid, wid, sid) != kCGErrorSuccess)
    {
       printf("ERR: cant set surface\n");
    }
 
    /* check drawable */
    CGLGetParameter(glCtx, kCGLCPHasDrawable, &params);
    if(params != 1)
    {
       printf("ERR: no drawable\n");
    }
 
    *width  = (int)w;
    *height = (int)h;

    return sid;
}
Exemplo n.º 2
0
block_t *screen_Capture(demux_t *p_demux)
{
    demux_sys_t *p_sys = p_demux->p_sys;
    screen_data_t *p_data = (screen_data_t *)p_sys->p_data;
    block_t *p_block;
    CGRect capture_rect;
    CGImageRef image;

    /* forward cursor location */
    CGPoint cursor_pos;

    CGEventRef event = CGEventCreate(NULL);
    cursor_pos = CGEventGetLocation(event);
    CFRelease(event);

    cursor_pos.x -= p_data->screen_left;
    cursor_pos.y -= p_data->screen_top;

    if (p_sys->b_follow_mouse)
        FollowMouse(p_sys, cursor_pos.x, cursor_pos.y);

    capture_rect.origin.x = p_sys->i_left;
    capture_rect.origin.y = p_sys->i_top;
    capture_rect.size.width = p_data->width;
    capture_rect.size.height = p_data->height;

    /* fetch image data */
    image = CGDisplayCreateImageForRect(p_data->display_id, capture_rect);
    if (!image) {
        msg_Warn(p_demux, "no image!");
        return NULL;
    }

    /* create offscreen context */
    if (!p_data->offscreen_context) {
        CGColorSpaceRef colorspace;

        colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);

        p_data->offscreen_bitmap_size = p_sys->fmt.video.i_width * p_sys->fmt.video.i_height * 4;
        p_data->offscreen_bitmap = calloc(1, p_data->offscreen_bitmap_size);
        if (p_data->offscreen_bitmap == NULL) {
            msg_Warn(p_demux, "can't allocate offscreen bitmap");
            CFRelease(image);
            return NULL;
        }

        p_data->offscreen_context = CGBitmapContextCreate(p_data->offscreen_bitmap, p_sys->fmt.video.i_width, p_sys->fmt.video.i_height, 8, p_sys->fmt.video.i_width * 4, colorspace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
        if (!p_data->offscreen_context) {
            msg_Warn(p_demux, "can't create offscreen bitmap context");
            CFRelease(image);
            return NULL;
        }

        CGColorSpaceRelease(colorspace);

        p_data->offscreen_rect = CGRectMake(0, 0, p_sys->fmt.video.i_width, p_sys->fmt.video.i_height);
    }

    /* fetch cursor image */
    CGImageRef cursor_image;
    int cid = CGSMainConnectionID();
    CGPoint outHotSpot;
    cursor_image = CGSCreateRegisteredCursorImage(cid, (char *)"com.apple.coregraphics.GlobalCurrent", &outHotSpot);

    /* draw screen image and cursor image */
    CGRect cursor_rect;
    cursor_rect.size.width = CGImageGetWidth(cursor_image);
    cursor_rect.size.height = CGImageGetHeight(cursor_image);
    cursor_rect.origin.x = cursor_pos.x - p_sys->i_left - outHotSpot.x;
    cursor_rect.origin.y = p_data->offscreen_rect.size.height
                           - (cursor_pos.y + cursor_rect.size.height - p_sys->i_top - outHotSpot.y);

    CGContextDrawImage(p_data->offscreen_context, p_data->offscreen_rect, image);
    CGContextDrawImage(p_data->offscreen_context, cursor_rect, cursor_image);

    /* build block */
    p_block = block_Alloc(p_data->offscreen_bitmap_size);
    if (!p_block) {
        msg_Warn(p_demux, "can't get block");
        CFRelease(image);
        return NULL;
    }

    memmove(p_block->p_buffer, p_data->offscreen_bitmap, p_data->offscreen_bitmap_size);

    CFRelease(image);

    return p_block;
}
Exemplo n.º 3
0
int main(int argc, const char *argv[]) {
	const char *levels[] = {"Base", "Minimum", "Desktop", "Backstop", "Normal", "Floating", "TornOffMenu", "Dock", "MainMenu", "Status", "ModalPanel", "PopUpMenu", "Dragging", "ScreenSaver", "Maximum", "Overlay", "Help", "Utility", "DesktopIcon", "Cursor", "AssistiveTechHigh"};
	if (argc < 2) {
usage:
		fprintf(stderr, "usage:  %s [-s <level>] <wid>\n"
				"Levels: Base Minimum Desktop Backstop Normal Floating TornOffMenu Dock MainMenu Status ModalPanel PopUpMenu Dragging ScreenSaver Maximum Overlay Help Utility DesktopIcon Cursor AssistiveTechHigh\n"
				, argv[0]);
		return 1;
	}

	CGSConnectionID cid = CGSMainConnectionID();
//	CGSWindowID wid = (int)strtol(argv[1], NULL, 10);
#if 0
	CGSConnectionID owner = 0;
	CGSSharingState sh;
	if (!((CGSGetWindowOwner(cid, wid, &owner) == 0 && owner == cid) || (CGSGetWindowSharingState(cid, wid, &sh) == 0 && sh == kCGSSharingReadWrite))) {
#endif
		size_t len = 0;
		struct kinfo_proc *r = NULL;
		int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
		if (sysctl(name, 3, NULL, &len, NULL, 0) || ((r = malloc(len)) == NULL) || sysctl(name, 3, r, &len, NULL, 0))
			err(1, NULL);
		size_t c = len / sizeof(struct kinfo_proc);
		struct kinfo_proc *k = r;
		while (c--) {
			if (strcmp(k->kp_proc.p_comm, "Dock") == 0)
				kill(k->kp_proc.p_pid, SIGKILL);
			k++;
		}
#if 0
	}
#endif

	CGSSetUniversalOwner(cid);

	CGWindowLevel level = 0;
	if ((strcmp(argv[1], "-s") == 0) && (argc > 3)) {
		char *endptr;
		level = (int)strtol(argv[2], &endptr, 10);
		if (endptr == argv[2]) {
			unsigned int i = 0;
			do {
				if (strcasecmp(argv[2], levels[i]) == 0) {
					level = CGWindowLevelForKey(i);
					break;
				}
				i += 1;
			} while (i < sizeof(levels)/sizeof(levels[0]));
			if (i == sizeof(levels)/sizeof(levels[0])) {
				fprintf(stderr, "%s: Unknown level\n", argv[2]);
				goto usage;
			}
		}
		for (int i = 3; i < argc; i++) {
			if (CGSSetWindowLevel(cid, (CGSWindowID)strtol(argv[i], NULL, 10), level) != noErr)
				warnx("%d: failed", argv[i]);
		}
	} else {
		for (int j = 1; j < argc; j++) {
			CGSGetWindowLevel(cid, (CGSWindowID)strtol(argv[j], NULL, 10), &level);
			unsigned int i = 0;
			do { // todo: option to just print number
				if (level == CGWindowLevelForKey(i)) {
					puts(levels[i]);
					break;
				}
			} while (++i < sizeof(levels)/sizeof(levels[0]));
			// TODO: if multiple wids, format |wid: level|
			if (i == sizeof(levels)/sizeof(levels[0]))
				printf("%d\n", level);
		}
	}
	return 0;
}