fakekey.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* fakekey.c
  2. * Functions for simulating keypresses.
  3. *
  4. * Copyright (C) 2010 the Speakup Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/types.h>
  17. #include <linux/slab.h>
  18. #include <linux/preempt.h>
  19. #include <linux/percpu.h>
  20. #include <linux/input.h>
  21. #include "speakup.h"
  22. #define PRESSED 1
  23. #define RELEASED 0
  24. static DEFINE_PER_CPU(int, reporting_keystroke);
  25. static struct input_dev *virt_keyboard;
  26. int speakup_add_virtual_keyboard(void)
  27. {
  28. int err;
  29. virt_keyboard = input_allocate_device();
  30. if (!virt_keyboard)
  31. return -ENOMEM;
  32. virt_keyboard->name = "Speakup";
  33. virt_keyboard->id.bustype = BUS_VIRTUAL;
  34. virt_keyboard->phys = "speakup/input0";
  35. virt_keyboard->dev.parent = NULL;
  36. __set_bit(EV_KEY, virt_keyboard->evbit);
  37. __set_bit(KEY_DOWN, virt_keyboard->keybit);
  38. err = input_register_device(virt_keyboard);
  39. if (err) {
  40. input_free_device(virt_keyboard);
  41. virt_keyboard = NULL;
  42. }
  43. return err;
  44. }
  45. void speakup_remove_virtual_keyboard(void)
  46. {
  47. if (virt_keyboard != NULL) {
  48. input_unregister_device(virt_keyboard);
  49. virt_keyboard = NULL;
  50. }
  51. }
  52. /*
  53. * Send a simulated down-arrow to the application.
  54. */
  55. void speakup_fake_down_arrow(void)
  56. {
  57. unsigned long flags;
  58. /* disable keyboard interrupts */
  59. local_irq_save(flags);
  60. /* don't change CPU */
  61. preempt_disable();
  62. __this_cpu_write(reporting_keystroke, true);
  63. input_report_key(virt_keyboard, KEY_DOWN, PRESSED);
  64. input_report_key(virt_keyboard, KEY_DOWN, RELEASED);
  65. input_sync(virt_keyboard);
  66. __this_cpu_write(reporting_keystroke, false);
  67. /* reenable preemption */
  68. preempt_enable();
  69. /* reenable keyboard interrupts */
  70. local_irq_restore(flags);
  71. }
  72. /*
  73. * Are we handling a simulated keypress on the current CPU?
  74. * Returns a boolean.
  75. */
  76. bool speakup_fake_key_pressed(void)
  77. {
  78. return this_cpu_read(reporting_keystroke);
  79. }