src/video/ipod/SDL_ipodvideo.c
author Ozkan Sezer <sezeroz@gmail.com>
Sun, 14 Oct 2018 15:25:04 +0300
branchSDL-1.2
changeset 12325 c4f2eeda176f
parent 6137 4720145f848b
permissions -rw-r--r--
remove symlink for libSDL-1.0.so.0 from the rpm spec file.

also fix a changelog date.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2012 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 	/* Determine the current screen size */
   291 	this->info.current_w = lcd_width;
   292 	this->info.current_h = lcd_height;
   293 
   294 	if ((generation >= 60000) && (generation < 70000)) {
   295 	    vformat->BitsPerPixel = 16;
   296 	    vformat->Rmask = 0xF800;
   297 	    vformat->Gmask = 0x07E0;
   298 	    vformat->Bmask = 0x001F;
   299 	} else {
   300 	    vformat->BitsPerPixel = 8;
   301 	    vformat->Rmask = vformat->Gmask = vformat->Bmask = 0;
   302 	}
   303 
   304 	initd = 1;
   305 	if (dbgout) fprintf (dbgout, "Initialized.\n\n");
   306     }
   307     return 0;
   308 }
   309 
   310 static SDL_Rect **iPod_ListModes (_THIS, SDL_PixelFormat *format, Uint32 flags)
   311 {
   312     int width, height, fd;
   313     static SDL_Rect r;
   314     static SDL_Rect *rs[2] = { &r, 0 };
   315 
   316     if ((fd = open ("/dev/fb0", O_RDWR)) < 0) {
   317 	return 0;
   318     } else {
   319 	struct fb_var_screeninfo vinfo;
   320 	
   321 	ioctl (fbfd, FBIOGET_VSCREENINFO, &vinfo);
   322 	close (fbfd);
   323 	
   324 	width = vinfo.xres;
   325 	height = vinfo.yres;
   326     }
   327     r.x = r.y = 0;
   328     r.w = width;
   329     r.h = height;
   330     return rs;
   331 }
   332 
   333 
   334 static SDL_Surface *iPod_SetVideoMode (_THIS, SDL_Surface *current, int width, int height, int bpp,
   335 				       Uint32 flags)
   336 {
   337     Uint32 Rmask, Gmask, Bmask;
   338     if (bpp > 8) {
   339 	Rmask = 0xF800;
   340 	Gmask = 0x07E0;
   341 	Bmask = 0x001F;	
   342     } else {
   343 	Rmask = Gmask = Bmask = 0;
   344     }
   345 
   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");
   350 	return 0;
   351     }
   352 
   353     memset (this->hidden->buffer, 0, width * height * (bpp / 8));
   354 
   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;
   359 	return 0;
   360     }
   361 
   362     if (bpp <= 8) {
   363 	int i, j;
   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;
   369 	    }
   370 	}
   371     }
   372 
   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;
   378 
   379     return current;
   380 }
   381 
   382 static int iPod_SetColors (_THIS, int firstcolor, int ncolors, SDL_Color *colors)
   383 {
   384     if (SDL_VideoSurface && SDL_VideoSurface->format && SDL_VideoSurface->format->palette) {
   385 	int i, j;
   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;
   391 	    }
   392 	}
   393     }
   394     return 0;
   395 }
   396 
   397 static void iPod_VideoQuit (_THIS)
   398 {
   399     ioctl (kbfd, KDSETMODE, KD_TEXT);
   400     tcsetattr (kbfd, TCSAFLUSH, &old_termios);
   401     old_kbmode = -1;
   402 
   403     if (oldvt > 0)
   404 	ioctl (kbfd, VT_ACTIVATE, oldvt);
   405     
   406     if (kbfd > 0)
   407 	close (kbfd);
   408 
   409     if (dbgout) {
   410 	fprintf (dbgout, "<-- Ended SDL -->\n");
   411 	fclose (dbgout);
   412     }
   413     
   414     kbfd = -1;
   415 }
   416 
   417 static char iPod_SC_keymap[] = {
   418     0,				/* 0 - no key */
   419     '[' - 0x40,			/* ESC (Ctrl+[) */
   420     '1', '2', '3', '4', '5', '6', '7', '8', '9',
   421     '-', '=',
   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 */
   432     0, 0,			/* padding */
   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 */
   437     0, 0,			/* Ins, Del */
   438     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* padding */
   439     0, 0,			/* RWin, LWin */
   440     0				/* no key */
   441 };
   442     
   443 
   444 static void iPod_keyboard() 
   445 {
   446     unsigned char keybuf[128];
   447     int i, nread;
   448     SDL_keysym keysym;
   449     SDL_Event ev;
   450 
   451     keysym.mod = 0;
   452     keysym.scancode = 0xff;
   453     memset (&ev, 0, sizeof(SDL_Event));
   454 
   455     nread = read (kbfd, keybuf, 128);
   456     for (i = 0; i < nread; i++) {
   457 	char ascii = iPod_SC_keymap[keybuf[i] & 0x7f];
   458 
   459 	if (dbgout) fprintf (dbgout, "Key! %02x is %c %s", keybuf[i], ascii, (keybuf[i] & 0x80)? "up" : "down");
   460 
   461 	keysym.sym = keysym.unicode = ascii;
   462 	ev.type = (keybuf[i] & 0x80)? SDL_KEYUP : SDL_KEYDOWN;
   463 	ev.key.state = 0;
   464 	ev.key.keysym = keysym;
   465 	SDL_PushEvent (&ev);
   466     }
   467 }
   468 
   469 static void iPod_PumpEvents (_THIS) 
   470 {
   471     fd_set fdset;
   472     int max_fd = 0;
   473     static struct timeval zero;
   474     int posted;
   475 
   476     do {
   477 	posted = 0;
   478 
   479 	FD_ZERO (&fdset);
   480 	if (kbfd >= 0) {
   481 	    FD_SET (kbfd, &fdset);
   482 	    max_fd = kbfd;
   483 	}
   484 	if (dbgout) fprintf (dbgout, "Selecting");
   485 	if (select (max_fd + 1, &fdset, 0, 0, &zero) > 0) {
   486 	    if (dbgout) fprintf (dbgout, " -> match!\n");
   487 	    iPod_keyboard();
   488 	    posted++;
   489 	}
   490 	if (dbgout) fprintf (dbgout, "\n");
   491     } while (posted);
   492 }
   493 
   494 // enough space for 160x128x2
   495 static char ipod_scr[160 * (128/4)];
   496 
   497 #define outl(datum,addr) (*(volatile unsigned long *)(addr) = (datum))
   498 #define inl(addr) (*(volatile unsigned long *)(addr))
   499 
   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. ***/
   502 
   503 /* get current usec counter */
   504 static int M_timer_get_current(void)
   505 {
   506 	return inl(lcd_rtc);
   507 }
   508 
   509 /* check if number of useconds has past */
   510 static int M_timer_check(int clock_start, int usecs)
   511 {
   512 	unsigned long clock;
   513 	clock = inl(lcd_rtc);
   514 	
   515 	if ( (clock - clock_start) >= usecs ) {
   516 		return 1;
   517 	} else {
   518 		return 0;
   519 	}
   520 }
   521 
   522 /* wait for LCD with timeout */
   523 static void M_lcd_wait_write(void)
   524 {
   525 	if ( (inl(lcd_base) & 0x8000) != 0 ) {
   526 		int start = M_timer_get_current();
   527 			
   528 		do {
   529 			if ( (inl(lcd_base) & (unsigned int)0x8000) == 0 ) 
   530 				break;
   531 		} while ( M_timer_check(start, 1000) == 0 );
   532 	}
   533 }
   534 
   535 
   536 /* send LCD data */
   537 static void M_lcd_send_data(int data_lo, int data_hi)
   538 {
   539 	M_lcd_wait_write();
   540 	
   541 	outl(data_lo, lcd_base + LCD_DATA);
   542 		
   543 	M_lcd_wait_write();
   544 	
   545 	outl(data_hi, lcd_base + LCD_DATA);
   546 
   547 }
   548 
   549 /* send LCD command */
   550 static void
   551 M_lcd_prepare_cmd(int cmd)
   552 {
   553 	M_lcd_wait_write();
   554 
   555 	outl(0x0, lcd_base + LCD_CMD);
   556 
   557 	M_lcd_wait_write();
   558 	
   559 	outl(cmd, lcd_base + LCD_CMD);
   560 	
   561 }
   562 
   563 /* send LCD command and data */
   564 static void M_lcd_cmd_and_data(int cmd, int data_lo, int data_hi)
   565 {
   566 	M_lcd_prepare_cmd(cmd);
   567 
   568 	M_lcd_send_data(data_lo, data_hi);
   569 }
   570 
   571 // Copied from uW
   572 static void M_update_display(int sx, int sy, int mx, int my)
   573 {
   574 	int y;
   575 	unsigned short cursor_pos;
   576 
   577 	sx >>= 3;
   578 	mx >>= 3;
   579 
   580 	cursor_pos = sx + (sy << 5);
   581 
   582 	for ( y = sy; y <= my; y++ ) {
   583 		unsigned char *img_data;
   584 		int x;
   585 
   586 		/* move the cursor */
   587 		M_lcd_cmd_and_data(0x11, cursor_pos >> 8, cursor_pos & 0xff);
   588 
   589 		/* setup for printing */
   590 		M_lcd_prepare_cmd(0x12);
   591 
   592 		img_data = ipod_scr + (sx << 1) + (y * (lcd_width/4));
   593 
   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);
   598 
   599 			img_data += 2;
   600 		}
   601 
   602 		/* update cursor pos counter */
   603 		cursor_pos += 0x20;
   604 	}
   605 }
   606 
   607 /* get current usec counter */
   608 static int C_timer_get_current(void)
   609 {
   610 	return inl(0x60005010);
   611 }
   612 
   613 /* check if number of useconds has past */
   614 static int C_timer_check(int clock_start, int usecs)
   615 {
   616 	unsigned long clock;
   617 	clock = inl(0x60005010);
   618 	
   619 	if ( (clock - clock_start) >= usecs ) {
   620 		return 1;
   621 	} else {
   622 		return 0;
   623 	}
   624 }
   625 
   626 /* wait for LCD with timeout */
   627 static void C_lcd_wait_write(void)
   628 {
   629 	if ((inl(0x70008A0C) & 0x80000000) != 0) {
   630 		int start = C_timer_get_current();
   631 			
   632 		do {
   633 			if ((inl(0x70008A0C) & 0x80000000) == 0) 
   634 				break;
   635 		} while (C_timer_check(start, 1000) == 0);
   636 	}
   637 }
   638 static void C_lcd_cmd_data(int cmd, int data)
   639 {
   640 	C_lcd_wait_write();
   641 	outl(cmd | 0x80000000, 0x70008A0C);
   642 
   643 	C_lcd_wait_write();
   644 	outl(data | 0x80000000, 0x70008A0C);
   645 }
   646 
   647 static void C_update_display(int sx, int sy, int mx, int my)
   648 {
   649 	int height = (my - sy) + 1;
   650 	int width = (mx - sx) + 1;
   651 
   652 	char *addr = SDL_VideoSurface->pixels;
   653 
   654 	if (width & 1) width++;
   655 
   656 	/* start X and Y */
   657 	C_lcd_cmd_data(0x12, (sy & 0xff));
   658 	C_lcd_cmd_data(0x13, (((SDL_VideoSurface->w - 1) - sx) & 0xff));
   659 
   660 	/* max X and Y */
   661 	C_lcd_cmd_data(0x15, (((sy + height) - 1) & 0xff));
   662 	C_lcd_cmd_data(0x16, (((((SDL_VideoSurface->w - 1) - sx) - width) + 1) & 0xff));
   663 
   664 	addr += sx + sy * SDL_VideoSurface->pitch;
   665 
   666 	while (height > 0) {
   667 		int h, x, y, pixels_to_write;
   668 
   669 		pixels_to_write = (width * height) * 2;
   670 
   671 		/* calculate how much we can do in one go */
   672 		h = height;
   673 		if (pixels_to_write > 64000) {
   674 			h = (64000/2) / width;
   675 			pixels_to_write = (width * h) * 2;
   676 		}
   677 
   678 		outl(0x10000080, 0x70008A20);
   679 		outl((pixels_to_write - 1) | 0xC0010000, 0x70008A24);
   680 		outl(0x34000000, 0x70008A20);
   681 
   682 		/* for each row */
   683 		for (x = 0; x < h; x++)
   684 		{
   685 			/* for each column */
   686 			for (y = 0; y < width; y += 2) {
   687 				unsigned two_pixels;
   688 
   689 				two_pixels = addr[0] | (addr[1] << 16);
   690 				addr += 2;
   691 
   692 				while ((inl(0x70008A20) & 0x1000000) == 0);
   693 
   694 				/* output 2 pixels */
   695 				outl(two_pixels, 0x70008B00);
   696 			}
   697 
   698 			addr += SDL_VideoSurface->w - width;
   699 		}
   700 
   701 		while ((inl(0x70008A20) & 0x4000000) == 0);
   702 
   703 		outl(0x0, 0x70008A24);
   704 
   705 		height = height - h;
   706 	}
   707 }
   708 
   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) 
   711 {
   712     if (SDL_VideoSurface->format->BitsPerPixel == 16) {
   713 	C_update_display (0, 0, lcd_width, lcd_height);
   714     } else {
   715 	int i, y, x;
   716 	for (i = 0; i < nrects; i++) {
   717 	    SDL_Rect *r = rects + i;
   718 	    if (!r) {
   719 		continue;
   720 	    }
   721 	    
   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));
   727 		}
   728 	    }
   729 	}
   730 	
   731 	M_update_display (0, 0, lcd_width, lcd_height);
   732     }
   733 }