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"
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
35 #include <linux/keyboard.h>
38 #include "SDL_video.h"
39 #include "SDL_mouse.h"
40 #include "../SDL_sysvideo.h"
41 #include "../SDL_pixels_c.h"
42 #include "../../events/SDL_events_c.h"
43 #include "SDL_sysevents.h"
44 #include "SDL_ipodvideo.h"
46 #define _THIS SDL_VideoDevice *this
48 static int iPod_VideoInit (_THIS, SDL_PixelFormat *vformat);
49 static SDL_Rect **iPod_ListModes (_THIS, SDL_PixelFormat *format, Uint32 flags);
50 static SDL_Surface *iPod_SetVideoMode (_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
51 static int iPod_SetColors (_THIS, int firstcolor, int ncolors, SDL_Color *colors);
52 static void iPod_UpdateRects (_THIS, int nrects, SDL_Rect *rects);
53 static void iPod_VideoQuit (_THIS);
54 static void iPod_PumpEvents (_THIS);
56 static long iPod_GetGeneration();
61 static int oldvt = -1;
62 static int curvt = -1;
63 static int old_kbmode = -1;
64 static long generation = 0;
65 static struct termios old_termios, cur_termios;
71 #define IPOD_OLD_LCD_BASE 0xc0001000
72 #define IPOD_OLD_LCD_RTC 0xcf001110
73 #define IPOD_NEW_LCD_BASE 0x70003000
74 #define IPOD_NEW_LCD_RTC 0x60005010
76 static unsigned long lcd_base, lcd_rtc, lcd_width, lcd_height;
78 static long iPod_GetGeneration()
85 if ((file = fopen("/proc/cpuinfo", "r")) != NULL) {
86 while (fgets(cpuinfo, sizeof(cpuinfo), file) != NULL)
87 if (SDL_strncmp(cpuinfo, "Revision", 8) == 0)
91 for (i = 0; !isspace(cpuinfo[i]); i++);
92 for (; isspace(cpuinfo[i]); i++);
93 ptr = cpuinfo + i + 2;
95 return SDL_strtol(ptr, NULL, 10);
98 static int iPod_Available()
103 static void iPod_DeleteDevice (SDL_VideoDevice *device)
105 free (device->hidden);
109 void iPod_InitOSKeymap (_THIS) {}
111 static SDL_VideoDevice *iPod_CreateDevice (int devindex)
113 SDL_VideoDevice *this;
115 this = (SDL_VideoDevice *)SDL_malloc (sizeof(SDL_VideoDevice));
117 memset (this, 0, sizeof *this);
118 this->hidden = (struct SDL_PrivateVideoData *) SDL_malloc (sizeof(struct SDL_PrivateVideoData));
120 if (!this || !this->hidden) {
126 memset (this->hidden, 0, sizeof(struct SDL_PrivateVideoData));
128 generation = iPod_GetGeneration();
130 this->VideoInit = iPod_VideoInit;
131 this->ListModes = iPod_ListModes;
132 this->SetVideoMode = iPod_SetVideoMode;
133 this->SetColors = iPod_SetColors;
134 this->UpdateRects = iPod_UpdateRects;
135 this->VideoQuit = iPod_VideoQuit;
136 this->AllocHWSurface = 0;
137 this->CheckHWBlit = 0;
138 this->FillHWRect = 0;
139 this->SetHWColorKey = 0;
140 this->SetHWAlpha = 0;
141 this->LockHWSurface = 0;
142 this->UnlockHWSurface = 0;
143 this->FlipHWSurface = 0;
144 this->FreeHWSurface = 0;
145 this->SetCaption = 0;
147 this->IconifyWindow = 0;
150 this->InitOSKeymap = iPod_InitOSKeymap;
151 this->PumpEvents = iPod_PumpEvents;
152 this->free = iPod_DeleteDevice;
157 VideoBootStrap iPod_bootstrap = {
158 "ipod", "iPod Framebuffer Driver",
159 iPod_Available, iPod_CreateDevice
164 static int iPod_VideoInit (_THIS, SDL_PixelFormat *vformat)
167 /*** Code adapted/copied from SDL fbcon driver. ***/
169 static const char * const tty0[] = { "/dev/tty0", "/dev/vc/0", 0 };
170 static const char * const vcs[] = { "/dev/vc/%d", "/dev/tty%d", 0 };
173 dbgout = fdopen (open ("/etc/sdlpod.log", O_WRONLY | O_SYNC | O_APPEND), "a");
176 fprintf (dbgout, "--> Started SDL <--\n");
179 // Try to query for a free VT
181 for ( i=0; tty0[i] && (tty0_fd < 0); ++i ) {
182 tty0_fd = open(tty0[i], O_WRONLY, 0);
185 tty0_fd = dup(0); /* Maybe stdin is a VT? */
187 ioctl(tty0_fd, VT_OPENQRY, &curvt);
190 tty0_fd = open("/dev/tty", O_RDWR, 0);
191 if ( tty0_fd >= 0 ) {
192 ioctl(tty0_fd, TIOCNOTTY, 0);
196 if ( (geteuid() == 0) && (curvt > 0) ) {
197 for ( i=0; vcs[i] && (kbfd < 0); ++i ) {
200 SDL_snprintf(vtpath, SDL_arraysize(vtpath), vcs[i], curvt);
201 kbfd = open(vtpath, O_RDWR);
205 if (dbgout) fprintf (dbgout, "Couldn't open any VC\n");
208 if (dbgout) fprintf (stderr, "Current VT: %d\n", curvt);
211 /* Switch to the correct virtual terminal */
213 struct vt_stat vtstate;
215 if ( ioctl(kbfd, VT_GETSTATE, &vtstate) == 0 ) {
216 oldvt = vtstate.v_active;
218 if ( ioctl(kbfd, VT_ACTIVATE, curvt) == 0 ) {
219 if (dbgout) fprintf (dbgout, "Waiting for switch to this VT... ");
220 ioctl(kbfd, VT_WAITACTIVE, curvt);
221 if (dbgout) fprintf (dbgout, "done!\n");
225 // Set terminal input mode
226 if (tcgetattr (kbfd, &old_termios) < 0) {
227 if (dbgout) fprintf (dbgout, "Can't get termios\n");
230 cur_termios = old_termios;
231 // cur_termios.c_iflag &= ~(ICRNL | INPCK | ISTRIP | IXON);
232 // cur_termios.c_iflag |= (BRKINT);
233 // cur_termios.c_lflag &= ~(ICANON | ECHO | ISIG | IEXTEN);
234 // cur_termios.c_oflag &= ~(OPOST);
235 // cur_termios.c_oflag |= (ONOCR | ONLRET);
236 cur_termios.c_lflag &= ~(ICANON | ECHO | ISIG);
237 cur_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
238 cur_termios.c_cc[VMIN] = 0;
239 cur_termios.c_cc[VTIME] = 0;
241 if (tcsetattr (kbfd, TCSAFLUSH, &cur_termios) < 0) {
242 if (dbgout) fprintf (dbgout, "Can't set termios\n");
245 if (ioctl (kbfd, KDSKBMODE, K_MEDIUMRAW) < 0) {
246 if (dbgout) fprintf (dbgout, "Can't set medium-raw mode\n");
249 if (ioctl (kbfd, KDSETMODE, KD_GRAPHICS) < 0) {
250 if (dbgout) fprintf (dbgout, "Can't set graphics\n");
255 // Open the framebuffer
256 if ((fbfd = open ("/dev/fb0", O_RDWR)) < 0) {
257 if (dbgout) fprintf (dbgout, "Can't open framebuffer\n");
260 struct fb_var_screeninfo vinfo;
262 if (dbgout) fprintf (dbgout, "Generation: %ld\n", generation);
264 if (generation >= 40000) {
265 lcd_base = IPOD_NEW_LCD_BASE;
267 lcd_base = IPOD_OLD_LCD_BASE;
270 ioctl (fbfd, FBIOGET_VSCREENINFO, &vinfo);
273 if (lcd_base == IPOD_OLD_LCD_BASE)
274 lcd_rtc = IPOD_OLD_LCD_RTC;
275 else if (lcd_base == IPOD_NEW_LCD_BASE)
276 lcd_rtc = IPOD_NEW_LCD_RTC;
278 SDL_SetError ("Unknown iPod version");
282 lcd_width = vinfo.xres;
283 lcd_height = vinfo.yres;
285 if (dbgout) fprintf (dbgout, "LCD is %dx%d\n", lcd_width, lcd_height);
288 fcntl (kbfd, F_SETFL, O_RDWR | O_NONBLOCK);
290 /* Determine the current screen size */
291 this->info.current_w = lcd_width;
292 this->info.current_h = lcd_height;
294 if ((generation >= 60000) && (generation < 70000)) {
295 vformat->BitsPerPixel = 16;
296 vformat->Rmask = 0xF800;
297 vformat->Gmask = 0x07E0;
298 vformat->Bmask = 0x001F;
300 vformat->BitsPerPixel = 8;
301 vformat->Rmask = vformat->Gmask = vformat->Bmask = 0;
305 if (dbgout) fprintf (dbgout, "Initialized.\n\n");
310 static SDL_Rect **iPod_ListModes (_THIS, SDL_PixelFormat *format, Uint32 flags)
312 int width, height, fd;
314 static SDL_Rect *rs[2] = { &r, 0 };
316 if ((fd = open ("/dev/fb0", O_RDWR)) < 0) {
319 struct fb_var_screeninfo vinfo;
321 ioctl (fbfd, FBIOGET_VSCREENINFO, &vinfo);
334 static SDL_Surface *iPod_SetVideoMode (_THIS, SDL_Surface *current, int width, int height, int bpp,
337 Uint32 Rmask, Gmask, Bmask;
343 Rmask = Gmask = Bmask = 0;
346 if (this->hidden->buffer) SDL_free (this->hidden->buffer);
347 this->hidden->buffer = SDL_malloc (width * height * (bpp / 8));
348 if (!this->hidden->buffer) {
349 SDL_SetError ("Couldn't allocate buffer for requested mode");
353 memset (this->hidden->buffer, 0, width * height * (bpp / 8));
355 if (!SDL_ReallocFormat (current, bpp, Rmask, Gmask, Bmask, 0)) {
356 SDL_SetError ("Couldn't allocate new pixel format");
357 SDL_free (this->hidden->buffer);
358 this->hidden->buffer = 0;
364 for (i = 0; i < 256; i += 4) {
365 for (j = 0; j < 4; j++) {
366 current->format->palette->colors[i+j].r = 85 * j;
367 current->format->palette->colors[i+j].g = 85 * j;
368 current->format->palette->colors[i+j].b = 85 * j;
373 current->flags = flags & SDL_FULLSCREEN;
374 this->hidden->w = current->w = width;
375 this->hidden->h = current->h = height;
376 current->pitch = current->w * (bpp / 8);
377 current->pixels = this->hidden->buffer;
382 static int iPod_SetColors (_THIS, int firstcolor, int ncolors, SDL_Color *colors)
384 if (SDL_VideoSurface && SDL_VideoSurface->format && SDL_VideoSurface->format->palette) {
386 for (i = 0; i < 256; i += 4) {
387 for (j = 0; j < 4; j++) {
388 SDL_VideoSurface->format->palette->colors[i+j].r = 85 * j;
389 SDL_VideoSurface->format->palette->colors[i+j].g = 85 * j;
390 SDL_VideoSurface->format->palette->colors[i+j].b = 85 * j;
397 static void iPod_VideoQuit (_THIS)
399 ioctl (kbfd, KDSETMODE, KD_TEXT);
400 tcsetattr (kbfd, TCSAFLUSH, &old_termios);
404 ioctl (kbfd, VT_ACTIVATE, oldvt);
410 fprintf (dbgout, "<-- Ended SDL -->\n");
417 static char iPod_SC_keymap[] = {
419 '[' - 0x40, /* ESC (Ctrl+[) */
420 '1', '2', '3', '4', '5', '6', '7', '8', '9',
422 '\b', '\t', /* Backspace, Tab (Ctrl+H,Ctrl+I) */
423 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']',
424 '\n', 0, /* Enter, Left CTRL */
425 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`',
426 0, '\\', /* left shift, backslash */
427 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
428 0, '*', 0, ' ', 0, /* right shift, KP mul, left alt, space, capslock */
429 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F1-10 */
430 0, 0, /* numlock, scrollock */
431 '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', /* numeric keypad */
433 0, 0, 0, /* "less" (?), F11, F12 */
434 0, 0, 0, 0, 0, 0, 0, /* padding */
435 '\n', 0, '/', 0, 0, /* KP enter, Rctrl, Ctrl, KP div, PrtSc, RAlt */
436 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Break, Home, Up, PgUp, Left, Right, End, Down, PgDn */
438 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* padding */
439 0, 0, /* RWin, LWin */
444 static void iPod_keyboard()
446 unsigned char keybuf[128];
452 keysym.scancode = 0xff;
453 memset (&ev, 0, sizeof(SDL_Event));
455 nread = read (kbfd, keybuf, 128);
456 for (i = 0; i < nread; i++) {
457 char ascii = iPod_SC_keymap[keybuf[i] & 0x7f];
459 if (dbgout) fprintf (dbgout, "Key! %02x is %c %s", keybuf[i], ascii, (keybuf[i] & 0x80)? "up" : "down");
461 keysym.sym = keysym.unicode = ascii;
462 ev.type = (keybuf[i] & 0x80)? SDL_KEYUP : SDL_KEYDOWN;
464 ev.key.keysym = keysym;
469 static void iPod_PumpEvents (_THIS)
473 static struct timeval zero;
481 FD_SET (kbfd, &fdset);
484 if (dbgout) fprintf (dbgout, "Selecting");
485 if (select (max_fd + 1, &fdset, 0, 0, &zero) > 0) {
486 if (dbgout) fprintf (dbgout, " -> match!\n");
490 if (dbgout) fprintf (dbgout, "\n");
494 // enough space for 160x128x2
495 static char ipod_scr[160 * (128/4)];
497 #define outl(datum,addr) (*(volatile unsigned long *)(addr) = (datum))
498 #define inl(addr) (*(volatile unsigned long *)(addr))
500 /*** The following LCD code is taken from Linux kernel uclinux-2.4.24-uc0-ipod2,
501 file arch/armnommu/mach-ipod/fb.c. A few modifications have been made. ***/
503 /* get current usec counter */
504 static int M_timer_get_current(void)
509 /* check if number of useconds has past */
510 static int M_timer_check(int clock_start, int usecs)
513 clock = inl(lcd_rtc);
515 if ( (clock - clock_start) >= usecs ) {
522 /* wait for LCD with timeout */
523 static void M_lcd_wait_write(void)
525 if ( (inl(lcd_base) & 0x8000) != 0 ) {
526 int start = M_timer_get_current();
529 if ( (inl(lcd_base) & (unsigned int)0x8000) == 0 )
531 } while ( M_timer_check(start, 1000) == 0 );
537 static void M_lcd_send_data(int data_lo, int data_hi)
541 outl(data_lo, lcd_base + LCD_DATA);
545 outl(data_hi, lcd_base + LCD_DATA);
549 /* send LCD command */
551 M_lcd_prepare_cmd(int cmd)
555 outl(0x0, lcd_base + LCD_CMD);
559 outl(cmd, lcd_base + LCD_CMD);
563 /* send LCD command and data */
564 static void M_lcd_cmd_and_data(int cmd, int data_lo, int data_hi)
566 M_lcd_prepare_cmd(cmd);
568 M_lcd_send_data(data_lo, data_hi);
572 static void M_update_display(int sx, int sy, int mx, int my)
575 unsigned short cursor_pos;
580 cursor_pos = sx + (sy << 5);
582 for ( y = sy; y <= my; y++ ) {
583 unsigned char *img_data;
586 /* move the cursor */
587 M_lcd_cmd_and_data(0x11, cursor_pos >> 8, cursor_pos & 0xff);
589 /* setup for printing */
590 M_lcd_prepare_cmd(0x12);
592 img_data = ipod_scr + (sx << 1) + (y * (lcd_width/4));
594 /* loops up to 160 times */
595 for ( x = sx; x <= mx; x++ ) {
596 /* display eight pixels */
597 M_lcd_send_data(*(img_data + 1), *img_data);
602 /* update cursor pos counter */
607 /* get current usec counter */
608 static int C_timer_get_current(void)
610 return inl(0x60005010);
613 /* check if number of useconds has past */
614 static int C_timer_check(int clock_start, int usecs)
617 clock = inl(0x60005010);
619 if ( (clock - clock_start) >= usecs ) {
626 /* wait for LCD with timeout */
627 static void C_lcd_wait_write(void)
629 if ((inl(0x70008A0C) & 0x80000000) != 0) {
630 int start = C_timer_get_current();
633 if ((inl(0x70008A0C) & 0x80000000) == 0)
635 } while (C_timer_check(start, 1000) == 0);
638 static void C_lcd_cmd_data(int cmd, int data)
641 outl(cmd | 0x80000000, 0x70008A0C);
644 outl(data | 0x80000000, 0x70008A0C);
647 static void C_update_display(int sx, int sy, int mx, int my)
649 int height = (my - sy) + 1;
650 int width = (mx - sx) + 1;
652 char *addr = SDL_VideoSurface->pixels;
654 if (width & 1) width++;
657 C_lcd_cmd_data(0x12, (sy & 0xff));
658 C_lcd_cmd_data(0x13, (((SDL_VideoSurface->w - 1) - sx) & 0xff));
661 C_lcd_cmd_data(0x15, (((sy + height) - 1) & 0xff));
662 C_lcd_cmd_data(0x16, (((((SDL_VideoSurface->w - 1) - sx) - width) + 1) & 0xff));
664 addr += sx + sy * SDL_VideoSurface->pitch;
667 int h, x, y, pixels_to_write;
669 pixels_to_write = (width * height) * 2;
671 /* calculate how much we can do in one go */
673 if (pixels_to_write > 64000) {
674 h = (64000/2) / width;
675 pixels_to_write = (width * h) * 2;
678 outl(0x10000080, 0x70008A20);
679 outl((pixels_to_write - 1) | 0xC0010000, 0x70008A24);
680 outl(0x34000000, 0x70008A20);
683 for (x = 0; x < h; x++)
685 /* for each column */
686 for (y = 0; y < width; y += 2) {
689 two_pixels = addr[0] | (addr[1] << 16);
692 while ((inl(0x70008A20) & 0x1000000) == 0);
694 /* output 2 pixels */
695 outl(two_pixels, 0x70008B00);
698 addr += SDL_VideoSurface->w - width;
701 while ((inl(0x70008A20) & 0x4000000) == 0);
703 outl(0x0, 0x70008A24);
709 // Should work with photo. However, I don't have one, so I'm not sure.
710 static void iPod_UpdateRects (_THIS, int nrects, SDL_Rect *rects)
712 if (SDL_VideoSurface->format->BitsPerPixel == 16) {
713 C_update_display (0, 0, lcd_width, lcd_height);
716 for (i = 0; i < nrects; i++) {
717 SDL_Rect *r = rects + i;
722 for (y = r->y; (y < r->y + r->h) && y < lcd_height; y++) {
723 for (x = r->x; (x < r->x + r->w) && x < lcd_width; x++) {
724 ipod_scr[y*(lcd_width/4) + x/4] &= ~(3 << (2 * (x%4)));
725 ipod_scr[y*(lcd_width/4) + x/4] |=
726 (((Uint8*)(SDL_VideoSurface->pixels))[ y*SDL_VideoSurface->pitch + x ] & 3) << (2 * (x%4));
731 M_update_display (0, 0, lcd_width, lcd_height);