~ubuntu-branches/ubuntu/raring/libcaca/raring

« back to all changes in this revision

Viewing changes to caca/event.c

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hocevar
  • Date: 2010-02-08 01:40:59 UTC
  • mfrom: (1.1.8 upstream) (4.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100208014059-9q4av8pze8p7uw3i
Tags: 0.99.beta17-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *  libcaca       Colour ASCII-Art library
3
 
 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
 
3
 *  Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
4
4
 *                All Rights Reserved
5
5
 *
6
 
 *  $Id$
7
 
 *
8
6
 *  This library is free software. It comes without any warranty, to
9
7
 *  the extent permitted by applicable law. You can redistribute it
10
8
 *  and/or modify it under the terms of the Do What The Fuck You Want
36
34
/* Start repeating key after AUTOREPEAT_TRIGGER usec and send keypress
37
35
 * events every AUTOREPEAT_RATE usec. */
38
36
#define AUTOREPEAT_TRIGGER 300000
39
 
#define AUTOREPEAT_RATE 100000
 
37
#define AUTOREPEAT_RATE 20000
40
38
#endif
41
39
 
42
40
/** \brief Get the next mouse or keyboard input event.
67
65
{
68
66
    caca_privevent_t privevent;
69
67
    caca_timer_t timer = {0, 0};
70
 
    int usec = 0;
 
68
#if defined PROF
 
69
    caca_timer_t proftimer = {0, 0};
 
70
    int profsys = 0, profwait = 0;
 
71
#endif
 
72
    int ret = 0, usec = 0;
 
73
 
 
74
#if defined PROF
 
75
    _caca_getticks(&proftimer);
 
76
#endif
71
77
 
72
78
    if(!event_mask)
73
 
        return 0;
 
79
        goto end;
74
80
 
75
81
    if(timeout > 0)
76
82
        _caca_getticks(&timer);
77
83
 
78
84
    for( ; ; )
79
85
    {
80
 
        int ret = _get_next_event(dp, &privevent);
 
86
#if defined PROF
 
87
        profwait += _caca_getticks(&proftimer);
 
88
#endif
 
89
        ret = _get_next_event(dp, &privevent);
 
90
#if defined PROF
 
91
        profsys += _caca_getticks(&proftimer);
 
92
#endif
81
93
 
82
94
        /* If we got the event we wanted, return */
83
95
        if(privevent.type & event_mask)
84
96
        {
85
97
            if(ev)
86
98
                memcpy(ev, &privevent, sizeof(privevent));
87
 
            return ret;
 
99
            goto end;
88
100
        }
89
101
 
90
102
        /* If there is no timeout, sleep and try again. */
91
103
        if(timeout < 0)
92
104
        {
93
 
            _caca_sleep(10000);
 
105
            _caca_sleep(1000);
94
106
            continue;
95
107
        }
96
108
 
100
112
            privevent.type = CACA_EVENT_NONE;
101
113
            if(ev)
102
114
                memcpy(ev, &privevent, sizeof(privevent));
103
 
            return 0;
 
115
            ret = 0;
 
116
            goto end;
104
117
        }
105
118
 
106
119
        /* Otherwise sleep a bit. Our granularity is far too high for values
107
120
         * below 10000 microseconds so we cheat a bit. */
108
 
        if(usec > 10000)
109
 
            _caca_sleep(10000);
110
 
        else
111
 
            _caca_sleep(1000);
 
121
        _caca_sleep(usec > 10000 ? 10000 : 1000);
112
122
 
113
123
        usec += _caca_getticks(&timer);
114
124
    }
 
125
 
 
126
end:
 
127
#if defined PROF
 
128
    profwait += _caca_getticks(&proftimer);
 
129
    STAT_IADD(&dp->ev_sys_stat, profsys);
 
130
    STAT_IADD(&dp->ev_wait_stat, profwait);
 
131
#endif
 
132
 
 
133
    return ret;
115
134
}
116
135
 
117
136
/** \brief Return the X mouse coordinate.
229
248
 *  will be undefined. See caca_get_event_type() for more information.
230
249
 *
231
250
 *  \param ev The libcaca event.
 
251
 *  \param utf8 A string buffer with enough bytes to hold the pressed
 
252
 *              key value in UTF-8. Though fewer bytes may be written to
 
253
 *              it, 7 bytes is the minimum safe size.
232
254
 *  \return This function always returns 0.
233
255
 */
234
256
int caca_get_event_key_utf8(caca_event_t const *ev, char *utf8)
245
267
 *  type \c CACA_EVENT_MOUSE_PRESS or \c CACA_EVENT_MOUSE_RELEASE, or the
246
268
 *  results will be undefined. See caca_get_event_type() for more information.
247
269
 *
 
270
 *  This function returns 1 for the left mouse button, 2 for the right mouse
 
271
 *  button, and 3 for the middle mouse button.
 
272
 *
248
273
 *  \param ev The libcaca event.
249
274
 *  \return The event's mouse button.
250
275
 */