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

Last change on this file since 522 was 522, checked in by Sam Hocevar, 16 years ago
  • Changed the licensing to WTFPL, as per all copyright holders' permission.
  • Property svn:keywords set to Id
File size: 3.7 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 522 2006-03-04 00:35:45Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14#include "config.h"
15
16#include <stdio.h>
17#include <string.h>
18#include <stdlib.h>
19
20#include "caca.h"
21
22static void print_event(int, int, unsigned int);
23
24int main(int argc, char **argv)
25{
26    int *events;
27    int i, h, quit;
28
29    if(caca_init())
30        return 1;
31
32    h = caca_get_height() - 1;
33
34    caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
35    caca_draw_line(0, 0, caca_get_width() - 1, 0, ' ');
36
37    caca_draw_line(0, h, caca_get_width() - 1, h, ' ');
38    caca_putstr(0, h, "type \"quit\" to exit");
39
40    caca_refresh();
41
42    events = malloc(h * sizeof(int));
43    memset(events, 0, h * sizeof(int));
44
45    for(quit = 0; quit < 4; )
46    {
47        static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
48        unsigned int event = caca_wait_event(CACA_EVENT_ANY);
49
50        if(!event)
51            continue;
52
53        do
54        {
55            /* "quit" quits */
56            if(event & CACA_EVENT_KEY_PRESS)
57            {
58                int key = event & ~CACA_EVENT_KEY_PRESS;
59                if((key == 'q' && quit == 0) || (key == 'u' && quit == 1)
60                    || (key == 'i' && quit == 2) || (key == 't' && quit == 3))
61                    quit++;
62                else if(key == 'q')
63                    quit = 1;
64                else
65                    quit = 0;
66            }
67
68            memmove(events + 1, events, (h - 1) * sizeof(int));
69            events[0] = event;
70
71            event = caca_get_event(CACA_EVENT_ANY);
72        }
73        while(event);
74
75        caca_clear();
76
77        /* Print current event */
78        caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
79        caca_draw_line(0, 0, caca_get_width() - 1, 0, ' ');
80        print_event(0, 0, events[0]);
81
82        caca_draw_line(0, h, caca_get_width() - 1, h, ' ');
83        caca_printf(0, h, "type \"quit\" to exit: %s", quit_string[quit]);
84
85        /* Print previous events */
86        caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLACK);
87        for(i = 1; i < h && events[i]; i++)
88            print_event(0, i, events[i]);
89
90        caca_refresh();
91    }
92
93    /* Clean up */
94    caca_end();
95
96    return 0;
97}
98
99static void print_event(int x, int y, unsigned int event)
100{
101    int character;
102
103    switch(event & 0xff000000)
104    {
105    case CACA_EVENT_NONE:
106        caca_printf(x, y, "CACA_EVENT_NONE");
107        break;
108    case CACA_EVENT_KEY_PRESS:
109        character = event & 0x00ffffff;
110        caca_printf(x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
111                    (character > 0x20 && character < 0x80) ? character : '?');
112        break;
113    case CACA_EVENT_KEY_RELEASE:
114        character = event & 0x00ffffff;
115        caca_printf(x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
116                    (character > 0x20 && character < 0x80) ? character : '?');
117        break;
118    case CACA_EVENT_MOUSE_MOTION:
119        caca_printf(x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
120                    (event & 0x00fff000) >> 12, event & 0x00000fff);
121        break;
122    case CACA_EVENT_MOUSE_PRESS:
123        caca_printf(x, y, "CACA_EVENT_MOUSE_PRESS %u",
124                    event & 0x00ffffff);
125        break;
126    case CACA_EVENT_MOUSE_RELEASE:
127        caca_printf(x, y, "CACA_EVENT_MOUSE_RELEASE %u",
128                    event & 0x00ffffff);
129        break;
130    case CACA_EVENT_RESIZE:
131        caca_printf(x, y, "CACA_EVENT_RESIZE");
132        break;
133    default:
134        caca_printf(x, y, "CACA_EVENT_UNKNOWN");
135    }
136}
137
Note: See TracBrowser for help on using the repository browser.