Ignore:
Timestamp:
Nov 25, 2007, 12:11:54 PM (12 years ago)
Author:
Sam Hocevar
Message:
  • Made the caca_event_t structure opaque and created a whole bunch of functions to access its real data. This is a big API change that will break your software, sorry :(
Location:
libcaca/trunk/caca
Files:
12 edited

Legend:

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

    r2043 r2049  
    5454struct caca_event
    5555{
    56     /** \brief User event type enumeration.
    57      *
    58      *  This enum serves two purposes:
    59      *  - Build listening masks for caca_get_event().
    60      *  - Define the type of a \e caca_event_t.
    61      */
    62     enum caca_event_type
    63     {
    64         CACA_EVENT_NONE =          0x0000, /**< No event. */
     56    unsigned char opaque_structure[32];
     57};
    6558
    66         CACA_EVENT_KEY_PRESS =     0x0001, /**< A key was pressed. */
    67         CACA_EVENT_KEY_RELEASE =   0x0002, /**< A key was released. */
    68         CACA_EVENT_MOUSE_PRESS =   0x0004, /**< A mouse button was pressed. */
    69         CACA_EVENT_MOUSE_RELEASE = 0x0008, /**< A mouse button was released. */
    70         CACA_EVENT_MOUSE_MOTION =  0x0010, /**< The mouse was moved. */
    71         CACA_EVENT_RESIZE =        0x0020, /**< The window was resized. */
    72         CACA_EVENT_QUIT =          0x0040, /**< The user requested to quit. */
     59/** \brief User event type enumeration.
     60 *
     61 *  This enum serves two purposes:
     62 *  - Build listening masks for caca_get_event().
     63 *  - Define the type of a \e caca_event_t.
     64 */
     65enum caca_event_type
     66{
     67    CACA_EVENT_NONE =          0x0000, /**< No event. */
    7368
    74         CACA_EVENT_ANY =           0xffff  /**< Bitmask for any event. */
    75     }
    76     /** \brief User event type member.
    77      *
    78      *  This field is always valid when caca_get_event() returns.
    79     */
    80     type;
     69    CACA_EVENT_KEY_PRESS =     0x0001, /**< A key was pressed. */
     70    CACA_EVENT_KEY_RELEASE =   0x0002, /**< A key was released. */
     71    CACA_EVENT_MOUSE_PRESS =   0x0004, /**< A mouse button was pressed. */
     72    CACA_EVENT_MOUSE_RELEASE = 0x0008, /**< A mouse button was released. */
     73    CACA_EVENT_MOUSE_MOTION =  0x0010, /**< The mouse was moved. */
     74    CACA_EVENT_RESIZE =        0x0020, /**< The window was resized. */
     75    CACA_EVENT_QUIT =          0x0040, /**< The user requested to quit. */
    8176
    82     /** \brief User event data member.
    83      *
    84      *  The validity of the \e data union depends on the value of the \e type
    85      *  field:
    86      *  - \c CACA_EVENT_NONE: no other field is valid.
    87      *  - \c CACA_EVENT_KEY_PRESS, \c CACA_EVENT_KEY_RELEASE: the
    88      *  \e data.key.ch field is valid and contains either the ASCII value for
    89      *  the key, or an \e enum \e caca_key value. If the value is a printable
    90      *  ASCII character, the \e data.key.utf32 and \e data.key.utf8 fields are
    91      *  also filled and contain respectively the UTF-32/UCS-4 and the UTF-8
    92      *  representations of the character. Otherwise, their content is
    93      *  undefined.
    94      *  - \c CACA_EVENT_MOUSE_PRESS, \c CACA_EVENT_MOUSE_RELEASE: the
    95      *  \e data.mouse.button field is valid and contains the index of the
    96      *  mouse button that was pressed.
    97      *  - \c CACA_EVENT_MOUSE_MOTION: the \e data.mouse.x and \e data.mouse.y
    98      *  fields are valid and contain the mouse coordinates in character
    99      *  cells.
    100      *  - \c CACA_EVENT_RESIZE: the \e data.resize.w and \e data.resize.h
    101      *  fields are valid and contain the new width and height values of
    102      *  the \e libcucul canvas attached to \e libcaca.
    103      *  - \c CACA_EVENT_QUIT: no other field is valid.
    104      *
    105      *  The result of accessing data members outside the above conditions is
    106      *  undefined.
    107      */
    108     union
    109     {
    110         struct { unsigned int x, y, button; } mouse;
    111         struct { unsigned int w, h; } resize;
    112         struct { unsigned int ch; unsigned long int utf32; char utf8[8]; } key;
    113     } data;
     77    CACA_EVENT_ANY =           0xffff  /**< Bitmask for any event. */
    11478};
    11579
     
    197161__extern unsigned int caca_get_display_height(caca_display_t const *);
    198162__extern int caca_set_display_title(caca_display_t *, char const *);
     163__extern int caca_set_mouse(caca_display_t *, int);
     164__extern int caca_set_cursor(caca_display_t *, int);
    199165/*  @} */
    200166
     
    209175__extern unsigned int caca_get_mouse_x(caca_display_t const *);
    210176__extern unsigned int caca_get_mouse_y(caca_display_t const *);
    211 __extern int caca_set_mouse(caca_display_t *, int);
    212 __extern int caca_set_cursor(caca_display_t *, int);
     177__extern enum caca_event_type caca_get_event_type(caca_event_t const *);
     178__extern unsigned int caca_get_event_key_ch(caca_event_t const *);
     179__extern unsigned long int caca_get_event_key_utf32(caca_event_t const *);
     180__extern int caca_get_event_key_utf8(caca_event_t const *, char *);
     181__extern unsigned int caca_get_event_mouse_button(caca_event_t const *);
     182__extern unsigned int caca_get_event_mouse_x(caca_event_t const *);
     183__extern unsigned int caca_get_event_mouse_y(caca_event_t const *);
     184__extern unsigned int caca_get_event_resize_width(caca_event_t const *);
     185__extern unsigned int caca_get_event_resize_height(caca_event_t const *);
    213186/*  @} */
    214187
  • libcaca/trunk/caca/caca0.c

    r1882 r2049  
    8585        return 0x00000000;
    8686
    87     switch(ev.type)
     87    switch(caca_get_event_type(&ev))
    8888    {
    8989        case CACA_EVENT_KEY_PRESS:
    90             return 0x01000000 | ev.data.key.ch;
     90            return 0x01000000 | caca_get_event_key_ch(&ev);
    9191        case CACA_EVENT_KEY_RELEASE:
    92             return 0x02000000 | ev.data.key.ch;
     92            return 0x02000000 | caca_get_event_key_ch(&ev);
    9393        case CACA_EVENT_MOUSE_PRESS:
    94             return 0x04000000 | ev.data.mouse.button;
     94            return 0x04000000 | caca_get_event_mouse_button(&ev);
    9595        case CACA_EVENT_MOUSE_RELEASE:
    96             return 0x08000000 | ev.data.mouse.button;
     96            return 0x08000000 | caca_get_event_mouse_button(&ev);
    9797        case CACA_EVENT_MOUSE_MOTION:
    98             return 0x10000000 | ((ev.data.mouse.x & 0xfff) << 12)
    99                               | (ev.data.mouse.y & 0xfff);
     98            return 0x10000000 | ((caca_get_event_mouse_x(&ev) & 0xfff) << 12)
     99                              | (caca_get_event_mouse_y(&ev) & 0xfff);
    100100        case CACA_EVENT_RESIZE:
    101101            return 0x20000000;
  • libcaca/trunk/caca/caca_internals.h

    r2043 r2049  
    2121
    2222typedef struct caca_timer caca_timer_t;
     23typedef struct caca_privevent caca_privevent_t;
    2324
    2425#if !defined(_DOXYGEN_SKIP_ME)
     
    9091};
    9192
     93/* Private event structure */
     94struct caca_privevent
     95{
     96    enum caca_event_type type;
     97
     98    union
     99    {
     100        struct { unsigned int x, y, button; } mouse;
     101        struct { unsigned int w, h; } resize;
     102        struct { unsigned int ch; unsigned long int utf32; char utf8[8]; } key;
     103    } data;
     104};
     105
    92106/* Internal caca display context */
    93107struct caca_display
     
    113127        void (* display) (caca_display_t *);
    114128        void (* handle_resize) (caca_display_t *);
    115         int (* get_event) (caca_display_t *, caca_event_t *);
     129        int (* get_event) (caca_display_t *, caca_privevent_t *);
    116130        void (* set_mouse) (caca_display_t *, int);
    117131        void (* set_cursor) (caca_display_t *, int);
     
    139153    {
    140154#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
    141         caca_event_t buf[EVENTBUF_LEN];
     155        caca_privevent_t buf[EVENTBUF_LEN];
    142156        int queue;
    143157#endif
     
    146160        unsigned int last_key_ticks;
    147161        unsigned int autorepeat_ticks;
    148         caca_event_t last_key_event;
     162        caca_privevent_t last_key_event;
    149163#endif
    150164#if defined(USE_WIN32)
     
    161175extern void _caca_handle_resize(caca_display_t *);
    162176#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
    163 extern void _push_event(caca_display_t *, caca_event_t *);
    164 extern int _pop_event(caca_display_t *, caca_event_t *);
     177extern void _push_event(caca_display_t *, caca_privevent_t *);
     178extern int _pop_event(caca_display_t *, caca_privevent_t *);
    165179#endif
    166180
  • libcaca/trunk/caca/driver_conio.c

    r2043 r2049  
    131131}
    132132
    133 static int conio_get_event(caca_display_t *dp, caca_event_t *ev)
     133static int conio_get_event(caca_display_t *dp, caca_privevent_t *ev)
    134134{
    135135    unsigned char ch;
    136     caca_event_t release;
     136    caca_privevent_t release;
    137137
    138138    if(!_conio_kbhit())
  • libcaca/trunk/caca/driver_gl.c

    r2043 r2049  
    323323}
    324324
    325 static int gl_get_event(caca_display_t *dp, caca_event_t *ev)
     325static int gl_get_event(caca_display_t *dp, caca_privevent_t *ev)
    326326{
    327327#ifdef HAVE_GLUTCHECKLOOP
  • libcaca/trunk/caca/driver_ncurses.c

    r2043 r2049  
    385385}
    386386
    387 static int ncurses_get_event(caca_display_t *dp, caca_event_t *ev)
     387static int ncurses_get_event(caca_display_t *dp, caca_privevent_t *ev)
    388388{
    389389    int intkey;
  • libcaca/trunk/caca/driver_raw.c

    r2043 r2049  
    8484}
    8585
    86 static int raw_get_event(caca_display_t *dp, caca_event_t *ev)
     86static int raw_get_event(caca_display_t *dp, caca_privevent_t *ev)
    8787{
    8888    ev->type = CACA_EVENT_NONE;
  • libcaca/trunk/caca/driver_slang.c

    r2043 r2049  
    273273}
    274274
    275 static int slang_get_event(caca_display_t *dp, caca_event_t *ev)
     275static int slang_get_event(caca_display_t *dp, caca_privevent_t *ev)
    276276{
    277277    int intkey;
  • libcaca/trunk/caca/driver_vga.c

    r2043 r2049  
    144144}
    145145
    146 static int vga_get_event(caca_display_t *dp, caca_event_t *ev)
     146static int vga_get_event(caca_display_t *dp, caca_privevent_t *ev)
    147147{
    148148    /* FIXME */
  • libcaca/trunk/caca/driver_win32.c

    r2043 r2049  
    245245}
    246246
    247 static int win32_get_event(caca_display_t *dp, caca_event_t *ev)
     247static int win32_get_event(caca_display_t *dp, caca_privevent_t *ev)
    248248{
    249249    INPUT_RECORD rec;
  • libcaca/trunk/caca/driver_x11.c

    r2043 r2049  
    370370}
    371371
    372 static int x11_get_event(caca_display_t *dp, caca_event_t *ev)
     372static int x11_get_event(caca_display_t *dp, caca_privevent_t *ev)
    373373{
    374374    XEvent xevent;
  • libcaca/trunk/caca/event.c

    r2043 r2049  
    2222#if !defined(__KERNEL__)
    2323#   include <stdio.h>
     24#   include <string.h>
    2425#endif
    2526
     
    2930#include "caca_internals.h"
    3031
    31 static int _get_next_event(caca_display_t *, caca_event_t *);
    32 static int _lowlevel_event(caca_display_t *, caca_event_t *);
     32static int _get_next_event(caca_display_t *, caca_privevent_t *);
     33static int _lowlevel_event(caca_display_t *, caca_privevent_t *);
    3334
    3435#if !defined(_DOXYGEN_SKIP_ME)
     
    6869                   caca_event_t *ev, int timeout)
    6970{
    70     caca_event_t dummy_event;
     71    caca_privevent_t privevent;
    7172    caca_timer_t timer;
    7273    int usec = 0;
     
    7879        _caca_getticks(&timer);
    7980
    80     if(ev == NULL)
    81         ev = &dummy_event;
    82 
    8381    for( ; ; )
    8482    {
    85         int ret = _get_next_event(dp, ev);
     83        int ret = _get_next_event(dp, &privevent);
    8684
    8785        /* If we got the event we wanted, return */
    88         if(ev->type & event_mask)
     86        if(privevent.type & event_mask)
     87        {
     88            if(ev)
     89                memcpy(ev, &privevent, sizeof(privevent));
    8990            return ret;
     91        }
    9092
    9193        /* If there is no timeout, sleep and try again. */
     
    99101        if(usec >= timeout)
    100102        {
    101             ev->type = CACA_EVENT_NONE;
     103            privevent.type = CACA_EVENT_NONE;
     104            if(ev)
     105                memcpy(ev, &privevent, sizeof(privevent));
    102106            return 0;
    103107        }
     
    154158}
    155159
     160/** \brief Return an event's type.
     161 *
     162 *  Return the type of an event. This function may always be called on an
     163 *  event after caca_get_event() was called, and its return value indicates
     164 *  which other functions may be called:
     165 *  - \c CACA_EVENT_NONE: no other function may be called.
     166 *  - \c CACA_EVENT_KEY_PRESS, \c CACA_EVENT_KEY_RELEASE:
     167 *  caca_get_event_key_ch(), caca_get_event_key_utf32() and
     168 *  caca_get_event_key_utf8() may be called.
     169 *  - \c CACA_EVENT_MOUSE_PRESS, \c CACA_EVENT_MOUSE_RELEASE:
     170 *  caca_get_event_mouse_button() may be called.
     171 *  - \c CACA_EVENT_MOUSE_MOTION: caca_get_event_mouse_x() and
     172 *  caca_get_event_mouse_y() may be called.
     173 *  - \c CACA_EVENT_RESIZE: caca_get_event_resize_width() and
     174 *  caca_get_event_resize_height() may be called.
     175 *  - \c CACA_EVENT_QUIT: no other function may be called.
     176 *
     177 *  This function never fails.
     178 *
     179 *  \param ev The libcaca event.
     180 *  \return The event's type.
     181 */
     182enum caca_event_type caca_get_event_type(caca_event_t const *ev)
     183{
     184    return ((caca_privevent_t const *)ev)->type;
     185}
     186
     187/** \brief Return a key press or key release event's value
     188 *
     189 *  Return either the ASCII value for an event's key, or if the key is not
     190 *  an ASCII character, an appropriate \e enum \e caca_key value.
     191 *
     192 *  This function never fails, but must only be called with a valid event of
     193 *  type \c CACA_EVENT_KEY_PRESS or \c CACA_EVENT_KEY_RELEASE, or the results
     194 *  will be undefined. See caca_get_event_type() for more information.
     195 *
     196 *  \param ev The libcaca event.
     197 *  \return The key value.
     198 */
     199unsigned int caca_get_event_key_ch(caca_event_t const *ev)
     200{
     201    return ((caca_privevent_t const *)ev)->data.key.ch;
     202}
     203
     204/** \brief Return a key press or key release event's Unicode value
     205 *
     206 *  Return the UTF-32/UCS-4 value for an event's key if it resolves to a
     207 *  printable character.
     208 *
     209 *  This function never fails, but must only be called with a valid event of
     210 *  type \c CACA_EVENT_KEY_PRESS or \c CACA_EVENT_KEY_RELEASE, or the results
     211 *  will be undefined. See caca_get_event_type() for more information.
     212 *
     213 *  \param ev The libcaca event.
     214 *  \return The key's Unicode value.
     215 */
     216unsigned long int caca_get_event_key_utf32(caca_event_t const *ev)
     217{
     218    return ((caca_privevent_t const *)ev)->data.key.utf32;
     219}
     220
     221/** \brief Return a key press or key release event's UTF-8 value
     222 *
     223 *  Write the UTF-8 value for an event's key if it resolves to a printable
     224 *  character. Up to 6 UTF-8 bytes and a null termination are written.
     225 *
     226 *  This function never fails, but must only be called with a valid event of
     227 *  type \c CACA_EVENT_KEY_PRESS or \c CACA_EVENT_KEY_RELEASE, or the results
     228 *  will be undefined. See caca_get_event_type() for more information.
     229 *
     230 *  \param ev The libcaca event.
     231 *  \return This function always returns 0.
     232 */
     233int caca_get_event_key_utf8(caca_event_t const *ev, char *utf8)
     234{
     235    memcpy(utf8, ((caca_privevent_t const *)ev)->data.key.utf8, 8);
     236    return 0;
     237}
     238
     239/** \brief Return a mouse press or mouse release event's button
     240 *
     241 *  Return the mouse button index for an event.
     242 *
     243 *  This function never fails, but must only be called with a valid event of
     244 *  type \c CACA_EVENT_MOUSE_PRESS or \c CACA_EVENT_MOUSE_RELEASE, or the
     245 *  results will be undefined. See caca_get_event_type() for more information.
     246 *
     247 *  \param ev The libcaca event.
     248 *  \return The event's mouse button.
     249 */
     250unsigned int caca_get_event_mouse_button(caca_event_t const *ev)
     251{
     252    return ((caca_privevent_t const *)ev)->data.mouse.button;
     253}
     254
     255/** \brief Return a mouse motion event's X coordinate.
     256 *
     257 *  Return the X coordinate for a mouse motion event.
     258 *
     259 *  This function never fails, but must only be called with a valid event of
     260 *  type \c CACA_EVENT_MOUSE_MOTION, or the results will be undefined. See
     261 *  caca_get_event_type() for more information.
     262 *
     263 *  \param ev The libcaca event.
     264 *  \return The event's X mouse coordinate.
     265 */
     266unsigned int caca_get_event_mouse_x(caca_event_t const *ev)
     267{
     268    return ((caca_privevent_t const *)ev)->data.mouse.x;
     269}
     270
     271/** \brief Return a mouse motion event's Y coordinate.
     272 *
     273 *  Return the Y coordinate for a mouse motion event.
     274 *
     275 *  This function never fails, but must only be called with a valid event of
     276 *  type \c CACA_EVENT_MOUSE_MOTION, or the results will be undefined. See
     277 *  caca_get_event_type() for more information.
     278 *
     279 *  \param ev The libcaca event.
     280 *  \return The event's Y mouse coordinate.
     281 */
     282unsigned int caca_get_event_mouse_y(caca_event_t const *ev)
     283{
     284    return ((caca_privevent_t const *)ev)->data.mouse.y;
     285}
     286
     287/** \brief Return a resize event's display width value.
     288 *
     289 *  Return the width value for a display resize event.
     290 *
     291 *  This function never fails, but must only be called with a valid event of
     292 *  type \c CACA_EVENT_RESIZE, or the results will be undefined. See
     293 *  caca_get_event_type() for more information.
     294 *
     295 *  \param ev The libcaca event.
     296 *  \return The event's new display width value.
     297 */
     298unsigned int caca_get_event_resize_width(caca_event_t const *ev)
     299{
     300    return ((caca_privevent_t const *)ev)->data.resize.w;
     301}
     302
     303/** \brief Return a resize event's display height value.
     304 *
     305 *  Return the height value for a display resize event.
     306 *
     307 *  This function never fails, but must only be called with a valid event of
     308 *  type \c CACA_EVENT_RESIZE, or the results will be undefined. See
     309 *  caca_get_event_type() for more information.
     310 *
     311 *  \param ev The libcaca event.
     312 *  \return The event's new display height value.
     313 */
     314unsigned int caca_get_event_resize_height(caca_event_t const *ev)
     315{
     316    return ((caca_privevent_t const *)ev)->data.resize.h;
     317}
     318
    156319/*
    157320 * XXX: The following functions are local.
    158321 */
    159322
    160 static int _get_next_event(caca_display_t *dp, caca_event_t *ev)
     323static int _get_next_event(caca_display_t *dp, caca_privevent_t *ev)
    161324{
    162325#if defined(USE_SLANG) || defined(USE_NCURSES)
     
    240403}
    241404
    242 static int _lowlevel_event(caca_display_t *dp, caca_event_t *ev)
     405static int _lowlevel_event(caca_display_t *dp, caca_privevent_t *ev)
    243406{
    244407#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
     
    253416
    254417#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
    255 void _push_event(caca_display_t *dp, caca_event_t *ev)
     418void _push_event(caca_display_t *dp, caca_privevent_t *ev)
    256419{
    257420    if(!ev->type || dp->events.queue == EVENTBUF_LEN)
     
    261424}
    262425
    263 int _pop_event(caca_display_t *dp, caca_event_t *ev)
     426int _pop_event(caca_display_t *dp, caca_privevent_t *ev)
    264427{
    265428    int i;
Note: See TracChangeset for help on using the changeset viewer.