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

WIP: Initial Bluetooth handoff impl #1337

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions data/org.gnome.Shell.Extensions.GSConnect.gresource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<file compressed="true" preprocess="xml-stripblanks">ui/messaging-conversation-summary.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/messaging-conversation.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/messaging-window.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/bluetooth-handoff-dialog.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/bluetooth-handoff-device-row.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/mousepad-input-dialog.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/notification-reply-dialog.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/preferences-command-editor.ui</file>
Expand Down
2 changes: 1 addition & 1 deletion data/org.gnome.Shell.Extensions.GSConnect.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<default>{}</default>
</key>
<key name="menu-actions" type="as">
<default>["sms", "ring", "mount", "commands", "share", "photo", "keyboard"]</default>
<default>["sms", "ring", "mount", "commands", "share", "photo", "keyboard", "openBluetoothHandoffDialog"]</default>
</key>
<key name="name" type="s">
<default>""</default>
Expand Down
46 changes: 46 additions & 0 deletions data/ui/bluetooth-handoff-device-row.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface domain="org.gnome.Shell.Extensions.GSConnect">
<requires lib="gtk+" version="3.24"/>
<template class="GSConnectBluetoothHandoffDeviceRow" parent="GtkListBoxRow">
<property name="visible">True</property>
<property name="hexpand">True</property>
<property name="can_focus">False</property>
<property name="selectable">False</property>
<property name="activatable">True</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="border-width">12</property>
<property name="margin_left">6</property>
<property name="margin_right">6</property>
<property name="orientation">horizontal</property>
<child>
<object class="GtkImage" id="device_icon">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="valign">center</property>
<property name="margin_right">12</property>
<property name="icon_name">bluetooth-active-symbolic</property>
</object>
</child>
<child>
<object class="GtkLabel" id="device_name">
<property name="visible">True</property>
<property name="hexpand">True</property>
<property name="label"></property>
<property name="xalign">0.0</property>
</object>
</child>
<child>
<object class="GtkSwitch" id="switch">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="valign">center</property>
<signal name="state-set" handler="_onDeviceToggled" swapped="no"/>
</object>
</child>
</object>
</child>
</template>
</interface>

61 changes: 61 additions & 0 deletions data/ui/bluetooth-handoff-dialog.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="3.24"/>
<template class="GSConnectBluetoothHandoffDialog" parent="GtkDialog">
<property name="can_focus">False</property>
<property name="default_width">500</property>
<property name="default_height">400</property>
<property name="type_hint">dialog</property>
<child internal-child="vbox">
<object class="GtkBox">
<property name="can_focus">False</property>
<property name="margin_left">36</property>
<property name="margin_right">36</property>
<property name="margin_top">24</property>
<property name="margin_bottom">24</property>
<property name="orientation">vertical</property>
<property name="visible">True</property>
<child>
<object class="GtkListBox" id="device_list">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="visible">True</property>
<property name="margin_bottom">18</property>
<property name="selection-mode">none</property>
<style>
<class name="frame"/>
</style>
<child type="placeholder">
<object class="GtkListBoxRow">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="selectable">False</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="margin_top">12</property>
<property name="margin_bottom">12</property>
<property name="margin_left">12</property>
<property name="margin_right">12</property>
<property name="label" translatable="yes">No Devices</property>
<style>
<class name="dim-label"/>
</style>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkLabel" id="instruction-label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="wrap">True</property>
</object>
</child>
</object>
</child>
</template>
</interface>

188 changes: 188 additions & 0 deletions src/service/components/bluez.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
'use strict';

const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;

function log(message) {
GLib.log_structured('GSConnect', GLib.LogLevelFlags.LEVEL_MESSAGE, {
'MESSAGE': message,
'SYSLOG_IDENTIFIER': 'org.gnome.Shell.Extensions.GSConnect.BluetoothHandoff'
});
}

/**
* A class representing the local bluetooth devices.
*/
var LocalBluetoothDevices = GObject.registerClass({
GTypeName: 'GSConnectLocalBluetoothDevices',
Signals: {
'changed': {
flags: GObject.SignalFlags.RUN_FIRST,
},
},
}, class LocalBluetoothDevices extends GObject.Object {

_init() {
try {
super._init();

this._cancellable = new Gio.Cancellable();
this._proxy = null;
this._propertiesChangedId = 0;
this._localDevices = {};
this._localDevicesJson = "{}";

this._loadBluez();
} catch (e) {
log("bluez ERR: " + e.message);
}
}

async _loadBluez() {
try {
this._proxy = new Gio.DBusProxy({
g_bus_type: Gio.BusType.SYSTEM,
g_name: 'org.bluez',
g_object_path: '/',
g_interface_name: 'org.freedesktop.DBus.ObjectManager',
});

await new Promise((resolve, reject) => {
this._proxy.init_async(
GLib.PRIORITY_DEFAULT,
this._cancellable,
(proxy, res) => {
try {
resolve(proxy.init_finish(res));
} catch (e) {
reject(e);
}
}
);
});

this._fetchDevices();
GLib.timeout_add(GLib.PRIORITY_DEFAULT, 1000, () => {
this._fetchDevices();

if (!this._cancellable.is_cancelled()) {
return GLib.SOURCE_CONTINUE;
} else {
return GLib.SOURCE_REMOVE;
}
});
} catch (e) {
log("ERR: " + e.message);
this._proxy = null;
}
}

async _fetchDevices() {
try {
let objects = await new Promise((resolve, reject) => {
this._proxy.call(
'GetManagedObjects',
null,
Gio.DBusCallFlags.NO_AUTO_START,
1000,
this._cancellable,
(proxy, res) => {
try {
const reply = proxy.call_finish(res);
resolve(reply.deepUnpack()[0]);
} catch (e) {
Gio.DBusError.strip_remote_error(e);
reject(e);
}
}
);
});

const newLocalDevices = await Promise.all(Object.keys(objects)
.filter((rawObjectPath) => {
const objectPath = rawObjectPath.split('/');
return objectPath.length == 5 && objectPath[0] == '' && objectPath[1] == 'org' && objectPath[2] == 'bluez'
&& objectPath[3].startsWith('hci') && objectPath[4].startsWith('dev_');
}).map(async (objectPath) => {
const objectProxy = new Gio.DBusProxy({
g_bus_type: Gio.BusType.SYSTEM,
g_name: 'org.bluez',
g_object_path: objectPath,
g_interface_name: 'org.bluez.Device1',
});

await new Promise((resolve, reject) => {
objectProxy.init_async(
GLib.PRIORITY_DEFAULT,
null,
(proxy, res) => {
try {
proxy.init_finish(res);
resolve();
} catch (e) {
reject(e);
}
}
);
});

try {
let addr = objectProxy.get_cached_property('Address');
if (addr) addr = addr.recursiveUnpack();
let alias = objectProxy.get_cached_property('Alias');
if (alias) alias = alias.recursiveUnpack();
let name = objectProxy.get_cached_property('Name');
if (name) name = name.recursiveUnpack();
let adapter = objectProxy.get_cached_property('Adapter');
if (adapter) adapter = adapter.recursiveUnpack();
let icon = objectProxy.get_cached_property('Icon');
if (icon) icon = icon.recursiveUnpack();
let paired = objectProxy.get_cached_property('Paired');
if (paired) paired = paired.recursiveUnpack();
let connected = objectProxy.get_cached_property('Connected');
if (connected) connected = connected.recursiveUnpack();

return {
addr,
alias,
name,
adapter,
icon,
paired,
connected,
}
} catch (e) {
log(" -> err - " + e);
}
}));

const newLocalDevicesJson = JSON.stringify(newLocalDevices);
if (newLocalDevicesJson != this._localDevicesJson) {
this._localDevices = newLocalDevices;
this._localDevicesJson = newLocalDevicesJson;
this.emit('changed');
}
} catch (e) {
log(" -> err - " + e);
}
}

get local_devices() {
return this._localDevices;
}

destroy() {
if (this._cancellable.is_cancelled())
return;

this._cancellable.cancel();
}
});


/**
* The service class for this component
*/
var Component = LocalBluetoothDevices;

Loading