/***************************************************************************** * * grail - Gesture Recognition And Instantiation Library * * Copyright (C) 2010 Canonical Ltd. * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . * * Authors: * Henrik Rydberg * ****************************************************************************/ #include "grail-inserter.h" #include #include #include struct gesture_recognizer { struct touch_frame frame; touch_time_t scroll_time; int scroll_active; int scroll_gslot; int scroll_slots[2]; }; int gru_init(struct grail *ge) { ge->gru = calloc(1, sizeof(struct gesture_recognizer)); if (!ge->gru) return -ENOMEM; return 0; } void gru_destroy(struct grail *ge) { free(ge->gru); ge->gru = NULL; } void gru_recognize(struct grail *ge, const struct touch_frame *frame) { struct gesture_inserter *gin = ge->gin; struct gesture_recognizer *gru = ge->gru; grail_prop_t prop[DIM_GRAIL_PROP]; grail_mask_t span[16]; int slot, nslot, x, y, dx, dy; if (!gru) return; if (frame->ncreate || frame->ndestroy) { gin_gesture_end_all(gin); gru->scroll_active = 0; return; } if (frame->ntouch != 2) return; memset(span, 0, sizeof(span)); nslot = 0; x = y = dx = dy = 0; for (slot = frame->slot; slot >= 0; slot = next_slot(frame, slot)) { struct touch *ot = &gru->frame.touch[slot]; const struct touch *t = &frame->touch[slot]; gru->scroll_slots[nslot] = slot; grail_set_mask(span, slot); x += t->prop[TP_POS_X]; y += t->prop[TP_POS_Y]; dx += t->prop[TP_POS_X] - ot->prop[TP_POS_X]; dy += t->prop[TP_POS_Y] - ot->prop[TP_POS_Y]; nslot++; } x /= nslot; y /= nslot; dx /= nslot; dy /= nslot; if (!dy) return; if (!gru->scroll_active) { gru->scroll_gslot = gin_gesture_begin(ge, frame, GRAIL_TYPE_VSCROLL, x, y, span); gru->scroll_active = 1; } prop[0] = dy; gin_gesture_event(gin, gru->scroll_gslot, prop); gru->frame = *frame; }