src/video/ipod/SDL_ipodvideo.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 15 Mar 2006 17:46:41 +0000
changeset 1545 8d9bb0cf2c2a
parent 1402 d910939febfa
child 1662 782fd950bd46
child 1895 c121d94672cb
child 4159 a1b03ba2fcd0
permissions -rw-r--r--
Added current_w and current_h to the SDL_VideoInfo structure, which is set to the desktop resolution during video intialization, and then set to the current resolution when a video mode is set.

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