Debian Codegraph @

src/android/android_mouse.c

raw
1 #include "allegro5/allegro.h"
2 #include "allegro5/internal/aintern_mouse.h"
3 #include "allegro5/internal/aintern_display.h"
4 #include "allegro5/internal/aintern_android.h"
5
6 typedef struct ALLEGRO_MOUSE_ANDROID {
7 ALLEGRO_MOUSE parent;
8 ALLEGRO_MOUSE_STATE state;
9 } ALLEGRO_MOUSE_ANDROID;
10
11 static ALLEGRO_MOUSE_ANDROID the_mouse;
12
13 static bool amouse_installed;
14
15 /*
16 * Helper to generate a mouse event.
17 */
18 void _al_android_generate_mouse_event(unsigned int type, int x, int y,
19 unsigned int button, ALLEGRO_DISPLAY *d)
20 {
21 ALLEGRO_EVENT event;
22
23 _al_event_source_lock(&the_mouse.parent.es);
24
25 //_al_android_translate_from_screen(d, &x, &y);
26
27 the_mouse.state.x = x;
28 the_mouse.state.y = y;
29
30 if (type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
31 the_mouse.state.buttons |= (1 << button);
32 }
33 else if (type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
34 the_mouse.state.buttons &= ~(1 << button);
35 }
36
37 the_mouse.state.pressure = the_mouse.state.buttons ? 1.0 : 0.0; // TODO
38
39 if (_al_event_source_needs_to_generate_event(&the_mouse.parent.es)) {
40 event.mouse.type = type;
41 event.mouse.timestamp = al_get_time();
42 event.mouse.display = d;
43 event.mouse.x = x;
44 event.mouse.y = y;
45 event.mouse.z = 0;
46 event.mouse.w = 0;
47 event.mouse.dx = 0; // TODO
48 event.mouse.dy = 0; // TODO
49 event.mouse.dz = 0; // TODO
50 event.mouse.dw = 0; // TODO
51 event.mouse.button = button;
52 event.mouse.pressure = the_mouse.state.pressure;
53 _al_event_source_emit_event(&the_mouse.parent.es, &event);
54 }
55
56 _al_event_source_unlock(&the_mouse.parent.es);
57 }
58
59 static void amouse_exit(void);
60 static bool amouse_init(void)
61 {
62 if (amouse_installed)
63 amouse_exit();
64 memset(&the_mouse, 0, sizeof the_mouse);
65 _al_event_source_init(&the_mouse.parent.es);
66 amouse_installed = true;
67 return true;
68 }
69
70 static void amouse_exit(void)
71 {
72 if (!amouse_installed)
73 return;
74 amouse_installed = false;
75 _al_event_source_free(&the_mouse.parent.es);
76 }
77
78 static ALLEGRO_MOUSE *amouse_get_mouse(void)
79 {
80 ASSERT(amouse_installed);
81 return (ALLEGRO_MOUSE *)&the_mouse;
82 }
83
84 /* We report multi-touch as different buttons. */
85 static unsigned int amouse_get_mouse_num_buttons(void)
86 {
87 return 5;
88 }
89
90 static unsigned int amouse_get_mouse_num_axes(void)
91 {
92 return 2;
93 }
94
95 /* Hard to accomplish on a touch screen. */
96 static bool amouse_set_mouse_xy(ALLEGRO_DISPLAY *display, int x, int y)
97 {
98 (void)display;
99 (void)x;
100 (void)y;
101 return false;
102 }
103
104 static bool amouse_set_mouse_axis(int which, int z)
105 {
106 (void)which;
107 (void)z;
108 return false;
109 }
110
111 static ALLEGRO_MOUSE_DRIVER android_mouse_driver = {
112 AL_ID('A', 'N', 'D', 'R'),
113 "",
114 "",
115 "android mouse",
116 amouse_init,
117 amouse_exit,
118 amouse_get_mouse,
119 amouse_get_mouse_num_buttons,
120 amouse_get_mouse_num_axes,
121 amouse_set_mouse_xy,
122 amouse_set_mouse_axis,
123 _al_android_mouse_get_state
124 };
125
126 ALLEGRO_MOUSE_DRIVER *_al_get_android_mouse_driver(void)
127 {
128 return &android_mouse_driver;
129 }