2 * Acess2 SDL Video Driver
3 * - By John Hodge (thePowersGang)
5 * video/acess/ptyvideo.c
6 * - PTY-based video driver
8 #include "SDL_config.h"
10 #include <acess/devices/pty.h>
11 #include "SDL_video.h"
12 #include "../SDL_sysvideo.h"
14 #define _THIS SDL_VideoDevice *this
15 #define HIDDEN (this->hidden)
18 struct SDL_PrivateVideoData
22 SDL_Rect *FullscreenModes[2];
23 Uint32 *Palette; // 256 colours
27 static int AcessPTY_Avaliable(void);
28 static SDL_VideoDevice *AcessPTY_Create(int devidx);
29 static void AcessPTY_Destroy(SDL_VideoDevice *device);
31 static int AcessPTY_VideoInit(_THIS, SDL_PixelFormat *vformat);
32 static SDL_Rect **AcessPTY_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
33 static SDL_Surface *AcessPTY_SetVideoMode(_THIS, SDL_Surface *Current, int W, int H, int BPP, Uint32 Flags);
34 static int AcessPTY_SetColors(_THIS, int FirstColour, int NColours, SDL_Color *Colours);
35 static void AcessPTY_VideoQuit(_THIS);
36 static void AcessPTY_UpdateRects(_THIS, int NumRects, SDL_Rect *Rects);
37 // - Hardware Surfaces (TODO)
38 //static int AcessPTY_AllocHWSurface(_THIS, SDL_Surface *Surface);
39 //static int AcessPTY_FillHWRect(_THIS, SDL_Surface *Dst, SDL_Rect *Rect, Uint32 Color);
42 VideoBootStrap AcessPTY_bootstrap = {
43 "acesspty", "Acess2 PTY Video",
44 AcessPTY_Avaliable, AcessPTY_Create
46 SDL_VideoDevice AcessPTY_TemplateDevice = {
47 .VideoInit = AcessPTY_VideoInit,
48 .ListModes = AcessPTY_ListModes,
49 .SetVideoMode = AcessPTY_SetVideoMode,
50 .SetColors = AcessPTY_SetColors,
51 .VideoQuit = AcessPTY_VideoQuit,
52 .UpdateRects = AcessPTY_UpdateRects,
53 //.FillHWRect = AcessPTY_FillHWRect,
54 .free = AcessPTY_Destroy
57 int AcessPTY_Avaliable(void)
59 // - test on stdin so stdout can be redirected without breaking stuff
60 if( _SysIOCtl(0, DRV_IOCTL_TYPE, NULL) != DRV_TYPE_TERMINAL )
62 // TODO: Check if the terminal supports graphics modes
66 SDL_VideoDevice *AcessPTY_Create(int devidx)
70 // Allocate and pre-populate
71 ret = malloc( sizeof(SDL_VideoDevice) );
76 memcpy(ret, &AcessPTY_TemplateDevice, sizeof(SDL_VideoDevice));
77 ret->hidden = malloc( sizeof(struct SDL_PrivateVideoData) );
83 memset(ret->hidden, 0, sizeof(*ret->hidden));
85 ret->hidden->OutFD = _SysCopyFD(0, -1);
86 if( ret->hidden->OutFD == -1 ) {
92 // Redirect stdout/stderr if they point to the terminal
93 if( 1 ) { //_SysIsSameNode(0, 1) ) {
94 _SysReopen(1, "stdout.txt", OPENFLAG_WRITE);
96 if( 1 ) { //_SysIsSameNode(0, 2) ) {
97 _SysReopen(2, "stderr.txt", OPENFLAG_WRITE);
103 void AcessPTY_Destroy(SDL_VideoDevice *device)
105 _SysClose(device->hidden->OutFD);
106 free(device->hidden);
113 int AcessPTY_VideoInit(_THIS, SDL_PixelFormat *vformat)
115 // Set PTY to graphics mode
116 struct ptymode mode = {.OutputMode = PTYBUFFMT_FB, .InputMode = PTYIMODE_RAW};
117 _SysIOCtl(HIDDEN->OutFD, PTY_IOCTL_SETMODE, &mode);
119 // Get current dimensions (and use these as the max)
120 // - Not strictly true when using a GUI Terminal, but does apply for VTerms
122 _SysIOCtl(HIDDEN->OutFD, PTY_IOCTL_GETDIMS, &dims);
124 HIDDEN->MaxRect.x = 0;
125 HIDDEN->MaxRect.y = 0;
126 HIDDEN->MaxRect.w = dims.PW;
127 HIDDEN->MaxRect.h = dims.PH;
129 HIDDEN->FullscreenModes[0] = &HIDDEN->MaxRect;
130 HIDDEN->FullscreenModes[1] = NULL;
135 SDL_Rect **AcessPTY_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
137 if( flags & SDL_FULLSCREEN)
138 return HIDDEN->FullscreenModes; // Should be only one entry
139 // else if( SDLToDFBPixelFormat (format) != DSPF_UNKNOWN )
140 return (SDL_Rect**) -1;
145 SDL_Surface *AcessPTY_SetVideoMode(_THIS, SDL_Surface *Current, int W, int H, int BPP, Uint32 Flags)
148 if( W <= 0 || W > HIDDEN->MaxRect.w )
150 if( H <= 0 || H > HIDDEN->MaxRect.h )
153 struct ptydims dims = {.PW = W, .PH = H};
154 _SysIOCtl(HIDDEN->OutFD, PTY_IOCTL_SETDIMS, &dims);
156 Current->pixels = SDL_malloc(W * H * 4);
157 if( !Current->pixels ) {
164 int AcessPTY_SetColors(_THIS, int FirstColour, int NColours, SDL_Color *Colours)
169 void AcessPTY_VideoQuit(_THIS)
173 void AcessPTY_UpdateRects(_THIS, int NumRects, SDL_Rect *Rects)
175 int w = this->screen->w;
176 int h = this->screen->h;
177 _SysWrite(HIDDEN->OutFD, w*h*4, this->screen->pixels);