mouse.c 5.7 KB

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