LGPL
[vlc.git] / include / vlc_mouse.h
1 /*****************************************************************************
2  * vlc_mouse.h: mouse related structures and functions
3  *****************************************************************************
4  * Copyright (C) 2009 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifndef _VLC_MOUSE_H
25 #define _VLC_MOUSE_H 1
26
27 /**
28  * Mouse buttons
29  */
30 enum
31 {
32     MOUSE_BUTTON_LEFT=0,
33     MOUSE_BUTTON_CENTER,
34     MOUSE_BUTTON_RIGHT,
35     MOUSE_BUTTON_WHEEL_UP,
36     MOUSE_BUTTON_WHEEL_DOWN,
37     MOUSE_BUTTON_WHEEL_LEFT,
38     MOUSE_BUTTON_WHEEL_RIGHT,
39     MOUSE_BUTTON_MAX
40 };
41
42 /**
43  * Mouse state
44  */
45 typedef struct
46 {
47     /* Coordinate */
48     int i_x;
49     int i_y;
50     /* Mask of pressed button */
51     int i_pressed;
52     /* Is double clicked */
53     bool b_double_click;
54 } vlc_mouse_t;
55
56 static inline void vlc_mouse_Init( vlc_mouse_t *p_mouse )
57 {
58     p_mouse->i_x = 0;
59     p_mouse->i_y = 0;
60     p_mouse->i_pressed = 0;
61     p_mouse->b_double_click = false;
62 }
63
64 /* */
65 static inline void vlc_mouse_SetPressed( vlc_mouse_t *p_mouse,
66                                          int i_button )
67 {
68     p_mouse->i_pressed |= 1 << i_button;
69 }
70 static inline void vlc_mouse_SetReleased( vlc_mouse_t *p_mouse,
71                                           int i_button )
72 {
73     p_mouse->i_pressed &= ~(1 << i_button);
74 }
75 static inline void vlc_mouse_SetPosition( vlc_mouse_t *p_mouse,
76                                           int i_x, int i_y )
77 {
78     p_mouse->i_x = i_x;
79     p_mouse->i_y = i_y;
80 }
81
82 /* */
83 static inline bool vlc_mouse_IsPressed( const vlc_mouse_t *p_mouse,
84                                         int i_button )
85 {
86     return ( p_mouse->i_pressed & (1 << i_button) ) != 0;
87 }
88 static inline bool vlc_mouse_IsLeftPressed( const vlc_mouse_t *p_mouse )
89 {
90     return vlc_mouse_IsPressed( p_mouse, MOUSE_BUTTON_LEFT );
91 }
92 static inline bool vlc_mouse_IsCenterPressed( const vlc_mouse_t *p_mouse )
93 {
94     return vlc_mouse_IsPressed( p_mouse, MOUSE_BUTTON_CENTER );
95 }
96 static inline bool vlc_mouse_IsRightPressed( const vlc_mouse_t *p_mouse )
97 {
98     return vlc_mouse_IsPressed( p_mouse, MOUSE_BUTTON_RIGHT );
99 }
100 static inline bool vlc_mouse_IsWheelUpPressed( const vlc_mouse_t *p_mouse )
101 {
102     return vlc_mouse_IsPressed( p_mouse, MOUSE_BUTTON_WHEEL_UP );
103 }
104 static inline bool vlc_mouse_IsWheelDownPressed( const vlc_mouse_t *p_mouse )
105 {
106     return vlc_mouse_IsPressed( p_mouse, MOUSE_BUTTON_WHEEL_DOWN );
107 }
108 static inline void vlc_mouse_GetMotion( int *pi_x, int *pi_y,
109                                         const vlc_mouse_t *p_old,
110                                         const vlc_mouse_t *p_new )
111 {
112     *pi_x = p_new->i_x - p_old->i_x;
113     *pi_y = p_new->i_y - p_old->i_y;
114 }
115
116 /* */
117 static inline bool vlc_mouse_HasChanged( const vlc_mouse_t *p_old,
118                                          const vlc_mouse_t *p_new )
119 {
120     return p_old->i_x != p_new->i_x || p_old->i_y != p_new->i_y ||
121            p_old->i_pressed != p_new->i_pressed;
122 }
123 static inline bool vlc_mouse_HasMoved( const vlc_mouse_t *p_old,
124                                        const vlc_mouse_t *p_new )
125 {
126     return p_old->i_x != p_new->i_x || p_old->i_y != p_new->i_y;
127 }
128 static inline bool vlc_mouse_HasButton( const vlc_mouse_t *p_old,
129                                         const vlc_mouse_t *p_new )
130 {
131     return p_old->i_pressed != p_new->i_pressed;
132 }
133 static inline bool vlc_mouse_HasPressed( const vlc_mouse_t *p_old,
134                                          const vlc_mouse_t *p_new,
135                                          int i_button )
136 {
137     const int i_mask = 1 << i_button;
138     return (p_old->i_pressed & i_mask) == 0 && (p_new->i_pressed & i_mask);
139 }
140 static inline bool vlc_mouse_HasReleased( const vlc_mouse_t *p_old,
141                                           const vlc_mouse_t *p_new,
142                                           int i_button )
143 {
144     const int i_mask = 1 << i_button;
145     return (p_old->i_pressed & i_mask) && (p_new->i_pressed & i_mask) == 0;
146 }
147 #endif /* _VLC_MOUSE_H */
148