Writing addon interfaces: example

Updated: April 19, 2023

Suppose you have an application that needs to use screensavers. You can define screensaver interfaces and write addons that expose functionality through these interfaces. Your application can then load the available addons that implement these interfaces and call functions to start, stop, and set options for a particular screensaver.

To write the necessary addon interfaces, you must perform steps like the following:
  1. Determine the basic functionality required by the application. For each screensaver addon, you definitely need functions to start and stop the screensaver, and likely need a function to create and populate an options pane. You may also want a function that initializes the addon (to acquire resources) and one that uninitializes it (to release them).
    After writing the code for these functions, you must expose them in an interface like this:
    typedef struct {
        int32_t (*Initialize)(void);
        int32_t (*Uninitialize)(void);
        int32_t (*Options)(void);
        int32_t (*Start)(void);
        int32_t (*Stop)(void);
    } ScreenSaver;
    
  2. A screensaver addon might have to support user-defined preferences; in this case, you could have another set of functions exposed by another interface. The application could then display preferences for only those screensaver addons with this interface, which might look like this:
    typedef struct {
        int32_t (*LoadPrefs)(void);
        int32_t (*SavePrefs)(void);
    } ScreenSaverPrefs;
    
  3. In each addon, you must declare the available interfaces, to allow the AOI library to discover and use them. This is done by defining a set of AOInterface_t structures:
    AOInterface_t interfaces[] = {
        { "Name", 0, "my_screensaver" },
        { "ScreenSaver", SS_VERSION, &SS2 },
        { "ScreenSaverPrefs", SS_PREFSVERSION, &SSP2 },
        { 0, 0, 0 }
    };
    

    The first interface, Name, makes the interface set discoverable to the library. Note the last “dummy” entry in the interface set; this is an end-of-list marker.

Your application can now use the AOI library to find the available addons (which are DLLs) that implement this set of interfaces. It can then allow the user to select and configure options for a particular screensaver. For an explanation of how your application can access and use the addon interfaces, see the Core API overview.

A more detailed example of writing an addon and the application code that compares addon ratings, selects the best one, and then uses it is given in the AoIterate() reference.