mouse.c 5.6 KB

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