ligius- / lenovo-backlight-control

Simple wrapper for Windows 8/10 to control the Thinkpad keyboard backlight
Apache License 2.0
40 stars 14 forks source link

TODO: make the backlight wake up after screen off #10

Closed peddy22 closed 2 years ago

peddy22 commented 2 years ago

Hi,

Thanks for the awesome script.

Did you ever find a fix or workaround for the issue described in the title (taken from the ahk file)

Thanks!

peddy22 commented 2 years ago

Quick and dirty (python in Windows)

import win32con
import win32api
import win32gui
import time
from ctypes import POINTER, windll, Structure, cast, CFUNCTYPE, c_int, c_uint, c_void_p, c_bool
from comtypes import GUID
from ctypes.wintypes import HANDLE, DWORD

PBT_POWERSETTINGCHANGE = 0x8013
GUID_MONITOR_POWER_ON = '{02731015-4510-4526-99E6-E5A17EBD1AEA}'

global firstRun
firstRun = 1

class POWERBROADCAST_SETTING(Structure):
    _fields_ = [("PowerSetting", GUID),
                ("DataLength", DWORD),
                ("Data", DWORD)]

def wndproc(hwnd, msg, wparam, lparam):
    global firstRun

    if msg == win32con.WM_POWERBROADCAST:

        if wparam == PBT_POWERSETTINGCHANGE:
            print('Power setting changed...')
            settings = cast(lparam, POINTER(POWERBROADCAST_SETTING)).contents
            power_setting = str(settings.PowerSetting)
            data_length = settings.DataLength
            data = settings.Data

            if power_setting == GUID_MONITOR_POWER_ON:

                if data == 1: 
                    print('Monitor on')
                    if firstRun == 1: 
                        print "first run, doing nothing"
                        firstRun = 0
                    else:

                        print "restarting AHK"
                        import subprocess
                        subprocess.Popen(["C:\Program Files\AutoHotkey\AutoHotkey.exe", "C:\Users\Arno\Documents\Programs\X230Backlight.ahk"], shell=False)

                if data == 0: print('Monitor off')

        return True

    return False

if __name__ == "__main__":
    print("*** STARTING ***")
    hinst = win32api.GetModuleHandle(None)
    wndclass = win32gui.WNDCLASS()
    wndclass.hInstance = hinst
    wndclass.lpszClassName = "testWindowClass"
    CMPFUNC = CFUNCTYPE(c_bool, c_int, c_uint, c_uint, c_void_p)
    wndproc_pointer = CMPFUNC(wndproc)
    wndclass.lpfnWndProc = {win32con.WM_POWERBROADCAST : wndproc_pointer}
    try:
        myWindowClass = win32gui.RegisterClass(wndclass)
        hwnd = win32gui.CreateWindowEx(win32con.WS_EX_LEFT,
                                     myWindowClass, 
                                     "testMsgWindow", 
                                     0, 
                                     0, 
                                     0, 
                                     win32con.CW_USEDEFAULT, 
                                     win32con.CW_USEDEFAULT, 
                                     0, 
                                     0, 
                                     hinst, 
                                     None)
    except Exception as e:
        print("Exception: %s" % str(e))

    if hwnd is None:
        print("hwnd is none!")
    else:
        print("hwnd: %s" % hwnd)

    guids_info = {
                    'GUID_MONITOR_POWER_ON' : GUID_MONITOR_POWER_ON,
                 }
    for name, guid_info in guids_info.items():
        result = windll.user32.RegisterPowerSettingNotification(HANDLE(hwnd), GUID(guid_info), DWORD(0))
        print('registering', name)
        print('result:', hex(result))
        print('lastError:', win32api.GetLastError())
        print()

    print('\nEntering loop')
    while True:
        win32gui.PumpWaitingMessages()
        time.sleep(1)
ligius- commented 2 years ago

Hi @peddy22 , thanks for the quick fix. I have not investigated this issue yet as I barely use my X230, but perhaps there is already a way to do it in AHK. For reference: https://www.autohotkey.com/boards/viewtopic.php?p=85239&sid=a31e2ba06dbc83b24c22a715e5cf27c3#p85239

peddy22 commented 2 years ago

any idea why it stops working after a screen-off?