forked from ValveSoftware/wine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
3852 lines (3355 loc) · 129 KB
/
queue.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
/*
* Server-side message queues
*
* Copyright (C) 2000 Alexandre Julliard
*
* 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 St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <poll.h>
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
#include "winternl.h"
#include "handle.h"
#include "file.h"
#include "thread.h"
#include "process.h"
#include "request.h"
#include "user.h"
#include "esync.h"
#include "fsync.h"
#define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
#define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
enum message_kind { SEND_MESSAGE, POST_MESSAGE };
#define NB_MSG_KINDS (POST_MESSAGE+1)
struct message_result
{
struct list sender_entry; /* entry in sender list */
struct message *msg; /* message the result is for */
struct message_result *recv_next; /* next in receiver list */
struct msg_queue *sender; /* sender queue */
struct msg_queue *receiver; /* receiver queue */
int replied; /* has it been replied to? */
unsigned int error; /* error code to pass back to sender */
lparam_t result; /* reply result */
struct message *hardware_msg; /* hardware message if low-level hook result */
struct desktop *desktop; /* desktop for hardware message */
struct message *callback_msg; /* message to queue for callback */
void *data; /* message reply data */
unsigned int data_size; /* size of message reply data */
struct timeout_user *timeout; /* result timeout */
thread_id_t hook_thread_id;/* Hook owner thread id. */
client_ptr_t hook_proc; /* Hook proc address. */
};
struct message
{
struct list entry; /* entry in message list */
enum message_type type; /* message type */
user_handle_t win; /* window handle */
unsigned int msg; /* message code */
lparam_t wparam; /* parameters */
lparam_t lparam; /* parameters */
int x; /* message position */
int y;
unsigned int time; /* message time */
void *data; /* message data for sent messages */
unsigned int data_size; /* size of message data */
unsigned int unique_id; /* unique id for nested hw message waits */
struct message_result *result; /* result in sender queue */
};
struct timer
{
struct list entry; /* entry in timer list */
abstime_t when; /* next expiration */
unsigned int rate; /* timer rate in ms */
user_handle_t win; /* window handle */
unsigned int msg; /* message to post */
lparam_t id; /* timer id */
lparam_t lparam; /* lparam for message */
};
struct thread_input
{
struct object obj; /* object header */
struct desktop *desktop; /* desktop that this thread input belongs to */
int caret_hide; /* caret hide count */
int caret_state; /* caret on/off state */
struct list msg_list; /* list of hardware messages */
unsigned char desktop_keystate[256]; /* desktop keystate when keystate was synced */
struct object *shared_mapping; /* thread input shared memory mapping */
volatile struct input_shared_memory *shared; /* thread input shared memory ptr */
};
struct msg_queue
{
struct object obj; /* object header */
struct fd *fd; /* optional file descriptor to poll */
unsigned int wake_bits; /* wakeup bits */
unsigned int wake_mask; /* wakeup mask */
unsigned int changed_bits; /* changed wakeup bits */
unsigned int changed_mask; /* changed wakeup mask */
int paint_count; /* pending paint messages count */
int hotkey_count; /* pending hotkey messages count */
int quit_message; /* is there a pending quit message? */
int exit_code; /* exit code of pending quit message */
int cursor_count; /* per-queue cursor show count */
int destroyed; /* queue has been cleaned up */
struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
struct list send_result; /* stack of sent messages waiting for result */
struct list callback_result; /* list of callback messages waiting for result */
struct message_result *recv_result; /* stack of received messages waiting for result */
struct list pending_timers; /* list of pending timers */
struct list expired_timers; /* list of expired timers */
lparam_t next_timer_id; /* id for the next timer with a 0 window */
struct timeout_user *timeout; /* timeout for next timer to expire */
struct thread_input *input; /* thread input descriptor */
struct hook_table *hooks; /* hook table */
timeout_t last_get_msg; /* time of last get message call */
int keystate_lock; /* owns an input keystate lock */
int esync_fd; /* esync file descriptor (signalled on message) */
int esync_in_msgwait; /* our thread is currently waiting on us */
unsigned int fsync_idx;
int fsync_in_msgwait; /* our thread is currently waiting on us */
volatile struct queue_shared_memory *shared; /* thread queue shared memory ptr */
};
struct hotkey
{
struct list entry; /* entry in desktop hotkey list */
struct msg_queue *queue; /* queue owning this hotkey */
user_handle_t win; /* window handle */
int id; /* hotkey id */
unsigned int vkey; /* virtual key code */
unsigned int flags; /* key modifiers */
};
static void msg_queue_dump( struct object *obj, int verbose );
static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
static int msg_queue_get_esync_fd( struct object *obj, enum esync_type *type );
static unsigned int msg_queue_get_fsync_idx( struct object *obj, enum fsync_type *type );
static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
static void msg_queue_destroy( struct object *obj );
static void msg_queue_poll_event( struct fd *fd, int event );
static void thread_input_dump( struct object *obj, int verbose );
static void thread_input_destroy( struct object *obj );
static void timer_callback( void *private );
static const struct object_ops msg_queue_ops =
{
sizeof(struct msg_queue), /* size */
&no_type, /* type */
msg_queue_dump, /* dump */
msg_queue_add_queue, /* add_queue */
msg_queue_remove_queue, /* remove_queue */
msg_queue_signaled, /* signaled */
msg_queue_get_esync_fd, /* get_esync_fd */
msg_queue_get_fsync_idx, /* get_fsync_idx */
msg_queue_satisfied, /* satisfied */
no_signal, /* signal */
no_get_fd, /* get_fd */
default_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
no_get_full_name, /* get_full_name */
no_lookup_name, /* lookup_name */
no_link_name, /* link_name */
NULL, /* unlink_name */
no_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
no_close_handle, /* close_handle */
msg_queue_destroy /* destroy */
};
static const struct fd_ops msg_queue_fd_ops =
{
NULL, /* get_poll_events */
msg_queue_poll_event, /* poll_event */
NULL, /* flush */
NULL, /* get_fd_type */
NULL, /* ioctl */
NULL, /* queue_async */
NULL, /* reselect_async */
NULL /* cancel async */
};
static const struct object_ops thread_input_ops =
{
sizeof(struct thread_input), /* size */
&no_type, /* type */
thread_input_dump, /* dump */
no_add_queue, /* add_queue */
NULL, /* remove_queue */
NULL, /* signaled */
NULL, /* get_esync_fd */
NULL, /* get_fsync_idx */
NULL, /* satisfied */
no_signal, /* signal */
no_get_fd, /* get_fd */
default_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
no_get_full_name, /* get_full_name */
no_lookup_name, /* lookup_name */
no_link_name, /* link_name */
NULL, /* unlink_name */
no_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
no_close_handle, /* close_handle */
thread_input_destroy /* destroy */
};
/* pointer to input structure of foreground thread */
static unsigned int last_input_time;
static cursor_pos_t cursor_history[64];
static unsigned int cursor_history_latest;
#if defined(__i386__) || defined(__x86_64__)
#define SHARED_WRITE_BEGIN( x ) \
do { \
volatile unsigned int __seq = *(x); \
assert( (__seq & SEQUENCE_MASK) != SEQUENCE_MASK ); \
*(x) = ++__seq; \
} while(0)
#define SHARED_WRITE_END( x ) \
do { \
volatile unsigned int __seq = *(x); \
assert( (__seq & SEQUENCE_MASK) != 0 ); \
if ((__seq & SEQUENCE_MASK) > 1) __seq--; \
else __seq += SEQUENCE_MASK; \
*(x) = __seq; \
} while(0)
#else
#define SHARED_WRITE_BEGIN( x ) \
do { \
assert( (*(x) & SEQUENCE_MASK) != SEQUENCE_MASK ); \
if ((__atomic_add_fetch( x, 1, __ATOMIC_RELAXED ) & SEQUENCE_MASK) == 1) \
__atomic_thread_fence( __ATOMIC_RELEASE ); \
} while(0)
#define SHARED_WRITE_END( x ) \
do { \
assert( (*(x) & SEQUENCE_MASK) != 0 ); \
if ((*(x) & SEQUENCE_MASK) > 1) \
__atomic_sub_fetch( x, 1, __ATOMIC_RELAXED ); \
else { \
__atomic_thread_fence( __ATOMIC_RELEASE ); \
__atomic_add_fetch( x, SEQUENCE_MASK, __ATOMIC_RELAXED ); \
} \
} while(0)
#endif
static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
static void free_message( struct message *msg );
/* set the caret window in a given thread input */
static void set_caret_window( struct thread_input *input, user_handle_t win )
{
SHARED_WRITE_BEGIN( &input->shared->seq );
if (!win || win != input->shared->caret)
{
input->shared->caret_rect.left = 0;
input->shared->caret_rect.top = 0;
input->shared->caret_rect.right = 0;
input->shared->caret_rect.bottom = 0;
}
input->shared->caret = win;
input->caret_hide = 1;
input->caret_state = 0;
SHARED_WRITE_END( &input->shared->seq );
}
/* create a thread input object */
static struct thread_input *create_thread_input( struct thread *thread )
{
struct thread_input *input;
if ((input = alloc_object( &thread_input_ops )))
{
input->shared_mapping = grab_object( thread->input_shared_mapping );
input->shared = thread->input_shared;
SHARED_WRITE_BEGIN( &input->shared->seq );
input->shared->focus = 0;
input->shared->capture = 0;
input->shared->active = 0;
input->shared->menu_owner = 0;
input->shared->move_size = 0;
input->shared->cursor = 0;
input->shared->cursor_count = 0;
input->shared->keystate_lock = 0;
memset( (void *)input->shared->keystate, 0, sizeof(input->shared->keystate) );
SHARED_WRITE_END( &input->shared->seq );
list_init( &input->msg_list );
set_caret_window( input, 0 );
if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
{
release_object( input );
return NULL;
}
memcpy( input->desktop_keystate, (void *)input->desktop->shared->keystate,
sizeof(input->desktop_keystate) );
SHARED_WRITE_BEGIN( &input->shared->seq );
input->shared->created = TRUE;
SHARED_WRITE_END( &input->shared->seq );
}
return input;
}
/* create a message queue object */
static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
{
struct thread_input *new_input = NULL;
struct msg_queue *queue;
int i;
if (!input)
{
if (!(new_input = create_thread_input( thread ))) return NULL;
input = new_input;
}
if ((queue = alloc_object( &msg_queue_ops )))
{
queue->fd = NULL;
queue->wake_bits = 0;
queue->wake_mask = 0;
queue->changed_bits = 0;
queue->changed_mask = 0;
queue->paint_count = 0;
queue->hotkey_count = 0;
queue->quit_message = 0;
queue->cursor_count = 0;
queue->destroyed = 0;
queue->recv_result = NULL;
queue->next_timer_id = 0x7fff;
queue->timeout = NULL;
queue->input = (struct thread_input *)grab_object( input );
queue->hooks = NULL;
queue->last_get_msg = current_time;
queue->keystate_lock = 0;
queue->esync_fd = -1;
queue->esync_in_msgwait = 0;
queue->fsync_idx = 0;
queue->fsync_in_msgwait = 0;
queue->shared = thread->queue_shared;
list_init( &queue->send_result );
list_init( &queue->callback_result );
list_init( &queue->pending_timers );
list_init( &queue->expired_timers );
for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
if (do_fsync())
queue->fsync_idx = fsync_alloc_shm( 0, 0 );
if (do_esync())
queue->esync_fd = esync_create_fd( 0, 0 );
SHARED_WRITE_BEGIN( &queue->shared->seq );
queue->shared->created = TRUE;
SHARED_WRITE_END( &queue->shared->seq );
thread->queue = queue;
}
if (new_input)
{
SHARED_WRITE_BEGIN( &queue->shared->seq );
queue->shared->input_tid = new_input->shared->tid;
SHARED_WRITE_END( &queue->shared->seq );
release_object( new_input );
}
return queue;
}
/* synchronize thread input keystate with the desktop */
static void sync_input_keystate( struct thread_input *input )
{
int i;
if (!input->desktop || input->shared->keystate_lock) return;
SHARED_WRITE_BEGIN( &input->shared->seq );
for (i = 0; i < sizeof(input->shared->keystate); ++i)
{
if (input->desktop_keystate[i] == input->desktop->shared->keystate[i]) continue;
input->shared->keystate[i] = input->desktop_keystate[i] = input->desktop->shared->keystate[i];
}
input->shared->sync_serial = input->desktop->shared->update_serial;
SHARED_WRITE_END( &input->shared->seq );
}
/* locks thread input keystate to prevent synchronization */
static void lock_input_keystate( struct thread_input *input )
{
SHARED_WRITE_BEGIN( &input->shared->seq );
input->shared->keystate_lock++;
SHARED_WRITE_END( &input->shared->seq );
}
/* unlock the thread input keystate and synchronize it again */
static void unlock_input_keystate( struct thread_input *input )
{
SHARED_WRITE_BEGIN( &input->shared->seq );
input->shared->keystate_lock--;
SHARED_WRITE_END( &input->shared->seq );
if (!input->shared->keystate_lock) sync_input_keystate( input );
}
/* change the thread input data of a given thread */
static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
{
struct msg_queue *queue = thread->queue;
if (!queue)
{
thread->queue = create_msg_queue( thread, new_input );
return thread->queue != NULL;
}
if (queue->input)
{
SHARED_WRITE_BEGIN( &queue->input->shared->seq );
queue->input->shared->cursor_count -= queue->cursor_count;
SHARED_WRITE_END( &queue->input->shared->seq );
if (queue->keystate_lock) unlock_input_keystate( queue->input );
release_object( queue->input );
}
queue->input = (struct thread_input *)grab_object( new_input );
if (queue->keystate_lock) lock_input_keystate( queue->input );
SHARED_WRITE_BEGIN( &new_input->shared->seq );
new_input->shared->cursor_count += queue->cursor_count;
SHARED_WRITE_END( &new_input->shared->seq );
SHARED_WRITE_BEGIN( &queue->shared->seq );
queue->shared->input_tid = queue->input->shared->tid;
SHARED_WRITE_END( &queue->shared->seq );
return 1;
}
/* allocate a hardware message and its data */
static struct message *alloc_hardware_message( lparam_t info, struct hw_msg_source source,
unsigned int time, data_size_t extra_size )
{
struct hardware_msg_data *msg_data;
struct message *msg;
if (!(msg = mem_alloc( sizeof(*msg) ))) return NULL;
if (!(msg_data = mem_alloc( sizeof(*msg_data) + extra_size )))
{
free( msg );
return NULL;
}
memset( msg, 0, sizeof(*msg) );
msg->type = MSG_HARDWARE;
msg->time = time;
msg->data = msg_data;
msg->data_size = sizeof(*msg_data) + extra_size;
memset( msg_data, 0, sizeof(*msg_data) + extra_size );
msg_data->info = info;
msg_data->size = msg->data_size;
msg_data->source = source;
return msg;
}
static int update_desktop_cursor_pos( struct desktop *desktop, int x, int y )
{
int updated;
unsigned int time = get_tick_count();
x = max( min( x, desktop->shared->cursor.clip.right - 1 ), desktop->shared->cursor.clip.left );
y = max( min( y, desktop->shared->cursor.clip.bottom - 1 ), desktop->shared->cursor.clip.top );
updated = (desktop->shared->cursor.x != x || desktop->shared->cursor.y != y);
SHARED_WRITE_BEGIN( &desktop->shared->seq );
desktop->shared->cursor.x = x;
desktop->shared->cursor.y = y;
desktop->shared->cursor.last_change = time;
SHARED_WRITE_END( &desktop->shared->seq );
return updated;
}
/* set the cursor position and queue the corresponding mouse message */
static void set_cursor_pos( struct desktop *desktop, int x, int y )
{
static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
const struct rawinput_device *device;
struct message *msg;
if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
{
update_desktop_cursor_pos( desktop, x, y );
return;
}
if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
msg->msg = WM_MOUSEMOVE;
msg->x = x;
msg->y = y;
queue_hardware_message( desktop, msg, 1 );
}
/* retrieve default position and time for synthesized messages */
static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
{
struct desktop *desktop = queue->input->desktop;
*x = desktop->shared->cursor.x;
*y = desktop->shared->cursor.y;
*time = get_tick_count();
}
/* set the cursor clip rectangle */
void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, int send_clip_msg )
{
rectangle_t top_rect, new_rect;
int x, y;
get_top_window_rectangle( desktop, &top_rect );
if (rect)
{
new_rect = *rect;
if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
}
else new_rect = top_rect;
SHARED_WRITE_BEGIN( &desktop->shared->seq );
desktop->shared->cursor.clip = new_rect;
if (desktop->cursor_clip_msg && send_clip_msg)
post_desktop_message( desktop, desktop->cursor_clip_msg, rect != NULL, 0 );
/* warp the mouse to be inside the clip rect */
x = max( min( desktop->shared->cursor.x, desktop->shared->cursor.clip.right - 1 ), desktop->shared->cursor.clip.left );
y = max( min( desktop->shared->cursor.y, desktop->shared->cursor.clip.bottom - 1 ), desktop->shared->cursor.clip.top );
if (x != desktop->shared->cursor.x || y != desktop->shared->cursor.y) set_cursor_pos( desktop, x, y );
SHARED_WRITE_END( &desktop->shared->seq );
}
/* change the foreground input and reset the cursor clip rect */
static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
{
if (desktop->foreground_input == input) return;
set_clip_rectangle( desktop, NULL, 1 );
desktop->foreground_input = input;
SHARED_WRITE_BEGIN( &desktop->shared->seq );
desktop->shared->foreground_tid = input ? input->shared->tid : 0;
SHARED_WRITE_END( &desktop->shared->seq );
}
/* get the hook table for a given thread */
struct hook_table *get_queue_hooks( struct thread *thread )
{
if (!thread->queue) return NULL;
return thread->queue->hooks;
}
/* set the hook table for a given thread, allocating the queue if needed */
void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
{
struct msg_queue *queue = thread->queue;
if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
if (queue->hooks) release_object( queue->hooks );
queue->hooks = hooks;
}
/* check the queue status */
static inline int is_signaled( struct msg_queue *queue )
{
return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
}
/* set some queue bits */
static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
{
if (bits & (QS_KEY | QS_MOUSEBUTTON))
{
if (!queue->keystate_lock) lock_input_keystate( queue->input );
queue->keystate_lock = 1;
}
queue->wake_bits |= bits;
queue->changed_bits |= bits;
SHARED_WRITE_BEGIN( &queue->shared->seq );
queue->shared->wake_bits = queue->wake_bits;
queue->shared->changed_bits = queue->changed_bits;
SHARED_WRITE_END( &queue->shared->seq );
if (is_signaled( queue )) wake_up( &queue->obj, 0 );
}
/* clear some queue bits */
static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
{
queue->wake_bits &= ~bits;
queue->changed_bits &= ~bits;
if (!(queue->wake_bits & (QS_KEY | QS_MOUSEBUTTON)))
{
if (queue->keystate_lock) unlock_input_keystate( queue->input );
queue->keystate_lock = 0;
}
if (do_fsync() && !is_signaled( queue ))
fsync_clear( &queue->obj );
if (do_esync() && !is_signaled( queue ))
esync_clear( queue->esync_fd );
SHARED_WRITE_BEGIN( &queue->shared->seq );
queue->shared->wake_bits = queue->wake_bits;
queue->shared->changed_bits = queue->changed_bits;
SHARED_WRITE_END( &queue->shared->seq );
}
/* check whether msg is a keyboard message */
static inline int is_keyboard_msg( struct message *msg )
{
return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
}
/* check if message is matched by the filter */
static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
{
return (msg >= first && msg <= last);
}
/* check whether a message filter contains at least one potential hardware message */
static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
{
/* hardware message ranges are (in numerical order):
* WM_NCMOUSEFIRST .. WM_NCMOUSELAST
* WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
* WM_MOUSEFIRST .. WM_MOUSELAST
*/
if (last < WM_NCMOUSEFIRST) return 0;
if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
if (first > WM_MOUSELAST) return 0;
return 1;
}
/* get the QS_* bit corresponding to a given hardware message */
static inline int get_hardware_msg_bit( struct message *msg )
{
if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE ||
msg->msg == WM_POINTERDOWN || msg->msg == WM_POINTERUP ||
msg->msg == WM_POINTERUPDATE) return QS_MOUSEMOVE;
if (is_keyboard_msg( msg )) return QS_KEY;
return QS_MOUSEBUTTON;
}
/* get the current thread queue, creating it if needed */
static inline struct msg_queue *get_current_queue(void)
{
struct msg_queue *queue = current->queue;
if (!queue) queue = create_msg_queue( current, NULL );
return queue;
}
/* get a (pseudo-)unique id to tag hardware messages */
static inline unsigned int get_unique_id(void)
{
static unsigned int id;
if (!++id) id = 1; /* avoid an id of 0 */
return id;
}
static int merge_pointer_update_message( struct thread_input *input, const struct message *msg )
{
struct hardware_msg_data *prev_data, *msg_data = msg->data;
struct message *prev;
struct list *ptr;
for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
{
prev = LIST_ENTRY( ptr, struct message, entry );
if (prev->msg != WM_POINTERUPDATE || !(prev_data = prev->data)) continue;
if (LOWORD(prev_data->rawinput.mouse.data) == LOWORD(msg_data->rawinput.mouse.data)) break;
}
if (!ptr) return 0;
if (prev->result) return 0;
if (prev->win && msg->win && prev->win != msg->win) return 0;
if (prev->type != msg->type) return 0;
/* now we can merge it */
prev->wparam = msg->wparam;
prev->lparam = msg->lparam;
prev->x = msg->x;
prev->y = msg->y;
prev->time = msg->time;
prev_data->rawinput.mouse.data |= msg_data->rawinput.mouse.data;
prev_data->rawinput.mouse.x = msg_data->rawinput.mouse.x;
prev_data->rawinput.mouse.y = msg_data->rawinput.mouse.y;
list_remove( ptr );
list_add_tail( &input->msg_list, ptr );
return 1;
}
/* try to merge a message with the last in the list; return 1 if successful */
static int merge_message( struct thread_input *input, const struct message *msg )
{
struct message *prev;
struct list *ptr;
if (msg->msg == WM_POINTERUPDATE) return merge_pointer_update_message( input, msg );
if (msg->msg != WM_MOUSEMOVE) return 0;
for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
{
prev = LIST_ENTRY( ptr, struct message, entry );
if (prev->msg != WM_INPUT && prev->msg != WM_POINTERUPDATE) break;
}
if (!ptr) return 0;
if (prev->result) return 0;
if (prev->win && msg->win && prev->win != msg->win) return 0;
if (prev->msg != msg->msg) return 0;
if (prev->type != msg->type) return 0;
/* now we can merge it */
prev->wparam = msg->wparam;
prev->lparam = msg->lparam;
prev->x = msg->x;
prev->y = msg->y;
prev->time = msg->time;
if (msg->type == MSG_HARDWARE && prev->data && msg->data)
{
struct hardware_msg_data *prev_data = prev->data;
struct hardware_msg_data *msg_data = msg->data;
prev_data->info = msg_data->info;
}
list_remove( ptr );
list_add_tail( &input->msg_list, ptr );
return 1;
}
/* free a result structure */
static void free_result( struct message_result *result )
{
if (result->timeout) remove_timeout_user( result->timeout );
free( result->data );
if (result->callback_msg) free_message( result->callback_msg );
if (result->hardware_msg) free_message( result->hardware_msg );
if (result->desktop) release_object( result->desktop );
free( result );
}
/* remove the result from the sender list it is on */
static inline void remove_result_from_sender( struct message_result *result )
{
assert( result->sender );
list_remove( &result->sender_entry );
result->sender = NULL;
if (!result->receiver) free_result( result );
}
/* store the message result in the appropriate structure */
static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
{
res->result = result;
res->error = error;
res->replied = 1;
if (res->timeout)
{
remove_timeout_user( res->timeout );
res->timeout = NULL;
}
if (res->hardware_msg)
{
if (!error && result) /* rejected by the hook */
free_message( res->hardware_msg );
else
queue_hardware_message( res->desktop, res->hardware_msg, 0 );
res->hardware_msg = NULL;
}
if (res->sender)
{
if (res->callback_msg)
{
/* queue the callback message in the sender queue */
struct callback_msg_data *data = res->callback_msg->data;
data->result = result;
list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
set_queue_bits( res->sender, QS_SENDMESSAGE );
res->callback_msg = NULL;
remove_result_from_sender( res );
}
else
{
/* wake sender queue if waiting on this result */
if (list_head(&res->sender->send_result) == &res->sender_entry)
set_queue_bits( res->sender, QS_SMRESULT );
}
}
else if (!res->receiver) free_result( res );
}
/* free a message when deleting a queue or window */
static void free_message( struct message *msg )
{
struct message_result *result = msg->result;
if (result)
{
result->msg = NULL;
result->receiver = NULL;
store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
}
free( msg->data );
free( msg );
}
/* remove (and free) a message from a message list */
static void remove_queue_message( struct msg_queue *queue, struct message *msg,
enum message_kind kind )
{
list_remove( &msg->entry );
switch(kind)
{
case SEND_MESSAGE:
if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
break;
case POST_MESSAGE:
if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
clear_queue_bits( queue, QS_HOTKEY );
break;
}
free_message( msg );
}
/* message timed out without getting a reply */
static void result_timeout( void *private )
{
struct message_result *result = private;
assert( !result->replied );
result->timeout = NULL;
if (result->msg) /* not received yet */
{
struct message *msg = result->msg;
if (result->sender && result->hook_thread_id && result->hook_proc)
{
if (debug_level > 1)
fprintf( stderr, "disabling hung hook: tid %04x, proc %#lx\n",
result->hook_thread_id, (unsigned long)result->hook_proc );
disable_hung_hook( result->sender->input->desktop, msg->msg, result->hook_thread_id, result->hook_proc );
}
result->msg = NULL;
msg->result = NULL;
remove_queue_message( result->receiver, msg, SEND_MESSAGE );
result->receiver = NULL;
}
store_message_result( result, 0, STATUS_TIMEOUT );
}
/* allocate and fill a message result structure */
static struct message_result *alloc_message_result( struct msg_queue *send_queue,
struct msg_queue *recv_queue,
struct message *msg, timeout_t timeout,
thread_id_t hook_thread_id, client_ptr_t hook_proc)
{
struct message_result *result = mem_alloc( sizeof(*result) );
if (result)
{
result->msg = msg;
result->sender = send_queue;
result->receiver = recv_queue;
result->replied = 0;
result->data = NULL;
result->data_size = 0;
result->timeout = NULL;
result->hardware_msg = NULL;
result->desktop = NULL;
result->callback_msg = NULL;
result->hook_thread_id = hook_thread_id;
result->hook_proc = hook_proc;
if (msg->type == MSG_CALLBACK)
{
struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
if (!callback_msg)
{
free( result );
return NULL;
}
callback_msg->type = MSG_CALLBACK_RESULT;
callback_msg->win = msg->win;
callback_msg->msg = msg->msg;
callback_msg->wparam = 0;
callback_msg->lparam = 0;
callback_msg->time = get_tick_count();
callback_msg->result = NULL;
/* steal the data from the original message */
callback_msg->data = msg->data;
callback_msg->data_size = msg->data_size;
msg->data = NULL;
msg->data_size = 0;
result->callback_msg = callback_msg;
list_add_head( &send_queue->callback_result, &result->sender_entry );
}
else if (send_queue)
{
list_add_head( &send_queue->send_result, &result->sender_entry );
clear_queue_bits( send_queue, QS_SMRESULT );
}
if (timeout != TIMEOUT_INFINITE)
result->timeout = add_timeout_user( timeout, result_timeout, result );
}
return result;
}
/* receive a message, removing it from the sent queue */
static void receive_message( struct msg_queue *queue, struct message *msg,
struct get_message_reply *reply )
{
struct message_result *result = msg->result;
reply->total = msg->data_size;
if (msg->data_size > get_reply_max_size())
{
set_error( STATUS_BUFFER_OVERFLOW );
return;
}
reply->type = msg->type;
reply->win = msg->win;
reply->msg = msg->msg;
reply->wparam = msg->wparam;
reply->lparam = msg->lparam;
reply->x = msg->x;
reply->y = msg->y;
reply->time = msg->time;
if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
list_remove( &msg->entry );
/* put the result on the receiver result stack */
if (result)
{
result->msg = NULL;
result->recv_next = queue->recv_result;
queue->recv_result = result;
}
free( msg );
if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
}
/* set the result of the current received message */
static void reply_message( struct msg_queue *queue, lparam_t result,
unsigned int error, int remove, const void *data, data_size_t len )
{
struct message_result *res = queue->recv_result;
if (remove)
{
queue->recv_result = res->recv_next;
res->receiver = NULL;
if (!res->sender && !res->hardware_msg) /* no one waiting for it */
{
free_result( res );
return;
}