forked from OpenXT/input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.c
3078 lines (2596 loc) · 81.4 KB
/
input.c
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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2014 Citrix Systems, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "project.h"
#include "keyboard.h"
#define EVENT_FILES "/dev/input/event"
#define VALUE_KEY_UP 0
#define VALUE_KEY_DOWN 1
#define VALUE_KEY_REPEAT 2
#define KEY_STATUS_SIZE 256
#define BUTTONS_SIZE 3
#define MIN_CONFIG_MOUSE_SPEED 1
#define MAX_CONFIG_MOUSE_SPEED 10
#define DEFAULT_CONFIG_MOUSE_SPEED 5
static void wrapper_revert_to_auth(int fd, short event, void *opaque);
static void wrapper_input_read(int fd, short event, void *opaque);
static void wrapper_force_timer(int fd, short event, void *opaque);
static void wrapper_input_lock_timer(int fd, short event, void *opaque);
static void send_config(struct domain *d, int slot);
static void broadcast_config(int slot);
static int input_check_secure_mode();
static void dup_mouse_clicks(struct domain* d);
static int check_mouse_keys(struct domain* d, int slot, struct input_event* e);
static struct domain *mouse_dest;
static struct domain *mouse_parent;
static struct domain *keyb_parent;
static struct udev *udev;
static struct udev_monitor *udev_mon;
static int current_grab = 0;
struct timeval global_last_input_event;
int platform_lock_timeout = -1;
static int resistance = -1;
static double mouse_x = 0;
static double mouse_y = 0;
static double mouse_speed = 0;
static double mouse_speed_threshold_1 = 0;
static double mouse_speed_threshold_2 = 0;
struct mousebutton
{
int code;
struct domain* domain;
int slot;
int x;
int y;
};
#define MAX_NUM_PRESSED 5
static struct mousebutton mouse_pressed[MAX_NUM_PRESSED];
static int num_mouse_pressed;
static uint32_t buttons[BUTTONS_SIZE];
static int mouse_button = 0;
static uint32_t keyb_modbits = 0;
int keyb_waits_for_click = 0;
struct input_binding
{
int *binding;
input_binding_cb_t cb;
input_binding_cb_t force_cb;
void *opaque;
int matched;
int down;
int force_ticks;
};
#define AUTH_FIELD_USERNAME 0
#define AUTH_FIELD_PASSWORD 1
#define AUTH_FIELD_PASSWORD_CONFIRM 2
#define AUTH_FIELD_PASSWORD_PREVIOUS 3
struct auth_input_state
{
int cursor;
char *passwd;
char *passwd_confirm;
char *passwd_previous; /* containing previous password when we request password change */
char *username;
int current_field;
};
static struct input_dev
{
enum input_device_type types[N_DEVS];
int fds[N_DEVS];
int grab[N_DEVS];
int key_status[KEY_STATUS_SIZE];
struct input_binding *bindings;
int nbinding;
int secure_mode; /* are keys being swallowed by dom0 */
int collect_password; /* are we collecting password */
struct auth_input_state auth;
struct event device_events[N_DEVS];
} input_dev;
static struct timeval then = { 0, 0 };
static struct event revert_to_auth_event, udev_monitor_event;
static void timeout_start(void)
{
gettimeofday(&then, NULL);
}
static int timeout_expired(void)
{
struct timeval now, diff;
gettimeofday(&now, NULL);
timersub(&now, &then, &diff);
return ((diff.tv_sec < 0) || (diff.tv_sec > 4));
}
static void update_grabs(void)
{
int i = 0;
for (i = 0; i < N_DEVS; i++)
if (input_dev.fds[i] != -1 && input_dev.grab[i] != current_grab)
{
if (ioctl(input_dev.fds[i], EVIOCGRAB, current_grab) == -1)
info("grab failed for event%d: %s", i, strerror(errno));
else
input_dev.grab[i] = current_grab;
}
}
static void do_grab(int grab)
{
info("grab=%d", grab);
current_grab = grab;
update_grabs();
}
/* The following defines are only tempory measure - they should come from input.h */
#define ABS_MT_SLOT 0x2f /* MT slot being modified */
#define ABS_MT_PRESSURE 0x3a /* Pressure on contact area */
#define ABS_MT_DISTANCE 0x3b /* Contact hover distance */
typedef enum
{
nope,
send_sync,
send_left
} sim_press;
typedef enum
{
discared_event,
send_event,
events_queued
} inputevent_action;
static inputevent_action demultitouch(struct input_event *e)
{
static sim_press deferpressed = nope;
static int draining = 0;
static int slot = 0;
static int pressed = 0;
static int had_slot0 = 0;
if (draining && deferpressed == nope)
info("WARNING! draining but not deferpressed!\n");
if ((slot != 0) || (draining))
{
if (!draining)
{
if ((e->type == EV_SYN) && (e->code == SYN_REPORT))
{
slot = 0;
had_slot0 = false;
}
if ((e->type == EV_ABS) && (e->code == ABS_MT_SLOT))
{
slot = e->value;
if (!slot)
had_slot0 = true;
}
}
switch (deferpressed)
{
case send_sync:
e->type = EV_SYN;
e->code = SYN_REPORT;
e->value = 0;
deferpressed = nope;
draining = 0;
return send_event;
case send_left:
e->type = EV_KEY;
e->code = BTN_LEFT;
e->value = 1;
deferpressed = send_sync;
return (draining) ? events_queued : send_event;
default:
return discared_event;
}
return discared_event;
}
if (e->type == EV_SYN)
{
if (e->code == SYN_MT_REPORT)
{
e->code = SYN_REPORT;
slot = 1;
return send_event;
}
else if (e->code == SYN_REPORT)
{
had_slot0 = false;
if (deferpressed)
{
draining = true;
return events_queued;
}
}
return send_event;
}
if (e->type == EV_ABS)
{
switch (e->code)
{
case ABS_MT_POSITION_X:
e->code = ABS_X;
break;
case ABS_MT_POSITION_Y:
e->code = ABS_Y;
break;
case ABS_MT_TRACKING_ID:
if (slot == 0)
{
int nowpressed = (e->value != (int32_t) 0xffffffff);
if (pressed != nowpressed)
{
pressed = nowpressed;
if (!pressed)
{
e->type = EV_KEY;
e->code = BTN_LEFT;
e->value = 0;
}
else
{
had_slot0 = true;
deferpressed = send_left;
return discared_event;
}
}
break;
}
case ABS_MT_SLOT:
slot = e->value;
if (slot != 0)
{
if (had_slot0)
{
e->type = EV_SYN;
e->code = SYN_REPORT;
e->value = 0;
return send_event;
}
return discared_event;
}
case ABS_MT_TOUCH_MAJOR:
case ABS_MT_TOUCH_MINOR:
case ABS_MT_WIDTH_MAJOR:
case ABS_MT_WIDTH_MINOR:
case ABS_MT_ORIENTATION:
case ABS_MT_TOOL_TYPE:
case ABS_MT_BLOB_ID:
case ABS_MT_PRESSURE:
case ABS_MT_DISTANCE:
had_slot0 = true;
return discared_event;
}
return (slot == 0);
}
had_slot0 = true;
return send_event;
}
static void send_config_reset(struct domain *d, uint8_t slot)
{
struct msg_input_config_reset msg;
msg.slot = slot;
input_config_reset(d->client, &msg, sizeof(msg));
if (d->plugin)
send_plugin_dev_event(d->plugin, DEV_RESET, slot);
}
void input_set_focus_change(void)
{
static struct domain* od=NULL;
static int osecure=0;
int secure=input_check_secure_mode();
if ((secure!=osecure) || (!secure && (get_keyb_dest() != od)))
{
osecure=secure;
od = get_keyb_dest();
if (secure)
notify_com_citrix_xenclient_input_keyboard_focus_change(xcbus_conn, SERVICE, OBJ_PATH, "S");
else
notify_com_citrix_xenclient_input_keyboard_focus_change(xcbus_conn, SERVICE, OBJ_PATH,
get_keyb_dest() ? get_keyb_dest()->uuid : "");
}
}
/* Our drivers seem to claim capabilities they do not have, in order to satisfy legacy behavior */
/* This code tries to correct for this */
void fixabsbits(uint64_t * bits)
{
if (*bits & ((uint64_t) 1 << ABS_MT_POSITION_X))
*bits &= ~(1 << ABS_X);
if (*bits & ((uint64_t) 1 << ABS_MT_POSITION_Y))
*bits &= ~(1 << ABS_Y);
}
void fixkeybits(unsigned long *keybits, uint64_t * absbits, int slot)
{
int key;
unsigned long *kb;
unsigned long bit;
if (input_dev.types[slot] == HID_TYPE_TOUCHPAD)
{ // crear out any keys that don't look like mouse keys
keybits[2] = 0;
keybits[0] &= ~ (unsigned long)0x1FF;
} else
if ((*absbits & ((uint64_t) 1 << ABS_MT_POSITION_X)) && (*absbits & ((uint64_t) 1 << ABS_MT_POSITION_Y)))
{
info("Clearing btn_touch for multitouch device.\n");
bit = 1 << OFF(key = BTN_TOUCH - BTN_MISC);
keybits[LONG(key)] &= ~ bit;
}
}
int relbits_to_absbits(struct domain *d, unsigned long *relbits, uint64_t * absbits)
{
if ((*relbits & (1 << REL_X)) && (*relbits & (1 << REL_Y)))
{
*relbits &= ~((1 << REL_Y) | (1 << REL_X));
*absbits |= ((1 << ABS_Y) | (1 << ABS_X));
return 1;
}
return 0;
}
static int anyset(unsigned long *keybit)
{
int i, a;
a = 0;
for (i = 0; i < BTN_WORDS; i++)
a |= keybit[i];
return a;
}
static void send_config_wrap(struct domain *d, void *o)
{
int slot = (int) o;
send_config(d, slot);
if (d->plugin)
send_plugin_dev_event(d->plugin, DEV_CONF, slot);
}
static void send_config_reset_wrap(struct domain *d, void *o)
{
uint8_t slot = (int) o;
send_config_reset(d, slot);
}
static void broadcast_removed_dev(int slot)
{
iterate_domains(send_config_reset_wrap, (void *) slot);
}
static void broadcast_config(int slot)
{
iterate_domains(send_config_wrap, (void *) slot);
}
static void send_config(struct domain *d, int slot)
{
if (!d || d->is_pv_domain)
return;
int fd = input_dev.fds[slot];
int ret = 0;
int is_touchpad=(input_dev.types[slot] == HID_TYPE_TOUCHPAD);
unsigned long eventtypes[NBITS(EV_MAX)];
if ((ret = ioctl(fd, EVIOCGBIT(0, sizeof(eventtypes)), eventtypes)) < 0)
{
info("Could not get evbits.\n");
return;
}
struct msg_input_config *msg;
int absposiblesize = NBITS(ABS_MAX) * sizeof(unsigned long);
int relposiblesize = NBITS(REL_MAX) * sizeof(unsigned long);
int evbits = (uint32_t) eventtypes[0];
int abssize = (evbits & (1 << EV_ABS)) ? absposiblesize : 0;
int relsize = (evbits & (1 << EV_REL)) || is_touchpad ? relposiblesize : 0;
int btnsize = (evbits & (1 << EV_KEY)) ? BTN_WORDS * sizeof(unsigned long) : 0;
unsigned long keybit[NBITS(KEY_OK)];
unsigned long absbit[NBITS(ABS_MAX)];
unsigned long relbit[NBITS(REL_MAX)];
unsigned long *btnbit = &(keybit[NBITS(BTN_MISC)]);
memset(absbit, 0, absposiblesize);
if (abssize)
{
if (is_touchpad)
{
absbit[0] |= ((1 << ABS_Y) | (1 << ABS_X));
}
else if ((ret = ioctl(fd, EVIOCGBIT(EV_ABS, abssize), absbit)) < 0)
{
info("Error: Could not get abs bits.\n");
abssize = 0;
}
else
{
fixabsbits((uint64_t *) absbit);
}
}
if (relsize)
{
if (is_touchpad)
{
relbit[0] = 1 << REL_WHEEL;
}
else if ((ret = ioctl(fd, EVIOCGBIT(EV_REL, relsize), relbit)) < 0)
{
info("Error: Could not get rel bits.\n");
relsize = 0;
}
else
{
if (relbits_to_absbits(d, relbit, (uint64_t *) absbit))
{
info("Making relative mouse absolute.\n");
abssize = absposiblesize;
}
if (relbit[0] == 0)
{
info("All rel bits removed.\n");
relsize = 0;
}
}
}
/* Ugly fix instead of cast to uint64_t */
if (absbit[0] == 0 && absbit[1] == 0)
abssize = 0;
if (btnsize)
{
if ((ret = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit)) < 0)
{
info("Error: Could not get button bits.\n");
btnsize = 0;
}
else
{
fixkeybits(btnbit, (uint64_t *) absbit, slot);
int any = anyset(btnbit);
if (!any)
{
info("Keys reported, but not in range.\n");
btnsize = 0;
}
}
}
int totalsize = abssize + relsize + btnsize + sizeof(struct msg_input_config) - sizeof(uint32_t);
msg = alloca(totalsize);
// Fill in structure
msg->c.evbits =
(evbits & 1) | ((abssize) ? (1 << EV_ABS) : 0) | ((relsize) ? (1 << EV_REL) : 0) | ((btnsize) ? (1 << EV_KEY) :
0);
msg->c.slot = slot;
uint8_t *payload = (uint8_t *) msg->c.bits;
if (ioctl(fd, EVIOCGNAME(sizeof(msg->c.name)), msg->c.name) == -1)
{
sprintf(msg->c.name, "S:%d T:%d", slot, input_dev.types[slot]);
}
msg->c.name[sizeof(msg->c.name) - 1] = 0;
info("Found device: %s\n", msg->c.name);
if (abssize)
{
#ifdef debug
print_abs_bit_meaning((unsigned long *) absbit);
#endif
memcpy(payload, absbit, abssize);
payload += abssize;
}
if (relsize)
{
#ifdef debug
print_rel_bit_meaning((unsigned long *) relbit);
#endif
memcpy(payload, relbit, relsize);
payload += relsize;
}
if (btnsize)
{
#ifdef debug
print_btn_bit_meaning(btnbit);
#endif
memcpy(payload, btnbit, btnsize);
}
input_config(d->client, msg, totalsize);
}
static void send_slot(struct domain *d)
{
struct msg_dom0_input_event msg;
if (NULL == d->client)
return;
msg.type = EV_DEV;
msg.code = DEV_SET;
msg.value = d->last_devslot;
dom0_input_event(d->client, &msg, sizeof(msg));
}
static void input_send(struct domain *d, int slot, struct input_event *e)
{
struct msg_dom0_input_event msg;
if (!d)
return;
if (d->plugin)
{
send_plugin_event(d,slot,e);
return;
}
if ((!(d->is_pv_domain)) && (d->last_devslot!=slot))
{
d->last_devslot = slot;
send_slot(d);
}
#ifdef debug_packets
debug_packet(slot, e);
#endif
inputevent_action ia;
do
{
ia = send_event;
if (d->is_pv_domain)
{
ia = demultitouch(e);
if (ia == discared_event)
return;
xen_vkbd_send_event(d, e);
}
else
{
msg.type = e->type;
msg.code = e->code;
msg.value = e->value;
if (NULL != d->client)
dom0_input_event(d->client, &msg, sizeof(msg));
}
}
while (ia == events_queued);
}
void input_domain_gone(struct domain *d)
{
if (mouse_dest == d)
{
info("lost the mouse domain, ditching mouse_events for now");
mouse_dest = NULL;
}
if (get_keyb_dest() == d)
{
info("lost the keyboard domain, ditching keyboard_events for now");
set_keyb_dest(NULL);
}
if (keyb_parent == d)
{
keyb_parent = NULL;
set_keyb_dest(NULL);
}
if (mouse_parent == d)
{
mouse_parent = NULL;
mouse_dest = NULL;
}
int i;
for (i=0; i<num_mouse_pressed;)
{
if (mouse_pressed[i].domain==d)
{
num_mouse_pressed--;
for (;i<num_mouse_pressed; i++)
memcpy(&mouse_pressed[i], &mouse_pressed[i+1], sizeof(struct mousebutton));
}
else
i++;
}
}
static void input_keyboard_reset(int reset_mouse_domain)
{
int i = 0;
struct input_event e;
if (get_keyb_dest() == NULL)
return;
info("input_keyboard_reset\n");
for (i = 0; i < KEY_STATUS_SIZE; i++)
if (input_dev.key_status[i])
{
e.type = EV_KEY;
e.code = i;
e.value = 0;
input_dev.key_status[i] = 0;
input_send(get_keyb_dest(), INPUTSLOT_DEFAULT, &e);
if (reset_mouse_domain && mouse_dest && (get_keyb_dest() != mouse_dest))
input_send(mouse_dest, INPUTSLOT_DEFAULT, &e);
}
}
void divert_domain_gone(struct divert_info_t* dv,struct domain* d)
{
if (dv->key_domain==d)
dv->key_domain=NULL;
if (dv->mouse_domain==d)
dv->mouse_domain=NULL;
}
void send_keypair(struct keypairs *key, struct domain *d)
{
struct input_event e;
int len = d->divert_info->modifers[0];
uint32_t *m = &d->divert_info->modifers[1];
int i;
e.type = EV_KEY;
e.value = 1;
for (i = 0; i < len; i++)
{
if (key->mod_bits & (1 << i))
{
e.code = m[i];
input_send(d, INPUTSLOT_DEFAULT, &e);
}
}
e.code = key->keycode;
input_send(d, INPUTSLOT_DEFAULT, &e);
/* Now release */
e.value = 0;
input_send(d, INPUTSLOT_DEFAULT, &e);
for (i = 0; i < len; i++)
{
if (key->mod_bits & (1 << i))
{
e.code = m[i];
input_send(d, INPUTSLOT_DEFAULT, &e);
}
}
}
int filter_keys(struct input_event *e)
{
struct divert_info_t *dv;
if (e->type != EV_KEY)
return 0;
if (!keyb_parent)
return 0;
dv = keyb_parent->divert_info;
if (!dv)
{
info("Input_server:filter_keys Warning!\n");
return 0;
}
if (!dv->keylist)
return 0;
int len = dv->modifers[0];
uint32_t *m = &dv->modifers[1];
int i;
// Find modifiers
for (i = 0; i < len; i++)
{
if (m[i] == e->code)
{
uint32_t mask = 1 << i;
if (e->value)
keyb_modbits |= mask;
else
keyb_modbits &= ~mask;
break;
}
}
// Filter action codes
for (i = 0; i < dv->num_keys; i++)
{
struct keypairs *kp = &dv->keylist[i];
if ((kp->keycode == e->code) && (kp->mod_bits == keyb_modbits))
{
if (e->value)
{
send_keypair(&dv->keylist[i], keyb_parent);
}
return 1;
}
}
return 0;
}
void set_kbd_domain(struct domain *d)
{
struct divert_info_t *dv = d->divert_info;
if (dv && (dv->key_domain))
{
keyb_parent = d;
keyb_modbits = 0;
set_keyb_dest(dv->key_domain);
keyb_waits_for_click = 1;
}
else
{
set_keyb_dest(d);
keyb_modbits = 0;
keyb_parent = NULL;
if (dv && (dv->mouse_domain) && (dv->mouse_domain!=d))
keyb_waits_for_click = 1;
}
}
void sync_mouse_domain(struct domain *d)
{
if ((mouse_parent == d) || (mouse_dest == d))
input_set_mouse(d);
// if mouse pressed, and click was within current frame, repeat click.
dup_mouse_clicks(d);
}
void sync_kbd_domain(struct domain *d)
{
if ((keyb_parent == d) || (get_keyb_dest() == d))
input_set_keyb(d);
}
void set_mouse_domain(struct domain *d)
{
struct divert_info_t *dv = d->divert_info;
if (dv && (dv->mouse_domain))
{
mouse_dest = dv->mouse_domain;
mouse_parent = d;
keyb_waits_for_click = 1;
}
else
{
mouse_dest = d;
mouse_parent = NULL;
}
// Just te be sure, check key focus anyway
input_set_focus_change();
}
void input_set_mouse(struct domain *d)
{
if (keyb_waits_for_click)
{
/* We mouse-switched and never clicked in the last VM. */
if (get_keyb_dest() == d)
/* We are just back where the keyboard is */
keyb_waits_for_click = 0;
}
else
keyb_waits_for_click = 1;
do_grab(1);
set_mouse_domain(d);
info("mouse input now directed to domid %d", mouse_dest ? mouse_dest->domid : -1);
}
void input_set_keyb(struct domain *d)
{
if (get_keyb_dest() != NULL)
{
/* Reset the keyboard of the previous domain before changing the
* keyboard and mouse destination. */
input_keyboard_reset(0);
/* Turn numlock off before switching to another domain. Note that this
* needs to be done after sending the Ctrl key up event, in the case of
* switching between domains using Ctrl+slot number. This is because
* pressing numlock does not have any effect when the Ctrl key is
* pressed. */
if (!input_get_numlock_restore_on_switch())
turn_numlock_off();
}
keyb_waits_for_click = 0;
do_grab(1);
set_kbd_domain(d);
info("keyboard input now directed to domid %d", get_keyb_dest() ? get_keyb_dest()->domid : -1);
}
void input_set(struct domain *d)
{
struct timeval now;
if (get_keyb_dest() != NULL)
{
/* Reset the keyboard of the previous domain before changing the
* keyboard and mouse destination. */
input_keyboard_reset(1);
/* Turn numlock off before switching to another domain. Note that this
* needs to be done after sending the Ctrl key up event, in the case of
* switching between domains using Ctrl+slot number. This is because
* pressing numlock does not have any effect when the Ctrl key is
* pressed. */
if (!input_get_numlock_restore_on_switch())
turn_numlock_off();
}
keyb_waits_for_click = 0;
do_grab(1);
set_mouse_domain(d);
set_kbd_domain(d);
if (mouse_dest == get_keyb_dest())
info("all input now directed to domid:%d abs_enabled:%d", mouse_dest ? mouse_dest->domid : -1,
mouse_dest->abs_enabled);
else
{
info("keyboard input now directed to domid %d", get_keyb_dest() ? get_keyb_dest()->domid : -1);
info("mouse input now directed to domid %d", mouse_dest ? mouse_dest->domid : -1);
}
gettimeofday(&now, NULL);
d->last_input_event = now;
}
/* Give the keyboard to domain d. */
void input_give_keyboard(struct domain *d)
{
if (d == get_keyb_dest())
return;
/* When switching between seamless apps shared by different VMs, sometimes
* the keyboard take command from the second VM is received before the
* keyboard release command from the first VM. In this case, the keyboard
* of the first VM will not be reset, so reset it here. */
if (get_keyb_dest() != mouse_dest)
input_keyboard_reset(0);
if (!input_get_numlock_restore_on_switch())
turn_numlock_off();
set_keyb_dest(d);
keyb_parent = NULL;
keyb_modbits = 0;
if (get_keyb_dest() != NULL)
{
input_led_code(get_keyb_dest()->keyboard_led_code, get_keyb_dest()->domid);
}
info("keyboard now directed to domid %d (mouse is on %d)", d ? d->domid : -1, mouse_dest ? mouse_dest->domid : -1);
}
/* Return the keyboard from domain d. */
void input_return_keyboard(struct domain *d)
{
if (d != get_keyb_dest())
return;
if (get_keyb_dest() != mouse_dest)
input_keyboard_reset(0);
if (!input_get_numlock_restore_on_switch())
turn_numlock_off();
set_keyb_dest(mouse_dest);
keyb_parent = NULL;
keyb_modbits = 0;
if (get_keyb_dest() != NULL)
{
input_led_code(get_keyb_dest()->keyboard_led_code, get_keyb_dest()->domid);
}
info("keyboard now returned to domid %d (mouse is on %d)",
get_keyb_dest() ? get_keyb_dest()->domid : -1,
mouse_dest ? mouse_dest->domid : -1);
}
/* For domain d, give the keyboard to domain new_keyb_dest. */