Ignore:
Timestamp:
Nov 25, 2007, 12:11:54 PM (13 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 :(
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.