pcb 4.1.1
An interactive printed circuit board layout editor.

hidnogui.c

Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include "config.h"
00003 #endif
00004 
00005 #include <stdio.h>
00006 #include <stdarg.h>
00007 #include <stdlib.h>
00008 #include <string.h>
00009 
00010 #include "global.h"
00011 #include "hid.h"
00012 #include "hid_draw.h"
00013 
00014 #ifdef HAVE_LIBDMALLOC
00015 #include <dmalloc.h>
00016 #endif
00017 
00018 /* This is the "gui" that is installed at startup, and is used when
00019    there is no other real GUI to use.  For the most part, it just
00020    stops the application from (1) crashing randomly, and (2) doing
00021    gui-specific things when it shouldn't.  */
00022 
00023 #define CRASH fprintf(stderr, "HID error: pcb called GUI function %s without having a GUI available.\n", __FUNCTION__); abort()
00024 
00025 typedef struct hid_gc_struct
00026 {
00027   int nothing_interesting_here;
00028 } hid_gc_struct;
00029 
00030 static HID_Attribute *
00031 nogui_get_export_options (int *n_ret)
00032 {
00033   CRASH;
00034   return 0;
00035 }
00036 
00037 static void
00038 nogui_do_export (HID_Attr_Val * options)
00039 {
00040   CRASH;
00041 }
00042 
00043 static void
00044 nogui_parse_arguments (int *argc, char ***argv)
00045 {
00046   CRASH;
00047 }
00048 
00049 static void
00050 nogui_invalidate_lr (Coord l, Coord r, Coord t, Coord b)
00051 {
00052   printf("pcb: invalidate_lr() called without a GUI\n");
00053   printf("This is ok, if you run an action script\n");
00054 }
00055 
00056 static void
00057 nogui_invalidate_all (void)
00058 {
00059   CRASH;
00060 }
00061 
00062 static int
00063 nogui_set_layer (const char *name, int idx, int empty)
00064 {
00065   CRASH;
00066   return 0;
00067 }
00068 
00069 static void
00070 nogui_end_layer (void)
00071 {
00072 }
00073 
00074 static hidGC
00075 nogui_make_gc (void)
00076 {
00077   return 0;
00078 }
00079 
00080 static void
00081 nogui_destroy_gc (hidGC gc)
00082 {
00083 }
00084 
00085 static void
00086 nogui_use_mask (enum mask_mode mode)
00087 {
00088   CRASH;
00089 }
00090 
00091 static void
00092 nogui_set_color (hidGC gc, const char *name)
00093 {
00094   CRASH;
00095 }
00096 
00097 static void
00098 nogui_set_line_cap (hidGC gc, EndCapStyle style)
00099 {
00100   CRASH;
00101 }
00102 
00103 static void
00104 nogui_set_line_width (hidGC gc, Coord width)
00105 {
00106   CRASH;
00107 }
00108 
00109 static void
00110 nogui_set_draw_xor (hidGC gc, int xor_)
00111 {
00112   CRASH;
00113 }
00114 
00115 static void
00116 nogui_set_draw_faded (hidGC gc, int faded)
00117 {
00118 }
00119 
00120 static void
00121 nogui_draw_line (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
00122 {
00123   CRASH;
00124 }
00125 
00126 static void
00127 nogui_draw_arc (hidGC gc, Coord cx, Coord cy, Coord width, Coord height,
00128                 Angle start_angle, Angle end_angle)
00129 {
00130   CRASH;
00131 }
00132 
00133 static void
00134 nogui_draw_rect (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
00135 {
00136   CRASH;
00137 }
00138 
00139 static void
00140 nogui_fill_circle (hidGC gc, Coord cx, Coord cy, Coord radius)
00141 {
00142   CRASH;
00143 }
00144 
00145 static void
00146 nogui_fill_polygon (hidGC gc, int n_coords, Coord *x, Coord *y)
00147 {
00148   CRASH;
00149 }
00150 
00151 static void
00152 nogui_draw_pcb_polygon (hidGC gc, PolygonType *poly, const BoxType *clip_box)
00153 {
00154   CRASH;
00155 }
00156 
00157 static void
00158 nogui_fill_pcb_pad (hidGC gc, PadType *pad, bool clear, bool mask)
00159 {
00160   CRASH;
00161 }
00162 
00163 static void
00164 nogui_thindraw_pcb_pad (hidGC gc, PadType *pad, bool clear, bool mask)
00165 {
00166   CRASH;
00167 }
00168 
00169 static void
00170 nogui_fill_pcb_pv (hidGC fg_gc, hidGC bg_gc, PinType *pad, bool drawHole, bool mask)
00171 {
00172   CRASH;
00173 }
00174 
00175 static void
00176 nogui_thindraw_pcb_pv (hidGC fg_gc, hidGC bg_gc, PinType *pad, bool drawHole, bool mask)
00177 {
00178   CRASH;
00179 }
00180 
00181 static void
00182 nogui_fill_rect (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
00183 {
00184   CRASH;
00185 }
00186 
00187 static void
00188 nogui_calibrate (double xval, double yval)
00189 {
00190   CRASH;
00191 }
00192 
00193 static int
00194 nogui_shift_is_pressed (void)
00195 {
00196   /* This is called from FitCrosshairIntoGrid() when the board is loaded.  */
00197   return 0;
00198 }
00199 
00200 static int
00201 nogui_control_is_pressed (void)
00202 {
00203   CRASH;
00204   return 0;
00205 }
00206 
00207 static int
00208 nogui_mod1_is_pressed (void)
00209 {
00210   CRASH;
00211   return 0;
00212 }
00213 
00214 static void
00215 nogui_get_coords (const char *msg, Coord *x, Coord *y)
00216 {
00217   CRASH;
00218 }
00219 
00220 static void
00221 nogui_set_crosshair (int x, int y, int action)
00222 {
00223 }
00224 
00225 static hidval
00226 nogui_add_timer (void (*func) (hidval user_data),
00227                  unsigned long milliseconds, hidval user_data)
00228 {
00229   hidval rv;
00230   CRASH;
00231   rv.lval = 0;
00232   return rv;
00233 }
00234 
00235 static void
00236 nogui_stop_timer (hidval timer)
00237 {
00238   CRASH;
00239 }
00240 
00241 static hidval
00242 nogui_watch_file (int fd, unsigned int condition, void (*func) (hidval watch, int fd, unsigned int condition, hidval user_data),
00243   hidval user_data)
00244 {
00245   hidval rv;
00246   CRASH;
00247   rv.lval = 0;
00248   return rv;
00249 }
00250 
00251 static void
00252 nogui_unwatch_file (hidval watch)
00253 {
00254   CRASH;
00255 }
00256 
00257 static hidval
00258 nogui_add_block_hook (void (*func) (hidval data), hidval data)
00259 {
00260   hidval rv;
00261   CRASH;
00262   rv.ptr = NULL;
00263   return rv;
00264 }
00265 
00266 static void
00267 nogui_stop_block_hook (hidval block_hook)
00268 {
00269   CRASH;
00270 }
00271 
00272 static void
00273 nogui_log (const char *fmt, ...)
00274 {
00275   va_list ap;
00276   va_start (ap, fmt);
00277   vprintf (fmt, ap);
00278   va_end (ap);
00279 }
00280 
00281 static void
00282 nogui_logv (const char *fmt, va_list ap)
00283 {
00284   vprintf (fmt, ap);
00285 }
00286 
00287 /* Return a line of user input text, stripped of any newline characters.
00288  * Returns NULL if the user simply presses enter, or otherwise gives no input.
00289  */
00290 #define MAX_LINE_LENGTH 1024
00291 static char *
00292 read_stdin_line (void)
00293 {
00294   static char buf[MAX_LINE_LENGTH];
00295   char *s;
00296   int i;
00297 
00298   s = fgets (buf, MAX_LINE_LENGTH, stdin);
00299   if (s == NULL)
00300     {
00301       printf ("\n");
00302       return NULL;
00303     }
00304 
00305   /* Strip any trailing newline characters */
00306   for (i = strlen (s) - 1; i >= 0; i--)
00307     if (s[i] == '\r' || s[i] == '\n')
00308       s[i] = '\0';
00309 
00310   if (s[0] == '\0')
00311     return NULL;
00312 
00313   return strdup (s);
00314 }
00315 #undef MAX_LINE_LENGTH
00316 
00317 static int
00318 nogui_confirm_dialog (char *msg, ...)
00319 {
00320   char *answer;
00321   int ret = 0;
00322   bool valid_answer = false;
00323   va_list args;
00324 
00325   do
00326     {
00327       va_start (args, msg);
00328       vprintf (msg, args);
00329       va_end (args);
00330 
00331       printf (" ? 0=cancel 1 = ok : ");
00332 
00333       answer = read_stdin_line ();
00334 
00335       if (answer == NULL)
00336         continue;
00337 
00338       if (answer[0] == '0' && answer[1] == '\0')
00339         {
00340           ret = 0;
00341           valid_answer = true;
00342         }
00343 
00344       if (answer[0] == '1' && answer[1] == '\0')
00345         {
00346           ret = 1;
00347           valid_answer = true;
00348         }
00349 
00350       free (answer);
00351     }
00352   while (!valid_answer);
00353   return ret;
00354 }
00355 
00356 static int
00357 nogui_close_confirm_dialog ()
00358 {
00359   return nogui_confirm_dialog (_("OK to lose data ?"), NULL);
00360 }
00361 
00362 static void
00363 nogui_report_dialog (char *title, char *msg)
00364 {
00365   printf ("--- %s ---\n%s\n", title, msg);
00366 }
00367 
00368 static char *
00369 nogui_prompt_for (const char *msg, const char *default_string)
00370 {
00371   char *answer;
00372 
00373   if (default_string)
00374     printf ("%s [%s] : ", msg, default_string);
00375   else
00376     printf ("%s : ", msg);
00377 
00378   answer = read_stdin_line ();
00379   if (answer == NULL)
00380     return strdup ((default_string != NULL) ? default_string : "");
00381   else
00382     return answer;
00383 }
00384 
00385 /* FIXME - this could use some enhancement to actually use the other
00386    args */
00387 static char *
00388 nogui_fileselect (const char *title, const char *descr,
00389                   char *default_file, char *default_ext,
00390                   const char *history_tag, int flags)
00391 {
00392   char *answer;
00393 
00394   if (default_file)
00395     printf ("%s [%s] : ", title, default_file);
00396   else
00397     printf ("%s : ", title);
00398 
00399   answer = read_stdin_line ();
00400   if (answer == NULL)
00401     return (default_file != NULL) ? strdup (default_file) : NULL;
00402   else
00403     return answer;
00404 }
00405 
00406 static int
00407 nogui_attribute_dialog (HID_Attribute * attrs,
00408                         int n_attrs, HID_Attr_Val * results,
00409                         const char * title, const char * descr)
00410 {
00411   CRASH;
00412 }
00413 
00414 static void
00415 nogui_show_item (void *item)
00416 {
00417   CRASH;
00418 }
00419 
00420 static void
00421 nogui_beep (void)
00422 {
00423   putchar (7);
00424   fflush (stdout);
00425 }
00426 
00427 static int
00428 nogui_progress (int so_far, int total, const char *message)
00429 {
00430   return 0;
00431 }
00432 
00433 static HID_DRAW *
00434 nogui_request_debug_draw (void)
00435 {
00436   return NULL;
00437 }
00438 
00439 static void
00440 nogui_flush_debug_draw (void)
00441 {
00442 }
00443 
00444 static void
00445 nogui_finish_debug_draw (void)
00446 {
00447 }
00448 
00449 void
00450 common_nogui_init (HID *hid)
00451 {
00452   hid->get_export_options =   nogui_get_export_options;
00453   hid->do_export =            nogui_do_export;
00454   hid->parse_arguments =      nogui_parse_arguments;
00455   hid->invalidate_lr =        nogui_invalidate_lr;
00456   hid->invalidate_all =       nogui_invalidate_all;
00457   hid->set_layer =            nogui_set_layer;
00458   hid->end_layer =            nogui_end_layer;
00459   hid->calibrate =            nogui_calibrate;
00460   hid->shift_is_pressed =     nogui_shift_is_pressed;
00461   hid->control_is_pressed =   nogui_control_is_pressed;
00462   hid->mod1_is_pressed =      nogui_mod1_is_pressed;
00463   hid->get_coords =           nogui_get_coords;
00464   hid->set_crosshair =        nogui_set_crosshair;
00465   hid->add_timer =            nogui_add_timer;
00466   hid->stop_timer =           nogui_stop_timer;
00467   hid->watch_file =           nogui_watch_file;
00468   hid->unwatch_file =         nogui_unwatch_file;
00469   hid->add_block_hook =       nogui_add_block_hook;
00470   hid->stop_block_hook =      nogui_stop_block_hook;
00471   hid->log =                  nogui_log;
00472   hid->logv =                 nogui_logv;
00473   hid->confirm_dialog =       nogui_confirm_dialog;
00474   hid->close_confirm_dialog = nogui_close_confirm_dialog;
00475   hid->report_dialog =        nogui_report_dialog;
00476   hid->prompt_for =           nogui_prompt_for;
00477   hid->fileselect =           nogui_fileselect;
00478   hid->attribute_dialog =     nogui_attribute_dialog;
00479   hid->show_item =            nogui_show_item;
00480   hid->beep =                 nogui_beep;
00481   hid->progress =             nogui_progress;
00482   hid->request_debug_draw =   nogui_request_debug_draw;
00483   hid->flush_debug_draw =     nogui_flush_debug_draw;
00484   hid->finish_debug_draw =    nogui_finish_debug_draw;
00485 }
00486 
00487 void
00488 common_nogui_graphics_init (HID_DRAW *graphics)
00489 {
00490   graphics->make_gc =         nogui_make_gc;
00491   graphics->destroy_gc =      nogui_destroy_gc;
00492   graphics->use_mask =        nogui_use_mask;
00493   graphics->set_color =       nogui_set_color;
00494   graphics->set_line_cap =    nogui_set_line_cap;
00495   graphics->set_line_width =  nogui_set_line_width;
00496   graphics->set_draw_xor =    nogui_set_draw_xor;
00497   graphics->set_draw_faded =  nogui_set_draw_faded;
00498   graphics->draw_line =       nogui_draw_line;
00499   graphics->draw_arc =        nogui_draw_arc;
00500   graphics->draw_rect =       nogui_draw_rect;
00501   graphics->fill_circle =     nogui_fill_circle;
00502   graphics->fill_polygon =    nogui_fill_polygon;
00503   graphics->fill_rect =       nogui_fill_rect;
00504 
00505   graphics->draw_pcb_polygon = nogui_draw_pcb_polygon;
00506   graphics->fill_pcb_pad =     nogui_fill_pcb_pad;
00507   graphics->thindraw_pcb_pad = nogui_thindraw_pcb_pad;
00508   graphics->fill_pcb_pv =      nogui_fill_pcb_pv;
00509   graphics->thindraw_pcb_pv =  nogui_thindraw_pcb_pv;
00510 }
00511 
00512 static HID nogui_hid;
00513 static HID_DRAW nogui_graphics;
00514 
00515 HID *
00516 hid_nogui_get_hid (void)
00517 {
00518   memset (&nogui_hid, 0, sizeof (HID));
00519   memset (&nogui_graphics, 0, sizeof (HID_DRAW));
00520 
00521   nogui_hid.struct_size = sizeof (HID);
00522   nogui_hid.name        = "nogui";
00523   nogui_hid.description = "Default GUI when no other GUI is present.  "
00524                           "Does nothing.";
00525   nogui_hid.graphics    = &nogui_graphics;
00526 
00527   common_nogui_init (&nogui_hid);
00528   common_nogui_graphics_init (&nogui_graphics);
00529 
00530   return &nogui_hid;
00531 }