summaryrefslogtreecommitdiffstats
path: root/lib/tty/mouse.c
blob: 574e3493809f5370839d9ba70d5a4d6f5166743b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
   Mouse managing

   Copyright (C) 1994-2024
   Free Software Foundation, Inc.

   Written by:
   Andrew Borodin <aborodin@vmail.ru>, 2009.

   This file is part of the Midnight Commander.

   The Midnight Commander is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   The Midnight Commander is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file mouse.c
 *  \brief Source: mouse managing
 *
 *  Events received by clients of this library have their coordinates 0 based
 */

#include <config.h>

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

#include "lib/global.h"

#include "tty.h"
#include "tty-internal.h"       /* mouse_enabled */
#include "mouse.h"
#include "key.h"                /* define sequence */

/*** global variables ****************************************************************************/

Mouse_Type use_mouse_p = MOUSE_NONE;
gboolean mouse_enabled = FALSE;
int mouse_fd = -1;              /* for when gpm_fd changes to < 0 and the old one must be cleared from select_set */
const char *xmouse_seq;
const char *xmouse_extended_seq;

/*** file scope macro definitions ****************************************************************/

/*** file scope type declarations ****************************************************************/

/*** file scope variables ************************************************************************/

/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */

/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */

void
show_mouse_pointer (int x, int y)
{
#ifdef HAVE_LIBGPM
    if (use_mouse_p == MOUSE_GPM)
        Gpm_DrawPointer (x, y, gpm_consolefd);
#else
    (void) x;
    (void) y;
#endif /* HAVE_LIBGPM */
}

/* --------------------------------------------------------------------------------------------- */

void
init_mouse (void)
{
    switch (use_mouse_p)
    {
#ifdef HAVE_LIBGPM
    case MOUSE_NONE:
        use_mouse_p = MOUSE_GPM;
        break;
#endif /* HAVE_LIBGPM */

    case MOUSE_XTERM_NORMAL_TRACKING:
    case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
        if (xmouse_seq != NULL)
            define_sequence (MCKEY_MOUSE, xmouse_seq, MCKEY_NOACTION);
        if (xmouse_extended_seq != NULL)
            define_sequence (MCKEY_EXTENDED_MOUSE, xmouse_extended_seq, MCKEY_NOACTION);
        break;

    default:
        break;
    }

    enable_mouse ();
}

/* --------------------------------------------------------------------------------------------- */

void
enable_mouse (void)
{
    if (mouse_enabled)
        return;

    switch (use_mouse_p)
    {
#ifdef HAVE_LIBGPM
    case MOUSE_GPM:
        {
            Gpm_Connect conn;

            conn.eventMask = ~GPM_MOVE;
            conn.defaultMask = GPM_MOVE;
            conn.minMod = 0;
            conn.maxMod = 0;

            mouse_fd = Gpm_Open (&conn, 0);
            if (mouse_fd == -1)
            {
                use_mouse_p = MOUSE_NONE;
                return;
            }
            mouse_enabled = TRUE;
        }
        break;
#endif /* HAVE_LIBGPM */

    case MOUSE_XTERM_NORMAL_TRACKING:
        /* save old highlight mouse tracking */
        printf (ESC_STR "[?1001s");

        /* enable mouse tracking */
        printf (ESC_STR "[?1000h");

        /* enable SGR extended mouse reporting */
        printf (ESC_STR "[?1006h");

        fflush (stdout);
        mouse_enabled = TRUE;
        break;

    case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
        /* save old highlight mouse tracking */
        printf (ESC_STR "[?1001s");

        /* enable mouse tracking */
        printf (ESC_STR "[?1002h");

        /* enable SGR extended mouse reporting */
        printf (ESC_STR "[?1006h");

        fflush (stdout);
        mouse_enabled = TRUE;
        break;

    default:
        break;
    }
}

/* --------------------------------------------------------------------------------------------- */

void
disable_mouse (void)
{
    if (!mouse_enabled)
        return;

    mouse_enabled = FALSE;

    switch (use_mouse_p)
    {
#ifdef HAVE_LIBGPM
    case MOUSE_GPM:
        Gpm_Close ();
        break;
#endif
    case MOUSE_XTERM_NORMAL_TRACKING:
        /* disable SGR extended mouse reporting */
        printf (ESC_STR "[?1006l");

        /* disable mouse tracking */
        printf (ESC_STR "[?1000l");

        /* restore old highlight mouse tracking */
        printf (ESC_STR "[?1001r");

        fflush (stdout);
        break;
    case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
        /* disable SGR extended mouse reporting */
        printf (ESC_STR "[?1006l");

        /* disable mouse tracking */
        printf (ESC_STR "[?1002l");

        /* restore old highlight mouse tracking */
        printf (ESC_STR "[?1001r");

        fflush (stdout);
        break;
    default:
        break;
    }
}

/* --------------------------------------------------------------------------------------------- */