source: libcaca/trunk/examples/mouse.c @ 4328

Last change on this file since 4328 was 4126, checked in by Sam Hocevar, 10 years ago

Fix various cosmetic errors (Jylam: tiens un highlight gratuit dans ta gueule pour les trailing spaces !)

File size: 2.4 KB
Line 
1/*
2 *  mouse         libcaca mouse events
3 *  Copyright (c) 2006-2009 Jean-Yves Lamoureux <jylam@lnxscene.org>
4 *                All Rights Reserved
5 *
6 *  $Id: mouse.c 3495 2009-05-21 20:55:21Z jylam $
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#include "config.h"
15
16#if !defined(__KERNEL__)
17#   include <math.h>
18#   include <string.h>
19#   include <stdio.h>
20#endif
21
22#include "caca.h"
23
24
25int main(int argc, char *argv[])
26{
27    int quit = 0;
28    int x = 0, y = 0, p = 0, b = 0;
29    caca_canvas_t *cv;
30    caca_display_t *dp;
31
32    cv = caca_create_canvas(80, 24);
33    if (cv == NULL)
34    {
35        printf("Failed to create canvas\n");
36        return 1;
37    }
38
39    dp = caca_create_display(cv);
40    if (dp == NULL)
41    {
42        printf("Failed to create display\n");
43        return 1;
44    }
45
46    caca_set_display_time(dp, 40000);
47    caca_set_cursor(dp, 0);
48
49    while (!quit)
50    {
51        caca_event_t ev;
52        while (caca_get_event(dp, CACA_EVENT_ANY, &ev, 0))
53        {
54            if ((caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS))
55            {
56                quit = 1;
57            }
58            if (caca_get_event_type(&ev) & CACA_EVENT_MOUSE_MOTION)
59            {
60                x = caca_get_event_mouse_x(&ev);
61                y = caca_get_event_mouse_y(&ev);
62            }
63            if (caca_get_event_type(&ev) & CACA_EVENT_MOUSE_PRESS)
64            {
65                p = 1;
66                b = caca_get_event_mouse_button(&ev);
67            }
68            else if (caca_get_event_type(&ev) & CACA_EVENT_MOUSE_RELEASE)
69            {
70                p = 0;
71                b = caca_get_event_mouse_button(&ev);
72            }
73        }
74        caca_printf(cv, 0, 0, "%d,%d", x, y);
75        if (b)
76        {
77            caca_printf(cv, 0, 1, "Mouse button %d %s", b,
78                        p == 1 ? "pressed" : "released");
79        }
80
81        caca_printf(cv, x - 2, y - 1, "  |");
82        caca_printf(cv, x - 2, y, "--|--");
83        caca_printf(cv, x - 2, y + 1, "  |");
84        caca_refresh_display(dp);
85        caca_clear_canvas(cv);
86    }
87
88    caca_free_display(dp);
89    caca_free_canvas(cv);
90
91    return 0;
92}
Note: See TracBrowser for help on using the repository browser.