1 /* Hello, Emacs, this is -*-C-*-
2 * $Id: linux.trm,v 1.29 2006/07/21 02:35:47 sfeam Exp $
6 /* GNUPLOT - linux.trm */
9 * Copyright 1993, 1998, 2004
11 * Permission to use, copy, and distribute this software and its
12 * documentation for any purpose with or without fee is hereby granted,
13 * provided that the above copyright notice appear in all copies and
14 * that both that copyright notice and this permission notice appear
15 * in supporting documentation.
17 * Permission to modify the software is granted, but not the right to
18 * distribute the complete modified source code. Modifications are to
19 * be distributed as patches to the released version. Permission to
20 * distribute binaries produced by compiling modified sources is granted,
22 * 1. distribute the corresponding source modifications from the
23 * released version in the form of a patch file along with the binaries,
24 * 2. add special version identification to distinguish your version
25 * in addition to the base release version number,
26 * 3. provide your name and address as the primary contact for the
27 * support of your modified version, and
28 * 4. retain our contact information in regard to use of the base
30 * Permission to distribute the released version of the source code along
31 * with corresponding source modifications in the form of a patch file is
32 * granted with same provisions 2 through 4 for binary distributions.
34 * This software is provided "as is" without express or implied warranty
35 * to the extent permitted by applicable law.
39 * This file is included by ../term.c.
41 * This terminal driver supports:
42 * SVGA up to 1024x768x256 for PC's running the Linux Operating System
43 * (also VGA 640x480x16, and SVGA 800x600x256)
46 * Scott Heavner (sdh@po.cwru.edu)
47 * based on original linux.trm by Tommy Frandsen (frandsen@diku.dk)
48 * patched by David J. Liu (liu@molecule.phri.nyu.edu)
49 * to increase perfomance and safety based on the features of SVGALib/GL.
50 * send your comments or suggestions to (pixar!info-gnuplot@sun.com).
54 * Compile with Linux SVGAlib 0.95 currently maintained by
55 * Harm Hanemaayer (hhanemaa@cs.ruu.nl).
56 * supports Trident, Tseng, Cirrus, Oak and generic vga.
67 #define LINUX_VCHAR FNT5X9_VCHAR
68 #define LINUX_HCHAR FNT5X9_HCHAR
71 #define LINUX_XMAX 0 /* These two entries are just place holders. */
72 #define LINUX_YMAX 0 /* The actual values will be filled in init. */
74 TERM_PUBLIC void LINUX_options __PROTO((void));
75 TERM_PUBLIC void LINUX_init __PROTO((void));
76 TERM_PUBLIC void LINUX_reset __PROTO((void));
77 TERM_PUBLIC void LINUX_text __PROTO((void));
78 TERM_PUBLIC void LINUX_graphics __PROTO((void));
79 TERM_PUBLIC void LINUX_linetype __PROTO((int linetype));
80 TERM_PUBLIC void LINUX_move __PROTO((unsigned int x, unsigned int y));
81 TERM_PUBLIC void LINUX_vector __PROTO((unsigned int x, unsigned int y));
82 TERM_PUBLIC int LINUX_text_angle __PROTO((int ang));
83 TERM_PUBLIC void LINUX_put_text __PROTO((unsigned int x, unsigned int y, const char *str));
84 TERM_PUBLIC void LINUX_suspend __PROTO((void));
85 TERM_PUBLIC void LINUX_resume __PROTO((void));
87 /* also used in vgagl.trm, so export it: */
88 TERM_PUBLIC int LINUX_graphics_allowed;
97 static int linux_vmode = G1024x768x256; /* default mode */
98 static int vgacolor[] = { 7, 8, 2, 3, 4, 5, 9, 14, 12, 15, 13, 10, 11, 1, 6 };
99 static int graphics_on = FALSE;
100 static vga_modeinfo *modeinfo;
101 static int linux_startx, linux_starty, linux_lasty;
102 static int linux_angle;
103 TERM_PUBLIC int LINUX_graphics_allowed = FALSE; /* also used in vgagl.trm */
104 extern void drop_privilege __PROTO((void));
105 extern void take_privilege __PROTO((void));
107 typedef int (*linux_line_func_ptr) __PROTO((int x1, int y1, int x2, int y2));
109 static void LINUX_putc __PROTO((unsigned int x, unsigned int y, int c, int ang,
110 linux_line_func_ptr line_func));
112 /* this function is called at the very beginning of main() to initialize
113 * the vgalib and to revoke suid privileges.
114 * /dev/console and /dev/tty\d and /dev/vc/\d are considered graphic terminals, all other
115 * don't support the linux terminal */
123 LINUX_graphics_allowed = FALSE;
126 return; /* if we aren't root, we cannot init graphics */
128 if ((pipe = popen("/usr/bin/tty", "r")) != NULL) {
130 fgets(line, 256, pipe);
132 line[strlen(line) - 1] = '\0';
134 strcmp(line, "/dev/console") == 0 ||
136 ( ( strncmp(line, "/dev/tty", 8) == 0 || strncmp(line, "/dev/vc/", 8) == 0 )
137 && isdigit((unsigned char) line[8]))
140 LINUX_graphics_allowed = TRUE;
142 /* check for socket name as set for example by `screen' */
143 char* sty = getenv("STY");
146 if (3 == sscanf(sty, "%d.tty%d.%s", &n1, &n2, line)) {
147 /* we could check here, if host is the
148 * same as gethostname() returns. */
149 LINUX_graphics_allowed = TRUE;
154 if (LINUX_graphics_allowed) {
159 /* err - shouldn't we give up root uid whatever happens ?
160 * or perhaps vga_init() does it ?
169 if (!LINUX_graphics_allowed) {
170 int_error(NO_CARET, "Linux terminal driver not available");
172 fprintf(stderr, "%s\n", vga_getmodename(linux_vmode));
178 /* vga_init () has been moved to immediately after main () for security */
179 if (vga_getdefaultmode() != -1)
180 linux_vmode = vga_getdefaultmode();
181 /* get the default mode from GSVGAMODE, if available */
182 if (!vga_hasmode(linux_vmode))
183 linux_vmode = G640x480x16;
184 /* test default mode first */
185 if (!vga_hasmode(linux_vmode)) {
186 fputs("Error, unable to initiate graphics.\n", stderr);
188 } /* this mode is the bottom line */
189 modeinfo = vga_getmodeinfo(linux_vmode);
190 term->xmax = modeinfo->width;
191 term->ymax = modeinfo->height;
192 linux_lasty = modeinfo->height - 1;
218 vga_setmode(linux_vmode);
236 LINUX_linetype(int linetype)
242 vga_setcolor(vgacolor[linetype + 2]);
246 LINUX_move(unsigned int x, unsigned int y)
253 LINUX_vector(unsigned int x, unsigned int y)
255 vga_drawline(linux_startx, linux_lasty - linux_starty, x, linux_lasty - y);
261 LINUX_text_angle(int ang)
263 linux_angle = (ang ? 1 : 0);
269 unsigned int x, unsigned int y,
272 linux_line_func_ptr line_func)
275 unsigned int pixelon;
278 for (j = 0; j < FNT5X9_VBITS; j++) {
279 for (k = 0; k < FNT5X9_HBITS; k++) {
280 pixelon = (((unsigned int) (fnt5x9[i][j])) >> k & 1);
284 (*line_func) (x + k + 1, y - j, x + k + 1, y - j);
287 (*line_func) (x - j, y - k - 1, x - j, y - k - 1);
296 LINUX_put_text(unsigned int x, unsigned int y, const char *str)
299 switch (linux_angle) {
301 y -= LINUX_VCHAR / 2;
304 x += LINUX_VCHAR / 2;
307 for (i = 0; str[i]; i++) {
308 LINUX_putc(x, linux_lasty - y, str[i], linux_angle, vga_drawline);
309 switch (linux_angle) {
323 TERM_TABLE_START(linux_driver)
324 "linux", "Linux PC with (s)vgalib",
325 LINUX_XMAX, LINUX_YMAX, LINUX_VCHAR, LINUX_HCHAR,
326 LINUX_VTIC, LINUX_HTIC, LINUX_options, LINUX_init, LINUX_reset,
327 LINUX_text, null_scale, LINUX_graphics, LINUX_move, LINUX_vector,
328 LINUX_linetype, LINUX_put_text, LINUX_text_angle,
329 null_justify_text, do_point, do_arrow, set_font_null,
331 TERM_CAN_MULTIPLOT, LINUX_suspend, LINUX_resume
332 TERM_TABLE_END(linux_driver)
334 #define LAST_TERM linux_driver
340 "?commands set terminal linux",
341 "?set terminal linux",
346 " The `linux` driver has no additional options to specify. It looks at the",
347 " environment variable GSVGAMODE for the default mode; if not set, it uses",
348 " 1024x768x256 as default mode or, if that is not possible, 640x480x16",