Blame SOURCES/0002-fakehid-Disconnect-from-PS3-remote-after-10-mins.patch

19062e
From cca11542bcd4d1748c850806c1599ed1b76ea19a Mon Sep 17 00:00:00 2001
19062e
From: Bastien Nocera <hadess@hadess.net>
19062e
Date: Sun, 17 Jun 2012 01:26:18 +0200
19062e
Subject: [PATCH 2/3] fakehid: Disconnect from PS3 remote after 10 mins
19062e
19062e
After 10 minutes, disconnect the PS3 BD Remote to avoid draining its
19062e
battery. This is consistent with its behaviour on the PS3.
19062e
19062e
Original patch by Ruslan N. Marchenko <rufferson@gmail.com>
19062e
---
19062e
 input/device.h  |    1 +
19062e
 input/fakehid.c |   36 ++++++++++++++++++++++++++++++++++++
19062e
 2 files changed, 37 insertions(+)
19062e
19062e
diff --git a/input/device.h b/input/device.h
19062e
index ff52967..d8baa2c 100644
19062e
--- a/input/device.h
19062e
+++ b/input/device.h
19062e
@@ -33,6 +33,7 @@ struct fake_input {
19062e
 	int		uinput;		/* uinput socket */
19062e
 	int		rfcomm;		/* RFCOMM socket */
19062e
 	uint8_t		ch;		/* RFCOMM channel number */
19062e
+	guint           timeout_id;	/* Disconnect timeout ID */
19062e
 	gboolean	(*connect) (struct input_conn *iconn, GError **err);
19062e
 	int		(*disconnect) (struct input_conn *iconn);
19062e
 	void		*priv;
19062e
diff --git a/input/fakehid.c b/input/fakehid.c
19062e
index 3181538..a125356 100644
19062e
--- a/input/fakehid.c
19062e
+++ b/input/fakehid.c
19062e
@@ -44,6 +44,9 @@
19062e
 #include "fakehid.h"
19062e
 #include "uinput.h"
19062e
 
19062e
+/* Timeout to get the PS3 remote disconnected, in seconds */
19062e
+#define PS3_REMOTE_TIMEOUT 10 * 60
19062e
+
19062e
 enum ps3remote_special_keys {
19062e
 	PS3R_BIT_PS = 0,
19062e
 	PS3R_BIT_ENTER = 3,
19062e
@@ -141,6 +144,20 @@ static unsigned int ps3remote_keymap[] = {
19062e
 	[0xff] = KEY_MAX,
19062e
 };
19062e
 
19062e
+static gboolean ps3_remote_timeout_cb(gpointer user_data);
19062e
+
19062e
+static void ps3remote_set_timeout(struct fake_input *fake, gboolean enable)
19062e
+{
19062e
+	if (enable) {
19062e
+		fake->timeout_id = g_timeout_add_seconds(PS3_REMOTE_TIMEOUT, ps3_remote_timeout_cb, fake);
19062e
+	} else {
19062e
+		if (fake->timeout_id > 0) {
19062e
+			g_source_remove(fake->timeout_id);
19062e
+			fake->timeout_id = 0;
19062e
+		}
19062e
+	}
19062e
+}
19062e
+
19062e
 static int ps3remote_decode(char *buff, int size, unsigned int *value)
19062e
 {
19062e
 	static unsigned int lastkey = 0;
19062e
@@ -203,6 +220,16 @@ error:
19062e
 	return -1;
19062e
 }
19062e
 
19062e
+static gboolean
19062e
+ps3_remote_timeout_cb(gpointer user_data)
19062e
+{
19062e
+	struct fake_input *fake = (struct fake_input *) user_data;
19062e
+	input_device_request_disconnect(fake);
19062e
+	DBG("Disconnected PS3 BD Remote after timeout");
19062e
+	fake->timeout_id = 0;
19062e
+	return FALSE;
19062e
+}
19062e
+
19062e
 static gboolean ps3remote_event(GIOChannel *chan, GIOCondition cond,
19062e
 				gpointer data)
19062e
 {
19062e
@@ -221,6 +248,9 @@ static gboolean ps3remote_event(GIOChannel *chan, GIOCondition cond,
19062e
 		goto failed;
19062e
 	}
19062e
 
19062e
+	/* Remove the old timeout */
19062e
+	ps3remote_set_timeout(fake, FALSE);
19062e
+
19062e
 	fd = g_io_channel_unix_get_fd(chan);
19062e
 
19062e
 	memset(buff, 0, sizeof(buff));
19062e
@@ -256,6 +286,8 @@ static gboolean ps3remote_event(GIOChannel *chan, GIOCondition cond,
19062e
 		goto failed;
19062e
 	}
19062e
 
19062e
+	ps3remote_set_timeout(fake, TRUE);
19062e
+
19062e
 	return TRUE;
19062e
 
19062e
 failed:
19062e
@@ -318,6 +350,8 @@ static int ps3remote_setup_uinput(struct fake_input *fake,
19062e
 		goto err;
19062e
 	}
19062e
 
19062e
+	ps3remote_set_timeout(fake, TRUE);
19062e
+
19062e
 	return 0;
19062e
 
19062e
 err:
19062e
@@ -378,6 +412,8 @@ struct fake_input *fake_hid_connadd(struct fake_input *fake,
19062e
 	for (l = fake_hid->devices; l != NULL; l = l->next) {
19062e
 		old = l->data;
19062e
 		if (old->idev == fake->idev) {
19062e
+			if (fake->timeout_id > 0)
19062e
+				g_source_remove(fake->timeout_id);
19062e
 			g_free(fake);
19062e
 			fake = old;
19062e
 			fake_hid->connect(fake, NULL);
19062e
-- 
19062e
1.7.10
19062e