File: mouse.c

package info (click to toggle)
libcaca 0.99.beta20-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,264 kB
  • sloc: ansic: 25,091; php: 2,763; python: 2,637; cs: 1,213; cpp: 1,127; java: 916; objc: 836; makefile: 543; perl: 505; sh: 472; asm: 297; ruby: 215; xml: 33
file content (90 lines) | stat: -rw-r--r-- 2,353 bytes parent folder | download | duplicates (7)
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
/*
 *  mouse         libcaca mouse events
 *  Copyright (c) 2006-2012 Jean-Yves Lamoureux <jylam@lnxscene.org>
 *                All Rights Reserved
 *
 *  This program is free software. It comes without any warranty, to
 *  the extent permitted by applicable law. You can redistribute it
 *  and/or modify it under the terms of the Do What the Fuck You Want
 *  to Public License, Version 2, as published by Sam Hocevar. See
 *  http://www.wtfpl.net/ for more details.
 */
#include "config.h"

#if !defined(__KERNEL__)
#   include <math.h>
#   include <string.h>
#   include <stdio.h>
#endif

#include "caca.h"


int main(int argc, char *argv[])
{
    int quit = 0;
    int x = 0, y = 0, p = 0, b = 0;
    caca_canvas_t *cv;
    caca_display_t *dp;

    cv = caca_create_canvas(80, 24);
    if (cv == NULL)
    {
        printf("Failed to create canvas\n");
        return 1;
    }

    dp = caca_create_display(cv);
    if (dp == NULL)
    {
        printf("Failed to create display\n");
        return 1;
    }

    caca_set_display_time(dp, 40000);
    caca_set_cursor(dp, 0);

    while (!quit)
    {
        caca_event_t ev;
        while (caca_get_event(dp, CACA_EVENT_ANY, &ev, 0))
        {
            if ((caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS))
            {
                quit = 1;
            }
            if (caca_get_event_type(&ev) & CACA_EVENT_MOUSE_MOTION)
            {
                x = caca_get_event_mouse_x(&ev);
                y = caca_get_event_mouse_y(&ev);
            }
            if (caca_get_event_type(&ev) & CACA_EVENT_MOUSE_PRESS)
            {
                p = 1;
                b = caca_get_event_mouse_button(&ev);
            }
            else if (caca_get_event_type(&ev) & CACA_EVENT_MOUSE_RELEASE)
            {
                p = 0;
                b = caca_get_event_mouse_button(&ev);
            }
        }
        caca_printf(cv, 0, 0, "%d,%d", x, y);
        if (b)
        {
            caca_printf(cv, 0, 1, "Mouse button %d %s", b,
                        p == 1 ? "pressed" : "released");
        }

        caca_printf(cv, x - 2, y - 1, "  |");
        caca_printf(cv, x - 2, y, "--|--");
        caca_printf(cv, x - 2, y + 1, "  |");
        caca_refresh_display(dp);
        caca_clear_canvas(cv);
    }

    caca_free_display(dp);
    caca_free_canvas(cv);

    return 0;
}