Hook example code

From Second Life Wiki
Revision as of 20:24, 12 February 2007 by Bushing Spatula (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
#include "lldarray.h"
// data1 and data2 may be used for any arbitrary user data.
// from_plugin should be set to FALSE if the function is being called due to a user's action,
// and TRUE if it is being called from another plugin.  (The idea is to avoid event loops, but
// maybe there is a better way.
// If function pointer returns FALSE, and this is one of a series of callbacks for a hook, 
// the function should terminate without passing control to any further plugins or to the
// rest of the viewer function.  (This is to allow hook functions to "swallow" events by return FALSE.)
typedef bool (*func_ptr)(void *data1, void *data2);

class hook {
public:
        const char * name;
        LLDynamicArray<func_ptr> *hook_ptrs;
        hook() {}
        hook(const char *n) {name = n; hook_ptrs = new (LLDynamicArray<func_ptr>)();}
};

class viewer_func  {
public:
        const char * name;
        func_ptr ptr;
        viewer_func() {}
        viewer_func(const char * n, func_ptr p) { name=n; ptr=p;}
};

class plugin_interface {
private:
        hook * get_hook_by_name(const char *name);
        viewer_func * get_viewer_func_by_name(const char *name);
public:
        LLDynamicArray<hook> hooks;
        LLDynamicArray<viewer_func> viewer_funcs;
        
        void add_hook(const char *name, const func_ptr ptr);  

       void add_viewer_func(const char *name, func_ptr ptr);
       char ** get_viewer_func_list();
       char ** get_hook_list();
       
       const char * get_interface_version();
       void * call_viewer_func(const char *name, void *data1, void *data2);
       bool call_hooks(const char *name, void *data1, void *data2);
       void init();
       //plugin_interface();   
       
};

#define PLUGIN_HOOK(name, data1, data2) fprintf(stderr, "PLUGIN_HOOK(%s)\n", name); if(!gPluginInterface.call_hooks(name, (void *)(data1), (void *)(data2))) return; 

extern plugin_interface gPluginInterface;