1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/*
* event event lister for libcaca
* Copyright (c) 2004 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* $Id: event.c 246 2004-01-13 22:33:09Z sam $
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "caca.h"
static void print_event(int, int, unsigned int);
int main(int argc, char **argv)
{
int *events;
int i, h, quit;
if(caca_init())
return 1;
h = caca_get_height() - 1;
caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
caca_draw_line(0, 0, caca_get_width() - 1, 0, ' ');
caca_draw_line(0, h, caca_get_width() - 1, h, ' ');
caca_putstr(0, h, "type \"quit\" to exit");
caca_refresh();
events = malloc(h * sizeof(int));
memset(events, 0, h * sizeof(int));
for(quit = 0; quit < 4; )
{
static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
unsigned int event = caca_wait_event(CACA_EVENT_ANY);
if(!event)
continue;
do
{
/* "quit" quits */
if(event & CACA_EVENT_KEY_PRESS)
{
int key = event & ~CACA_EVENT_KEY_PRESS;
if((key == 'q' && quit == 0) || (key == 'u' && quit == 1)
|| (key == 'i' && quit == 2) || (key == 't' && quit == 3))
quit++;
else if(key == 'q')
quit = 1;
else
quit = 0;
}
memmove(events + 1, events, (h - 1) * sizeof(int));
events[0] = event;
event = caca_get_event(CACA_EVENT_ANY);
}
while(event);
caca_clear();
/* Print current event */
caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
caca_draw_line(0, 0, caca_get_width() - 1, 0, ' ');
print_event(0, 0, events[0]);
caca_draw_line(0, h, caca_get_width() - 1, h, ' ');
caca_printf(0, h, "type \"quit\" to exit: %s", quit_string[quit]);
/* Print previous events */
caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLACK);
for(i = 1; i < h && events[i]; i++)
print_event(0, i, events[i]);
caca_refresh();
}
/* Clean up */
caca_end();
return 0;
}
static void print_event(int x, int y, unsigned int event)
{
int character;
switch(event & 0xff000000)
{
case CACA_EVENT_NONE:
caca_printf(x, y, "CACA_EVENT_NONE");
break;
case CACA_EVENT_KEY_PRESS:
character = event & 0x00ffffff;
caca_printf(x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%c)", character,
(character > 0x20 && character < 0x80) ? character : '?');
break;
case CACA_EVENT_KEY_RELEASE:
character = event & 0x00ffffff;
caca_printf(x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%c)", character,
(character > 0x20 && character < 0x80) ? character : '?');
break;
case CACA_EVENT_MOUSE_MOTION:
caca_printf(x, y, "CACA_EVENT_MOUSE_MOTION %u %u",
(event & 0x00fff000) >> 12, event & 0x00000fff);
break;
case CACA_EVENT_MOUSE_PRESS:
caca_printf(x, y, "CACA_EVENT_MOUSE_PRESS %u",
event & 0x00ffffff);
break;
case CACA_EVENT_MOUSE_RELEASE:
caca_printf(x, y, "CACA_EVENT_MOUSE_RELEASE %u",
event & 0x00ffffff);
break;
case CACA_EVENT_RESIZE:
caca_printf(x, y, "CACA_EVENT_RESIZE");
break;
default:
caca_printf(x, y, "CACA_EVENT_UNKNOWN");
}
}
|