mouse.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. /* enable urxvt extended mouse coordinate reporting */
  110. printf (ESC_STR "[?1015h");
  111. fflush (stdout);
  112. mouse_enabled = TRUE;
  113. break;
  114. case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
  115. /* save old highlight mouse tracking */
  116. printf (ESC_STR "[?1001s");
  117. /* enable mouse tracking */
  118. printf (ESC_STR "[?1002h");
  119. /* enable urxvt extended mouse coordinate reporting */
  120. printf (ESC_STR "[?1015h");
  121. fflush (stdout);
  122. mouse_enabled = TRUE;
  123. break;
  124. default:
  125. break;
  126. }
  127. }
  128. /* --------------------------------------------------------------------------------------------- */
  129. void
  130. disable_mouse (void)
  131. {
  132. if (!mouse_enabled)
  133. return;
  134. mouse_enabled = FALSE;
  135. switch (use_mouse_p)
  136. {
  137. #ifdef HAVE_LIBGPM
  138. case MOUSE_GPM:
  139. Gpm_Close ();
  140. break;
  141. #endif
  142. case MOUSE_XTERM_NORMAL_TRACKING:
  143. /* disable urxvt extended mouse coordinate reporting */
  144. printf (ESC_STR "[?1015l");
  145. /* disable mouse tracking */
  146. printf (ESC_STR "[?1000l");
  147. /* restore old highlight mouse tracking */
  148. printf (ESC_STR "[?1001r");
  149. fflush (stdout);
  150. break;
  151. case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
  152. /* disable urxvt extended mouse coordinate reporting */
  153. printf (ESC_STR "[?1015l");
  154. /* disable mouse tracking */
  155. printf (ESC_STR "[?1002l");
  156. /* restore old highlight mouse tracking */
  157. printf (ESC_STR "[?1001r");
  158. fflush (stdout);
  159. break;
  160. default:
  161. break;
  162. }
  163. }
  164. /* --------------------------------------------------------------------------------------------- */