2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 14:07:06 +00:00
|
|
|
/* exported Indicator */
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2022-08-08 10:08:26 +00:00
|
|
|
const {Gio, GLib, GnomeBluetooth, GObject, Pango, St} = imports.gi;
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2022-08-08 10:08:26 +00:00
|
|
|
const {Spinner} = imports.ui.animation;
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
const {QuickMenuToggle, SystemIndicator} = imports.ui.quickSettings;
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2022-07-26 08:45:35 +00:00
|
|
|
const {loadInterfaceXML} = imports.misc.fileUtils;
|
2018-09-06 00:55:20 +00:00
|
|
|
|
2022-08-23 14:30:32 +00:00
|
|
|
const {AdapterState} = GnomeBluetooth;
|
|
|
|
|
2013-11-26 13:06:41 +00:00
|
|
|
const BUS_NAME = 'org.gnome.SettingsDaemon.Rfkill';
|
|
|
|
const OBJECT_PATH = '/org/gnome/SettingsDaemon/Rfkill';
|
|
|
|
|
2018-09-06 00:55:20 +00:00
|
|
|
const RfkillManagerInterface = loadInterfaceXML('org.gnome.SettingsDaemon.Rfkill');
|
2022-07-29 10:57:50 +00:00
|
|
|
const rfkillManagerInfo = Gio.DBusInterfaceInfo.new_for_xml(RfkillManagerInterface);
|
2013-11-26 13:06:41 +00:00
|
|
|
|
2022-08-08 10:08:26 +00:00
|
|
|
Gio._promisify(GnomeBluetooth.Client.prototype, 'connect_service');
|
|
|
|
|
2022-07-28 13:00:45 +00:00
|
|
|
const BtClient = GObject.registerClass({
|
|
|
|
Properties: {
|
|
|
|
'available': GObject.ParamSpec.boolean('available', '', '',
|
|
|
|
GObject.ParamFlags.READABLE,
|
|
|
|
false),
|
|
|
|
'active': GObject.ParamSpec.boolean('active', '', '',
|
|
|
|
GObject.ParamFlags.READABLE,
|
|
|
|
false),
|
2022-08-23 14:30:32 +00:00
|
|
|
'adapter-state': GObject.ParamSpec.enum('adapter-state', '', '',
|
|
|
|
GObject.ParamFlags.READABLE,
|
|
|
|
AdapterState, AdapterState.ABSENT),
|
2022-07-28 13:00:45 +00:00
|
|
|
},
|
|
|
|
Signals: {
|
|
|
|
'devices-changed': {},
|
2022-08-08 10:08:26 +00:00
|
|
|
'device-removed': {param_types: [GObject.TYPE_STRING]},
|
2022-07-28 13:00:45 +00:00
|
|
|
},
|
|
|
|
}, class BtClient extends GObject.Object {
|
2019-07-16 09:24:13 +00:00
|
|
|
_init() {
|
|
|
|
super._init();
|
2013-06-06 21:27:25 +00:00
|
|
|
|
2022-01-18 10:14:47 +00:00
|
|
|
this._client = new GnomeBluetooth.Client();
|
2022-07-28 13:00:45 +00:00
|
|
|
this._client.connect('notify::default-adapter-powered', () => {
|
|
|
|
this.notify('active');
|
|
|
|
this.notify('available');
|
|
|
|
});
|
2022-08-23 14:30:32 +00:00
|
|
|
this._client.connect('notify::default-adapter-state',
|
|
|
|
() => this.notify('adapter-state'));
|
2022-02-24 18:35:06 +00:00
|
|
|
this._client.connect('notify::default-adapter', () => {
|
|
|
|
const newAdapter = this._client.default_adapter ?? null;
|
|
|
|
|
|
|
|
this._adapter = newAdapter;
|
|
|
|
this._deviceNotifyConnected.clear();
|
2022-07-28 13:00:45 +00:00
|
|
|
this.emit('devices-changed');
|
|
|
|
|
|
|
|
this.notify('active');
|
|
|
|
this.notify('available');
|
2022-02-24 18:35:06 +00:00
|
|
|
});
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2022-07-29 10:57:50 +00:00
|
|
|
this._proxy = new Gio.DBusProxy({
|
|
|
|
g_connection: Gio.DBus.session,
|
|
|
|
g_name: BUS_NAME,
|
|
|
|
g_object_path: OBJECT_PATH,
|
|
|
|
g_interface_name: rfkillManagerInfo.name,
|
|
|
|
g_interface_info: rfkillManagerInfo,
|
|
|
|
});
|
|
|
|
this._proxy.connect('g-properties-changed', (p, properties) => {
|
2022-09-15 16:03:22 +00:00
|
|
|
const changedProperties = properties.unpack();
|
|
|
|
if ('BluetoothHardwareAirplaneMode' in changedProperties)
|
|
|
|
this.notify('available');
|
|
|
|
else if ('BluetoothHasAirplaneMode' in changedProperties)
|
2022-07-28 13:00:45 +00:00
|
|
|
this.notify('available');
|
2022-07-29 10:57:50 +00:00
|
|
|
});
|
|
|
|
this._proxy.init_async(GLib.PRIORITY_DEFAULT, null)
|
|
|
|
.catch(e => console.error(e.message));
|
2013-11-26 13:06:41 +00:00
|
|
|
|
2020-04-03 18:44:41 +00:00
|
|
|
this._adapter = null;
|
2020-04-03 18:27:34 +00:00
|
|
|
|
2021-12-02 10:44:47 +00:00
|
|
|
this._deviceNotifyConnected = new Set();
|
|
|
|
|
|
|
|
const deviceStore = this._client.get_devices();
|
|
|
|
for (let i = 0; i < deviceStore.get_n_items(); i++)
|
|
|
|
this._connectDeviceNotify(deviceStore.get_item(i));
|
|
|
|
|
2021-12-02 10:44:47 +00:00
|
|
|
this._client.connect('device-removed', (c, path) => {
|
2021-12-02 10:44:47 +00:00
|
|
|
this._deviceNotifyConnected.delete(path);
|
2022-08-08 10:08:26 +00:00
|
|
|
this.emit('device-removed', path);
|
2022-07-28 13:00:45 +00:00
|
|
|
this.emit('devices-changed');
|
2021-12-02 10:44:47 +00:00
|
|
|
});
|
|
|
|
this._client.connect('device-added', (c, device) => {
|
|
|
|
this._connectDeviceNotify(device);
|
2022-07-28 13:00:45 +00:00
|
|
|
this.emit('devices-changed');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get available() {
|
2022-09-15 16:03:22 +00:00
|
|
|
// If we have an rfkill switch, make sure it's not a hardware
|
|
|
|
// one as we can't get out of it in software
|
|
|
|
return this._proxy.BluetoothHasAirplaneMode
|
2022-07-28 13:00:45 +00:00
|
|
|
? !this._proxy.BluetoothHardwareAirplaneMode
|
|
|
|
: this.active;
|
|
|
|
}
|
|
|
|
|
|
|
|
get active() {
|
|
|
|
return this._client.default_adapter_powered;
|
|
|
|
}
|
|
|
|
|
2022-08-23 14:30:32 +00:00
|
|
|
get adapter_state() {
|
|
|
|
return this._client.default_adapter_state;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:00:45 +00:00
|
|
|
toggleActive() {
|
|
|
|
this._proxy.BluetoothAirplaneMode = this.active;
|
|
|
|
if (!this._client.default_adapter_powered)
|
|
|
|
this._client.default_adapter_powered = true;
|
|
|
|
}
|
|
|
|
|
2022-08-08 10:08:26 +00:00
|
|
|
async toggleDevice(device) {
|
|
|
|
const connect = !device.connected;
|
|
|
|
console.debug(`${connect
|
|
|
|
? 'Connect' : 'Disconnect'} device "${device.name}"`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this._client.connect_service(
|
|
|
|
device.get_object_path(),
|
|
|
|
connect,
|
|
|
|
null);
|
|
|
|
console.debug(`Device "${device.name}" ${
|
|
|
|
connect ? 'connected' : 'disconnected'}`);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`Failed to ${connect
|
|
|
|
? 'connect' : 'disconnect'} device "${device.name}": ${e.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:00:45 +00:00
|
|
|
*getDevices() {
|
2023-02-23 18:03:38 +00:00
|
|
|
// Ignore any lingering device references when turned off
|
|
|
|
if (!this.active)
|
|
|
|
return;
|
|
|
|
|
2022-07-28 13:00:45 +00:00
|
|
|
const deviceStore = this._client.get_devices();
|
|
|
|
|
|
|
|
for (let i = 0; i < deviceStore.get_n_items(); i++) {
|
|
|
|
const device = deviceStore.get_item(i);
|
|
|
|
|
|
|
|
if (device.paired || device.trusted)
|
|
|
|
yield device;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_queueDevicesChanged() {
|
|
|
|
if (this._devicesChangedId)
|
|
|
|
return;
|
|
|
|
this._devicesChangedId = GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
|
|
|
|
delete this._devicesChangedId;
|
|
|
|
this.emit('devices-changed');
|
|
|
|
return GLib.SOURCE_REMOVE;
|
2021-12-02 10:44:47 +00:00
|
|
|
});
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2021-12-02 10:44:47 +00:00
|
|
|
_connectDeviceNotify(device) {
|
2021-12-02 10:44:47 +00:00
|
|
|
const path = device.get_object_path();
|
|
|
|
|
|
|
|
if (this._deviceNotifyConnected.has(path))
|
2021-12-02 10:44:47 +00:00
|
|
|
return;
|
2021-12-02 10:44:47 +00:00
|
|
|
|
2022-07-28 13:00:45 +00:00
|
|
|
device.connect('notify::alias', () => this._queueDevicesChanged());
|
|
|
|
device.connect('notify::paired', () => this._queueDevicesChanged());
|
|
|
|
device.connect('notify::trusted', () => this._queueDevicesChanged());
|
|
|
|
device.connect('notify::connected', () => this._queueDevicesChanged());
|
2021-12-02 10:44:47 +00:00
|
|
|
|
|
|
|
this._deviceNotifyConnected.add(path);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2022-07-28 13:00:45 +00:00
|
|
|
});
|
2013-11-26 13:46:13 +00:00
|
|
|
|
2022-08-08 10:08:26 +00:00
|
|
|
const BluetoothDeviceItem = GObject.registerClass(
|
|
|
|
class BluetoothDeviceItem extends PopupMenu.PopupBaseMenuItem {
|
|
|
|
constructor(device, client) {
|
|
|
|
super({
|
|
|
|
style_class: 'bt-device-item',
|
|
|
|
});
|
|
|
|
|
|
|
|
this._device = device;
|
|
|
|
this._client = client;
|
|
|
|
|
|
|
|
this._icon = new St.Icon({
|
|
|
|
style_class: 'popup-menu-icon',
|
|
|
|
});
|
|
|
|
this.add_child(this._icon);
|
|
|
|
|
|
|
|
this._label = new St.Label({
|
|
|
|
x_expand: true,
|
|
|
|
});
|
|
|
|
this.add_child(this._label);
|
|
|
|
|
|
|
|
this._subtitle = new St.Label({
|
|
|
|
style_class: 'device-subtitle',
|
|
|
|
});
|
|
|
|
this.add_child(this._subtitle);
|
|
|
|
|
|
|
|
this._spinner = new Spinner(16, {hideOnStop: true});
|
|
|
|
this.add_child(this._spinner);
|
|
|
|
|
|
|
|
this._spinner.bind_property('visible',
|
|
|
|
this._subtitle, 'visible',
|
|
|
|
GObject.BindingFlags.SYNC_CREATE |
|
|
|
|
GObject.BindingFlags.INVERT_BOOLEAN);
|
|
|
|
|
|
|
|
this._device.bind_property('connectable',
|
|
|
|
this, 'visible',
|
|
|
|
GObject.BindingFlags.SYNC_CREATE);
|
|
|
|
this._device.bind_property('icon',
|
|
|
|
this._icon, 'icon-name',
|
|
|
|
GObject.BindingFlags.SYNC_CREATE);
|
|
|
|
this._device.bind_property('name',
|
|
|
|
this._label, 'text',
|
|
|
|
GObject.BindingFlags.SYNC_CREATE);
|
|
|
|
this._device.bind_property_full('connected',
|
|
|
|
this._subtitle, 'text',
|
|
|
|
GObject.BindingFlags.SYNC_CREATE,
|
|
|
|
(bind, source) => [true, source ? _('Disconnect') : _('Connect')],
|
|
|
|
null);
|
|
|
|
|
|
|
|
this.connect('destroy', () => (this._spinner = null));
|
|
|
|
this.connect('activate', () => this._toggleConnected().catch(logError));
|
|
|
|
}
|
|
|
|
|
|
|
|
async _toggleConnected() {
|
|
|
|
this._spinner.play();
|
|
|
|
await this._client.toggleDevice(this._device);
|
|
|
|
this._spinner?.stop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-07-26 08:45:35 +00:00
|
|
|
const BluetoothToggle = GObject.registerClass(
|
2022-08-08 10:08:26 +00:00
|
|
|
class BluetoothToggle extends QuickMenuToggle {
|
2022-07-26 08:45:35 +00:00
|
|
|
_init(client) {
|
2023-01-30 20:32:39 +00:00
|
|
|
super._init({title: _('Bluetooth')});
|
2022-07-26 08:45:35 +00:00
|
|
|
|
2022-08-08 10:08:26 +00:00
|
|
|
this.menu.setHeader('bluetooth-active-symbolic', _('Bluetooth'));
|
|
|
|
|
|
|
|
this._deviceItems = new Map();
|
|
|
|
this._deviceSection = new PopupMenu.PopupMenuSection();
|
|
|
|
this.menu.addMenuItem(this._deviceSection);
|
|
|
|
|
|
|
|
this._placeholderItem = new PopupMenu.PopupMenuItem('', {
|
|
|
|
style_class: 'bt-menu-placeholder',
|
|
|
|
reactive: false,
|
|
|
|
can_focus: false,
|
|
|
|
});
|
|
|
|
this._placeholderItem.label.clutter_text.set({
|
|
|
|
ellipsize: Pango.EllipsizeMode.NONE,
|
|
|
|
line_wrap: true,
|
|
|
|
});
|
|
|
|
this.menu.addMenuItem(this._placeholderItem);
|
|
|
|
|
|
|
|
this._deviceSection.actor.bind_property('visible',
|
|
|
|
this._placeholderItem, 'visible',
|
|
|
|
GObject.BindingFlags.SYNC_CREATE |
|
|
|
|
GObject.BindingFlags.INVERT_BOOLEAN);
|
|
|
|
|
|
|
|
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
|
|
|
|
this.menu.addSettingsAction(_('Bluetooth Settings'),
|
|
|
|
'gnome-bluetooth-panel.desktop');
|
|
|
|
|
2022-07-26 08:45:35 +00:00
|
|
|
this._client = client;
|
|
|
|
|
|
|
|
this._client.bind_property('available',
|
|
|
|
this, 'visible',
|
|
|
|
GObject.BindingFlags.SYNC_CREATE);
|
|
|
|
this._client.bind_property('active',
|
|
|
|
this, 'checked',
|
|
|
|
GObject.BindingFlags.SYNC_CREATE);
|
2022-08-22 22:06:42 +00:00
|
|
|
this._client.bind_property_full('adapter-state',
|
2022-07-26 08:45:35 +00:00
|
|
|
this, 'icon-name',
|
|
|
|
GObject.BindingFlags.SYNC_CREATE,
|
2022-08-22 22:06:42 +00:00
|
|
|
(bind, source) => [true, this._getIconNameFromState(source)],
|
2022-07-26 08:45:35 +00:00
|
|
|
null);
|
|
|
|
|
2023-02-08 13:31:10 +00:00
|
|
|
this._client.connectObject(
|
2022-08-08 10:08:26 +00:00
|
|
|
'notify::active', () => this._onActiveChanged(),
|
2023-02-08 13:31:10 +00:00
|
|
|
'devices-changed', () => this._sync(),
|
2022-08-08 10:08:26 +00:00
|
|
|
'device-removed', (c, path) => this._removeDevice(path),
|
2023-02-08 13:31:10 +00:00
|
|
|
this);
|
|
|
|
|
2022-08-08 10:08:26 +00:00
|
|
|
this.menu.connect('open-state-changed', isOpen => {
|
|
|
|
// We don't reorder the list while the menu is open,
|
|
|
|
// so do it now to start with the proper order
|
|
|
|
if (isOpen)
|
|
|
|
this._reorderDeviceItems();
|
|
|
|
});
|
|
|
|
|
2022-07-26 08:45:35 +00:00
|
|
|
this.connect('clicked', () => this._client.toggleActive());
|
2023-02-08 13:31:10 +00:00
|
|
|
|
2022-08-08 10:08:26 +00:00
|
|
|
this._updatePlaceholder();
|
|
|
|
this._sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onActiveChanged() {
|
|
|
|
this._updatePlaceholder();
|
|
|
|
|
|
|
|
this._deviceItems.forEach(item => item.destroy());
|
|
|
|
this._deviceItems.clear();
|
|
|
|
|
2023-02-08 13:31:10 +00:00
|
|
|
this._sync();
|
|
|
|
}
|
|
|
|
|
2022-08-08 10:08:26 +00:00
|
|
|
_updatePlaceholder() {
|
|
|
|
this._placeholderItem.label.text = this._client.active
|
|
|
|
? _('No available or connected devices')
|
|
|
|
: _('Turn on Bluetooth to connect to devices');
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateDeviceVisibility() {
|
|
|
|
this._deviceSection.actor.visible =
|
|
|
|
[...this._deviceItems.values()].some(item => item.visible);
|
|
|
|
}
|
|
|
|
|
|
|
|
_getSortedDevices() {
|
|
|
|
return [...this._client.getDevices()].sort((dev1, dev2) => {
|
|
|
|
if (dev1.connected !== dev2.connected)
|
|
|
|
return dev2.connected - dev1.connected;
|
|
|
|
return dev1.alias.localeCompare(dev2.alias);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_removeDevice(path) {
|
|
|
|
this._deviceItems.get(path)?.destroy();
|
|
|
|
this._deviceItems.delete(path);
|
|
|
|
|
|
|
|
this._updateDeviceVisibility();
|
|
|
|
}
|
|
|
|
|
|
|
|
_reorderDeviceItems() {
|
|
|
|
const devices = this._getSortedDevices();
|
|
|
|
for (const [i, dev] of devices.entries()) {
|
|
|
|
const item = this._deviceItems.get(dev.get_object_path());
|
|
|
|
if (!item)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
this._deviceSection.moveMenuItem(item, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 13:31:10 +00:00
|
|
|
_sync() {
|
2022-08-08 10:08:26 +00:00
|
|
|
const devices = this._getSortedDevices();
|
|
|
|
|
|
|
|
for (const dev of devices) {
|
|
|
|
const path = dev.get_object_path();
|
|
|
|
if (this._deviceItems.has(path))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const item = new BluetoothDeviceItem(dev, this._client);
|
|
|
|
item.connect('notify::visible', () => this._updateDeviceVisibility());
|
|
|
|
|
|
|
|
this._deviceSection.addMenuItem(item);
|
|
|
|
this._deviceItems.set(path, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
const connectedDevices = devices.filter(dev => dev.connected);
|
2023-02-08 13:31:10 +00:00
|
|
|
const nConnected = connectedDevices.length;
|
|
|
|
|
|
|
|
if (nConnected > 1)
|
|
|
|
/* Translators: This is the number of connected bluetooth devices */
|
|
|
|
this.subtitle = ngettext('%d Connected', '%d Connected', nConnected).format(nConnected);
|
|
|
|
else if (nConnected === 1)
|
|
|
|
this.subtitle = connectedDevices[0].alias;
|
|
|
|
else
|
|
|
|
this.subtitle = null;
|
2022-08-08 10:08:26 +00:00
|
|
|
|
|
|
|
this._updateDeviceVisibility();
|
2022-07-26 08:45:35 +00:00
|
|
|
}
|
2022-08-22 22:06:42 +00:00
|
|
|
|
|
|
|
_getIconNameFromState(state) {
|
|
|
|
switch (state) {
|
|
|
|
case AdapterState.ON:
|
|
|
|
return 'bluetooth-active-symbolic';
|
|
|
|
case AdapterState.OFF:
|
|
|
|
case AdapterState.ABSENT:
|
|
|
|
return 'bluetooth-disabled-symbolic';
|
|
|
|
case AdapterState.TURNING_ON:
|
|
|
|
case AdapterState.TURNING_OFF:
|
|
|
|
return 'bluetooth-acquiring-symbolic';
|
|
|
|
default:
|
|
|
|
console.warn(`Unexpected state ${
|
|
|
|
GObject.enum_to_string(AdapterState, state)}`);
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2022-07-26 08:45:35 +00:00
|
|
|
});
|
|
|
|
|
2022-07-28 13:00:45 +00:00
|
|
|
var Indicator = GObject.registerClass(
|
2022-07-26 08:45:35 +00:00
|
|
|
class Indicator extends SystemIndicator {
|
2022-07-28 13:00:45 +00:00
|
|
|
_init() {
|
|
|
|
super._init();
|
2021-12-02 10:44:47 +00:00
|
|
|
|
2022-07-28 13:00:45 +00:00
|
|
|
this._client = new BtClient();
|
2022-07-26 08:45:35 +00:00
|
|
|
this._client.connect('devices-changed', () => this._sync());
|
2014-11-13 09:58:35 +00:00
|
|
|
|
2022-07-26 08:45:35 +00:00
|
|
|
this._indicator = this._addIndicator();
|
|
|
|
this._indicator.icon_name = 'bluetooth-active-symbolic';
|
2022-07-28 13:00:45 +00:00
|
|
|
|
2022-07-26 08:45:35 +00:00
|
|
|
this.quickSettingsItems.push(new BluetoothToggle(this._client));
|
2022-07-28 13:00:45 +00:00
|
|
|
|
|
|
|
this._sync();
|
2020-04-03 18:27:34 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_sync() {
|
2022-07-28 13:00:45 +00:00
|
|
|
const devices = [...this._client.getDevices()];
|
2020-03-30 08:48:06 +00:00
|
|
|
const connectedDevices = devices.filter(dev => dev.connected);
|
|
|
|
const nConnectedDevices = connectedDevices.length;
|
|
|
|
|
2014-11-13 09:58:35 +00:00
|
|
|
this._indicator.visible = nConnectedDevices > 0;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2019-07-16 09:24:13 +00:00
|
|
|
});
|