From f5de0aac96621b584bdb5fd1ca5916529d219b0a Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 16 Sep 2012 20:53:04 +0200 Subject: cocoa_common: use IOKit to perform power management This allows to remove the call to the deprecated `UpdateSystemActivity`. The additional benefit is power management is disabled only if the video is really playing. A paused video will not stop the system from idling. --- configure | 4 ++-- libvo/cocoa_common.h | 2 ++ libvo/cocoa_common.m | 52 +++++++++++++++++++++++++++++++++++++++++---------- libvo/gl_common.c | 2 ++ libvo/gl_common.h | 2 ++ libvo/vo_corevideo.m | 10 ++++++++++ libvo/vo_opengl.c | 10 ++++++++++ libvo/vo_opengl_old.c | 10 ++++++++++ 8 files changed, 80 insertions(+), 12 deletions(-) diff --git a/configure b/configure index a8753ec3b0..3af45d6798 100755 --- a/configure +++ b/configure @@ -1980,10 +1980,10 @@ int main(void) { } EOF _cocoa=no - cc_check -framework Cocoa -framework OpenGL && _cocoa=yes + cc_check -framework IOKit -framework Cocoa -framework OpenGL && _cocoa=yes fi if test "$_cocoa" = yes ; then - libs_mplayer="$libs_mplayer -framework Cocoa -framework OpenGL" + libs_mplayer="$libs_mplayer -framework IOKit -framework Cocoa -framework OpenGL" def_cocoa='#define CONFIG_COCOA 1' else def_cocoa='#undef CONFIG_COCOA' diff --git a/libvo/cocoa_common.h b/libvo/cocoa_common.h index a158a501fd..079e497441 100644 --- a/libvo/cocoa_common.h +++ b/libvo/cocoa_common.h @@ -41,6 +41,8 @@ void vo_cocoa_swap_buffers(struct vo *vo); int vo_cocoa_check_events(struct vo *vo); void vo_cocoa_fullscreen(struct vo *vo); void vo_cocoa_ontop(struct vo *vo); +void vo_cocoa_pause(struct vo *vo); +void vo_cocoa_resume(struct vo *vo); // returns an int to conform to the gl extensions from other platforms int vo_cocoa_swap_interval(int enabled); diff --git a/libvo/cocoa_common.m b/libvo/cocoa_common.m index e88d2d49ae..aba18140ba 100644 --- a/libvo/cocoa_common.m +++ b/libvo/cocoa_common.m @@ -21,6 +21,7 @@ #import #import #import // for CGDisplayHideCursor +#import #include #include "cocoa_common.h" @@ -62,6 +63,11 @@ @end #endif +// add power management assertion not available on OSX versions prior to 10.7 +#ifndef kIOPMAssertionTypePreventUserIdleDisplaySleep +#define kIOPMAssertionTypePreventUserIdleDisplaySleep CFSTR("PreventUserIdleDisplaySleep") +#endif + @interface GLMPlayerWindow : NSWindow { struct vo *_vo; } @@ -100,14 +106,14 @@ struct vo_cocoa_state { NSInteger windowed_window_level; NSInteger fullscreen_window_level; - int last_screensaver_update; - int display_cursor; int cursor_timer; int cursor_autohide_delay; bool did_resize; bool out_fs_resize; + + IOPMAssertionID power_mgmt_assertion; }; static int _instances = 0; @@ -129,6 +135,7 @@ static struct vo_cocoa_state *vo_cocoa_init_state(struct vo *vo) .out_fs_resize = NO, .display_cursor = 1, .cursor_autohide_delay = vo->opts->cursor_autohide_delay, + .power_mgmt_assertion = kIOPMNullAssertionID, }; return s; } @@ -158,6 +165,27 @@ void *vo_cocoa_glgetaddr(const char *s) return ret; } +static void enable_power_management(struct vo *vo) +{ + struct vo_cocoa_state *s = vo->cocoa; + if (!s->power_mgmt_assertion) return; + IOPMAssertionRelease(s->power_mgmt_assertion); + s->power_mgmt_assertion = kIOPMNullAssertionID; +} + +static void disable_power_management(struct vo *vo) +{ + struct vo_cocoa_state *s = vo->cocoa; + if (s->power_mgmt_assertion) return; + + CFStringRef assertion_type = kIOPMAssertionTypeNoDisplaySleep; + if (is_osx_version_at_least(10, 7, 0)) + assertion_type = kIOPMAssertionTypePreventUserIdleDisplaySleep; + + IOPMAssertionCreateWithName(assertion_type, kIOPMAssertionLevelOn, + CFSTR("org.mplayer2.power_mgmt"), &s->power_mgmt_assertion); +} + int vo_cocoa_init(struct vo *vo) { vo->cocoa = vo_cocoa_init_state(vo); @@ -166,6 +194,7 @@ int vo_cocoa_init(struct vo *vo) NSApplicationLoad(); NSApp = [NSApplication sharedApplication]; [NSApp setActivationPolicy: NSApplicationActivationPolicyRegular]; + disable_power_management(vo); return 1; } @@ -174,6 +203,7 @@ void vo_cocoa_uninit(struct vo *vo) { struct vo_cocoa_state *s = vo->cocoa; CGDisplayShowCursor(kCGDirectMainDisplay); + enable_power_management(vo); [NSApp setPresentationOptions:NSApplicationPresentationDefault]; [s->window release]; @@ -186,6 +216,16 @@ void vo_cocoa_uninit(struct vo *vo) _instances--; } +void vo_cocoa_pause(struct vo *vo) +{ + enable_power_management(vo); +} + +void vo_cocoa_resume(struct vo *vo) +{ + disable_power_management(vo); +} + static int current_screen_has_dock_or_menubar(struct vo *vo) { struct vo_cocoa_state *s = vo->cocoa; @@ -394,14 +434,6 @@ int vo_cocoa_check_events(struct vo *vo) s->cursor_timer = msCurTime; } - //update activity every 30 seconds to prevent - //screensaver from starting up. - if ((int)curTime - s->last_screensaver_update >= 30 || s->last_screensaver_update == 0) - { - UpdateSystemActivity(UsrActivity); - s->last_screensaver_update = (int)curTime; - } - event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSEventTrackingRunLoopMode dequeue:YES]; if (event == nil) diff --git a/libvo/gl_common.c b/libvo/gl_common.c index 43df55ea9b..fabd4e23d7 100644 --- a/libvo/gl_common.c +++ b/libvo/gl_common.c @@ -2492,6 +2492,8 @@ MPGLContext *mpgl_init(enum MPGLType type, struct vo *vo) ctx->fullscreen = cocoa_fullscreen; ctx->ontop = vo_cocoa_ontop; ctx->vo_init = vo_cocoa_init; + ctx->pause = vo_cocoa_pause; + ctx->resume = vo_cocoa_resume; ctx->vo_uninit = vo_cocoa_uninit; break; #endif diff --git a/libvo/gl_common.h b/libvo/gl_common.h index 7f9dc28083..4add21bb62 100644 --- a/libvo/gl_common.h +++ b/libvo/gl_common.h @@ -212,6 +212,8 @@ typedef struct MPGLContext { uint32_t d_height, uint32_t flags); // optional + void (*pause)(struct vo *vo); + void (*resume)(struct vo *vo); void (*ontop)(struct vo *vo); void (*border)(struct vo *vo); void (*update_xinerama_info)(struct vo *vo); diff --git a/libvo/vo_corevideo.m b/libvo/vo_corevideo.m index 19a9003b9e..0db161fd18 100644 --- a/libvo/vo_corevideo.m +++ b/libvo/vo_corevideo.m @@ -423,6 +423,16 @@ static int control(struct vo *vo, uint32_t request, void *data) case VOCTRL_ONTOP: p->mpglctx->ontop(vo); return VO_TRUE; + case VOCTRL_PAUSE: + if (!p->mpglctx->pause) + break; + p->mpglctx->pause(vo); + return VO_TRUE; + case VOCTRL_RESUME: + if (!p->mpglctx->resume) + break; + p->mpglctx->resume(vo); + return VO_TRUE; case VOCTRL_FULLSCREEN: p->mpglctx->fullscreen(vo); resize(vo, vo->dwidth, vo->dheight); diff --git a/libvo/vo_opengl.c b/libvo/vo_opengl.c index b6f2ddd811..7f400ce62c 100644 --- a/libvo/vo_opengl.c +++ b/libvo/vo_opengl.c @@ -1941,6 +1941,16 @@ static int control(struct vo *vo, uint32_t request, void *data) break; p->glctx->ontop(vo); return VO_TRUE; + case VOCTRL_PAUSE: + if (!p->glctx->pause) + break; + p->glctx->pause(vo); + return VO_TRUE; + case VOCTRL_RESUME: + if (!p->glctx->resume) + break; + p->glctx->resume(vo); + return VO_TRUE; case VOCTRL_FULLSCREEN: p->glctx->fullscreen(vo); resize(p); diff --git a/libvo/vo_opengl_old.c b/libvo/vo_opengl_old.c index d3a9c0e170..d302504846 100644 --- a/libvo/vo_opengl_old.c +++ b/libvo/vo_opengl_old.c @@ -1442,6 +1442,16 @@ static int control(struct vo *vo, uint32_t request, void *data) if (vo_doublebuffering) do_render(vo); return true; + case VOCTRL_PAUSE: + if (!p->glctx->pause) + break; + p->glctx->pause(vo); + return VO_TRUE; + case VOCTRL_RESUME: + if (!p->glctx->resume) + break; + p->glctx->resume(vo); + return VO_TRUE; case VOCTRL_SCREENSHOT: { struct voctrl_screenshot_args *args = data; if (args->full_window) -- cgit v1.2.3