Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement nickel_bluetooth action #152

Merged
merged 4 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions res/doc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -147,6 +148,10 @@
# 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
# disable
# toggle
#
# chain_success:<action>:<arg>
# chain_failure:<action>:<arg>
Expand Down
1 change: 1 addition & 0 deletions src/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions src/action_cc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<void*&>(var) = dlsym(RTLD_DEFAULT, sym)
#define NM_ACT_XSYM(var, symb, err) do { \
Expand Down Expand Up @@ -896,3 +897,42 @@ NM_ACTION_(cmd_output) {
? nm_action_result_silent()
: nm_action_result_msg("%s", qPrintable(Qt::convertFromPlainText(out, Qt::WhiteSpacePre)));
}


NM_ACTION_(nickel_bluetooth) {
bool enable=true, toggle=false;
if (!strcmp(arg, "enable")) enable = true;
else if (!strcmp(arg, "disable")) enable = false;
else if (!strcmp(arg, "toggle")) toggle = true;
else
NM_ERR_RET(nullptr, "unknown nickel_bluetooth action '%s'", arg);

//_ZN16BluetoothManager14sharedInstanceEv
BluetoothManager *(*BluetoothManager_sharedInstance)();
NM_ACT_XSYM(BluetoothManager_sharedInstance, "_ZN16BluetoothManager14sharedInstanceEv", "could not dlsym BluetoothManager::sharedInstance");


onatbas marked this conversation as resolved.
Show resolved Hide resolved
BluetoothManager *btm = BluetoothManager_sharedInstance();
NM_CHECK(nullptr, btm, "could not get shared bluetooth manager pointer");

if (toggle){
onatbas marked this conversation as resolved.
Show resolved Hide resolved
//_ZNK16BluetoothManager2upEv - returns 1 if bluetooth is turned on.
uint (*BluetoothManager_up)(BluetoothManager *);
NM_ACT_XSYM(BluetoothManager_up, "_ZNK16BluetoothManager2upEv", "could not dlsym BluetoothManager::up");

uint isUp = BluetoothManager_up(btm);
enable = isUp ? false : true;
}

if (enable){
void (*BluetoothManager_on)(BluetoothManager *);
NM_ACT_XSYM(BluetoothManager_on, "_ZN16BluetoothManager2onEv", "could not dlsym BluetoothManager::on");
BluetoothManager_on(btm);
}else{
onatbas marked this conversation as resolved.
Show resolved Hide resolved
void (*BluetoothManager_off)(BluetoothManager *);
NM_ACT_XSYM(BluetoothManager_off, "_ZN16BluetoothManager3offEv", "could not dlsym BluetoothManager::off");
BluetoothManager_off(btm);
}

return nm_action_result_toast("Bluetooth turned %s.", enable ? "on" : "off");
}