Greg Kroah-Hartman | 6496922 | 2018-01-11 11:08:40 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 2 | /* fakekey.c |
| 3 | * Functions for simulating keypresses. |
| 4 | * |
| 5 | * Copyright (C) 2010 the Speakup Team |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 6 | */ |
| 7 | #include <linux/types.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/preempt.h> |
| 10 | #include <linux/percpu.h> |
| 11 | #include <linux/input.h> |
| 12 | |
| 13 | #include "speakup.h" |
| 14 | |
| 15 | #define PRESSED 1 |
| 16 | #define RELEASED 0 |
| 17 | |
Jeff Becker | 911b04d | 2015-12-18 12:42:51 -0800 | [diff] [blame] | 18 | static DEFINE_PER_CPU(int, reporting_keystroke); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 19 | |
| 20 | static struct input_dev *virt_keyboard; |
| 21 | |
| 22 | int speakup_add_virtual_keyboard(void) |
| 23 | { |
| 24 | int err; |
| 25 | |
| 26 | virt_keyboard = input_allocate_device(); |
| 27 | |
| 28 | if (!virt_keyboard) |
| 29 | return -ENOMEM; |
| 30 | |
| 31 | virt_keyboard->name = "Speakup"; |
| 32 | virt_keyboard->id.bustype = BUS_VIRTUAL; |
| 33 | virt_keyboard->phys = "speakup/input0"; |
| 34 | virt_keyboard->dev.parent = NULL; |
| 35 | |
| 36 | __set_bit(EV_KEY, virt_keyboard->evbit); |
| 37 | __set_bit(KEY_DOWN, virt_keyboard->keybit); |
| 38 | |
| 39 | err = input_register_device(virt_keyboard); |
| 40 | if (err) { |
| 41 | input_free_device(virt_keyboard); |
| 42 | virt_keyboard = NULL; |
| 43 | } |
| 44 | |
| 45 | return err; |
| 46 | } |
| 47 | |
| 48 | void speakup_remove_virtual_keyboard(void) |
| 49 | { |
Arushi Singhal | 0d6ff61 | 2017-03-24 16:59:59 +0530 | [diff] [blame] | 50 | if (virt_keyboard) { |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 51 | input_unregister_device(virt_keyboard); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 52 | virt_keyboard = NULL; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /* |
Arushi Singhal | 4d0bdcb | 2017-02-12 16:15:58 +0530 | [diff] [blame] | 57 | * Send a simulated down-arrow to the application. |
| 58 | */ |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 59 | void speakup_fake_down_arrow(void) |
| 60 | { |
| 61 | unsigned long flags; |
| 62 | |
| 63 | /* disable keyboard interrupts */ |
| 64 | local_irq_save(flags); |
| 65 | /* don't change CPU */ |
| 66 | preempt_disable(); |
| 67 | |
Christoph Lameter | 4a6f4fe | 2010-12-06 11:16:24 -0600 | [diff] [blame] | 68 | __this_cpu_write(reporting_keystroke, true); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 69 | input_report_key(virt_keyboard, KEY_DOWN, PRESSED); |
| 70 | input_report_key(virt_keyboard, KEY_DOWN, RELEASED); |
covici@ccs.covici.com | b1d562a | 2015-05-20 05:44:11 -0400 | [diff] [blame] | 71 | input_sync(virt_keyboard); |
Christoph Lameter | 4a6f4fe | 2010-12-06 11:16:24 -0600 | [diff] [blame] | 72 | __this_cpu_write(reporting_keystroke, false); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 73 | |
| 74 | /* reenable preemption */ |
| 75 | preempt_enable(); |
| 76 | /* reenable keyboard interrupts */ |
| 77 | local_irq_restore(flags); |
| 78 | } |
| 79 | |
| 80 | /* |
Arushi Singhal | 4d0bdcb | 2017-02-12 16:15:58 +0530 | [diff] [blame] | 81 | * Are we handling a simulated keypress on the current CPU? |
| 82 | * Returns a boolean. |
| 83 | */ |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 84 | bool speakup_fake_key_pressed(void) |
| 85 | { |
Christoph Lameter | 5309665 | 2010-12-06 11:16:26 -0600 | [diff] [blame] | 86 | return this_cpu_read(reporting_keystroke); |
William Hubbs | c6e3fd2 | 2010-10-07 13:20:02 -0500 | [diff] [blame] | 87 | } |