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