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

Last change on this file since 2049 was 2049, checked in by Sam Hocevar, 12 years ago
  • Made the caca_event_t structure opaque and created a whole bunch of functions to access its real data. This is a big API change that will break your software, sorry :(
  • Property svn:keywords set to Id
File size: 4.8 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 2049 2007-11-25 11:11:54Z sam $
7 *
8 *  This program is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15#include "config.h"
16#include "common.h"
17#if !defined(__KERNEL__)
18#   include <stdio.h>
19#   include <string.h>
20#   include <stdlib.h>
21#endif
22
23#include "cucul.h"
24#include "caca.h"
25
26static cucul_canvas_t *cv;
27static caca_display_t *dp;
28
29static void print_event(int, int, caca_event_t *);
30
31int main(int argc, char **argv)
32{
33    caca_event_t *events;
34    int i, h, quit;
35
36    cv = cucul_create_canvas(80, 24);
37    if(cv == NULL)
38    {
39        printf("Failed to create canvas\n");
40        return 1;
41    }
42
43    dp = caca_create_display(cv);
44    if(dp == NULL)
45    {
46        printf("Failed to create display\n");
47        return 1;
48    }
49
50    h = cucul_get_canvas_height(cv) - 1;
51
52    cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
53    cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
54
55    cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, ' ');
56    cucul_put_str(cv, 0, h, "type \"quit\" to exit");
57
58    caca_refresh_display(dp);
59
60    events = malloc(h * sizeof(caca_event_t));
61    memset(events, 0, h * sizeof(caca_event_t));
62
63    for(quit = 0; quit < 4; )
64    {
65        caca_event_t ev;
66        static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
67        int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, -1);
68
69        if(!ret)
70            continue;
71
72        do
73        {
74            /* "quit" quits */
75            if(caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
76            {
77                int key = caca_get_event_key_ch(&ev);
78                if((key == 'q' && quit == 0) || (key == 'u' && quit == 1)
79                    || (key == 'i' && quit == 2) || (key == 't' && quit == 3))
80                    quit++;
81                else if(key == 'q')
82                    quit = 1;
83                else
84                    quit = 0;
85            }
86
87            memmove(events + 1, events, (h - 1) * sizeof(caca_event_t));
88            events[0] = ev;
89
90            ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
91        }
92        while(ret);
93
94        cucul_set_color_ansi(cv, CUCUL_LIGHTGRAY, CUCUL_BLACK);
95        cucul_clear_canvas(cv);
96
97        /* Print current event */
98        cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
99        cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
100        print_event(0, 0, events);
101
102        cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, ' ');
103        cucul_printf(cv, 0, h, "type \"quit\" to exit: %s", quit_string[quit]);
104
105        /* Print previous events */
106        cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLACK);
107        for(i = 1; i < h && caca_get_event_type(&events[i]); i++)
108            print_event(0, i, events + i);
109
110        caca_refresh_display(dp);
111    }
112
113    /* Clean up */
114    caca_free_display(dp);
115    cucul_free_canvas(cv);
116
117    return 0;
118}
119
120static void print_event(int x, int y, caca_event_t *ev)
121{
122    int character;
123
124    switch(caca_get_event_type(ev))
125    {
126    case CACA_EVENT_NONE:
127        cucul_printf(cv, x, y, "CACA_EVENT_NONE");
128        break;
129    case CACA_EVENT_KEY_PRESS:
130        character = caca_get_event_key_ch(ev);
131        cucul_printf(cv, x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
132                     (character > 0x1f && character < 0x80) ? character : '?');
133        break;
134    case CACA_EVENT_KEY_RELEASE:
135        character = caca_get_event_key_ch(ev);
136        cucul_printf(cv, x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
137                     (character > 0x1f && character < 0x80) ? character : '?');
138        break;
139    case CACA_EVENT_MOUSE_MOTION:
140        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
141                     caca_get_event_mouse_x(ev), caca_get_event_mouse_y(ev));
142        break;
143    case CACA_EVENT_MOUSE_PRESS:
144        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_PRESS %u",
145                     caca_get_event_mouse_button(ev));
146        break;
147    case CACA_EVENT_MOUSE_RELEASE:
148        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_RELEASE %u",
149                     caca_get_event_mouse_button(ev));
150        break;
151    case CACA_EVENT_RESIZE:
152        cucul_printf(cv, x, y, "CACA_EVENT_RESIZE %u %u",
153                     caca_get_event_resize_width(ev),
154                     caca_get_event_resize_height(ev));
155        break;
156    case CACA_EVENT_QUIT:
157        cucul_printf(cv, x, y, "CACA_EVENT_QUIT");
158        break;
159    default:
160        cucul_printf(cv, x, y, "CACA_EVENT_UNKNOWN");
161    }
162}
163
Note: See TracBrowser for help on using the repository browser.