Blame uwac/libuwac/uwac-tools.c

Packit 1fb8d4
/*
Packit 1fb8d4
 * Copyright © 2015 David FORT <contact@hardening-consulting.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Permission to use, copy, modify, distribute, and sell this software and its
Packit 1fb8d4
 * documentation for any purpose is hereby granted without fee, provided that
Packit 1fb8d4
 * the above copyright notice appear in all copies and that both that copyright
Packit 1fb8d4
 * notice and this permission notice appear in supporting documentation, and
Packit 1fb8d4
 * that the name of the copyright holders not be used in advertising or
Packit 1fb8d4
 * publicity pertaining to distribution of the software without specific,
Packit 1fb8d4
 * written prior permission.  The copyright holders make no representations
Packit 1fb8d4
 * about the suitability of this software for any purpose.  It is provided "as
Packit 1fb8d4
 * is" without express or implied warranty.
Packit 1fb8d4
 *
Packit 1fb8d4
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
Packit 1fb8d4
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
Packit 1fb8d4
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
Packit 1fb8d4
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
Packit 1fb8d4
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
Packit 1fb8d4
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
Packit 1fb8d4
 * OF THIS SOFTWARE.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#include <wayland-util.h>
Packit 1fb8d4
#include <string.h>
Packit 1fb8d4
#include <uwac/uwac-tools.h>
Packit 1fb8d4
Packit 1fb8d4
/** @brief */
Packit 1fb8d4
struct uwac_touch_automata {
Packit 1fb8d4
	struct wl_array tp;
Packit 1fb8d4
};
Packit 1fb8d4
Packit 1fb8d4
void UwacTouchAutomataInit(UwacTouchAutomata *automata) {
Packit 1fb8d4
	wl_array_init(&automata->tp);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void UwacTouchAutomataReset(UwacTouchAutomata *automata) {
Packit 1fb8d4
	automata->tp.size = 0;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
bool UwacTouchAutomataInjectEvent(UwacTouchAutomata *automata, UwacEvent *event) {
Packit 1fb8d4
Packit 1fb8d4
	UwacTouchPoint *tp;
Packit 1fb8d4
Packit 1fb8d4
	switch (event->type) {
Packit 1fb8d4
	case UWAC_EVENT_TOUCH_FRAME_BEGIN:
Packit 1fb8d4
		break;
Packit 1fb8d4
Packit 1fb8d4
	case UWAC_EVENT_TOUCH_UP: {
Packit 1fb8d4
		UwacTouchUp *touchUp = &event->touchUp;
Packit 1fb8d4
		int toMove = automata->tp.size - sizeof(UwacTouchPoint);
Packit 1fb8d4
Packit 1fb8d4
		wl_array_for_each(tp, &automata->tp) {
Packit 1fb8d4
			if (tp->id == touchUp->id) {
Packit 1fb8d4
				if (toMove)
Packit 1fb8d4
					memmove(tp, tp+1, toMove);
Packit 1fb8d4
				return true;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			toMove -= sizeof(UwacTouchPoint);
Packit 1fb8d4
		}
Packit 1fb8d4
		break;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	case UWAC_EVENT_TOUCH_DOWN: {
Packit 1fb8d4
		UwacTouchDown *touchDown = &event->touchDown;
Packit 1fb8d4
Packit 1fb8d4
		wl_array_for_each(tp, &automata->tp) {
Packit 1fb8d4
			if (tp->id == touchDown->id) {
Packit 1fb8d4
				tp->x = touchDown->x;
Packit 1fb8d4
				tp->y = touchDown->y;
Packit 1fb8d4
				return true;
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		tp = wl_array_add(&automata->tp, sizeof(UwacTouchPoint));
Packit 1fb8d4
		if (!tp)
Packit 1fb8d4
			return false;
Packit 1fb8d4
Packit 1fb8d4
		tp->id = touchDown->id;
Packit 1fb8d4
		tp->x = touchDown->x;
Packit 1fb8d4
		tp->y = touchDown->y;
Packit 1fb8d4
		break;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	case UWAC_EVENT_TOUCH_FRAME_END:
Packit 1fb8d4
		break;
Packit 1fb8d4
Packit 1fb8d4
	default:
Packit 1fb8d4
		break;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return true;
Packit 1fb8d4
}