fakekey.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/types.h>
  21. #include <linux/slab.h>
  22. #include <linux/preempt.h>
  23. #include <linux/percpu.h>
  24. #include <linux/input.h>
  25. #include "speakup.h"
  26. #define PRESSED 1
  27. #define RELEASED 0
  28. static DEFINE_PER_CPU(bool, reporting_keystroke);
  29. static struct input_dev *virt_keyboard;
  30. int speakup_add_virtual_keyboard(void)
  31. {
  32. int err;
  33. virt_keyboard = input_allocate_device();
  34. if (!virt_keyboard)
  35. return -ENOMEM;
  36. virt_keyboard->name = "Speakup";
  37. virt_keyboard->id.bustype = BUS_VIRTUAL;
  38. virt_keyboard->phys = "speakup/input0";
  39. virt_keyboard->dev.parent = NULL;
  40. __set_bit(EV_KEY, virt_keyboard->evbit);
  41. __set_bit(KEY_DOWN, virt_keyboard->keybit);
  42. err = input_register_device(virt_keyboard);
  43. if (err) {
  44. input_free_device(virt_keyboard);
  45. virt_keyboard = NULL;
  46. }
  47. return err;
  48. }
  49. void speakup_remove_virtual_keyboard(void)
  50. {
  51. if (virt_keyboard != NULL) {
  52. input_unregister_device(virt_keyboard);
  53. virt_keyboard = NULL;
  54. }
  55. }
  56. /*
  57. * Send a simulated down-arrow to the application.
  58. */
  59. void speakup_fake_down_arrow(void)
  60. {
  61. unsigned long flags;
  62. /* disable keyboard interrupts */
  63. local_irq_save(flags);
  64. /* don't change CPU */
  65. preempt_disable();
  66. __this_cpu_write(reporting_keystroke, true);
  67. input_report_key(virt_keyboard, KEY_DOWN, PRESSED);
  68. input_report_key(virt_keyboard, KEY_DOWN, RELEASED);
  69. input_sync(virt_keyboard);
  70. __this_cpu_write(reporting_keystroke, false);
  71. /* reenable preemption */
  72. preempt_enable();
  73. /* reenable keyboard interrupts */
  74. local_irq_restore(flags);
  75. }
  76. /*
  77. * Are we handling a simulated keypress on the current CPU?
  78. * Returns a boolean.
  79. */
  80. bool speakup_fake_key_pressed(void)
  81. {
  82. return this_cpu_read(reporting_keystroke);
  83. }