mouse.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. Mouse managing
  3. Copyright (C) 1994-2024
  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
  37. = -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. if (xmouse_seq != NULL)
  73. define_sequence (MCKEY_MOUSE, xmouse_seq, MCKEY_NOACTION);
  74. if (xmouse_extended_seq != NULL)
  75. define_sequence (MCKEY_EXTENDED_MOUSE, xmouse_extended_seq, MCKEY_NOACTION);
  76. break;
  77. default:
  78. break;
  79. }
  80. enable_mouse ();
  81. }
  82. /* --------------------------------------------------------------------------------------------- */
  83. void
  84. enable_mouse (void)
  85. {
  86. if (mouse_enabled)
  87. return;
  88. switch (use_mouse_p)
  89. {
  90. #ifdef HAVE_LIBGPM
  91. case MOUSE_GPM:
  92. {
  93. Gpm_Connect conn;
  94. conn.eventMask = ~GPM_MOVE;
  95. conn.defaultMask = GPM_MOVE;
  96. conn.minMod = 0;
  97. conn.maxMod = 0;
  98. mouse_fd = Gpm_Open (&conn, 0);
  99. if (mouse_fd == -1)
  100. {
  101. use_mouse_p = MOUSE_NONE;
  102. return;
  103. }
  104. mouse_enabled = TRUE;
  105. }
  106. break;
  107. #endif /* HAVE_LIBGPM */
  108. case MOUSE_XTERM_NORMAL_TRACKING:
  109. /* save old highlight mouse tracking */
  110. printf (ESC_STR "[?1001s");
  111. /* enable mouse tracking */
  112. printf (ESC_STR "[?1000h");
  113. /* enable SGR extended mouse reporting */
  114. printf (ESC_STR "[?1006h");
  115. fflush (stdout);
  116. mouse_enabled = TRUE;
  117. break;
  118. case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
  119. /* save old highlight mouse tracking */
  120. printf (ESC_STR "[?1001s");
  121. /* enable mouse tracking */
  122. printf (ESC_STR "[?1002h");
  123. /* enable SGR extended mouse reporting */
  124. printf (ESC_STR "[?1006h");
  125. fflush (stdout);
  126. mouse_enabled = TRUE;
  127. break;
  128. default:
  129. break;
  130. }
  131. }
  132. /* --------------------------------------------------------------------------------------------- */
  133. void
  134. disable_mouse (void)
  135. {
  136. if (!mouse_enabled)
  137. return;
  138. mouse_enabled = FALSE;
  139. switch (use_mouse_p)
  140. {
  141. #ifdef HAVE_LIBGPM
  142. case MOUSE_GPM:
  143. Gpm_Close ();
  144. break;
  145. #endif
  146. case MOUSE_XTERM_NORMAL_TRACKING:
  147. /* disable SGR extended mouse reporting */
  148. printf (ESC_STR "[?1006l");
  149. /* disable mouse tracking */
  150. printf (ESC_STR "[?1000l");
  151. /* restore old highlight mouse tracking */
  152. printf (ESC_STR "[?1001r");
  153. fflush (stdout);
  154. break;
  155. case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
  156. /* disable SGR extended mouse reporting */
  157. printf (ESC_STR "[?1006l");
  158. /* disable mouse tracking */
  159. printf (ESC_STR "[?1002l");
  160. /* restore old highlight mouse tracking */
  161. printf (ESC_STR "[?1001r");
  162. fflush (stdout);
  163. break;
  164. default:
  165. break;
  166. }
  167. }
  168. /* --------------------------------------------------------------------------------------------- */