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