drivers/wifi: Remove unnecessary data structure copy
[coreboot2.git] / payloads / libpayload / curses / PDCurses / pdcurses / mouse.c
blob5071886d64a03d01184e7ac3321993c588eb48cd
1 /* Public Domain Curses */
3 #include <curspriv.h>
5 RCSID("$Id: mouse.c,v 1.45 2008/07/13 16:08:18 wmcbrine Exp $")
7 /*man-start**************************************************************
9 Name: mouse
11 Synopsis:
12 int mouse_set(unsigned long mbe);
13 int mouse_on(unsigned long mbe);
14 int mouse_off(unsigned long mbe);
15 int request_mouse_pos(void);
16 int map_button(unsigned long button);
17 void wmouse_position(WINDOW *win, int *y, int *x);
18 unsigned long getmouse(void);
19 unsigned long getbmap(void);
21 int mouseinterval(int wait);
22 bool wenclose(const WINDOW *win, int y, int x);
23 bool wmouse_trafo(const WINDOW *win, int *y, int *x, bool to_screen);
24 bool mouse_trafo(int *y, int *x, bool to_screen);
25 mmask_t mousemask(mmask_t mask, mmask_t *oldmask);
26 int nc_getmouse(MEVENT *event);
27 int ungetmouse(MEVENT *event);
29 Description:
30 As of PDCurses 3.0, there are two separate mouse interfaces: the
31 classic interface, which is based on the undocumented Sys V
32 mouse functions; and an ncurses-compatible interface. Both are
33 active at all times, and you can mix and match functions from
34 each, though it's not recommended. The ncurses interface is
35 essentially an emulation layer built on top of the classic
36 interface; it's here to allow easier porting of ncurses apps.
38 The classic interface: mouse_set(), mouse_on(), mouse_off(),
39 request_mouse_pos(), map_button(), wmouse_position(),
40 getmouse(), and getbmap(). An application using this interface
41 would start by calling mouse_set() or mouse_on() with a non-zero
42 value, often ALL_MOUSE_EVENTS. Then it would check for a
43 KEY_MOUSE return from getch(). If found, it would call
44 request_mouse_pos() to get the current mouse status.
46 mouse_set(), mouse_on() and mouse_off() are analagous to
47 attrset(), attron() and attroff(). These functions set the
48 mouse button events to trap. The button masks used in these
49 functions are defined in curses.h and can be or'ed together.
50 They are the group of masks starting with BUTTON1_RELEASED.
52 request_mouse_pos() requests curses to fill in the Mouse_status
53 structure with the current state of the mouse.
55 map_button() enables the specified mouse action to activate the
56 Soft Label Keys if the action occurs over the area of the screen
57 where the Soft Label Keys are displayed. The mouse actions are
58 defined in curses.h in the group that starts with BUTTON_RELEASED.
60 wmouse_position() determines if the current mouse position is
61 within the window passed as an argument. If the mouse is
62 outside the current window, -1 is returned in the y and x
63 arguments; otherwise the y and x coordinates of the mouse
64 (relative to the top left corner of the window) are returned in
65 y and x.
67 getmouse() returns the current status of the trapped mouse
68 buttons as set by mouse_set() or mouse_on().
70 getbmap() returns the current status of the button action used
71 to map a mouse action to the Soft Label Keys as set by the
72 map_button() function.
74 The ncurses interface: mouseinterval(), wenclose(),
75 wmouse_trafo(), mouse_trafo(), mousemask(), nc_getmouse(), and
76 ungetmouse(). A typical application using this interface would
77 start by calling mousemask() with a non-zero value, often
78 ALL_MOUSE_EVENTS. Then it would check for a KEY_MOUSE return
79 from getch(). If found, it would call nc_getmouse() to get the
80 current mouse status.
82 mouseinterval() sets the timeout for a mouse click. On all
83 current platforms, PDCurses receives mouse button press and
84 release events, but must synthesize click events. It does this
85 by checking whether a release event is queued up after a press
86 event. If it gets a press event, and there are no more events
87 waiting, it will wait for the timeout interval, then check again
88 for a release. A press followed by a release is reported as
89 BUTTON_CLICKED; otherwise it's passed through as BUTTON_PRESSED.
90 The default timeout is 150ms; valid values are 0 (no clicks
91 reported) through 1000ms. In x11, the timeout can also be set
92 via the clickPeriod resource. The return value from
93 mouseinterval() is the old timeout. To check the old value
94 without setting a new one, call it with a parameter of -1. Note
95 that although there's no classic equivalent for this function
96 (apart from the clickPeriod resource), the value set applies in
97 both interfaces.
99 wenclose() reports whether the given screen-relative y, x
100 coordinates fall within the given window.
102 wmouse_trafo() converts between screen-relative and window-
103 relative coordinates. A to_screen parameter of TRUE means to
104 convert from window to screen; otherwise the reverse. The
105 function returns FALSE if the coordinates aren't within the
106 window, or if any of the parameters are NULL. The coordinates
107 have been converted when the function returns TRUE.
109 mouse_trafo() is the stdscr version of wmouse_trafo().
111 mousemask() is nearly equivalent to mouse_set(), but instead of
112 OK/ERR, it returns the value of the mask after setting it. (This
113 isn't necessarily the same value passed in, since the mask could
114 be altered on some platforms.) And if the second parameter is a
115 non-null pointer, mousemask() stores the previous mask value
116 there. Also, since the ncurses interface doesn't work with
117 PDCurses' BUTTON_MOVED events, mousemask() filters them out.
119 nc_getmouse() returns the current mouse status in an MEVENT
120 struct. This is equivalent to ncurses' getmouse(), renamed to
121 avoid conflict with PDCurses' getmouse(). But if you define
122 NCURSES_MOUSE_VERSION (preferably as 2) before including
123 curses.h, it defines getmouse() to nc_getmouse(), along with a
124 few other redefintions needed for compatibility with ncurses
125 code. nc_getmouse() calls request_mouse_pos(), which (not
126 getmouse()) is the classic equivalent.
128 ungetmouse() is the mouse equivalent of ungetch(). However,
129 PDCurses doesn't maintain a queue of mouse events; only one can
130 be pushed back, and it can overwrite or be overwritten by real
131 mouse events.
133 Portability X/Open BSD SYS V
134 mouse_set - - 4.0
135 mouse_on - - 4.0
136 mouse_off - - 4.0
137 request_mouse_pos - - 4.0
138 map_button - - 4.0
139 wmouse_position - - 4.0
140 getmouse - - 4.0
141 getbmap - - 4.0
142 mouseinterval - - -
143 wenclose - - -
144 wmouse_trafo - - -
145 mouse_trafo - - -
146 mousemask - - -
147 nc_getmouse - - -
148 ungetmouse - - -
150 **man-end****************************************************************/
152 #include <string.h>
154 static bool ungot = FALSE;
156 int mouse_set(unsigned long mbe)
158 PDC_LOG(("mouse_set() - called: event %x\n", mbe));
160 SP->_trap_mbe = mbe;
161 return PDC_mouse_set();
164 int mouse_on(unsigned long mbe)
166 PDC_LOG(("mouse_on() - called: event %x\n", mbe));
168 SP->_trap_mbe |= mbe;
169 return PDC_mouse_set();
172 int mouse_off(unsigned long mbe)
174 PDC_LOG(("mouse_off() - called: event %x\n", mbe));
176 SP->_trap_mbe &= ~mbe;
177 return PDC_mouse_set();
180 int map_button(unsigned long button)
182 PDC_LOG(("map_button() - called: button %x\n", button));
184 /****************** this does nothing at the moment ***************/
185 SP->_map_mbe_to_key = button;
187 return OK;
190 int request_mouse_pos(void)
192 PDC_LOG(("request_mouse_pos() - called\n"));
194 Mouse_status = pdc_mouse_status;
196 return OK;
199 void wmouse_position(WINDOW *win, int *y, int *x)
201 PDC_LOG(("wmouse_position() - called\n"));
203 if (win && wenclose(win, MOUSE_Y_POS, MOUSE_X_POS))
205 if (y)
206 *y = MOUSE_Y_POS - win->_begy;
207 if (x)
208 *x = MOUSE_X_POS - win->_begx;
210 else
212 if (y)
213 *y = -1;
214 if (x)
215 *x = -1;
219 unsigned long getmouse(void)
221 PDC_LOG(("getmouse() - called\n"));
223 return SP->_trap_mbe;
226 unsigned long getbmap(void)
228 PDC_LOG(("getbmap() - called\n"));
230 return SP->_map_mbe_to_key;
233 /* ncurses mouse interface */
235 int mouseinterval(int wait)
237 int old_wait;
239 PDC_LOG(("mouseinterval() - called: %d\n", wait));
241 old_wait = SP->mouse_wait;
243 if (wait >= 0 && wait <= 1000)
244 SP->mouse_wait = wait;
246 return old_wait;
249 bool wenclose(const WINDOW *win, int y, int x)
251 PDC_LOG(("wenclose() - called: %p %d %d\n", win, y, x));
253 return (win && y >= win->_begy && y < win->_begy + win->_maxy
254 && x >= win->_begx && x < win->_begx + win->_maxx);
257 bool wmouse_trafo(const WINDOW *win, int *y, int *x, bool to_screen)
259 int newy, newx;
261 PDC_LOG(("wmouse_trafo() - called\n"));
263 if (!win || !y || !x)
264 return FALSE;
266 newy = *y;
267 newx = *x;
269 if (to_screen)
271 newy += win->_begy;
272 newx += win->_begx;
274 if (!wenclose(win, newy, newx))
275 return FALSE;
277 else
279 if (wenclose(win, newy, newx))
281 newy -= win->_begy;
282 newx -= win->_begx;
284 else
285 return FALSE;
288 *y = newy;
289 *x = newx;
291 return TRUE;
294 bool mouse_trafo(int *y, int *x, bool to_screen)
296 PDC_LOG(("mouse_trafo() - called\n"));
298 return wmouse_trafo(stdscr, y, x, to_screen);
301 mmask_t mousemask(mmask_t mask, mmask_t *oldmask)
303 PDC_LOG(("mousemask() - called\n"));
305 if (oldmask)
306 *oldmask = SP->_trap_mbe;
308 /* The ncurses interface doesn't work with our move events, so
309 filter them here */
311 mask &= ~(BUTTON1_MOVED | BUTTON2_MOVED | BUTTON3_MOVED);
313 mouse_set(mask);
315 return SP->_trap_mbe;
318 int nc_getmouse(MEVENT *event)
320 int i;
321 mmask_t bstate = 0;
323 PDC_LOG(("nc_getmouse() - called\n"));
325 if (!event)
326 return ERR;
328 ungot = FALSE;
330 request_mouse_pos();
332 event->id = 0;
334 event->x = Mouse_status.x;
335 event->y = Mouse_status.y;
336 event->z = 0;
338 for (i = 0; i < 3; i++)
340 if (Mouse_status.changes & (1 << i))
342 int shf = i * 5;
343 short button = Mouse_status.button[i] & BUTTON_ACTION_MASK;
345 if (button == BUTTON_RELEASED)
346 bstate |= (BUTTON1_RELEASED << shf);
347 else if (button == BUTTON_PRESSED)
348 bstate |= (BUTTON1_PRESSED << shf);
349 else if (button == BUTTON_CLICKED)
350 bstate |= (BUTTON1_CLICKED << shf);
351 else if (button == BUTTON_DOUBLE_CLICKED)
352 bstate |= (BUTTON1_DOUBLE_CLICKED << shf);
354 button = Mouse_status.button[i] & BUTTON_MODIFIER_MASK;
356 if (button & PDC_BUTTON_SHIFT)
357 bstate |= BUTTON_MODIFIER_SHIFT;
358 if (button & PDC_BUTTON_CONTROL)
359 bstate |= BUTTON_MODIFIER_CONTROL;
360 if (button & PDC_BUTTON_ALT)
361 bstate |= BUTTON_MODIFIER_ALT;
365 if (MOUSE_WHEEL_UP)
366 bstate |= BUTTON4_PRESSED;
367 else if (MOUSE_WHEEL_DOWN)
368 bstate |= BUTTON5_PRESSED;
370 /* extra filter pass -- mainly for button modifiers */
372 event->bstate = bstate & SP->_trap_mbe;
374 return OK;
377 int ungetmouse(MEVENT *event)
379 int i;
380 unsigned long bstate;
382 PDC_LOG(("ungetmouse() - called\n"));
384 if (!event || ungot)
385 return ERR;
387 ungot = TRUE;
389 pdc_mouse_status.x = event->x;
390 pdc_mouse_status.y = event->y;
392 pdc_mouse_status.changes = 0;
393 bstate = event->bstate;
395 for (i = 0; i < 3; i++)
397 int shf = i * 5;
398 short button = 0;
400 if (bstate & ((BUTTON1_RELEASED | BUTTON1_PRESSED |
401 BUTTON1_CLICKED | BUTTON1_DOUBLE_CLICKED) << shf))
403 pdc_mouse_status.changes |= 1 << i;
405 if (bstate & (BUTTON1_PRESSED << shf))
406 button = BUTTON_PRESSED;
407 if (bstate & (BUTTON1_CLICKED << shf))
408 button = BUTTON_CLICKED;
409 if (bstate & (BUTTON1_DOUBLE_CLICKED << shf))
410 button = BUTTON_DOUBLE_CLICKED;
412 if (bstate & BUTTON_MODIFIER_SHIFT)
413 button |= PDC_BUTTON_SHIFT;
414 if (bstate & BUTTON_MODIFIER_CONTROL)
415 button |= PDC_BUTTON_CONTROL;
416 if (bstate & BUTTON_MODIFIER_ALT)
417 button |= PDC_BUTTON_ALT;
420 pdc_mouse_status.button[i] = button;
423 if (bstate & BUTTON4_PRESSED)
424 pdc_mouse_status.changes |= PDC_MOUSE_WHEEL_UP;
425 else if (bstate & BUTTON5_PRESSED)
426 pdc_mouse_status.changes |= PDC_MOUSE_WHEEL_DOWN;
428 return ungetch(KEY_MOUSE);