source: libcaca/branches/0.99.beta1/test/event.c @ 3128

Last change on this file since 3128 was 832, checked in by Sam Hocevar, 16 years ago
  • Changed cucul_clear_canvas() again. It now uses the current foreground and background colours to clear the screen.
  • Property svn:keywords set to Id
File size: 4.4 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 832 2006-04-21 20:49:17Z 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_canvas_t *cv;
24static caca_display_t *dp;
25
26static void print_event(int, int, caca_event_t *);
27
28int main(int argc, char **argv)
29{
30    caca_event_t *events;
31    int i, h, quit;
32
33    cv = cucul_create_canvas(0, 0);
34    if(!cv)
35        return 1;
36    dp = caca_create_display(cv);
37    if(!dp)
38        return 1;
39
40    h = cucul_get_canvas_height(cv) - 1;
41
42    cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
43    cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, " ");
44
45    cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, " ");
46    cucul_putstr(cv, 0, h, "type \"quit\" to exit");
47
48    caca_refresh_display(dp);
49
50    events = malloc(h * sizeof(caca_event_t));
51    memset(events, 0, h * sizeof(caca_event_t));
52
53    for(quit = 0; quit < 4; )
54    {
55        caca_event_t ev;
56        static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
57        int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, -1);
58
59        if(!ret)
60            continue;
61
62        do
63        {
64            /* "quit" quits */
65            if(ev.type & CACA_EVENT_KEY_PRESS)
66            {
67                int key = ev.data.key.ch;
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(caca_event_t));
78            events[0] = ev;
79
80            ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
81        }
82        while(ret);
83
84        cucul_set_color(cv, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
85        cucul_clear_canvas(cv);
86
87        /* Print current event */
88        cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
89        cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, " ");
90        print_event(0, 0, events);
91
92        cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, " ");
93        cucul_printf(cv, 0, h, "type \"quit\" to exit: %s", quit_string[quit]);
94
95        /* Print previous events */
96        cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK);
97        for(i = 1; i < h && events[i].type; i++)
98            print_event(0, i, events + i);
99
100        caca_refresh_display(dp);
101    }
102
103    /* Clean up */
104    caca_free_display(dp);
105    cucul_free_canvas(cv);
106
107    return 0;
108}
109
110static void print_event(int x, int y, caca_event_t *ev)
111{
112    int character;
113
114    switch(ev->type)
115    {
116    case CACA_EVENT_NONE:
117        cucul_printf(cv, x, y, "CACA_EVENT_NONE");
118        break;
119    case CACA_EVENT_KEY_PRESS:
120        character = ev->data.key.ch;
121        cucul_printf(cv, x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%cv)", character,
122                     (character > 0x1f && character < 0x80) ? character : '?');
123        break;
124    case CACA_EVENT_KEY_RELEASE:
125        character = ev->data.key.ch;
126        cucul_printf(cv, x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%cv)", character,
127                     (character > 0x1f && character < 0x80) ? character : '?');
128        break;
129    case CACA_EVENT_MOUSE_MOTION:
130        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
131                     ev->data.mouse.x, ev->data.mouse.y);
132        break;
133    case CACA_EVENT_MOUSE_PRESS:
134        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_PRESS %u",
135                     ev->data.mouse.button);
136        break;
137    case CACA_EVENT_MOUSE_RELEASE:
138        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_RELEASE %u",
139                     ev->data.mouse.button);
140        break;
141    case CACA_EVENT_RESIZE:
142        cucul_printf(cv, x, y, "CACA_EVENT_RESIZE %u %u",
143                     ev->data.resize.w, ev->data.resize.h);
144        break;
145    case CACA_EVENT_QUIT:
146        cucul_printf(cv, x, y, "CACA_EVENT_QUIT");
147        break;
148    default:
149        cucul_printf(cv, x, y, "CACA_EVENT_UNKNOWN");
150    }
151}
152
Note: See TracBrowser for help on using the repository browser.