source: libcaca/trunk/test/event.c @ 331

Last change on this file since 331 was 331, checked in by Sam Hocevar, 17 years ago
  • test/event.c: + Do not refresh after each event, but only when there is no event

pending.

+ If the pressed key is a printable character, display it.

  • src/time.c: + Moved _caca_getticks() to this file.
  • src/caca.c: + Set the escape delay to a very low value in the ncurses driver,

because I don't want escape sequences to be entered manually.

  • src/io.c: + Autorepeat emulation in the ncurses and slang drivers: do not

immediately send the key release event.

  • configure.ac: + Check for usleep. + Improvements in the win32 platform detection.
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1/*
2 *  event         event lister for libcaca
3 *  Copyright (c) 2004 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: event.c 331 2004-01-09 09:51:53Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the GNU Lesser General Public
10 *  License as published by the Free Software Foundation; either
11 *  version 2 of the License, or (at your option) any later version.
12 *
13 *  This program is distributed in the hope that it will be useful,
14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 *  Lesser General Public License for more details.
17 *
18 *  You should have received a copy of the GNU Lesser General Public
19 *  License along with this program; if not, write to the Free Software
20 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 *  02111-1307  USA
22 */
23
24#include "config.h"
25
26#include <stdio.h>
27#include <string.h>
28
29#include "caca.h"
30
31static void print_event(int, int, unsigned int);
32
33int main(int argc, char **argv)
34{
35    int *events;
36    int i, h, quit;
37
38    if(caca_init())
39        return 1;
40
41    caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
42    caca_draw_line(0, 0, caca_get_width() - 1, 0, ' ');
43
44    caca_refresh();
45
46    h = caca_get_height();
47
48    events = malloc(h * sizeof(int));
49    memset(events, 0, h * sizeof(int));
50
51    for(quit = 0; !quit; )
52    {
53        unsigned int event = caca_wait_event(CACA_EVENT_ANY);
54
55        if(!event)
56            continue;
57
58        do
59        {
60            /* q quits */
61            if(event == (CACA_EVENT_KEY_PRESS | 'q'))
62                quit = 1;
63
64            memmove(events + 1, events, (h - 1) * sizeof(int));
65            events[0] = event;
66
67            event = caca_get_event(CACA_EVENT_ANY);
68        }
69        while(event);
70
71        caca_clear();
72
73        /* Print current event */
74        caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
75        caca_draw_line(0, 0, caca_get_width() - 1, 0, ' ');
76        print_event(0, 0, events[0]);
77
78        /* Print previous events */
79        caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLACK);
80        for(i = 1; i < h && events[i]; i++)
81            print_event(0, i, events[i]);
82
83        caca_refresh();
84    }
85
86    /* Clean up */
87    caca_end();
88
89    return 0;
90}
91
92static void print_event(int x, int y, unsigned int event)
93{
94    int character;
95
96    switch(event & 0xff000000)
97    {
98    case CACA_EVENT_NONE:
99        caca_printf(x, y, "CACA_EVENT_NONE");
100        break;
101    case CACA_EVENT_KEY_PRESS:
102        character = event & 0x00ffffff;
103        caca_printf(x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
104                    (character > 0x20 && character < 0x80) ? character : '?');
105        break;
106    case CACA_EVENT_KEY_RELEASE:
107        character = event & 0x00ffffff;
108        caca_printf(x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
109                    (character > 0x20 && character < 0x80) ? character : '?');
110        break;
111    case CACA_EVENT_MOUSE_MOTION:
112        caca_printf(x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
113                    (event & 0x00fff000) >> 12, event & 0x00000fff);
114        break;
115    case CACA_EVENT_MOUSE_PRESS:
116        caca_printf(x, y, "CACA_EVENT_MOUSE_PRESS %u",
117                    event & 0x00ffffff);
118        break;
119    case CACA_EVENT_MOUSE_RELEASE:
120        caca_printf(x, y, "CACA_EVENT_MOUSE_RELEASE %u",
121                    event & 0x00ffffff);
122        break;
123    default:
124        caca_printf(x, y, "CACA_EVENT_UNKNOWN");
125    }
126}
127
Note: See TracBrowser for help on using the repository browser.