|
| 1 | +#include <caca.h> |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <stdlib.h> |
| 6 | + |
| 7 | +#include "cacadriver.h" |
| 8 | + |
| 9 | +#define CACA_NATIVE(obj) (VISUAL_CHECK_CAST ((obj), CacaNative)) |
| 10 | + |
| 11 | + |
| 12 | +typedef struct _CacaNative CacaNative; |
| 13 | + |
| 14 | +static int native_create (SADisplay *display, VisVideoDepth depth, VisVideoAttributeOptions *vidoptions, |
| 15 | + int width, int height, int resizable); |
| 16 | +static int native_close (SADisplay *display); |
| 17 | +static int native_lock (SADisplay *display); |
| 18 | +static int native_unlock (SADisplay *display); |
| 19 | +static int native_fullscreen (SADisplay *display, int fullscreen, int autoscale); |
| 20 | +static int native_getvideo (SADisplay *display, VisVideo *screen); |
| 21 | +static int native_updaterect (SADisplay *display, VisRectangle *rect); |
| 22 | +static int native_drainevents (SADisplay *display, VisEventQueue *eventqueue); |
| 23 | + |
| 24 | + |
| 25 | +static int caca_initialized; |
| 26 | + |
| 27 | +struct _CacaNative { |
| 28 | + VisObject object; |
| 29 | + |
| 30 | + struct caca_bitmap *bitmap; |
| 31 | + unsigned char *area; |
| 32 | + |
| 33 | + VisVideoDepth depth; |
| 34 | + int width; |
| 35 | + int height; |
| 36 | + |
| 37 | + unsigned int red[256]; |
| 38 | + unsigned int green[256]; |
| 39 | + unsigned int blue[256]; |
| 40 | + unsigned int alpha[256]; |
| 41 | +}; |
| 42 | + |
| 43 | +SADisplayDriver *caca_driver_new () |
| 44 | +{ |
| 45 | + SADisplayDriver *driver; |
| 46 | + |
| 47 | + driver = visual_mem_new0 (SADisplayDriver, 1); |
| 48 | + |
| 49 | + visual_object_initialize (VISUAL_OBJECT (driver), TRUE, NULL); |
| 50 | + |
| 51 | + driver->create = native_create; |
| 52 | + driver->close = native_close; |
| 53 | + driver->lock = native_lock; |
| 54 | + driver->unlock = native_unlock; |
| 55 | + driver->fullscreen = native_fullscreen; |
| 56 | + driver->getvideo = native_getvideo; |
| 57 | + driver->updaterect = native_updaterect; |
| 58 | + driver->drainevents = native_drainevents; |
| 59 | + |
| 60 | + return driver; |
| 61 | +} |
| 62 | + |
| 63 | +static int native_create (SADisplay *display, VisVideoDepth depth, VisVideoAttributeOptions *vidoptions, |
| 64 | + int width, int height, int resizable) |
| 65 | +{ |
| 66 | + CacaNative *native = CACA_NATIVE (display->native); |
| 67 | + unsigned int rmask; |
| 68 | + unsigned int gmask; |
| 69 | + unsigned int bmask; |
| 70 | + unsigned int amask; |
| 71 | + |
| 72 | + if (native == NULL) { |
| 73 | + native = visual_mem_new0 (CacaNative, 1); |
| 74 | + |
| 75 | + visual_object_initialize (VISUAL_OBJECT (native), TRUE, NULL); |
| 76 | + } |
| 77 | + |
| 78 | + if (caca_initialized == FALSE) { |
| 79 | + if (caca_init () < 0) |
| 80 | + return -1; |
| 81 | + |
| 82 | + caca_set_window_title ("Libcaca libvisual display"); |
| 83 | + } |
| 84 | + |
| 85 | + if (native->bitmap != NULL) |
| 86 | + caca_free_bitmap (native->bitmap); |
| 87 | + |
| 88 | + |
| 89 | + if (native->area != NULL) |
| 90 | + visual_mem_free (native->area); |
| 91 | + |
| 92 | + switch (depth) { |
| 93 | + case VISUAL_VIDEO_DEPTH_32BIT: |
| 94 | + rmask = 0xff000000; |
| 95 | + gmask = 0x00ff0000; |
| 96 | + bmask = 0x0000ff00; |
| 97 | + amask = 0x000000ff; |
| 98 | + |
| 99 | + break; |
| 100 | + |
| 101 | + case VISUAL_VIDEO_DEPTH_24BIT: |
| 102 | + rmask = 0xff0000; |
| 103 | + gmask = 0x00ff00; |
| 104 | + bmask = 0x0000ff; |
| 105 | + amask = 0x000000; |
| 106 | + |
| 107 | + break; |
| 108 | + |
| 109 | + case VISUAL_VIDEO_DEPTH_16BIT: |
| 110 | + rmask = 0x7c00; |
| 111 | + gmask = 0x03e0; |
| 112 | + bmask = 0x001f; |
| 113 | + amask = 0x0000; |
| 114 | + |
| 115 | + break; |
| 116 | + |
| 117 | + case VISUAL_VIDEO_DEPTH_8BIT: |
| 118 | + rmask = gmask = bmask = amask = 0; |
| 119 | + |
| 120 | + break; |
| 121 | + |
| 122 | + default: |
| 123 | + rmask = gmask = bmask = amask = 0; |
| 124 | + |
| 125 | + break; |
| 126 | + } |
| 127 | + |
| 128 | + native->bitmap = caca_create_bitmap (visual_video_depth_value_from_enum (depth), |
| 129 | + width, |
| 130 | + height, |
| 131 | + width * (visual_video_depth_value_from_enum (depth) / 8), |
| 132 | + rmask, gmask, bmask, amask); |
| 133 | + |
| 134 | + native->area = visual_mem_malloc0 (width * height * (visual_video_depth_value_from_enum (depth) / 8)); |
| 135 | + |
| 136 | + native->width = width; |
| 137 | + native->height = height; |
| 138 | + native->depth = depth; |
| 139 | + |
| 140 | + display->native = VISUAL_OBJECT (native); |
| 141 | + |
| 142 | + caca_initialized = TRUE; |
| 143 | + |
| 144 | + return 0; |
| 145 | +} |
| 146 | + |
| 147 | +static int native_close (SADisplay *display) |
| 148 | +{ |
| 149 | + caca_end (); |
| 150 | + |
| 151 | + return 0; |
| 152 | +} |
| 153 | + |
| 154 | +static int native_lock (SADisplay *display) |
| 155 | +{ |
| 156 | + return 0; |
| 157 | +} |
| 158 | + |
| 159 | +static int native_unlock (SADisplay *display) |
| 160 | +{ |
| 161 | + return 0; |
| 162 | +} |
| 163 | + |
| 164 | +static int native_fullscreen (SADisplay *display, int fullscreen, int autoscale) |
| 165 | +{ |
| 166 | + return 0; |
| 167 | +} |
| 168 | + |
| 169 | +static int native_getvideo (SADisplay *display, VisVideo *screen) |
| 170 | +{ |
| 171 | + CacaNative *native = CACA_NATIVE (display->native); |
| 172 | + |
| 173 | + visual_video_set_depth (screen, native->depth); |
| 174 | + |
| 175 | + visual_video_set_dimension (screen, native->width, native->height); |
| 176 | + visual_video_set_buffer (screen, native->area); |
| 177 | + |
| 178 | + return 0; |
| 179 | +} |
| 180 | + |
| 181 | +static int native_updaterect (SADisplay *display, VisRectangle *rect) |
| 182 | +{ |
| 183 | + CacaNative *native = CACA_NATIVE (display->native); |
| 184 | + VisPalette *pal; |
| 185 | + int i; |
| 186 | + |
| 187 | + pal = display->screen->pal; |
| 188 | + |
| 189 | + if (display->screen->depth == VISUAL_VIDEO_DEPTH_8BIT) { |
| 190 | + for (i = 0; i < 256; i++) { |
| 191 | + native->red[i] = pal->colors[i].r * 16; |
| 192 | + native->green[i] = pal->colors[i].g * 16; |
| 193 | + native->blue[i] = pal->colors[i].b * 16; |
| 194 | + } |
| 195 | + |
| 196 | + caca_set_bitmap_palette (native->bitmap, |
| 197 | + native->red, |
| 198 | + native->green, |
| 199 | + native->blue, |
| 200 | + native->alpha); |
| 201 | + } |
| 202 | + |
| 203 | + caca_draw_bitmap (0, 0, caca_get_width() - 1, caca_get_height() - 1, |
| 204 | + native->bitmap, native->area); |
| 205 | + |
| 206 | + caca_refresh (); |
| 207 | + |
| 208 | + return 0; |
| 209 | +} |
| 210 | + |
| 211 | +static int native_drainevents (SADisplay *display, VisEventQueue *eventqueue) |
| 212 | +{ |
| 213 | + CacaNative *native = CACA_NATIVE (display->native); |
| 214 | + unsigned int event; |
| 215 | + |
| 216 | + while ((event = caca_get_event (CACA_EVENT_ANY)) > 0) { |
| 217 | + |
| 218 | + if (event & CACA_EVENT_KEY_PRESS) { |
| 219 | + |
| 220 | + visual_event_queue_add_keyboard (eventqueue, event & 0xff, 0, |
| 221 | + VISUAL_KEY_DOWN); |
| 222 | + |
| 223 | + } else if (event & CACA_EVENT_KEY_RELEASE) { |
| 224 | + |
| 225 | + visual_event_queue_add_keyboard (eventqueue, event & 0xff, 0, |
| 226 | + VISUAL_KEY_UP); |
| 227 | + |
| 228 | + } else if (event & CACA_EVENT_MOUSE_MOTION) { |
| 229 | + |
| 230 | + visual_event_queue_add_mousemotion (eventqueue, caca_get_mouse_x (), caca_get_mouse_y ()); |
| 231 | + |
| 232 | + } else if (event & CACA_EVENT_MOUSE_PRESS) { |
| 233 | + |
| 234 | + visual_event_queue_add_mousebutton (eventqueue, event & 0xff, VISUAL_MOUSE_DOWN, |
| 235 | + caca_get_mouse_x (), caca_get_mouse_y ()); |
| 236 | + |
| 237 | + } else if (event & CACA_EVENT_MOUSE_RELEASE) { |
| 238 | + |
| 239 | + visual_event_queue_add_mousebutton (eventqueue, event & 0xff, VISUAL_MOUSE_UP, |
| 240 | + caca_get_mouse_x (), caca_get_mouse_y ()); |
| 241 | + } else if (event & CACA_EVENT_RESIZE) { |
| 242 | + |
| 243 | + |
| 244 | + native_create (display, display->screen->depth, |
| 245 | + NULL, |
| 246 | + caca_get_window_width (), |
| 247 | + caca_get_window_height (), |
| 248 | + TRUE); |
| 249 | + |
| 250 | + visual_event_queue_add_resize (eventqueue, display->screen, |
| 251 | + caca_get_window_width (), |
| 252 | + caca_get_window_height ()); |
| 253 | + |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + return 0; |
| 258 | +} |
| 259 | + |
0 commit comments