Index: trunk/src/main.c =================================================================== --- trunk/src/main.c (revision 12108) +++ trunk/src/main.c (revision 12109) @@ -61,6 +61,7 @@ #include "layer_vis.h" #include "obj_text.h" #include "pcb_minuid.h" +#include "tool.h" #include "hid_actions.h" #include "hid_init.h" @@ -254,6 +255,7 @@ free(PCB); PCB = NULL; + pcb_tool_uninit(); pcb_hid_uninit(); pcb_text_uninit(); layer_vis_uninit(); @@ -435,6 +437,7 @@ pcb_events_init(); pcb_text_init(); + pcb_tool_init(); pup_init(&pcb_pup); pcb_pup.error_stack_enable = 1; Index: trunk/src/tool.c =================================================================== --- trunk/src/tool.c (revision 12108) +++ trunk/src/tool.c (revision 12109) @@ -19,3 +19,95 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ + +#include "config.h" + +#include "tool.h" + +#include "error.h" + + +static void default_tool_reg(void); +static void default_tool_unreg(void); + +void pcb_tool_init(void) +{ + vtp0_init(&pcb_tools); + default_tool_reg(); /* temporary */ +} + +void pcb_tool_uninit(void) +{ + default_tool_unreg(); /* temporary */ + while(vtp0_len(&pcb_tools) != 0) { + const pcb_tool_t *tool = pcb_tool_get(0); + pcb_message(PCB_MSG_WARNING, "Unregistered tool: %s of %s; check your plugins, fix them to unregister their tools!\n", tool->name, tool->cookie); + pcb_tool_unreg_by_cookie(tool->cookie); + } + vtp0_uninit(&pcb_tools); +} + +int pcb_tool_reg(const pcb_tool_t *tool) +{ + if (pcb_tool_lookup(tool->name) != PCB_TOOLID_INVALID) /* don't register two tools with the same name */ + return -1; + vtp0_append(&pcb_tools, (void *)tool); + return 0; +} + +void pcb_tool_unreg_by_cookie(const char *cookie) +{ + pcb_toolid_t n; + for(n = 0; n < vtp0_len(&pcb_tools); n++) { + const pcb_tool_t *tool = pcb_tool_get(0); + if (tool->cookie == cookie) { + vtp0_remove(&pcb_tools, n, 1); + n--; + } + } +} + +pcb_toolid_t pcb_tool_lookup(const char *name) +{ + pcb_toolid_t n; + for(n = 0; n < vtp0_len(&pcb_tools); n++) { + const pcb_tool_t *tool = (const pcb_tool_t *)pcb_tools.array[n]; + if (strcmp(tool->name, name) == 0) + return n; + } + return PCB_TOOLID_INVALID; +} + + + +#warning tool TODO: move this out to a tool plugin + +#include "tool_arc.h" +#include "tool_arrow.h" +#include "tool_buffer.h" +#include "tool_copy.h" +#include "tool_insert.h" +#include "tool_line.h" +#include "tool_lock.h" +#include "tool_move.h" +#include "tool_poly.h" +#include "tool_polyhole.h" +#include "tool_rectangle.h" +#include "tool_remove.h" +#include "tool_rotate.h" +#include "tool_text.h" +#include "tool_thermal.h" +#include "tool_via.h" + +const char *cookie = "default tools"; + +static void default_tool_reg(void) +{ + +} + +static void default_tool_unreg(void) +{ + pcb_tool_unreg_by_cookie(cookie); +} + Index: trunk/src/tool.h =================================================================== --- trunk/src/tool.h (revision 12108) +++ trunk/src/tool.h (revision 12109) @@ -19,3 +19,55 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ + +#ifndef PCB_TOOL_H +#define PCB_TOOL_H + +#include + +typedef int pcb_toolid_t; +#define PCB_TOOLID_INVALID (-1) + +typedef struct pcb_tool_s { + const char *name; /* textual name of the tool */ + const char *cookie; /* plugin cookie _pointer_ of the registrar (comparision is pointer based, not strcmp) */ + int priority; /* lower values are higher priorities; escaping mode will try to select the highest prio tool */ + + /* tool implementation */ + void (*notify_mode)(void); +} pcb_tool_t; + +vtp0_t pcb_tools; + +/* (un)initialize the tool subsystem */ +void pcb_tool_init(void); +void pcb_tool_uninit(void); + +/* Insert a new tool in pcb_tools; returns 0 on success */ +int pcb_tool_reg(const pcb_tool_t *tool); + +/* Unregister all tools that has matching cookie */ +void pcb_tool_unreg_by_cookie(const char *cookie); + +/* Return the ID of a tool by name; returns -1 on error */ +pcb_toolid_t pcb_tool_lookup(const char *name); + + +/* Select a tool by name, id or pick the highest prio tool; return 0 on success */ +int pcb_tool_select_by_name(const char *name); +int pcb_tool_select_by_id(pcb_toolid_t id); +int pcb_tool_select_highest(void); + +/**** Tool function wrappers; calling these will operate on the current tool + as defined in conf_core.editor.mode ****/ + +void pcb_tool_notify_mode(void); + + + +/**** Low level, for internal use ****/ + +/* Get the tool pointer of a tool by id */ +#define pcb_tool_get(id) ((const pcb_tool_t *)vtp0_get(&pcb_tools, id, 0)) + +#endif