Debian Codegraph @

src/iphone/iphone_mouse.m

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_iphone.h"
5
6 typedef struct ALLEGRO_MOUSE_IPHONE {
7 ALLEGRO_MOUSE parent;
8 ALLEGRO_MOUSE_STATE state;
9 } ALLEGRO_MOUSE_IPHONE;
10
11 static ALLEGRO_MOUSE_IPHONE the_mouse;
12
13 static bool imouse_installed;
14
15 /*
16 * Helper to generate a mouse event.
17 */
18 void _al_iphone_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_iphone_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 imouse_exit(void);
60 static bool imouse_init(void)
61 {
62 if (imouse_installed)
63 imouse_exit();
64 memset(&the_mouse, 0, sizeof the_mouse);
65 _al_event_source_init(&the_mouse.parent.es);
66 imouse_installed = true;
67 return true;
68 }
69
70 static void imouse_exit(void)
71 {
72 if (!imouse_installed)
73 return;
74 imouse_installed = false;
75 _al_event_source_free(&the_mouse.parent.es);
76 }
77
78 static ALLEGRO_MOUSE *imouse_get_mouse(void)
79 {
80 ASSERT(imouse_installed);
81 return (ALLEGRO_MOUSE *)&the_mouse;
82 }
83
84 /* We report multi-touch as different buttons. */
85 static unsigned int imouse_get_mouse_num_buttons(void)
86 {
87 return 5;
88 }
89
90 static unsigned int imouse_get_mouse_num_axes(void)
91 {
92 return 2;
93 }
94
95 /* Hard to accomplish on a touch screen. */
96 static bool imouse_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 imouse_set_mouse_axis(int which, int z)
105 {
106 (void)which;
107 (void)z;
108 return false;
109 }
110
111 /*
112 static void imouse_get_state(ALLEGRO_MOUSE_STATE *ret_state)
113 {
114 ASSERT(imouse_installed);
115
116 _al_event_source_lock(&the_mouse.parent.es);
117 {
118 *ret_state = the_mouse.state;
119 }
120 _al_event_source_unlock(&the_mouse.parent.es);
121 }
122 */
123 void imouse_get_state(ALLEGRO_MOUSE_STATE *ret_state);
124
125 static ALLEGRO_MOUSE_DRIVER iphone_mouse_driver = {
126 AL_ID('I', 'P', 'H', 'O'),
127 "",
128 "",
129 "iphone mouse",
130 imouse_init,
131 imouse_exit,
132 imouse_get_mouse,
133 imouse_get_mouse_num_buttons,
134 imouse_get_mouse_num_axes,
135 imouse_set_mouse_xy,
136 imouse_set_mouse_axis,
137 imouse_get_state
138 };
139
140 ALLEGRO_MOUSE_DRIVER *_al_get_iphone_mouse_driver(void)
141 {
142 return &iphone_mouse_driver;
143 }