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

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

Large source code cleanup, getting rid of spaces, tabs, and svn keywords.

File size: 2.3 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 *  This program is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12#include "config.h"
13
14#if !defined(__KERNEL__)
15#   include <math.h>
16#   include <string.h>
17#   include <stdio.h>
18#endif
19
20#include "caca.h"
21
22
23int main(int argc, char *argv[])
24{
25    int quit = 0;
26    int x = 0, y = 0, p = 0, b = 0;
27    caca_canvas_t *cv;
28    caca_display_t *dp;
29
30    cv = caca_create_canvas(80, 24);
31    if (cv == NULL)
32    {
33        printf("Failed to create canvas\n");
34        return 1;
35    }
36
37    dp = caca_create_display(cv);
38    if (dp == NULL)
39    {
40        printf("Failed to create display\n");
41        return 1;
42    }
43
44    caca_set_display_time(dp, 40000);
45    caca_set_cursor(dp, 0);
46
47    while (!quit)
48    {
49        caca_event_t ev;
50        while (caca_get_event(dp, CACA_EVENT_ANY, &ev, 0))
51        {
52            if ((caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS))
53            {
54                quit = 1;
55            }
56            if (caca_get_event_type(&ev) & CACA_EVENT_MOUSE_MOTION)
57            {
58                x = caca_get_event_mouse_x(&ev);
59                y = caca_get_event_mouse_y(&ev);
60            }
61            if (caca_get_event_type(&ev) & CACA_EVENT_MOUSE_PRESS)
62            {
63                p = 1;
64                b = caca_get_event_mouse_button(&ev);
65            }
66            else if (caca_get_event_type(&ev) & CACA_EVENT_MOUSE_RELEASE)
67            {
68                p = 0;
69                b = caca_get_event_mouse_button(&ev);
70            }
71        }
72        caca_printf(cv, 0, 0, "%d,%d", x, y);
73        if (b)
74        {
75            caca_printf(cv, 0, 1, "Mouse button %d %s", b,
76                        p == 1 ? "pressed" : "released");
77        }
78
79        caca_printf(cv, x - 2, y - 1, "  |");
80        caca_printf(cv, x - 2, y, "--|--");
81        caca_printf(cv, x - 2, y + 1, "  |");
82        caca_refresh_display(dp);
83        caca_clear_canvas(cv);
84    }
85
86    caca_free_display(dp);
87    caca_free_canvas(cv);
88
89    return 0;
90}
Note: See TracBrowser for help on using the repository browser.