summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbjarosze <bjarosze@users.noreply.github.com>2022-04-11 13:28:55 +0200
committerGitHub <noreply@github.com>2022-04-11 13:28:55 +0200
commit5a63d94585d025f2bdf8870b8d56ca83b79c73ae (patch)
tree7d505b201d829020970e76cad8ef7b5775c84099
parent15bd48e3a50e233d8a318370509a10e2ea539fb1 (diff)
parentc4b2942ec8b5755969d36a3c2d49d0a6a158a4e5 (diff)
Merge pull request #57 from bjarosze/leagcy_bt
support legacy Gnome Bluetooth API
-rw-r--r--.gitignore1
-rw-r--r--bluetooth_legacy.js124
-rw-r--r--extension.js7
-rw-r--r--metadata.json2
4 files changed, 133 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index 2586ac4..8fafc6b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
/.idea
/schemas/gschemas.compiled
/.gnome-shell
+/gnome-bluetooth-quick-connect.zip
diff --git a/bluetooth_legacy.js b/bluetooth_legacy.js
new file mode 100644
index 0000000..e266187
--- /dev/null
+++ b/bluetooth_legacy.js
@@ -0,0 +1,124 @@
+// Copyright 2018 Bartosz Jaroszewski
+// SPDX-License-Identifier: GPL-2.0-or-later
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+const GnomeBluetooth = imports.gi.GnomeBluetooth;
+const Signals = imports.signals;
+const GLib = imports.gi.GLib;
+const ExtensionUtils = imports.misc.extensionUtils;
+const Me = ExtensionUtils.getCurrentExtension();
+const Utils = Me.imports.utils;
+
+var BluetoothController = class {
+ constructor() {
+ this._client = new GnomeBluetooth.Client();
+ this._model = this._client.get_model();
+ }
+
+ enable() {
+ this._connectSignal(this._model, 'row-changed', (arg0, arg1, iter) => {
+ if (iter) {
+ let device = this._buildDevice(iter);
+ if (device.isDefault)
+ this.emit('default-adapter-changed', device);
+ else
+ this.emit('device-changed', device);
+ }
+
+ });
+ this._connectSignal(this._model, 'row-deleted', () => {
+ this.emit('device-deleted');
+ });
+ this._connectSignal(this._model, 'row-inserted', (arg0, arg1, iter) => {
+ if (iter) {
+ let device = this._buildDevice(iter);
+ this.emit('device-inserted', device);
+ }
+ });
+ }
+
+ getDevices() {
+ let adapter = this._getDefaultAdapter();
+ if (!adapter)
+ return [];
+
+ let devices = [];
+
+ let [ret, iter] = this._model.iter_children(adapter);
+ while (ret) {
+ let device = this._buildDevice(iter);
+ devices.push(device);
+ ret = this._model.iter_next(iter);
+ }
+
+ return devices;
+ }
+
+ getConnectedDevices() {
+ return this.getDevices().filter((device) => {
+ return device.isConnected;
+ });
+ }
+
+ destroy() {
+ this._disconnectSignals();
+ }
+
+ _getDefaultAdapter() {
+ let [ret, iter] = this._model.get_iter_first();
+ while (ret) {
+ let isDefault = this._model.get_value(iter, GnomeBluetooth.Column.DEFAULT);
+ let isPowered = this._model.get_value(iter, GnomeBluetooth.Column.POWERED);
+ if (isDefault && isPowered)
+ return iter;
+ ret = this._model.iter_next(iter);
+ }
+ return null;
+ }
+
+ _buildDevice(iter) {
+ return new BluetoothDevice(this._model, iter);
+ }
+}
+
+Signals.addSignalMethods(BluetoothController.prototype);
+Utils.addSignalsHelperMethods(BluetoothController.prototype);
+
+var BluetoothDevice = class {
+ constructor(model, iter) {
+ this._model = model;
+ this.update(iter);
+ }
+
+ update(iter) {
+ this.name = this._model.get_value(iter, GnomeBluetooth.Column.ALIAS) || this._model.get_value(iter, GnomeBluetooth.Column.NAME);
+ this.isConnected = this._model.get_value(iter, GnomeBluetooth.Column.CONNECTED);
+ this.isPaired = this._model.get_value(iter, GnomeBluetooth.Column.PAIRED);
+ this.mac = this._model.get_value(iter, GnomeBluetooth.Column.ADDRESS);
+ this.isDefault = this._model.get_value(iter, GnomeBluetooth.Column.DEFAULT);
+ }
+
+ disconnect() {
+ Utils.spawn(`bluetoothctl -- disconnect ${this.mac}`)
+ }
+
+ connect() {
+ Utils.spawn(`bluetoothctl -- connect ${this.mac}`)
+ }
+
+ reconnect() {
+ Utils.spawn(`bluetoothctl -- disconnect ${this.mac} && sleep 7 && bluetoothctl -- connect ${this.mac}`)
+ }
+}
diff --git a/extension.js b/extension.js
index 89dbf86..0b70bbe 100644
--- a/extension.js
+++ b/extension.js
@@ -20,7 +20,12 @@ const GLib = imports.gi.GLib;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const UiExtension = Me.imports.ui;
-const Bluetooth = Me.imports.bluetooth;
+
+const GnomeBluetooth = imports.gi.GnomeBluetooth;
+const Bluetooth = new GnomeBluetooth.Client().get_devices === undefined ?
+ Me.imports.bluetooth_legacy :
+ Me.imports.bluetooth;
+
const Utils = Me.imports.utils;
const Settings = Me.imports.settings.Settings;
const BatteryProvider = Me.imports.power.UPowerBatteryProvider;
diff --git a/metadata.json b/metadata.json
index ef11a2f..924d940 100644
--- a/metadata.json
+++ b/metadata.json
@@ -6,6 +6,8 @@
"settings-schema": "org.gnome.shell.extensions.bluetooth-quick-connect",
"gettext-domain": "bluetooth-quick-connect",
"shell-version": [
+ "40",
+ "41",
"42"
]
}