4 * \brief Human Interface Device.
6 * \author This file, hid.h, was written and is
7 * Copyright (c) 2006 DJ Delorie <dj@delorie.com>
9 * The way the HID layer works is that you instantiate a HID device
10 * structure, and invoke functions through its members.
12 * Code in the common part of PCB may *not* rely on *anything* other
13 * than what's defined in this file.
15 * Code in the HID layers *may* rely on data and functions in the common
16 * code (like, board size and such) but it's considered bad form to do
19 * Coordinates are ALWAYS in pcb's default resolution.
21 * Positive X is right, positive Y is down.
23 * Angles are degrees, with 0 being right (positive X) and 90 being up
26 * All zoom, scaling, panning, and conversions are hidden inside the HID
29 * The main structure is at the end of this file.
31 * Data structures passed to the HIDs will be copied if the HID needs to
34 * Data structures retured from the HIDs must not be freed, and may be
35 * changed by the HID in response to new information.
39 * <h1><b>Copyright.</b></h1>\n
41 * PCB, interactive printed circuit board design
43 * Copyright (C) 1994,1995,1996 Thomas Nau
45 * Copyright (C) 1998,1999,2000,2001 harry eaton
47 * This program is free software; you can redistribute it and/or modify
48 * it under the terms of the GNU General Public License as published by
49 * the Free Software Foundation; either version 2 of the License, or
50 * (at your option) any later version.
52 * This program is distributed in the hope that it will be useful,
53 * but WITHOUT ANY WARRANTY; without even the implied warranty of
54 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
55 * GNU General Public License for more details.
57 * You should have received a copy of the GNU General Public License
58 * along with this program; if not, write to the Free Software
59 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
61 * Contact addresses for paper mail and Email:
62 * harry eaton, 6697 Buttonhole Ct, Columbia, MD 21044 USA
63 * haceaton@aplcomm.jhuapl.edu
71 #if defined(__cplusplus) && __cplusplus
77 * \brief Like end cap styles.
79 * The cap *always* extends beyond the coordinates given, by half the
82 * Beveled ends can used to make octagonal pads by giving the same x,y
87 Trace_Cap
, /*!< This means we're drawing a trace, which has round caps. */
88 Square_Cap
, /*!< Square pins or pads. */
89 Round_Cap
, /*!< Round pins or round-ended pads, thermals. */
90 Beveled_Cap
/*!< Octagon pins or bevel-cornered pads. */
94 * \brief The HID may need something more than an "int" for colors,
97 * So it passes/returns one of these, which is castable to a variety
107 * This graphics context is an opaque pointer defined by the HID.
109 * GCs are HID-specific; attempts to use one HID's GC for a different
110 * HID will result in a fatal error.
112 typedef struct hid_gc_struct
*hidGC
;
114 #define HIDCONCAT(a,b) a##b
117 * \brief This is used to register the action callbacks (for menus and
120 * HID assumes the following actions are available for its use:
121 * - SaveAs(filename);
127 /*!< This is matched against action names in the GUI configuration */
128 const char *need_coord_msg
;
129 /*!< If this string is non-NULL, the action needs to know the X,Y
130 * coordinates to act on, and this string may be used to prompt
131 * the user to select a coordinate. If NULL, the coordinates may
132 * be 0,0 if none are known. */
133 int (*trigger_cb
) (int argc
, char **argv
, Coord x
, Coord y
);
134 /*!< Called when the action is triggered. If this function returns
135 * non-zero, no further actions will be invoked for this key/mouse
137 const char *description
;
138 /*!< Short description that sometimes accompanies the name. */
140 /*!< Full allowed syntax; use \\n to separate lines. */
143 extern void hid_register_action (HID_Action
*);
145 extern void hid_register_actions (HID_Action
*, int);
146 #define REGISTER_ACTIONS(a) HIDCONCAT(void register_,a) ()\
147 { hid_register_actions(a, sizeof(a)/sizeof(a[0])); }
149 /* Note that PCB expects the gui to provide the following actions:
153 NetlistChanged() (but core should call "void NetlistChanged(int);" in netlist.c)
159 extern const char pcbchanged_help
[];
160 extern const char pcbchanged_syntax
[];
161 extern const char routestyleschanged_help
[];
162 extern const char routestyleschanged_syntax
[];
163 extern const char netlistchanged_help
[];
164 extern const char netlistchanged_syntax
[];
165 extern const char layerschanged_help
[];
166 extern const char layerschanged_syntax
[];
167 extern const char librarychanged_help
[];
168 extern const char librarychanged_syntax
[];
170 int hid_action (const char *action_
);
171 int hid_actionl (const char *action_
, ...); /* NULL terminated */
172 int hid_actionv (const char *action_
, int argc_
, char **argv_
);
173 void hid_save_settings (int);
174 void hid_load_settings (void);
177 * \brief Parse the given command string into action calls, and call
178 * hid_actionv for each action found.
180 * Accepts both "action(arg1, arg2)" and command-style
181 * "action arg1 arg2", allowing only one action in the later case.
183 * \return Returns nonzero if the action handler(s) return nonzero.
185 int hid_parse_command (const char *str_
);
188 * \brief Parse the given string into action calls, and call hid_actionv
189 * for each action found.
191 * Accepts only "action(arg1, arg2)".
193 int hid_parse_actions (const char *str_
);
197 char *name
; /*!< Name of the flag */
198 int (*function
) (void *);
199 /*!< Function to call to get the value of the flag. */
201 /*!< Additional parameter to pass to that function. */
204 extern void hid_register_flags (HID_Flag
*, int);
205 #define REGISTER_FLAGS(a) HIDCONCAT(void register_,a) ()\
206 { hid_register_flags(a, sizeof(a)/sizeof(a[0])); }
209 * \brief Looks up one of the flags registered above.
211 * \return If the flag is unknown, returns zero.
213 int hid_get_flag (const char *name_
);
216 * \brief Used for HID attributes (exporting and printing, mostly).
218 * HA_boolean uses int_value, HA_enum sets int_value to the index and
219 * str_value to the enumeration string.
221 * HID_Label just shows the default str_value.
223 * HID_Mixed is a real_value followed by an enum, like 0.5in or 100mm.
228 const char *str_value
;
234 { HID_Label
, HID_Integer
, HID_Real
, HID_String
,
235 HID_Boolean
, HID_Enum
, HID_Mixed
, HID_Path
,
242 #define ATTR_UNDOCUMENTED ((char *)(1))
244 /*!< If the help_text is this, usage() won't show this option */
246 int min_val
; /*!< For integer and real */
247 int max_val
; /*!< For integer and real */
248 HID_Attr_Val default_val
; /*!< Also actual value for global attributes. */
249 const char **enumerations
;
251 /*!< If set, this is used for global attributes (i.e. those set
252 * statically with REGISTER_ATTRIBUTES below) instead of changing
254 * \note Note that a HID_Mixed attribute must specify a pointer to
255 * HID_Attr_Val here, and HID_Boolean assumes this is "char *" so
256 * the value should be initialized to zero, and may be set to
257 * non-zero (not always one).
259 int hash
; /*!< For detecting changes. */
262 extern void hid_register_attributes (HID_Attribute
*, int);
264 #define REGISTER_ATTRIBUTES(a) HIDCONCAT(void register_,a) ()\
265 { hid_register_attributes(a, sizeof(a)/sizeof(a[0])); }
267 /* These three are set by hid_parse_command_line(). */
268 extern char *program_name
;
269 extern char *program_directory
;
270 extern char *program_basename
;
272 /* These are used for set_layer(). */
273 #define SL_0_SIDE 0x0000
274 #define SL_TOP_SIDE 0x0001
275 #define SL_BOTTOM_SIDE 0x0002
276 #define SL_INNER_SIDE 0x0004
279 * These are types of "layers" without direct physical representation.
280 * Their content can be derived from other layers and element data.
282 * These values are used by DrawEverything() in draw.c to ask the active
283 * GUI or exporter to act on these layers. Depending on the GUI/exporter
284 * they result in a per-layer file, in some additional graphics or just
287 #define SL_SILK 0x0010 /*!< Physical layer, deprecated, use LT_SILK. */
288 #define SL_MASK 0x0020
289 #define SL_PDRILL 0x0030
290 #define SL_UDRILL 0x0040
291 #define SL_PASTE 0x0050
292 #define SL_INVISIBLE 0x0060
293 #define SL_FAB 0x0070
294 #define SL_ASSY 0x0080
295 #define SL_RATS 0x0090
297 /* Callers should use this. */
298 #define SL(type,side) (~0xfff | SL_##type | SL_##side##_SIDE)
301 * These are layers with direct physical representation, like copper, dye
302 * or to be milled paths. Their data can't be derived from other layers or
305 * To add more layer types, add them to the list here and in layerflags.c.
306 * Order of entries in both lists must be the same.
312 LT_MASK
, /*!< Complements SL_MASK above. */
313 LT_PASTE
, /*!< Complements SL_PASTE above. */
314 LT_OUTLINE
, /*!< Board outline; exists only once. */
317 LT_FAB
, /*!< Complements SL_FAB above. */
318 LT_ASSY
, /*!< Complements SL_ASSY above. */
320 LT_NUM_LAYERTYPES
/*!< Must be the last one. */
324 * \brief File Watch flags.
326 * Based upon those in dbus/dbus-connection.h.
330 PCB_WATCH_READABLE
= 1 << 0, /*!< As in POLLIN */
331 PCB_WATCH_WRITABLE
= 1 << 1, /*!< As in POLLOUT */
332 PCB_WATCH_ERROR
= 1 << 2, /*!< As in POLLERR */
333 PCB_WATCH_HANGUP
= 1 << 3 /*!< As in POLLHUP */
337 * \brief DRC GUI Hooks.
341 int log_drc_overview
;
342 int log_drc_violations
;
343 void (*reset_drc_dialog_message
) (void);
344 void (*append_drc_violation
) (DrcViolationType
*violation
);
345 int (*throw_drc_dialog
) (void);
348 typedef struct hid_st HID
;
349 typedef struct hid_draw_st HID_DRAW
;
352 * \brief This is the main HID structure.
357 /*!< The size of this structure.
359 * We use this as a compatibility check; a HID built with a
360 * different hid.h than we're expecting should have a different
365 /*!< The name of this HID.
367 * This should be suitable for command line options,
368 * multi-selection menus, file names, etc.
371 const char *description
;
372 /*!< Likewise, but allowed to be longer and more descriptive. */
375 /*!< If set, this is the GUI HID.
377 * Exactly one of these three flags must be set; setting "gui"
378 * lets the expose callback optimize and coordinate itself.
382 /*!< If set, this is the printer-class HID.
384 * The common part of PCB may use this to do command-line printing,
385 * without having instantiated any GUI HIDs.
386 * Only one printer HID is normally defined at a time.
390 /*!< If set, this HID provides an export option, and should be
391 * used as part of the File->Export menu option.
393 * Examples are PNG, Gerber, and EPS exporters.
397 /*!< If set, the redraw code will draw polygons before erasing the
402 /*!< If set, the redraw code will draw polygons after erasing the
405 * \note Note that HIDs may set both of these, in which case
406 * polygons will be drawn twice.
409 HID_Attribute
*(*get_export_options
) (int *n_ret_
);
410 /*!< Returns a set of resources describing options the export or
411 * print HID supports.
413 * In GUI mode, the print/export dialogs use this to set up the
414 * selectable options.
416 * In command line mode, these are used to interpret command line
419 * If \c n_ret_ is non-NULL, the number of attributes is stored
423 void (*do_export
) (HID_Attr_Val
* options_
);
424 /*!< Export (or print) the current PCB.
426 * The options given represent the choices made from the options
427 * returned from get_export_options.
429 * Call with options == NULL to start the primary GUI (create a
430 * main window, print, export, etc).
433 void (*parse_arguments
) (int *argc_
, char ***argv_
);
434 /*!< Parse the command line.
436 * Call this early for whatever HID will be the primary HID, as it
437 * will set all the registered attributes.
439 * The HID should remove all arguments, leaving any possible file
443 void (*invalidate_lr
) (int left_
, int right_
, int top_
, int bottom_
);
444 /*!< This may be called to ask the GUI to force a redraw of a
448 void (*invalidate_all
) (void);
450 void (*notify_crosshair_change
) (bool changes_complete
);
452 void (*notify_mark_change
) (bool changes_complete
);
454 int (*set_layer
) (const char *name_
, int group_
, int _empty
);
455 /*!< During redraw or print/export cycles, this is called once per
456 * layer (or layer group, for copper layers).
458 * If it returns false (zero), the HID does not want that layer,
459 * and none of the drawing functions should be called.
461 * If it returns true (nonzero), the items in that layer [group]
462 * should be drawn using the various drawing functions.
464 * In addition to the MAX_GROUP copper layer groups, you may
465 * select layers indicated by the macros SL_* defined above, or
466 * any others with an index of -1.
468 * For copper layer groups, you may pass NULL for name to have a
469 * name fetched from the PCB struct.
471 * The EMPTY argument is a hint - if set, the layer is empty, if
472 * zero it may be non-empty.
475 void (*end_layer
) (void);
476 /*!< Tell the GUI the layer last selected has been finished with. */
480 void (*calibrate
) (double xval_
, double yval_
);
481 /*!< This is for the printer.
483 * If you call this for the GUI, \c xval_ and \c yval_ are ignored,
484 * and a dialog pops up to lead you through the calibration
487 * For the printer, if \c xval_ and \c yval_ are zero, a
488 * calibration page is printed with instructions for calibrating
491 * After calibrating, nonzero \c xval_ and \c yval_ are passed
492 * according to the instructions.
494 * Metric is nonzero if the user prefers metric units, else inches
498 /* GUI layout functions. Not used or defined for print/export
502 int (*shift_is_pressed
) (void);
504 int (*control_is_pressed
) (void);
505 int (*mod1_is_pressed
) (void);
507 void (*get_coords
) (const char *msg_
, Coord
*x_
, Coord
*y_
);
509 void (*set_crosshair
) (int x_
, int y_
, int cursor_action_
);
510 /*!< Sets the crosshair, which may differ from the pointer
511 * depending on grid and pad snap.
513 * \note Note that the HID is responsible for hiding, showing,
514 * redrawing, etc. The core just tells it what coordinates it's
517 * \note Note that this routine may need to know what "pcb units"
518 * are so it can display them in mm or mils accordingly.
520 * If \c cursor_action_ is set, the cursor or screen may be
521 * adjusted so that the cursor and the crosshair are at the same
522 * point on the screen.
524 #define HID_SC_DO_NOTHING 0
526 #define HID_SC_WARP_POINTER 1
528 #define HID_SC_PAN_VIEWPORT 2
530 #define HID_SC_CENTER_IN_VIEWPORT 3
532 #define HID_SC_CENTER_IN_VIEWPORT_AND_WARP_POINTER 4
534 hidval (*add_timer
) (void (*func
) (hidval user_data_
),
535 unsigned long milliseconds_
, hidval user_data_
);
536 /*!< Causes func to be called at some point in the future.
538 * Timers are only good for *one* call; if you want it to
539 * repeat, add another timer during the callback for the first.
541 * \c user_data can be anything, it's just passed to func.
543 * \warning Times are not guaranteed to be accurate.
546 void (*stop_timer
) (hidval timer_
);
547 /*!< Use this to stop a timer that hasn't triggered yet. */
549 hidval (*watch_file
) (int fd_
, unsigned int condition_
, void (*func_
) (hidval watch_
, int fd_
, unsigned int condition_
, hidval user_data_
),
551 /*!< Causes \c func_ to be called when some condition occurs on
552 * the file descriptor passed.
554 * Conditions include data for reading, writing, hangup, and
557 * \c user_data can be anything, it's just passed to \c func_.
560 void (*unwatch_file
) (hidval watch_
);
561 /*!< Use this to stop a file watch. */
563 hidval (*add_block_hook
) (void (*func_
) (hidval data_
), hidval data_
);
564 /*!< Causes \c func to be called in the main loop prior to
568 void (*stop_block_hook
) (hidval block_hook_
);
569 /*!< Use this to stop a main loop block hook. */
571 /* Various dialogs */
573 void (*log
) (const char *fmt_
, ...);
574 /*!< Log a message to the log window. */
576 void (*logv
) (const char *fmt_
, va_list args_
);
577 /*!< Log a message to the log window. */
579 int (*confirm_dialog
) (char *msg_
, ...);
580 /*!< A generic yes/no dialog.
582 * Returns zero if the cancel button is pressed, one for the ok
585 * If you specify alternate labels for ..., they are used instead
586 * of the default OK/Cancel ones, and the return value is the
587 * index of the label chosen.
589 * \warning You MUST pass NULL as the last parameter to this.
592 int (*close_confirm_dialog
) ();
593 /*!< A close confirmation dialog for unsaved pages, for example,
594 * with options "Close without saving", "Cancel" and "Save".
596 * Returns zero if the close is cancelled, or one if it should
599 * The HID is responsible for any "Save" action the user may wish
600 * before confirming the close.
602 #define HID_CLOSE_CONFIRM_CANCEL 0
604 #define HID_CLOSE_CONFIRM_OK 1
606 void (*report_dialog
) (char *title_
, char *msg_
);
607 /*!< Just prints text. */
609 char *(*prompt_for
) (const char *msg_
, const char *default_string_
);
610 /*!< Prompts the user to enter a string, returns the string.
612 * If \c default_string isn't NULL, the form is pre-filled with
615 * "msg" is like "Enter value:".
618 #define HID_FILESELECT_READ 0x01
619 /*!< Prompts the user for a filename or directory name.
621 * For GUI HID's this would mean a file select dialog box.
623 * The 'flags' argument is the bitwise OR of the following values.
626 #define HID_FILESELECT_MAY_NOT_EXIST 0x02
627 /*!< The function calling hid->fileselect will deal with the case
628 * where the selected file already exists.
630 * If not given, then the gui will prompt with an "overwrite?"
633 * Only used when writing.
636 #define HID_FILESELECT_IS_TEMPLATE 0x04
637 /*!< The call is supposed to return a file template (for gerber
638 * output for example) instead of an actual file.
640 * Only used when writing.
643 char *(*fileselect
) (const char *title_
, const char *descr_
,
644 char *default_file_
, char *default_ext_
,
645 const char *history_tag_
, int flags_
);
646 /*!< \c title_ may be used as a dialog box title. Ignored if NULL.
648 * \c descr_ is a longer help string. Ignored if NULL.
650 * \c default_file_ is the default file name. Ignored if NULL.
652 * \c default_ext_ is the default file extension, like ".pdf".
655 * \c history_tag_ may be used by the GUI to keep track of file
656 * history. Examples would be "board", "vendor", "renumber",
657 * etc. If NULL, no specific history is kept.
659 * \c flags_ are the bitwise or of the HID_FILESELECT defines
663 int (*attribute_dialog
) (HID_Attribute
* attrs_
,
664 int n_attrs_
, HID_Attr_Val
* results_
,
665 const char * title_
, const char * descr_
);
666 /*!< A generic dialog to ask for a set of attributes.
668 * If \c n_attrs_ is zero, \c attrs(.name) must be NULL terminated.
670 * Returns non-zero if an error occurred (usually, this means the
671 * user cancelled the dialog or something).
673 * \c title_ is the title of the dialog box.
675 * \c descr_ (if not NULL) can be a longer description of what the
676 * attributes are used for.
678 * The HID may choose to ignore it or it may use it for a tooltip
679 * or text in a dialog box, or a help string.
682 void (*show_item
) (void *item_
);
683 /*!< This causes a second window to display, which only shows the
686 * The expose callback is called twice; once to size the extents
687 * of the item, and once to draw it.
689 * To pass magic values, pass the address of a variable created
694 /*!< Something to alert the user. */
696 int (*progress
) (int so_far_
, int total_
, const char *message_
);
697 /*!< Used by optimizers and autorouter to show progress to the
700 * Pass all zeros to flush display and remove any dialogs.
702 * Returns nonzero if the user wishes to cancel the operation.
705 HID_DRC_GUI
*drc_gui
;
707 void (*edit_attributes
) (char *owner
, AttributeListType
*attrlist_
);
709 /* Debug drawing support. These APIs must be implemented (non NULL),
710 * but they do not have to be functional. request_debug_draw can
711 * return NULL to indicate debug drawing is not permitted.
713 * Debug drawing is not guaranteed to be re-entrant.
714 * The caller must not nest requests for debug drawing.
717 HID_DRAW
*(*request_debug_draw
) (void);
718 /*!< Request permission for debug drawing.
720 * Returns a HID_DRAW pointer which should be used rather than the
721 * global gui->graphics-> for making drawing calls.
723 * If the return value is NULL, then permission has been denied,
724 * and the drawing must not continue.
726 * \warning Debug drawing is not guaranteed to be re-entrant.
727 * The caller must not nest requests for debug drawing.
730 void (*flush_debug_draw
) (void);
731 /*!< Flush pending drawing to the screen.
733 * May be implemented as a NOOP if the GUI has chosen to send the
734 * debug drawing directly to the screen.
737 void (*finish_debug_draw
) (void);
738 /*!< When finished, the user must inform the GUI to clean up
741 * Any remaining rendering will be flushed to the screen.
744 void (*notify_save_pcb
) (const char *filename
, bool done
);
745 /*!< Notification to the GUI around saving the PCB file.
747 * Called with a false parameter before the save, called again
748 * with true after the save.
750 * Allows GUIs which watch for file-changes on disk to ignore
751 * our deliberate changes.
754 void (*notify_filename_changed
) (void);
755 /*!< Notification to the GUI that the PCB file has been renamed. */
759 * \brief Call this as soon as possible from main().
761 * No other HID calls are valid until this is called.
763 void hid_init (void);
766 * \brief When PCB runs in interactive mode, this is called to
767 * instantiate one GUI HID which happens to be the GUI.
769 * This HID is the one that interacts with the mouse and keyboard.
771 HID
*hid_find_gui ();
774 * \brief Finds the one printer HID and instantiates it.
776 HID
*hid_find_printer (void);
779 * \brief Finds the indicated exporter HID and instantiates it.
781 HID
*hid_find_exporter (const char *);
784 * \brief This returns a NULL-terminated array of available HIDs.
786 * The only real reason to use this is to locate all the export-style
789 HID
**hid_enumerate (void);
792 * \brief This function (in the common code) will be called whenever
793 * the GUI needs to redraw the screen, print the board, or export a
796 * If item is not NULL, only draw the given item.
797 * Item is only non-NULL if the HID was created via show_item.
799 * Each time func is called, it should do the following:
801 * allocate any colors needed, via get_color.
803 * cycle through the layers, calling set_layer for each layer to be
804 * drawn, and only drawing elements (all or specified) of desired
807 * Do *not* assume that the hid that is passed is the GUI hid.
809 * This callback is also used for printing and exporting.
811 void hid_expose_callback (HID
* hid_
, struct BoxType
*region_
, void *item_
);
814 /*!< This is initially set to a "no-gui" gui, and later reset by
817 * hid_expose_callback also temporarily set it for drawing.
820 extern HID
*exporter
;
821 /*!< This is either NULL or points to the current HID that is being
822 * called to do the exporting.
824 * The gui HIDs set and unset this var.
827 extern HID_Action
*current_action
;
828 /*!< This is either NULL or points to the current HID_Action that is
831 * The action launcher sets and unsets this variable.
834 extern int pixel_slop
;
835 /*!< The GUI may set this to be approximately the PCB size of a
836 * pixel, to allow for near-misses in selection and changes in
837 * drawing items smaller than a screen pixel.
840 #if defined(__cplusplus) && __cplusplus