Skip to content

Instantly share code, notes, and snippets.

@Kethen
Created March 28, 2023 12:36
Show Gist options
  • Select an option

  • Save Kethen/93c26675b2da5484d8057e7fbaeb517b to your computer and use it in GitHub Desktop.

Select an option

Save Kethen/93c26675b2da5484d8057e7fbaeb517b to your computer and use it in GitHub Desktop.

Revisions

  1. Kethen created this gist Mar 28, 2023.
    121 changes: 121 additions & 0 deletions hidl_perf_boost.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,121 @@
    #!/usr/bin/python3
    import dbus
    from dbus.mainloop.glib import DBusGMainLoop
    from gi.repository import GLib
    import subprocess
    import gbinder
    import time
    from threading import Thread

    match_string = "type='signal',member='Changed',interface='org.gtk.Actions',path='/org/ayatana/indicator/power'"

    binder_interface = "/dev/hwbinder"
    service_name = "android.hardware.power@1.0::IPower/default"
    interface_name = "android.hardware.power@1.0::IPower"

    set_interactive_id = 1
    power_hint_id = 2

    enum_low_power = 5
    enum_sustained_performance = 6

    first_boot_check_file = "/tmp/hidl_perf_boost_booted"

    power_saving_toggle_file = "/home/phablet/.config/power_saving"

    def is_power_saving():
    try:
    f = open(power_saving_toggle_file, "r")
    f.close()
    return True
    except:
    return False

    first_boot_state = None
    def is_first_boot():
    global first_boot_state
    if first_boot_state is None:
    try:
    open(first_boot_check_file, "r")
    first_boot_state = False
    except:
    first_boot_state = True
    f = open(first_boot_check_file, "w")
    f.close()
    return first_boot_state

    def hidl_set_power_hint(client, type_enum, enable):
    if enable:
    enable = 1
    else:
    enable = 0
    request = client.new_request()
    request.append_int32(type_enum)
    request.append_int32(enable)
    reply, status = client.transact_sync_reply(power_hint_id, request)
    return (reply, status)

    def hidl_set_interactive(client, interactive):
    request = client.new_request()
    request.append_bool(interactive)
    reply, status = client.transact_sync_reply(set_interactive_id, request)
    return (reply, status)

    was_interactive = not is_first_boot()
    set_interactive_client = None
    def set_interactive(is_interactive):
    global was_interactive
    global set_interactive_client
    if set_interactive_client is None:
    set_interactive_client = gbinder.Client(service, interface_name)
    client = set_interactive_client

    if client is None:
    raise Exception("failed opening {0}::{1}".format(service_name, interface_name))

    if is_interactive != was_interactive:
    was_interactive = is_interactive
    power_saving = is_power_saving()

    hidl_set_interactive(client, is_interactive)
    hidl_set_power_hint(client, enum_low_power, (not is_interactive) or power_saving)
    hidl_set_power_hint(client, enum_sustained_performance, is_interactive and (not power_saving))

    def filter_cb(bus, message):
    arg_list = message.get_args_list()
    for item in arg_list:
    if isinstance(item, dict):
    if 'brightness' in item:
    brightness = item['brightness']
    if brightness < 0:
    set_interactive(False)
    else:
    set_interactive(True)

    sm = gbinder.ServiceManager(binder_interface)
    intf = sm.list_sync();

    service = None
    tries = 0
    while service is None and tries < 20:
    service, status = sm.get_service_sync(service_name)
    time.sleep(0.5)
    tries = tries + 1

    if service is None:
    raise Exception("failed oepning {0}".format(service_name))

    print("binder is ready")

    DBusGMainLoop(set_as_default=True)

    bus = dbus.SessionBus()
    bus.add_match_string(match_string)
    bus.add_message_filter(filter_cb)

    set_interactive(is_first_boot())

    print("starting glib loop")
    loop = GLib.MainLoop()
    loop.run()