From 2e5819422d96acd03fc53811a6f44d1ee9aba443 Mon Sep 17 00:00:00 2001 From: Onat Date: Fri, 15 Dec 2023 16:36:06 -0500 Subject: [PATCH] Implement nickel_bluetooth action (#152) closes #142 --- res/doc | 7 +++++ src/action.h | 1 + src/action_cc.cc | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/res/doc b/res/doc index 2fd8f3d..69dd045 100644 --- a/res/doc +++ b/res/doc @@ -65,6 +65,7 @@ # nickel_misc - other stuff which isn't significant enough for its own category # nickel_open - opens a view # nickel_wifi - controls wifi (note: it doesn't wait for it to connect or disconnect, neither does it check for success) +# nickel_bluetooth - controls bluetooth # nickel_orientation - controls screen orientation # (devices without an orientation sensor may need to use the kobopatch patch "Allow rotation on all devices" or set [DeveloperSettings] ForceAllowLandscape=true) # (devices with an orientation sensor don't need to do anything, but can set the config setting to make this work on all views) @@ -147,6 +148,12 @@ # reboot (4.13.12638+) # sleep (4.13.12638+) # skip - the number of actions to skip, or -1 to skip all remaining ones (i.e. end the chain) +# nickel_bluetooth - one of: +# enable (4.34.20097+) +# disable (4.34.20097+) +# toggle (4.34.20097+) +# check (4.34.20097+) +# scan (4.34.20097+) # # chain_success:: # chain_failure:: diff --git a/src/action.h b/src/action.h index 13fef5d..001fc8a 100644 --- a/src/action.h +++ b/src/action.h @@ -50,6 +50,7 @@ void nm_action_result_free(nm_action_result_t *res); X(nickel_misc) \ X(nickel_open) \ X(nickel_wifi) \ + X(nickel_bluetooth) \ X(nickel_orientation) \ X(power) \ X(skip) diff --git a/src/action_cc.cc b/src/action_cc.cc index c531e00..7a6638f 100644 --- a/src/action_cc.cc +++ b/src/action_cc.cc @@ -57,6 +57,7 @@ typedef void WirelessWorkflowManager; typedef void StatusBarView; typedef void MoreController; typedef void MainWindowController; +typedef void BluetoothManager; #define NM_ACT_SYM(var, sym) reinterpret_cast(var) = dlsym(RTLD_DEFAULT, sym) #define NM_ACT_XSYM(var, symb, err) do { \ @@ -896,3 +897,72 @@ NM_ACTION_(cmd_output) { ? nm_action_result_silent() : nm_action_result_msg("%s", qPrintable(Qt::convertFromPlainText(out, Qt::WhiteSpacePre))); } + +NM_ACTION_(nickel_bluetooth) { + enum BLUETOOTH_ACTION { + ENABLE = 0b00001, + DISABLE = 0b00010, + TOGGLE = 0b00100, + CHECK = 0b01000, + SCAN = 0b10000 + }; + + int action = 0; + if (!strcmp(arg, "enable")) action |= ENABLE; + else if (!strcmp(arg, "disable")) action |= DISABLE; + else if (!strcmp(arg, "toggle")) action |= TOGGLE; + else if (!strcmp(arg, "check")) action |= CHECK; + else if (!strcmp(arg, "scan")) action |= SCAN; + else + NM_ERR_RET(nullptr, "unknown nickel_bluetooth action '%s'", arg); + + //libnickel 4.34.20097 * _ZN16BluetoothManager14sharedInstanceEv + BluetoothManager *(*BluetoothManager_sharedInstance)(); + NM_ACT_XSYM(BluetoothManager_sharedInstance, "_ZN16BluetoothManager14sharedInstanceEv", "could not dlsym BluetoothManager::sharedInstance"); + + //libnickel 4.34.20097 * _ZNK16BluetoothManager2upEv + uint (*BluetoothManager_up)(BluetoothManager *); + NM_ACT_XSYM(BluetoothManager_up, "_ZNK16BluetoothManager2upEv", "could not dlsym BluetoothManager::up"); + + //libnickel 4.34.20097 * _ZN16BluetoothManager2onEv + void (*BluetoothManager_on)(BluetoothManager *); + NM_ACT_XSYM(BluetoothManager_on, "_ZN16BluetoothManager2onEv", "could not dlsym BluetoothManager::on"); + + //libnickel 4.34.20097 * _ZN16BluetoothManager4scanEv + void (*BluetoothManager_scan)(BluetoothManager *); + NM_ACT_XSYM(BluetoothManager_scan, "_ZN16BluetoothManager4scanEv", "could not dlsym BluetoothManager::BluetoothManager::scanEv"); + + //libnickel 4.34.20097 * _ZN16BluetoothManager8stopScanEv + void (*BluetoothManager_stopScan)(BluetoothManager *); + NM_ACT_XSYM(BluetoothManager_stopScan, "_ZN16BluetoothManager8stopScanEv", "could not dlsym BluetoothManager::stopScan"); + + //libnickel 4.34.20097 * _ZN16BluetoothManager3offEv + void (*BluetoothManager_off)(BluetoothManager *); + NM_ACT_XSYM(BluetoothManager_off, "_ZN16BluetoothManager3offEv", "could not dlsym BluetoothManager::off"); + + BluetoothManager *btm = BluetoothManager_sharedInstance(); + NM_CHECK(nullptr, btm, "could not get shared bluetooth manager pointer"); + + uint isUp = BluetoothManager_up(btm); + if (action & TOGGLE) + action = (action & ~TOGGLE) | (isUp ? DISABLE : ENABLE); + + switch (action) { + case CHECK: + return nm_action_result_toast("Bluetooth is %s.", isUp ? "on" : "off"); + case ENABLE: + BluetoothManager_on(btm); + BluetoothManager_scan(btm); + return nm_action_result_toast("Bluetooth turned on."); + case DISABLE: + BluetoothManager_stopScan(btm); + BluetoothManager_off(btm); + return nm_action_result_toast("Bluetooth turned off."); + case SCAN: + BluetoothManager_scan(btm); + return nm_action_result_toast("Bluetooth scan initiated."); + default: + NM_ERR_RET(nullptr, "unknown nickel_bluetooth action '%s'", arg); + break; + } +}