Changeset 2055 for libcaca/trunk


Ignore:
Timestamp:
Nov 25, 2007, 3:11:58 PM (13 years ago)
Author:
Sam Hocevar
Message:
  • Add cucul_manage_canvas() and cucul_unmanage_canvas(). Display drivers no longer need to access the cv->refcount private member and cucul_set_canvas_size() is no longer needed.
Location:
libcaca/trunk
Files:
16 edited

Legend:

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

    r1462 r2055  
    4343#endif
    4444
     45static int caca_can_resize(caca_display_t *);
    4546static int caca_select_driver(caca_display_t *);
    4647#if defined(USE_PLUGINS)
     
    6263 *  \return The caca graphical context or NULL if an error occurred.
    6364 */
    64 caca_display_t * caca_create_display(cucul_canvas_t * cv)
     65caca_display_t * caca_create_display(cucul_canvas_t *cv)
    6566{
    6667    caca_display_t *dp = malloc(sizeof(caca_display_t));
     
    6970    {
    7071        seterrno(ENOMEM);
     72        return NULL;
     73    }
     74
     75    if(cucul_manage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp))
     76    {
     77        free(dp);
     78        seterrno(EBUSY);
    7179        return NULL;
    7280    }
     
    8391            dlclose(dp->plugin);
    8492#endif
     93        cucul_unmanage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp);
    8594        free(dp);
    8695        seterrno(ENODEV);
     
    94103            dlclose(dp->plugin);
    95104#endif
     105        cucul_unmanage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp);
    96106        free(dp);
    97107        seterrno(ENODEV);
    98108        return NULL;
    99109    }
    100 
    101     /* Attached! */
    102     dp->cv->refcount++;
    103110
    104111    /* Graphics stuff */
     
    128135    /* Resize events */
    129136    dp->resize.resized = 0;
     137    dp->resize.allow = 0;
    130138
    131139    return dp;
     
    150158        dlclose(dp->plugin);
    151159#endif
    152     dp->cv->refcount--;
     160    cucul_unmanage_canvas(dp->cv, (int (*)(void *))caca_can_resize, (void *)dp);
    153161    free(dp);
    154162
     
    159167 * XXX: The following functions are local.
    160168 */
     169
     170static int caca_can_resize(caca_display_t *dp)
     171{
     172    return dp->resize.allow;
     173}
    161174
    162175static int caca_select_driver(caca_display_t *dp)
  • libcaca/trunk/caca/caca_internals.h

    r2049 r2055  
    142142    {
    143143        int resized;   /* A resize event was requested */
     144        int allow;     /* The display driver allows resizing */
    144145        unsigned w, h; /* Requested width and height */
    145146    } resize;
  • libcaca/trunk/caca/driver_cocoa.m

    r2040 r2055  
    835835
    836836    unsigned int width = dp->cv->width, height = dp->cv->height;
    837     __cucul_set_canvas_size(dp->cv, width ? width : 80, height ? height : 32);
     837    dp->resize.allow = 1;
     838    cucul_set_canvas_size(dp->cv, width ? width : 80, height ? height : 32);
     839    dp->resize.allow = 0;
    838840
    839841    // first create a full cocoa app if the host has no bundle
  • libcaca/trunk/caca/driver_conio.c

    r2049 r2055  
    5959    /* FIXME */
    6060#   endif
    61     __cucul_set_canvas_size(dp->cv, dp->drv.p->ti.screenwidth,
    62                                     dp->drv.p->ti.screenheight);
     61    dp->resize.allow = 1;
     62    cucul_set_canvas_size(dp->cv, dp->drv.p->ti.screenwidth,
     63                                  dp->drv.p->ti.screenheight);
     64    dp->resize.allow = 0;
     65
    6366    return 0;
    6467}
  • libcaca/trunk/caca/driver_gl.c

    r2049 r2055  
    103103#endif
    104104
    105     __cucul_set_canvas_size(dp->cv, width ? width : 80, height ? height : 32);
     105    dp->resize.allow = 1;
     106    cucul_set_canvas_size(dp->cv, width ? width : 80, height ? height : 32);
     107    dp->resize.allow = 0;
    106108
    107109    /* Load a libcucul internal font */
  • libcaca/trunk/caca/driver_ncurses.c

    r2049 r2055  
    302302        }
    303303
    304     __cucul_set_canvas_size(dp->cv, COLS, LINES);
     304    dp->resize.allow = 1;
     305    cucul_set_canvas_size(dp->cv, COLS, LINES);
     306    dp->resize.allow = 0;
    305307
    306308    return 0;
  • libcaca/trunk/caca/driver_raw.c

    r2049 r2055  
    4141#endif
    4242
    43     __cucul_set_canvas_size(dp->cv, width ? width : 80, height ? height : 24);
     43    dp->resize.allow = 1;
     44    cucul_set_canvas_size(dp->cv, width ? width : 80, height ? height : 24);
     45    dp->resize.allow = 0;
    4446
    4547    return 0;
  • libcaca/trunk/caca/driver_slang.c

    r2049 r2055  
    171171#endif
    172172
    173     __cucul_set_canvas_size(dp->cv, SLtt_Screen_Cols, SLtt_Screen_Rows);
     173    dp->resize.allow = 1;
     174    cucul_set_canvas_size(dp->cv, SLtt_Screen_Cols, SLtt_Screen_Rows);
     175    dp->resize.allow = 0;
    174176
    175177    return 0;
  • libcaca/trunk/caca/driver_vga.c

    r2049 r2055  
    7777
    7878    /* We don't have much choice */
    79     __cucul_set_canvas_size(dp->cv, 80, 25);
     79    dp->resize.allow = 1;
     80    cucul_set_canvas_size(dp->cv, 80, 25);
     81    dp->resize.allow = 0;
    8082
    8183    return 0;
  • libcaca/trunk/caca/driver_win32.c

    r2049 r2055  
    127127        return -1;
    128128
    129     __cucul_set_canvas_size(dp->cv,
    130                             csbi.srWindow.Right - csbi.srWindow.Left + 1,
    131                             csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
     129    dp->resize.allow = 1;
     130    cucul_set_canvas_size(dp->cv,
     131                          csbi.srWindow.Right - csbi.srWindow.Left + 1,
     132                          csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
     133    dp->resize.allow = 0;
    132134
    133135    SetConsoleMode(dp->drv.p->screen, 0);
  • libcaca/trunk/caca/driver_x11.c

    r2049 r2055  
    8989#endif
    9090
    91     __cucul_set_canvas_size(dp->cv, width ? width : 80, height ? height : 32);
     91    dp->resize.allow = 1;
     92    cucul_set_canvas_size(dp->cv, width ? width : 80, height ? height : 32);
     93    dp->resize.allow = 0;
    9294
    9395    dp->drv.p->dpy = XOpenDisplay(NULL);
  • libcaca/trunk/caca/graphics.c

    r2043 r2055  
    239239    /* Tell libcucul we changed size */
    240240    if(dp->resize.w != dp->cv->width || dp->resize.h != dp->cv->height)
    241         __cucul_set_canvas_size(dp->cv, dp->resize.w, dp->resize.h);
     241    {
     242        dp->resize.allow = 1;
     243        cucul_set_canvas_size(dp->cv, dp->resize.w, dp->resize.h);
     244        dp->resize.allow = 0;
     245    }
    242246}
    243247
  • libcaca/trunk/cucul/cucul.c

    r2043 r2055  
    3535#include "cucul_internals.h"
    3636
     37static int cucul_resize(cucul_canvas_t *, unsigned int, unsigned int);
     38
    3739/** \brief Initialise a \e libcucul canvas.
    3840 *
     
    6163    cv->refcount = 0;
    6264    cv->autoinc = 0;
     65    cv->resize_callback = NULL;
     66    cv->resize_data = NULL;
    6367
    6468    cv->frame = 0;
     
    8286    cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
    8387
    84     if(__cucul_set_canvas_size(cv, width, height) < 0)
     88    if(cucul_resize(cv, width, height) < 0)
    8589    {
    8690        int saved_errno = geterrno();
     
    99103}
    100104
     105/** \brief Manage a canvas.
     106 *
     107 *  Lock a canvas to prevent it from being resized. If non-NULL,
     108 *  the \e callback function pointer will be called upon each
     109 *  \e cucul_set_canvas_size call and if the returned value is zero, the
     110 *  canvas resize request will be denied.
     111 *
     112 *  This function is only useful for display drivers such as the \e libcaca
     113 *  library.
     114 *
     115 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     116 *  - \c EBUSY The canvas is already being managed.
     117 *
     118 *  \param cv A libcucul canvas.
     119 *  \param callback An optional callback function pointer.
     120 *  \param p The argument to be passed to \e callback.
     121 *  \return 0 in case of success, -1 if an error occurred.
     122 */
     123int cucul_manage_canvas(cucul_canvas_t *cv, int (*callback)(void *), void *p)
     124{
     125    if(cv->refcount)
     126    {
     127        seterrno(EBUSY);
     128        return -1;
     129    }
     130
     131    cv->refcount = 1;
     132
     133    return 0;
     134}
     135
     136/** \brief Unmanage a canvas.
     137 *
     138 *  Unlock a canvas previously locked by cucul_manage_canvas(). For safety
     139 *  reasons, the callback and callback data arguments must be the same as for
     140 *  the cucul_manage_canvas() call.
     141 *
     142 *  This function is only useful for display drivers such as the \e libcaca
     143 *  library.
     144 *
     145 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     146 *  - \c EINVAL The canvas is not managed, or the callback arguments do
     147 *              not match.
     148 *
     149 *  \param cv A libcucul canvas.
     150 *  \param callback The \e callback argument previously passed to
     151                    cucul_manage_canvas().
     152 *  \param p The \e p argument previously passed to cucul_manage_canvas().
     153 *  \return 0 in case of success, -1 if an error occurred.
     154 */
     155int cucul_unmanage_canvas(cucul_canvas_t *cv, int (*callback)(void *), void *p)
     156{
     157    if(!cv->refcount
     158        || cv->resize_callback != callback || cv->resize_data != p)
     159    {
     160        seterrno(EINVAL);
     161        return -1;
     162    }
     163
     164    cv->refcount = 0;
     165
     166    return 0;
     167}
     168
    101169/** \brief Resize a canvas.
    102170 *
     
    124192 *    happens, the canvas handle becomes invalid and should not be used.
    125193 *
    126  *  \param cv A libcucul canvas
    127  *  \param width The desired canvas width
    128  *  \param height The desired canvas height
     194 *  \param cv A libcucul canvas.
     195 *  \param width The desired canvas width.
     196 *  \param height The desired canvas height.
    129197 *  \return 0 in case of success, -1 if an error occurred.
    130198 */
     
    132200                                              unsigned int height)
    133201{
    134     if(cv->refcount)
     202    if(cv->refcount && cv->resize_callback
     203        && !cv->resize_callback(cv->resize_data))
    135204    {
    136205        seterrno(EBUSY);
     
    138207    }
    139208
    140     return __cucul_set_canvas_size(cv, width, height);
     209    return cucul_resize(cv, width, height);
    141210}
    142211
     
    147216 *  This function never fails.
    148217 *
    149  *  \param cv A libcucul canvas
     218 *  \param cv A libcucul canvas.
    150219 *  \return The canvas width.
    151220 */
     
    161230 *  This function never fails.
    162231 *
    163  *  \param cv A libcucul canvas
     232 *  \param cv A libcucul canvas.
    164233 *  \return The canvas height.
    165234 */
     
    178247 *  - \c EBUSY The canvas is in use by a display driver and cannot be freed.
    179248 *
    180  *  \param cv A libcucul canvas
     249 *  \param cv A libcucul canvas.
    181250 *  \return 0 in case of success, -1 if an error occurred.
    182251 */
     
    232301 */
    233302
    234 int __cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
    235                                                 unsigned int height)
     303int cucul_resize(cucul_canvas_t *cv, unsigned int width, unsigned int height)
    236304{
    237305    unsigned int x, y, f, old_width, old_height, new_size, old_size;
  • libcaca/trunk/cucul/cucul.h

    r2043 r2055  
    8585 *  @{ */
    8686__extern cucul_canvas_t * cucul_create_canvas(unsigned int, unsigned int);
     87__extern int cucul_manage_canvas(cucul_canvas_t *, int (*)(void *), void *);
     88__extern int cucul_unmanage_canvas(cucul_canvas_t *, int (*)(void *), void *);
    8789__extern int cucul_set_canvas_size(cucul_canvas_t *, unsigned int,
    8890                                   unsigned int);
  • libcaca/trunk/cucul/cucul_internals.h

    r2040 r2055  
    4444    struct cucul_frame *frames;
    4545
     46    /* Canvas management */
    4647    unsigned int refcount;
    4748    unsigned int autoinc;
     49    int (*resize_callback)(void *);
     50    void *resize_data;
    4851
    4952    /* Shortcut to the active frame information */
     
    6164};
    6265
    63 /* Canvas functions */
    64 extern int __cucul_set_canvas_size(cucul_canvas_t *,
    65                                    unsigned int, unsigned int);
    66 
    6766/* Colour functions */
    6867extern uint16_t _cucul_attr_to_rgb12fg(uint32_t);
  • libcaca/trunk/msvc/libcucul.def

    r2040 r2055  
    1 LIBRARY "libcucul"
    2 
    3 EXPORTS
    4     __cucul_set_canvas_size ; Private symbol used by libcaca
Note: See TracChangeset for help on using the changeset viewer.