From a24d2b50a5f5346ffbaa861d6f2cd13e4c5e3142 Mon Sep 17 00:00:00 2001 From: parport0 Date: Wed, 24 Jun 2020 12:53:08 +0300 Subject: [PATCH] bluetooth: split into labels and sublabels --- bluetooth/bluetooth_driver.h | 3 +++ bluetooth/drivers/bluetoothctl.c | 13 ++++++++++++- bluetooth/drivers/bluez.c | 8 +++++++- menu/cbs/menu_cbs_sublabel.c | 14 ++++++++++++++ retroarch.c | 7 +++++++ 5 files changed, 43 insertions(+), 2 deletions(-) diff --git a/bluetooth/bluetooth_driver.h b/bluetooth/bluetooth_driver.h index a6e4b76442..840315e463 100644 --- a/bluetooth/bluetooth_driver.h +++ b/bluetooth/bluetooth_driver.h @@ -50,6 +50,7 @@ typedef struct bluetooth_driver void (*scan)(void); void (*get_devices)(struct string_list *list); bool (*device_is_connected)(unsigned i); + void (*device_get_sublabel)(char *s, unsigned i, size_t len); bool (*connect_device)(unsigned i); const char *ident; @@ -79,6 +80,8 @@ void driver_bluetooth_get_devices(struct string_list *list); bool driver_bluetooth_device_is_connected(unsigned i); +void driver_bluetooth_device_get_sublabel(char *s, unsigned i, size_t len); + bool driver_bluetooth_connect_device(unsigned i); bool bluetooth_driver_ctl(enum rarch_bluetooth_ctl_state state, void *data); diff --git a/bluetooth/drivers/bluetoothctl.c b/bluetooth/drivers/bluetoothctl.c index 4981e494dc..5cc021b7d6 100644 --- a/bluetooth/drivers/bluetoothctl.c +++ b/bluetooth/drivers/bluetoothctl.c @@ -93,7 +93,7 @@ static void bluetoothctl_get_devices(struct string_list* devices) * $ bluetoothctl devices * 'Device (mac address) (device name)' */ - strlcpy(device, line+7, sizeof(device)); + strlcpy(device, line+24, sizeof(device)); string_list_append(devices, device, attr); } } @@ -190,6 +190,16 @@ static bool bluetoothctl_connect_device(unsigned idx) return true; } +void bluetoothctl_device_get_sublabel (char *s, unsigned i, size_t len) +{ + /* bluetoothctl devices outputs lines of the format: + * $ bluetoothctl devices + * 'Device (mac address) (device name)' + */ + const char *line = lines->elems[i].data; + strlcpy(s, line+7, 18); +} + bluetooth_driver_t bluetooth_bluetoothctl = { bluetoothctl_init, bluetoothctl_free, @@ -198,6 +208,7 @@ bluetooth_driver_t bluetooth_bluetoothctl = { bluetoothctl_scan, bluetoothctl_get_devices, bluetoothctl_device_is_connected, + bluetoothctl_device_get_sublabel, bluetoothctl_connect_device, "bluetoothctl", }; diff --git a/bluetooth/drivers/bluez.c b/bluetooth/drivers/bluez.c index 18379c3517..328eebe325 100644 --- a/bluetooth/drivers/bluez.c +++ b/bluetooth/drivers/bluez.c @@ -529,7 +529,7 @@ static void bluez_get_devices (struct string_list* devices_string_list) for (i = 0; i < devices->count; i++) { char device[64]; - snprintf(device, 64, "%s %s", devices->data[i].address, devices->data[i].name); + strlcpy(device, devices->data[i].name, 64); string_list_append(devices_string_list, device, attr); } } @@ -553,6 +553,11 @@ static bool bluez_device_is_connected (unsigned i) } } +static void bluez_device_get_sublabel (char *s, unsigned i, size_t len) +{ + strlcpy(s, devices->data[i].address, len); +} + static bool bluez_connect_device (unsigned i) { bluez_dbus_connect(); @@ -582,6 +587,7 @@ bluetooth_driver_t bluetooth_bluez = { bluez_scan, bluez_get_devices, bluez_device_is_connected, + bluez_device_get_sublabel, bluez_connect_device, "bluez", }; diff --git a/menu/cbs/menu_cbs_sublabel.c b/menu/cbs/menu_cbs_sublabel.c index d3ff612262..897f84c5d2 100644 --- a/menu/cbs/menu_cbs_sublabel.c +++ b/menu/cbs/menu_cbs_sublabel.c @@ -30,6 +30,7 @@ #endif #include "../../core_info.h" #include "../../verbosity.h" +#include "../../bluetooth/bluetooth_driver.h" #ifdef HAVE_NETWORKING #include "../../network/netplay/netplay.h" @@ -875,6 +876,16 @@ static int action_bind_sublabel_systeminfo_controller_entry( return 0; } +static int action_bind_sublabel_bluetooth_list( + file_list_t *list, + unsigned type, unsigned i, + const char *label, const char *path, + char *s, size_t len) +{ + driver_bluetooth_device_get_sublabel(s, i, len); + return 0; +} + static int action_bind_sublabel_cheevos_entry( file_list_t *list, unsigned type, unsigned i, @@ -3042,6 +3053,9 @@ int menu_cbs_init_bind_sublabel(menu_file_list_cbs_t *cbs, case MENU_ENUM_LABEL_VIDEO_SHARED_CONTEXT: BIND_ACTION_SUBLABEL(cbs, action_bind_sublabel_video_shared_context); break; + case MENU_ENUM_LABEL_CONNECT_BLUETOOTH: + BIND_ACTION_SUBLABEL(cbs, action_bind_sublabel_bluetooth_list); + break; case MENU_ENUM_LABEL_CHEEVOS_UNLOCKED_ENTRY: case MENU_ENUM_LABEL_CHEEVOS_UNLOCKED_ENTRY_HARDCORE: case MENU_ENUM_LABEL_CHEEVOS_LOCKED_ENTRY: diff --git a/retroarch.c b/retroarch.c index c3a78b7def..3ede312acc 100644 --- a/retroarch.c +++ b/retroarch.c @@ -866,6 +866,7 @@ static bluetooth_driver_t bluetooth_null = { NULL, /* scan */ NULL, /* get_devices */ NULL, /* device_is_connected */ + NULL, /* device_get_sublabel */ NULL, /* connect_device */ "null", }; @@ -20140,6 +20141,12 @@ bool driver_bluetooth_device_is_connected(unsigned i) return p_rarch->bluetooth_driver->device_is_connected(i); } +void driver_bluetooth_device_get_sublabel(char *s, unsigned i, size_t len) +{ + struct rarch_state *p_rarch = &rarch_st; + p_rarch->bluetooth_driver->device_get_sublabel(s, i, len); +} + bool driver_bluetooth_connect_device(unsigned i) { struct rarch_state *p_rarch = &rarch_st;