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

Last change on this file since 332 was 332, checked in by Sam Hocevar, 17 years ago
  • src/graphics: + Initialise the char buffers in the X11 driver.
  • src/io.c: + caca_get_event() and caca_wait_event() immediately return zero if

event_mask is zero.

+ Added support for the Home, End, PgUp? and PgDown? keys.
+ In the S-Lang driver, added a call to SLang_getkey() before the call to

SLkp_getkey(), otherwise a single Escape call will not be interpreted.

  • test/event.c: + The "quit" sequence now quits the program, instead of "q" alone. + Added a status bar below.
  • Property svn:keywords set to Id
File size: 4.1 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 332 2004-01-10 19:29:12Z 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    h = caca_get_height() - 1;
42
43    caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
44    caca_draw_line(0, 0, caca_get_width() - 1, 0, ' ');
45
46    caca_draw_line(0, h, caca_get_width() - 1, h, ' ');
47    caca_putstr(0, h, "type \"quit\" to exit");
48
49    caca_refresh();
50
51    events = malloc(h * sizeof(int));
52    memset(events, 0, h * sizeof(int));
53
54    for(quit = 0; quit < 4; )
55    {
56        static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
57        unsigned int event = caca_wait_event(CACA_EVENT_ANY);
58
59        if(!event)
60            continue;
61
62        do
63        {
64            /* "quit" quits */
65            if(event & CACA_EVENT_KEY_PRESS)
66            {
67                int key = event & ~CACA_EVENT_KEY_PRESS;
68                if((key == 'q' && quit == 0) || (key == 'u' && quit == 1)
69                    || (key == 'i' && quit == 2) || (key == 't' && quit == 3))
70                    quit++;
71                else if(key == 'q')
72                    quit = 1;
73                else
74                    quit = 0;
75            }
76
77            memmove(events + 1, events, (h - 1) * sizeof(int));
78            events[0] = event;
79
80            event = caca_get_event(CACA_EVENT_ANY);
81        }
82        while(event);
83
84        caca_clear();
85
86        /* Print current event */
87        caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
88        caca_draw_line(0, 0, caca_get_width() - 1, 0, ' ');
89        print_event(0, 0, events[0]);
90
91        caca_draw_line(0, h, caca_get_width() - 1, h, ' ');
92        caca_printf(0, h, "type \"quit\" to exit: %s", quit_string[quit]);
93
94        /* Print previous events */
95        caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLACK);
96        for(i = 1; i < h && events[i]; i++)
97            print_event(0, i, events[i]);
98
99        caca_refresh();
100    }
101
102    /* Clean up */
103    caca_end();
104
105    return 0;
106}
107
108static void print_event(int x, int y, unsigned int event)
109{
110    int character;
111
112    switch(event & 0xff000000)
113    {
114    case CACA_EVENT_NONE:
115        caca_printf(x, y, "CACA_EVENT_NONE");
116        break;
117    case CACA_EVENT_KEY_PRESS:
118        character = event & 0x00ffffff;
119        caca_printf(x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
120                    (character > 0x20 && character < 0x80) ? character : '?');
121        break;
122    case CACA_EVENT_KEY_RELEASE:
123        character = event & 0x00ffffff;
124        caca_printf(x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
125                    (character > 0x20 && character < 0x80) ? character : '?');
126        break;
127    case CACA_EVENT_MOUSE_MOTION:
128        caca_printf(x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
129                    (event & 0x00fff000) >> 12, event & 0x00000fff);
130        break;
131    case CACA_EVENT_MOUSE_PRESS:
132        caca_printf(x, y, "CACA_EVENT_MOUSE_PRESS %u",
133                    event & 0x00ffffff);
134        break;
135    case CACA_EVENT_MOUSE_RELEASE:
136        caca_printf(x, y, "CACA_EVENT_MOUSE_RELEASE %u",
137                    event & 0x00ffffff);
138        break;
139    default:
140        caca_printf(x, y, "CACA_EVENT_UNKNOWN");
141    }
142}
143
Note: See TracBrowser for help on using the repository browser.