2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
26 #include <sys/ioctl.h>
27 #include <dev/wscons/wsdisplay_usl_io.h>
32 #include "SDL_video.h"
33 #include "SDL_mouse.h"
34 #include "../SDL_sysvideo.h"
35 #include "../SDL_pixels_c.h"
36 #include "../../events/SDL_events_c.h"
38 #include "SDL_wsconsvideo.h"
39 #include "SDL_wsconsevents_c.h"
40 #include "SDL_wsconsmouse_c.h"
42 #define WSCONSVID_DRIVER_NAME "wscons"
44 WSCONS_ROTATE_NONE = 0,
45 WSCONS_ROTATE_CCW = 90,
46 WSCONS_ROTATE_UD = 180,
47 WSCONS_ROTATE_CW = 270
50 #define min(a,b) ((a)<(b)?(a):(b))
52 /* Initialization/Query functions */
53 static int WSCONS_VideoInit(_THIS, SDL_PixelFormat *vformat);
54 static SDL_Rect **WSCONS_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
55 static SDL_Surface *WSCONS_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
56 static int WSCONS_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
57 static void WSCONS_VideoQuit(_THIS);
59 /* Hardware surface functions */
60 static int WSCONS_AllocHWSurface(_THIS, SDL_Surface *surface);
61 static int WSCONS_LockHWSurface(_THIS, SDL_Surface *surface);
62 static void WSCONS_UnlockHWSurface(_THIS, SDL_Surface *surface);
63 static void WSCONS_FreeHWSurface(_THIS, SDL_Surface *surface);
66 static WSCONS_bitBlit WSCONS_blit16;
67 static WSCONS_bitBlit WSCONS_blit16blocked;
68 static void WSCONS_UpdateRects(_THIS, int numrects, SDL_Rect *rects);
70 void WSCONS_ReportError(char *fmt, ...)
77 va_start(vaArgs, fmt);
78 vsnprintf(message, 199, fmt, vaArgs);
81 SDL_SetError(message);
82 fprintf(stderr, "WSCONS error: %s\n", message);
85 /* WSCONS driver bootstrap functions */
87 static int WSCONS_Available(void)
92 static void WSCONS_DeleteDevice(SDL_VideoDevice *device)
94 SDL_free(device->hidden);
98 static SDL_VideoDevice *WSCONS_CreateDevice(int devindex)
100 SDL_VideoDevice *device;
102 /* Initialize all variables that we clean on shutdown */
103 device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
104 if (device == NULL) {
108 SDL_memset(device, 0, (sizeof *device));
110 (struct SDL_PrivateVideoData *)SDL_malloc((sizeof *device->hidden));
111 if (device->hidden == NULL) {
116 SDL_memset(device->hidden, 0, (sizeof *device->hidden));
117 device->hidden->fd = -1;
119 /* Set the function pointers */
120 device->VideoInit = WSCONS_VideoInit;
121 device->ListModes = WSCONS_ListModes;
122 device->SetVideoMode = WSCONS_SetVideoMode;
123 device->SetColors = WSCONS_SetColors;
124 device->UpdateRects = WSCONS_UpdateRects;
125 device->VideoQuit = WSCONS_VideoQuit;
126 device->AllocHWSurface = WSCONS_AllocHWSurface;
127 device->LockHWSurface = WSCONS_LockHWSurface;
128 device->UnlockHWSurface = WSCONS_UnlockHWSurface;
129 device->FreeHWSurface = WSCONS_FreeHWSurface;
130 device->InitOSKeymap = WSCONS_InitOSKeymap;
131 device->PumpEvents = WSCONS_PumpEvents;
132 device->free = WSCONS_DeleteDevice;
137 VideoBootStrap WSCONS_bootstrap = {
138 WSCONSVID_DRIVER_NAME,
139 "SDL wscons video driver",
144 #define WSCONSDEV_FORMAT "/dev/ttyC%01x"
146 int WSCONS_VideoInit(_THIS, SDL_PixelFormat *vformat)
152 int wsmode = WSDISPLAYIO_MODE_DUMBFB;
157 devname = SDL_getenv("SDL_WSCONSDEV");
158 if (devname == NULL) {
160 if (ioctl(STDIN_FILENO, VT_GETACTIVE, &activeVT) == -1) {
161 WSCONS_ReportError("Unable to determine active terminal: %s",
165 SDL_snprintf(devnamebuf, sizeof(devnamebuf), WSCONSDEV_FORMAT, activeVT - 1);
166 devname = devnamebuf;
169 private->fd = open(devname, O_RDWR | O_NONBLOCK, 0);
170 if (private->fd == -1) {
171 WSCONS_ReportError("open %s: %s", devname, strerror(errno));
174 if (ioctl(private->fd, WSDISPLAYIO_GINFO, &private->info) == -1) {
175 WSCONS_ReportError("ioctl WSDISPLAY_GINFO: %s", strerror(errno));
178 if (ioctl(private->fd, WSDISPLAYIO_GTYPE, &wstype) == -1) {
179 WSCONS_ReportError("ioctl WSDISPLAY_GTYPE: %s", strerror(errno));
182 if (ioctl(private->fd, WSDISPLAYIO_LINEBYTES, &private->physlinebytes) == -1) {
183 WSCONS_ReportError("ioctl WSDISPLAYIO_LINEBYTES: %s", strerror(errno));
186 if (private->info.depth > 8) {
187 if (wstype == WSDISPLAY_TYPE_SUN24 ||
188 wstype == WSDISPLAY_TYPE_SUNCG12 ||
189 wstype == WSDISPLAY_TYPE_SUNCG14 ||
190 wstype == WSDISPLAY_TYPE_SUNTCX ||
191 wstype == WSDISPLAY_TYPE_SUNFFB) {
192 private->redMask = 0x0000ff;
193 private->greenMask = 0x00ff00;
194 private->blueMask = 0xff0000;
195 #ifdef WSDISPLAY_TYPE_PXALCD
196 } else if (wstype == WSDISPLAY_TYPE_PXALCD) {
197 private->redMask = 0x1f << 11;
198 private->greenMask = 0x3f << 5;
199 private->blueMask = 0x1f;
202 WSCONS_ReportError("Unknown video hardware");
206 WSCONS_ReportError("Displays with 8 bpp or less are not supported");
210 private->rotate = WSCONS_ROTATE_NONE;
211 rotation = SDL_getenv("SDL_VIDEO_WSCONS_ROTATION");
212 if (rotation != NULL) {
213 if (SDL_strlen(rotation) == 0) {
214 private->shadowFB = 0;
215 private->rotate = WSCONS_ROTATE_NONE;
216 printf("Not rotating, no shadow\n");
217 } else if (!SDL_strcmp(rotation, "NONE")) {
218 private->shadowFB = 1;
219 private->rotate = WSCONS_ROTATE_NONE;
220 printf("Not rotating, but still using shadow\n");
221 } else if (!SDL_strcmp(rotation, "CW")) {
222 private->shadowFB = 1;
223 private->rotate = WSCONS_ROTATE_CW;
224 printf("Rotating screen clockwise\n");
225 } else if (!SDL_strcmp(rotation, "CCW")) {
226 private->shadowFB = 1;
227 private->rotate = WSCONS_ROTATE_CCW;
228 printf("Rotating screen counter clockwise\n");
229 } else if (!SDL_strcmp(rotation, "UD")) {
230 private->shadowFB = 1;
231 private->rotate = WSCONS_ROTATE_UD;
232 printf("Rotating screen upside down\n");
234 WSCONS_ReportError("\"%s\" is not a valid value for "
235 "SDL_VIDEO_WSCONS_ROTATION", rotation);
240 switch (private->info.depth) {
244 len = private->physlinebytes * private->info.height;
247 if (private->physlinebytes == private->info.width) {
248 len = private->info.width * private->info.height * sizeof(short);
250 len = private->physlinebytes * private->info.height;
252 if (private->rotate == WSCONS_ROTATE_NONE ||
253 private->rotate == WSCONS_ROTATE_UD) {
254 private->blitFunc = WSCONS_blit16;
256 private->blitFunc = WSCONS_blit16blocked;
260 if (private->physlinebytes == private->info.width) {
261 len = private->info.width * private->info.height * sizeof(int);
263 len = private->physlinebytes * private->info.height;
267 WSCONS_ReportError("unsupported depth %d", private->info.depth);
271 if (private->shadowFB && private->blitFunc == NULL) {
272 WSCONS_ReportError("Using software buffer, but no blitter function is "
273 "available for this %d bpp.", private->info.depth);
277 if (ioctl(private->fd, WSDISPLAYIO_SMODE, &wsmode) == -1) {
278 WSCONS_ReportError("ioctl SMODE");
282 pagemask = getpagesize() - 1;
283 mapsize = ((int)len + pagemask) & ~pagemask;
284 private->physmem = (Uint8 *)mmap(NULL, mapsize,
285 PROT_READ | PROT_WRITE, MAP_SHARED,
286 private->fd, (off_t)0);
287 if (private->physmem == (Uint8 *)MAP_FAILED) {
288 private->physmem = NULL;
289 WSCONS_ReportError("mmap: %s", strerror(errno));
292 private->fbmem_len = len;
294 if (private->rotate == WSCONS_ROTATE_CW ||
295 private->rotate == WSCONS_ROTATE_CCW) {
296 width = private->info.height;
297 height = private->info.width;
299 width = private->info.width;
300 height = private->info.height;
303 this->info.current_w = width;
304 this->info.current_h = height;
306 if (private->shadowFB) {
307 private->shadowmem = (Uint8 *)SDL_malloc(len);
308 if (private->shadowmem == NULL) {
309 WSCONS_ReportError("No memory for shadow");
312 private->fbstart = private->shadowmem;
313 private->fblinebytes = width * ((private->info.depth + 7) / 8);
315 private->fbstart = private->physmem;
316 private->fblinebytes = private->physlinebytes;
319 private->SDL_modelist[0] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
320 private->SDL_modelist[0]->w = width;
321 private->SDL_modelist[0]->h = height;
323 vformat->BitsPerPixel = private->info.depth;
324 vformat->BytesPerPixel = private->info.depth / 8;
326 if (WSCONS_InitKeyboard(this) == -1) {
333 SDL_Rect **WSCONS_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
335 if (format->BitsPerPixel == private->info.depth) {
336 return private->SDL_modelist;
342 SDL_Surface *WSCONS_SetVideoMode(_THIS, SDL_Surface *current,
343 int width, int height, int bpp, Uint32 flags)
345 if (width != private->SDL_modelist[0]->w ||
346 height != private->SDL_modelist[0]->h) {
347 WSCONS_ReportError("Requested video mode %dx%d not supported.",
351 if (bpp != private->info.depth) {
352 WSCONS_ReportError("Requested video depth %d bpp not supported.", bpp);
356 if (!SDL_ReallocFormat(current,
362 WSCONS_ReportError("Couldn't allocate new pixel format");
366 current->flags &= SDL_FULLSCREEN;
367 if (private->shadowFB) {
368 current->flags |= SDL_SWSURFACE;
370 current->flags |= SDL_HWSURFACE;
374 current->pitch = private->fblinebytes;
375 current->pixels = private->fbstart;
377 SDL_memset(private->fbstart, 0, private->fbmem_len);
382 static int WSCONS_AllocHWSurface(_THIS, SDL_Surface *surface)
386 static void WSCONS_FreeHWSurface(_THIS, SDL_Surface *surface)
390 static int WSCONS_LockHWSurface(_THIS, SDL_Surface *surface)
395 static void WSCONS_UnlockHWSurface(_THIS, SDL_Surface *surface)
399 static void WSCONS_blit16(Uint8 *byte_src_pos,
408 Uint16 *src_pos = (Uint16 *)byte_src_pos;
409 Uint16 *dst_pos = (Uint16 *)byte_dst_pos;
412 Uint16 *src = src_pos;
413 Uint16 *dst = dst_pos;
414 for (w = width; w != 0; w--) {
416 src += srcRightDelta;
419 dst_pos = (Uint16 *)((Uint8 *)dst_pos + dst_linebytes);
420 src_pos += srcDownDelta;
425 #define BLOCKSIZE_W 32
426 #define BLOCKSIZE_H 32
428 static void WSCONS_blit16blocked(Uint8 *byte_src_pos,
437 Uint16 *src_pos = (Uint16 *)byte_src_pos;
438 Uint16 *dst_pos = (Uint16 *)byte_dst_pos;
441 Uint16 *src = src_pos;
442 Uint16 *dst = dst_pos;
443 for (w = width; w > 0; w -= BLOCKSIZE_W) {
444 WSCONS_blit16((Uint8 *)src,
450 min(height, BLOCKSIZE_H));
451 src += srcRightDelta * BLOCKSIZE_W;
454 dst_pos = (Uint16 *)((Uint8 *)dst_pos + dst_linebytes * BLOCKSIZE_H);
455 src_pos += srcDownDelta * BLOCKSIZE_H;
456 height -= BLOCKSIZE_H;
460 static void WSCONS_UpdateRects(_THIS, int numrects, SDL_Rect *rects)
462 int width = private->SDL_modelist[0]->w;
463 int height = private->SDL_modelist[0]->h;
464 int bytesPerPixel = (private->info.depth + 7) / 8;
467 if (!private->shadowFB) {
471 if (private->info.depth != 16) {
472 WSCONS_ReportError("Shadow copy only implemented for 16 bpp");
476 for (i = 0; i < numrects; i++) {
478 int scr_x1, scr_y1, scr_x2, scr_y2;
480 int shadowRightDelta; /* Address change when moving right in dest */
481 int shadowDownDelta; /* Address change when moving down in dest */
487 x2 = x1 + rects[i].w;
488 y2 = y1 + rects[i].h;
492 } else if (x1 > width) {
497 } else if (x2 > width) {
502 } else if (y1 > height) {
507 } else if (y2 > height) {
510 if (x2 <= x1 || y2 <= y1) {
514 switch (private->rotate) {
515 case WSCONS_ROTATE_NONE:
516 sha_x1 = scr_x1 = x1;
517 sha_y1 = scr_y1 = y1;
520 shadowRightDelta = 1;
521 shadowDownDelta = width;
523 case WSCONS_ROTATE_CCW:
530 shadowRightDelta = width;
531 shadowDownDelta = -1;
533 case WSCONS_ROTATE_UD:
535 scr_y1 = height - y2;
537 scr_y2 = height - y1;
540 shadowRightDelta = -1;
541 shadowDownDelta = -width;
543 case WSCONS_ROTATE_CW:
544 scr_x1 = height - y2;
546 scr_x2 = height - y1;
550 shadowRightDelta = -width;
554 WSCONS_ReportError("Unknown rotation");
558 src_start = private->shadowmem + (sha_y1 * width + sha_x1) * bytesPerPixel;
559 dst_start = private->physmem + scr_y1 * private->physlinebytes +
560 scr_x1 * bytesPerPixel;
562 private->blitFunc(src_start,
566 private->physlinebytes,
572 int WSCONS_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
578 * Note: If we are terminated, this could be called in the middle of
579 * another SDL video routine -- notably UpdateRects.
581 void WSCONS_VideoQuit(_THIS)
583 int mode = WSDISPLAYIO_MODE_EMUL;
585 if (private->shadowmem != NULL) {
586 SDL_free(private->shadowmem);
587 private->shadowmem = NULL;
589 private->fbstart = NULL;
590 if (this->screen != NULL) {
591 this->screen->pixels = NULL;
594 if (private->SDL_modelist[0] != NULL) {
595 SDL_free(private->SDL_modelist[0]);
596 private->SDL_modelist[0] = NULL;
599 if (ioctl(private->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
600 WSCONS_ReportError("ioctl SMODE");
603 WSCONS_ReleaseKeyboard(this);
605 if (private->fd != -1) {