Detect Windows 10 Night Light

If you have a question or need help, this is the place to be.
User avatar
kgschlosser
Site Admin
Posts: 4623
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Detect Windows 10 Night Light

Post by kgschlosser » Sat Sep 08, 2018 8:41 pm

here is a new copy of the script. I set the script so that the data was taken when the screens were bright. I can change it easily.

The 3 sets of 256 for the data each set was identical to each other. so i was able to shorten up the used data values.

Code: Select all

data = [0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4096, 4352, 4608, 4864, 5120, 5376, 5632, 5888, 6144, 6400, 6656, 6912, 7168, 7424, 7680, 7936, 8192, 8448, 8704, 8960, 9216, 9472, 9728, 9984, 10240, 10496, 10752, 11008, 11264, 11520, 11776, 12032, 12288, 12544, 12800, 13056, 13312, 13568, 13824, 14080, 14336, 14592, 14848, 15104, 15360, 15616, 15872, 16128, 16384, 16640, 16896, 17152, 17408, 17664, 17920, 18176, 18432, 18688, 18944, 19200, 19456, 19712, 19968, 20224, 20480, 20736, 20992, 21248, 21504, 21760, 22016, 22272, 22528, 22784, 23040, 23296, 23552, 23808, 24064, 24320, 24576, 24832, 25088, 25344, 25600, 25856, 26112, 26368, 26624, 26880, 27136, 27392, 27648, 27904, 28160, 28416, 28672, 28928, 29184, 29440, 29696, 29952, 30208, 30464, 30720, 30976, 31232, 31488, 31744, 32000, 32256, 32512, 32768, 33024, 33280, 33536, 33792, 34048, 34304, 34560, 34816, 35072, 35328, 35584, 35840, 36096, 36352, 36608, 36864, 37120, 37376, 37632, 37888, 38144, 38400, 38656, 38912, 39168, 39424, 39680, 39936, 40192, 40448, 40704, 40960, 41216, 41472, 41728, 41984, 42240, 42496, 42752, 43008, 43264, 43520, 43776, 44032, 44288, 44544, 44800, 45056, 45312, 45568, 45824, 46080, 46336, 46592, 46848, 47104, 47360, 47616, 47872, 48128, 48384, 48640, 48896, 49152, 49408, 49664, 49920, 50176, 50432, 50688, 50944, 51200, 51456, 51712, 51968, 52224, 52480, 52736, 52992, 53248, 53504, 53760, 54016, 54272, 54528, 54784, 55040, 55296, 55552, 55808, 56064, 56320, 56576, 56832, 57088, 57344, 57600, 57856, 58112, 58368, 58624, 58880, 59136, 59392, 59648, 59904, 60160, 60416, 60672, 60928, 61184, 61440, 61696, 61952, 62208, 62464, 62720, 62976, 63232, 63488, 63744, 64000, 64256, 64512, 64768, 65024, 65280]


import threading


try:
    event.set()
    t.join(1.0)
except NameError:
    event = threading.Event()
    
    def stop_thread(_):
        event.set()
        eg.Unbind('Main.OnClose', stop_thread)
        return False

    eg.Bind('Main.OnClose', stop_thread)


def run():
    import ctypes
    from ctypes.wintypes import WORD, HDC, BOOL
    
    user32 = ctypes.windll.User32
    gdi32 = ctypes.windll.Gdi32

    GetDC = user32.GetDC
    GetDC.restype = HDC

    GetDeviceGammaRamp = gdi32.GetDeviceGammaRamp
    GetDeviceGammaRamp.restype = BOOL

    hdc = GetDC(eg.messageReceiver.hwnd)
    old_settings = [data] * 3

    while not event.isSet():
        lpRamp = ((WORD * 256) * 3)()
        res = GetDeviceGammaRamp(hdc, lpRamp)
        if not res:
            raise ctypes.WinError()

        new_settings = []
        for i in range(3):
            color = lpRamp[i]
            color_values = []
            for j in range(256):
                color_values += [int(color[j])]
            new_settings += [color_values]

        if new_settings != old_settings:
            old_settings = new_settings[:]
            if new_settings == [data] * 3:
                state = 'Off'
            else:
                state = 'On'
            eg.TriggerEvent(prefix='MonitorDimming', suffix=state)
            
            if new_settings[0] == new_settings[1] == new_settings[2]:
                new_settings = new_settings[0] 
            
            print new_settings

        event.wait(0.3)
    event.clear()


t = threading.Thread(target=run)
t.daemon = True
t.start()
If you like the work I have been doing then feel free to Image

cableghost
Posts: 36
Joined: Thu Oct 10, 2013 9:43 pm

Re: Detect Windows 10 Night Light

Post by cableghost » Sat Sep 08, 2018 9:55 pm

I have only one/same primary screen.

Edit: no script error...nothing shows in log after execution.

User avatar
kgschlosser
Site Admin
Posts: 4623
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Detect Windows 10 Night Light

Post by kgschlosser » Sat Sep 08, 2018 11:19 pm

cableghost wrote:
Sat Sep 08, 2018 2:15 pm
though I have 6 displays connected.

i guess i am kind of lost at this one.

if you get no event when you run the script and it is daytime where you are then it is working properly. It should trigger an event when it dims the display.

if it is dark where you are and the display is already dimmed then i need to make a change
If you like the work I have been doing then feel free to Image

cableghost
Posts: 36
Joined: Thu Oct 10, 2013 9:43 pm

Re: Detect Windows 10 Night Light

Post by cableghost » Sun Sep 09, 2018 12:04 am

I had simply been testing the script as an Action. How should I be using this?

User avatar
kgschlosser
Site Admin
Posts: 4623
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Detect Windows 10 Night Light

Post by kgschlosser » Sun Sep 09, 2018 8:07 am

you only need to run the script once. It creates a thread and that new thread will loop until the end of time (or you close EG)
That is why i said to place it in your autostart group. this way it will get run only the one time when you start EventGhost. don't worry if you ran it more then once. it closes the thread that is running if there is one and then it creates a new one. so you shouldn't have a whole mess of threads running doing the same thing. That script does everything it needs to in terms of handling the thread and termination of the thread.
If you like the work I have been doing then feel free to Image

cableghost
Posts: 36
Joined: Thu Oct 10, 2013 9:43 pm

Re: Detect Windows 10 Night Light

Post by cableghost » Sun Sep 09, 2018 4:56 pm

kgschlosser wrote:
Sun Sep 09, 2018 8:07 am
you only need to run the script once. It creates a thread and that new thread will loop until the end of time (or you close EG)
That is why i said to place it in your autostart group.
Yep, got that...thank you. Pardon my ignorance...how do I react to it now? If something is to show in the log that I can use, there's nothing when Night Light is enabled/disabled. Ultimately, what I want to do is use the LIFX plugin to change the brightness of my office light based on whether Night Light is enabled or not, so I need something to react to.

User avatar
kgschlosser
Site Admin
Posts: 4623
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Detect Windows 10 Night Light

Post by kgschlosser » Sun Sep 09, 2018 9:10 pm

well this is the learning process. if your monitor dimmed and you got no event for it then it is not changing the gamma levels on your screen to dim the monitor and the information I found is incorrect. it stated that the night light feature changes the gamma levels to dim the screen. which in all honesty sounds kind of strange to me. why not just alter the brightness level of the screen instead.


There are several things that can be used to control screen brightness.

backlight
brightness
gamma
RGB gains.

Not all screens give the ability to change these things. so in order for the nightlight to work Microsoft may try to adjust each one and if it cannot then moves onto the next possibility. This is what I am thinking it does. It doesn't use just a single means. Because you get no event when the screens becomes bright or when it dims that means that the gamma levels never change. I have the script checking for a change every 300 milliseconds.

So I think I am going to have to expand the functionality of the script to check all of the above mentioned possibilities. and if it changes then the "night light" has been turned on.

After we figure out which one it is changing i have to reverse engineer the math it is using when it applies the new value. this way we know it is windows making the change and not something else.


If you can do me a favor tho. go into your monitors settings and write down what the present values are for all of the adjustments. and then when the screen dims go back in and write them down again. see if any have changed. there are several ways to change the values as well. it can be done directly on the monitor. or it can be done on the video card. or windows may be doing something to directx
If you like the work I have been doing then feel free to Image

cableghost
Posts: 36
Joined: Thu Oct 10, 2013 9:43 pm

Re: Detect Windows 10 Night Light

Post by cableghost » Mon Sep 10, 2018 12:45 am

kgschlosser wrote:
Sun Sep 09, 2018 9:10 pm
If you can do me a favor tho. go into your monitors settings and write down what the present values are for all of the adjustments. and then when the screen dims go back in and write them down again. see if any have changed. there are several ways to change the values as well. it can be done directly on the monitor. or it can be done on the video card. or windows may be doing something to directx
Note, my primary screen is a 4k TV. Reviewing windows limited display settings, video card control panel, and TV settings, I could not see anything change, i.e. brightness, color temp (6500k), contrast, etc. With respect to DirectX, dxdiag did not present any useful info for this. I'll keep digging.

User avatar
kgschlosser
Site Admin
Posts: 4623
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Detect Windows 10 Night Light

Post by kgschlosser » Mon Sep 10, 2018 1:50 am

Ok if it is a TV and you have no explicit drivers installed for the TV then it is not controlling the settings on the TV it's self. It is either making the changes at the video card or to the input stream to the video card.

There are several windows API entry points for controlling brightness. I can set up a script to check each of these entry points for a data change. This is the best I can do. If you want to simply be able to control lights based on the time of day (sunrise or sunset) the plugin that jachin mentioned will give you events that you can use to do this. Tho it does require an Internet connection to function properly. I do not know if windows internally does the calculations for sunrise and sunset to set the night light. I doubt it does. It probably obtains the information from a nist/ntp server.
If you like the work I have been doing then feel free to Image

User avatar
kgschlosser
Site Admin
Posts: 4623
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Detect Windows 10 Night Light

Post by kgschlosser » Mon Sep 10, 2018 2:00 am

I personally think that it would be pretty cool when the screen dims the light in the room would do so at the same time.

Because of the Use of the TV a project I ha EA a been working on that allows for controlling the settings in a monitor will not work.
For some reason TVs do not have the mechanisms in place to be able to adjust the brightnes/color/backlight over the HDMI cable. This is something that can be done with a monitor.

However. I did discover that you can install any driver for any monitor. As an example. I have 2 HPs and a Samsung. Somehow I managed to install an HP driver for the Samsung monitor and the Samsung driver on the HP monitor. And the control of the brightness and what have you still worked. I am going to have to poke around with it a bit and see if it expands the funxtionality. The HP driver allows more control options I am wondering if I use the HP driver on the Samsung if it will actually allow those things to be controlled.

If this is the case then there should be no reason why a monitor driver could not be installed for a TV to open up access to these controls. The basic electronics are the same in a TV as a monitor. So a TV should function in the same manner with respect to the control codes.
If you like the work I have been doing then feel free to Image

cableghost
Posts: 36
Joined: Thu Oct 10, 2013 9:43 pm

Re: Detect Windows 10 Night Light

Post by cableghost » Mon Sep 10, 2018 5:03 pm

Thanks for the effort. In the meantime, I found another solution via Android/Tasker/AutoRemote. I already use this combo related to session locking. I also use a Tasker sunset-related plug-in to adjust my display brightness, so I can simply send a respective message to EG via AutoRemote and adjust the office light accordingly.

Post Reply