src/video/ipod/SDL_ipodvideo.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 21 Feb 2006 08:46:50 +0000
changeset 1402 d910939febfa
parent 1361 19418e4422cb
child 1545 8d9bb0cf2c2a
permissions -rw-r--r--
Use consistent identifiers for the various platforms we support.
Make sure every source file includes SDL_config.h, so the proper system
headers are chosen.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     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.
     9 
    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.
    14 
    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
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include <sys/types.h>
    25 #include <sys/ioctl.h>
    26 
    27 #include <unistd.h>
    28 #include <fcntl.h>
    29 #include <string.h>
    30 #include <termios.h>
    31 #include <ctype.h>
    32 
    33 #include <linux/vt.h>
    34 #include <linux/kd.h>
    35 #include <linux/keyboard.h>
    36 #include <linux/fb.h>
    37 
    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"
    45 
    46 #define _THIS SDL_VideoDevice *this
    47 
    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);
    55 
    56 static long iPod_GetGeneration();
    57 
    58 static int initd = 0;
    59 static int kbfd = -1;
    60 static int fbfd = -1;
    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;
    66 
    67 FILE *dbgout;
    68 
    69 #define LCD_DATA          0x10
    70 #define LCD_CMD           0x08
    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
    75 
    76 static unsigned long lcd_base, lcd_rtc, lcd_width, lcd_height;
    77 
    78 static long iPod_GetGeneration() 
    79 {
    80     int i;
    81     char cpuinfo[256];
    82     char *ptr;
    83     FILE *file;
    84     
    85     if ((file = fopen("/proc/cpuinfo", "r")) != NULL) {
    86 	while (fgets(cpuinfo, sizeof(cpuinfo), file) != NULL)
    87 	    if (SDL_strncmp(cpuinfo, "Revision", 8) == 0)
    88 		break;
    89 	fclose(file);
    90     }
    91     for (i = 0; !isspace(cpuinfo[i]); i++);
    92     for (; isspace(cpuinfo[i]); i++);
    93     ptr = cpuinfo + i + 2;
    94     
    95     return SDL_strtol(ptr, NULL, 10);
    96 }
    97 
    98 static int iPod_Available() 
    99 {
   100     return 1;
   101 }
   102 
   103 static void iPod_DeleteDevice (SDL_VideoDevice *device)
   104 {
   105     free (device->hidden);
   106     free (device);
   107 }
   108 
   109 void iPod_InitOSKeymap (_THIS) {}
   110 
   111 static SDL_VideoDevice *iPod_CreateDevice (int devindex)
   112 {
   113     SDL_VideoDevice *this;
   114     
   115     this = (SDL_VideoDevice *)SDL_malloc (sizeof(SDL_VideoDevice));
   116     if (this) {
   117 	memset (this, 0, sizeof *this);
   118 	this->hidden = (struct SDL_PrivateVideoData *) SDL_malloc (sizeof(struct SDL_PrivateVideoData));
   119     }
   120     if (!this || !this->hidden) {
   121 	SDL_OutOfMemory();
   122 	if (this)
   123 	    SDL_free (this);
   124 	return 0;
   125     }
   126     memset (this->hidden, 0, sizeof(struct SDL_PrivateVideoData));
   127     
   128     generation = iPod_GetGeneration();
   129 
   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;
   146     this->SetIcon = 0;
   147     this->IconifyWindow = 0;
   148     this->GrabInput = 0;
   149     this->GetWMInfo = 0;
   150     this->InitOSKeymap = iPod_InitOSKeymap;
   151     this->PumpEvents = iPod_PumpEvents;
   152     this->free = iPod_DeleteDevice;
   153 
   154     return this;
   155 }
   156 
   157 VideoBootStrap iPod_bootstrap = {
   158     "ipod", "iPod Framebuffer Driver",
   159     iPod_Available, iPod_CreateDevice
   160 };
   161 
   162 //--//
   163 
   164 static int iPod_VideoInit (_THIS, SDL_PixelFormat *vformat)
   165 {
   166     if (!initd) {
   167 	/*** Code adapted/copied from SDL fbcon driver. ***/
   168 
   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 };
   171 	int i, tty0_fd;
   172 
   173 	dbgout = fdopen (open ("/etc/sdlpod.log", O_WRONLY | O_SYNC | O_APPEND), "a");
   174 	if (dbgout) {
   175 	    setbuf (dbgout, 0);
   176 	    fprintf (dbgout, "--> Started SDL <--\n");
   177 	}
   178 
   179 	// Try to query for a free VT
   180 	tty0_fd = -1;
   181 	for ( i=0; tty0[i] && (tty0_fd < 0); ++i ) {
   182 	    tty0_fd = open(tty0[i], O_WRONLY, 0);
   183 	}
   184 	if ( tty0_fd < 0 ) {
   185 	    tty0_fd = dup(0); /* Maybe stdin is a VT? */
   186 	}
   187 	ioctl(tty0_fd, VT_OPENQRY, &curvt);
   188 	close(tty0_fd);
   189 
   190 	tty0_fd = open("/dev/tty", O_RDWR, 0);
   191 	if ( tty0_fd >= 0 ) {
   192 	    ioctl(tty0_fd, TIOCNOTTY, 0);
   193 	    close(tty0_fd);
   194 	}
   195 
   196 	if ( (geteuid() == 0) && (curvt > 0) ) {
   197 	    for ( i=0; vcs[i] && (kbfd < 0); ++i ) {
   198 		char vtpath[12];
   199 		
   200 		SDL_snprintf(vtpath, SDL_arraysize(vtpath), vcs[i], curvt);
   201 		kbfd = open(vtpath, O_RDWR);
   202 	    }
   203 	}
   204 	if ( kbfd < 0 ) {
   205 	    if (dbgout) fprintf (dbgout, "Couldn't open any VC\n");
   206 	    return -1;
   207 	}
   208 	if (dbgout) fprintf (stderr, "Current VT: %d\n", curvt);
   209 
   210 	if (kbfd >= 0) {
   211 	    /* Switch to the correct virtual terminal */
   212 	    if ( curvt > 0 ) {
   213 		struct vt_stat vtstate;
   214 		
   215 		if ( ioctl(kbfd, VT_GETSTATE, &vtstate) == 0 ) {
   216 		    oldvt = vtstate.v_active;
   217 		}
   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");
   222 		}
   223 	    }
   224 
   225 	    // Set terminal input mode
   226 	    if (tcgetattr (kbfd, &old_termios) < 0) {
   227 		if (dbgout) fprintf (dbgout, "Can't get termios\n");
   228 		return -1;
   229 	    }
   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;
   240 	    
   241 	    if (tcsetattr (kbfd, TCSAFLUSH, &cur_termios) < 0) {
   242 		if (dbgout) fprintf (dbgout, "Can't set termios\n");
   243 		return -1;
   244 	    }
   245 	    if (ioctl (kbfd, KDSKBMODE, K_MEDIUMRAW) < 0) {
   246 		if (dbgout) fprintf (dbgout, "Can't set medium-raw mode\n");
   247 		return -1;
   248 	    }
   249 	    if (ioctl (kbfd, KDSETMODE, KD_GRAPHICS) < 0) {
   250 		if (dbgout) fprintf (dbgout, "Can't set graphics\n");
   251 		return -1;
   252 	    }
   253 	}
   254 
   255 	// Open the framebuffer
   256 	if ((fbfd = open ("/dev/fb0", O_RDWR)) < 0) {
   257 	    if (dbgout) fprintf (dbgout, "Can't open framebuffer\n");
   258 	    return -1;
   259 	} else {
   260 	    struct fb_var_screeninfo vinfo;
   261 
   262 	    if (dbgout) fprintf (dbgout, "Generation: %ld\n", generation);
   263 
   264 	    if (generation >= 40000) {
   265 		lcd_base = IPOD_NEW_LCD_BASE;
   266 	    } else {
   267 		lcd_base = IPOD_OLD_LCD_BASE;
   268 	    }
   269 	    
   270 	    ioctl (fbfd, FBIOGET_VSCREENINFO, &vinfo);
   271 	    close (fbfd);
   272 
   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;
   277 	    else {
   278 		SDL_SetError ("Unknown iPod version");
   279 		return -1;
   280 	    }
   281 
   282 	    lcd_width = vinfo.xres;
   283 	    lcd_height = vinfo.yres;
   284 
   285 	    if (dbgout) fprintf (dbgout, "LCD is %dx%d\n", lcd_width, lcd_height);
   286 	}
   287 
   288 	fcntl (kbfd, F_SETFL, O_RDWR | O_NONBLOCK);
   289 
   290 	if ((generation >= 60000) && (generation < 70000)) {
   291 	    vformat->BitsPerPixel = 16;
   292 	    vformat->Rmask = 0xF800;
   293 	    vformat->Gmask = 0x07E0;
   294 	    vformat->Bmask = 0x001F;
   295 	} else {
   296 	    vformat->BitsPerPixel = 8;
   297 	    vformat->Rmask = vformat->Gmask = vformat->Bmask = 0;
   298 	}
   299 
   300 	initd = 1;
   301 	if (dbgout) fprintf (dbgout, "Initialized.\n\n");
   302     }
   303     return 0;
   304 }
   305 
   306 static SDL_Rect **iPod_ListModes (_THIS, SDL_PixelFormat *format, Uint32 flags)
   307 {
   308     int width, height, fd;
   309     static SDL_Rect r;
   310     static SDL_Rect *rs[2] = { &r, 0 };
   311 
   312     if ((fd = open ("/dev/fb0", O_RDWR)) < 0) {
   313 	return 0;
   314     } else {
   315 	struct fb_var_screeninfo vinfo;
   316 	
   317 	ioctl (fbfd, FBIOGET_VSCREENINFO, &vinfo);
   318 	close (fbfd);
   319 	
   320 	width = vinfo.xres;
   321 	height = vinfo.yres;
   322     }
   323     r.x = r.y = 0;
   324     r.w = width;
   325     r.h = height;
   326     return rs;
   327 }
   328 
   329 
   330 static SDL_Surface *iPod_SetVideoMode (_THIS, SDL_Surface *current, int width, int height, int bpp,
   331 				       Uint32 flags)
   332 {
   333     Uint32 Rmask, Gmask, Bmask;
   334     if (bpp > 8) {
   335 	Rmask = 0xF800;
   336 	Gmask = 0x07E0;
   337 	Bmask = 0x001F;	
   338     } else {
   339 	Rmask = Gmask = Bmask = 0;
   340     }
   341 
   342     if (this->hidden->buffer) SDL_free (this->hidden->buffer);
   343     this->hidden->buffer = SDL_malloc (width * height * (bpp / 8));
   344     if (!this->hidden->buffer) {
   345 	SDL_SetError ("Couldn't allocate buffer for requested mode");
   346 	return 0;
   347     }
   348 
   349     memset (this->hidden->buffer, 0, width * height * (bpp / 8));
   350 
   351     if (!SDL_ReallocFormat (current, bpp, Rmask, Gmask, Bmask, 0)) {
   352 	SDL_SetError ("Couldn't allocate new pixel format");
   353 	SDL_free (this->hidden->buffer);
   354 	this->hidden->buffer = 0;
   355 	return 0;
   356     }
   357 
   358     if (bpp <= 8) {
   359 	int i, j;
   360 	for (i = 0; i < 256; i += 4) {
   361 	    for (j = 0; j < 4; j++) {
   362 		current->format->palette->colors[i+j].r = 85 * j;
   363 		current->format->palette->colors[i+j].g = 85 * j;
   364 		current->format->palette->colors[i+j].b = 85 * j;
   365 	    }
   366 	}
   367     }
   368 
   369     current->flags = flags & SDL_FULLSCREEN;
   370     this->hidden->w = current->w = width;
   371     this->hidden->h = current->h = height;
   372     current->pitch = current->w * (bpp / 8);
   373     current->pixels = this->hidden->buffer;
   374 
   375     return current;
   376 }
   377 
   378 static int iPod_SetColors (_THIS, int firstcolor, int ncolors, SDL_Color *colors)
   379 {
   380     if (SDL_VideoSurface && SDL_VideoSurface->format && SDL_VideoSurface->format->palette) {
   381 	int i, j;
   382 	for (i = 0; i < 256; i += 4) {
   383 	    for (j = 0; j < 4; j++) {
   384 		SDL_VideoSurface->format->palette->colors[i+j].r = 85 * j;
   385 		SDL_VideoSurface->format->palette->colors[i+j].g = 85 * j;
   386 		SDL_VideoSurface->format->palette->colors[i+j].b = 85 * j;
   387 	    }
   388 	}
   389     }
   390     return 0;
   391 }
   392 
   393 static void iPod_VideoQuit (_THIS)
   394 {
   395     ioctl (kbfd, KDSETMODE, KD_TEXT);
   396     tcsetattr (kbfd, TCSAFLUSH, &old_termios);
   397     old_kbmode = -1;
   398 
   399     if (oldvt > 0)
   400 	ioctl (kbfd, VT_ACTIVATE, oldvt);
   401     
   402     if (kbfd > 0)
   403 	close (kbfd);
   404 
   405     if (dbgout) {
   406 	fprintf (dbgout, "<-- Ended SDL -->\n");
   407 	fclose (dbgout);
   408     }
   409     
   410     kbfd = -1;
   411 }
   412 
   413 static char iPod_SC_keymap[] = {
   414     0,				/* 0 - no key */
   415     '[' - 0x40,			/* ESC (Ctrl+[) */
   416     '1', '2', '3', '4', '5', '6', '7', '8', '9',
   417     '-', '=',
   418     '\b', '\t',			/* Backspace, Tab (Ctrl+H,Ctrl+I) */
   419     'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']',
   420     '\n', 0,			/* Enter, Left CTRL */
   421     'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`',
   422     0, '\\',			/* left shift, backslash */
   423     'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
   424     0, '*', 0, ' ', 0,		/* right shift, KP mul, left alt, space, capslock */
   425     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F1-10 */
   426     0, 0,			/* numlock, scrollock */
   427     '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', /* numeric keypad */
   428     0, 0,			/* padding */
   429     0, 0, 0,			/* "less" (?), F11, F12 */
   430     0, 0, 0, 0, 0, 0, 0,	/* padding */
   431     '\n', 0, '/', 0, 0,	/* KP enter, Rctrl, Ctrl, KP div, PrtSc, RAlt */
   432     0, 0, 0, 0, 0, 0, 0, 0, 0,	/* Break, Home, Up, PgUp, Left, Right, End, Down, PgDn */
   433     0, 0,			/* Ins, Del */
   434     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* padding */
   435     0, 0,			/* RWin, LWin */
   436     0				/* no key */
   437 };
   438     
   439 
   440 static void iPod_keyboard() 
   441 {
   442     unsigned char keybuf[128];
   443     int i, nread;
   444     SDL_keysym keysym;
   445     SDL_Event ev;
   446 
   447     keysym.mod = 0;
   448     keysym.scancode = 0xff;
   449     memset (&ev, 0, sizeof(SDL_Event));
   450 
   451     nread = read (kbfd, keybuf, 128);
   452     for (i = 0; i < nread; i++) {
   453 	char ascii = iPod_SC_keymap[keybuf[i] & 0x7f];
   454 
   455 	if (dbgout) fprintf (dbgout, "Key! %02x is %c %s", keybuf[i], ascii, (keybuf[i] & 0x80)? "up" : "down");
   456 
   457 	keysym.sym = keysym.unicode = ascii;
   458 	ev.type = (keybuf[i] & 0x80)? SDL_KEYUP : SDL_KEYDOWN;
   459 	ev.key.state = 0;
   460 	ev.key.keysym = keysym;
   461 	SDL_PushEvent (&ev);
   462     }
   463 }
   464 
   465 static void iPod_PumpEvents (_THIS) 
   466 {
   467     fd_set fdset;
   468     int max_fd = 0;
   469     static struct timeval zero;
   470     int posted;
   471 
   472     do {
   473 	posted = 0;
   474 
   475 	FD_ZERO (&fdset);
   476 	if (kbfd >= 0) {
   477 	    FD_SET (kbfd, &fdset);
   478 	    max_fd = kbfd;
   479 	}
   480 	if (dbgout) fprintf (dbgout, "Selecting");
   481 	if (select (max_fd + 1, &fdset, 0, 0, &zero) > 0) {
   482 	    if (dbgout) fprintf (dbgout, " -> match!\n");
   483 	    iPod_keyboard();
   484 	    posted++;
   485 	}
   486 	if (dbgout) fprintf (dbgout, "\n");
   487     } while (posted);
   488 }
   489 
   490 // enough space for 160x128x2
   491 static char ipod_scr[160 * (128/4)];
   492 
   493 #define outl(datum,addr) (*(volatile unsigned long *)(addr) = (datum))
   494 #define inl(addr) (*(volatile unsigned long *)(addr))
   495 
   496 /*** The following LCD code is taken from Linux kernel uclinux-2.4.24-uc0-ipod2,
   497      file arch/armnommu/mach-ipod/fb.c. A few modifications have been made. ***/
   498 
   499 /* get current usec counter */
   500 static int M_timer_get_current(void)
   501 {
   502 	return inl(lcd_rtc);
   503 }
   504 
   505 /* check if number of useconds has past */
   506 static int M_timer_check(int clock_start, int usecs)
   507 {
   508 	unsigned long clock;
   509 	clock = inl(lcd_rtc);
   510 	
   511 	if ( (clock - clock_start) >= usecs ) {
   512 		return 1;
   513 	} else {
   514 		return 0;
   515 	}
   516 }
   517 
   518 /* wait for LCD with timeout */
   519 static void M_lcd_wait_write(void)
   520 {
   521 	if ( (inl(lcd_base) & 0x8000) != 0 ) {
   522 		int start = M_timer_get_current();
   523 			
   524 		do {
   525 			if ( (inl(lcd_base) & (unsigned int)0x8000) == 0 ) 
   526 				break;
   527 		} while ( M_timer_check(start, 1000) == 0 );
   528 	}
   529 }
   530 
   531 
   532 /* send LCD data */
   533 static void M_lcd_send_data(int data_lo, int data_hi)
   534 {
   535 	M_lcd_wait_write();
   536 	
   537 	outl(data_lo, lcd_base + LCD_DATA);
   538 		
   539 	M_lcd_wait_write();
   540 	
   541 	outl(data_hi, lcd_base + LCD_DATA);
   542 
   543 }
   544 
   545 /* send LCD command */
   546 static void
   547 M_lcd_prepare_cmd(int cmd)
   548 {
   549 	M_lcd_wait_write();
   550 
   551 	outl(0x0, lcd_base + LCD_CMD);
   552 
   553 	M_lcd_wait_write();
   554 	
   555 	outl(cmd, lcd_base + LCD_CMD);
   556 	
   557 }
   558 
   559 /* send LCD command and data */
   560 static void M_lcd_cmd_and_data(int cmd, int data_lo, int data_hi)
   561 {
   562 	M_lcd_prepare_cmd(cmd);
   563 
   564 	M_lcd_send_data(data_lo, data_hi);
   565 }
   566 
   567 // Copied from uW
   568 static void M_update_display(int sx, int sy, int mx, int my)
   569 {
   570 	int y;
   571 	unsigned short cursor_pos;
   572 
   573 	sx >>= 3;
   574 	mx >>= 3;
   575 
   576 	cursor_pos = sx + (sy << 5);
   577 
   578 	for ( y = sy; y <= my; y++ ) {
   579 		unsigned char *img_data;
   580 		int x;
   581 
   582 		/* move the cursor */
   583 		M_lcd_cmd_and_data(0x11, cursor_pos >> 8, cursor_pos & 0xff);
   584 
   585 		/* setup for printing */
   586 		M_lcd_prepare_cmd(0x12);
   587 
   588 		img_data = ipod_scr + (sx << 1) + (y * (lcd_width/4));
   589 
   590 		/* loops up to 160 times */
   591 		for ( x = sx; x <= mx; x++ ) {
   592 		        /* display eight pixels */
   593 			M_lcd_send_data(*(img_data + 1), *img_data);
   594 
   595 			img_data += 2;
   596 		}
   597 
   598 		/* update cursor pos counter */
   599 		cursor_pos += 0x20;
   600 	}
   601 }
   602 
   603 /* get current usec counter */
   604 static int C_timer_get_current(void)
   605 {
   606 	return inl(0x60005010);
   607 }
   608 
   609 /* check if number of useconds has past */
   610 static int C_timer_check(int clock_start, int usecs)
   611 {
   612 	unsigned long clock;
   613 	clock = inl(0x60005010);
   614 	
   615 	if ( (clock - clock_start) >= usecs ) {
   616 		return 1;
   617 	} else {
   618 		return 0;
   619 	}
   620 }
   621 
   622 /* wait for LCD with timeout */
   623 static void C_lcd_wait_write(void)
   624 {
   625 	if ((inl(0x70008A0C) & 0x80000000) != 0) {
   626 		int start = C_timer_get_current();
   627 			
   628 		do {
   629 			if ((inl(0x70008A0C) & 0x80000000) == 0) 
   630 				break;
   631 		} while (C_timer_check(start, 1000) == 0);
   632 	}
   633 }
   634 static void C_lcd_cmd_data(int cmd, int data)
   635 {
   636 	C_lcd_wait_write();
   637 	outl(cmd | 0x80000000, 0x70008A0C);
   638 
   639 	C_lcd_wait_write();
   640 	outl(data | 0x80000000, 0x70008A0C);
   641 }
   642 
   643 static void C_update_display(int sx, int sy, int mx, int my)
   644 {
   645 	int height = (my - sy) + 1;
   646 	int width = (mx - sx) + 1;
   647 
   648 	char *addr = SDL_VideoSurface->pixels;
   649 
   650 	if (width & 1) width++;
   651 
   652 	/* start X and Y */
   653 	C_lcd_cmd_data(0x12, (sy & 0xff));
   654 	C_lcd_cmd_data(0x13, (((SDL_VideoSurface->w - 1) - sx) & 0xff));
   655 
   656 	/* max X and Y */
   657 	C_lcd_cmd_data(0x15, (((sy + height) - 1) & 0xff));
   658 	C_lcd_cmd_data(0x16, (((((SDL_VideoSurface->w - 1) - sx) - width) + 1) & 0xff));
   659 
   660 	addr += sx + sy * SDL_VideoSurface->pitch;
   661 
   662 	while (height > 0) {
   663 		int h, x, y, pixels_to_write;
   664 
   665 		pixels_to_write = (width * height) * 2;
   666 
   667 		/* calculate how much we can do in one go */
   668 		h = height;
   669 		if (pixels_to_write > 64000) {
   670 			h = (64000/2) / width;
   671 			pixels_to_write = (width * h) * 2;
   672 		}
   673 
   674 		outl(0x10000080, 0x70008A20);
   675 		outl((pixels_to_write - 1) | 0xC0010000, 0x70008A24);
   676 		outl(0x34000000, 0x70008A20);
   677 
   678 		/* for each row */
   679 		for (x = 0; x < h; x++)
   680 		{
   681 			/* for each column */
   682 			for (y = 0; y < width; y += 2) {
   683 				unsigned two_pixels;
   684 
   685 				two_pixels = addr[0] | (addr[1] << 16);
   686 				addr += 2;
   687 
   688 				while ((inl(0x70008A20) & 0x1000000) == 0);
   689 
   690 				/* output 2 pixels */
   691 				outl(two_pixels, 0x70008B00);
   692 			}
   693 
   694 			addr += SDL_VideoSurface->w - width;
   695 		}
   696 
   697 		while ((inl(0x70008A20) & 0x4000000) == 0);
   698 
   699 		outl(0x0, 0x70008A24);
   700 
   701 		height = height - h;
   702 	}
   703 }
   704 
   705 // Should work with photo. However, I don't have one, so I'm not sure.
   706 static void iPod_UpdateRects (_THIS, int nrects, SDL_Rect *rects) 
   707 {
   708     if (SDL_VideoSurface->format->BitsPerPixel == 16) {
   709 	C_update_display (0, 0, lcd_width, lcd_height);
   710     } else {
   711 	int i, y, x;
   712 	for (i = 0; i < nrects; i++) {
   713 	    SDL_Rect *r = rects + i;
   714 	    if (!r) {
   715 		continue;
   716 	    }
   717 	    
   718 	    for (y = r->y; (y < r->y + r->h) && y < lcd_height; y++) {
   719 		for (x = r->x; (x < r->x + r->w) && x < lcd_width; x++) {
   720 		    ipod_scr[y*(lcd_width/4) + x/4] &= ~(3 << (2 * (x%4)));
   721 		    ipod_scr[y*(lcd_width/4) + x/4] |=
   722 			(((Uint8*)(SDL_VideoSurface->pixels))[ y*SDL_VideoSurface->pitch + x ] & 3) << (2 * (x%4));
   723 		}
   724 	    }
   725 	}
   726 	
   727 	M_update_display (0, 0, lcd_width, lcd_height);
   728     }
   729 }