Externals - libsdl compiling, may not work
[tpg/acess2.git] / Externals / sdl12 / patches / src / video / acess / ptyvideo.c
1 /*
2  * Acess2 SDL Video Driver
3  * - By John Hodge (thePowersGang)
4  * 
5  * video/acess/ptyvideo.c
6  * - PTY-based video driver
7  */
8 #include "SDL_config.h"
9 #include <acess/sys.h>
10 #include <acess/devices/pty.h>
11 #include "SDL_video.h"
12 #include "../SDL_sysvideo.h"
13
14 #define _THIS SDL_VideoDevice *this
15 #define HIDDEN  (this->hidden)
16
17 // === TYPES ===
18 struct SDL_PrivateVideoData
19 {
20          int    OutFD;
21         SDL_Rect        MaxRect;
22         SDL_Rect        *FullscreenModes[2];
23         Uint32  *Palette;       // 256 colours
24 };
25
26 // === PROTOTYPES ===
27 static int AcessPTY_Avaliable(void);
28 static SDL_VideoDevice *AcessPTY_Create(int devidx);
29 static void AcessPTY_Destroy(SDL_VideoDevice *device);
30 // - Core functions
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);
40
41 // === GLOBALS ===
42 VideoBootStrap  AcessPTY_bootstrap = {
43         "acesspty", "Acess2 PTY Video",
44         AcessPTY_Avaliable, AcessPTY_Create
45 };
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
55 };
56
57 int AcessPTY_Avaliable(void)
58 {
59         // - test on stdin so stdout can be redirected without breaking stuff
60         if( _SysIOCtl(0, DRV_IOCTL_TYPE, NULL) != DRV_TYPE_TERMINAL )
61                 return 0;
62         // TODO: Check if the terminal supports graphics modes
63         return 1;
64 }
65
66 SDL_VideoDevice *AcessPTY_Create(int devidx)
67 {
68         SDL_VideoDevice *ret;
69
70         // Allocate and pre-populate
71         ret = malloc( sizeof(SDL_VideoDevice) );
72         if( !ret ) {
73                 SDL_OutOfMemory();
74                 return NULL;
75         }
76         memcpy(ret, &AcessPTY_TemplateDevice, sizeof(SDL_VideoDevice));
77         ret->hidden = malloc( sizeof(struct SDL_PrivateVideoData) );
78         if( !ret->hidden ) {
79                 SDL_OutOfMemory();
80                 free(ret);
81                 return NULL;
82         }
83         memset(ret->hidden, 0, sizeof(*ret->hidden));
84
85         ret->hidden->OutFD = _SysCopyFD(0, -1);
86         if( ret->hidden->OutFD == -1 ) {
87                 free(ret->hidden);
88                 free(ret);
89                 return NULL;
90         }
91
92         // Redirect stdout/stderr if they point to the terminal
93         if( 1 ) { //_SysIsSameNode(0, 1) ) {
94                 _SysReopen(1, "stdout.txt", OPENFLAG_WRITE);
95         }
96         if( 1 ) { //_SysIsSameNode(0, 2) ) {
97                 _SysReopen(2, "stderr.txt", OPENFLAG_WRITE);
98         }
99
100         return ret;
101 }
102
103 void AcessPTY_Destroy(SDL_VideoDevice *device)
104 {
105         _SysClose(device->hidden->OutFD);
106         free(device->hidden);
107         free(device);
108 }
109
110 // ---
111 // General functions
112 // ---
113 int AcessPTY_VideoInit(_THIS, SDL_PixelFormat *vformat)
114 {
115         // Set PTY to graphics mode
116         struct ptymode mode = {.OutputMode = PTYBUFFMT_FB, .InputMode = PTYIMODE_RAW};
117         _SysIOCtl(HIDDEN->OutFD, PTY_IOCTL_SETMODE, &mode);
118
119         // Get current dimensions (and use these as the max)
120         // - Not strictly true when using a GUI Terminal, but does apply for VTerms
121         struct ptydims  dims;
122         _SysIOCtl(HIDDEN->OutFD, PTY_IOCTL_GETDIMS, &dims);
123         
124         HIDDEN->MaxRect.x = 0;
125         HIDDEN->MaxRect.y = 0;
126         HIDDEN->MaxRect.w = dims.PW;
127         HIDDEN->MaxRect.h = dims.PH;
128         
129         HIDDEN->FullscreenModes[0] = &HIDDEN->MaxRect;
130         HIDDEN->FullscreenModes[1] = NULL;
131         
132         return 0;
133 }
134
135 SDL_Rect **AcessPTY_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
136 {
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;
141 //      else
142 //              return NULL;
143 }
144
145 SDL_Surface *AcessPTY_SetVideoMode(_THIS, SDL_Surface *Current, int W, int H, int BPP, Uint32 Flags)
146 {
147         // Sanity first
148         if( W <= 0 || W > HIDDEN->MaxRect.w )
149                 return NULL;
150         if( H <= 0 || H > HIDDEN->MaxRect.h )
151                 return NULL;
152
153         struct ptydims dims = {.PW = W, .PH = H};
154         _SysIOCtl(HIDDEN->OutFD, PTY_IOCTL_SETDIMS, &dims);
155
156         Current->pixels = SDL_malloc(W * H * 4);
157         if( !Current->pixels ) {
158                 
159         }
160         
161         return Current;
162 }
163
164 int AcessPTY_SetColors(_THIS, int FirstColour, int NColours, SDL_Color *Colours)
165 {
166         return 1;
167 }
168
169 void AcessPTY_VideoQuit(_THIS)
170 {
171 }
172
173 void AcessPTY_UpdateRects(_THIS, int NumRects, SDL_Rect *Rects)
174 {
175          int    w = this->screen->w;
176          int    h = this->screen->h;
177         _SysWrite(HIDDEN->OutFD, w*h*4, this->screen->pixels);
178 }
179

UCC git Repository :: git.ucc.asn.au