File: svga.c

package info (click to toggle)
svncviewer 1:0.1.1-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 336 kB
  • ctags: 304
  • sloc: ansic: 2,608; makefile: 60; sh: 29
file content (270 lines) | stat: -rw-r--r-- 6,084 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 *  Copyright (C) 1997, 1998 Olivetti & Oracle Research Laboratory
 *
 *  This is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This software 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this software; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 *  USA.
 */

/*
 * svga.c - the svgalib interface
 * Basically hacked from x.c by {ganesh,sitaram}@cse.iitb.ernet.in
 */

#include <vncviewer.h>

#include <vga.h>
#include <vgagl.h>
#include <vgakeyboard.h>
#include <vgamouse.h>
#include <fcntl.h>
#include <unistd.h>

#define INVALID_PIXEL 0xffffffff
#define COLORMAP_SIZE 256

unsigned long BGR233ToPixel[COLORMAP_SIZE];

static char *screenData = NULL;

static int Mode = 0;

static void CopyBGR233ToScreen(CARD8 *buf, int x, int y, int width,int height);
static void FindBestMode (void);
static int colorbits (int);
static int hasmode (int, int, int);

/*
 * InitSvga
 */
Bool InitSvga (void)
{
	vga_setmousesupport (1);
	FindBestMode ();

#define NO_ACCEL 1
#ifdef NO_ACCEL
	/* This workaround is to prevent gl from using accelerated functions.
	 * At least on my CLGD 5430/40, svgalib acceleration is buggy.
	 */
	vga_setmode (G320x200x256);
	gl_setcontextvga (Mode);
	vga_setmode (Mode);
#else
	vga_setmode (10);
	gl_setcontextvga (Mode);
#endif
	if (useBGR233) {
		int r, g, b, c;
		for (r = 0; r < 8; r++)
			for (g = 0; g < 8; g++)
				for (b = 0; b < 4; b++) {
					c = (b << 6) | (g << 3) | r;
					gl_setpalettecolor (c, r * 63 / 7, g * 63 / 7, b * 63 / 3);
					BGR233ToPixel[c] = c;
				}
	}

	if (!svncKeyboardInit()) return False;

	fprintf (stderr, "SVGA: using mode %d\n", Mode);

	return True;
}	

/*
 * ShutdownSvga.
 */
void
ShutdownSvga()
{
	keyboard_close ();
	vga_setmode (TEXT);
	mouse_close ();
}

void HandleMouseEvent (void)
{
	int x, y, button;
	
	x = mouse_getx ();
	y = mouse_gety ();
	button = mouse_getbutton ();
	button = ((button & 1) << 2) | ((button >> 2) & 1) | (button & 0xfa);
	SendPointerEvent (x, y, button);
}


static void FindBestMode (void)
{
	int i;
	int mode = -1;
	vga_modeinfo *mi;
	int width, height, depth;

	width = si.framebufferWidth;
	height = si.framebufferHeight;
	depth = si.format.depth;

	if (forceOwnCmap || useBGR233) {
		if ((mode = hasmode (width, height, 8)) < 0)
			goto nomode;
	} else if (forceTruecolour) {
		i = (requestedDepth) ? requestedDepth : depth;
		i = (i == 8) ? 16 : i;
		if ((mode = hasmode (width, height, i)) < 0)
			if ((mode = hasmode (width, height, 16)) < 0)
				goto nomode;
	} else {
		if ((mode = hasmode (width, height, depth)) < 0)
			if ((mode = hasmode (width, height, 16)) < 0)
				if ((mode = hasmode (width, height, 8)) < 0)
					goto nomode;
	}

	Mode = mode;
	mi = vga_getmodeinfo (mode);

	myFormat.bitsPerPixel = mi->bytesperpixel * 8;
	myFormat.depth = colorbits(mi->colors);
	myFormat.bigEndian = 0;
	myFormat.trueColour = (myFormat.depth == 8 && !useBGR233) ? 0 : 1;
	if (myFormat.trueColour) {
		if (myFormat.depth == 8) {
			myFormat.redMax = myFormat.greenMax = 7;
			myFormat.blueMax = 3;
			myFormat.redShift = 0;
			myFormat.greenShift = 3;
			myFormat.blueShift = 6;
		}
		else if (myFormat.depth == 15) {
			myFormat.redMax = myFormat.greenMax = myFormat.blueMax = 31;
			myFormat.redShift = 10;
			myFormat.greenShift = 5;
			myFormat.blueShift = 0;
		}
		else if (myFormat.depth == 16) {
			myFormat.redMax = myFormat.blueMax = 31;
			myFormat.greenMax = 63;
			myFormat.redShift = 11;
			myFormat.greenShift = 5;
			myFormat.blueShift = 0;
		}
		else if (myFormat.depth == 24) {
			myFormat.redMax = myFormat.greenMax = myFormat.blueMax = 255;
			myFormat.redShift = 16;
			myFormat.greenShift = 8;
			myFormat.blueShift = 0;
		}
	}
	return;

nomode:
	fprintf (stderr, "No suitable mode available\n");
	ShutdownSvga ();
	exit (2);
}

/*
 * CopyDataToScreen.
 */

void
CopyDataToScreen(CARD8 *buf, int x, int y, int width, int height)
{
    if (delay != 0) {
#if 0 /* what was this supposed to do ? */
	XFillRectangle(dpy, canvas, DefaultGC(dpy,DefaultScreen(dpy)),
		       x, y, width, height);

	XSync(dpy,False);
#endif
	usleep(delay * 1000);
    }

	gl_putbox (x, y, width, height, buf);
}


/*
 * CopyBGR233ToScreen.
 */

static void
CopyBGR233ToScreen(CARD8 *buf, int x, int y, int width, int height)
{
    int p, q;
    CARD8 *scr8 = ((CARD8 *)screenData) + y * si.framebufferWidth + x;
    CARD16 *scr16 = ((CARD16 *)screenData) + y * si.framebufferWidth + x;
    CARD32 *scr32 = ((CARD32 *)screenData) + y * si.framebufferWidth + x;

    switch (myFormat.depth) {

    case 8:
	for (q = 0; q < height; q++) {
	    for (p = 0; p < width; p++) {
		*(scr8++) = BGR233ToPixel[*(buf++)];
	    }
	    scr8 += si.framebufferWidth - width;
	}
	break;

    case 16:
	for (q = 0; q < height; q++) {
	    for (p = 0; p < width; p++) {
		*(scr16++) = BGR233ToPixel[*(buf++)];
	    }
	    scr16 += si.framebufferWidth - width;
	}
	break;

    case 24:
	for (q = 0; q < height; q++) {
	    for (p = 0; p < width; p++) {
		*(scr32++) = BGR233ToPixel[*(buf++)];
	    }
	    scr32 += si.framebufferWidth - width;
	}
	break;
    }
}

static int 
colorbits (int cols)
{
	switch (cols) {
		case 256: return 8;break;
		case 32768: return 15;break;
		case 65536: return 16;break;
		case 65536*256: return 24;break;
		default: return -1; break;
	}
}

static int
hasmode (int width, int height, int depth)
{
	int i;
	vga_modeinfo *mi;

	for (i = 0; i < 256; i++) {
		if (!vga_hasmode (i))
			continue;
		mi = vga_getmodeinfo (i);
		if (mi->width >= width && mi->height >= height &&
			mi->colors == (1 << depth))
			return i;
	}
	return -1;
}