summaryrefslogtreecommitdiff
path: root/devices/gdevvglb.c
blob: 931fafd51f21991c4f9a2fbeb14ccf3dba88225c (plain)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* Copyright (C) 2001-2012 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
   CA  94903, U.S.A., +1(415)492-9861, for further information.
*/


/*
 * This is a driver for 386 PCs using vgalib for graphics on the console
 * display.  Note that this driver only works with 16-color modes.
 *
 * Written by Sigfrid Lundberg, siglun@euler.teorekol.lu.se.
 * Modified by Erik Talvola, talvola@gnu.ai.mit.edu
 * Updated 9/28/96 by L. Peter Deutsch, ghost@aladdin.com: allow setting
 *   the display mode as a device parameter.
 * Updated 2/13/97 by ghost@aladdin.com: make the device identify itself
 *   as a page device.
 * Updated 5/2/97 by ghost@aladdin.com: copy_mono computed some parameters
 *   before doing fit_copy.
 * Update 1997-06-28 by ghost@aladdin.com: get_bits wasn't implemented.
 */

#include "gx.h"
#include "gserrors.h"
#include "gsparam.h"
#include "gxdevice.h"
#include "gdevpccm.h"

#include <errno.h>
#include <vga.h>

typedef struct gx_device_vgalib {
    gx_device_common;
    int display_mode;
} gx_device_vgalib;

#define vga_dev ((gx_device_vgalib *)dev)

#define XDPI   60		/* to get a more-or-less square aspect ratio */
#define YDPI   60

#ifndef A4			/*Letter size */
#define YSIZE (20.0 * YDPI / 2.5)
#define XSIZE (8.5 / 11)*YSIZE	/* 8.5 x 11 inch page, by default */
#else /* A4 paper */
#define XSIZE 8.27
#define YSIZE 11.69
#endif

static dev_proc_open_device(vgalib_open);
static dev_proc_close_device(vgalib_close);
static dev_proc_map_rgb_color(vgalib_map_rgb_color);
static dev_proc_map_color_rgb(vgalib_map_color_rgb);
static dev_proc_fill_rectangle(vgalib_fill_rectangle);
static dev_proc_tile_rectangle(vgalib_tile_rectangle);
static dev_proc_copy_mono(vgalib_copy_mono);
static dev_proc_copy_color(vgalib_copy_color);
static dev_proc_get_bits(vgalib_get_bits);
static dev_proc_get_params(vgalib_get_params);
static dev_proc_put_params(vgalib_put_params);

const gx_device_vgalib gs_vgalib_device =
{
    std_device_std_body(gx_device_vgalib, 0, "vgalib",
                        0, 0, 1, 1),
    {vgalib_open,
     NULL,			/* get_initial_matrix */
     NULL,			/* sync_output */
     NULL,			/* output_page */
     vgalib_close,
     vgalib_map_rgb_color,
     vgalib_map_color_rgb,
     vgalib_fill_rectangle,
     vgalib_tile_rectangle,
     vgalib_copy_mono,
     vgalib_copy_color,
     NULL,			/* draw_line (obsolete) */
     vgalib_get_bits,
     vgalib_get_params,
     vgalib_put_params,
     NULL,			/* map_cmyk_color */
     NULL,			/* get_xfont_procs */
     NULL,			/* get_xfont_device */
     NULL,			/* map_rgb_alpha_color */
     gx_page_device_get_page_device
    },
    -1				/* display_mode */
};

static int
vgalib_open(gx_device * dev)
{
    int VGAMODE = vga_dev->display_mode;
    int width = dev->width, height = dev->height;

    if (VGAMODE == -1)
        VGAMODE = vga_getdefaultmode();
    if (VGAMODE == -1)
        vga_setmode(G640x480x16);
    else
        vga_setmode(VGAMODE);
    vga_clear();
    if (width == 0)
        width = vga_getxdim() + 1;
    if (height == 0)
        height = vga_getydim() + 1;

    /*vgalib provides no facilities for finding out aspect ratios */
    if (dev->y_pixels_per_inch == 1) {
        dev->y_pixels_per_inch = height / 11.0;
        dev->x_pixels_per_inch = dev->y_pixels_per_inch;
    }
    gx_device_set_width_height(dev, width, height);

    /* Find out if the device supports color */
    /* (default initialization is monochrome). */
    /* We only recognize 16-color devices right now. */
    if (vga_getcolors() > 1) {
        int index;

        static const gx_device_color_info vgalib_16color = dci_pc_4bit;

        dev->color_info = vgalib_16color;

        for (index = 0; index < 16; ++index) {
            gx_color_value rgb[3];

            (*dev_proc(dev, map_color_rgb)) (dev, (gx_color_index) index, rgb);
#define cv2pv(cv) ((cv) >> (gx_color_value_bits - 8))
            vga_setpalette(index, cv2pv(rgb[0]), cv2pv(rgb[1]), cv2pv(rgb[2]));
#undef cv2pv
        }
    }
    return 0;
}

static int
vgalib_close(gx_device * dev)
{
    vga_setmode(TEXT);
    return 0;
}

static gx_color_index
vgalib_map_rgb_color(gx_device * dev, const gx_color_value cv[])
{
    return pc_4bit_map_rgb_color(dev, cv);
}

static int
vgalib_map_color_rgb(gx_device * dev, gx_color_index index,
                     unsigned short rgb[3])
{
    return pc_4bit_map_color_rgb(dev, index, rgb);
}

static int
vgalib_tile_rectangle(gx_device * dev, const gx_tile_bitmap * tile,
                      int x, int y, int w, int h, gx_color_index czero,
                      gx_color_index cone, int px, int py)
{
    if (czero != gx_no_color_index && cone != gx_no_color_index) {
        vgalib_fill_rectangle(dev, x, y, w, h, czero);
        czero = gx_no_color_index;
    }
    return gx_default_tile_rectangle(dev, tile, x, y, w, h, czero, cone, px,
                                     py);
}

static int
vgalib_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
                      gx_color_index color)
{
    int i, j;

    fit_fill(dev, x, y, w, h);
    vga_setcolor((int)color);
    if ((w | h) > 3) {		/* Draw larger rectangles as lines. */
        if (w > h)
            for (i = y; i < y + h; ++i)
                vga_drawline(x, i, x + w - 1, i);
        else
            for (j = x; j < x + w; ++j)
                vga_drawline(j, y, j, y + h - 1);
    } else {			/* Draw small rectangles point-by-point. */
        for (i = y; i < y + h; i++)
            for (j = x; j < x + w; j++)
                vga_drawpixel(j, i);
    }
    return 0;
}

static int
vgalib_copy_mono(gx_device * dev, const byte * base, int sourcex,
                 int raster, gx_bitmap_id id, int x, int y, int width,
                 int height, gx_color_index zero, gx_color_index one)
{
    const byte *ptr_line;
    int left_bit, dest_y, end_x;
    int invert = 0;
    int color;

    fit_copy(dev, base, sourcex, raster, id, x, y, width, height);
    ptr_line = base + (sourcex >> 3);
    left_bit = 0x80 >> (sourcex & 7);
    dest_y = y, end_x = x + width;

    if (zero == gx_no_color_index) {
        if (one == gx_no_color_index)
            return 0;
        color = (int)one;
    } else {
        if (one == gx_no_color_index) {
            color = (int)zero;
            invert = -1;
        } else {		/* Pre-clear the rectangle to zero */
            vgalib_fill_rectangle(dev, x, y, width, height, zero);
            color = (int)one;
        }
    }

    vga_setcolor(color);
    while (height--) {		/* for each line */
        const byte *ptr_source = ptr_line;
        register int dest_x = x;
        register int bit = left_bit;

        while (dest_x < end_x) {	/* for each bit in the line */
            if ((*ptr_source ^ invert) & bit)
                vga_drawpixel(dest_x, dest_y);
            dest_x++;
            if ((bit >>= 1) == 0)
                bit = 0x80, ptr_source++;
        }

        dest_y++;
        ptr_line += raster;
    }
    return 0;
}

/* Copy a color pixel map.  This is just like a bitmap, except that */
/* each pixel takes 4 bits instead of 1 when device driver has color. */
static int
vgalib_copy_color(gx_device * dev, const byte * base, int sourcex,
                  int raster, gx_bitmap_id id, int x, int y,
                  int width, int height)
{

    fit_copy(dev, base, sourcex, raster, id, x, y, width, height);

    if (gx_device_has_color(dev)) {	/* color device, four bits per pixel */
        const byte *line = base + (sourcex >> 1);
        int dest_y = y, end_x = x + width;

        if (width <= 0)
            return 0;
        while (height--) {	/* for each line */
            const byte *source = line;
            register int dest_x = x;

            if (sourcex & 1) {	/* odd nibble first */
                int color = *source++ & 0xf;

                vga_setcolor(color);
                vga_drawpixel(dest_x, dest_y);
                dest_x++;
            }
            /* Now do full bytes */
            while (dest_x < end_x) {
                int color = *source >> 4;

                vga_setcolor(color);
                vga_drawpixel(dest_x, dest_y);
                dest_x++;

                if (dest_x < end_x) {
                    color = *source++ & 0xf;
                    vga_setcolor(color);
                    vga_drawpixel(dest_x, dest_y);
                    dest_x++;
                }
            }

            dest_y++;
            line += raster;
        }
    } else {			/* monochrome device: one bit per pixel */
        /* bitmap is the same as bgi_copy_mono: one bit per pixel */
        vgalib_copy_mono(dev, base, sourcex, raster, id, x, y, width, height,
                         (gx_color_index) 0, (gx_color_index) 7);
    }

    return 0;
}

/* Read bits back from the device. */
static int
vgalib_get_bits(gx_device * dev, int y, byte * data, byte ** actual_data)
{
    int x;
    byte *dest = data;
    int b = 0;
    int depth = dev->color_info.depth;	/* 1 or 4 */
    int mask = (1 << depth) - 1;
    int left = 8;

    if (actual_data)
        *actual_data = data;
    for (x = 0; x < dev->width; ++x) {
        int color = vga_getpixel(x, y);

        if ((left -= depth) < 0)
            *dest++ = b, b = 0, left += 8;
        b += (color & mask) << left;
    }
    if (left < 8)
        *dest = b;
    return 0;
}

/* Get/put the display mode parameter. */
static int
vgalib_get_params(gx_device * dev, gs_param_list * plist)
{
    int code = gx_default_get_params(dev, plist);

    if (code < 0)
        return code;
    return param_write_int(plist, "DisplayMode", &vga_dev->display_mode);
}
static int
vgalib_put_params(gx_device * dev, gs_param_list * plist)
{
    int ecode = 0;
    int code;
    int imode = vga_dev->display_mode;
    const char *param_name;

    switch (code = param_read_int(plist, (param_name = "DisplayMode"), &imode)) {
        default:
            ecode = code;
            param_signal_error(plist, param_name, ecode);
        case 0:
        case 1:
            break;
    }

    if (ecode < 0)
        return ecode;
    code = gx_default_put_params(dev, plist);
    if (code < 0)
        return code;

    if (imode != vga_dev->display_mode) {
        if (dev->is_open)
            gs_closedevice(dev);
        vga_dev->display_mode = imode;
    }
    return 0;
}

#ifdef GS_DEVS_SHARED
extern void gs_lib_register_device(const gx_device *dev);
void
gs_shared_init(void)
{
  gs_lib_register_device(&gs_vgalib_device);
}
#endif