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

Last change on this file since 811 was 811, checked in by Sam Hocevar, 14 years ago
  • Renamed caca_t into caca_display_t.
  • Renamed canvas handle variables from "c" to "cv". Eh ouais mon gros.
  • Property svn:keywords set to Id
File size: 4.2 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 811 2006-04-18 15:11:25Z 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(0, 0);
34    if(!cv)
35        return 1;
36    dp = caca_attach(cv);
37    if(!dp)
38        return 1;
39
40    h = cucul_get_height(cv) - 1;
41
42    cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
43    cucul_draw_line(cv, 0, 0, cucul_get_width(cv) - 1, 0, " ");
44
45    cucul_draw_line(cv, 0, h, cucul_get_width(cv) - 1, h, " ");
46    cucul_putstr(cv, 0, h, "type \"quit\" to exit");
47
48    caca_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_clear(cv);
85
86        /* Print current event */
87        cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
88        cucul_draw_line(cv, 0, 0, cucul_get_width(cv) - 1, 0, " ");
89        print_event(0, 0, events);
90
91        cucul_draw_line(cv, 0, h, cucul_get_width(cv) - 1, h, " ");
92        cucul_printf(cv, 0, h, "type \"quit\" to exit: %s", quit_string[quit]);
93
94        /* Print previous events */
95        cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK);
96        for(i = 1; i < h && events[i].type; i++)
97            print_event(0, i, events + i);
98
99        caca_display(dp);
100    }
101
102    /* Clean up */
103    caca_detach(dp);
104    cucul_free(cv);
105
106    return 0;
107}
108
109static void print_event(int x, int y, caca_event_t *ev)
110{
111    int character;
112
113    switch(ev->type)
114    {
115    case CACA_EVENT_NONE:
116        cucul_printf(cv, x, y, "CACA_EVENT_NONE");
117        break;
118    case CACA_EVENT_KEY_PRESS:
119        character = ev->data.key.ch;
120        cucul_printf(cv, x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%cv)", character,
121                     (character > 0x1f && character < 0x80) ? character : '?');
122        break;
123    case CACA_EVENT_KEY_RELEASE:
124        character = ev->data.key.ch;
125        cucul_printf(cv, x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%cv)", character,
126                     (character > 0x1f && character < 0x80) ? character : '?');
127        break;
128    case CACA_EVENT_MOUSE_MOTION:
129        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
130                     ev->data.mouse.x, ev->data.mouse.y);
131        break;
132    case CACA_EVENT_MOUSE_PRESS:
133        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_PRESS %u",
134                     ev->data.mouse.button);
135        break;
136    case CACA_EVENT_MOUSE_RELEASE:
137        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_RELEASE %u",
138                     ev->data.mouse.button);
139        break;
140    case CACA_EVENT_RESIZE:
141        cucul_printf(cv, x, y, "CACA_EVENT_RESIZE %u %u",
142                     ev->data.resize.w, ev->data.resize.h);
143        break;
144    case CACA_EVENT_QUIT:
145        cucul_printf(cv, x, y, "CACA_EVENT_QUIT");
146        break;
147    default:
148        cucul_printf(cv, x, y, "CACA_EVENT_UNKNOWN");
149    }
150}
151
Note: See TracBrowser for help on using the repository browser.