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

Last change on this file since 710 was 710, checked in by Sam Hocevar, 15 years ago
  • Merged caca_wait_event() into caca_get_event() and added a timeout parameter to do what both functions did before, and even more.
  • Property svn:keywords set to Id
File size: 4.1 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 710 2006-04-01 14:44:09Z 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, struct caca_event *);
27
28int main(int argc, char **argv)
29{
30    struct caca_event *events;
31    int i, h, quit;
32
33    qq = cucul_create(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(struct caca_event));
51    memset(events, 0, h * sizeof(struct caca_event));
52
53    for(quit = 0; quit < 4; )
54    {
55        struct caca_event ev;
56        static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
57        int ret = caca_get_event(kk, 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.c;
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(struct caca_event));
78            events[0] = ev;
79
80            ret = caca_get_event(kk, CACA_EVENT_ANY, &ev, 0);
81        }
82        while(ret);
83
84        cucul_clear(qq);
85
86        /* Print current event */
87        cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
88        cucul_draw_line(qq, 0, 0, cucul_get_width(qq) - 1, 0, " ");
89        print_event(0, 0, events);
90
91        cucul_draw_line(qq, 0, h, cucul_get_width(qq) - 1, h, " ");
92        cucul_printf(qq, 0, h, "type \"quit\" to exit: %s", quit_string[quit]);
93
94        /* Print previous events */
95        cucul_set_color(qq, 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(kk);
100    }
101
102    /* Clean up */
103    caca_detach(kk);
104    cucul_free(qq);
105
106    return 0;
107}
108
109static void print_event(int x, int y, struct caca_event *ev)
110{
111    int character;
112
113    switch(ev->type)
114    {
115    case CACA_EVENT_NONE:
116        cucul_printf(qq, x, y, "CACA_EVENT_NONE");
117        break;
118    case CACA_EVENT_KEY_PRESS:
119        character = ev->data.key.c;
120        cucul_printf(qq, x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
121                     (character > 0x20 && character < 0x80) ? character : '?');
122        break;
123    case CACA_EVENT_KEY_RELEASE:
124        character = ev->data.key.c;
125        cucul_printf(qq, x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
126                     (character > 0x20 && character < 0x80) ? character : '?');
127        break;
128    case CACA_EVENT_MOUSE_MOTION:
129        cucul_printf(qq, 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(qq, x, y, "CACA_EVENT_MOUSE_PRESS %u",
134                     ev->data.mouse.button);
135        break;
136    case CACA_EVENT_MOUSE_RELEASE:
137        cucul_printf(qq, x, y, "CACA_EVENT_MOUSE_RELEASE %u",
138                     ev->data.mouse.button);
139        break;
140    case CACA_EVENT_RESIZE:
141        cucul_printf(qq, x, y, "CACA_EVENT_RESIZE %u %u",
142                     ev->data.resize.w, ev->data.resize.h);
143        break;
144    default:
145        cucul_printf(qq, x, y, "CACA_EVENT_UNKNOWN");
146    }
147}
148
Note: See TracBrowser for help on using the repository browser.