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

Last change on this file since 665 was 665, checked in by Sam Hocevar, 16 years ago
  • Changed the cucul_init parameter so that it now directly accepts a canvas size (use cucul_init(0, 0) for the old behaviour).
  • Property svn:keywords set to Id
File size: 4.0 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 665 2006-03-22 17:46:11Z 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 "cucul.h"
21#include "caca.h"
22
23static cucul_t *qq;
24static caca_t *kk;
25
26static void print_event(int, int, unsigned int);
27
28int main(int argc, char **argv)
29{
30    int *events;
31    int i, h, quit;
32
33    qq = cucul_init(0, 0);
34    if(!qq)
35        return 1;
36    kk = caca_attach(qq);
37    if(!kk)
38        return 1;
39
40    h = cucul_get_height(qq) - 1;
41
42    cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
43    cucul_draw_line(qq, 0, 0, cucul_get_width(qq) - 1, 0, ' ');
44
45    cucul_draw_line(qq, 0, h, cucul_get_width(qq) - 1, h, ' ');
46    cucul_putstr(qq, 0, h, "type \"quit\" to exit");
47
48    caca_display(kk);
49
50    events = malloc(h * sizeof(int));
51    memset(events, 0, h * sizeof(int));
52
53    for(quit = 0; quit < 4; )
54    {
55        static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
56        unsigned int event = caca_wait_event(kk, CACA_EVENT_ANY);
57
58        if(!event)
59            continue;
60
61        do
62        {
63            /* "quit" quits */
64            if(event & CACA_EVENT_KEY_PRESS)
65            {
66                int key = event & ~CACA_EVENT_KEY_PRESS;
67                if((key == 'q' && quit == 0) || (key == 'u' && quit == 1)
68                    || (key == 'i' && quit == 2) || (key == 't' && quit == 3))
69                    quit++;
70                else if(key == 'q')
71                    quit = 1;
72                else
73                    quit = 0;
74            }
75
76            memmove(events + 1, events, (h - 1) * sizeof(int));
77            events[0] = event;
78
79            event = caca_get_event(kk, CACA_EVENT_ANY);
80        }
81        while(event);
82
83        cucul_clear(qq);
84
85        /* Print current event */
86        cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
87        cucul_draw_line(qq, 0, 0, cucul_get_width(qq) - 1, 0, ' ');
88        print_event(0, 0, events[0]);
89
90        cucul_draw_line(qq, 0, h, cucul_get_width(qq) - 1, h, ' ');
91        cucul_printf(qq, 0, h, "type \"quit\" to exit: %s", quit_string[quit]);
92
93        /* Print previous events */
94        cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK);
95        for(i = 1; i < h && events[i]; i++)
96            print_event(0, i, events[i]);
97
98        caca_display(kk);
99    }
100
101    /* Clean up */
102    caca_detach(kk);
103    cucul_end(qq);
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        cucul_printf(qq, x, y, "CACA_EVENT_NONE");
116        break;
117    case CACA_EVENT_KEY_PRESS:
118        character = event & 0x00ffffff;
119        cucul_printf(qq, 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        cucul_printf(qq, 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        cucul_printf(qq, x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
129                     (event & 0x00fff000) >> 12, event & 0x00000fff);
130        break;
131    case CACA_EVENT_MOUSE_PRESS:
132        cucul_printf(qq, x, y, "CACA_EVENT_MOUSE_PRESS %u",
133                     event & 0x00ffffff);
134        break;
135    case CACA_EVENT_MOUSE_RELEASE:
136        cucul_printf(qq, x, y, "CACA_EVENT_MOUSE_RELEASE %u",
137                     event & 0x00ffffff);
138        break;
139    case CACA_EVENT_RESIZE:
140        cucul_printf(qq, x, y, "CACA_EVENT_RESIZE");
141        break;
142    default:
143        cucul_printf(qq, x, y, "CACA_EVENT_UNKNOWN");
144    }
145}
146
Note: See TracBrowser for help on using the repository browser.