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

Last change on this file since 1462 was 1462, checked in by Sam Hocevar, 13 years ago
  • Bwarf, typo in the no warranty clause.
  • Property svn:keywords set to Id
File size: 4.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 1462 2006-12-12 01:53: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(0, 0);
37    if(!cv)
38        return 1;
39    dp = caca_create_display(cv);
40    if(!dp)
41        return 1;
42
43    h = cucul_get_canvas_height(cv) - 1;
44
45    cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
46    cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
47
48    cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, ' ');
49    cucul_put_str(cv, 0, h, "type \"quit\" to exit");
50
51    caca_refresh_display(dp);
52
53    events = malloc(h * sizeof(caca_event_t));
54    memset(events, 0, h * sizeof(caca_event_t));
55
56    for(quit = 0; quit < 4; )
57    {
58        caca_event_t ev;
59        static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
60        int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, -1);
61
62        if(!ret)
63            continue;
64
65        do
66        {
67            /* "quit" quits */
68            if(ev.type & CACA_EVENT_KEY_PRESS)
69            {
70                int key = ev.data.key.ch;
71                if((key == 'q' && quit == 0) || (key == 'u' && quit == 1)
72                    || (key == 'i' && quit == 2) || (key == 't' && quit == 3))
73                    quit++;
74                else if(key == 'q')
75                    quit = 1;
76                else
77                    quit = 0;
78            }
79
80            memmove(events + 1, events, (h - 1) * sizeof(caca_event_t));
81            events[0] = ev;
82
83            ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
84        }
85        while(ret);
86
87        cucul_set_color_ansi(cv, CUCUL_LIGHTGRAY, CUCUL_BLACK);
88        cucul_clear_canvas(cv);
89
90        /* Print current event */
91        cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
92        cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
93        print_event(0, 0, events);
94
95        cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, ' ');
96        cucul_printf(cv, 0, h, "type \"quit\" to exit: %s", quit_string[quit]);
97
98        /* Print previous events */
99        cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLACK);
100        for(i = 1; i < h && events[i].type; i++)
101            print_event(0, i, events + i);
102
103        caca_refresh_display(dp);
104    }
105
106    /* Clean up */
107    caca_free_display(dp);
108    cucul_free_canvas(cv);
109
110    return 0;
111}
112
113static void print_event(int x, int y, caca_event_t *ev)
114{
115    int character;
116
117    switch(ev->type)
118    {
119    case CACA_EVENT_NONE:
120        cucul_printf(cv, x, y, "CACA_EVENT_NONE");
121        break;
122    case CACA_EVENT_KEY_PRESS:
123        character = ev->data.key.ch;
124        cucul_printf(cv, x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
125                     (character > 0x1f && character < 0x80) ? character : '?');
126        break;
127    case CACA_EVENT_KEY_RELEASE:
128        character = ev->data.key.ch;
129        cucul_printf(cv, x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
130                     (character > 0x1f && character < 0x80) ? character : '?');
131        break;
132    case CACA_EVENT_MOUSE_MOTION:
133        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
134                     ev->data.mouse.x, ev->data.mouse.y);
135        break;
136    case CACA_EVENT_MOUSE_PRESS:
137        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_PRESS %u",
138                     ev->data.mouse.button);
139        break;
140    case CACA_EVENT_MOUSE_RELEASE:
141        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_RELEASE %u",
142                     ev->data.mouse.button);
143        break;
144    case CACA_EVENT_RESIZE:
145        cucul_printf(cv, x, y, "CACA_EVENT_RESIZE %u %u",
146                     ev->data.resize.w, ev->data.resize.h);
147        break;
148    case CACA_EVENT_QUIT:
149        cucul_printf(cv, x, y, "CACA_EVENT_QUIT");
150        break;
151    default:
152        cucul_printf(cv, x, y, "CACA_EVENT_UNKNOWN");
153    }
154}
155
Note: See TracBrowser for help on using the repository browser.