]> ncurses.scripts.mit.edu Git - ncurses.git/blob - man/curs_mouse.3x
ncurses 6.4 - patch 20240323
[ncurses.git] / man / curs_mouse.3x
1 '\" t
2 .\"***************************************************************************
3 .\" Copyright 2018-2023,2024 Thomas E. Dickey                                *
4 .\" Copyright 1998-2015,2017 Free Software Foundation, Inc.                  *
5 .\"                                                                          *
6 .\" Permission is hereby granted, free of charge, to any person obtaining a  *
7 .\" copy of this software and associated documentation files (the            *
8 .\" "Software"), to deal in the Software without restriction, including      *
9 .\" without limitation the rights to use, copy, modify, merge, publish,      *
10 .\" distribute, distribute with modifications, sublicense, and/or sell       *
11 .\" copies of the Software, and to permit persons to whom the Software is    *
12 .\" furnished to do so, subject to the following conditions:                 *
13 .\"                                                                          *
14 .\" The above copyright notice and this permission notice shall be included  *
15 .\" in all copies or substantial portions of the Software.                   *
16 .\"                                                                          *
17 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18 .\" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20 .\" IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21 .\" DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22 .\" OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23 .\" THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24 .\"                                                                          *
25 .\" Except as contained in this notice, the name(s) of the above copyright   *
26 .\" holders shall not be used in advertising or otherwise to promote the     *
27 .\" sale, use or other dealings in this Software without prior written       *
28 .\" authorization.                                                           *
29 .\"***************************************************************************
30 .\"
31 .\" $Id: curs_mouse.3x,v 1.96 2024/03/23 20:38:57 tom Exp $
32 .TH curs_mouse 3X 2024-03-23 "ncurses @NCURSES_MAJOR@.@NCURSES_MINOR@" "Library calls"
33 .ie \n(.g \{\
34 .ds `` \(lq
35 .ds '' \(rq
36 .\}
37 .el \{\
38 .ie t .ds `` ``
39 .el   .ds `` ""
40 .ie t .ds '' ''
41 .el   .ds '' ""
42 .\}
43 .
44 .de bP
45 .ie n  .IP \(bu 4
46 .el    .IP \(bu 2
47 ..
48 .SH NAME
49 \fB\%has_mouse\fP,
50 \fB\%getmouse\fP,
51 \fB\%ungetmouse\fP,
52 \fB\%mousemask\fP,
53 \fB\%wenclose\fP,
54 \fB\%mouse_trafo\fP,
55 \fB\%wmouse_trafo\fP,
56 \fB\%mouseinterval\fP \-
57 get mouse events in \fIcurses\fR
58 .SH SYNOPSIS
59 .nf
60 \fB#include <curses.h>
61 .PP
62 \fBtypedef unsigned long mmask_t;
63 .PP
64 \fBtypedef struct {
65 \fB    short id;         \fI/* ID to distinguish multiple devices */
66 \fB    int x, y, z;      \fI/* event coordinates */
67 \fB    mmask_t bstate;   \fI/* button state bits */
68 \fB} MEVENT;
69 .PP
70 \fBbool has_mouse(void);
71 .PP
72 \fBint getmouse(MEVENT *\fIevent\fP);
73 \fBint ungetmouse(MEVENT *\fIevent\fP);
74 .PP
75 \fBmmask_t mousemask(mmask_t \fInewmask\fP, mmask_t *\fIoldmask\fP);
76 .PP
77 \fBbool wenclose(const WINDOW *\fIwin\fP, int \fIy\fP, int \fIx\fP);
78 .PP
79 \fBbool mouse_trafo(int* \fIpY\fP, int* \fIpX\fP, bool \fIto_screen\fP);
80 \fBbool wmouse_trafo(const WINDOW* \fIwin\fP,
81                   \fBint* \fIpY\fB, int* \fIpX\fB, bool \fIto_screen\fB);\fR
82 .PP
83 \fBint mouseinterval(int \fIerval\fB);\fR
84 .fi
85 .SH DESCRIPTION
86 These functions provide an interface to mouse events from
87 \fB\%ncurses\fP(3X).
88 Mouse events are represented by \fB\%KEY_MOUSE\fP
89 pseudo-key values in the \fB\%wgetch\fP(3X) input stream.
90 .SS mousemask
91 To make mouse events visible, use the \fB\%mousemask\fP function.
92 This sets the mouse events to be reported.
93 By default, no mouse events are reported.
94 .bP
95 The function returns an updated copy of \fInewmask\fP
96 to indicate which of the specified mouse events can be reported.
97 .IP
98 If the screen has not been initialized,
99 or if the terminal does not support mouse-events,
100 this function returns 0.
101 .bP
102 If \fIoldmask\fP is non-\fBNULL\fP,
103 this function fills the indicated location with the previous value of the
104 current screen's mouse event mask.
105 .PP
106 As a side effect, setting a zero mouse mask may turn off the mouse pointer;
107 setting a nonzero mask may turn it on.
108 Whether this happens is device-dependent.
109 .SS "Mouse Events"
110 Here are the mouse event type masks which may be defined:
111 .PP
112 .TS
113 Lb Lb
114 Lb Lx.
115 Name    Description
116 =
117 BUTTON1_PRESSED mouse button 1 down
118 BUTTON1_RELEASED        mouse button 1 up
119 BUTTON1_CLICKED mouse button 1 clicked
120 BUTTON1_DOUBLE_CLICKED  mouse button 1 double clicked
121 BUTTON1_TRIPLE_CLICKED  mouse button 1 triple clicked
122 _
123 BUTTON2_PRESSED mouse button 2 down
124 BUTTON2_RELEASED        mouse button 2 up
125 BUTTON2_CLICKED mouse button 2 clicked
126 BUTTON2_DOUBLE_CLICKED  mouse button 2 double clicked
127 BUTTON2_TRIPLE_CLICKED  mouse button 2 triple clicked
128 _
129 BUTTON3_PRESSED mouse button 3 down
130 BUTTON3_RELEASED        mouse button 3 up
131 BUTTON3_CLICKED mouse button 3 clicked
132 BUTTON3_DOUBLE_CLICKED  mouse button 3 double clicked
133 BUTTON3_TRIPLE_CLICKED  mouse button 3 triple clicked
134 _
135 BUTTON4_PRESSED mouse button 4 down
136 BUTTON4_RELEASED        mouse button 4 up
137 BUTTON4_CLICKED mouse button 4 clicked
138 BUTTON4_DOUBLE_CLICKED  mouse button 4 double clicked
139 BUTTON4_TRIPLE_CLICKED  mouse button 4 triple clicked
140 _
141 BUTTON5_PRESSED mouse button 5 down
142 BUTTON5_RELEASED        mouse button 5 up
143 BUTTON5_CLICKED mouse button 5 clicked
144 BUTTON5_DOUBLE_CLICKED  mouse button 5 double clicked
145 BUTTON5_TRIPLE_CLICKED  mouse button 5 triple clicked
146 _
147 BUTTON_SHIFT    T{
148 shift was down during button state change
149 T}
150 BUTTON_CTRL     T{
151 control was down during button state change
152 T}
153 BUTTON_ALT      T{
154 alt was down during button state change
155 T}
156 ALL_MOUSE_EVENTS        report all button state changes
157 REPORT_MOUSE_POSITION   report mouse movement
158 _
159 .TE
160 .SS getmouse
161 Once a class of mouse events has been made visible in a window,
162 calling the \fB\%wgetch\fP function on that window may return
163 \fB\%KEY_MOUSE\fP as an indicator that a mouse event has been queued.
164 To read the event data and pop the event off the queue, call
165 \fB\%getmouse\fP.
166 This function will return \fBOK\fP if a mouse event
167 is actually visible in the given window, \fBERR\fP otherwise.
168 When \fB\%getmouse\fP returns \fBOK\fP, the data deposited as y and
169 x in the event structure coordinates will be screen-relative character-cell
170 coordinates.
171 The returned state mask will have exactly one bit set to
172 indicate the event type.
173 The corresponding data in the queue is marked invalid.
174 A subsequent call to \fB\%getmouse\fP will retrieve the next older
175 item from the queue.
176 .SS ungetmouse
177 The \fB\%ungetmouse\fP function behaves analogously to \fB\%ungetch\fP.
178 It pushes
179 a \fB\%KEY_MOUSE\fP event onto the input queue, and associates with that event
180 the given state data and screen-relative character-cell coordinates.
181 .SS wenclose
182 The \fB\%wenclose\fP function tests whether a given pair of screen-relative
183 character-cell coordinates is enclosed by a given window, returning \fBTRUE\fP
184 if it is and \fBFALSE\fP otherwise.
185 It is useful for determining what subset of
186 the screen windows enclose the location of a mouse event.
187 .PP
188 If the parameter is a pad,
189 \fB\%wenclose\fP uses the most recent screen coordinates used for
190 this pad in
191 \fB\%prefresh\fP(3X) or
192 \fB\%pnoutrefresh\fP(3X).
193 .SS wmouse_trafo
194 The \fB\%wmouse_trafo\fP function transforms a given pair of coordinates
195 from \fB\%stdscr\fP-relative coordinates
196 to coordinates relative to the given window or vice versa.
197 The resulting \fB\%stdscr\fP-relative coordinates are not always
198 identical to screen coordinates due to the mechanism to reserve
199 lines on top or bottom of the screen for other purposes
200 (see the \fB\%ripoffline\fP(3X) and \fB\%slk_init\fP(3X) calls, for example).
201 .bP
202 If the parameter \fIto_screen\fP is \fBTRUE\fP, the pointers
203 \fIpY, pX\fP must reference the coordinates of a location
204 inside the window \fIwin\fP.
205 They are converted to \fB\%stdscr\fP-relative coordinates and returned
206 through the pointers.
207 If the conversion was successful, the function returns \fBTRUE\fP.
208 .IP
209 If one of the parameters was \fBNULL\fP or the location is
210 not inside the window, \fBFALSE\fP is returned.
211 .bP
212 If \fIto_screen\fP is
213 \fBFALSE\fP, the pointers \fIpY, pX\fP must reference
214 \fB\%stdscr\fP-relative coordinates.
215 They are converted to window-relative coordinates if the
216 window \fIwin\fP encloses this point.
217 In this case the function returns \fBTRUE\fP.
218 .IP
219 If one of the parameters is \fBNULL\fP or the point is not inside the
220 window, \fBFALSE\fP is returned.
221 .PP
222 The referenced coordinates
223 are only replaced by the converted coordinates if the transformation was
224 successful.
225 .SS mouse_trafo
226 The \fB\%mouse_trafo\fP function performs the same translation
227 as \fB\%wmouse_trafo\fP,
228 using \fB\%stdscr\fP for \fIwin\fP.
229 .SS mouseinterval
230 The \fB\%mouseinterval\fP function sets the maximum time
231 (in thousands of a second)
232 that can elapse between press and release events for them to
233 be resolved as a
234 .IR click .
235 An application might interpret button press and release events separated
236 by more than the mouse interval as a \*(``long press\*('',
237 or,
238 with motion,
239 as a \*(``drag\*(''.
240 .PP
241 Calling \fB\%mouseinterval(0)\fP disables click resolution.
242 When
243 .I \%ncurses
244 detects a mouse event,
245 it awaits further input activity up to this interval,
246 and then checks for a subsequent mouse event which can be combined
247 with the first event.
248 If the timeout expires without input activity
249 (which would happen with a zero interval),
250 then no click resolution will occur.
251 .PP
252 This function returns the previous interval value.
253 Use \fB\%mouseinterval(\-1)\fP to obtain the interval without altering it.
254 .PP
255 The mouse interval is set to one sixth of a second
256 when the corresponding screen is initialized,
257 e.g., in \fBinitscr\fP(3X) or \fBsetupterm\fP(3X).
258 .SS has_mouse
259 The \fB\%has_mouse\fP function returns \fBTRUE\fP if the mouse driver
260 has been successfully initialized,
261 and \fBFALSE\fP otherwise.
262 .PP
263 Mouse events are ignored when input is in cooked mode, and
264 cause an error beep when cooked mode is being simulated in a window by a
265 function such as \fB\%getstr\fP that expects a linefeed for input-loop
266 termination.
267 .SH RETURN VALUE
268 \fB\%has_mouse\fP,
269 \fB\%wenclose\fP,
270 \fB\%mouse_trafo\fP,
271 and
272 \fB\%wmouse_trafo\fP
273 return \fBTRUE\fP or \fBFALSE\fP as noted above.
274 .PP
275 \fB\%getmouse\fP and \fB\%ungetmouse\fP
276 return \fBERR\fP upon failure and \fBOK\fP upon success.
277 .PP
278 \fB\%getmouse\fP fails if:
279 .bP
280 no mouse driver was initialized,
281 .bP
282 the mask of reportable events is zero,
283 .bP
284 a mouse event was detected that does not match the mask,
285 .bP
286 or if no more events remain in the queue.
287 .PP
288 \fB\%ungetmouse\fP returns an error if the event queue is full.
289 .PP
290 \fB\%mousemask\fP
291 returns the mask of reportable events.
292 .PP
293 \fB\%mouseinterval\fP
294 returns the previous interval value, unless
295 the terminal was not initialized.
296 In that case, it returns the maximum interval value (166).
297 .SH NOTES
298 The order of the \fB\%MEVENT\fP structure members is not guaranteed.
299 Additional fields may be added to the structure in the future.
300 .PP
301 Under
302 .IR \%ncurses ,
303 these calls are implemented using either
304 .IR \%xterm 's
305 built-in mouse-tracking API or
306 platform-specific drivers including
307 .RS 3
308 .bP
309 Alessandro Rubini's gpm server
310 .bP
311 FreeBSD sysmouse
312 .bP
313 OS/2 EMX
314 .RE
315 .PP
316 If you are using an unsupported configuration,
317 mouse events will not be visible to
318 \fI\%ncurses\fP (and the \fB\%mousemask\fP function will always
319 return \fB0\fP).
320 .PP
321 If the
322 .I \%term\%info
323 entry contains a \fBXM\fP string,
324 this is used in the
325 .I \%xterm
326 mouse driver to control the
327 way the terminal is initialized for mouse operation.
328 The default, if \fBXM\fP is not found,
329 corresponds to private mode 1000 of
330 .I \%xterm:
331 .PP
332 .RS 3
333 \eE[?1000%?%p1%{1}%=%th%el%;
334 .RE
335 .PP
336 The mouse driver also recognizes a newer
337 .I \%xterm
338 private mode 1006,
339 e.g.,
340 .PP
341 .RS 3
342 \eE[?1006;1000%?%p1%{1}%=%th%el%;
343 .RE
344 .PP
345 The \fIz\fP member in the event structure is not presently used.
346 It is intended
347 for use with touch screens (which may be pressure-sensitive) or with
348 3D-mice/trackballs/power gloves.
349 .PP
350 The \fB\%ALL_MOUSE_EVENTS\fP class does not
351 include \fB\%REPORT_MOUSE_POSITION\fP.
352 They are distinct.
353 For example,
354 in
355 .IR \%xterm ,
356 wheel/scrolling mice send position reports as a sequence of
357 presses of buttons 4 or 5 without matching button-releases.
358 .SH EXTENSIONS
359 These functions were designed for
360 \fB\%ncurses\fP(3X),
361 and are not found in SVr4
362 .IR curses ,
363 4.4BSD
364 .IR curses ,
365 or any other previous curses implementation.
366 (SVr4
367 .I curses
368 did have a
369 .I \%getmouse
370 function,
371 which took no argument and returned a different type.)
372 .SH PORTABILITY
373 Applications employing the
374 .I \%ncurses
375 mouse extension should condition its use on the visibility of the
376 .B \%NCURSES_MOUSE_VERSION
377 preprocessor macro.
378 When the interface changes,
379 the macro's value increments.
380 Multiple versions are available when
381 .I \%ncurses
382 is configured;
383 see section \*(``ALTERNATE CONFIGURATIONS\*('' of \fB\%ncurses\fP(3X).
384 The following values may be specified.
385 .RS 3
386 .TP 3
387 1
388 has definitions for reserved events.
389 The mask uses 28 bits.
390 .TP 3
391 2
392 adds definitions for button 5,
393 removes the definitions for reserved events.
394 The mask uses 29 bits.
395 .RE
396 .PP
397 SVr4
398 .I curses
399 had support for the mouse in a variant of \fI\%xterm\fP(1).
400 It is mentioned in a few places,
401 with little supporting documentation.
402 .bP
403 Its \*(``libcurses\*('' manual page lists functions for this feature
404 prototyped in \fI\%curses.h\fP.
405 .PP
406 .RS 8
407 .EX
408 extern int mouse_set(long int);
409 extern int mouse_on(long int);
410 extern int mouse_off(long int);
411 extern int request_mouse_pos(void);
412 extern int map_button(unsigned long);
413 extern void wmouse_position(WINDOW *, int *, int *);
414 extern unsigned long getmouse(void), getbmap(void);
415 .EE
416 .RE
417 .bP
418 Its \*(``terminfo\*('' manual page lists capabilities for the feature.
419 .\" These don't appear in in the SVID 4th edition, Volume 3,
420 .\" terminfo(TI_ENV) man page.  They can be found in, e.g., the "z/OS
421 .\" V1R1.0 C Curses" book, Chapter 17, pp. 179-186 (PDF 213-220).
422 .RS 8
423 .TS
424 Lb Lb Lb Lx.
425 buttons btns    BT      T{
426 Number of buttons on the mouse
427 T}
428 get_mouse       getm    Gm      T{
429 Curses should get button events
430 T}
431 key_mouse       kmous   Km      T{
432 0631, Mouse event has occurred
433 T}
434 mouse_info      minfo   Mi      T{
435 Mouse status information
436 T}
437 req_mouse_pos   reqmp   RQ      T{
438 Request mouse position report
439 T}
440 .TE
441 .RE
442 .bP
443 The interface made assumptions
444 (as does
445 .IR \%ncurses )
446 about the escape sequences sent to and received from the terminal.
447 .IP
448 For instance,
449 the SVr4
450 .I curses
451 library used the \fB\%get_mouse\fP capability to tell the terminal which
452 mouse button events it should send,
453 passing the mouse-button bit mask to the terminal.
454 Also, it could ask the terminal
455 where the mouse was using the \fB\%req_mouse_pos\fP capability.
456 .IP
457 Those features required a terminal program that had been modified
458 to work with SVr4
459 .IR curses .
460 They were not part of the X Consortium's
461 .IR \%xterm .
462 .PP
463 When developing the
464 .I \%xterm
465 mouse support for
466 .I \%ncurses
467 in September 1995,
468 Eric Raymond was uninterested in using the same interface due to its
469 lack of documentation.
470 Later, in 1998, Mark Hesseling provided support in
471 .I \%PDCurses
472 2.3 using the SVr4 interface.
473 .IR \%PDCurses ,
474 however,
475 does not use video terminals,
476 making it unnecessary to be concerned about compatibility with the
477 escape sequences.
478 .SH BUGS
479 Mouse events from
480 .I \%xterm
481 are
482 .I not
483 ignored in cooked mode if they have been enabled by \fB\%mousemask\fP.
484 Instead,
485 the
486 .I \%xterm
487 mouse report sequence appears in the string read.
488 .PP
489 Mouse event reports from
490 .I \%xterm
491 are not detected correctly in a window with keypad application mode
492 disabled,
493 since they are interpreted as a variety of function key.
494 Set the terminal's
495 .I \%term\%info
496 capability \fB\%kmous\fP to \*(``\eE[M\*(''
497 (the beginning of the response from
498 .I \%xterm
499 for mouse clicks).
500 Other values of \fB\%kmous\fP are permitted under the same assumption,
501 that is,
502 the report begins with that sequence.
503 .PP
504 Because there are no standard response sequences that serve to identify
505 terminals supporting the
506 .I \%xterm
507 mouse protocol,
508 .I \%ncurses
509 assumes that if \fB\%kmous\fP is defined in the terminal description,
510 or if the terminal type's primary name or aliases contain the string
511 \%\*(``xterm\*('',
512 then the terminal may send mouse events.
513 The \fB\%kmous\fP capability is checked first,
514 allowing use of newer
515 .I \%xterm
516 mouse protocols,
517 such as its private mode 1006.
518 .SH SEE ALSO
519 \fB\%curses\fP(3X),
520 \fB\%curs_inopts\fP(3X),
521 \fB\%curs_kernel\fP(3X),
522 \fB\%curs_pad\fP(3X),
523 \fB\%curs_slk\fP(3X),
524 \fB\%curs_variables\fP(3X)