source: libcaca/trunk/examples/event.c @ 2101

Last change on this file since 2101 was 2101, checked in by Sam Hocevar, 12 years ago
  • Rename "test" to "examples". The former will become a real testsuite.
  • Autodetect zzuf in the configure step. It might become handy.
  • 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 2101 2007-11-30 23:48:39Z 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    free(events);
115    caca_free_display(dp);
116    cucul_free_canvas(cv);
117
118    return 0;
119}
120
121static void print_event(int x, int y, caca_event_t *ev)
122{
123    int character;
124
125    switch(caca_get_event_type(ev))
126    {
127    case CACA_EVENT_NONE:
128        cucul_printf(cv, x, y, "CACA_EVENT_NONE");
129        break;
130    case CACA_EVENT_KEY_PRESS:
131        character = caca_get_event_key_ch(ev);
132        cucul_printf(cv, x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
133                     (character > 0x1f && character < 0x80) ? character : '?');
134        break;
135    case CACA_EVENT_KEY_RELEASE:
136        character = caca_get_event_key_ch(ev);
137        cucul_printf(cv, x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
138                     (character > 0x1f && character < 0x80) ? character : '?');
139        break;
140    case CACA_EVENT_MOUSE_MOTION:
141        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
142                     caca_get_event_mouse_x(ev), caca_get_event_mouse_y(ev));
143        break;
144    case CACA_EVENT_MOUSE_PRESS:
145        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_PRESS %u",
146                     caca_get_event_mouse_button(ev));
147        break;
148    case CACA_EVENT_MOUSE_RELEASE:
149        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_RELEASE %u",
150                     caca_get_event_mouse_button(ev));
151        break;
152    case CACA_EVENT_RESIZE:
153        cucul_printf(cv, x, y, "CACA_EVENT_RESIZE %u %u",
154                     caca_get_event_resize_width(ev),
155                     caca_get_event_resize_height(ev));
156        break;
157    case CACA_EVENT_QUIT:
158        cucul_printf(cv, x, y, "CACA_EVENT_QUIT");
159        break;
160    default:
161        cucul_printf(cv, x, y, "CACA_EVENT_UNKNOWN");
162    }
163}
164
Note: See TracBrowser for help on using the repository browser.