[vlc-devel] [PATCH] Dim MacBook keyboard backlight during video playback on OSX

David Fuhrmann david.fuhrmann at gmail.com
Mon May 11 23:10:33 CEST 2015


> Am 11.05.2015 um 19:01 schrieb Maxime Mouchet <max at maxmouchet.com>:
> 
> Hello,
> 
> Here is the edited patch including the code fixes and the new behavior. Keyboard backlight is now turned off only during fullscreen playback. Lights will be dimmed when fullscreen is entered while a video is playing, and restored when fullscreen is exited.
> 
> Regards,
> Maxime
> 
> 
> This patch adds the option to turn off the keyboard backlight when
> a video is playing in fullscreen, to the OSX user interface.
> The option is off by default and it works better if the automatic
> keyboard backlight adjustment is disabled in System Preferences.
> 
> Smooth brightness transitions are implemented, and the "before-playing"
> level is saved to be restored on pause.
> If the user manually (i.e. using hardware keys) put backlight on,
> brightness level is not changed, in order to preserve his choice.
> ---
> modules/gui/macosx/CoreInteraction.m   |  23 +++++-
> modules/gui/macosx/KeyboardBacklight.h |  51 ++++++++++++
> modules/gui/macosx/KeyboardBacklight.m | 145 +++++++++++++++++++++++++++++++++
> modules/gui/macosx/Modules.am          |   3 +
> modules/gui/macosx/intf.m              |   9 ++
> modules/gui/macosx/macosx.m            |   4 +
> 6 files changed, 233 insertions(+), 2 deletions(-)
> create mode 100644 modules/gui/macosx/KeyboardBacklight.h
> create mode 100644 modules/gui/macosx/KeyboardBacklight.m
> 
> diff --git a/modules/gui/macosx/CoreInteraction.m b/modules/gui/macosx/CoreInteraction.m
> index 5d13a5f..ca214f8 100644
> --- a/modules/gui/macosx/CoreInteraction.m
> +++ b/modules/gui/macosx/CoreInteraction.m
> @@ -25,6 +25,7 @@
> #import "intf.h"
> #import "open.h"
> #import "playlist.h"
> +#import "KeyboardBacklight.h"
> #import <math.h>
> #import <vlc_playlist.h>
> #import <vlc_input.h>
> @@ -617,15 +618,33 @@ static VLCCoreInteraction *_o_sharedInstance = nil;
>     if (!p_intf)
>         return;
> 
> +    BOOL b_fs;
>     vout_thread_t *p_vout = getVoutForActiveWindow();
>     if (p_vout) {
> -        BOOL b_fs = var_ToggleBool(p_vout, "fullscreen");
> +        b_fs = var_ToggleBool(p_vout, "fullscreen");
>         var_SetBool(pl_Get(p_intf), "fullscreen", b_fs);
>         vlc_object_release(p_vout);
>     } else { // e.g. lion fullscreen toggle
> -        BOOL b_fs = var_ToggleBool(pl_Get(p_intf), "fullscreen");
> +        b_fs = var_ToggleBool(pl_Get(p_intf), "fullscreen");
>         [[[VLCMain sharedInstance] voutController] setFullscreen:b_fs forWindow:nil withAnimation:YES];
>     }
> +
> +    input_thread_t *p_input = pl_CurrentInput(VLCIntf);
> +    if (p_input) {
> +        BOOL b_vplay = (var_GetInteger(p_input, "state") == PLAYING_S);
> +
> +        // Dim keyboard backlight if we're going to fullscreen and a video is playing.
> +        if (var_InheritBool(VLCIntf, "macosx-dim-keyboard") && b_fs && b_vplay) {
> +            [NSThread detachNewThreadSelector:@selector(lightsDown) toTarget:[KeyboardBacklight sharedInstance] withObject:nil];
> +        }
> +
> +        // Restore keyboard backlight if we're exiting fullscreen.
> +        if (var_InheritBool(VLCIntf, "macosx-dim-keyboard") && !b_fs) {
> +            [NSThread detachNewThreadSelector:@selector(lightsUp) toTarget:[KeyboardBacklight sharedInstance] withObject:nil];
> +        }
> +
> +        vlc_object_release(p_input);
> +    }
> }
> 
> #pragma mark - uncommon stuff
> diff --git a/modules/gui/macosx/KeyboardBacklight.h b/modules/gui/macosx/KeyboardBacklight.h
> new file mode 100644
> index 0000000..6f2ca1c
> --- /dev/null
> +++ b/modules/gui/macosx/KeyboardBacklight.h
> @@ -0,0 +1,51 @@
> +/*****************************************************************************
> + * KeyboardBacklight.h: MacBook keyboard backlight control for VLC
> + *****************************************************************************
> + * Copyright (C) 2015 VLC authors and VideoLAN
> + *
> + *
> + * Authors: Maxime Mouchet <max at maxmouchet.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
> + *****************************************************************************/
> +
> +#import <Foundation/Foundation.h>
> +#import <IOKit/IOKitLib.h>
> +
> +enum {
> +    kGetSensorReadingID = 0, // getSensorReading(int *, int *)
> +    kGetLEDBrightnessID = 1, // getLEDBrightness(int, int *)
> +    kSetLEDBrightnessID = 2, // setLEDBrightness(int, int, int *)
> +    kSetLEDFadeID = 3        // setLEDFade(int, int, int, int *)
> +};
> +
> + at interface KeyboardBacklight : NSObject
> +
> ++ (instancetype)sharedInstance;
> +
> +/*!
> + *  Smoothly turn on backlight on MacBook keyboard.
> + *
> + *  Try to restore the previous brightness level. If the user has put
> + *  the backlight on manually (using hardware keys), nothing will be done.
> + */
> +- (void)lightsUp;
> +
> +/*!
> + *  Smoothly turn off backlight on MacBook keyboard.
> + */
> +- (void)lightsDown;
> +
> + at end
> diff --git a/modules/gui/macosx/KeyboardBacklight.m b/modules/gui/macosx/KeyboardBacklight.m
> new file mode 100644
> index 0000000..739998c
> --- /dev/null
> +++ b/modules/gui/macosx/KeyboardBacklight.m
> @@ -0,0 +1,145 @@
> +/*****************************************************************************
> + * KeyboardBacklight.m: MacBook keyboard backlight control for VLC
> + *****************************************************************************
> + * Copyright (C) 2015 VLC authors and VideoLAN
> + *
> + *
> + * Authors: Maxime Mouchet <max at maxmouchet.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
> + *****************************************************************************/
> +
> +#import "KeyboardBacklight.h"
> +
> + at implementation KeyboardBacklight {
> +    io_connect_t dataPort;
> +    float lastBrightnessLevel;
> +}
> +
> ++ (instancetype)sharedInstance {
> +    static id sharedInstance = nil;
> +
> +    static dispatch_once_t onceToken;
> +    dispatch_once(&onceToken, ^{
> +        sharedInstance = [[[self class] alloc] init];
> +    });
> +
> +    return sharedInstance;
> +}

Hi,

As an additional remark: Why using a singleton pattern? I think its really not needed for this class.
I know the singleton pattern is misused a lot in the macosx codebase, but I want to get rid of them soon, if possible. So I would prefer if we can prevent introducing new singletons.

Best,
David




More information about the vlc-devel mailing list