Skip to contentSkip to conversation

Add virtual "idle" device

RFC PATCH In review Marcin Slusarz 3 participants 5 messages Mar 2, 2009 – Mar 12, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 2/2 Add virtual "idle" device

This virtual device can be used to tell user space about periods of time when user didn't "touch" input devices (keyboards, mice, touchpads, joysticks, etc). For now it supports only keyboard events.

Notification is done through simple select/poll + ioctl interface.

It can be used to implement screen savers, automatic suspend, autoaway/autodisconnect (e.g. in instant messangers) without any help from X server and its overhead (context switches).

---

As this driver is finished but to make it fully functional I need to extend input subsystem, I would like to know what do other think about this code.

Please comment.

---
 Documentation/idle/Makefile    |    1 +
 Documentation/idle/test_idle.c |  153 ++++++++++++++
 drivers/char/Kconfig           |   19 ++
 drivers/char/Makefile          |    1 +
 drivers/char/idle.c            |  445 ++++++++++++++++++++++++++++++++++++++++
 include/linux/Kbuild           |    1 +
 include/linux/idle.h           |   22 ++
 7 files changed, 642 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/idle/Makefile
 create mode 100644 Documentation/idle/test_idle.c
 create mode 100644 drivers/char/idle.c
 create mode 100644 include/linux/idle.h
Documentation/idle/Makefile new file +1 -0
new file mode 100644
index 0000000..3c122dd
@@ -0,0 +1 @@
+test_idle: test_idle.c
Documentation/idle/test_idle.c new file +153 -0

153 lines not on this page · raw message

drivers/char/Kconfig +19 -0
index 735bbe2..8f2d773 100644
@@ -1093,6 +1093,25 @@ config TELCLOCK
 	  /sys/devices/platform/telco_clock, with a number of files for
 	  controlling the behavior of this hardware.
 
+config IDLEDEV
+	tristate "/dev/idle support"
+	depends on VT
+	default y
+	help
+	  If you say Y here, user space applications can be notified when you
+	  don't "touch" input devices (for now keyboard only; later it will
+	  support mice, touchpads, joysticks, etc) for long periods of time.
+
+	  It can be used to implement screen savers, automatic suspend,
+	  autodisconnects (e.g. in instant messangers) without help from
+	  X server.
+
+	  See <file:Documentation/idle/> for example program using this device.
+
+	  Note: If you compile this driver as a module, it will _not_ be loaded
+	  automatically (like usual drivers). You will need to load it manually
+	  (or add it to list of modules loaded during boot).
+
 config DEVPORT
 	bool
 	depends on !M68K
drivers/char/Makefile +1 -0
index 9caf5b5..f77ef5c 100644
@@ -12,6 +12,7 @@ obj-y	 += mem.o random.o tty_io.o n_tty.o tty_ioctl.o tty_ldisc.o tty_buffer.o t
 obj-$(CONFIG_LEGACY_PTYS)	+= pty.o
 obj-$(CONFIG_UNIX98_PTYS)	+= pty.o
 obj-y				+= misc.o
+obj-$(CONFIG_IDLEDEV)		+= idle.o
 obj-$(CONFIG_VT)		+= vt_ioctl.o vc_screen.o selection.o keyboard.o
 obj-$(CONFIG_CONSOLE_TRANSLATIONS) += consolemap.o consolemap_deftbl.o
 obj-$(CONFIG_HW_CONSOLE)	+= vt.o defkeymap.o
drivers/char/idle.c new file +445 -0

445 lines not on this page · raw message

include/linux/Kbuild +1 -0
index b97cdc5..08c284f 100644
@@ -76,6 +76,7 @@ header-y += gigaset_dev.h
 header-y += hysdn_if.h
 header-y += i2o-dev.h
 header-y += i8k.h
+header-y += idle.h
 header-y += if_addrlabel.h
 header-y += if_arcnet.h
 header-y += if_bonding.h
include/linux/idle.h new file +22 -0
new file mode 100644
index 0000000..38f8f43
@@ -0,0 +1,22 @@
+#ifndef __LINUX_IDLE_H
+#define __LINUX_IDLE_H
+
+/* ioctl commands */
+/* get current event (unsigned char) IDLE_IDLE / IDLE_WAKEUP */
+#define IDLE_IOCTL_GET_EVENT 0
+
+/* set timeout */
+#define IDLE_IOCTL_SET 1
+
+/* remove timeout */
+#define IDLE_IOCTL_REMOVE 2
+
+/* get current idle time (seconds) */
+#define IDLE_IOCTL_GET_CURRENT_IDLE 3
+/* end of ioctls */
+
+/* types of events */
+#define IDLE_IDLE   0
+#define IDLE_WAKEUP 1
+
+#endif
Signature · 1 line
1.6.0.6
#1 lore raw

drivers/char/idle.c line 41

Quoted 9 lines · Marcin Slusarz
drivers/char/idle.c new file +5 -0
@@ -0,0 +1,445 @@
 
+#ifdef DEBUG
+#define idle_printk(args...) printk(KERN_DEBUG "IDLEDEV: " args)
+#else
+#define idle_printk(args...) do {} while (0)
+#endif

Please use the standard debug interface, i.e.

#define pr_fmt(fmt) "IDLEDEV: " fmt

and use pr_debug() everywhere, to always have printf()-style format checking.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#2 lore raw
Quoted 13 lines · Geert Uytterhoeven

drivers/char/idle.c

Quoted 9 lines · Marcin Slusarz
drivers/char/idle.c new file +5 -0
@@ -0,0 +1,445 @@
 
+#ifdef DEBUG
+#define idle_printk(args...) printk(KERN_DEBUG "IDLEDEV: " args)
+#else
+#define idle_printk(args...) do {} while (0)
+#endif

Please use the standard debug interface, i.e.

Quoted 3 lines

#define pr_fmt(fmt) "IDLEDEV: " fmt

and use pr_debug() everywhere, to always have printf()-style format checking.

Thanks for looking at the patch!

And here's a patch implementing your request:

---
From: Marcin Slusarz <[email protected]>
Subject: [PATCH] idle: convert idle_printk to pr_debug

---
 drivers/char/idle.c |   40 +++++++++++++++++++---------------------
 1 files changed, 19 insertions(+), 21 deletions(-)
drivers/char/idle.c +19 -21
index 5fe792d..d2b6b2b 100644
@@ -13,11 +13,17 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  */
+
+/* these 2 defines must be before include linux/kernel.h */
+/*#define DEBUG*/
+#define pr_fmt(fmt) "IDLEDEV: " fmt
+
 #include <linux/bug.h>
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/fs.h>
 #include <linux/hrtimer.h>
+#include <linux/kernel.h>
 #include <linux/keyboard.h>
 #include <linux/ktime.h>
 #include <linux/module.h>
@@ -32,14 +38,6 @@
 
 /* TODO: support all input devices */
 
-/*#define DEBUG*/
-
-#ifdef DEBUG
-#define idle_printk(args...) printk(KERN_DEBUG "IDLEDEV: " args)
-#else
-#define idle_printk(args...) do {} while (0)
-#endif
-
 /* spinlock protecting all data */
 static DEFINE_SPINLOCK(idle_events_lock);
 
@@ -66,7 +64,7 @@ static int idle_kb_event(struct notifier_block *this,
 {
 	unsigned long flags;
 
-	idle_printk("kb_event\n");
+	pr_debug("kb_event\n");
 
 	spin_lock_irqsave(&idle_events_lock, flags);
 
@@ -124,7 +122,7 @@ static enum hrtimer_restart idle_timer_timeout(struct hrtimer *timer)
 			container_of(timer, struct idle_file_data, timer);
 	unsigned long flags;
 
-	idle_printk("timeout\n");
+	pr_debug("timeout\n");
 
 	spin_lock_irqsave(&idle_events_lock, flags);
 
@@ -147,7 +145,7 @@ static int idle_get_event(struct idle_file_data *d, u8 __user *user_event)
 	bool wakeup;
 	u8 event;
 
-	idle_printk("idle_get_event\n");
+	pr_debug("idle_get_event\n");
 
 	spin_lock_irq(&idle_events_lock);
 
@@ -187,7 +185,7 @@ static int idle_set_timeout(struct idle_file_data *d, u32 secs)
 	if (ktime_compare(timeout, d->timeout) == 0)
 		return -EALREADY;
 
-	idle_printk("setting timeout to %u\n", secs);
+	pr_debug("setting timeout to %u\n", secs);
 
 	hrtimer_cancel(&d->timer);
 
@@ -204,7 +202,7 @@ static int idle_set_timeout(struct idle_file_data *d, u32 secs)
 
 static int idle_remove_timeout(struct idle_file_data *d)
 {
-	idle_printk("removing timeout\n");
+	pr_debug("removing timeout\n");
 
 	hrtimer_cancel(&d->timer);
 
@@ -260,13 +258,13 @@ static unsigned int idle_poll(struct file *f, poll_table *pt)
 	unsigned int mask = POLLOUT | POLLWRNORM;
 	struct idle_file_data *d = f->private_data;
 
-	idle_printk("idle_poll\n");
+	pr_debug("idle_poll\n");
 
 	spin_lock_irq(&idle_events_lock);
 
 	if (event_available(d)) {
 		mask |= POLLIN | POLLRDNORM;
-		idle_printk("idle data available\n");
+		pr_debug("idle data available\n");
 	}
 
 	if (d->waiting_for_wakeup)
@@ -296,7 +294,7 @@ static int idle_new_client(void)
 	mutex_lock(&open_counter_mutex);
 
 	if (open_counter == 0) {
-		idle_printk("first client connected\n");
+		pr_debug("first client connected\n");
 
 		last_input_event_time = ktime_get_real();
 
@@ -344,7 +342,7 @@ static void idle_client_leaving(void)
 	mutex_lock(&open_counter_mutex);
 
 	if (--open_counter == 0) {
-		idle_printk("last client disconnected\n");
+		pr_debug("last client disconnected\n");
 
 		WARN_ON(unregister_keyboard_notifier(&idle_nb));
 	}
@@ -381,11 +379,11 @@ static int __init idle_init(void)
 {
 	int err = 0;
 
-	idle_printk("starting\n");
+	pr_debug("starting\n");
 
 	idle_major = register_chrdev(0, "idle", &idle_ops);
 	if (idle_major < 0) {
-		idle_printk("cannot register device\n");
+		pr_debug("cannot register device\n");
 		return idle_major;
 	}
 
@@ -402,7 +400,7 @@ static int __init idle_init(void)
 		goto out_device;
 	}
 
-	idle_printk("device major: %d\n", idle_major);
+	pr_debug("device major: %d\n", idle_major);
 
 	if (daemon_mode) {
 		err = idle_new_client();
@@ -423,7 +421,7 @@ out_class:
 
 static void __exit idle_exit(void)
 {
-	idle_printk("exiting\n");
+	pr_debug("exiting\n");
 
 	if (daemon_mode)
 		idle_client_leaving();
Signature · 1 line
1.6.0.6
#3 lore raw
In reply to Marcin Slusarz
Quoted 9 lines · Marcin Slusarz

This virtual device can be used to tell user space about periods of time when user didn't "touch" input devices (keyboards, mice, touchpads, joysticks, etc). For now it supports only keyboard events.

Notification is done through simple select/poll + ioctl interface.

It can be used to implement screen savers, automatic suspend, autoaway/autodisconnect (e.g. in instant messangers) without any help from X server and its overhead (context switches).

I don't think this is good interface; what if you have more than one seat? And without mouse handling it is useless anyway, so....

Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#4 lore raw
Quoted 5 lines · Pavel Machek
Quoted 4 lines · Marcin Slusarz

This virtual device can be used to tell user space about periods of time when user didn't "touch" input devices (keyboards, mice, touchpads, joysticks, etc). For now it supports only keyboard events.

Quoted 8 lines
Quoted 5 lines

Notification is done through simple select/poll + ioctl interface.

It can be used to implement screen savers, automatic suspend, autoaway/autodisconnect (e.g. in instant messangers) without any help from X server and its overhead (context switches).

I don't think this is good interface; what if you have more than one seat?

Good point. I haven't thought about it. Moreover it wouldn't work with remote X sessions...

It could be used on single user systems (like laptops, personal desktops, embedded), but user space couldn't know whether it safe to use it (maybe excluding embedded).

Quoted 1 line

And without mouse handling it is useless anyway, so....

Well, I didn't want to dig too much into input layer just to discover after patch submission that my core functionality is broken :).

Marcin