Ignore:
Timestamp:
Dec 16, 2007, 2:50:41 AM (13 years ago)
Author:
Sam Hocevar
Message:
  • Implement caca_create_display_with_driver() to specify an output driver without using environment variables..
  • Implement caca_get_display_driver() to return the current display's output driver.
  • Add a simple example program to test caca_create_display_with_driver().
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca/caca.c

    r2137 r2138  
    3838
    3939#if defined(USE_PLUGINS)
    40 #   define gl_install(p) caca_plugin_install("gl", p)
    41 #   define x11_install(p) caca_plugin_install("x11", p)
     40#   define gl_install(p) caca_plugin_install(p, "gl")
     41#   define x11_install(p) caca_plugin_install(p, "x11")
    4242#endif
    4343
    4444static int caca_can_resize(caca_display_t *);
    45 static int caca_select_driver(caca_display_t *);
     45static int caca_select_driver(caca_display_t *, char const *);
    4646#if defined(USE_PLUGINS)
    4747static int caca_plugin_install(char const *, caca_display_t *);
     
    5959 *  caca_free_display() is called.
    6060 *
     61 *  See also caca_create_display_with_driver().
     62 *
    6163 *  If an error occurs, NULL is returned and \b errno is set accordingly:
    6264 *  - \c ENOMEM Not enough memory.
    6365 *  - \c ENODEV Graphical device could not be initialised.
    6466 *
    65  *  \param cv The cucul cavas.
     67 *  \param cv The cucul canvas or NULL to create a canvas automatically.
    6668 *  \return The caca graphical context or NULL if an error occurred.
    6769 */
    6870caca_display_t * caca_create_display(cucul_canvas_t *cv)
     71{
     72    return caca_create_display_with_driver(cv, NULL);
     73}
     74
     75/** \brief Attach a caca graphical context to a cucul canvas.
     76 *
     77 *  Create a graphical context using device-dependent features (ncurses for
     78 *  terminals, an X11 window, a DOS command window...) that attaches to a
     79 *  libcucul canvas. Everything that gets drawn in the libcucul canvas can
     80 *  then be displayed by the libcaca driver.
     81 *
     82 *  If no cucul canvas is provided, a new one is created. Its handle can be
     83 *  retrieved using caca_get_canvas() and it is automatically destroyed when
     84 *  caca_free_display() is called.
     85 *
     86 *  See also caca_create_display().
     87 *
     88 *  If an error occurs, NULL is returned and \b errno is set accordingly:
     89 *  - \c ENOMEM Not enough memory.
     90 *  - \c ENODEV Graphical device could not be initialised.
     91 *
     92 *  \param cv The cucul canvas or NULL to create a canvas automatically.
     93 *  \param driver A string describing the desired output driver or NULL to
     94 *                choose the best driver automatically.
     95 *  \return The caca graphical context or NULL if an error occurred.
     96 */
     97caca_display_t * caca_create_display_with_driver(cucul_canvas_t *cv,
     98                                                 char const *driver)
    6999{
    70100    caca_display_t *dp = malloc(sizeof(caca_display_t));
     
    96126#endif
    97127
    98     if(caca_select_driver(dp))
     128    if(caca_select_driver(dp, driver))
    99129    {
    100130#if defined(USE_PLUGINS)
     
    155185}
    156186
     187/** \brief Return the current output driver
     188 *
     189 *  Return the given display's current output driver.
     190 *
     191 *  This function never fails.
     192 *
     193 *  \param dp The caca display.
     194 *  \return A static string.
     195 */
     196char const * caca_get_display_driver(caca_display_t *dp)
     197{
     198    return dp->drv.driver;
     199}
     200
    157201/** \brief Detach a caca graphical context from a cucul backend context.
    158202 *
     
    224268 *  \return An array of strings.
    225269 */
    226 char const * const * caca_get_display_driver_list(caca_display_t *dp)
     270char const * const * caca_get_display_driver_list(void)
    227271{
    228272    static char const * const list[] =
     
    270314}
    271315
    272 static int caca_select_driver(caca_display_t *dp)
    273 {
    274 #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
    275     char *var = getenv("CACA_DRIVER");
    276 
     316static int caca_select_driver(caca_display_t *dp, char const *driver)
     317{
     318    char const *var = driver;
     319#if defined(HAVE_GETENV)
     320    if(!var)
     321        var = getenv("CACA_DRIVER");
     322#endif
     323
     324#if defined(HAVE_STRCASECMP)
    277325    /* If the environment variable was set, use it */
    278326    if(var && *var)
     
    340388
    341389#if defined(USE_PLUGINS)
    342 static int caca_plugin_install(char const *name, caca_display_t *dp)
     390static int caca_plugin_install(caca_display_t *dp, char const *driver)
    343391{
    344392    char buf[512];
    345393    int (*sym) (caca_display_t *);
    346394
    347     sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, name);
     395    sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, driver);
    348396    dp->plugin = dlopen(buf, RTLD_NOW);
    349397    if(!dp->plugin)
    350398    {
    351         sprintf(buf, "lib%s_plugin.so", name);
     399        sprintf(buf, "lib%s_plugin.so", driver);
    352400        dp->plugin = dlopen(buf, RTLD_NOW);
    353401        if(!dp->plugin)
     
    355403    }
    356404
    357     sprintf(buf, "%s_install", name);
     405    sprintf(buf, "%s_install", driver);
    358406    sym = dlsym(dp->plugin, buf);
    359407    if(!sym)
Note: See TracChangeset for help on using the changeset viewer.