Ignore:
Timestamp:
Dec 16, 2007, 2:50:41 AM (12 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().
Location:
libcaca/trunk/caca
Files:
13 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)
  • libcaca/trunk/caca/caca.h

    r2135 r2138  
    155155 *  @{ */
    156156__extern caca_display_t * caca_create_display(cucul_canvas_t *);
     157__extern caca_display_t * caca_create_display_with_driver(cucul_canvas_t *,
     158                                                          char const *);
     159__extern char const * caca_get_display_driver(caca_display_t *);
    157160__extern int caca_free_display(caca_display_t *);
    158161__extern cucul_canvas_t * caca_get_canvas(caca_display_t *);
    159162__extern int caca_refresh_display(caca_display_t *);
    160 __extern char const * const * caca_get_display_driver_list(caca_display_t *);
     163__extern char const * const * caca_get_display_driver_list(void);
    161164__extern int caca_set_display_time(caca_display_t *, unsigned int);
    162165__extern unsigned int caca_get_display_time(caca_display_t const *);
  • libcaca/trunk/caca/caca_internals.h

    r2061 r2138  
    118118    struct drv
    119119    {
    120         enum caca_driver driver;
     120        char const * driver;
     121        enum caca_driver id;
    121122        struct driver_private *p;
    122123
  • libcaca/trunk/caca/driver_cocoa.m

    r2129 r2138  
    998998int cocoa_install(caca_display_t *dp)
    999999{
    1000     dp->drv.driver = CACA_DRIVER_RAW;
     1000    dp->drv.id = CACA_DRIVER_COCOA;
     1001    dp->drv.driver = "cocoa";
    10011002
    10021003    dp->drv.init_graphics = cocoa_init_graphics;
  • libcaca/trunk/caca/driver_conio.c

    r2056 r2138  
    167167int conio_install(caca_display_t *dp)
    168168{
    169     dp->drv.driver = CACA_DRIVER_CONIO;
     169    dp->drv.id = CACA_DRIVER_CONIO;
     170    dp->drv.driver = "conio";
    170171
    171172    dp->drv.init_graphics = conio_init_graphics;
  • libcaca/trunk/caca/driver_gl.c

    r2057 r2138  
    598598#endif
    599599
    600     dp->drv.driver = CACA_DRIVER_GL;
     600    dp->drv.id = CACA_DRIVER_GL;
     601    dp->drv.driver = "gl";
    601602
    602603    dp->drv.init_graphics = gl_init_graphics;
  • libcaca/trunk/caca/driver_ncurses.c

    r2056 r2138  
    809809int ncurses_install(caca_display_t *dp)
    810810{
    811     dp->drv.driver = CACA_DRIVER_NCURSES;
     811    dp->drv.id = CACA_DRIVER_NCURSES;
     812    dp->drv.driver = "ncurses";
    812813
    813814    dp->drv.init_graphics = ncurses_init_graphics;
  • libcaca/trunk/caca/driver_raw.c

    r2056 r2138  
    9898int raw_install(caca_display_t *dp)
    9999{
    100     dp->drv.driver = CACA_DRIVER_RAW;
     100    dp->drv.id = CACA_DRIVER_RAW;
     101    dp->drv.driver = "raw";
    101102
    102103    dp->drv.init_graphics = raw_init_graphics;
  • libcaca/trunk/caca/driver_slang.c

    r2056 r2138  
    523523int slang_install(caca_display_t *dp)
    524524{
    525     dp->drv.driver = CACA_DRIVER_SLANG;
     525    dp->drv.id = CACA_DRIVER_SLANG;
     526    dp->drv.driver = "slang";
    526527
    527528    dp->drv.init_graphics = slang_init_graphics;
  • libcaca/trunk/caca/driver_vga.c

    r2056 r2138  
    160160int vga_install(caca_display_t *dp)
    161161{
    162     dp->drv.driver = CACA_DRIVER_VGA;
     162    dp->drv.id = CACA_DRIVER_VGA;
     163    dp->drv.driver = "vga";
    163164
    164165    dp->drv.init_graphics = vga_init_graphics;
  • libcaca/trunk/caca/driver_win32.c

    r2056 r2138  
    350350int win32_install(caca_display_t *dp)
    351351{
    352     dp->drv.driver = CACA_DRIVER_WIN32;
     352    dp->drv.id = CACA_DRIVER_WIN32;
     353    dp->drv.driver = "win32";
    353354
    354355    dp->drv.init_graphics = win32_init_graphics;
  • libcaca/trunk/caca/driver_x11.c

    r2057 r2138  
    790790#endif
    791791
    792     dp->drv.driver = CACA_DRIVER_X11;
     792    dp->drv.id = CACA_DRIVER_X11;
     793    dp->drv.driver = "x11";
    793794
    794795    dp->drv.init_graphics = x11_init_graphics;
  • libcaca/trunk/caca/event.c

    r2056 r2138  
    345345
    346346#if defined(USE_SLANG)
    347     if(dp->drv.driver != CACA_DRIVER_SLANG)
     347    if(dp->drv.id != CACA_DRIVER_SLANG)
    348348#endif
    349349#if defined(USE_NCURSES)
    350     if(dp->drv.driver != CACA_DRIVER_NCURSES)
     350    if(dp->drv.id != CACA_DRIVER_NCURSES)
    351351#endif
    352352    return ret;
Note: See TracChangeset for help on using the changeset viewer.