From b6ec6e455e844193835db23ed5625283c99d7502 Mon Sep 17 00:00:00 2001 From: Maciek Borzecki Date: Thu, 1 Jun 2017 23:46:17 +0200 Subject: [PATCH] packethandlers: allow releasing of a device (eg. when device is no longer paired) --- src/mconnect/battery.vala | 17 +++++++++++------ src/mconnect/mousepad.vala | 19 ++++++++++++------- src/mconnect/notification.vala | 15 +++++++++------ src/mconnect/packethandlerinterface.vala | 2 ++ src/mconnect/packethandlers.vala | 6 ++++++ src/mconnect/telephony.vala | 16 ++++++++++------ 6 files changed, 50 insertions(+), 25 deletions(-) diff --git a/src/mconnect/battery.vala b/src/mconnect/battery.vala index f8f7448..70a9e34 100644 --- a/src/mconnect/battery.vala +++ b/src/mconnect/battery.vala @@ -36,14 +36,19 @@ class BatteryHandler : Object, PacketHandlerInterface { public void use_device(Device dev) { debug("use device %s for battery status updates", dev.to_string()); - dev.message.connect((d, pkt) => { - if (pkt.pkt_type == BATTERY) { - this.message(pkt); - } - }); + dev.message.connect(this.message); } - public void message(Packet pkt) { + public void release_device(Device dev) { + debug("release device %s", dev.to_string()); + dev.message.disconnect(this.message); + } + + public void message(Device dev, Packet pkt) { + if (pkt.pkt_type != BATTERY) { + return; + } + debug("got battery packet"); int64 level = pkt.body.get_int_member("currentCharge"); diff --git a/src/mconnect/mousepad.vala b/src/mconnect/mousepad.vala index 4160185..5c7ae07 100644 --- a/src/mconnect/mousepad.vala +++ b/src/mconnect/mousepad.vala @@ -46,15 +46,20 @@ class MousepadHandler : Object, PacketHandlerInterface { } public void use_device(Device dev) { - debug("use device %s for battery status updates", dev.to_string()); - dev.message.connect((d, pkt) => { - if (pkt.pkt_type == MOUSEPAD) { - this.message(pkt); - } - }); + debug("use device %s for mouse/keyboard input", dev.to_string()); + dev.message.connect(this.message); } - public void message(Packet pkt) { + public void release_device(Device dev) { + debug("release device %s ", dev.to_string()); + dev.message.disconnect(this.message); + } + + private void message(Device dev, Packet pkt) { + if (pkt.pkt_type != MOUSEPAD) { + return; + } + debug("got mousepad packet"); if (_display == null) { diff --git a/src/mconnect/notification.vala b/src/mconnect/notification.vala index 8876465..db08f78 100644 --- a/src/mconnect/notification.vala +++ b/src/mconnect/notification.vala @@ -38,14 +38,17 @@ class NotificationHandler : Object, PacketHandlerInterface { } public void use_device(Device dev) { - dev.message.connect((d, pkt) => { - if (pkt.pkt_type == NOTIFICATION) { - this.message(pkt); - } - }); + dev.message.connect(this.message); + } + + public void release_device(Device dev) { + dev.message.disconnect(this.message); } - public void message(Packet pkt) { + public void message(Device dev, Packet pkt) { + if (pkt.pkt_type != NOTIFICATION) { + return; + } debug("got notification packet"); // get application ID diff --git a/src/mconnect/packethandlerinterface.vala b/src/mconnect/packethandlerinterface.vala index 1688d14..e814e50 100644 --- a/src/mconnect/packethandlerinterface.vala +++ b/src/mconnect/packethandlerinterface.vala @@ -24,4 +24,6 @@ interface PacketHandlerInterface : Object { public abstract void use_device(Device dev); + public abstract void release_device(Device dev); + } \ No newline at end of file diff --git a/src/mconnect/packethandlers.vala b/src/mconnect/packethandlers.vala index 3d81f0c..c40b28c 100644 --- a/src/mconnect/packethandlers.vala +++ b/src/mconnect/packethandlers.vala @@ -69,4 +69,10 @@ class PacketHandlers : Object { h.use_device(dev); }); } + + public void release_device(Device dev) { + _handlers.foreach((h) => { + h.release_device(dev); + }); + } } \ No newline at end of file diff --git a/src/mconnect/telephony.vala b/src/mconnect/telephony.vala index 407349f..9f2a9c1 100644 --- a/src/mconnect/telephony.vala +++ b/src/mconnect/telephony.vala @@ -36,14 +36,18 @@ class TelephonyHandler : Object, PacketHandlerInterface { } public void use_device(Device dev) { - dev.message.connect((d, pkt) => { - if (pkt.pkt_type == TELEPHONY) { - this.message(pkt); - } - }); + dev.message.connect(this.message); + } + + public void release_device(Device dev) { + dev.message.disconnect(this.message); } - public void message(Packet pkt) { + public void message(Device dev, Packet pkt) { + if (pkt.pkt_type != TELEPHONY) { + return; + } + debug("got telephony packet"); if (pkt.body.has_member("phoneNumber") == false ||