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.
This commit is contained in:
Stefano Pigozzi 2012-09-16 20:53:04 +02:00
parent b946197243
commit f5de0aac96
8 changed files with 80 additions and 12 deletions

4
configure vendored
View File

@ -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'

View File

@ -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);

View File

@ -21,6 +21,7 @@
#import <OpenGL/OpenGL.h>
#import <QuartzCore/QuartzCore.h>
#import <CoreServices/CoreServices.h> // for CGDisplayHideCursor
#import <IOKit/pwr_mgt/IOPMLib.h>
#include <dlfcn.h>
#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 <NSWindowDelegate> {
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)

View File

@ -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

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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)