blob: cd029968462f4006e595c42c8f909350f4c6775a [file] [log] [blame]
Greg Kroah-Hartman64969222018-01-11 11:08:40 +01001// SPDX-License-Identifier: GPL-2.0+
William Hubbsc6e3fd22010-10-07 13:20:02 -05002/* fakekey.c
3 * Functions for simulating keypresses.
4 *
5 * Copyright (C) 2010 the Speakup Team
William Hubbsc6e3fd22010-10-07 13:20:02 -05006 */
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 Becker911b04d2015-12-18 12:42:51 -080018static DEFINE_PER_CPU(int, reporting_keystroke);
William Hubbsc6e3fd22010-10-07 13:20:02 -050019
20static struct input_dev *virt_keyboard;
21
22int 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
48void speakup_remove_virtual_keyboard(void)
49{
Arushi Singhal0d6ff612017-03-24 16:59:59 +053050 if (virt_keyboard) {
William Hubbsc6e3fd22010-10-07 13:20:02 -050051 input_unregister_device(virt_keyboard);
William Hubbsc6e3fd22010-10-07 13:20:02 -050052 virt_keyboard = NULL;
53 }
54}
55
56/*
Arushi Singhal4d0bdcb2017-02-12 16:15:58 +053057 * Send a simulated down-arrow to the application.
58 */
William Hubbsc6e3fd22010-10-07 13:20:02 -050059void 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 Lameter4a6f4fe2010-12-06 11:16:24 -060068 __this_cpu_write(reporting_keystroke, true);
William Hubbsc6e3fd22010-10-07 13:20:02 -050069 input_report_key(virt_keyboard, KEY_DOWN, PRESSED);
70 input_report_key(virt_keyboard, KEY_DOWN, RELEASED);
covici@ccs.covici.comb1d562a2015-05-20 05:44:11 -040071 input_sync(virt_keyboard);
Christoph Lameter4a6f4fe2010-12-06 11:16:24 -060072 __this_cpu_write(reporting_keystroke, false);
William Hubbsc6e3fd22010-10-07 13:20:02 -050073
74 /* reenable preemption */
75 preempt_enable();
76 /* reenable keyboard interrupts */
77 local_irq_restore(flags);
78}
79
80/*
Arushi Singhal4d0bdcb2017-02-12 16:15:58 +053081 * Are we handling a simulated keypress on the current CPU?
82 * Returns a boolean.
83 */
William Hubbsc6e3fd22010-10-07 13:20:02 -050084bool speakup_fake_key_pressed(void)
85{
Christoph Lameter53096652010-12-06 11:16:26 -060086 return this_cpu_read(reporting_keystroke);
William Hubbsc6e3fd22010-10-07 13:20:02 -050087}