1 // SPDX-License-Identifier: GPL-2.0+
3 * Functions for simulating keypresses.
5 * Copyright (C) 2010 the Speakup Team
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>
18 static DEFINE_PER_CPU(int, reporting_keystroke
);
20 static struct input_dev
*virt_keyboard
;
22 int speakup_add_virtual_keyboard(void)
26 virt_keyboard
= input_allocate_device();
31 virt_keyboard
->name
= "Speakup";
32 virt_keyboard
->id
.bustype
= BUS_VIRTUAL
;
33 virt_keyboard
->phys
= "speakup/input0";
34 virt_keyboard
->dev
.parent
= NULL
;
36 __set_bit(EV_KEY
, virt_keyboard
->evbit
);
37 __set_bit(KEY_DOWN
, virt_keyboard
->keybit
);
39 err
= input_register_device(virt_keyboard
);
41 input_free_device(virt_keyboard
);
48 void speakup_remove_virtual_keyboard(void)
51 input_unregister_device(virt_keyboard
);
57 * Send a simulated down-arrow to the application.
59 void speakup_fake_down_arrow(void)
63 /* disable keyboard interrupts */
64 local_irq_save(flags
);
65 /* don't change CPU */
68 __this_cpu_write(reporting_keystroke
, true);
69 input_report_key(virt_keyboard
, KEY_DOWN
, PRESSED
);
70 input_report_key(virt_keyboard
, KEY_DOWN
, RELEASED
);
71 input_sync(virt_keyboard
);
72 __this_cpu_write(reporting_keystroke
, false);
74 /* reenable preemption */
76 /* reenable keyboard interrupts */
77 local_irq_restore(flags
);
81 * Are we handling a simulated keypress on the current CPU?
84 bool speakup_fake_key_pressed(void)
86 return this_cpu_read(reporting_keystroke
);