1 | #ifndef _WIN32 |
---|
2 | |
---|
3 | /* X include files */ |
---|
4 | #define _HPUX_SOURCE |
---|
5 | #include <unistd.h> /* hbu for sleep */ |
---|
6 | #include <X11/Xlib.h> |
---|
7 | #include <X11/Xutil.h> |
---|
8 | |
---|
9 | #include <stdio.h> |
---|
10 | #include <stdlib.h> |
---|
11 | #include <string.h> |
---|
12 | #include <time.h> |
---|
13 | |
---|
14 | #include "mad_extrn_f.h" |
---|
15 | #include "mad_gxx11c.h" |
---|
16 | #include "mad_mem.h" |
---|
17 | |
---|
18 | // #define MAXCOL 256 // not used |
---|
19 | |
---|
20 | /* declarations */ |
---|
21 | static char logo[] = "GXplot"; |
---|
22 | static Display* mydisplay; |
---|
23 | static Window mywindow; |
---|
24 | static GC mygc[4]; /* one gc per linestyle */ |
---|
25 | static XEvent myevent; |
---|
26 | // static KeySym mykey; // not used |
---|
27 | static XSizeHints myhint; |
---|
28 | static XWMHints mywmhint; |
---|
29 | static XSetWindowAttributes xswa; |
---|
30 | static int i, done, myscreen, style; // , ncolors // not used |
---|
31 | static unsigned int wwidth, wwidth_mm, wheight, wheight_mm; |
---|
32 | static float xwidth, xwidth_mm, xheight, xheight_mm; |
---|
33 | static float w_lx, w_ly, wx_fact, wy_fact; |
---|
34 | static float v_corrf; |
---|
35 | static unsigned long myforeground, mybackground, valuemask; |
---|
36 | static Colormap cmap; |
---|
37 | static int colored; |
---|
38 | static XColor color, colore; //, colors[MAXCOL]; |
---|
39 | static XWindowAttributes theAtt; |
---|
40 | static XGCValues gcv; |
---|
41 | |
---|
42 | static char solid[1] = {0}, |
---|
43 | dashed[2] = {3,3}, |
---|
44 | dotted[2] = {1,3}, |
---|
45 | dot_dashed[4] = {1,3,3,3}; |
---|
46 | static char *dash_list[] = {solid, dashed, dotted, dot_dashed}; |
---|
47 | static int dash_list_length[] = {0, 2, 2, 4}; |
---|
48 | |
---|
49 | void |
---|
50 | wopen(int *uswid, int *ushi) |
---|
51 | { |
---|
52 | int argc; |
---|
53 | char **argv; |
---|
54 | |
---|
55 | argc = 0; |
---|
56 | argv = NULL; |
---|
57 | |
---|
58 | /* initialization */ |
---|
59 | mydisplay = XOpenDisplay((char *) getenv("DISPLAY")); |
---|
60 | if (!mydisplay) |
---|
61 | { |
---|
62 | fprintf(stderr, "cannot open display\n"); exit(1); |
---|
63 | } |
---|
64 | myscreen = DefaultScreen(mydisplay); |
---|
65 | |
---|
66 | /* default pixel values */ |
---|
67 | |
---|
68 | mybackground = WhitePixel(mydisplay, myscreen); |
---|
69 | myforeground = BlackPixel(mydisplay, myscreen); |
---|
70 | |
---|
71 | /* default program-specified window position and size */ |
---|
72 | |
---|
73 | myhint.x = 100; myhint.y = 200; |
---|
74 | myhint.width = *uswid; myhint.height = *ushi; |
---|
75 | myhint.flags = PPosition | PSize; |
---|
76 | mywmhint.flags = InputHint; |
---|
77 | mywmhint.input = True; |
---|
78 | |
---|
79 | /* window creation */ |
---|
80 | |
---|
81 | mywindow = XCreateSimpleWindow(mydisplay, DefaultRootWindow(mydisplay), |
---|
82 | myhint.x, myhint.y, myhint.width, myhint.height, 5, |
---|
83 | myforeground, mybackground); |
---|
84 | XSetStandardProperties(mydisplay, mywindow, logo, logo, |
---|
85 | None, argv, argc, &myhint); |
---|
86 | XSetWMHints(mydisplay, mywindow, &mywmhint); |
---|
87 | xswa.backing_store = Always; |
---|
88 | valuemask = CWBackingStore; |
---|
89 | XChangeWindowAttributes(mydisplay, mywindow, valuemask, &xswa); |
---|
90 | |
---|
91 | /* GC creation and initialization */ |
---|
92 | |
---|
93 | mygc[0] = XCreateGC(mydisplay, mywindow, 0, 0); |
---|
94 | XSetBackground(mydisplay, mygc[0], mybackground); |
---|
95 | XSetForeground(mydisplay, mygc[0], myforeground); |
---|
96 | for (i = 1;i < 4 ;i++ ) |
---|
97 | { |
---|
98 | gcv.line_style = LineOnOffDash; |
---|
99 | mygc[i] = XCreateGC(mydisplay, mywindow, GCLineStyle, &gcv); |
---|
100 | XSetDashes(mydisplay, mygc[i], 0, dash_list[i], |
---|
101 | dash_list_length[i]); |
---|
102 | XSetBackground(mydisplay, mygc[i], mybackground); |
---|
103 | XSetForeground(mydisplay, mygc[i], myforeground); |
---|
104 | XSetFillStyle (mydisplay, mygc[i], FillSolid); |
---|
105 | XSetFillRule (mydisplay, mygc[i], WindingRule); |
---|
106 | } |
---|
107 | XGetWindowAttributes(mydisplay, mywindow, &theAtt); |
---|
108 | // ncolors = theAtt.visual->map_entries; // not used |
---|
109 | cmap = theAtt.colormap; |
---|
110 | colored = DisplayPlanes( mydisplay, myscreen ) > 1; |
---|
111 | if(colored) cmap = DefaultColormap( mydisplay, myscreen); |
---|
112 | |
---|
113 | /* input event selection */ |
---|
114 | |
---|
115 | XSelectInput(mydisplay, mywindow, |
---|
116 | ButtonPressMask | KeyPressMask | ExposureMask); |
---|
117 | |
---|
118 | /* window mapping */ |
---|
119 | |
---|
120 | XMapRaised(mydisplay, mywindow); |
---|
121 | XFlush(mydisplay); |
---|
122 | wwidth = DisplayWidth(mydisplay, myscreen); |
---|
123 | wwidth_mm = DisplayWidthMM(mydisplay, myscreen); |
---|
124 | wheight = DisplayHeight(mydisplay, myscreen); |
---|
125 | wheight_mm = DisplayHeightMM(mydisplay, myscreen); |
---|
126 | if (wwidth != 0 && wwidth_mm != 0 && |
---|
127 | wheight != 0 && wheight_mm != 0) |
---|
128 | { |
---|
129 | xwidth = wwidth; |
---|
130 | xwidth_mm = wwidth_mm; |
---|
131 | xheight = wheight; |
---|
132 | xheight_mm = wheight_mm; |
---|
133 | v_corrf = (xheight * xwidth_mm) |
---|
134 | / (xwidth * xheight_mm); |
---|
135 | if ((v_corrf - 1.) * (v_corrf - 1.) <= 0.002) |
---|
136 | { |
---|
137 | v_corrf = 1.; |
---|
138 | } |
---|
139 | |
---|
140 | } |
---|
141 | sleep(3); |
---|
142 | } |
---|
143 | |
---|
144 | void |
---|
145 | wclose(void) |
---|
146 | { |
---|
147 | |
---|
148 | /* termination */ |
---|
149 | |
---|
150 | for (i = 0;i < 4 ;i++ ) |
---|
151 | { |
---|
152 | XFreeGC(mydisplay, mygc[i]); |
---|
153 | } |
---|
154 | XDestroyWindow(mydisplay, mywindow); |
---|
155 | XCloseDisplay(mydisplay); |
---|
156 | |
---|
157 | } |
---|
158 | |
---|
159 | void |
---|
160 | wclrwk(int *i1,int *i2) |
---|
161 | { |
---|
162 | (void)i1, (void)i2; |
---|
163 | /* clear workstation */ |
---|
164 | XClearWindow(mydisplay, mywindow); |
---|
165 | |
---|
166 | } |
---|
167 | |
---|
168 | void |
---|
169 | wpl(int *np, float *xp, float *yp) |
---|
170 | { |
---|
171 | /* plot PolyLine of np points with coordinates (xp,yp) */ |
---|
172 | char rout_name[] = "wpl"; |
---|
173 | |
---|
174 | XPoint *points; |
---|
175 | int i, k; |
---|
176 | |
---|
177 | /* polyline drawing */ |
---|
178 | points = (XPoint *) mycalloc(rout_name,*np, sizeof(XPoint)); |
---|
179 | for (i = 0;i < *np ;i++ ) |
---|
180 | { |
---|
181 | points[i].x = (xp[i] - w_lx) * wx_fact + 0.5; |
---|
182 | k = (yp[i] - w_ly) * wy_fact + 0.5; |
---|
183 | points[i].y = myhint.height - k; |
---|
184 | } |
---|
185 | XDrawLines(mydisplay, mywindow, mygc[style], points, *np, 0); |
---|
186 | myfree(rout_name, points); |
---|
187 | |
---|
188 | } |
---|
189 | |
---|
190 | void |
---|
191 | wfa(int *np, float *xp, float *yp) |
---|
192 | { |
---|
193 | /* plot area-filled PolyLine of np points with coordinates (xp,yp) */ |
---|
194 | char rout_name[] = "wfa"; |
---|
195 | |
---|
196 | XPoint *points; |
---|
197 | int i, k; |
---|
198 | |
---|
199 | /* polyline drawing */ |
---|
200 | points = (XPoint *) mycalloc(rout_name,*np, sizeof(XPoint)); |
---|
201 | for (i = 0;i < *np ;i++ ) |
---|
202 | { |
---|
203 | points[i].x = (xp[i] - w_lx) * wx_fact + 0.5; |
---|
204 | k = (yp[i] - w_ly) * wy_fact + 0.5; |
---|
205 | points[i].y = myhint.height - k; |
---|
206 | } |
---|
207 | XFillPolygon(mydisplay, mywindow, mygc[style], points, *np, |
---|
208 | Nonconvex, 0); |
---|
209 | myfree(rout_name, points); |
---|
210 | |
---|
211 | } |
---|
212 | |
---|
213 | void |
---|
214 | wswn(float *wlx, float *wxfact, |
---|
215 | float *wly, float *wyfact) |
---|
216 | { |
---|
217 | /* store window lower world coord.s, and conversion factors */ |
---|
218 | w_lx = *wlx; wx_fact = *wxfact; |
---|
219 | w_ly = *wly; wy_fact = *wyfact; |
---|
220 | |
---|
221 | } |
---|
222 | |
---|
223 | void |
---|
224 | wtx(float *xp,float *yp, char *string) |
---|
225 | { |
---|
226 | int k, x, y; |
---|
227 | x = (*xp - w_lx) * wx_fact + 0.5; |
---|
228 | k = (*yp - w_ly) * wy_fact + 0.5; |
---|
229 | y = myhint.height - k; |
---|
230 | XDrawString(mydisplay, mywindow, mygc[style], |
---|
231 | x, y, string, strlen(string)); |
---|
232 | } |
---|
233 | |
---|
234 | void |
---|
235 | wwait(void) |
---|
236 | { |
---|
237 | |
---|
238 | /* main event reading loop */ |
---|
239 | done = 0; |
---|
240 | while (done == 0) |
---|
241 | { |
---|
242 | /* read the next event */ |
---|
243 | XNextEvent(mydisplay, &myevent); |
---|
244 | |
---|
245 | switch (myevent.type) |
---|
246 | { |
---|
247 | |
---|
248 | /* process keyboard input */ |
---|
249 | case KeyPress: |
---|
250 | done = 1; |
---|
251 | break; |
---|
252 | } |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | void |
---|
257 | wsetci(char *uscol) |
---|
258 | { |
---|
259 | if(colored) |
---|
260 | { |
---|
261 | if (XAllocNamedColor(mydisplay, cmap, uscol, &color, &colore)) |
---|
262 | { |
---|
263 | for( i = 0; i < 4; i++ ) |
---|
264 | { |
---|
265 | XSetForeground(mydisplay, mygc[i], color.pixel); |
---|
266 | } |
---|
267 | } |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | void |
---|
272 | wsetls(int *ls) |
---|
273 | { |
---|
274 | style = *ls; |
---|
275 | } |
---|
276 | /* |
---|
277 | * return null terminated and blank trimmed string |
---|
278 | */ |
---|
279 | |
---|
280 | void |
---|
281 | wstring( char *s, int *l ) |
---|
282 | { |
---|
283 | int i, loc; |
---|
284 | |
---|
285 | loc = *l; |
---|
286 | for( i = 0; i < *l; i++ ) |
---|
287 | while( loc > 0 && s[loc-1] == ' ' ) |
---|
288 | loc--; |
---|
289 | |
---|
290 | s[loc] = '\0'; |
---|
291 | } |
---|
292 | |
---|
293 | #else // case _WIN32 |
---|
294 | |
---|
295 | #include <time.h> |
---|
296 | #include "mad_extrn_f.h" |
---|
297 | #include "mad_gxx11c.h" |
---|
298 | |
---|
299 | #endif |
---|
300 | |
---|
301 | void |
---|
302 | cbyt(int* source, int* s_pos, int* target, int* t_pos, int* n) |
---|
303 | /* inserts n_bit byte from source at position s_pos in target at t_pos. |
---|
304 | Attention: least significant bit is #1, positions are those of the least |
---|
305 | significant bit in byte */ |
---|
306 | { |
---|
307 | int mask = 1; |
---|
308 | int so = *source, tg = *target; |
---|
309 | so >>= (*s_pos - 1); |
---|
310 | mask <<= *n; |
---|
311 | mask -= 1; |
---|
312 | so &= mask; |
---|
313 | mask <<= (*t_pos - 1); so <<= (*t_pos - 1); |
---|
314 | tg &= ~mask; tg |= so; |
---|
315 | *target = tg; |
---|
316 | } |
---|
317 | |
---|
318 | void |
---|
319 | mydtime(int* year, int* month, int* day, int* hour, int* minute, int* sec) |
---|
320 | { |
---|
321 | time_t _time; |
---|
322 | struct tm* tm; |
---|
323 | time(&_time); /* initialize timing */ |
---|
324 | tm = localtime(&_time); /* split system time */ |
---|
325 | *year = tm->tm_year%100; |
---|
326 | *month = tm->tm_mon+1; |
---|
327 | *day = tm->tm_mday; |
---|
328 | *hour = tm->tm_hour; |
---|
329 | *minute = tm->tm_min; |
---|
330 | *sec = tm->tm_sec; |
---|
331 | } |
---|
332 | |
---|