File: switch_input.c

package info (click to toggle)
retroarch 1.22.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 80,716 kB
  • sloc: ansic: 1,275,794; cpp: 115,470; objc: 9,973; asm: 6,624; python: 4,071; makefile: 2,867; sh: 2,828; xml: 1,408; perl: 393; java: 298; javascript: 196
file content (966 lines) | stat: -rw-r--r-- 30,170 bytes parent folder | download | duplicates (2)
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
/*  RetroArch - A frontend for libretro.
 *
 *  RetroArch 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 Found-
 *  ation, either version 3 of the License, or (at your option) any later version.
 *
 *  RetroArch 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 RetroArch.
 *  If not, see <http://www.gnu.org/licenses/>.
 */
#include <stdint.h>
#include <stdlib.h>

#include <boolean.h>
#include <libretro.h>
#include <retro_miscellaneous.h>

#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif

#include "../../config.def.h"

#include "../../retroarch.h"

#ifdef HAVE_LIBNX
#include <switch.h>

/* Supports up to this many fingers at once */
#define MULTITOUCH_LIMIT 4
/* Abstraction of pointer coords */
#define TOUCH_AXIS_MAX 0x7fff
/* Size of rarch_key_map_switch */
#define SWITCH_NUM_SCANCODES 110
/* See https://switchbrew.github.io/libnx/hid_8h.html */
#define SWITCH_MAX_SCANCODE 0xfb
#define MOUSE_MAX_X 1920
#define MOUSE_MAX_Y 1080

/* Beginning of touch mouse defines and types */
#define TOUCH_MAX_X 1280
#define TOUCH_MAX_Y 720
#define TOUCH_MOUSE_BUTTON_LEFT 0
#define TOUCH_MOUSE_BUTTON_RIGHT 1
/* Finger ID setting if finger is not touching the screen */
#define NO_TOUCH -1 

enum
{
   MAX_NUM_FINGERS = 3, /* number of fingers to track per panel for touch mouse */
   MAX_TAP_TIME = 250, /* taps longer than this will not result in mouse click events */
   MAX_TAP_MOTION_DISTANCE = 10, /* max distance finger motion in Vita screen pixels to be considered a tap */
   SIMULATED_CLICK_DURATION = 50, /* time in ms how long simulated mouse clicks should be */
}; /* track three fingers per panel */

typedef struct
{
   int id; /* -1: no touch */
   int time_last_down;
   float last_down_x;
   float last_down_y;
} Touch;

typedef enum DraggingType
{
   DRAG_NONE = 0,
   DRAG_TWO_FINGER,
   DRAG_THREE_FINGER,
} DraggingType;

typedef enum TouchEventType
{
   FINGERDOWN,
   FINGERUP,
   FINGERMOTION,
} TouchEventType;

typedef struct
{
   TouchEventType type;
   uint64_t timestamp;
   int touchId;
   int fingerId;
   float x;
   float y;
   float dx;
   float dy;
} FingerType;

typedef struct
{
   TouchEventType type;
   FingerType tfinger;
} TouchEvent;
/* end of touch mouse defines and types */
#endif

#include "../input_keymaps.h"

/* TODO/FIXME -
 * fix game focus toggle */

typedef struct switch_input
{
#ifdef HAVE_LIBNX
   /* pointer */
   bool touch_state[MULTITOUCH_LIMIT];
   bool previous_touch_state[MULTITOUCH_LIMIT];
   int16_t touch_x_viewport[MULTITOUCH_LIMIT]; /* used for POINTER device */
   int16_t touch_y_viewport[MULTITOUCH_LIMIT]; /* used for POINTER device */
   int16_t touch_x_screen[MULTITOUCH_LIMIT]; /* used for POINTER_SCREEN device */
   int16_t touch_y_screen[MULTITOUCH_LIMIT]; /* used for POINTER_SCREEN device */
   uint32_t touch_x[MULTITOUCH_LIMIT]; /* used for touch mouse */
   uint32_t touch_y[MULTITOUCH_LIMIT]; /* used for touch mouse */
   uint32_t touch_previous_x[MULTITOUCH_LIMIT];
   uint32_t touch_previous_y[MULTITOUCH_LIMIT];
   bool keyboard_state[SWITCH_MAX_SCANCODE + 1];

   /* physical mouse */
   int32_t mouse_x;
   int32_t mouse_y;
   int32_t mouse_x_delta;
   int32_t mouse_y_delta;
   int32_t mouse_wheel;
   bool mouse_button_left;
   bool mouse_button_right;
   bool mouse_button_middle;
   uint32_t mouse_previous_buttons;

   /* touch mouse */
   bool touch_mouse_indirect;
   float touch_mouse_speed_factor;
   int hires_dx; /* sub-pixel touch mouse precision */
   int hires_dy; /* sub-pixel touch mouse precision */
   Touch finger[MAX_NUM_FINGERS]; /* keep track of finger status for touch mouse */
   DraggingType multi_finger_dragging; /* keep track whether we are currently drag-and-dropping */
   int32_t simulated_click_start_time[2]; /* initiation time of last simulated left or right click (zero if no click) */

   /* sensor handles */
   HidSixAxisSensorHandle sixaxis_handles[DEFAULT_MAX_PADS][4];
   unsigned sixaxis_handles_count[DEFAULT_MAX_PADS];
#else
   void *empty;
#endif
} switch_input_t;

#ifdef HAVE_LIBNX
/* beginning of touch mouse function declarations */
static void switch_handle_touch_mouse(switch_input_t *sw);
static void switch_normalize_touch_mouse_xy(float *normalized_x, float *normalized_y, int reported_x, int reported_y);
static void switch_process_touch_mouse_event(switch_input_t *sw, TouchEvent *event);
static void switch_process_touch_mouse_finger_down(switch_input_t * sw, TouchEvent *event);
static void switch_process_touch_mouse_finger_up(switch_input_t *sw, TouchEvent *event);
static void switch_process_touch_mouse_finger_motion(switch_input_t *sw, TouchEvent *event);
static void switch_normalized_to_screen_xy(int *screenX, int *screenY, float x, float y);
static void switch_finish_simulated_mouse_clicks(switch_input_t *sw, uint64_t currentTime);
/* end of touch mouse function declarations */
#endif

#ifdef HAVE_LIBNX
static void switch_input_poll(void *data)
{
   HidTouchScreenState touch_screen_state;
   HidKeyboardState kbd_state;
   HidMouseState mouse_state;
   uint32_t touch_count;
   bool key_pressed;
   unsigned int i                = 0;
   int key_sym                   = 0;
   unsigned key_code             = 0;
   uint16_t mod                  = 0;
   switch_input_t *sw            = (switch_input_t*) data;

   hidGetTouchScreenStates(&touch_screen_state, 1);
   touch_count = MIN(MULTITOUCH_LIMIT, touch_screen_state.count);
   for (i = 0; i < MULTITOUCH_LIMIT; i++)
   {
      sw->previous_touch_state[i] = sw->touch_state[i];
      sw->touch_state[i]          = touch_count > i;

      if (sw->touch_state[i])
      {
         struct video_viewport vp;

         sw->touch_previous_x[i]     = sw->touch_x[i];
         sw->touch_previous_y[i]     = sw->touch_y[i];
         sw->touch_x[i]              = touch_screen_state.touches[i].x;
         sw->touch_y[i]              = touch_screen_state.touches[i].y;

         /* convert from event coordinates to core and screen coordinates */
         vp.x                        = 0;
         vp.y                        = 0;
         vp.width                    = 0;
         vp.height                   = 0;
         vp.full_width               = 0;
         vp.full_height              = 0;

         video_driver_translate_coord_viewport_wrap(
            &vp,
            touch_screen_state.touches[i].x,
            touch_screen_state.touches[i].y,
            &sw->touch_x_viewport[i],
            &sw->touch_y_viewport[i],
            &sw->touch_x_screen[i],
            &sw->touch_y_screen[i]);
      }
   }

   hidGetKeyboardStates(&kbd_state, 1);
   if (kbd_state.modifiers & (HidKeyboardModifier_LeftAlt | HidKeyboardModifier_RightAlt))
      mod |= RETROKMOD_ALT;
   if (kbd_state.modifiers & HidKeyboardModifier_Control)
      mod |= RETROKMOD_CTRL;
   if (kbd_state.modifiers & HidKeyboardModifier_Shift)
      mod |= RETROKMOD_SHIFT;
   if (kbd_state.modifiers & HidKeyboardModifier_Gui)
      mod |= RETROKMOD_META;
   if (kbd_state.modifiers & HidKeyboardModifier_CapsLock)
      mod |= RETROKMOD_CAPSLOCK;
   if (kbd_state.modifiers & HidKeyboardModifier_ScrollLock)
      mod |= RETROKMOD_SCROLLOCK;
   if (kbd_state.modifiers & HidKeyboardModifier_NumLock)
      mod |= RETROKMOD_NUMLOCK;

   for (i = 0; i < SWITCH_NUM_SCANCODES; i++)
   {
      key_sym     = rarch_key_map_switch[i].sym;
      key_code    = input_keymaps_translate_keysym_to_rk(key_sym);
      key_pressed = hidKeyboardStateGetKey(&kbd_state, key_sym);
      if (key_sym && key_pressed && !(sw->keyboard_state[key_sym]))
      {
         sw->keyboard_state[key_sym] = true;
         input_keyboard_event(true, key_code, 0, mod, RETRO_DEVICE_KEYBOARD);
      }
      else if (key_sym && !key_pressed && sw->keyboard_state[key_sym])
      {
         sw->keyboard_state[key_sym] = false;
         input_keyboard_event(false, key_code, 0, mod, RETRO_DEVICE_KEYBOARD);
      }
   }

   /* update physical mouse buttons only when they change
    * this allows the physical mouse and touch mouse to coexist */
   hidGetMouseStates(&mouse_state, 1);
   if ((mouse_state.buttons & HidMouseButton_Left)
         != (sw->mouse_previous_buttons & HidMouseButton_Left))
   {
      if (mouse_state.buttons & HidMouseButton_Left)
         sw->mouse_button_left = true;
      else
         sw->mouse_button_left = false;
   }

   if ((mouse_state.buttons & HidMouseButton_Right)
         != (sw->mouse_previous_buttons & HidMouseButton_Right))
   {
      if (mouse_state.buttons & HidMouseButton_Right)
         sw->mouse_button_right = true;
      else
         sw->mouse_button_right = false;
   }

   if ((mouse_state.buttons & HidMouseButton_Middle)
         != (sw->mouse_previous_buttons & HidMouseButton_Middle))
   {
      if (mouse_state.buttons & HidMouseButton_Middle)
         sw->mouse_button_middle = true;
      else
         sw->mouse_button_middle = false;
   }
   sw->mouse_previous_buttons = mouse_state.buttons;

   /* physical mouse position */
   sw->mouse_x_delta = mouse_state.delta_x;
   sw->mouse_y_delta = mouse_state.delta_y;

   sw->mouse_x       = mouse_state.x;
   sw->mouse_y       = mouse_state.y;

   /* touch mouse events
    * switch_handle_touch_mouse will update sw->mouse_* variables
    * depending on touch input gestures
    * see first comment in switch_process_touch_mouse_event() for a list of
    * supported touch gestures */
   switch_handle_touch_mouse(sw);

   if (sw->mouse_x < 0)
      sw->mouse_x  = 0;
   else if (sw->mouse_x > MOUSE_MAX_X)
      sw->mouse_x  = MOUSE_MAX_X;

   if (sw->mouse_y < 0) 
      sw->mouse_y  = 0;
   else if (sw->mouse_y > MOUSE_MAX_Y)
      sw->mouse_y  = MOUSE_MAX_Y;

   sw->mouse_wheel = mouse_state.wheel_delta_y;
}
#endif

static int16_t switch_input_state(
      void *data,
      const input_device_driver_t *joypad,
      const input_device_driver_t *sec_joypad,
      rarch_joypad_info_t *joypad_info,
      const retro_keybind_set *binds,
      bool keyboard_mapping_blocked,
      unsigned port,
      unsigned device,
      unsigned idx,
      unsigned id)
{
   switch_input_t *sw = (switch_input_t*) data;

   if (port > DEFAULT_MAX_PADS - 1)
      return 0;

   switch (device)
   {
      case RETRO_DEVICE_JOYPAD:
      case RETRO_DEVICE_ANALOG:
         break;
#ifdef HAVE_LIBNX
      case RETRO_DEVICE_KEYBOARD:
         return (id && id < RETROK_LAST) && sw->keyboard_state[rarch_keysym_lut[(enum retro_key)id]];
      case RETRO_DEVICE_MOUSE:
      case RARCH_DEVICE_MOUSE_SCREEN:
         {
            int16_t val = 0;
            bool screen = (device == RARCH_DEVICE_MOUSE_SCREEN);
            switch (id)
            {
               case RETRO_DEVICE_ID_MOUSE_LEFT:
                  return sw->mouse_button_left;
               case RETRO_DEVICE_ID_MOUSE_RIGHT:
                  return sw->mouse_button_right;
               case RETRO_DEVICE_ID_MOUSE_MIDDLE:
                  return sw->mouse_button_middle;
               case RETRO_DEVICE_ID_MOUSE_X:
                  if (screen)
                     return sw->mouse_x;

                  val                = sw->mouse_x_delta;
                  sw->mouse_x_delta  = 0;
                  /* flush delta after it has been read */
                  break;
               case RETRO_DEVICE_ID_MOUSE_Y:
                  if (screen)
                     return sw->mouse_y;

                  val                = sw->mouse_y_delta;
                  sw->mouse_y_delta  = 0;
                  /* flush delta after it has been read */
                  break;
               case RETRO_DEVICE_ID_MOUSE_WHEELUP:
                  if (sw->mouse_wheel > 0)
                  {
                     val             = sw->mouse_wheel;
                     sw->mouse_wheel = 0;
                  }
                  break;
               case RETRO_DEVICE_ID_MOUSE_WHEELDOWN:
                  if (sw->mouse_wheel < 0)
                  {
                     val             = sw->mouse_wheel;
                     sw->mouse_wheel = 0;
                  }
                  break;
            }

            return val;
         }
         break;
      case RETRO_DEVICE_POINTER:
         if (idx < MULTITOUCH_LIMIT)
         {
            switch (id)
            {
               case RETRO_DEVICE_ID_POINTER_PRESSED:
                  return sw->touch_state[idx];
               case RETRO_DEVICE_ID_POINTER_X:
                  return sw->touch_x_viewport[idx];
               case RETRO_DEVICE_ID_POINTER_Y:
                  return sw->touch_y_viewport[idx];
            }
         }
         break;
      case RARCH_DEVICE_POINTER_SCREEN:
         if (idx < MULTITOUCH_LIMIT)
         {
            switch (id)
            {
               case RETRO_DEVICE_ID_POINTER_PRESSED:
                  return sw->touch_state[idx];
               case RETRO_DEVICE_ID_POINTER_X:
                  return sw->touch_x_screen[idx];
               case RETRO_DEVICE_ID_POINTER_Y:
                  return sw->touch_y_screen[idx];
            }
         }
         break;
#endif
   }

   return 0;
}

#ifdef HAVE_LIBNX
static void switch_handle_touch_mouse(switch_input_t *sw)
{
   unsigned int i;
   int finger_id         = 0;
   uint64_t current_time = svcGetSystemTick() * 1000 / 19200000;

   switch_finish_simulated_mouse_clicks(sw, current_time);

   for (i = 0; i < MULTITOUCH_LIMIT; i++)
   {
      if (sw->touch_state[i])
      {
         float x   = 0;
         float y   = 0;
         switch_normalize_touch_mouse_xy(&x, &y, sw->touch_x[i], sw->touch_y[i]);
         finger_id = i;

         /* Send an initial touch if finger hasn't been down */
         if (!sw->previous_touch_state[i])
         {
            TouchEvent ev;
            ev.type              = FINGERDOWN;
            ev.tfinger.timestamp = current_time;
            ev.tfinger.fingerId  = finger_id;
            ev.tfinger.x         = x;
            ev.tfinger.y         = y;
            switch_process_touch_mouse_event(sw, &ev);
         }
         else
         {
            /* If finger moved, send motion event instead */
            if (sw->touch_x[i] != sw->touch_previous_x[i] ||
                sw->touch_y[i] != sw->touch_previous_y[i])
            {
               TouchEvent ev;
               float oldx = 0;
               float oldy = 0;
               switch_normalize_touch_mouse_xy(&oldx, &oldy, sw->touch_previous_x[i], sw->touch_previous_y[i]);
               ev.type              = FINGERMOTION;
               ev.tfinger.timestamp = current_time;
               ev.tfinger.fingerId  = finger_id;
               ev.tfinger.x         = x;
               ev.tfinger.y         = y;
               ev.tfinger.dx        = x - oldx;
               ev.tfinger.dy        = y - oldy;
               switch_process_touch_mouse_event(sw, &ev);
            }
         }
      }

      /* some fingers might have been let go */
      if (sw->previous_touch_state[i] && sw->touch_state[i] == false)
      {
         float x = 0;
         float y = 0;
         TouchEvent ev;
         switch_normalize_touch_mouse_xy(&x, &y,
               sw->touch_previous_x[i], sw->touch_previous_y[i]);
         finger_id            = i;
         /* finger released from screen */
         ev.type              = FINGERUP;
         ev.tfinger.timestamp = current_time;
         ev.tfinger.fingerId  = finger_id;
         ev.tfinger.x         = x;
         ev.tfinger.y         = y;
         switch_process_touch_mouse_event(sw, &ev);
      }
   }
}

static void switch_normalize_touch_mouse_xy(float *normalized_x,
      float *normalized_y, int reported_x, int reported_y)
{
   float x = (float) reported_x / TOUCH_MAX_X;
   float y = (float) reported_y / TOUCH_MAX_Y;

   if (x < 0.0f)
      x = 0.0f;
   else if (x > 1.0f)
      x = 1.0f;

   if (y < 0.0f)
      y = 0.0f;
   else if (y > 1.0f)
      y = 1.0f;
   *normalized_x = x;
   *normalized_y = y;
}

static void switch_process_touch_mouse_event(switch_input_t *sw, TouchEvent *event)
{
   /* supported touch gestures:
    * pointer motion = single finger drag
    * left mouse click = single finger short tap
    * right mouse click = second finger short tap 
    * while first finger is still down
    * left click drag and drop = dual finger drag
    * right click drag and drop = triple finger drag */
   if (     event->type == FINGERDOWN 
         || event->type == FINGERUP
         || event->type == FINGERMOTION)
   {
      switch (event->type)
      {
         case FINGERDOWN:
            switch_process_touch_mouse_finger_down(sw, event);
            break;
         case FINGERUP:
            switch_process_touch_mouse_finger_up(sw, event);
            break;
         case FINGERMOTION:
            switch_process_touch_mouse_finger_motion(sw, event);
            break;
      }
   }
}

static void switch_process_touch_mouse_finger_down(switch_input_t *sw, TouchEvent *event)
{
   /* id (for multitouch) */
   unsigned int i;
   int id = event->tfinger.fingerId;

   /* make sure each finger is not reported down multiple times */
   for (i = 0; i < MAX_NUM_FINGERS; i++)
   {
      if (sw->finger[i].id == id)
         sw->finger[i].id = NO_TOUCH;
   }

   /* we need the timestamps to decide later if the 
    * user performed a short tap (click)
    * or a long tap (drag)
    * we also need the last coordinates for each finger 
    * to keep track of dragging */
   for (i = 0; i < MAX_NUM_FINGERS; i++)
   {
      if (sw->finger[i].id == NO_TOUCH)
      {
         sw->finger[i].id             = id;
         sw->finger[i].time_last_down = event->tfinger.timestamp;
         sw->finger[i].last_down_x    = event->tfinger.x;
         sw->finger[i].last_down_y    = event->tfinger.y;
         break;
      }
   }
}

static void switch_process_touch_mouse_finger_up(switch_input_t *sw, TouchEvent *event)
{
   unsigned int i;
   /* id (for multitouch) */
   int id = event->tfinger.fingerId;
   /* find out how many fingers were down before this event */
   int num_fingers_down = 0;

   for (i = 0; i < MAX_NUM_FINGERS; i++)
   {
      if (sw->finger[i].id >= 0)
         num_fingers_down++;
   }

   for (i = 0; i < MAX_NUM_FINGERS; i++)
   {
      if (sw->finger[i].id != id)
         continue;

      sw->finger[i].id = NO_TOUCH;
      if (!sw->multi_finger_dragging)
      {
         float xrel;
         float yrel;
         float max_r_squared;
         int simulated_button;

         if ((event->tfinger.timestamp - sw->finger[i].time_last_down) 
               > MAX_TAP_TIME)
            continue;

         /* short (<MAX_TAP_TIME ms) tap is interpreted as 
          * right/left mouse click depending on # fingers already down
          * but only if the finger hasn't moved since it was 
          * pressed down by more than MAX_TAP_MOTION_DISTANCE pixels */
         xrel = ((event->tfinger.x * TOUCH_MAX_X) - (sw->finger[i].last_down_x * TOUCH_MAX_X));
         yrel = ((event->tfinger.y * TOUCH_MAX_Y) - (sw->finger[i].last_down_y * TOUCH_MAX_Y));
         max_r_squared = (float) (MAX_TAP_MOTION_DISTANCE * MAX_TAP_MOTION_DISTANCE);
         if ((xrel * xrel + yrel * yrel) >= max_r_squared)
            continue;
         if (num_fingers_down != 2 && num_fingers_down != 1)
            continue;
         simulated_button = 0;
         if (num_fingers_down == 2)
         {
            simulated_button = TOUCH_MOUSE_BUTTON_RIGHT;
            /* need to raise the button later */
            sw->simulated_click_start_time[TOUCH_MOUSE_BUTTON_RIGHT] = event->tfinger.timestamp;
         }
         else if (num_fingers_down == 1)
         {
            if (!sw->touch_mouse_indirect)
            {
               int x;
               int y;
               switch_normalized_to_screen_xy(&x, &y, event->tfinger.x, event->tfinger.y);
               sw->mouse_x_delta = x - sw->mouse_x;
               sw->mouse_y_delta = y - sw->mouse_y;
               sw->mouse_x       = x;
               sw->mouse_y       = y;
            }
            simulated_button = TOUCH_MOUSE_BUTTON_LEFT;
            /* need to raise the button later */
            sw->simulated_click_start_time[TOUCH_MOUSE_BUTTON_LEFT] = event->tfinger.timestamp;
         }
         /* simulate mouse button down */
         if (simulated_button == TOUCH_MOUSE_BUTTON_LEFT)
            sw->mouse_button_left = true;
         else if (simulated_button == TOUCH_MOUSE_BUTTON_RIGHT)
            sw->mouse_button_right = true;
      }
      else if (num_fingers_down == 1)
      {
         /* when dragging, and the last finger is lifted, the drag is over
          * so simulate mouse button up */
         if (sw->multi_finger_dragging == DRAG_THREE_FINGER)
            sw->mouse_button_right = false;
         else
            sw->mouse_button_left = false;
         sw->multi_finger_dragging = DRAG_NONE;
      }
   }
}

static void switch_process_touch_mouse_finger_motion(switch_input_t *sw, TouchEvent *event)
{
   unsigned int i;
   unsigned int j;
   bool update_pointer;
   /* id (for multitouch) */
   int id = event->tfinger.fingerId;
   /* find out how many fingers were down before this event */
   int num_fingers_down = 0;

   for (i = 0; i < MAX_NUM_FINGERS; i++)
   {
      if (sw->finger[i].id >= 0)
         num_fingers_down++;
   }

   if (num_fingers_down == 0)
      return;

   /* If we are starting a multi-finger drag, 
    * start holding down the mouse button */
   if (num_fingers_down >= 2 && !sw->multi_finger_dragging)
   {
      /* only start a multi-finger drag if at least 
       * two fingers have been down long enough */
      int num_fingers_down_long = 0;
      for (i = 0; i < MAX_NUM_FINGERS; i++)
      {
         if (sw->finger[i].id == NO_TOUCH)
            continue;
         if (event->tfinger.timestamp - sw->finger[i].time_last_down 
               > MAX_TAP_TIME)
            num_fingers_down_long++;
      }
      if (num_fingers_down_long >= 2)
      {
         int simulated_button = 0;
         if (num_fingers_down_long == 2)
         {
            simulated_button          = TOUCH_MOUSE_BUTTON_LEFT;
            sw->multi_finger_dragging = DRAG_TWO_FINGER;
         }
         else
         {
            simulated_button          = TOUCH_MOUSE_BUTTON_RIGHT;
            sw->multi_finger_dragging = DRAG_THREE_FINGER;
         }

         if (simulated_button == TOUCH_MOUSE_BUTTON_LEFT)
            sw->mouse_button_left     = true;
         else if (simulated_button == TOUCH_MOUSE_BUTTON_RIGHT)
            sw->mouse_button_right    = true;
      }
   }

   /* check if this is the "oldest" finger down (or the only finger down)
    * otherwise it will not affect mouse motion */
   update_pointer = true;
   if (num_fingers_down > 1)
   {
      for (i = 0; i < MAX_NUM_FINGERS; i++)
      {
         if (sw->finger[i].id != id)
            continue;
         for (j = 0; j < MAX_NUM_FINGERS; j++)
         {
            if (sw->finger[j].id == NO_TOUCH || (j == i))
               continue;
            if (sw->finger[j].time_last_down < sw->finger[i].time_last_down)
               update_pointer = false;
         }
      }
   }
   if (!update_pointer)
      return;

   if (!sw->touch_mouse_indirect)
   {
      int x;
      int y;
      switch_normalized_to_screen_xy(&x, &y, event->tfinger.x, event->tfinger.y);
      sw->mouse_x_delta = x - sw->mouse_x;
      sw->mouse_y_delta = y - sw->mouse_y;
      sw->mouse_x       = x;
      sw->mouse_y       = y;
   }
   else
   {
      /* for relative mode, use the pointer speed setting */
      int dx        = event->tfinger.dx * TOUCH_MAX_X * 256 * 
         sw->touch_mouse_speed_factor;
      int dy        = event->tfinger.dy * TOUCH_MAX_Y * 256 * 
         sw->touch_mouse_speed_factor;
      sw->hires_dx += dx;
      sw->hires_dy += dy;
      int x_rel     = sw->hires_dx / 256;
      int y_rel     = sw->hires_dy / 256;
      if (x_rel || y_rel)
      {
         sw->mouse_x_delta  = x_rel;
         sw->mouse_y_delta  = y_rel;
         sw->mouse_x       += x_rel;
         sw->mouse_y       += y_rel;
      }
      sw->hires_dx         %= 256;
      sw->hires_dy         %= 256;
   }
}

static void switch_normalized_to_screen_xy(
      int *screenX, int *screenY, float x, float y)
{
   /* map to display */
   *screenX = x * TOUCH_MAX_X;
   *screenY = y * TOUCH_MAX_Y;
}

static void switch_finish_simulated_mouse_clicks(
      switch_input_t *sw, uint64_t currentTime)
{
   unsigned int i;
   for (i = 0; i < 2; i++)
   {
      if (sw->simulated_click_start_time[i] == 0)
         continue;

      if (currentTime - sw->simulated_click_start_time[i] 
            < SIMULATED_CLICK_DURATION)
         continue;

      if (i == 0)
         sw->mouse_button_left = false;
      else
         sw->mouse_button_right = false;

      sw->simulated_click_start_time[i] = 0;
   }
}
#endif

static void switch_input_free_input(void *data)
{
   unsigned i,j;
   switch_input_t *sw = (switch_input_t*) data;

   if (!sw)
      return;

   for (i = 0; i < DEFAULT_MAX_PADS; i++)
      if (sw->sixaxis_handles_count[i] > 0)
         for (j = 0; j < sw->sixaxis_handles_count[i]; j++)
            hidStopSixAxisSensor(sw->sixaxis_handles[i][j]);

   free(sw);
}

static void* switch_input_init(const char *joypad_driver)
{
#ifdef HAVE_LIBNX
   unsigned int i;
#endif
   switch_input_t *sw = (switch_input_t*) calloc(1, sizeof(*sw));
   if (!sw)
      return NULL;

#ifdef HAVE_LIBNX
    hidInitializeTouchScreen();
    hidInitializeMouse();
    hidInitializeKeyboard();

   /* Here we assume that the touch screen is always 1280x720 */
   input_keymaps_init_keyboard_lut(rarch_key_map_switch);
   for (i = 0; i <= SWITCH_MAX_SCANCODE; i++)
      sw->keyboard_state[i]     = false;

   sw->mouse_x                  = 0;
   sw->mouse_y                  = 0;
   sw->mouse_previous_buttons   = 0;

   /* touch mouse init */
   sw->touch_mouse_indirect     = true;
   /* direct mode is not calibrated it seems */
   sw->touch_mouse_speed_factor = 1.0f;
   for (i = 0; i < MAX_NUM_FINGERS; i++)
      sw->finger[i].id = NO_TOUCH;

   sw->multi_finger_dragging = DRAG_NONE;

   for (i = 0; i < 2; i++)
      sw->simulated_click_start_time[i] = 0;
   for (i = 0; i < DEFAULT_MAX_PADS; i++)
      sw->sixaxis_handles_count[i]      = 0;
#endif

   return sw;
}

static uint64_t switch_input_get_capabilities(void *data)
{
   return
#ifdef HAVE_LIBNX
           (1 << RETRO_DEVICE_POINTER) 
         | (1 << RETRO_DEVICE_KEYBOARD) 
         | (1 << RETRO_DEVICE_MOUSE) |
#endif
           (1 << RETRO_DEVICE_JOYPAD) 
         | (1 << RETRO_DEVICE_ANALOG);
}

static bool switch_input_set_sensor_state(void *data, unsigned port,
      enum retro_sensor_action action, unsigned event_rate)
{
#ifdef HAVE_LIBNX
   unsigned i, handles_count;
   switch_input_t *sw = (switch_input_t*) data;

   if (sw)
   {
      bool available = false;
      switch(action)
      {
         case RETRO_SENSOR_ILLUMINANCE_ENABLE:
            appletIsIlluminanceAvailable(&available);
            return available;

         case RETRO_SENSOR_ILLUMINANCE_DISABLE:
         case RETRO_SENSOR_ACCELEROMETER_DISABLE:
         case RETRO_SENSOR_GYROSCOPE_DISABLE:
            return true;

         case RETRO_SENSOR_ACCELEROMETER_ENABLE:
         case RETRO_SENSOR_GYROSCOPE_ENABLE:
            if (port < DEFAULT_MAX_PADS && sw->sixaxis_handles_count[port] == 0)
            {
               hidGetSixAxisSensorHandles(&sw->sixaxis_handles[port][0], 2, port, HidNpadStyleTag_NpadJoyDual);
               hidGetSixAxisSensorHandles(&sw->sixaxis_handles[port][2], 1, port, HidNpadStyleTag_NpadFullKey);

               if (port == 0)
               {
                  hidGetSixAxisSensorHandles(&sw->sixaxis_handles[port][3], 1, HidNpadIdType_Handheld, HidNpadStyleTag_NpadHandheld);
                  handles_count = 4;
               }
               else
                  handles_count = 3;

               for (i = 0; i < handles_count; i++)
                  hidStartSixAxisSensor(sw->sixaxis_handles[port][i]);

               sw->sixaxis_handles_count[port] = handles_count;
            }
            return true;
         case RETRO_SENSOR_DUMMY:
            break;
      }
   }
#endif

   return false;
}

static float switch_input_get_sensor_input(void *data,
      unsigned port, unsigned id)
{
#ifdef HAVE_LIBNX
   float f;
   unsigned int i;
   HidSixAxisSensorState sixaxis;
   switch_input_t *sw = (switch_input_t*) data;

   if (id >= RETRO_SENSOR_ACCELEROMETER_X && id <= RETRO_SENSOR_GYROSCOPE_Z
      && port < DEFAULT_MAX_PADS)
   {
      for (i = 0; i < sw->sixaxis_handles_count[port]; i++)
      {
         hidGetSixAxisSensorStates(sw->sixaxis_handles[port][i], &sixaxis, 1);
         if (sixaxis.delta_time)
            break;
      }

      switch(id)
      {
         case RETRO_SENSOR_ACCELEROMETER_X:
            return sixaxis.acceleration.x;
         case RETRO_SENSOR_ACCELEROMETER_Y:
            return sixaxis.acceleration.y;
         case RETRO_SENSOR_ACCELEROMETER_Z:
            return sixaxis.acceleration.z;
         case RETRO_SENSOR_GYROSCOPE_X:
            return sixaxis.angular_velocity.x;
         case RETRO_SENSOR_GYROSCOPE_Y:
            return sixaxis.angular_velocity.y;
         case RETRO_SENSOR_GYROSCOPE_Z:
            return sixaxis.angular_velocity.z;
      }
   }

   if (id == RETRO_SENSOR_ILLUMINANCE)
   {
      appletGetCurrentIlluminance(&f);
      return f;
   }
#endif

   return 0.0f;
}

input_driver_t input_switch = {
   switch_input_init,
#ifdef HAVE_LIBNX
   switch_input_poll,
#else
   NULL,                            /* poll       */
#endif
   switch_input_state,
   switch_input_free_input,
   switch_input_set_sensor_state,
   switch_input_get_sensor_input,
   switch_input_get_capabilities,
   "switch",
   NULL,                            /* grab_mouse */
   NULL,
   NULL
};