mouse.c 5.7 KB

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