19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/types.h>
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/ioctl.h>
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <fcntl.h>
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <string.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <termios.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <ctype.h>
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <linux/vt.h>
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <linux/kd.h>
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <linux/keyboard.h>
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <linux/fb.h>
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_video.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_mouse.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysvideo.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_pixels_c.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_sysevents.h"
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_ipodvideo.h"
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define _THIS SDL_VideoDevice *this
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int iPod_VideoInit (_THIS, SDL_PixelFormat *vformat);
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Rect **iPod_ListModes (_THIS, SDL_PixelFormat *format, Uint32 flags);
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Surface *iPod_SetVideoMode (_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int iPod_SetColors (_THIS, int firstcolor, int ncolors, SDL_Color *colors);
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void iPod_UpdateRects (_THIS, int nrects, SDL_Rect *rects);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void iPod_VideoQuit (_THIS);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void iPod_PumpEvents (_THIS);
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic long iPod_GetGeneration();
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int initd = 0;
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int kbfd = -1;
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int fbfd = -1;
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int oldvt = -1;
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int curvt = -1;
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int old_kbmode = -1;
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic long generation = 0;
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic struct termios old_termios, cur_termios;
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallFILE *dbgout;
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define LCD_DATA          0x10
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define LCD_CMD           0x08
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define IPOD_OLD_LCD_BASE 0xc0001000
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define IPOD_OLD_LCD_RTC  0xcf001110
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define IPOD_NEW_LCD_BASE 0x70003000
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define IPOD_NEW_LCD_RTC  0x60005010
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic unsigned long lcd_base, lcd_rtc, lcd_width, lcd_height;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic long iPod_GetGeneration()
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i;
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char cpuinfo[256];
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *ptr;
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    FILE *file;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ((file = fopen("/proc/cpuinfo", "r")) != NULL) {
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while (fgets(cpuinfo, sizeof(cpuinfo), file) != NULL)
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (SDL_strncmp(cpuinfo, "Revision", 8) == 0)
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fclose(file);
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for (i = 0; !isspace(cpuinfo[i]); i++);
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for (; isspace(cpuinfo[i]); i++);
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ptr = cpuinfo + i + 2;
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return SDL_strtol(ptr, NULL, 10);
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int iPod_Available()
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 1;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void iPod_DeleteDevice (SDL_VideoDevice *device)
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    free (device->hidden);
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    free (device);
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid iPod_InitOSKeymap (_THIS) {}
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_VideoDevice *iPod_CreateDevice (int devindex)
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_VideoDevice *this;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this = (SDL_VideoDevice *)SDL_malloc (sizeof(SDL_VideoDevice));
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (this) {
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	memset (this, 0, sizeof *this);
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden = (struct SDL_PrivateVideoData *) SDL_malloc (sizeof(struct SDL_PrivateVideoData));
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (!this || !this->hidden) {
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_OutOfMemory();
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (this)
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    SDL_free (this);
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    memset (this->hidden, 0, sizeof(struct SDL_PrivateVideoData));
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    generation = iPod_GetGeneration();
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->VideoInit = iPod_VideoInit;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->ListModes = iPod_ListModes;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->SetVideoMode = iPod_SetVideoMode;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->SetColors = iPod_SetColors;
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->UpdateRects = iPod_UpdateRects;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->VideoQuit = iPod_VideoQuit;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->AllocHWSurface = 0;
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->CheckHWBlit = 0;
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->FillHWRect = 0;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->SetHWColorKey = 0;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->SetHWAlpha = 0;
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->LockHWSurface = 0;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->UnlockHWSurface = 0;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->FlipHWSurface = 0;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->FreeHWSurface = 0;
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->SetCaption = 0;
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->SetIcon = 0;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->IconifyWindow = 0;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->GrabInput = 0;
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->GetWMInfo = 0;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->InitOSKeymap = iPod_InitOSKeymap;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->PumpEvents = iPod_PumpEvents;
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->free = iPod_DeleteDevice;
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return this;
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallVideoBootStrap iPod_bootstrap = {
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    "ipod", "iPod Framebuffer Driver",
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    iPod_Available, iPod_CreateDevice
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//--//
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int iPod_VideoInit (_THIS, SDL_PixelFormat *vformat)
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (!initd) {
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*** Code adapted/copied from SDL fbcon driver. ***/
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static const char * const tty0[] = { "/dev/tty0", "/dev/vc/0", 0 };
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static const char * const vcs[] = { "/dev/vc/%d", "/dev/tty%d", 0 };
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, tty0_fd;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	dbgout = fdopen (open ("/etc/sdlpod.log", O_WRONLY | O_SYNC | O_APPEND), "a");
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (dbgout) {
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    setbuf (dbgout, 0);
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    fprintf (dbgout, "--> Started SDL <--\n");
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// Try to query for a free VT
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tty0_fd = -1;
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; tty0[i] && (tty0_fd < 0); ++i ) {
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    tty0_fd = open(tty0[i], O_WRONLY, 0);
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( tty0_fd < 0 ) {
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    tty0_fd = dup(0); /* Maybe stdin is a VT? */
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ioctl(tty0_fd, VT_OPENQRY, &curvt);
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	close(tty0_fd);
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tty0_fd = open("/dev/tty", O_RDWR, 0);
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( tty0_fd >= 0 ) {
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    ioctl(tty0_fd, TIOCNOTTY, 0);
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    close(tty0_fd);
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (geteuid() == 0) && (curvt > 0) ) {
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    for ( i=0; vcs[i] && (kbfd < 0); ++i ) {
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		char vtpath[12];
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_snprintf(vtpath, SDL_arraysize(vtpath), vcs[i], curvt);
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		kbfd = open(vtpath, O_RDWR);
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( kbfd < 0 ) {
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (dbgout) fprintf (dbgout, "Couldn't open any VC\n");
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    return -1;
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (dbgout) fprintf (stderr, "Current VT: %d\n", curvt);
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (kbfd >= 0) {
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Switch to the correct virtual terminal */
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if ( curvt > 0 ) {
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		struct vt_stat vtstate;
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ioctl(kbfd, VT_GETSTATE, &vtstate) == 0 ) {
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    oldvt = vtstate.v_active;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ioctl(kbfd, VT_ACTIVATE, curvt) == 0 ) {
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    if (dbgout) fprintf (dbgout, "Waiting for switch to this VT... ");
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    ioctl(kbfd, VT_WAITACTIVE, curvt);
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    if (dbgout) fprintf (dbgout, "done!\n");
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    // Set terminal input mode
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (tcgetattr (kbfd, &old_termios) < 0) {
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (dbgout) fprintf (dbgout, "Can't get termios\n");
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    cur_termios = old_termios;
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    //	    cur_termios.c_iflag &= ~(ICRNL | INPCK | ISTRIP | IXON);
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    //	    cur_termios.c_iflag |= (BRKINT);
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    //	    cur_termios.c_lflag &= ~(ICANON | ECHO | ISIG | IEXTEN);
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    //	    cur_termios.c_oflag &= ~(OPOST);
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    //	    cur_termios.c_oflag |= (ONOCR | ONLRET);
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    cur_termios.c_lflag &= ~(ICANON | ECHO | ISIG);
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    cur_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    cur_termios.c_cc[VMIN] = 0;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    cur_termios.c_cc[VTIME] = 0;
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (tcsetattr (kbfd, TCSAFLUSH, &cur_termios) < 0) {
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (dbgout) fprintf (dbgout, "Can't set termios\n");
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (ioctl (kbfd, KDSKBMODE, K_MEDIUMRAW) < 0) {
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (dbgout) fprintf (dbgout, "Can't set medium-raw mode\n");
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (ioctl (kbfd, KDSETMODE, KD_GRAPHICS) < 0) {
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (dbgout) fprintf (dbgout, "Can't set graphics\n");
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	// Open the framebuffer
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((fbfd = open ("/dev/fb0", O_RDWR)) < 0) {
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (dbgout) fprintf (dbgout, "Can't open framebuffer\n");
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    return -1;
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    struct fb_var_screeninfo vinfo;
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (dbgout) fprintf (dbgout, "Generation: %ld\n", generation);
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (generation >= 40000) {
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lcd_base = IPOD_NEW_LCD_BASE;
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    } else {
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lcd_base = IPOD_OLD_LCD_BASE;
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    ioctl (fbfd, FBIOGET_VSCREENINFO, &vinfo);
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    close (fbfd);
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (lcd_base == IPOD_OLD_LCD_BASE)
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lcd_rtc = IPOD_OLD_LCD_RTC;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    else if (lcd_base == IPOD_NEW_LCD_BASE)
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lcd_rtc = IPOD_NEW_LCD_RTC;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    else {
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError ("Unknown iPod version");
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    lcd_width = vinfo.xres;
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    lcd_height = vinfo.yres;
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (dbgout) fprintf (dbgout, "LCD is %dx%d\n", lcd_width, lcd_height);
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fcntl (kbfd, F_SETFL, O_RDWR | O_NONBLOCK);
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Determine the current screen size */
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->info.current_w = lcd_width;
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->info.current_h = lcd_height;
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((generation >= 60000) && (generation < 70000)) {
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    vformat->BitsPerPixel = 16;
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    vformat->Rmask = 0xF800;
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    vformat->Gmask = 0x07E0;
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    vformat->Bmask = 0x001F;
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    vformat->BitsPerPixel = 8;
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    vformat->Rmask = vformat->Gmask = vformat->Bmask = 0;
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	initd = 1;
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (dbgout) fprintf (dbgout, "Initialized.\n\n");
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Rect **iPod_ListModes (_THIS, SDL_PixelFormat *format, Uint32 flags)
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int width, height, fd;
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    static SDL_Rect r;
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    static SDL_Rect *rs[2] = { &r, 0 };
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ((fd = open ("/dev/fb0", O_RDWR)) < 0) {
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct fb_var_screeninfo vinfo;
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ioctl (fbfd, FBIOGET_VSCREENINFO, &vinfo);
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	close (fbfd);
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	width = vinfo.xres;
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	height = vinfo.yres;
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    r.x = r.y = 0;
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    r.w = width;
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    r.h = height;
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return rs;
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Surface *iPod_SetVideoMode (_THIS, SDL_Surface *current, int width, int height, int bpp,
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				       Uint32 flags)
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Uint32 Rmask, Gmask, Bmask;
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (bpp > 8) {
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Rmask = 0xF800;
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Gmask = 0x07E0;
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Bmask = 0x001F;
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Rmask = Gmask = Bmask = 0;
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (this->hidden->buffer) SDL_free (this->hidden->buffer);
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->hidden->buffer = SDL_malloc (width * height * (bpp / 8));
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (!this->hidden->buffer) {
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError ("Couldn't allocate buffer for requested mode");
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    memset (this->hidden->buffer, 0, width * height * (bpp / 8));
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (!SDL_ReallocFormat (current, bpp, Rmask, Gmask, Bmask, 0)) {
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError ("Couldn't allocate new pixel format");
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free (this->hidden->buffer);
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->buffer = 0;
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (bpp <= 8) {
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, j;
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i = 0; i < 256; i += 4) {
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    for (j = 0; j < 4; j++) {
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		current->format->palette->colors[i+j].r = 85 * j;
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		current->format->palette->colors[i+j].g = 85 * j;
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		current->format->palette->colors[i+j].b = 85 * j;
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    current->flags = flags & SDL_FULLSCREEN;
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->hidden->w = current->w = width;
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->hidden->h = current->h = height;
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    current->pitch = current->w * (bpp / 8);
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    current->pixels = this->hidden->buffer;
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return current;
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int iPod_SetColors (_THIS, int firstcolor, int ncolors, SDL_Color *colors)
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (SDL_VideoSurface && SDL_VideoSurface->format && SDL_VideoSurface->format->palette) {
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, j;
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i = 0; i < 256; i += 4) {
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    for (j = 0; j < 4; j++) {
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_VideoSurface->format->palette->colors[i+j].r = 85 * j;
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_VideoSurface->format->palette->colors[i+j].g = 85 * j;
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_VideoSurface->format->palette->colors[i+j].b = 85 * j;
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void iPod_VideoQuit (_THIS)
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ioctl (kbfd, KDSETMODE, KD_TEXT);
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    tcsetattr (kbfd, TCSAFLUSH, &old_termios);
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    old_kbmode = -1;
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (oldvt > 0)
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ioctl (kbfd, VT_ACTIVATE, oldvt);
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (kbfd > 0)
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	close (kbfd);
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (dbgout) {
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf (dbgout, "<-- Ended SDL -->\n");
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fclose (dbgout);
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    kbfd = -1;
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic char iPod_SC_keymap[] = {
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    0,				/* 0 - no key */
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    '[' - 0x40,			/* ESC (Ctrl+[) */
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    '1', '2', '3', '4', '5', '6', '7', '8', '9',
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    '-', '=',
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    '\b', '\t',			/* Backspace, Tab (Ctrl+H,Ctrl+I) */
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']',
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    '\n', 0,			/* Enter, Left CTRL */
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`',
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    0, '\\',			/* left shift, backslash */
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    0, '*', 0, ' ', 0,		/* right shift, KP mul, left alt, space, capslock */
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F1-10 */
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    0, 0,			/* numlock, scrollock */
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', /* numeric keypad */
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    0, 0,			/* padding */
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    0, 0, 0,			/* "less" (?), F11, F12 */
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    0, 0, 0, 0, 0, 0, 0,	/* padding */
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    '\n', 0, '/', 0, 0,	/* KP enter, Rctrl, Ctrl, KP div, PrtSc, RAlt */
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    0, 0, 0, 0, 0, 0, 0, 0, 0,	/* Break, Home, Up, PgUp, Left, Right, End, Down, PgDn */
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    0, 0,			/* Ins, Del */
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* padding */
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    0, 0,			/* RWin, LWin */
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    0				/* no key */
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void iPod_keyboard()
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    unsigned char keybuf[128];
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i, nread;
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_keysym keysym;
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_Event ev;
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    keysym.mod = 0;
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    keysym.scancode = 0xff;
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    memset (&ev, 0, sizeof(SDL_Event));
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    nread = read (kbfd, keybuf, 128);
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for (i = 0; i < nread; i++) {
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char ascii = iPod_SC_keymap[keybuf[i] & 0x7f];
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (dbgout) fprintf (dbgout, "Key! %02x is %c %s", keybuf[i], ascii, (keybuf[i] & 0x80)? "up" : "down");
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	keysym.sym = keysym.unicode = ascii;
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ev.type = (keybuf[i] & 0x80)? SDL_KEYUP : SDL_KEYDOWN;
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ev.key.state = 0;
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ev.key.keysym = keysym;
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_PushEvent (&ev);
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void iPod_PumpEvents (_THIS)
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fd_set fdset;
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int max_fd = 0;
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    static struct timeval zero;
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int posted;
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    do {
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	posted = 0;
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FD_ZERO (&fdset);
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (kbfd >= 0) {
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    FD_SET (kbfd, &fdset);
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    max_fd = kbfd;
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (dbgout) fprintf (dbgout, "Selecting");
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (select (max_fd + 1, &fdset, 0, 0, &zero) > 0) {
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (dbgout) fprintf (dbgout, " -> match!\n");
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    iPod_keyboard();
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    posted++;
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (dbgout) fprintf (dbgout, "\n");
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } while (posted);
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// enough space for 160x128x2
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic char ipod_scr[160 * (128/4)];
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define outl(datum,addr) (*(volatile unsigned long *)(addr) = (datum))
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define inl(addr) (*(volatile unsigned long *)(addr))
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*** The following LCD code is taken from Linux kernel uclinux-2.4.24-uc0-ipod2,
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     file arch/armnommu/mach-ipod/fb.c. A few modifications have been made. ***/
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* get current usec counter */
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int M_timer_get_current(void)
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return inl(lcd_rtc);
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* check if number of useconds has past */
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int M_timer_check(int clock_start, int usecs)
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned long clock;
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	clock = inl(lcd_rtc);
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (clock - clock_start) >= usecs ) {
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 1;
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* wait for LCD with timeout */
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void M_lcd_wait_write(void)
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (inl(lcd_base) & 0x8000) != 0 ) {
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int start = M_timer_get_current();
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		do {
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( (inl(lcd_base) & (unsigned int)0x8000) == 0 )
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} while ( M_timer_check(start, 1000) == 0 );
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* send LCD data */
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void M_lcd_send_data(int data_lo, int data_hi)
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	M_lcd_wait_write();
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	outl(data_lo, lcd_base + LCD_DATA);
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	M_lcd_wait_write();
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	outl(data_hi, lcd_base + LCD_DATA);
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* send LCD command */
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallM_lcd_prepare_cmd(int cmd)
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	M_lcd_wait_write();
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	outl(0x0, lcd_base + LCD_CMD);
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	M_lcd_wait_write();
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	outl(cmd, lcd_base + LCD_CMD);
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* send LCD command and data */
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void M_lcd_cmd_and_data(int cmd, int data_lo, int data_hi)
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	M_lcd_prepare_cmd(cmd);
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	M_lcd_send_data(data_lo, data_hi);
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Copied from uW
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void M_update_display(int sx, int sy, int mx, int my)
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int y;
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned short cursor_pos;
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sx >>= 3;
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mx >>= 3;
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cursor_pos = sx + (sy << 5);
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( y = sy; y <= my; y++ ) {
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		unsigned char *img_data;
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int x;
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* move the cursor */
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		M_lcd_cmd_and_data(0x11, cursor_pos >> 8, cursor_pos & 0xff);
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* setup for printing */
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		M_lcd_prepare_cmd(0x12);
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		img_data = ipod_scr + (sx << 1) + (y * (lcd_width/4));
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* loops up to 160 times */
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( x = sx; x <= mx; x++ ) {
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		        /* display eight pixels */
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			M_lcd_send_data(*(img_data + 1), *img_data);
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			img_data += 2;
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* update cursor pos counter */
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cursor_pos += 0x20;
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* get current usec counter */
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int C_timer_get_current(void)
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return inl(0x60005010);
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* check if number of useconds has past */
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int C_timer_check(int clock_start, int usecs)
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned long clock;
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	clock = inl(0x60005010);
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (clock - clock_start) >= usecs ) {
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 1;
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* wait for LCD with timeout */
6279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void C_lcd_wait_write(void)
6289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((inl(0x70008A0C) & 0x80000000) != 0) {
6309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int start = C_timer_get_current();
6319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		do {
6339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ((inl(0x70008A0C) & 0x80000000) == 0)
6349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
6359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} while (C_timer_check(start, 1000) == 0);
6369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void C_lcd_cmd_data(int cmd, int data)
6399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	C_lcd_wait_write();
6419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	outl(cmd | 0x80000000, 0x70008A0C);
6429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	C_lcd_wait_write();
6449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	outl(data | 0x80000000, 0x70008A0C);
6459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void C_update_display(int sx, int sy, int mx, int my)
6489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int height = (my - sy) + 1;
6509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int width = (mx - sx) + 1;
6519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *addr = SDL_VideoSurface->pixels;
6539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (width & 1) width++;
6559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* start X and Y */
6579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	C_lcd_cmd_data(0x12, (sy & 0xff));
6589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	C_lcd_cmd_data(0x13, (((SDL_VideoSurface->w - 1) - sx) & 0xff));
6599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* max X and Y */
6619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	C_lcd_cmd_data(0x15, (((sy + height) - 1) & 0xff));
6629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	C_lcd_cmd_data(0x16, (((((SDL_VideoSurface->w - 1) - sx) - width) + 1) & 0xff));
6639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	addr += sx + sy * SDL_VideoSurface->pitch;
6659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while (height > 0) {
6679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int h, x, y, pixels_to_write;
6689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pixels_to_write = (width * height) * 2;
6709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* calculate how much we can do in one go */
6729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		h = height;
6739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (pixels_to_write > 64000) {
6749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			h = (64000/2) / width;
6759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			pixels_to_write = (width * h) * 2;
6769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		outl(0x10000080, 0x70008A20);
6799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		outl((pixels_to_write - 1) | 0xC0010000, 0x70008A24);
6809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		outl(0x34000000, 0x70008A20);
6819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* for each row */
6839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for (x = 0; x < h; x++)
6849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
6859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* for each column */
6869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for (y = 0; y < width; y += 2) {
6879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				unsigned two_pixels;
6889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				two_pixels = addr[0] | (addr[1] << 16);
6909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				addr += 2;
6919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				while ((inl(0x70008A20) & 0x1000000) == 0);
6939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* output 2 pixels */
6959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				outl(two_pixels, 0x70008B00);
6969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
6979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			addr += SDL_VideoSurface->w - width;
6999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		while ((inl(0x70008A20) & 0x4000000) == 0);
7029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		outl(0x0, 0x70008A24);
7049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		height = height - h;
7069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
7089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Should work with photo. However, I don't have one, so I'm not sure.
7109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void iPod_UpdateRects (_THIS, int nrects, SDL_Rect *rects)
7119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
7129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (SDL_VideoSurface->format->BitsPerPixel == 16) {
7139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	C_update_display (0, 0, lcd_width, lcd_height);
7149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
7159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, y, x;
7169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i = 0; i < nrects; i++) {
7179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    SDL_Rect *r = rects + i;
7189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (!r) {
7199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		continue;
7209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
7219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    for (y = r->y; (y < r->y + r->h) && y < lcd_height; y++) {
7239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for (x = r->x; (x < r->x + r->w) && x < lcd_width; x++) {
7249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    ipod_scr[y*(lcd_width/4) + x/4] &= ~(3 << (2 * (x%4)));
7259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    ipod_scr[y*(lcd_width/4) + x/4] |=
7269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			(((Uint8*)(SDL_VideoSurface->pixels))[ y*SDL_VideoSurface->pitch + x ] & 3) << (2 * (x%4));
7279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
7299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	M_update_display (0, 0, lcd_width, lcd_height);
7329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
7339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
734