mouse.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. const char *xmouse_seq;
  38. /*** file scope macro definitions ****************************************************************/
  39. /*** file scope type declarations ****************************************************************/
  40. /*** file scope variables ************************************************************************/
  41. /*** file scope functions ************************************************************************/
  42. /* --------------------------------------------------------------------------------------------- */
  43. /* --------------------------------------------------------------------------------------------- */
  44. /*** public functions ****************************************************************************/
  45. /* --------------------------------------------------------------------------------------------- */
  46. void
  47. show_mouse_pointer (int x, int y)
  48. {
  49. #ifdef HAVE_LIBGPM
  50. if (use_mouse_p == MOUSE_GPM)
  51. Gpm_DrawPointer (x, y, gpm_consolefd);
  52. #else
  53. (void) x;
  54. (void) y;
  55. #endif /* HAVE_LIBGPM */
  56. }
  57. /* --------------------------------------------------------------------------------------------- */
  58. void
  59. init_mouse (void)
  60. {
  61. switch (use_mouse_p)
  62. {
  63. #ifdef HAVE_LIBGPM
  64. case MOUSE_NONE:
  65. use_mouse_p = MOUSE_GPM;
  66. break;
  67. #endif /* HAVE_LIBGPM */
  68. case MOUSE_XTERM_NORMAL_TRACKING:
  69. case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
  70. define_sequence (MCKEY_MOUSE, xmouse_seq, MCKEY_NOACTION);
  71. break;
  72. default:
  73. break;
  74. }
  75. enable_mouse ();
  76. }
  77. /* --------------------------------------------------------------------------------------------- */
  78. void
  79. enable_mouse (void)
  80. {
  81. if (mouse_enabled)
  82. return;
  83. switch (use_mouse_p)
  84. {
  85. #ifdef HAVE_LIBGPM
  86. case MOUSE_GPM:
  87. {
  88. int mouse_d;
  89. Gpm_Connect conn;
  90. conn.eventMask = ~GPM_MOVE;
  91. conn.defaultMask = GPM_MOVE;
  92. conn.minMod = 0;
  93. conn.maxMod = 0;
  94. mouse_d = Gpm_Open (&conn, 0);
  95. if (mouse_d == -1)
  96. {
  97. use_mouse_p = MOUSE_NONE;
  98. return;
  99. }
  100. mouse_enabled = TRUE;
  101. }
  102. break;
  103. #endif /* HAVE_LIBGPM */
  104. case MOUSE_XTERM_NORMAL_TRACKING:
  105. /* save old highlight mouse tracking */
  106. printf (ESC_STR "[?1001s");
  107. /* enable mouse tracking */
  108. printf (ESC_STR "[?1000h");
  109. fflush (stdout);
  110. mouse_enabled = TRUE;
  111. break;
  112. case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
  113. /* save old highlight mouse tracking */
  114. printf (ESC_STR "[?1001s");
  115. /* enable mouse tracking */
  116. printf (ESC_STR "[?1002h");
  117. fflush (stdout);
  118. mouse_enabled = TRUE;
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. /* --------------------------------------------------------------------------------------------- */
  125. void
  126. disable_mouse (void)
  127. {
  128. if (!mouse_enabled)
  129. return;
  130. mouse_enabled = FALSE;
  131. switch (use_mouse_p)
  132. {
  133. #ifdef HAVE_LIBGPM
  134. case MOUSE_GPM:
  135. Gpm_Close ();
  136. break;
  137. #endif
  138. case MOUSE_XTERM_NORMAL_TRACKING:
  139. /* disable mouse tracking */
  140. printf (ESC_STR "[?1000l");
  141. /* restore old highlight mouse tracking */
  142. printf (ESC_STR "[?1001r");
  143. fflush (stdout);
  144. break;
  145. case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
  146. /* disable mouse tracking */
  147. printf (ESC_STR "[?1002l");
  148. /* restore old highlight mouse tracking */
  149. printf (ESC_STR "[?1001r");
  150. fflush (stdout);
  151. break;
  152. default:
  153. break;
  154. }
  155. }
  156. /* --------------------------------------------------------------------------------------------- */