mouse.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Mouse managing
  2. Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006,
  3. 2007, 2009 Free Software Foundation, Inc.
  4. Written by:
  5. Andrew Borodin <aborodin@vmail.ru>, 2009.
  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. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  17. /** \file mouse.c
  18. * \brief Source: mouse managing
  19. *
  20. * Events received by clients of this library have their coordinates 0 based
  21. */
  22. #include <config.h>
  23. #include <stdio.h>
  24. #include <sys/types.h>
  25. #include <unistd.h>
  26. #include "lib/global.h"
  27. #include "tty.h"
  28. #include "tty-internal.h" /* mouse_enabled */
  29. #include "mouse.h"
  30. #include "key.h" /* define sequence */
  31. /*** global variables ****************************************************************************/
  32. Mouse_Type use_mouse_p = MOUSE_NONE;
  33. gboolean mouse_enabled = FALSE;
  34. const char *xmouse_seq;
  35. /*** file scope macro definitions ****************************************************************/
  36. /*** file scope type declarations ****************************************************************/
  37. /*** file scope variables ************************************************************************/
  38. /*** file scope functions ************************************************************************/
  39. /* --------------------------------------------------------------------------------------------- */
  40. /* --------------------------------------------------------------------------------------------- */
  41. /*** public functions ****************************************************************************/
  42. /* --------------------------------------------------------------------------------------------- */
  43. void
  44. show_mouse_pointer (int x, int y)
  45. {
  46. #ifdef HAVE_LIBGPM
  47. if (use_mouse_p == MOUSE_GPM)
  48. Gpm_DrawPointer (x, y, gpm_consolefd);
  49. #else
  50. (void) x;
  51. (void) y;
  52. #endif /* HAVE_LIBGPM */
  53. }
  54. /* --------------------------------------------------------------------------------------------- */
  55. void
  56. init_mouse (void)
  57. {
  58. switch (use_mouse_p)
  59. {
  60. #ifdef HAVE_LIBGPM
  61. case MOUSE_NONE:
  62. use_mouse_p = MOUSE_GPM;
  63. break;
  64. #endif /* HAVE_LIBGPM */
  65. case MOUSE_XTERM_NORMAL_TRACKING:
  66. case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
  67. define_sequence (MCKEY_MOUSE, xmouse_seq, MCKEY_NOACTION);
  68. break;
  69. default:
  70. break;
  71. }
  72. enable_mouse ();
  73. }
  74. /* --------------------------------------------------------------------------------------------- */
  75. void
  76. enable_mouse (void)
  77. {
  78. if (mouse_enabled)
  79. return;
  80. switch (use_mouse_p)
  81. {
  82. #ifdef HAVE_LIBGPM
  83. case MOUSE_GPM:
  84. {
  85. int mouse_d;
  86. Gpm_Connect conn;
  87. conn.eventMask = ~GPM_MOVE;
  88. conn.defaultMask = GPM_MOVE;
  89. conn.minMod = 0;
  90. conn.maxMod = 0;
  91. mouse_d = Gpm_Open (&conn, 0);
  92. if (mouse_d == -1)
  93. {
  94. use_mouse_p = MOUSE_NONE;
  95. return;
  96. }
  97. mouse_enabled = 1;
  98. }
  99. break;
  100. #endif /* HAVE_LIBGPM */
  101. case MOUSE_XTERM_NORMAL_TRACKING:
  102. /* save old highlight mouse tracking */
  103. printf (ESC_STR "[?1001s");
  104. /* enable mouse tracking */
  105. printf (ESC_STR "[?1000h");
  106. fflush (stdout);
  107. mouse_enabled = TRUE;
  108. break;
  109. case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
  110. /* save old highlight mouse tracking */
  111. printf (ESC_STR "[?1001s");
  112. /* enable mouse tracking */
  113. printf (ESC_STR "[?1002h");
  114. fflush (stdout);
  115. mouse_enabled = TRUE;
  116. break;
  117. default:
  118. break;
  119. }
  120. }
  121. /* --------------------------------------------------------------------------------------------- */
  122. void
  123. disable_mouse (void)
  124. {
  125. if (!mouse_enabled)
  126. return;
  127. mouse_enabled = FALSE;
  128. switch (use_mouse_p)
  129. {
  130. #ifdef HAVE_LIBGPM
  131. case MOUSE_GPM:
  132. Gpm_Close ();
  133. break;
  134. #endif
  135. case MOUSE_XTERM_NORMAL_TRACKING:
  136. /* disable mouse tracking */
  137. printf (ESC_STR "[?1000l");
  138. /* restore old highlight mouse tracking */
  139. printf (ESC_STR "[?1001r");
  140. fflush (stdout);
  141. break;
  142. case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
  143. /* disable mouse tracking */
  144. printf (ESC_STR "[?1002l");
  145. /* restore old highlight mouse tracking */
  146. printf (ESC_STR "[?1001r");
  147. fflush (stdout);
  148. break;
  149. default:
  150. break;
  151. }
  152. }
  153. /* --------------------------------------------------------------------------------------------- */