summaryrefslogtreecommitdiffstats
path: root/uwac/libuwac/uwac-tools.c
blob: 760970e6789d582e15568eaef9952a74035b6deb (plain)
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
/*
 * Copyright © 2015 David FORT <contact@hardening-consulting.com>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#include <wayland-util.h>
#include <string.h>
#include <uwac/uwac-tools.h>

struct uwac_touch_automata
{
	struct wl_array tp;
};

void UwacTouchAutomataInit(UwacTouchAutomata* automata)
{
	wl_array_init(&automata->tp);
}

void UwacTouchAutomataReset(UwacTouchAutomata* automata)
{
	automata->tp.size = 0;
}

bool UwacTouchAutomataInjectEvent(UwacTouchAutomata* automata, UwacEvent* event)
{

	UwacTouchPoint* tp = NULL;

	switch (event->type)
	{
		case UWAC_EVENT_TOUCH_FRAME_BEGIN:
			break;

		case UWAC_EVENT_TOUCH_UP:
		{
			UwacTouchUp* touchUp = &event->touchUp;
			size_t toMove = automata->tp.size - sizeof(UwacTouchPoint);

			wl_array_for_each(tp, &automata->tp)
			{
				if ((int64_t)tp->id == touchUp->id)
				{
					if (toMove)
						memmove(tp, tp + 1, toMove);
					return true;
				}

				toMove -= sizeof(UwacTouchPoint);
			}
			break;
		}

		case UWAC_EVENT_TOUCH_DOWN:
		{
			UwacTouchDown* touchDown = &event->touchDown;

			wl_array_for_each(tp, &automata->tp)
			{
				if ((int64_t)tp->id == touchDown->id)
				{
					tp->x = touchDown->x;
					tp->y = touchDown->y;
					return true;
				}
			}

			tp = wl_array_add(&automata->tp, sizeof(UwacTouchPoint));
			if (!tp)
				return false;

			if (touchDown->id < 0)
				return false;

			tp->id = (uint32_t)touchDown->id;
			tp->x = touchDown->x;
			tp->y = touchDown->y;
			break;
		}

		case UWAC_EVENT_TOUCH_FRAME_END:
			break;

		default:
			break;
	}

	return true;
}