Debian Codegraph @

src/raspberrypi/pisystem.c

raw
1 #include <sys/time.h>
2
3 #include "allegro5/allegro.h"
4 #include "allegro5/platform/aintraspberrypi.h"
5 #include "allegro5/internal/aintern_raspberrypi.h"
6 #include "allegro5/platform/aintunix.h"
7 #include "allegro5/platform/aintlnx.h"
8 #include "allegro5/internal/aintern_x.h"
9 #include "allegro5/internal/aintern_xevents.h"
10 #include "allegro5/internal/aintern_xmouse.h"
11
12 #include <bcm_host.h>
13
14 #include <signal.h>
15
16 ALLEGRO_DEBUG_CHANNEL("system")
17
18 static ALLEGRO_SYSTEM_INTERFACE *pi_vt;
19
20 static ALLEGRO_SYSTEM *pi_initialize(int flags)
21 {
22 (void)flags;
23
24 ALLEGRO_SYSTEM_RASPBERRYPI *s;
25
26 bcm_host_init();
27
28 s = al_calloc(1, sizeof *s);
29
30 _al_vector_init(&s->system.displays, sizeof (ALLEGRO_DISPLAY_RASPBERRYPI *));
31
32 _al_unix_init_time();
33
34 if (getenv("DISPLAY")) {
35 _al_mutex_init_recursive(&s->lock);
36 s->x11display = XOpenDisplay(0);
37 _al_thread_create(&s->thread, _al_xwin_background_thread, s);
38 ALLEGRO_INFO("events thread spawned.\n");
39 /* We need to put *some* atom into the ClientMessage we send for
40 * faking mouse movements with al_set_mouse_xy - so let's ask X11
41 * for one here.
42 */
43 s->AllegroAtom = XInternAtom(s->x11display, "AllegroAtom", False);
44 }
45
46 s->inhibit_screensaver = false;
47
48 s->system.vt = pi_vt;
49
50 return &s->system;
51 }
52
53 static void pi_shutdown_system(void)
54 {
55 ALLEGRO_SYSTEM *s = al_get_system_driver();
56 ALLEGRO_SYSTEM_RASPBERRYPI *spi = (void *)s;
57
58 ALLEGRO_INFO("shutting down.\n");
59
60 /* Close all open displays. */
61 while (_al_vector_size(&s->displays) > 0) {
62 ALLEGRO_DISPLAY **dptr = _al_vector_ref(&s->displays, 0);
63 ALLEGRO_DISPLAY *d = *dptr;
64 al_destroy_display(d);
65 }
66 _al_vector_free(&s->displays);
67
68 if (getenv("DISPLAY")) {
69 _al_thread_join(&spi->thread);
70 XCloseDisplay(spi->x11display);
71 }
72
73 bcm_host_deinit();
74
75 raise(SIGINT);
76
77 al_free(spi);
78 }
79
80 static ALLEGRO_KEYBOARD_DRIVER *pi_get_keyboard_driver(void)
81 {
82 if (getenv("DISPLAY")) {
83 return _al_xwin_keyboard_driver();
84 }
85 return _al_linux_keyboard_driver_list[0].driver;
86 }
87
88 static ALLEGRO_MOUSE_DRIVER *pi_get_mouse_driver(void)
89 {
90 if (getenv("DISPLAY")) {
91 return _al_xwin_mouse_driver();
92 }
93 return _al_linux_mouse_driver_list[0].driver;
94 }
95
96 static ALLEGRO_JOYSTICK_DRIVER *pi_get_joystick_driver(void)
97 {
98 return _al_joystick_driver_list[0].driver;
99 }
100
101 static int pi_get_num_video_adapters(void)
102 {
103 return 1;
104 }
105
106 static bool pi_get_monitor_info(int adapter, ALLEGRO_MONITOR_INFO *info)
107 {
108 (void)adapter;
109 int dx, dy, w, h;
110 _al_raspberrypi_get_screen_info(&dx, &dy, &w, &h);
111 info->x1 = 0;
112 info->y1 = 0;
113 info->x2 = w;
114 info->y2 = h;
115 return true;
116 }
117
118 static bool pi_get_cursor_position(int *ret_x, int *ret_y)
119 {
120 // FIXME:
121 (void)ret_x;
122 (void)ret_y;
123 return false;
124 }
125
126 static bool pi_inhibit_screensaver(bool inhibit)
127 {
128 ALLEGRO_SYSTEM_RASPBERRYPI *system = (void *)al_get_system_driver();
129
130 system->inhibit_screensaver = inhibit;
131 return true;
132 }
133
134 static int pi_get_num_display_modes(void)
135 {
136 return 1;
137 }
138
139 static ALLEGRO_DISPLAY_MODE *pi_get_display_mode(int mode, ALLEGRO_DISPLAY_MODE *dm)
140 {
141 (void)mode;
142 int dx, dy, w, h;
143 _al_raspberrypi_get_screen_info(&dx, &dy, &w, &h);
144 dm->width = w;
145 dm->height = h;
146 dm->format = 0; // FIXME
147 dm->refresh_rate = 60;
148 return dm;
149 }
150
151 static ALLEGRO_MOUSE_CURSOR *pi_create_mouse_cursor(ALLEGRO_BITMAP *bmp, int focus_x_ignored, int focus_y_ignored)
152 {
153 (void)focus_x_ignored;
154 (void)focus_y_ignored;
155
156 ALLEGRO_STATE state;
157 al_store_state(&state, ALLEGRO_STATE_NEW_BITMAP_PARAMETERS | ALLEGRO_STATE_TARGET_BITMAP);
158 al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ARGB_8888);
159 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
160 ALLEGRO_BITMAP *cursor_bmp = al_clone_bitmap(bmp);
161 ALLEGRO_MOUSE_CURSOR_RASPBERRYPI *cursor = al_malloc(sizeof(ALLEGRO_MOUSE_CURSOR_RASPBERRYPI));
162 cursor->bitmap = cursor_bmp;
163 al_restore_state(&state);
164 return (ALLEGRO_MOUSE_CURSOR *)cursor;
165 }
166
167 static void pi_destroy_mouse_cursor(ALLEGRO_MOUSE_CURSOR *cursor)
168 {
169 ALLEGRO_MOUSE_CURSOR_RASPBERRYPI *pi_cursor = (void *)cursor;
170 al_destroy_bitmap(pi_cursor->bitmap);
171 al_free(pi_cursor);
172 }
173
174 /* Internal function to get a reference to this driver. */
175 ALLEGRO_SYSTEM_INTERFACE *_al_system_raspberrypi_driver(void)
176 {
177 if (pi_vt)
178 return pi_vt;
179
180 pi_vt = al_calloc(1, sizeof *pi_vt);
181
182 pi_vt->id = ALLEGRO_SYSTEM_ID_RASPBERRYPI;
183 pi_vt->initialize = pi_initialize;
184 pi_vt->get_display_driver = _al_get_raspberrypi_display_interface;
185 pi_vt->get_keyboard_driver = pi_get_keyboard_driver;
186 pi_vt->get_mouse_driver = pi_get_mouse_driver;
187 pi_vt->get_joystick_driver = pi_get_joystick_driver;
188 pi_vt->get_num_display_modes = pi_get_num_display_modes;
189 pi_vt->get_display_mode = pi_get_display_mode;
190 pi_vt->shutdown_system = pi_shutdown_system;
191 pi_vt->get_num_video_adapters = pi_get_num_video_adapters;
192 pi_vt->get_monitor_info = pi_get_monitor_info;
193 pi_vt->create_mouse_cursor = pi_create_mouse_cursor;
194 pi_vt->destroy_mouse_cursor = pi_destroy_mouse_cursor;
195 pi_vt->get_cursor_position = pi_get_cursor_position;
196 pi_vt->get_path = _al_unix_get_path;
197 pi_vt->inhibit_screensaver = pi_inhibit_screensaver;
198 pi_vt->get_time = _al_unix_get_time;
199 pi_vt->rest = _al_unix_rest;
200 pi_vt->init_timeout = _al_unix_init_timeout;
201
202 return pi_vt;
203 }
204
205
206 /* vim: set sts=3 sw=3 et: */