aboutsummaryrefslogtreecommitdiff
path: root/xbmc/input/TouchInput.cpp
blob: 2eb2b09d48057187a4e51e969b0ca1ea3228e343 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
 *      Copyright (C) 2012 Team XBMC
 *      http://www.xbmc.org
 *
 *  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 2, 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 XBMC; see the file COPYING.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

#include "TouchInput.h"
#include "threads/SingleLock.h"
#include "utils/log.h"

CTouchInput::CTouchInput()
     : m_holdTimeout(1000),
       m_handler(NULL),
       m_gestureState(TouchGestureUnknown),
       m_gestureStateOld(TouchGestureUnknown)
{
  m_holdTimer = new CTimer(this);
}

CTouchInput::~CTouchInput()
{
  delete m_holdTimer;
}

CTouchInput &CTouchInput::Get()
{
  static CTouchInput sTouchInput;
  return sTouchInput;
}

void CTouchInput::RegisterHandler(ITouchHandler *touchHandler)
{
  m_handler = touchHandler;
}

void CTouchInput::UnregisterHandler()
{
  m_handler = NULL;
}

void CTouchInput::SetTouchHoldTimeout(int32_t timeout)
{
  if (timeout <= 0)
    return;

  m_holdTimeout = timeout;
}

bool CTouchInput::Handle(TouchEvent event, float x, float y, int64_t time, int32_t pointer /* = 0 */, float size /* = 0.0f */)
{
  if (time < 0 || pointer < 0 || pointer >= TOUCH_MAX_POINTERS)
    return false;

  CSingleLock lock(m_critical);

  bool result = true;

  m_pointers[pointer].current.x = x;
  m_pointers[pointer].current.y = y;
  m_pointers[pointer].current.time = time;

  switch (event)
  {
    case TouchEventAbort:
    {
      CLog::Log(LOGDEBUG, "CTouchInput: TouchEventAbort");
      setGestureState(TouchGestureUnknown);
      for (unsigned int pIndex = 0; pIndex < TOUCH_MAX_POINTERS; pIndex++)
        m_pointers[pIndex].reset();

      OnTouchAbort();
      break;
    }

    case TouchEventDown:
    {
      CLog::Log(LOGDEBUG, "CTouchInput: TouchEventDown");
      m_pointers[pointer].down.x = x;
      m_pointers[pointer].down.y = y;
      m_pointers[pointer].down.time = time;
      m_pointers[pointer].moving = false;
      m_pointers[pointer].size = size;

      // If this is the down event of the primary pointer
      // we start by assuming that it's a single touch
      if (pointer == 0)
      {
        setGestureState(TouchGestureSingleTouch);
        result = OnSingleTouchStart(x, y);

        m_holdTimer->Start(m_holdTimeout);
      }
      // Otherwise it's the down event of another pointer
      else
      {
        // If we so far assumed single touch or still have the primary
        // pointer of a previous multi touch pressed down, we can update to multi touch
        if (m_gestureState == TouchGestureSingleTouch || m_gestureState == TouchGestureSingleTouchHold ||
            m_gestureState == TouchGestureMultiTouchDone)
        {
          if (m_gestureState == TouchGestureSingleTouch || m_gestureState == TouchGestureSingleTouchHold)
          {
            result = OnMultiTouchStart(x, y);

            m_holdTimer->Stop(true);
            m_holdTimer->Start(m_holdTimeout);
          }
          else
          {
            result = OnMultiTouchDown(x, y, pointer);

            m_holdTimer->Stop(false);
          }

          setGestureState(TouchGestureMultiTouchStart);
        }
        // Otherwise we should ignore this pointer
        else
        {
          m_pointers[pointer].reset();
          break;
        }
      }
      return result;
    }

    case TouchEventUp:
    {
      CLog::Log(LOGDEBUG, "CTouchInput: TouchEventUp");
      // unexpected event => abort
      if (!m_pointers[pointer].valid() ||
          m_gestureState == TouchGestureUnknown)
        break;

      m_holdTimer->Stop(false);

      // Just a single tap with a pointer
      if (m_gestureState == TouchGestureSingleTouch || m_gestureState == TouchGestureSingleTouchHold)
      {
        result = OnSingleTouchEnd(x, y);

        if (m_gestureState == TouchGestureSingleTouch)
          OnSingleTap(x, y);
      }
      // A pan gesture started with a single pointer (ignoring any other pointers)
      else if (m_gestureState == TouchGesturePan)
      {
        float velocityX = 0.0f; // number of pixels per second
        float velocityY = 0.0f; // number of pixels per second
        m_pointers[pointer].velocity(velocityX, velocityY, false);

        result = OnTouchGesturePanEnd(x, y,
                                      x - m_pointers[pointer].down.x, y - m_pointers[pointer].down.y,
                                      velocityX, velocityY);
      }
      // we are in multi-touch
      else
        result = OnMultiTouchUp(x, y, pointer);

      // If we were in multi touch mode and lifted one pointer
      // we can go into the TouchGestureMultiTouchDone state which will allow
      // the user to go back into multi touch mode without lifting the primary pointer
      if (m_gestureState == TouchGestureMultiTouchStart || m_gestureState == TouchGestureMultiTouchHold || m_gestureState == TouchGestureMultiTouch)
      {
        setGestureState(TouchGestureMultiTouchDone);

        // after lifting the primary pointer, the secondary pointer will
        // become the primary pointer in the next event
        if (pointer == 0)
        {
          m_pointers[0] = m_pointers[1];
          pointer = 1;
        }
      }
      // Otherwise abort
      else
      {
        if (m_gestureState == TouchGestureMultiTouchDone)
        {
          result = OnMultiTouchEnd(m_pointers[0].down.x, m_pointers[0].down.y);

          // if neither of the two pointers moved we have a double tap
          if (m_gestureStateOld != TouchGestureMultiTouchHold && m_gestureStateOld != TouchGestureMultiTouch)
            OnDoubleTap(m_pointers[0].down.x, m_pointers[0].down.y,
                        m_pointers[1].down.x, m_pointers[1].down.y);
        }

        setGestureState(TouchGestureUnknown);
      }

      m_pointers[pointer].reset();
      return result;
    }

    case TouchEventMove:
    {
      CLog::Log(LOGDEBUG, "CTouchInput: TouchEventMove");
      // unexpected event => abort
      if (!m_pointers[pointer].valid() ||
          m_gestureState == TouchGestureUnknown ||
          m_gestureState == TouchGestureMultiTouchDone)
        break;

      if (m_pointers[pointer].moving)
        m_holdTimer->Stop();

      if (m_gestureState == TouchGestureSingleTouch)
      {
        // Check if the touch has moved far enough to count as movement
        if (!m_pointers[pointer].moving)
          break;

        result = OnTouchGesturePanStart(m_pointers[pointer].down.x, m_pointers[pointer].down.y);

        m_pointers[pointer].last.copy(m_pointers[pointer].down);
        setGestureState(TouchGesturePan);
      }
      else if (m_gestureState == TouchGestureMultiTouchStart)
      {
        setGestureState(TouchGestureMultiTouch);

        // set the starting point
        saveLastTouch();
      }

      float offsetX = x - m_pointers[pointer].last.x;
      float offsetY = y - m_pointers[pointer].last.y;
      float velocityX = 0.0f; // number of pixels per second
      float velocityY = 0.0f; // number of pixels per second
      m_pointers[pointer].velocity(velocityX, velocityY);

      if (m_pointers[pointer].moving &&
         (m_gestureState == TouchGestureSingleTouch || m_gestureState == TouchGestureSingleTouchHold || m_gestureState == TouchGesturePan))
        result = OnSingleTouchMove(x, y, offsetX, offsetY, velocityX, velocityY);

      // Let's see if we have a pan gesture (i.e. the primary and only pointer moving)
      if (m_gestureState == TouchGesturePan)
      {
        result = OnTouchGesturePan(x, y, offsetX, offsetY, velocityX, velocityY);

        m_pointers[pointer].last.x = x;
        m_pointers[pointer].last.y = y;
      }
      else if (m_gestureState == TouchGestureMultiTouch)
      {
        if (m_pointers[pointer].moving)
          result = OnMultiTouchMove(x, y,offsetX, offsetY, velocityX, velocityY, pointer);

        handleMultiTouchGesture();
      }
      else
        break;

      return result;
    }

    default:
      CLog::Log(LOGDEBUG, "CTouchInput: unknown TouchEvent");
      break;
  }

  return false;
}

bool CTouchInput::Update(int32_t pointer, float x, float y, int64_t time, float size /* = 0.0f */)
{
  if (pointer < 0 || pointer > TOUCH_MAX_POINTERS)
    return false;

  CSingleLock lock(m_critical);

  m_pointers[pointer].last.copy(m_pointers[pointer].current);

  m_pointers[pointer].current.x = x;
  m_pointers[pointer].current.y = y;
  m_pointers[pointer].current.time = time;
  if (size > 0.0f)
    m_pointers[pointer].size = size;

  // calculate whether the pointer has moved at all
  if (!m_pointers[pointer].moving)
  {
    CVector down = m_pointers[pointer].down;
    CVector current = m_pointers[pointer].current;
    CVector distance = down - current;

    if (distance.length() > m_pointers[pointer].size)
      m_pointers[pointer].moving = true;
  }

  return true;
}

void CTouchInput::saveLastTouch()
{
  for (unsigned int pointer = 0; pointer < TOUCH_MAX_POINTERS; pointer++)
    m_pointers[pointer].last.copy(m_pointers[pointer].current);
}

void CTouchInput::handleMultiTouchGesture()
{
  handleZoomPinch();
}

void CTouchInput::handleZoomPinch()
{
  Pointer& primaryPointer = m_pointers[0];
  Pointer& secondaryPointer = m_pointers[1];

  // calculate zoom/pinch
  CVector primary = primaryPointer.down;
  CVector secondary = secondaryPointer.down;
  CVector diagonal = primary - secondary;

  float baseDiffLength = diagonal.length();
  if (baseDiffLength != 0.0f)
  {
    CVector primaryNow = primaryPointer.current;
    CVector secondaryNow = secondaryPointer.current;
    CVector diagonalNow = primaryNow - secondaryNow;
    float curDiffLength = diagonalNow.length();

    float centerX = (primary.x + secondary.x) / 2;
    float centerY = (primary.y + secondary.y) / 2;

    float zoom = curDiffLength / baseDiffLength;

    OnZoomPinch(centerX, centerY, zoom);
  }
}

void CTouchInput::OnTimeout()
{
  CSingleLock lock(m_critical);

  switch (m_gestureState)
  {
    case TouchGestureSingleTouch:
      setGestureState(TouchGestureSingleTouchHold);

      OnSingleTouchHold(m_pointers[0].down.x, m_pointers[0].down.y);
      OnSingleLongPress(m_pointers[0].down.x, m_pointers[0].down.y);
      break;

    case TouchGestureMultiTouchStart:
      if (!m_pointers[0].moving && !m_pointers[1].moving)
      {
        setGestureState(TouchGestureMultiTouchHold);

        OnMultiTouchHold(m_pointers[0].down.x, m_pointers[0].down.y);
        OnDoubleLongPress(m_pointers[0].down.x, m_pointers[0].down.y, m_pointers[1].down.x, m_pointers[1].down.y);
      }
      break;

    default:
      break;
  }
}

void CTouchInput::OnTouchAbort()
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);

  if (m_handler)
    m_handler->OnTouchAbort();
}

bool CTouchInput::OnSingleTouchStart(float x, float y)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);

  if (m_handler)
    return m_handler->OnSingleTouchStart(x, y);

  return true;
}

bool CTouchInput::OnSingleTouchHold(float x, float y)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);

  if (m_handler)
    return m_handler->OnSingleTouchHold(x, y);

  return true;
}

bool CTouchInput::OnSingleTouchMove(float x, float y, float offsetX, float offsetY, float velocityX, float velocityY)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);

  if (m_handler)
    return m_handler->OnSingleTouchMove(x, y, offsetX, offsetY, velocityX, velocityY);

  return true;
}

bool CTouchInput::OnSingleTouchEnd(float x, float y)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);

  if (m_handler)
    return m_handler->OnSingleTouchEnd(x, y);

  return true;
}

bool CTouchInput::OnMultiTouchStart(float x, float y, int32_t pointers /* = 2 */)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);

  if (m_handler)
    return m_handler->OnMultiTouchStart(x, y, pointers);

  return true;
}

bool CTouchInput::OnMultiTouchDown(float x, float y, int32_t pointer)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);

  if (m_handler)
    return m_handler->OnMultiTouchDown(x, y, pointer);

  return true;
}

bool CTouchInput::OnMultiTouchHold(float x, float y, int32_t pointers /* = 2 */)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);

  if (m_handler)
    return m_handler->OnMultiTouchHold(x, y, pointers);

  return true;
}

bool CTouchInput::OnMultiTouchMove(float x, float y, float offsetX, float offsetY, float velocityX, float velocityY, int32_t pointer)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);

  if (m_handler)
    return m_handler->OnMultiTouchMove(x, y, offsetX, offsetY, velocityX, velocityY, pointer);

  return true;
}

bool CTouchInput::OnMultiTouchUp(float x, float y, int32_t pointer)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);

  if (m_handler)
    return m_handler->OnMultiTouchUp(x, y, pointer);

  return true;
}

bool CTouchInput::OnMultiTouchEnd(float x, float y, int32_t pointers /* = 2 */)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);

  if (m_handler)
    return m_handler->OnMultiTouchEnd(x, y, pointers);

  return true;
}

bool CTouchInput::OnTouchGesturePanStart(float x, float y)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);

  if (m_handler)
    return m_handler->OnTouchGesturePanStart(x, y);

  return true;
}

bool CTouchInput::OnTouchGesturePan(float x, float y, float offsetX, float offsetY, float velocityX, float velocityY)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);

  if (m_handler)
    return m_handler->OnTouchGesturePan(x, y, offsetX, offsetY, velocityX, velocityY);

  return true;
}

bool CTouchInput::OnTouchGesturePanEnd(float x, float y, float offsetX, float offsetY, float velocityX, float velocityY)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);

  if (m_handler)
    return m_handler->OnTouchGesturePanEnd(x, y, offsetX, offsetY, velocityX, velocityY);

  return true;
}

void CTouchInput::OnSingleTap(float x, float y)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);

  if (m_handler)
    m_handler->OnSingleTap(x, y);
}

void CTouchInput::OnSingleLongPress(float x, float y)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);

  if (m_handler)
    m_handler->OnSingleLongPress(x, y);
}

void CTouchInput::OnDoubleTap(float x1, float y1, float x2, float y2)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);

  if (m_handler)
    m_handler->OnDoubleTap(x1, y1, x2, y2);
}

void CTouchInput::OnDoubleLongPress(float x1, float y1, float x2, float y2)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);
  
  if (m_handler)
    m_handler->OnDoubleLongPress(x1, y1, x2, y2);
}

void CTouchInput::OnZoomPinch(float centerX, float centerY, float zoomFactor)
{
  CLog::Log(LOGDEBUG, "%s", __FUNCTION__);
  
  if (m_handler)
    m_handler->OnZoomPinch(centerX, centerY, zoomFactor);
}