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
RevLine 
[324]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 *
[1462]8 *  This program is free software. It comes without any warranty, to
[1452]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
[522]12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
[324]13 */
14
15#include "config.h"
[859]16#include "common.h"
[1048]17#if !defined(__KERNEL__)
18#   include <stdio.h>
19#   include <string.h>
20#   include <stdlib.h>
21#endif
[324]22
[524]23#include "cucul.h"
[324]24#include "caca.h"
25
[811]26static cucul_canvas_t *cv;
27static caca_display_t *dp;
[524]28
[777]29static void print_event(int, int, caca_event_t *);
[324]30
31int main(int argc, char **argv)
32{
[777]33    caca_event_t *events;
[331]34    int i, h, quit;
[324]35
[1823]36    cv = cucul_create_canvas(80, 24);
[1753]37    if(cv == NULL)
38    {
39        printf("Failed to create canvas\n");
[324]40        return 1;
[1753]41    }
42
[819]43    dp = caca_create_display(cv);
[1753]44    if(dp == NULL)
45    {
46        printf("Failed to create display\n");
[524]47        return 1;
[1753]48    }
[324]49
[813]50    h = cucul_get_canvas_height(cv) - 1;
[332]51
[1267]52    cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
[1330]53    cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
[324]54
[1330]55    cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, ' ');
[1347]56    cucul_put_str(cv, 0, h, "type \"quit\" to exit");
[332]57
[819]58    caca_refresh_display(dp);
[324]59
[777]60    events = malloc(h * sizeof(caca_event_t));
61    memset(events, 0, h * sizeof(caca_event_t));
[324]62
[332]63    for(quit = 0; quit < 4; )
[324]64    {
[777]65        caca_event_t ev;
[332]66        static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
[811]67        int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, -1);
[324]68
[681]69        if(!ret)
[324]70            continue;
71
[331]72        do
73        {
[332]74            /* "quit" quits */
[2049]75            if(caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
[332]76            {
[2049]77                int key = caca_get_event_key_ch(&ev);
[332]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            }
[324]86
[777]87            memmove(events + 1, events, (h - 1) * sizeof(caca_event_t));
[681]88            events[0] = ev;
[331]89
[811]90            ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
[331]91        }
[681]92        while(ret);
[331]93
[1267]94        cucul_set_color_ansi(cv, CUCUL_LIGHTGRAY, CUCUL_BLACK);
[832]95        cucul_clear_canvas(cv);
[324]96
97        /* Print current event */
[1267]98        cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
[1330]99        cucul_draw_line(cv, 0, 0, cucul_get_canvas_width(cv) - 1, 0, ' ');
[681]100        print_event(0, 0, events);
[324]101
[1330]102        cucul_draw_line(cv, 0, h, cucul_get_canvas_width(cv) - 1, h, ' ');
[811]103        cucul_printf(cv, 0, h, "type \"quit\" to exit: %s", quit_string[quit]);
[332]104
[324]105        /* Print previous events */
[1267]106        cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLACK);
[2049]107        for(i = 1; i < h && caca_get_event_type(&events[i]); i++)
[681]108            print_event(0, i, events + i);
[324]109
[819]110        caca_refresh_display(dp);
[324]111    }
112
113    /* Clean up */
[2075]114    free(events);
[819]115    caca_free_display(dp);
[813]116    cucul_free_canvas(cv);
[324]117
118    return 0;
119}
120
[777]121static void print_event(int x, int y, caca_event_t *ev)
[324]122{
[331]123    int character;
124
[2049]125    switch(caca_get_event_type(ev))
[324]126    {
127    case CACA_EVENT_NONE:
[811]128        cucul_printf(cv, x, y, "CACA_EVENT_NONE");
[324]129        break;
130    case CACA_EVENT_KEY_PRESS:
[2049]131        character = caca_get_event_key_ch(ev);
[898]132        cucul_printf(cv, x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
[802]133                     (character > 0x1f && character < 0x80) ? character : '?');
[324]134        break;
135    case CACA_EVENT_KEY_RELEASE:
[2049]136        character = caca_get_event_key_ch(ev);
[898]137        cucul_printf(cv, x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
[802]138                     (character > 0x1f && character < 0x80) ? character : '?');
[324]139        break;
140    case CACA_EVENT_MOUSE_MOTION:
[811]141        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
[2049]142                     caca_get_event_mouse_x(ev), caca_get_event_mouse_y(ev));
[324]143        break;
144    case CACA_EVENT_MOUSE_PRESS:
[811]145        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_PRESS %u",
[2049]146                     caca_get_event_mouse_button(ev));
[324]147        break;
148    case CACA_EVENT_MOUSE_RELEASE:
[811]149        cucul_printf(cv, x, y, "CACA_EVENT_MOUSE_RELEASE %u",
[2049]150                     caca_get_event_mouse_button(ev));
[324]151        break;
[349]152    case CACA_EVENT_RESIZE:
[811]153        cucul_printf(cv, x, y, "CACA_EVENT_RESIZE %u %u",
[2049]154                     caca_get_event_resize_width(ev),
155                     caca_get_event_resize_height(ev));
[349]156        break;
[802]157    case CACA_EVENT_QUIT:
[811]158        cucul_printf(cv, x, y, "CACA_EVENT_QUIT");
[802]159        break;
[324]160    default:
[811]161        cucul_printf(cv, x, y, "CACA_EVENT_UNKNOWN");
[324]162    }
163}
164
Note: See TracBrowser for help on using the repository browser.