r109: reorganized header files
[ctsim.git] / libctgraphics / driver_x11.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: driver_x11.cpp,v 1.2 2000/06/19 19:04:05 kevin Exp $
6 **  $Log: driver_x11.cpp,v $
7 **  Revision 1.2  2000/06/19 19:04:05  kevin
8 **  reorganized header files
9 **
10 **  Revision 1.1  2000/06/19 18:05:03  kevin
11 **  initial cvs import
12 **
13 **  Revision 1.1  2000/05/08 20:08:15  kevin
14 **  *** empty log message ***
15 **
16 **
17 **  This program is free software; you can redistribute it and/or modify
18 **  it under the terms of the GNU General Public License (version 2) as
19 **  published by the Free Software Foundation.
20 **
21 **  This program is distributed in the hope that it will be useful,
22 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
23 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 **  GNU General Public License for more details.
25 **
26 **  You should have received a copy of the GNU General Public License
27 **  along with this program; if not, write to the Free Software
28 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29 ******************************************************************************/
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <math.h>
34 #include "ctsupport.h"
35 #include "sgp.h"
36
37 #include <X11/Xlib.h>
38 #include <X11/Xutil.h>
39
40 struct sgp_x11_driver {
41   Display *theDisplay;
42   int   theScreen;
43   int   theDepth;
44 };
45
46 typedef struct sgp_x11_driver SGP_X11_DRIVER;
47
48 SGP_X11_DRIVER sgp_x11;
49
50 sgpdrv_X11_init()
51 {
52   int i;
53
54   sgp_x11.theDisplay = NULL;
55   for (i = 0; i < 2 && theDisplay == NULL; i++)
56     sgp_x11.theDisplay = XOpenDisplay( NULL  );
57
58   if ( sgp_x11.theDisplay == NULL ) {
59     fprintf( stderr, "ERROR: Cannot establish a connection to the X Server %s\n", XDisplayName( NULL ));
60     exit( 1 );
61   }
62
63   sgp_x11.theScreen = DefaultScreen( sgp_x11.theDisplay );
64   sgp_x11.theDepth = DefaultDepth( sgp_x11.theDisplay, sgp_x11.theScreen );
65 }
66
67 sgpdrv_X11_info()
68 {
69   printf("%s version %d of the X Window System, X%d R%d\n", 
70          ServerVendor( sgp_x11.theDisplay ),
71          VendorRelease( sgp_x11.theDisplay ),
72          ProtocolVersion( sgp_x11.theDisplay ),
73          ProtocolRevision( sgp_x11.theDisplay ) );
74
75   if ( sgp_x11.theDepth == 1) 
76     printf( "Color plane depth....................%d (monochrome)\n", sgp_x11.theDepth );
77   else
78     printf( "Color plane depth....................%d\n", sgp_x11.theDepth);
79   
80   printf( "Display Width........................%d\n",
81           DisplayWidth( sgp_x11.theDisplay, sgp_x11.theScreen ));
82   printf( "Display Height.......................%d\n",
83           DisplayHeight( theDisplay, theScreen ));
84   printf( "The display %s\n", XDisplayName( sgp_x11.theDisplay ));
85 }
86
87
88
89 setlinestyle (LS_SOLID);
90 setcolor (1);
91 settext (XCHARSIZ, YCHARSIZ, 0.0, 0);
92 setbackg (0);
93 settextclr (1, 0);
94 gp_set_aspect (CRTDEV, CRTASPT);
95 gp_set_aspect (PRTDEV, 1.0);
96 state.xndc = 10.;               /* to guarntee a move */
97 stylus (0.0,0.0,0);             /* move to starting corner */
98
99
100 int 
101 initdevice (int dev, int mode, int xsize, int ysize)
102 {
103   //         crt.dotfunc = crt_wdot;
104   //        crt.linefunc = crt_line;
105   crt.xmin = 0;
106   crt.ymin = 0;
107   crt.xmax  = 319;
108   crt.xsize = 320;
109 }
110             
111
112 /* NAME
113  *   gp_closedev                Close device for output
114  *
115  * SYNOPSIS
116  *   gp_closedev (dev)
117  *   int dev                    Device handle
118  *
119  * DESCRIPTION
120  *    Temporarily suspends output from going to a device
121  */
122
123 void sgpdrv_x11_term()
124 {
125 }
126
127 void sgpdrv_x11_line()
128 {
129 }
130
131 void sgpdrive_x11_point()
132 {
133 }
134
135 void 
136 sgpdrv_x11_linestyle (int style)
137 {
138
139   state.linestyle = style;
140   
141         //      crt_line_style (crt.style);
142 }
143
144 void
145 sgpdrv_x11_linewidth (int wid)
146 {
147         state.linewidth = wid;
148 }
149
150 void
151 sgpdrv_x11_textparam (
152     double width,
153     double height,                      /* size of character in NDC */
154     double textangle,                   /* text angle */
155     int font                            /* text font */
156 )
157 {
158         double temp;
159
160         CLIP(height, 0.0, 1.0);
161         CLIP(width, 0.0, 1.0);
162 /*      textangle = textangle - (2 * PI * (int) (textangle / (2 * PI)));
163 */
164         cspec.width = width;
165         cspec.height = height;
166         cspec.textangle = textangle;
167         cspec.font = font;
168
169         if (textangle > - HALFPI / 2 && textangle < HALFPI / 2) {
170             cspec.updir = YPLUS;
171             cspec.textdir = XPLUS;
172         } else if (textangle > HALFPI / 2 && textangle < 3 * HALFPI / 2) {
173             cspec.updir = XMINUS;
174             cspec.textdir = YPLUS;
175         } else if (textangle > 3 * HALFPI / 2 && textangle < 5 * HALFPI / 2) {
176             cspec.updir = YMINUS;
177             cspec.textdir = XMINUS;
178         } else if (textangle > 5 * HALFPI / 2 && textangle < 7 * HALFPI / 2) {
179             cspec.updir = XPLUS;
180             cspec.textdir = YMINUS;
181         } else {
182             cspec.updir = YPLUS;
183             cspec.textdir = XPLUS;
184         }
185
186         if (cspec.updir == XMINUS || cspec.updir == XPLUS) {
187             temp = height;
188             height = width;
189             width = temp;
190         }
191
192         crt.icwidth = width * crt.xsize + 0.5;
193         crt.icheight = height * crt.ysize + 0.5;
194         crt.icwidth = iclip (crt.icwidth, 8, crt.xsize);
195         crt.icheight = iclip (crt.icheight, 8, crt.ysize);
196
197         prt.icwidth = width * prt.xsize + 0.5;
198         prt.icheight = height * prt.ysize + 0.5;
199         prt.icwidth = iclip (prt.icwidth, 8, prt.xsize);
200         prt.icheight = iclip (prt.icheight, 8, prt.ysize);
201 }
202
203 void
204 sgpdrv_textcolor (int fore, int back)
205 {
206         cspec.fore = fore;
207         cspec.back = back;
208         crt.cfore = fore;
209         crt.cback = back;
210         prt.cfore = iclip (fore, 0, prt.colormax);
211         prt.cback = back;
212 }
213
214 void
215 sgpdrv_color (int fore)
216 {
217         int back;
218
219         state.foregnd = fore;
220         back = state.backgnd;
221         crtcolor (crt.mode, &fore, &back);              /* set colors on crt */
222 }
223
224 void
225 sgpdrv_backgroundg (int back)
226 {
227         int fore;
228
229         state.backgnd = back;
230         fore = state.foregnd;
231         crtcolor (crt.mode, &fore, &back);
232 }
233
234 /*
235  * INITMARKER (<SYMBOL>)
236  *
237  * Sets the current marker symbol (cross, square, diamond..)
238  */
239 void sgpdrv_marker (int marker, int color)
240 {
241         if (marker > NMARKERS || marker < 0)
242             return(-1);
243         else {
244             state.marktype = marker;
245             state.markcolor = color;
246         }
247         return(0);
248 } /* end initmarker */
249
250 /*
251  * SETCHARDIR ( <DIRECTION> )
252  *
253  * indicates in which direction a string should be printed, e.g.:
254  *  direction = YPLUS  -> print on the horizontal from left to right
255  *  direction = YMINUS -> print upside-down characters from right to left
256  *  direction = XMINUS -> print on the vertical from down to up
257  *  direction = XPLUS  -> print on the vertical from up to down
258  */
259 void sgpdrv_x11_textdir (
260     int direction        /* direction flag */
261 )
262 {
263   if (direction != XPLUS &&
264       direction != XMINUS &&
265       direction != YPLUS &&
266       direction != YMINUS ) {
267     printf("Error in character direction: %d\n", direction);
268     return(-1);
269   }
270   cspec.textdir = direction;
271   charsize (cspec.width, cspec.height);
272 }
273
274 int 
275 void sgpdrv_x11_charsize (double wid, double height)
276 {
277         settext (wid, height, cspec.textangle, cspec.font);
278 }
279
280 void
281 sgpdrv_x11_textangle (double angle)
282 {
283   settext (cspec.width, cspec.height, angle, cspec.font);
284 }
285
286