Jspice3
mfb.c
Go to the documentation of this file.
1 /***************************************************************************
2 JSPICE3 adaptation of Spice3e2 - Copyright (c) Stephen R. Whiteley 1992
3 Copyright 1990 Regents of the University of California. All rights reserved.
4 Authors: 1988 Jeffrey M. Hsu
5  1992 Stephen R. Whiteley
6 ****************************************************************************/
7 
8 /*
9  * MFB driver
10  */
11 
12 #include "spice.h"
13 
14 #ifdef HAVE_MFB
15 
16 #include "mfb.h"
17 #include "plotdefs.h"
18 
19 extern char ErrorMessage[];
20 
21 static int mfb_wincount;
22 
23 char *MFBRCD;
24 
25 
26 #ifdef __STDC__
27 static void mfbsetcolors(void);
28 static void mfbsetlinestyles(void);
29 #else
30 static void mfbsetcolors();
31 static void mfbsetlinestyles();
32 #endif
33 
34 
35 int
36 Mfb_Init()
37 {
38  char buf[BSIZE_SP], dev[BSIZE_SP], *term;
39  static char old_term[BSIZE_SP] = "";
40  static char old_dev[BSIZE_SP] = "";
41  static char success = false;
42  int err;
43  extern char *kw_term, *kw_device;
44 
45  /* The mfb.rc file location defaults to the Spice Library */
47 
48  /* Try to identify the terminal type -- check variable,
49  * environment, then ask the user.
50  */
51  if (cp_getvar(kw_term, VT_STRING, buf)) {
52  term = buf;
53  }
54  else {
55  term = getenv("TERM");
56  if (term == NULL) {
57  /* prompt user w/ input() */
58  fprintf(cp_out, "Terminal type = ");
59  (void) fflush(cp_out);
60  (void) fscanf(cp_in, " %s", buf);
61  term = copy(buf);
62  }
63  }
64  if (!cp_getvar(kw_device, VT_STRING, dev))
65  dev[0] = '\0';
66 
67 /* We only re-open if things have changed */
68  if( (!success)
69  || (strcmp(old_term, term) != 0) || (strcmp(old_dev,dev) != 0)) {
70  (void) MFBOpen( term, (dev[0] ? dev : (char *) NULL), &err);
71  strcpy( old_term, term); strcpy( old_dev, dev );
72  if (err == MFBOK) {
73  success = true;
74  }
75  else {
76  success = false;
77  sprintf(ErrorMessage, "MFB error: %s", MFBError(err));
79  return (1);
80  }
81  }
82 
85  dispdev->width = MFBInfo(MAXX) + 1;
86  dispdev->height = MFBInfo(MAXY) + 1;
87 
88 #ifdef DEFCOLORS
89  if (dispdev->numcolors > 2 && MFBInfo(DEFCOLORS))
90  mfbsetcolors();
91 #endif
93  mfbsetlinestyles();
94 
95  (void)MFBSetTextMode(1); /* text overstrikes */
96 
97  /* get control back */
98  if (MFBHalt() != MFBOK) {
99  externalerror("Can't restore control of tty.");
100  return (1);
101  }
102  return (0);
103 }
104 
105 
106 int
107 Mfb_NewViewport(graph)
108 
109 GRAPH *graph;
110 {
111  if (MFBInitialize() != MFBOK) {
112  externalerror("Can't initialize MFB.");
113  return (1);
114  }
115 
116  graph->fontwidth = MFBInfo(FONTWIDTH);
117  graph->fontheight = MFBInfo(FONTHEIGHT);
118 
119  graph->viewport.height = dispdev->height;
120  graph->viewport.width = dispdev->width;
121 
122  graph->absolute.xpos = 0;
123  graph->absolute.ypos = 0;
124  graph->absolute.width = dispdev->width;
125  graph->absolute.height = dispdev->height;
126 
127  mfb_wincount++;
128  return (0);
129 }
130 
131 
132 int
133 Mfb_Close()
134 {
135  (void)MFBClose();
136  mfb_wincount = 0;
137  return (0);
138 }
139 
140 
141 int
142 Mfb_Halt()
143 {
144  if (!mfb_wincount)
145  return (0);
146  mfb_wincount--;
147  if (!mfb_wincount)
148  (void)MFBHalt();
149  return (0);
150 }
151 
152 
153 int
154 Mfb_Clear()
155 {
156  (void)MFBSetColor(0);
157  (void)MFBFlood();
158  return (0);
159 }
160 
161 
162 Mfb_Pixel(x, y)
163 
164 int x, y;
165 {
166  (void)MFBPixel(x, y);
167  return (0);
168 }
169 
170 
171 Mfb_Line(x1, y1, x2, y2)
172 
173 int x1, y1, x2, y2;
174 {
175  (void)MFBLine(x1, y1, x2, y2);
176  return (0);
177 }
178 
179 
180 Mfb_Box(x1, y1, x2, y2)
181 
182 int x1, y1, x2, y2;
183 {
184  (void)MFBBox(x1, y1, x2, y2);
185  return (0);
186 }
187 
188 
189 Mfb_Arc(x0, y0, radius, theta1, theta2)
190 int x0, y0, radius;
191 double theta1, theta2;
192 {
193  double theta3, theta4;
194 
195 /* note: figure some better value for nsides as a fn of radius
196  and delta theta */
197  if( (theta1 != theta2)
198  && sin(fabs( theta1 - theta2 )/2.0) < 0.5 ) {
199  theta3 = ceil((double) 180.0/M_PI * theta1);
200  theta4 = floor((double) 180.0/M_PI * theta2);
201 
202  if ( theta3 < theta4 && theta3 < 360.0 ) {
203  MFBArc(x0, y0, radius, (int)theta3, (int)theta4, 50);
204 
205  theta3 *= M_PI/180.0;
206  theta4 *= M_PI/180.0;
207  }
208  else {
209  theta3 = theta2;
210  theta4 = theta1;
211  }
212 
213  (void)MFBLine((int)(x0+radius*cos(theta1)),(int)(y0+radius*sin(theta1)),
214  (int)(x0+radius*cos(theta3)),(int)(y0+radius*sin(theta3)));
215  (void)MFBLine((int)(x0+radius*cos(theta2)),(int)(y0+radius*sin(theta2)),
216  (int)(x0+radius*cos(theta4)),(int)(y0+radius*sin(theta4)));
217  }
218  else {
219  (void)MFBArc(x0, y0, radius,
220  rnd((double) 180.0/M_PI * theta1),
221  rnd((double) 180.0/M_PI * theta2), 50);
222  }
223  return (0);
224 }
225 
226 
227 int
228 Mfb_Polygon(p)
229 
230 POLYGON *p;
231 {
232  (void)MFBPolygon(p);
233  return (0);
234 }
235 
236 
237 int
238 Mfb_Text(text, x, y)
239 
240 char *text;
241 int x, y;
242 {
243  (void)MFBText(text, x, y, 0);
244  return (0);
245 }
246 
247 
248 int
249 Mfb_SetGhost(callback,x,y)
250 
251 #ifdef __STDC__
252 void (*callback)(int,int,int,int);
253 #else
254 void (*callback)();
255 #endif
256 int x,y;
257 {
258  (void)MFBSetGhost(callback,x,y);
259  return (0);
260 }
261 
262 
263 int
264 Mfb_DefineColor(colorid, red, green, blue)
265 
266 int colorid;
267 double red, green, blue;
268 {
269  (void)MFBDefineColor(colorid,
270  rnd(red*1000),rnd(green*1000),rnd(blue*1000));
271  return (0);
272 }
273 
274 
275 int
276 Mfb_DefineLinestyle(linestyleid, mask)
277 
278 int linestyleid;
279 int mask;
280 {
281  (void)MFBDefineLineStyle(linestyleid, mask);
282  return (0);
283 }
284 
285 
286 int
287 Mfb_SetLinestyle(linestyle)
288 
289 int linestyle;
290 {
291  if (MFBSetLineStyle(linestyle) != MFBOK) {
292  externalerror("Bad linestyle");
293  }
294  return (0);
295 }
296 
297 
298 int
299 Mfb_SetColor(colorid)
300 
301 int colorid;
302 {
303  if (MFBSetColor(colorid) != MFBOK) {
304  externalerror("bad color");
305  }
306  return (0);
307 }
308 
309 
310 int
311 Mfb_Update()
312 {
313  MFBUpdate();
314  return (0);
315 }
316 
317 
318 int
319 Mfb_MakeMenu()
320 {
321  return (0);
322 }
323 
324 
325 int
326 Mfb_MakeDialog()
327 {
328  return (0);
329 }
330 
331 
332 int
333 Mfb_Input(request, response)
336 {
337  int button = 0,key = 0;
338  int x, y;
339 
340  switch (request->option) {
341  case char_option:
342  if (request->fp)
343  response->reply.ch = cp_inchar(request->fp);
344  else
345  response->reply.ch = MFBGetchar();
346  response->option = request->option;
347  break;
348  case button_option:
349  while (button == 0)
350  MFBPoint(&x,&y,&key,&button);
351  if (response) {
352  response->x = x;
353  response->y = y;
354  response->reply.button = button;
355  response->option = request->option;
356  }
357  break;
358  case point_option:
359  while (button == 0 && key == 0)
360  MFBPoint(&x,&y,&key,&button);
361  if (response) {
362  response->x = x;
363  response->y = y;
364  if (button) {
365  response->option = button_option;
366  response->reply.button = button;
367  }
368  else {
369  response->option = char_option;
370  response->reply.ch = key;
371  }
372  }
373  break;
374 
375  case click_option:
376  case checkup_option:
377  break;
378 
379  default:
380  /* just ignore, since we don't want a million error messages */
381  if (response)
382  response->option = error_option;
383  break;
384  }
385  return (0);
386 }
387 
388 /* Set colors properly. Assume that we won't need more than 20 colors.
389  * Also we should try to set the foreground and background colors.
390  * These color values are just off of the top of my head...
391  * Make the grid color (color 1) blue.
392  */
393 
394 #ifdef DEFCOLORS
395 
396 static int reds[] = { 999, 0, 999, 0, 0, 999, 999, 0, 999, 500,
397  500, 999, 999, 500, 999, 999, 600, 300, 600, 300 } ;
398 static int grns[] = { 999, 0, 0, 999, 0, 999, 0, 999, 500, 999,
399  500, 999, 500, 999, 600, 300, 999, 999, 300, 600 } ;
400 static int blus[] = { 999, 0, 0, 0, 999, 0, 999, 999, 500, 500,
401  999, 500, 999, 999, 300, 600, 300, 600, 999, 999 } ;
402 
403 static void
404 mfbsetcolors()
405 {
406  int i;
407  int red, green, blue;
408 
409  MFBDefineColor(1, 0, 0, 999);
410  for (i = 2; (i < dispdev->numcolors) && (i < 22); i++) {
411  red = reds[i - 2];
412  green = grns[i - 2];
413  blue = blus[i - 2];
414  MFBDefineColor(i, red, green, blue);
415  }
416  return;
417 }
418 
419 #endif
420 
421 
422 /* Set line styles if necessary. Assume we won't use more than 20 line
423  * styles. These line styles also are pretty random. Make sure that
424  * line style 0 is solid and line style 1 is dotted.
425  */
426 
427 static int styles[] = { 0333, 0347, 0360, 0071, 0216, 0366, 0155, 0011,
428  0123, 0317, 0330, 0145, 0275, 0016, 0315, 0101,
429  0222, 0174, 0251, 0376 } ;
430 
431 static void
432 mfbsetlinestyles()
433 {
434  int i, j;
435 
436  MFBDefineLineStyle(0, 0377); /* Solid. */
437  MFBDefineLineStyle(1, 0125); /* Dotted. */
438  for (i = 2; (i < dispdev->numlinestyles) && (i < 22); i++) {
439  j = styles[i - 2];
440  MFBDefineLineStyle(i, j);
441  }
442  return;
443 }
444 
445 #endif
446 
char * kw_device
Definition: options.c:351
static char buf[MAXPROMPT]
Definition: arg.c:18
int cp_inchar()
#define BSIZE_SP
Definition: misc.h:19
#define MAXLINESTYLES
Definition: mfb.h:526
bool cp_getvar(char *n, int t, char *r)
Definition: help.c:184
int MFBClose()
Definition: mfbopen.c:193
static char * linestyle[]
Definition: plot5.c:17
#define MAXCOLORS
Definition: mfb.h:523
#define MAXY
Definition: mfb.h:522
char * strcpy()
int MFBInitialize()
Definition: mfbopen.c:168
#define FONTWIDTH
Definition: mfb.h:546
int numlinestyles
Definition: plotdev.h:67
#define MFBOK
Definition: mfb.h:491
DISPDEVICE * dispdev
Definition: display.c:112
int MFBSetTextMode()
#define M_PI
Definition: spice.h:132
int MFBDefineColor()
int MFBInfo()
Definition: cddefs.h:215
int ch
Definition: ftegraph.h:196
char * MFBRCD
Definition: plotdev.h:14
char * copy()
char * getenv(char *c)
Definition: libfuncs.c:106
union response::@11 reply
char * Spice_Lib_Dir
int MFBSetGhost(void(*callback)(), int x, int y)
Definition: mfbopen.c:458
MFB * MFBOpen()
char * MFBError()
#define DEFLINEPATTERN
Definition: mfb.h:556
int MFBSetLineStyle()
void MFBText()
#define NULL
Definition: spdefs.h:121
int MFBPoint()
void MFBLine()
int MFBGetchar()
Definition: mfbopen.c:259
FILE * cp_out
Definition: help.c:101
double cos()
Definition: ftegraph.h:29
int MFBSetColor()
#define VT_STRING
Definition: cpstd.h:63
double sin()
void MFBPixel()
char * kw_term
Definition: options.c:423
void MFBFlood()
Definition: mfbflood.c:21
int button
Definition: ftegraph.h:198
void MFBArc()
int height
Definition: plotdev.h:66
int y
Definition: ftegraph.h:194
int numcolors
Definition: plotdev.h:67
int x
Definition: ftegraph.h:193
OPTION option
Definition: ftegraph.h:192
int MFBDefineLineStyle()
#define MAXX
Definition: mfb.h:521
int MFBUpdate()
Definition: mfbopen.c:252
int width
Definition: plotdev.h:66
Definition: mfb.h:383
int MFBHalt()
Definition: mfbopen.c:232
#define FONTHEIGHT
Definition: mfb.h:545
void MFBPolygon()
FILE * cp_in
Definition: help.c:101
void MFBBox()
char ErrorMessage[]
Definition: error.c:20
externalerror(char *message)
Definition: error.c:100
#define rnd(x)
Definition: ftegraph.h:203