📄 sdl_ipodvideo.c
字号:
/* SDL - Simple DirectMedia Layer Copyright (C) 1997-2006 Sam Lantinga This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Sam Lantinga slouken@libsdl.org*/#include "SDL_config.h"#include <sys/types.h>#include <sys/ioctl.h>#include <unistd.h>#include <fcntl.h>#include <string.h>#include <termios.h>#include <ctype.h>#include <linux/vt.h>#include <linux/kd.h>#include <linux/keyboard.h>#include <linux/fb.h>#include "SDL_video.h"#include "SDL_mouse.h"#include "../SDL_sysvideo.h"#include "../SDL_pixels_c.h"#include "../../events/SDL_events_c.h"#include "SDL_sysevents.h"#include "SDL_ipodvideo.h"#define _THIS SDL_VideoDevice *thisstatic int iPod_VideoInit (_THIS, SDL_PixelFormat *vformat);static SDL_Rect **iPod_ListModes (_THIS, SDL_PixelFormat *format, Uint32 flags);static SDL_Surface *iPod_SetVideoMode (_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);static int iPod_SetColors (_THIS, int firstcolor, int ncolors, SDL_Color *colors);static void iPod_UpdateRects (_THIS, int nrects, SDL_Rect *rects);static void iPod_VideoQuit (_THIS);static void iPod_PumpEvents (_THIS);static long iPod_GetGeneration();static int initd = 0;static int kbfd = -1;static int fbfd = -1;static int oldvt = -1;static int curvt = -1;static int old_kbmode = -1;static long generation = 0;static struct termios old_termios, cur_termios;FILE *dbgout;#define LCD_DATA 0x10#define LCD_CMD 0x08#define IPOD_OLD_LCD_BASE 0xc0001000#define IPOD_OLD_LCD_RTC 0xcf001110#define IPOD_NEW_LCD_BASE 0x70003000#define IPOD_NEW_LCD_RTC 0x60005010static unsigned long lcd_base, lcd_rtc, lcd_width, lcd_height;static long iPod_GetGeneration() { int i; char cpuinfo[256]; char *ptr; FILE *file; if ((file = fopen("/proc/cpuinfo", "r")) != NULL) { while (fgets(cpuinfo, sizeof(cpuinfo), file) != NULL) if (SDL_strncmp(cpuinfo, "Revision", 8) == 0) break; fclose(file); } for (i = 0; !isspace(cpuinfo[i]); i++); for (; isspace(cpuinfo[i]); i++); ptr = cpuinfo + i + 2; return SDL_strtol(ptr, NULL, 10);}static int iPod_Available() { return 1;}static void iPod_DeleteDevice (SDL_VideoDevice *device){ free (device->hidden); free (device);}void iPod_InitOSKeymap (_THIS) {}static SDL_VideoDevice *iPod_CreateDevice (int devindex){ SDL_VideoDevice *this; this = (SDL_VideoDevice *)SDL_malloc (sizeof(SDL_VideoDevice)); if (this) { memset (this, 0, sizeof *this); this->hidden = (struct SDL_PrivateVideoData *) SDL_malloc (sizeof(struct SDL_PrivateVideoData)); } if (!this || !this->hidden) { SDL_OutOfMemory(); if (this) SDL_free (this); return 0; } memset (this->hidden, 0, sizeof(struct SDL_PrivateVideoData)); generation = iPod_GetGeneration(); this->VideoInit = iPod_VideoInit; this->ListModes = iPod_ListModes; this->SetVideoMode = iPod_SetVideoMode; this->SetColors = iPod_SetColors; this->UpdateRects = iPod_UpdateRects; this->VideoQuit = iPod_VideoQuit; this->AllocHWSurface = 0; this->CheckHWBlit = 0; this->FillHWRect = 0; this->SetHWColorKey = 0; this->SetHWAlpha = 0; this->LockHWSurface = 0; this->UnlockHWSurface = 0; this->FlipHWSurface = 0; this->FreeHWSurface = 0; this->SetCaption = 0; this->SetIcon = 0; this->IconifyWindow = 0; this->GrabInput = 0; this->GetWMInfo = 0; this->InitOSKeymap = iPod_InitOSKeymap; this->PumpEvents = iPod_PumpEvents; this->free = iPod_DeleteDevice; return this;}VideoBootStrap iPod_bootstrap = { "ipod", "iPod Framebuffer Driver", iPod_Available, iPod_CreateDevice};//--//static int iPod_VideoInit (_THIS, SDL_PixelFormat *vformat){ if (!initd) { /*** Code adapted/copied from SDL fbcon driver. ***/ static const char * const tty0[] = { "/dev/tty0", "/dev/vc/0", 0 }; static const char * const vcs[] = { "/dev/vc/%d", "/dev/tty%d", 0 }; int i, tty0_fd; dbgout = fdopen (open ("/etc/sdlpod.log", O_WRONLY | O_SYNC | O_APPEND), "a"); if (dbgout) { setbuf (dbgout, 0); fprintf (dbgout, "--> Started SDL <--\n"); } // Try to query for a free VT tty0_fd = -1; for ( i=0; tty0[i] && (tty0_fd < 0); ++i ) { tty0_fd = open(tty0[i], O_WRONLY, 0); } if ( tty0_fd < 0 ) { tty0_fd = dup(0); /* Maybe stdin is a VT? */ } ioctl(tty0_fd, VT_OPENQRY, &curvt); close(tty0_fd); tty0_fd = open("/dev/tty", O_RDWR, 0); if ( tty0_fd >= 0 ) { ioctl(tty0_fd, TIOCNOTTY, 0); close(tty0_fd); } if ( (geteuid() == 0) && (curvt > 0) ) { for ( i=0; vcs[i] && (kbfd < 0); ++i ) { char vtpath[12]; SDL_snprintf(vtpath, SDL_arraysize(vtpath), vcs[i], curvt); kbfd = open(vtpath, O_RDWR); } } if ( kbfd < 0 ) { if (dbgout) fprintf (dbgout, "Couldn't open any VC\n"); return -1; } if (dbgout) fprintf (stderr, "Current VT: %d\n", curvt); if (kbfd >= 0) { /* Switch to the correct virtual terminal */ if ( curvt > 0 ) { struct vt_stat vtstate; if ( ioctl(kbfd, VT_GETSTATE, &vtstate) == 0 ) { oldvt = vtstate.v_active; } if ( ioctl(kbfd, VT_ACTIVATE, curvt) == 0 ) { if (dbgout) fprintf (dbgout, "Waiting for switch to this VT... "); ioctl(kbfd, VT_WAITACTIVE, curvt); if (dbgout) fprintf (dbgout, "done!\n"); } } // Set terminal input mode if (tcgetattr (kbfd, &old_termios) < 0) { if (dbgout) fprintf (dbgout, "Can't get termios\n"); return -1; } cur_termios = old_termios; // cur_termios.c_iflag &= ~(ICRNL | INPCK | ISTRIP | IXON); // cur_termios.c_iflag |= (BRKINT); // cur_termios.c_lflag &= ~(ICANON | ECHO | ISIG | IEXTEN); // cur_termios.c_oflag &= ~(OPOST); // cur_termios.c_oflag |= (ONOCR | ONLRET); cur_termios.c_lflag &= ~(ICANON | ECHO | ISIG); cur_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON); cur_termios.c_cc[VMIN] = 0; cur_termios.c_cc[VTIME] = 0; if (tcsetattr (kbfd, TCSAFLUSH, &cur_termios) < 0) { if (dbgout) fprintf (dbgout, "Can't set termios\n"); return -1; } if (ioctl (kbfd, KDSKBMODE, K_MEDIUMRAW) < 0) { if (dbgout) fprintf (dbgout, "Can't set medium-raw mode\n"); return -1; } if (ioctl (kbfd, KDSETMODE, KD_GRAPHICS) < 0) { if (dbgout) fprintf (dbgout, "Can't set graphics\n"); return -1; } } // Open the framebuffer if ((fbfd = open ("/dev/fb0", O_RDWR)) < 0) { if (dbgout) fprintf (dbgout, "Can't open framebuffer\n"); return -1; } else { struct fb_var_screeninfo vinfo; if (dbgout) fprintf (dbgout, "Generation: %ld\n", generation); if (generation >= 40000) { lcd_base = IPOD_NEW_LCD_BASE; } else { lcd_base = IPOD_OLD_LCD_BASE; } ioctl (fbfd, FBIOGET_VSCREENINFO, &vinfo); close (fbfd); if (lcd_base == IPOD_OLD_LCD_BASE) lcd_rtc = IPOD_OLD_LCD_RTC; else if (lcd_base == IPOD_NEW_LCD_BASE) lcd_rtc = IPOD_NEW_LCD_RTC; else { SDL_SetError ("Unknown iPod version"); return -1; } lcd_width = vinfo.xres; lcd_height = vinfo.yres; if (dbgout) fprintf (dbgout, "LCD is %dx%d\n", lcd_width, lcd_height); } fcntl (kbfd, F_SETFL, O_RDWR | O_NONBLOCK); /* Determine the current screen size */ this->info.current_w = lcd_width; this->info.current_h = lcd_height; if ((generation >= 60000) && (generation < 70000)) { vformat->BitsPerPixel = 16; vformat->Rmask = 0xF800; vformat->Gmask = 0x07E0; vformat->Bmask = 0x001F; } else { vformat->BitsPerPixel = 8; vformat->Rmask = vformat->Gmask = vformat->Bmask = 0; } initd = 1; if (dbgout) fprintf (dbgout, "Initialized.\n\n"); } return 0;}static SDL_Rect **iPod_ListModes (_THIS, SDL_PixelFormat *format, Uint32 flags){ int width, height, fd; static SDL_Rect r; static SDL_Rect *rs[2] = { &r, 0 }; if ((fd = open ("/dev/fb0", O_RDWR)) < 0) { return 0; } else { struct fb_var_screeninfo vinfo; ioctl (fbfd, FBIOGET_VSCREENINFO, &vinfo); close (fbfd); width = vinfo.xres; height = vinfo.yres; } r.x = r.y = 0; r.w = width; r.h = height; return rs;}static SDL_Surface *iPod_SetVideoMode (_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags){ Uint32 Rmask, Gmask, Bmask; if (bpp > 8) { Rmask = 0xF800; Gmask = 0x07E0; Bmask = 0x001F; } else { Rmask = Gmask = Bmask = 0; } if (this->hidden->buffer) SDL_free (this->hidden->buffer); this->hidden->buffer = SDL_malloc (width * height * (bpp / 8)); if (!this->hidden->buffer) { SDL_SetError ("Couldn't allocate buffer for requested mode"); return 0; } memset (this->hidden->buffer, 0, width * height * (bpp / 8)); if (!SDL_ReallocFormat (current, bpp, Rmask, Gmask, Bmask, 0)) { SDL_SetError ("Couldn't allocate new pixel format"); SDL_free (this->hidden->buffer); this->hidden->buffer = 0; return 0; } if (bpp <= 8) { int i, j; for (i = 0; i < 256; i += 4) { for (j = 0; j < 4; j++) { current->format->palette->colors[i+j].r = 85 * j; current->format->palette->colors[i+j].g = 85 * j;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -