queue.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399
  1. /*
  2. * Server-side message queues
  3. *
  4. * Copyright (C) 2000 Alexandre Julliard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include <assert.h>
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <poll.h>
  27. #include "ntstatus.h"
  28. #define WIN32_NO_STATUS
  29. #include "windef.h"
  30. #include "winbase.h"
  31. #include "wingdi.h"
  32. #include "winuser.h"
  33. #include "winternl.h"
  34. #include "handle.h"
  35. #include "file.h"
  36. #include "thread.h"
  37. #include "process.h"
  38. #include "request.h"
  39. #include "user.h"
  40. #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
  41. #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
  42. enum message_kind { SEND_MESSAGE, POST_MESSAGE };
  43. #define NB_MSG_KINDS (POST_MESSAGE+1)
  44. struct message_result
  45. {
  46. struct list sender_entry; /* entry in sender list */
  47. struct message *msg; /* message the result is for */
  48. struct message_result *recv_next; /* next in receiver list */
  49. struct msg_queue *sender; /* sender queue */
  50. struct msg_queue *receiver; /* receiver queue */
  51. int replied; /* has it been replied to? */
  52. unsigned int error; /* error code to pass back to sender */
  53. lparam_t result; /* reply result */
  54. struct message *hardware_msg; /* hardware message if low-level hook result */
  55. struct desktop *desktop; /* desktop for hardware message */
  56. struct message *callback_msg; /* message to queue for callback */
  57. void *data; /* message reply data */
  58. unsigned int data_size; /* size of message reply data */
  59. struct timeout_user *timeout; /* result timeout */
  60. };
  61. struct message
  62. {
  63. struct list entry; /* entry in message list */
  64. enum message_type type; /* message type */
  65. user_handle_t win; /* window handle */
  66. unsigned int msg; /* message code */
  67. lparam_t wparam; /* parameters */
  68. lparam_t lparam; /* parameters */
  69. int x; /* message position */
  70. int y;
  71. unsigned int time; /* message time */
  72. void *data; /* message data for sent messages */
  73. unsigned int data_size; /* size of message data */
  74. unsigned int unique_id; /* unique id for nested hw message waits */
  75. struct message_result *result; /* result in sender queue */
  76. };
  77. struct timer
  78. {
  79. struct list entry; /* entry in timer list */
  80. abstime_t when; /* next expiration */
  81. unsigned int rate; /* timer rate in ms */
  82. user_handle_t win; /* window handle */
  83. unsigned int msg; /* message to post */
  84. lparam_t id; /* timer id */
  85. lparam_t lparam; /* lparam for message */
  86. };
  87. struct thread_input
  88. {
  89. struct object obj; /* object header */
  90. struct desktop *desktop; /* desktop that this thread input belongs to */
  91. user_handle_t focus; /* focus window */
  92. user_handle_t capture; /* capture window */
  93. user_handle_t active; /* active window */
  94. user_handle_t menu_owner; /* current menu owner window */
  95. user_handle_t move_size; /* current moving/resizing window */
  96. user_handle_t caret; /* caret window */
  97. rectangle_t caret_rect; /* caret rectangle */
  98. int caret_hide; /* caret hide count */
  99. int caret_state; /* caret on/off state */
  100. user_handle_t cursor; /* current cursor */
  101. int cursor_count; /* cursor show count */
  102. struct list msg_list; /* list of hardware messages */
  103. unsigned char keystate[256]; /* state of each key */
  104. };
  105. struct msg_queue
  106. {
  107. struct object obj; /* object header */
  108. struct fd *fd; /* optional file descriptor to poll */
  109. unsigned int wake_bits; /* wakeup bits */
  110. unsigned int wake_mask; /* wakeup mask */
  111. unsigned int changed_bits; /* changed wakeup bits */
  112. unsigned int changed_mask; /* changed wakeup mask */
  113. int paint_count; /* pending paint messages count */
  114. int hotkey_count; /* pending hotkey messages count */
  115. int quit_message; /* is there a pending quit message? */
  116. int exit_code; /* exit code of pending quit message */
  117. int cursor_count; /* per-queue cursor show count */
  118. struct list msg_list[NB_MSG_KINDS]; /* lists of messages */
  119. struct list send_result; /* stack of sent messages waiting for result */
  120. struct list callback_result; /* list of callback messages waiting for result */
  121. struct message_result *recv_result; /* stack of received messages waiting for result */
  122. struct list pending_timers; /* list of pending timers */
  123. struct list expired_timers; /* list of expired timers */
  124. lparam_t next_timer_id; /* id for the next timer with a 0 window */
  125. struct timeout_user *timeout; /* timeout for next timer to expire */
  126. struct thread_input *input; /* thread input descriptor */
  127. struct hook_table *hooks; /* hook table */
  128. timeout_t last_get_msg; /* time of last get message call */
  129. };
  130. struct hotkey
  131. {
  132. struct list entry; /* entry in desktop hotkey list */
  133. struct msg_queue *queue; /* queue owning this hotkey */
  134. user_handle_t win; /* window handle */
  135. int id; /* hotkey id */
  136. unsigned int vkey; /* virtual key code */
  137. unsigned int flags; /* key modifiers */
  138. };
  139. static void msg_queue_dump( struct object *obj, int verbose );
  140. static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry );
  141. static void msg_queue_remove_queue( struct object *obj, struct wait_queue_entry *entry );
  142. static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry );
  143. static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry );
  144. static void msg_queue_destroy( struct object *obj );
  145. static void msg_queue_poll_event( struct fd *fd, int event );
  146. static void thread_input_dump( struct object *obj, int verbose );
  147. static void thread_input_destroy( struct object *obj );
  148. static void timer_callback( void *private );
  149. static const struct object_ops msg_queue_ops =
  150. {
  151. sizeof(struct msg_queue), /* size */
  152. &no_type, /* type */
  153. msg_queue_dump, /* dump */
  154. msg_queue_add_queue, /* add_queue */
  155. msg_queue_remove_queue, /* remove_queue */
  156. msg_queue_signaled, /* signaled */
  157. msg_queue_satisfied, /* satisfied */
  158. no_signal, /* signal */
  159. no_get_fd, /* get_fd */
  160. default_map_access, /* map_access */
  161. default_get_sd, /* get_sd */
  162. default_set_sd, /* set_sd */
  163. no_get_full_name, /* get_full_name */
  164. no_lookup_name, /* lookup_name */
  165. no_link_name, /* link_name */
  166. NULL, /* unlink_name */
  167. no_open_file, /* open_file */
  168. no_kernel_obj_list, /* get_kernel_obj_list */
  169. no_close_handle, /* close_handle */
  170. msg_queue_destroy /* destroy */
  171. };
  172. static const struct fd_ops msg_queue_fd_ops =
  173. {
  174. NULL, /* get_poll_events */
  175. msg_queue_poll_event, /* poll_event */
  176. NULL, /* flush */
  177. NULL, /* get_fd_type */
  178. NULL, /* ioctl */
  179. NULL, /* queue_async */
  180. NULL, /* reselect_async */
  181. NULL /* cancel async */
  182. };
  183. static const struct object_ops thread_input_ops =
  184. {
  185. sizeof(struct thread_input), /* size */
  186. &no_type, /* type */
  187. thread_input_dump, /* dump */
  188. no_add_queue, /* add_queue */
  189. NULL, /* remove_queue */
  190. NULL, /* signaled */
  191. NULL, /* satisfied */
  192. no_signal, /* signal */
  193. no_get_fd, /* get_fd */
  194. default_map_access, /* map_access */
  195. default_get_sd, /* get_sd */
  196. default_set_sd, /* set_sd */
  197. no_get_full_name, /* get_full_name */
  198. no_lookup_name, /* lookup_name */
  199. no_link_name, /* link_name */
  200. NULL, /* unlink_name */
  201. no_open_file, /* open_file */
  202. no_kernel_obj_list, /* get_kernel_obj_list */
  203. no_close_handle, /* close_handle */
  204. thread_input_destroy /* destroy */
  205. };
  206. /* pointer to input structure of foreground thread */
  207. static unsigned int last_input_time;
  208. static cursor_pos_t cursor_history[64];
  209. static unsigned int cursor_history_latest;
  210. static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue );
  211. static void free_message( struct message *msg );
  212. /* set the caret window in a given thread input */
  213. static void set_caret_window( struct thread_input *input, user_handle_t win )
  214. {
  215. if (!win || win != input->caret)
  216. {
  217. input->caret_rect.left = 0;
  218. input->caret_rect.top = 0;
  219. input->caret_rect.right = 0;
  220. input->caret_rect.bottom = 0;
  221. }
  222. input->caret = win;
  223. input->caret_hide = 1;
  224. input->caret_state = 0;
  225. }
  226. /* create a thread input object */
  227. static struct thread_input *create_thread_input( struct thread *thread )
  228. {
  229. struct thread_input *input;
  230. if ((input = alloc_object( &thread_input_ops )))
  231. {
  232. input->focus = 0;
  233. input->capture = 0;
  234. input->active = 0;
  235. input->menu_owner = 0;
  236. input->move_size = 0;
  237. input->cursor = 0;
  238. input->cursor_count = 0;
  239. list_init( &input->msg_list );
  240. set_caret_window( input, 0 );
  241. memset( input->keystate, 0, sizeof(input->keystate) );
  242. if (!(input->desktop = get_thread_desktop( thread, 0 /* FIXME: access rights */ )))
  243. {
  244. release_object( input );
  245. return NULL;
  246. }
  247. }
  248. return input;
  249. }
  250. /* create a message queue object */
  251. static struct msg_queue *create_msg_queue( struct thread *thread, struct thread_input *input )
  252. {
  253. struct thread_input *new_input = NULL;
  254. struct msg_queue *queue;
  255. int i;
  256. if (!input)
  257. {
  258. if (!(new_input = create_thread_input( thread ))) return NULL;
  259. input = new_input;
  260. }
  261. if ((queue = alloc_object( &msg_queue_ops )))
  262. {
  263. queue->fd = NULL;
  264. queue->wake_bits = 0;
  265. queue->wake_mask = 0;
  266. queue->changed_bits = 0;
  267. queue->changed_mask = 0;
  268. queue->paint_count = 0;
  269. queue->hotkey_count = 0;
  270. queue->quit_message = 0;
  271. queue->cursor_count = 0;
  272. queue->recv_result = NULL;
  273. queue->next_timer_id = 0x7fff;
  274. queue->timeout = NULL;
  275. queue->input = (struct thread_input *)grab_object( input );
  276. queue->hooks = NULL;
  277. queue->last_get_msg = current_time;
  278. list_init( &queue->send_result );
  279. list_init( &queue->callback_result );
  280. list_init( &queue->pending_timers );
  281. list_init( &queue->expired_timers );
  282. for (i = 0; i < NB_MSG_KINDS; i++) list_init( &queue->msg_list[i] );
  283. thread->queue = queue;
  284. }
  285. if (new_input) release_object( new_input );
  286. return queue;
  287. }
  288. /* free the message queue of a thread at thread exit */
  289. void free_msg_queue( struct thread *thread )
  290. {
  291. remove_thread_hooks( thread );
  292. if (!thread->queue) return;
  293. release_object( thread->queue );
  294. thread->queue = NULL;
  295. }
  296. /* change the thread input data of a given thread */
  297. static int assign_thread_input( struct thread *thread, struct thread_input *new_input )
  298. {
  299. struct msg_queue *queue = thread->queue;
  300. if (!queue)
  301. {
  302. thread->queue = create_msg_queue( thread, new_input );
  303. return thread->queue != NULL;
  304. }
  305. if (queue->input)
  306. {
  307. queue->input->cursor_count -= queue->cursor_count;
  308. release_object( queue->input );
  309. }
  310. queue->input = (struct thread_input *)grab_object( new_input );
  311. new_input->cursor_count += queue->cursor_count;
  312. return 1;
  313. }
  314. /* allocate a hardware message and its data */
  315. static struct message *alloc_hardware_message( lparam_t info, struct hw_msg_source source,
  316. unsigned int time, data_size_t extra_size )
  317. {
  318. struct hardware_msg_data *msg_data;
  319. struct message *msg;
  320. if (!(msg = mem_alloc( sizeof(*msg) ))) return NULL;
  321. if (!(msg_data = mem_alloc( sizeof(*msg_data) + extra_size )))
  322. {
  323. free( msg );
  324. return NULL;
  325. }
  326. memset( msg, 0, sizeof(*msg) );
  327. msg->type = MSG_HARDWARE;
  328. msg->time = time;
  329. msg->data = msg_data;
  330. msg->data_size = sizeof(*msg_data) + extra_size;
  331. memset( msg_data, 0, sizeof(*msg_data) + extra_size );
  332. msg_data->info = info;
  333. msg_data->size = msg->data_size;
  334. msg_data->source = source;
  335. return msg;
  336. }
  337. static int update_desktop_cursor_pos( struct desktop *desktop, int x, int y )
  338. {
  339. int updated;
  340. x = max( min( x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
  341. y = max( min( y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
  342. updated = (desktop->cursor.x != x || desktop->cursor.y != y);
  343. desktop->cursor.x = x;
  344. desktop->cursor.y = y;
  345. desktop->cursor.last_change = get_tick_count();
  346. return updated;
  347. }
  348. /* set the cursor position and queue the corresponding mouse message */
  349. static void set_cursor_pos( struct desktop *desktop, int x, int y )
  350. {
  351. static const struct hw_msg_source source = { IMDT_UNAVAILABLE, IMO_SYSTEM };
  352. const struct rawinput_device *device;
  353. struct message *msg;
  354. if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
  355. {
  356. update_desktop_cursor_pos( desktop, x, y );
  357. return;
  358. }
  359. if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
  360. msg->msg = WM_MOUSEMOVE;
  361. msg->x = x;
  362. msg->y = y;
  363. queue_hardware_message( desktop, msg, 1 );
  364. }
  365. /* retrieve default position and time for synthesized messages */
  366. static void get_message_defaults( struct msg_queue *queue, int *x, int *y, unsigned int *time )
  367. {
  368. struct desktop *desktop = queue->input->desktop;
  369. *x = desktop->cursor.x;
  370. *y = desktop->cursor.y;
  371. *time = get_tick_count();
  372. }
  373. /* set the cursor clip rectangle */
  374. static void set_clip_rectangle( struct desktop *desktop, const rectangle_t *rect, int send_clip_msg )
  375. {
  376. rectangle_t top_rect;
  377. int x, y;
  378. get_top_window_rectangle( desktop, &top_rect );
  379. if (rect)
  380. {
  381. rectangle_t new_rect = *rect;
  382. if (new_rect.left < top_rect.left) new_rect.left = top_rect.left;
  383. if (new_rect.right > top_rect.right) new_rect.right = top_rect.right;
  384. if (new_rect.top < top_rect.top) new_rect.top = top_rect.top;
  385. if (new_rect.bottom > top_rect.bottom) new_rect.bottom = top_rect.bottom;
  386. if (new_rect.left > new_rect.right || new_rect.top > new_rect.bottom) new_rect = top_rect;
  387. desktop->cursor.clip = new_rect;
  388. }
  389. else desktop->cursor.clip = top_rect;
  390. if (desktop->cursor.clip_msg && send_clip_msg)
  391. post_desktop_message( desktop, desktop->cursor.clip_msg, rect != NULL, 0 );
  392. /* warp the mouse to be inside the clip rect */
  393. x = max( min( desktop->cursor.x, desktop->cursor.clip.right - 1 ), desktop->cursor.clip.left );
  394. y = max( min( desktop->cursor.y, desktop->cursor.clip.bottom - 1 ), desktop->cursor.clip.top );
  395. if (x != desktop->cursor.x || y != desktop->cursor.y) set_cursor_pos( desktop, x, y );
  396. }
  397. /* change the foreground input and reset the cursor clip rect */
  398. static void set_foreground_input( struct desktop *desktop, struct thread_input *input )
  399. {
  400. if (desktop->foreground_input == input) return;
  401. set_clip_rectangle( desktop, NULL, 1 );
  402. desktop->foreground_input = input;
  403. }
  404. /* get the hook table for a given thread */
  405. struct hook_table *get_queue_hooks( struct thread *thread )
  406. {
  407. if (!thread->queue) return NULL;
  408. return thread->queue->hooks;
  409. }
  410. /* set the hook table for a given thread, allocating the queue if needed */
  411. void set_queue_hooks( struct thread *thread, struct hook_table *hooks )
  412. {
  413. struct msg_queue *queue = thread->queue;
  414. if (!queue && !(queue = create_msg_queue( thread, NULL ))) return;
  415. if (queue->hooks) release_object( queue->hooks );
  416. queue->hooks = hooks;
  417. }
  418. /* check the queue status */
  419. static inline int is_signaled( struct msg_queue *queue )
  420. {
  421. return ((queue->wake_bits & queue->wake_mask) || (queue->changed_bits & queue->changed_mask));
  422. }
  423. /* set some queue bits */
  424. static inline void set_queue_bits( struct msg_queue *queue, unsigned int bits )
  425. {
  426. queue->wake_bits |= bits;
  427. queue->changed_bits |= bits;
  428. if (is_signaled( queue )) wake_up( &queue->obj, 0 );
  429. }
  430. /* clear some queue bits */
  431. static inline void clear_queue_bits( struct msg_queue *queue, unsigned int bits )
  432. {
  433. queue->wake_bits &= ~bits;
  434. queue->changed_bits &= ~bits;
  435. }
  436. /* check whether msg is a keyboard message */
  437. static inline int is_keyboard_msg( struct message *msg )
  438. {
  439. return (msg->msg >= WM_KEYFIRST && msg->msg <= WM_KEYLAST);
  440. }
  441. /* check if message is matched by the filter */
  442. static inline int check_msg_filter( unsigned int msg, unsigned int first, unsigned int last )
  443. {
  444. return (msg >= first && msg <= last);
  445. }
  446. /* check whether a message filter contains at least one potential hardware message */
  447. static inline int filter_contains_hw_range( unsigned int first, unsigned int last )
  448. {
  449. /* hardware message ranges are (in numerical order):
  450. * WM_NCMOUSEFIRST .. WM_NCMOUSELAST
  451. * WM_INPUT_DEVICE_CHANGE .. WM_KEYLAST
  452. * WM_MOUSEFIRST .. WM_MOUSELAST
  453. */
  454. if (last < WM_NCMOUSEFIRST) return 0;
  455. if (first > WM_NCMOUSELAST && last < WM_INPUT_DEVICE_CHANGE) return 0;
  456. if (first > WM_KEYLAST && last < WM_MOUSEFIRST) return 0;
  457. if (first > WM_MOUSELAST) return 0;
  458. return 1;
  459. }
  460. /* get the QS_* bit corresponding to a given hardware message */
  461. static inline int get_hardware_msg_bit( struct message *msg )
  462. {
  463. if (msg->msg == WM_INPUT_DEVICE_CHANGE || msg->msg == WM_INPUT) return QS_RAWINPUT;
  464. if (msg->msg == WM_MOUSEMOVE || msg->msg == WM_NCMOUSEMOVE) return QS_MOUSEMOVE;
  465. if (is_keyboard_msg( msg )) return QS_KEY;
  466. return QS_MOUSEBUTTON;
  467. }
  468. /* get the current thread queue, creating it if needed */
  469. static inline struct msg_queue *get_current_queue(void)
  470. {
  471. struct msg_queue *queue = current->queue;
  472. if (!queue) queue = create_msg_queue( current, NULL );
  473. return queue;
  474. }
  475. /* get a (pseudo-)unique id to tag hardware messages */
  476. static inline unsigned int get_unique_id(void)
  477. {
  478. static unsigned int id;
  479. if (!++id) id = 1; /* avoid an id of 0 */
  480. return id;
  481. }
  482. /* try to merge a message with the last in the list; return 1 if successful */
  483. static int merge_message( struct thread_input *input, const struct message *msg )
  484. {
  485. struct message *prev;
  486. struct list *ptr;
  487. if (msg->msg != WM_MOUSEMOVE) return 0;
  488. for (ptr = list_tail( &input->msg_list ); ptr; ptr = list_prev( &input->msg_list, ptr ))
  489. {
  490. prev = LIST_ENTRY( ptr, struct message, entry );
  491. if (prev->msg != WM_INPUT) break;
  492. }
  493. if (!ptr) return 0;
  494. if (prev->result) return 0;
  495. if (prev->win && msg->win && prev->win != msg->win) return 0;
  496. if (prev->msg != msg->msg) return 0;
  497. if (prev->type != msg->type) return 0;
  498. /* now we can merge it */
  499. prev->wparam = msg->wparam;
  500. prev->lparam = msg->lparam;
  501. prev->x = msg->x;
  502. prev->y = msg->y;
  503. prev->time = msg->time;
  504. if (msg->type == MSG_HARDWARE && prev->data && msg->data)
  505. {
  506. struct hardware_msg_data *prev_data = prev->data;
  507. struct hardware_msg_data *msg_data = msg->data;
  508. prev_data->info = msg_data->info;
  509. }
  510. list_remove( ptr );
  511. list_add_tail( &input->msg_list, ptr );
  512. return 1;
  513. }
  514. /* free a result structure */
  515. static void free_result( struct message_result *result )
  516. {
  517. if (result->timeout) remove_timeout_user( result->timeout );
  518. free( result->data );
  519. if (result->callback_msg) free_message( result->callback_msg );
  520. if (result->hardware_msg) free_message( result->hardware_msg );
  521. if (result->desktop) release_object( result->desktop );
  522. free( result );
  523. }
  524. /* remove the result from the sender list it is on */
  525. static inline void remove_result_from_sender( struct message_result *result )
  526. {
  527. assert( result->sender );
  528. list_remove( &result->sender_entry );
  529. result->sender = NULL;
  530. if (!result->receiver) free_result( result );
  531. }
  532. /* store the message result in the appropriate structure */
  533. static void store_message_result( struct message_result *res, lparam_t result, unsigned int error )
  534. {
  535. res->result = result;
  536. res->error = error;
  537. res->replied = 1;
  538. if (res->timeout)
  539. {
  540. remove_timeout_user( res->timeout );
  541. res->timeout = NULL;
  542. }
  543. if (res->hardware_msg)
  544. {
  545. if (!error && result) /* rejected by the hook */
  546. free_message( res->hardware_msg );
  547. else
  548. queue_hardware_message( res->desktop, res->hardware_msg, 0 );
  549. res->hardware_msg = NULL;
  550. }
  551. if (res->sender)
  552. {
  553. if (res->callback_msg)
  554. {
  555. /* queue the callback message in the sender queue */
  556. struct callback_msg_data *data = res->callback_msg->data;
  557. data->result = result;
  558. list_add_tail( &res->sender->msg_list[SEND_MESSAGE], &res->callback_msg->entry );
  559. set_queue_bits( res->sender, QS_SENDMESSAGE );
  560. res->callback_msg = NULL;
  561. remove_result_from_sender( res );
  562. }
  563. else
  564. {
  565. /* wake sender queue if waiting on this result */
  566. if (list_head(&res->sender->send_result) == &res->sender_entry)
  567. set_queue_bits( res->sender, QS_SMRESULT );
  568. }
  569. }
  570. else if (!res->receiver) free_result( res );
  571. }
  572. /* free a message when deleting a queue or window */
  573. static void free_message( struct message *msg )
  574. {
  575. struct message_result *result = msg->result;
  576. if (result)
  577. {
  578. result->msg = NULL;
  579. result->receiver = NULL;
  580. store_message_result( result, 0, STATUS_ACCESS_DENIED /*FIXME*/ );
  581. }
  582. free( msg->data );
  583. free( msg );
  584. }
  585. /* remove (and free) a message from a message list */
  586. static void remove_queue_message( struct msg_queue *queue, struct message *msg,
  587. enum message_kind kind )
  588. {
  589. list_remove( &msg->entry );
  590. switch(kind)
  591. {
  592. case SEND_MESSAGE:
  593. if (list_empty( &queue->msg_list[kind] )) clear_queue_bits( queue, QS_SENDMESSAGE );
  594. break;
  595. case POST_MESSAGE:
  596. if (list_empty( &queue->msg_list[kind] ) && !queue->quit_message)
  597. clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
  598. if (msg->msg == WM_HOTKEY && --queue->hotkey_count == 0)
  599. clear_queue_bits( queue, QS_HOTKEY );
  600. break;
  601. }
  602. free_message( msg );
  603. }
  604. /* message timed out without getting a reply */
  605. static void result_timeout( void *private )
  606. {
  607. struct message_result *result = private;
  608. assert( !result->replied );
  609. result->timeout = NULL;
  610. if (result->msg) /* not received yet */
  611. {
  612. struct message *msg = result->msg;
  613. result->msg = NULL;
  614. msg->result = NULL;
  615. remove_queue_message( result->receiver, msg, SEND_MESSAGE );
  616. result->receiver = NULL;
  617. }
  618. store_message_result( result, 0, STATUS_TIMEOUT );
  619. }
  620. /* allocate and fill a message result structure */
  621. static struct message_result *alloc_message_result( struct msg_queue *send_queue,
  622. struct msg_queue *recv_queue,
  623. struct message *msg, timeout_t timeout )
  624. {
  625. struct message_result *result = mem_alloc( sizeof(*result) );
  626. if (result)
  627. {
  628. result->msg = msg;
  629. result->sender = send_queue;
  630. result->receiver = recv_queue;
  631. result->replied = 0;
  632. result->data = NULL;
  633. result->data_size = 0;
  634. result->timeout = NULL;
  635. result->hardware_msg = NULL;
  636. result->desktop = NULL;
  637. result->callback_msg = NULL;
  638. if (msg->type == MSG_CALLBACK)
  639. {
  640. struct message *callback_msg = mem_alloc( sizeof(*callback_msg) );
  641. if (!callback_msg)
  642. {
  643. free( result );
  644. return NULL;
  645. }
  646. callback_msg->type = MSG_CALLBACK_RESULT;
  647. callback_msg->win = msg->win;
  648. callback_msg->msg = msg->msg;
  649. callback_msg->wparam = 0;
  650. callback_msg->lparam = 0;
  651. callback_msg->time = get_tick_count();
  652. callback_msg->result = NULL;
  653. /* steal the data from the original message */
  654. callback_msg->data = msg->data;
  655. callback_msg->data_size = msg->data_size;
  656. msg->data = NULL;
  657. msg->data_size = 0;
  658. result->callback_msg = callback_msg;
  659. list_add_head( &send_queue->callback_result, &result->sender_entry );
  660. }
  661. else if (send_queue)
  662. {
  663. list_add_head( &send_queue->send_result, &result->sender_entry );
  664. clear_queue_bits( send_queue, QS_SMRESULT );
  665. }
  666. if (timeout != TIMEOUT_INFINITE)
  667. result->timeout = add_timeout_user( timeout, result_timeout, result );
  668. }
  669. return result;
  670. }
  671. /* receive a message, removing it from the sent queue */
  672. static void receive_message( struct msg_queue *queue, struct message *msg,
  673. struct get_message_reply *reply )
  674. {
  675. struct message_result *result = msg->result;
  676. reply->total = msg->data_size;
  677. if (msg->data_size > get_reply_max_size())
  678. {
  679. set_error( STATUS_BUFFER_OVERFLOW );
  680. return;
  681. }
  682. reply->type = msg->type;
  683. reply->win = msg->win;
  684. reply->msg = msg->msg;
  685. reply->wparam = msg->wparam;
  686. reply->lparam = msg->lparam;
  687. reply->x = msg->x;
  688. reply->y = msg->y;
  689. reply->time = msg->time;
  690. if (msg->data) set_reply_data_ptr( msg->data, msg->data_size );
  691. list_remove( &msg->entry );
  692. /* put the result on the receiver result stack */
  693. if (result)
  694. {
  695. result->msg = NULL;
  696. result->recv_next = queue->recv_result;
  697. queue->recv_result = result;
  698. }
  699. free( msg );
  700. if (list_empty( &queue->msg_list[SEND_MESSAGE] )) clear_queue_bits( queue, QS_SENDMESSAGE );
  701. }
  702. /* set the result of the current received message */
  703. static void reply_message( struct msg_queue *queue, lparam_t result,
  704. unsigned int error, int remove, const void *data, data_size_t len )
  705. {
  706. struct message_result *res = queue->recv_result;
  707. if (remove)
  708. {
  709. queue->recv_result = res->recv_next;
  710. res->receiver = NULL;
  711. if (!res->sender && !res->hardware_msg) /* no one waiting for it */
  712. {
  713. free_result( res );
  714. return;
  715. }
  716. }
  717. if (!res->replied)
  718. {
  719. if (len && (res->data = memdup( data, len ))) res->data_size = len;
  720. store_message_result( res, result, error );
  721. }
  722. }
  723. static int match_window( user_handle_t win, user_handle_t msg_win )
  724. {
  725. if (!win) return 1;
  726. if (win == -1 || win == 1) return !msg_win;
  727. if (msg_win == win) return 1;
  728. return is_child_window( win, msg_win );
  729. }
  730. /* retrieve a posted message */
  731. static int get_posted_message( struct msg_queue *queue, user_handle_t win,
  732. unsigned int first, unsigned int last, unsigned int flags,
  733. struct get_message_reply *reply )
  734. {
  735. struct message *msg;
  736. /* check against the filters */
  737. LIST_FOR_EACH_ENTRY( msg, &queue->msg_list[POST_MESSAGE], struct message, entry )
  738. {
  739. if (!match_window( win, msg->win )) continue;
  740. if (!check_msg_filter( msg->msg, first, last )) continue;
  741. goto found; /* found one */
  742. }
  743. return 0;
  744. /* return it to the app */
  745. found:
  746. reply->total = msg->data_size;
  747. if (msg->data_size > get_reply_max_size())
  748. {
  749. set_error( STATUS_BUFFER_OVERFLOW );
  750. return 1;
  751. }
  752. reply->type = msg->type;
  753. reply->win = msg->win;
  754. reply->msg = msg->msg;
  755. reply->wparam = msg->wparam;
  756. reply->lparam = msg->lparam;
  757. reply->x = msg->x;
  758. reply->y = msg->y;
  759. reply->time = msg->time;
  760. if (flags & PM_REMOVE)
  761. {
  762. if (msg->data)
  763. {
  764. set_reply_data_ptr( msg->data, msg->data_size );
  765. msg->data = NULL;
  766. msg->data_size = 0;
  767. }
  768. remove_queue_message( queue, msg, POST_MESSAGE );
  769. }
  770. else if (msg->data) set_reply_data( msg->data, msg->data_size );
  771. return 1;
  772. }
  773. static int get_quit_message( struct msg_queue *queue, unsigned int flags,
  774. struct get_message_reply *reply )
  775. {
  776. if (queue->quit_message)
  777. {
  778. reply->total = 0;
  779. reply->type = MSG_POSTED;
  780. reply->win = 0;
  781. reply->msg = WM_QUIT;
  782. reply->wparam = queue->exit_code;
  783. reply->lparam = 0;
  784. get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
  785. if (flags & PM_REMOVE)
  786. {
  787. queue->quit_message = 0;
  788. if (list_empty( &queue->msg_list[POST_MESSAGE] ))
  789. clear_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
  790. }
  791. return 1;
  792. }
  793. else
  794. return 0;
  795. }
  796. /* empty a message list and free all the messages */
  797. static void empty_msg_list( struct list *list )
  798. {
  799. struct list *ptr;
  800. while ((ptr = list_head( list )) != NULL)
  801. {
  802. struct message *msg = LIST_ENTRY( ptr, struct message, entry );
  803. list_remove( &msg->entry );
  804. free_message( msg );
  805. }
  806. }
  807. /* cleanup all pending results when deleting a queue */
  808. static void cleanup_results( struct msg_queue *queue )
  809. {
  810. struct list *entry;
  811. while ((entry = list_head( &queue->send_result )) != NULL)
  812. {
  813. remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
  814. }
  815. while ((entry = list_head( &queue->callback_result )) != NULL)
  816. {
  817. remove_result_from_sender( LIST_ENTRY( entry, struct message_result, sender_entry ) );
  818. }
  819. while (queue->recv_result)
  820. reply_message( queue, 0, STATUS_ACCESS_DENIED /*FIXME*/, 1, NULL, 0 );
  821. }
  822. /* check if the thread owning the queue is hung (not checking for messages) */
  823. static int is_queue_hung( struct msg_queue *queue )
  824. {
  825. struct wait_queue_entry *entry;
  826. if (current_time - queue->last_get_msg <= 5 * TICKS_PER_SEC)
  827. return 0; /* less than 5 seconds since last get message -> not hung */
  828. LIST_FOR_EACH_ENTRY( entry, &queue->obj.wait_queue, struct wait_queue_entry, entry )
  829. {
  830. if (get_wait_queue_thread(entry)->queue == queue)
  831. return 0; /* thread is waiting on queue -> not hung */
  832. }
  833. return 1;
  834. }
  835. static int msg_queue_add_queue( struct object *obj, struct wait_queue_entry *entry )
  836. {
  837. struct msg_queue *queue = (struct msg_queue *)obj;
  838. struct process *process = get_wait_queue_thread(entry)->process;
  839. /* a thread can only wait on its own queue */
  840. if (get_wait_queue_thread(entry)->queue != queue)
  841. {
  842. set_error( STATUS_ACCESS_DENIED );
  843. return 0;
  844. }
  845. if (process->idle_event && !(queue->wake_mask & QS_SMRESULT)) set_event( process->idle_event );
  846. if (queue->fd && list_empty( &obj->wait_queue )) /* first on the queue */
  847. set_fd_events( queue->fd, POLLIN );
  848. add_queue( obj, entry );
  849. return 1;
  850. }
  851. static void msg_queue_remove_queue(struct object *obj, struct wait_queue_entry *entry )
  852. {
  853. struct msg_queue *queue = (struct msg_queue *)obj;
  854. remove_queue( obj, entry );
  855. if (queue->fd && list_empty( &obj->wait_queue )) /* last on the queue is gone */
  856. set_fd_events( queue->fd, 0 );
  857. }
  858. static void msg_queue_dump( struct object *obj, int verbose )
  859. {
  860. struct msg_queue *queue = (struct msg_queue *)obj;
  861. fprintf( stderr, "Msg queue bits=%x mask=%x\n",
  862. queue->wake_bits, queue->wake_mask );
  863. }
  864. static int msg_queue_signaled( struct object *obj, struct wait_queue_entry *entry )
  865. {
  866. struct msg_queue *queue = (struct msg_queue *)obj;
  867. int ret = 0;
  868. if (queue->fd)
  869. {
  870. if ((ret = check_fd_events( queue->fd, POLLIN )))
  871. /* stop waiting on select() if we are signaled */
  872. set_fd_events( queue->fd, 0 );
  873. else if (!list_empty( &obj->wait_queue ))
  874. /* restart waiting on poll() if we are no longer signaled */
  875. set_fd_events( queue->fd, POLLIN );
  876. }
  877. return ret || is_signaled( queue );
  878. }
  879. static void msg_queue_satisfied( struct object *obj, struct wait_queue_entry *entry )
  880. {
  881. struct msg_queue *queue = (struct msg_queue *)obj;
  882. queue->wake_mask = 0;
  883. queue->changed_mask = 0;
  884. }
  885. static void msg_queue_destroy( struct object *obj )
  886. {
  887. struct msg_queue *queue = (struct msg_queue *)obj;
  888. struct list *ptr;
  889. struct hotkey *hotkey, *hotkey2;
  890. int i;
  891. cleanup_results( queue );
  892. for (i = 0; i < NB_MSG_KINDS; i++) empty_msg_list( &queue->msg_list[i] );
  893. LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &queue->input->desktop->hotkeys, struct hotkey, entry )
  894. {
  895. if (hotkey->queue == queue)
  896. {
  897. list_remove( &hotkey->entry );
  898. free( hotkey );
  899. }
  900. }
  901. while ((ptr = list_head( &queue->pending_timers )))
  902. {
  903. struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
  904. list_remove( &timer->entry );
  905. free( timer );
  906. }
  907. while ((ptr = list_head( &queue->expired_timers )))
  908. {
  909. struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
  910. list_remove( &timer->entry );
  911. free( timer );
  912. }
  913. if (queue->timeout) remove_timeout_user( queue->timeout );
  914. queue->input->cursor_count -= queue->cursor_count;
  915. release_object( queue->input );
  916. if (queue->hooks) release_object( queue->hooks );
  917. if (queue->fd) release_object( queue->fd );
  918. }
  919. static void msg_queue_poll_event( struct fd *fd, int event )
  920. {
  921. struct msg_queue *queue = get_fd_user( fd );
  922. assert( queue->obj.ops == &msg_queue_ops );
  923. if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
  924. else set_fd_events( queue->fd, 0 );
  925. wake_up( &queue->obj, 0 );
  926. }
  927. static void thread_input_dump( struct object *obj, int verbose )
  928. {
  929. struct thread_input *input = (struct thread_input *)obj;
  930. fprintf( stderr, "Thread input focus=%08x capture=%08x active=%08x\n",
  931. input->focus, input->capture, input->active );
  932. }
  933. static void thread_input_destroy( struct object *obj )
  934. {
  935. struct thread_input *input = (struct thread_input *)obj;
  936. empty_msg_list( &input->msg_list );
  937. if (input->desktop)
  938. {
  939. if (input->desktop->foreground_input == input) set_foreground_input( input->desktop, NULL );
  940. release_object( input->desktop );
  941. }
  942. }
  943. /* fix the thread input data when a window is destroyed */
  944. static inline void thread_input_cleanup_window( struct msg_queue *queue, user_handle_t window )
  945. {
  946. struct thread_input *input = queue->input;
  947. if (window == input->focus) input->focus = 0;
  948. if (window == input->capture) input->capture = 0;
  949. if (window == input->active) input->active = 0;
  950. if (window == input->menu_owner) input->menu_owner = 0;
  951. if (window == input->move_size) input->move_size = 0;
  952. if (window == input->caret) set_caret_window( input, 0 );
  953. }
  954. /* check if the specified window can be set in the input data of a given queue */
  955. static int check_queue_input_window( struct msg_queue *queue, user_handle_t window )
  956. {
  957. struct thread *thread;
  958. int ret = 0;
  959. if (!window) return 1; /* we can always clear the data */
  960. if ((thread = get_window_thread( window )))
  961. {
  962. ret = (queue->input == thread->queue->input);
  963. if (!ret) set_error( STATUS_ACCESS_DENIED );
  964. release_object( thread );
  965. }
  966. else set_error( STATUS_INVALID_HANDLE );
  967. return ret;
  968. }
  969. /* make sure the specified thread has a queue */
  970. int init_thread_queue( struct thread *thread )
  971. {
  972. if (thread->queue) return 1;
  973. return (create_msg_queue( thread, NULL ) != NULL);
  974. }
  975. /* attach two thread input data structures */
  976. int attach_thread_input( struct thread *thread_from, struct thread *thread_to )
  977. {
  978. struct desktop *desktop;
  979. struct thread_input *input;
  980. int ret;
  981. if (!thread_to->queue && !(thread_to->queue = create_msg_queue( thread_to, NULL ))) return 0;
  982. if (!(desktop = get_thread_desktop( thread_from, 0 ))) return 0;
  983. input = (struct thread_input *)grab_object( thread_to->queue->input );
  984. if (input->desktop != desktop)
  985. {
  986. set_error( STATUS_ACCESS_DENIED );
  987. release_object( input );
  988. release_object( desktop );
  989. return 0;
  990. }
  991. release_object( desktop );
  992. if (thread_from->queue)
  993. {
  994. if (!input->focus) input->focus = thread_from->queue->input->focus;
  995. if (!input->active) input->active = thread_from->queue->input->active;
  996. }
  997. ret = assign_thread_input( thread_from, input );
  998. if (ret) memset( input->keystate, 0, sizeof(input->keystate) );
  999. release_object( input );
  1000. return ret;
  1001. }
  1002. /* detach two thread input data structures */
  1003. void detach_thread_input( struct thread *thread_from )
  1004. {
  1005. struct thread *thread;
  1006. struct thread_input *input, *old_input = thread_from->queue->input;
  1007. if ((input = create_thread_input( thread_from )))
  1008. {
  1009. if (old_input->focus && (thread = get_window_thread( old_input->focus )))
  1010. {
  1011. if (thread == thread_from)
  1012. {
  1013. input->focus = old_input->focus;
  1014. old_input->focus = 0;
  1015. }
  1016. release_object( thread );
  1017. }
  1018. if (old_input->active && (thread = get_window_thread( old_input->active )))
  1019. {
  1020. if (thread == thread_from)
  1021. {
  1022. input->active = old_input->active;
  1023. old_input->active = 0;
  1024. }
  1025. release_object( thread );
  1026. }
  1027. assign_thread_input( thread_from, input );
  1028. release_object( input );
  1029. }
  1030. }
  1031. /* set the next timer to expire */
  1032. static void set_next_timer( struct msg_queue *queue )
  1033. {
  1034. struct list *ptr;
  1035. if (queue->timeout)
  1036. {
  1037. remove_timeout_user( queue->timeout );
  1038. queue->timeout = NULL;
  1039. }
  1040. if ((ptr = list_head( &queue->pending_timers )))
  1041. {
  1042. struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
  1043. queue->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, queue );
  1044. }
  1045. /* set/clear QS_TIMER bit */
  1046. if (list_empty( &queue->expired_timers ))
  1047. clear_queue_bits( queue, QS_TIMER );
  1048. else
  1049. set_queue_bits( queue, QS_TIMER );
  1050. }
  1051. /* find a timer from its window and id */
  1052. static struct timer *find_timer( struct msg_queue *queue, user_handle_t win,
  1053. unsigned int msg, lparam_t id )
  1054. {
  1055. struct list *ptr;
  1056. /* we need to search both lists */
  1057. LIST_FOR_EACH( ptr, &queue->pending_timers )
  1058. {
  1059. struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
  1060. if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
  1061. }
  1062. LIST_FOR_EACH( ptr, &queue->expired_timers )
  1063. {
  1064. struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
  1065. if (timer->win == win && timer->msg == msg && timer->id == id) return timer;
  1066. }
  1067. return NULL;
  1068. }
  1069. /* callback for the next timer expiration */
  1070. static void timer_callback( void *private )
  1071. {
  1072. struct msg_queue *queue = private;
  1073. struct list *ptr;
  1074. queue->timeout = NULL;
  1075. /* move on to the next timer */
  1076. ptr = list_head( &queue->pending_timers );
  1077. list_remove( ptr );
  1078. list_add_tail( &queue->expired_timers, ptr );
  1079. set_next_timer( queue );
  1080. }
  1081. /* link a timer at its rightful place in the queue list */
  1082. static void link_timer( struct msg_queue *queue, struct timer *timer )
  1083. {
  1084. struct list *ptr;
  1085. for (ptr = queue->pending_timers.next; ptr != &queue->pending_timers; ptr = ptr->next)
  1086. {
  1087. struct timer *t = LIST_ENTRY( ptr, struct timer, entry );
  1088. if (t->when <= timer->when) break;
  1089. }
  1090. list_add_before( ptr, &timer->entry );
  1091. }
  1092. /* remove a timer from the queue timer list and free it */
  1093. static void free_timer( struct msg_queue *queue, struct timer *timer )
  1094. {
  1095. list_remove( &timer->entry );
  1096. free( timer );
  1097. set_next_timer( queue );
  1098. }
  1099. /* restart an expired timer */
  1100. static void restart_timer( struct msg_queue *queue, struct timer *timer )
  1101. {
  1102. list_remove( &timer->entry );
  1103. while (-timer->when <= monotonic_time) timer->when -= (timeout_t)timer->rate * 10000;
  1104. link_timer( queue, timer );
  1105. set_next_timer( queue );
  1106. }
  1107. /* find an expired timer matching the filtering parameters */
  1108. static struct timer *find_expired_timer( struct msg_queue *queue, user_handle_t win,
  1109. unsigned int get_first, unsigned int get_last,
  1110. int remove )
  1111. {
  1112. struct list *ptr;
  1113. LIST_FOR_EACH( ptr, &queue->expired_timers )
  1114. {
  1115. struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
  1116. if (win && timer->win != win) continue;
  1117. if (check_msg_filter( timer->msg, get_first, get_last ))
  1118. {
  1119. if (remove) restart_timer( queue, timer );
  1120. return timer;
  1121. }
  1122. }
  1123. return NULL;
  1124. }
  1125. /* add a timer */
  1126. static struct timer *set_timer( struct msg_queue *queue, unsigned int rate )
  1127. {
  1128. struct timer *timer = mem_alloc( sizeof(*timer) );
  1129. if (timer)
  1130. {
  1131. timer->rate = max( rate, 1 );
  1132. timer->when = -monotonic_time - (timeout_t)timer->rate * 10000;
  1133. link_timer( queue, timer );
  1134. /* check if we replaced the next timer */
  1135. if (list_head( &queue->pending_timers ) == &timer->entry) set_next_timer( queue );
  1136. }
  1137. return timer;
  1138. }
  1139. /* change the input key state for a given key */
  1140. static void set_input_key_state( unsigned char *keystate, unsigned char key, int down )
  1141. {
  1142. if (down)
  1143. {
  1144. if (!(keystate[key] & 0x80)) keystate[key] ^= 0x01;
  1145. keystate[key] |= down;
  1146. }
  1147. else keystate[key] &= ~0x80;
  1148. }
  1149. /* update the input key state for a keyboard message */
  1150. static void update_input_key_state( struct desktop *desktop, unsigned char *keystate,
  1151. unsigned int msg, lparam_t wparam )
  1152. {
  1153. unsigned char key;
  1154. int down = 0;
  1155. switch (msg)
  1156. {
  1157. case WM_LBUTTONDOWN:
  1158. down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
  1159. /* fall through */
  1160. case WM_LBUTTONUP:
  1161. set_input_key_state( keystate, VK_LBUTTON, down );
  1162. break;
  1163. case WM_MBUTTONDOWN:
  1164. down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
  1165. /* fall through */
  1166. case WM_MBUTTONUP:
  1167. set_input_key_state( keystate, VK_MBUTTON, down );
  1168. break;
  1169. case WM_RBUTTONDOWN:
  1170. down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
  1171. /* fall through */
  1172. case WM_RBUTTONUP:
  1173. set_input_key_state( keystate, VK_RBUTTON, down );
  1174. break;
  1175. case WM_XBUTTONDOWN:
  1176. down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
  1177. /* fall through */
  1178. case WM_XBUTTONUP:
  1179. if (wparam >> 16 == XBUTTON1) set_input_key_state( keystate, VK_XBUTTON1, down );
  1180. else if (wparam >> 16 == XBUTTON2) set_input_key_state( keystate, VK_XBUTTON2, down );
  1181. break;
  1182. case WM_KEYDOWN:
  1183. case WM_SYSKEYDOWN:
  1184. down = (keystate == desktop->keystate) ? 0xc0 : 0x80;
  1185. /* fall through */
  1186. case WM_KEYUP:
  1187. case WM_SYSKEYUP:
  1188. key = (unsigned char)wparam;
  1189. set_input_key_state( keystate, key, down );
  1190. switch(key)
  1191. {
  1192. case VK_LCONTROL:
  1193. case VK_RCONTROL:
  1194. down = (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80;
  1195. set_input_key_state( keystate, VK_CONTROL, down );
  1196. break;
  1197. case VK_LMENU:
  1198. case VK_RMENU:
  1199. down = (keystate[VK_LMENU] | keystate[VK_RMENU]) & 0x80;
  1200. set_input_key_state( keystate, VK_MENU, down );
  1201. break;
  1202. case VK_LSHIFT:
  1203. case VK_RSHIFT:
  1204. down = (keystate[VK_LSHIFT] | keystate[VK_RSHIFT]) & 0x80;
  1205. set_input_key_state( keystate, VK_SHIFT, down );
  1206. break;
  1207. }
  1208. break;
  1209. }
  1210. }
  1211. /* update the desktop key state according to a mouse message flags */
  1212. static void update_desktop_mouse_state( struct desktop *desktop, unsigned int flags,
  1213. int x, int y, lparam_t wparam )
  1214. {
  1215. if (flags & MOUSEEVENTF_MOVE)
  1216. update_desktop_cursor_pos( desktop, x, y );
  1217. if (flags & MOUSEEVENTF_LEFTDOWN)
  1218. update_input_key_state( desktop, desktop->keystate, WM_LBUTTONDOWN, wparam );
  1219. if (flags & MOUSEEVENTF_LEFTUP)
  1220. update_input_key_state( desktop, desktop->keystate, WM_LBUTTONUP, wparam );
  1221. if (flags & MOUSEEVENTF_RIGHTDOWN)
  1222. update_input_key_state( desktop, desktop->keystate, WM_RBUTTONDOWN, wparam );
  1223. if (flags & MOUSEEVENTF_RIGHTUP)
  1224. update_input_key_state( desktop, desktop->keystate, WM_RBUTTONUP, wparam );
  1225. if (flags & MOUSEEVENTF_MIDDLEDOWN)
  1226. update_input_key_state( desktop, desktop->keystate, WM_MBUTTONDOWN, wparam );
  1227. if (flags & MOUSEEVENTF_MIDDLEUP)
  1228. update_input_key_state( desktop, desktop->keystate, WM_MBUTTONUP, wparam );
  1229. if (flags & MOUSEEVENTF_XDOWN)
  1230. update_input_key_state( desktop, desktop->keystate, WM_XBUTTONDOWN, wparam );
  1231. if (flags & MOUSEEVENTF_XUP)
  1232. update_input_key_state( desktop, desktop->keystate, WM_XBUTTONUP, wparam );
  1233. }
  1234. /* release the hardware message currently being processed by the given thread */
  1235. static void release_hardware_message( struct msg_queue *queue, unsigned int hw_id )
  1236. {
  1237. struct thread_input *input = queue->input;
  1238. struct message *msg, *other;
  1239. int clr_bit;
  1240. LIST_FOR_EACH_ENTRY( msg, &input->msg_list, struct message, entry )
  1241. {
  1242. if (msg->unique_id == hw_id) break;
  1243. }
  1244. if (&msg->entry == &input->msg_list) return; /* not found */
  1245. /* clear the queue bit for that message */
  1246. clr_bit = get_hardware_msg_bit( msg );
  1247. LIST_FOR_EACH_ENTRY( other, &input->msg_list, struct message, entry )
  1248. {
  1249. if (other != msg && get_hardware_msg_bit( other ) == clr_bit)
  1250. {
  1251. clr_bit = 0;
  1252. break;
  1253. }
  1254. }
  1255. if (clr_bit) clear_queue_bits( queue, clr_bit );
  1256. update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
  1257. list_remove( &msg->entry );
  1258. free_message( msg );
  1259. }
  1260. static int queue_hotkey_message( struct desktop *desktop, struct message *msg )
  1261. {
  1262. struct hotkey *hotkey;
  1263. unsigned int modifiers = 0;
  1264. if (msg->msg != WM_KEYDOWN && msg->msg != WM_SYSKEYDOWN) return 0;
  1265. if (desktop->keystate[VK_MENU] & 0x80) modifiers |= MOD_ALT;
  1266. if (desktop->keystate[VK_CONTROL] & 0x80) modifiers |= MOD_CONTROL;
  1267. if (desktop->keystate[VK_SHIFT] & 0x80) modifiers |= MOD_SHIFT;
  1268. if ((desktop->keystate[VK_LWIN] & 0x80) || (desktop->keystate[VK_RWIN] & 0x80)) modifiers |= MOD_WIN;
  1269. LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
  1270. {
  1271. if (hotkey->vkey != msg->wparam) continue;
  1272. if ((hotkey->flags & (MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN)) == modifiers) goto found;
  1273. }
  1274. return 0;
  1275. found:
  1276. msg->type = MSG_POSTED;
  1277. msg->win = hotkey->win;
  1278. msg->msg = WM_HOTKEY;
  1279. msg->wparam = hotkey->id;
  1280. msg->lparam = ((hotkey->vkey & 0xffff) << 16) | modifiers;
  1281. free( msg->data );
  1282. msg->data = NULL;
  1283. msg->data_size = 0;
  1284. list_add_tail( &hotkey->queue->msg_list[POST_MESSAGE], &msg->entry );
  1285. set_queue_bits( hotkey->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE|QS_HOTKEY );
  1286. hotkey->queue->hotkey_count++;
  1287. return 1;
  1288. }
  1289. /* find the window that should receive a given hardware message */
  1290. static user_handle_t find_hardware_message_window( struct desktop *desktop, struct thread_input *input,
  1291. struct message *msg, unsigned int *msg_code,
  1292. struct thread **thread )
  1293. {
  1294. user_handle_t win = 0;
  1295. *thread = NULL;
  1296. *msg_code = msg->msg;
  1297. if (msg->msg == WM_INPUT || msg->msg == WM_INPUT_DEVICE_CHANGE)
  1298. {
  1299. if (!(win = msg->win) && input) win = input->focus;
  1300. }
  1301. else if (is_keyboard_msg( msg ))
  1302. {
  1303. if (input && !(win = input->focus))
  1304. {
  1305. win = input->active;
  1306. if (*msg_code < WM_SYSKEYDOWN) *msg_code += WM_SYSKEYDOWN - WM_KEYDOWN;
  1307. }
  1308. }
  1309. else if (!input || !(win = input->capture)) /* mouse message */
  1310. {
  1311. if (is_window_visible( msg->win ) && !is_window_transparent( msg->win )) win = msg->win;
  1312. else win = shallow_window_from_point( desktop, msg->x, msg->y );
  1313. *thread = window_thread_from_point( win, msg->x, msg->y );
  1314. }
  1315. if (!*thread)
  1316. *thread = get_window_thread( win );
  1317. return win;
  1318. }
  1319. static struct rawinput_device_entry *find_rawinput_device( struct process *process, unsigned short usage_page, unsigned short usage )
  1320. {
  1321. struct rawinput_device_entry *e;
  1322. LIST_FOR_EACH_ENTRY( e, &process->rawinput_devices, struct rawinput_device_entry, entry )
  1323. {
  1324. if (e->device.usage_page != usage_page || e->device.usage != usage) continue;
  1325. return e;
  1326. }
  1327. return NULL;
  1328. }
  1329. static void update_rawinput_device(const struct rawinput_device *device)
  1330. {
  1331. struct rawinput_device_entry *e;
  1332. if (!(e = find_rawinput_device( current->process, device->usage_page, device->usage )))
  1333. {
  1334. if (!(e = mem_alloc( sizeof(*e) ))) return;
  1335. list_add_tail( &current->process->rawinput_devices, &e->entry );
  1336. }
  1337. if (device->flags & RIDEV_REMOVE)
  1338. {
  1339. list_remove( &e->entry );
  1340. free( e );
  1341. return;
  1342. }
  1343. e->device = *device;
  1344. e->device.target = get_user_full_handle( e->device.target );
  1345. }
  1346. static void prepend_cursor_history( int x, int y, unsigned int time, lparam_t info )
  1347. {
  1348. cursor_pos_t *pos = &cursor_history[--cursor_history_latest % ARRAY_SIZE(cursor_history)];
  1349. pos->x = x;
  1350. pos->y = y;
  1351. pos->time = time;
  1352. pos->info = info;
  1353. }
  1354. /* queue a hardware message into a given thread input */
  1355. static void queue_hardware_message( struct desktop *desktop, struct message *msg, int always_queue )
  1356. {
  1357. user_handle_t win;
  1358. struct thread *thread;
  1359. struct thread_input *input;
  1360. struct hardware_msg_data *msg_data = msg->data;
  1361. unsigned int msg_code;
  1362. update_input_key_state( desktop, desktop->keystate, msg->msg, msg->wparam );
  1363. last_input_time = get_tick_count();
  1364. if (msg->msg != WM_MOUSEMOVE) always_queue = 1;
  1365. if (is_keyboard_msg( msg ))
  1366. {
  1367. if (queue_hotkey_message( desktop, msg )) return;
  1368. if (desktop->keystate[VK_MENU] & 0x80) msg->lparam |= KF_ALTDOWN << 16;
  1369. if (msg->wparam == VK_SHIFT || msg->wparam == VK_LSHIFT || msg->wparam == VK_RSHIFT)
  1370. msg->lparam &= ~(KF_EXTENDED << 16);
  1371. }
  1372. else if (msg->msg != WM_INPUT && msg->msg != WM_INPUT_DEVICE_CHANGE)
  1373. {
  1374. if (msg->msg == WM_MOUSEMOVE)
  1375. {
  1376. prepend_cursor_history( msg->x, msg->y, msg->time, msg_data->info );
  1377. if (update_desktop_cursor_pos( desktop, msg->x, msg->y )) always_queue = 1;
  1378. }
  1379. if (desktop->keystate[VK_LBUTTON] & 0x80) msg->wparam |= MK_LBUTTON;
  1380. if (desktop->keystate[VK_MBUTTON] & 0x80) msg->wparam |= MK_MBUTTON;
  1381. if (desktop->keystate[VK_RBUTTON] & 0x80) msg->wparam |= MK_RBUTTON;
  1382. if (desktop->keystate[VK_SHIFT] & 0x80) msg->wparam |= MK_SHIFT;
  1383. if (desktop->keystate[VK_CONTROL] & 0x80) msg->wparam |= MK_CONTROL;
  1384. if (desktop->keystate[VK_XBUTTON1] & 0x80) msg->wparam |= MK_XBUTTON1;
  1385. if (desktop->keystate[VK_XBUTTON2] & 0x80) msg->wparam |= MK_XBUTTON2;
  1386. }
  1387. msg->x = desktop->cursor.x;
  1388. msg->y = desktop->cursor.y;
  1389. if (msg->win && (thread = get_window_thread( msg->win )))
  1390. {
  1391. input = thread->queue->input;
  1392. release_object( thread );
  1393. }
  1394. else input = desktop->foreground_input;
  1395. win = find_hardware_message_window( desktop, input, msg, &msg_code, &thread );
  1396. if (!win || !thread)
  1397. {
  1398. if (input) update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
  1399. free_message( msg );
  1400. return;
  1401. }
  1402. input = thread->queue->input;
  1403. if (win != desktop->cursor.win) always_queue = 1;
  1404. desktop->cursor.win = win;
  1405. if (!always_queue || merge_message( input, msg )) free_message( msg );
  1406. else
  1407. {
  1408. msg->unique_id = 0; /* will be set once we return it to the app */
  1409. list_add_tail( &input->msg_list, &msg->entry );
  1410. set_queue_bits( thread->queue, get_hardware_msg_bit(msg) );
  1411. }
  1412. release_object( thread );
  1413. }
  1414. /* send the low-level hook message for a given hardware message */
  1415. static int send_hook_ll_message( struct desktop *desktop, struct message *hardware_msg,
  1416. const hw_input_t *input, struct msg_queue *sender )
  1417. {
  1418. struct thread *hook_thread;
  1419. struct msg_queue *queue;
  1420. struct message *msg;
  1421. timeout_t timeout = 2000 * -10000; /* FIXME: load from registry */
  1422. int id = (input->type == INPUT_MOUSE) ? WH_MOUSE_LL : WH_KEYBOARD_LL;
  1423. if (!(hook_thread = get_first_global_hook( id ))) return 0;
  1424. if (!(queue = hook_thread->queue)) return 0;
  1425. if (is_queue_hung( queue )) return 0;
  1426. if (!(msg = mem_alloc( sizeof(*msg) ))) return 0;
  1427. msg->type = MSG_HOOK_LL;
  1428. msg->win = 0;
  1429. msg->msg = id;
  1430. msg->wparam = hardware_msg->msg;
  1431. msg->x = hardware_msg->x;
  1432. msg->y = hardware_msg->y;
  1433. msg->time = hardware_msg->time;
  1434. msg->data_size = hardware_msg->data_size;
  1435. msg->result = NULL;
  1436. if (input->type == INPUT_KEYBOARD)
  1437. {
  1438. unsigned short vkey = input->kbd.vkey;
  1439. if (input->kbd.flags & KEYEVENTF_UNICODE) vkey = VK_PACKET;
  1440. msg->lparam = (input->kbd.scan << 16) | vkey;
  1441. }
  1442. else msg->lparam = input->mouse.data << 16;
  1443. if (!(msg->data = memdup( hardware_msg->data, hardware_msg->data_size )) ||
  1444. !(msg->result = alloc_message_result( sender, queue, msg, timeout )))
  1445. {
  1446. free_message( msg );
  1447. return 0;
  1448. }
  1449. msg->result->hardware_msg = hardware_msg;
  1450. msg->result->desktop = (struct desktop *)grab_object( desktop );
  1451. list_add_tail( &queue->msg_list[SEND_MESSAGE], &msg->entry );
  1452. set_queue_bits( queue, QS_SENDMESSAGE );
  1453. return 1;
  1454. }
  1455. /* get the foreground thread for a desktop and a window receiving input */
  1456. static struct thread *get_foreground_thread( struct desktop *desktop, user_handle_t window )
  1457. {
  1458. /* if desktop has no foreground process, assume the receiving window is */
  1459. if (desktop->foreground_input) return get_window_thread( desktop->foreground_input->focus );
  1460. if (window) return get_window_thread( window );
  1461. return NULL;
  1462. }
  1463. struct rawinput_message
  1464. {
  1465. struct thread *foreground;
  1466. struct desktop *desktop;
  1467. struct hw_msg_source source;
  1468. unsigned int time;
  1469. unsigned int message;
  1470. struct hardware_msg_data data;
  1471. const void *hid_report;
  1472. };
  1473. /* check if process is supposed to receive a WM_INPUT message and eventually queue it */
  1474. static int queue_rawinput_message( struct process* process, void *arg )
  1475. {
  1476. const struct rawinput_message* raw_msg = arg;
  1477. const struct rawinput_device_entry *entry;
  1478. const struct rawinput_device *device = NULL;
  1479. struct desktop *target_desktop = NULL, *desktop = NULL;
  1480. struct thread *target_thread = NULL, *foreground = NULL;
  1481. struct message *msg;
  1482. data_size_t report_size;
  1483. int wparam = RIM_INPUT;
  1484. if (raw_msg->data.rawinput.type == RIM_TYPEMOUSE)
  1485. device = process->rawinput_mouse;
  1486. else if (raw_msg->data.rawinput.type == RIM_TYPEKEYBOARD)
  1487. device = process->rawinput_kbd;
  1488. else if ((entry = find_rawinput_device( process, raw_msg->data.rawinput.hid.usage_page, raw_msg->data.rawinput.hid.usage )))
  1489. device = &entry->device;
  1490. if (!device) return 0;
  1491. if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && !(device->flags & RIDEV_DEVNOTIFY)) return 0;
  1492. if (raw_msg->desktop) desktop = (struct desktop *)grab_object( raw_msg->desktop );
  1493. else if (!(desktop = get_desktop_obj( process, process->desktop, 0 ))) goto done;
  1494. if (raw_msg->foreground) foreground = (struct thread *)grab_object( raw_msg->foreground );
  1495. else if (!(foreground = get_foreground_thread( desktop, 0 ))) goto done;
  1496. if (process != foreground->process)
  1497. {
  1498. if (raw_msg->message == WM_INPUT && !(device->flags & RIDEV_INPUTSINK)) goto done;
  1499. if (!(target_thread = get_window_thread( device->target ))) goto done;
  1500. if (!(target_desktop = get_thread_desktop( target_thread, 0 ))) goto done;
  1501. if (target_desktop != desktop) goto done;
  1502. wparam = RIM_INPUTSINK;
  1503. }
  1504. if (raw_msg->data.rawinput.type != RIM_TYPEHID || !raw_msg->hid_report) report_size = 0;
  1505. else report_size = raw_msg->data.size - sizeof(raw_msg->data);
  1506. if (!(msg = alloc_hardware_message( raw_msg->data.info, raw_msg->source, raw_msg->time, report_size )))
  1507. goto done;
  1508. msg->win = device->target;
  1509. msg->msg = raw_msg->message;
  1510. msg->wparam = wparam;
  1511. msg->lparam = 0;
  1512. memcpy( msg->data, &raw_msg->data, sizeof(raw_msg->data) );
  1513. if (report_size) memcpy( (struct hardware_msg_data *)msg->data + 1, raw_msg->hid_report, report_size );
  1514. if (raw_msg->message == WM_INPUT_DEVICE_CHANGE && raw_msg->data.rawinput.type == RIM_TYPEHID)
  1515. {
  1516. msg->wparam = raw_msg->data.rawinput.hid.param;
  1517. msg->lparam = raw_msg->data.rawinput.hid.device;
  1518. }
  1519. queue_hardware_message( desktop, msg, 1 );
  1520. done:
  1521. if (target_thread) release_object( target_thread );
  1522. if (target_desktop) release_object( target_desktop );
  1523. if (foreground) release_object( foreground );
  1524. if (desktop) release_object( desktop );
  1525. return 0;
  1526. }
  1527. /* queue a hardware message for a mouse event */
  1528. static int queue_mouse_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
  1529. unsigned int origin, struct msg_queue *sender )
  1530. {
  1531. const struct rawinput_device *device;
  1532. struct hardware_msg_data *msg_data;
  1533. struct rawinput_message raw_msg;
  1534. struct message *msg;
  1535. struct thread *foreground;
  1536. unsigned int i, time, flags;
  1537. struct hw_msg_source source = { IMDT_MOUSE, origin };
  1538. int wait = 0, x, y;
  1539. static const unsigned int messages[] =
  1540. {
  1541. WM_MOUSEMOVE, /* 0x0001 = MOUSEEVENTF_MOVE */
  1542. WM_LBUTTONDOWN, /* 0x0002 = MOUSEEVENTF_LEFTDOWN */
  1543. WM_LBUTTONUP, /* 0x0004 = MOUSEEVENTF_LEFTUP */
  1544. WM_RBUTTONDOWN, /* 0x0008 = MOUSEEVENTF_RIGHTDOWN */
  1545. WM_RBUTTONUP, /* 0x0010 = MOUSEEVENTF_RIGHTUP */
  1546. WM_MBUTTONDOWN, /* 0x0020 = MOUSEEVENTF_MIDDLEDOWN */
  1547. WM_MBUTTONUP, /* 0x0040 = MOUSEEVENTF_MIDDLEUP */
  1548. WM_XBUTTONDOWN, /* 0x0080 = MOUSEEVENTF_XDOWN */
  1549. WM_XBUTTONUP, /* 0x0100 = MOUSEEVENTF_XUP */
  1550. 0, /* 0x0200 = unused */
  1551. 0, /* 0x0400 = unused */
  1552. WM_MOUSEWHEEL, /* 0x0800 = MOUSEEVENTF_WHEEL */
  1553. WM_MOUSEHWHEEL /* 0x1000 = MOUSEEVENTF_HWHEEL */
  1554. };
  1555. desktop->cursor.last_change = get_tick_count();
  1556. flags = input->mouse.flags;
  1557. time = input->mouse.time;
  1558. if (!time) time = desktop->cursor.last_change;
  1559. if (flags & MOUSEEVENTF_MOVE)
  1560. {
  1561. if (flags & MOUSEEVENTF_ABSOLUTE)
  1562. {
  1563. x = input->mouse.x;
  1564. y = input->mouse.y;
  1565. if (flags & ~(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE) &&
  1566. x == desktop->cursor.x && y == desktop->cursor.y)
  1567. flags &= ~MOUSEEVENTF_MOVE;
  1568. }
  1569. else
  1570. {
  1571. x = desktop->cursor.x + input->mouse.x;
  1572. y = desktop->cursor.y + input->mouse.y;
  1573. }
  1574. }
  1575. else
  1576. {
  1577. x = desktop->cursor.x;
  1578. y = desktop->cursor.y;
  1579. }
  1580. if ((foreground = get_foreground_thread( desktop, win )))
  1581. {
  1582. memset( &raw_msg, 0, sizeof(raw_msg) );
  1583. raw_msg.foreground = foreground;
  1584. raw_msg.desktop = desktop;
  1585. raw_msg.source = source;
  1586. raw_msg.time = time;
  1587. raw_msg.message = WM_INPUT;
  1588. msg_data = &raw_msg.data;
  1589. msg_data->info = input->mouse.info;
  1590. msg_data->size = sizeof(*msg_data);
  1591. msg_data->flags = flags;
  1592. msg_data->rawinput.type = RIM_TYPEMOUSE;
  1593. msg_data->rawinput.mouse.x = x - desktop->cursor.x;
  1594. msg_data->rawinput.mouse.y = y - desktop->cursor.y;
  1595. msg_data->rawinput.mouse.data = input->mouse.data;
  1596. enum_processes( queue_rawinput_message, &raw_msg );
  1597. release_object( foreground );
  1598. }
  1599. if ((device = current->process->rawinput_mouse) && (device->flags & RIDEV_NOLEGACY))
  1600. {
  1601. update_desktop_mouse_state( desktop, flags, x, y, input->mouse.data << 16 );
  1602. return 0;
  1603. }
  1604. for (i = 0; i < ARRAY_SIZE( messages ); i++)
  1605. {
  1606. if (!messages[i]) continue;
  1607. if (!(flags & (1 << i))) continue;
  1608. flags &= ~(1 << i);
  1609. if (!(msg = alloc_hardware_message( input->mouse.info, source, time, 0 ))) return 0;
  1610. msg_data = msg->data;
  1611. msg->win = get_user_full_handle( win );
  1612. msg->msg = messages[i];
  1613. msg->wparam = input->mouse.data << 16;
  1614. msg->lparam = 0;
  1615. msg->x = x;
  1616. msg->y = y;
  1617. if (origin == IMO_INJECTED) msg_data->flags = LLMHF_INJECTED;
  1618. /* specify a sender only when sending the last message */
  1619. if (!(flags & ((1 << ARRAY_SIZE( messages )) - 1)))
  1620. {
  1621. if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
  1622. queue_hardware_message( desktop, msg, 0 );
  1623. }
  1624. else if (!send_hook_ll_message( desktop, msg, input, NULL ))
  1625. queue_hardware_message( desktop, msg, 0 );
  1626. }
  1627. return wait;
  1628. }
  1629. /* queue a hardware message for a keyboard event */
  1630. static int queue_keyboard_message( struct desktop *desktop, user_handle_t win, const hw_input_t *input,
  1631. unsigned int origin, struct msg_queue *sender )
  1632. {
  1633. struct hw_msg_source source = { IMDT_KEYBOARD, origin };
  1634. const struct rawinput_device *device;
  1635. struct hardware_msg_data *msg_data;
  1636. struct rawinput_message raw_msg;
  1637. struct message *msg;
  1638. struct thread *foreground;
  1639. unsigned char vkey = input->kbd.vkey;
  1640. unsigned int message_code, time;
  1641. int wait;
  1642. if (!(time = input->kbd.time)) time = get_tick_count();
  1643. if (!(input->kbd.flags & KEYEVENTF_UNICODE))
  1644. {
  1645. switch (vkey)
  1646. {
  1647. case VK_MENU:
  1648. case VK_LMENU:
  1649. case VK_RMENU:
  1650. vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RMENU : VK_LMENU;
  1651. break;
  1652. case VK_CONTROL:
  1653. case VK_LCONTROL:
  1654. case VK_RCONTROL:
  1655. vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RCONTROL : VK_LCONTROL;
  1656. break;
  1657. case VK_SHIFT:
  1658. case VK_LSHIFT:
  1659. case VK_RSHIFT:
  1660. vkey = (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) ? VK_RSHIFT : VK_LSHIFT;
  1661. break;
  1662. }
  1663. }
  1664. message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_KEYUP : WM_KEYDOWN;
  1665. switch (vkey)
  1666. {
  1667. case VK_LMENU:
  1668. case VK_RMENU:
  1669. if (input->kbd.flags & KEYEVENTF_KEYUP)
  1670. {
  1671. /* send WM_SYSKEYUP if Alt still pressed and no other key in between */
  1672. /* we use 0x02 as a flag to track if some other SYSKEYUP was sent already */
  1673. if ((desktop->keystate[VK_MENU] & 0x82) != 0x82) break;
  1674. message_code = WM_SYSKEYUP;
  1675. desktop->keystate[VK_MENU] &= ~0x02;
  1676. }
  1677. else
  1678. {
  1679. /* send WM_SYSKEYDOWN for Alt except with Ctrl */
  1680. if (desktop->keystate[VK_CONTROL] & 0x80) break;
  1681. message_code = WM_SYSKEYDOWN;
  1682. desktop->keystate[VK_MENU] |= 0x02;
  1683. }
  1684. break;
  1685. case VK_LCONTROL:
  1686. case VK_RCONTROL:
  1687. /* send WM_SYSKEYUP on release if Alt still pressed */
  1688. if (!(input->kbd.flags & KEYEVENTF_KEYUP)) break;
  1689. if (!(desktop->keystate[VK_MENU] & 0x80)) break;
  1690. message_code = WM_SYSKEYUP;
  1691. desktop->keystate[VK_MENU] &= ~0x02;
  1692. break;
  1693. default:
  1694. /* send WM_SYSKEY for Alt-anykey and for F10 */
  1695. if (desktop->keystate[VK_CONTROL] & 0x80) break;
  1696. if (!(desktop->keystate[VK_MENU] & 0x80)) break;
  1697. /* fall through */
  1698. case VK_F10:
  1699. message_code = (input->kbd.flags & KEYEVENTF_KEYUP) ? WM_SYSKEYUP : WM_SYSKEYDOWN;
  1700. desktop->keystate[VK_MENU] &= ~0x02;
  1701. break;
  1702. }
  1703. if ((foreground = get_foreground_thread( desktop, win )))
  1704. {
  1705. memset( &raw_msg, 0, sizeof(raw_msg) );
  1706. raw_msg.foreground = foreground;
  1707. raw_msg.desktop = desktop;
  1708. raw_msg.source = source;
  1709. raw_msg.time = time;
  1710. raw_msg.message = WM_INPUT;
  1711. msg_data = &raw_msg.data;
  1712. msg_data->info = input->kbd.info;
  1713. msg_data->size = sizeof(*msg_data);
  1714. msg_data->flags = input->kbd.flags;
  1715. msg_data->rawinput.type = RIM_TYPEKEYBOARD;
  1716. msg_data->rawinput.kbd.message = message_code;
  1717. msg_data->rawinput.kbd.vkey = vkey;
  1718. msg_data->rawinput.kbd.scan = input->kbd.scan;
  1719. enum_processes( queue_rawinput_message, &raw_msg );
  1720. release_object( foreground );
  1721. }
  1722. if ((device = current->process->rawinput_kbd) && (device->flags & RIDEV_NOLEGACY))
  1723. {
  1724. update_input_key_state( desktop, desktop->keystate, message_code, vkey );
  1725. return 0;
  1726. }
  1727. if (!(msg = alloc_hardware_message( input->kbd.info, source, time, 0 ))) return 0;
  1728. msg_data = msg->data;
  1729. msg->win = get_user_full_handle( win );
  1730. msg->msg = message_code;
  1731. msg->lparam = (input->kbd.scan << 16) | 1u; /* repeat count */
  1732. if (origin == IMO_INJECTED) msg_data->flags = LLKHF_INJECTED;
  1733. if (input->kbd.flags & KEYEVENTF_UNICODE && !vkey)
  1734. {
  1735. msg->wparam = VK_PACKET;
  1736. }
  1737. else
  1738. {
  1739. unsigned int flags = 0;
  1740. if (input->kbd.flags & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
  1741. /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
  1742. if (input->kbd.flags & KEYEVENTF_KEYUP) flags |= KF_REPEAT | KF_UP;
  1743. else if (desktop->keystate[vkey] & 0x80) flags |= KF_REPEAT;
  1744. msg->wparam = vkey;
  1745. msg->lparam |= flags << 16;
  1746. msg_data->flags |= (flags & (KF_EXTENDED | KF_ALTDOWN | KF_UP)) >> 8;
  1747. }
  1748. if (!(wait = send_hook_ll_message( desktop, msg, input, sender )))
  1749. queue_hardware_message( desktop, msg, 1 );
  1750. return wait;
  1751. }
  1752. /* queue a hardware message for a custom type of event */
  1753. static void queue_custom_hardware_message( struct desktop *desktop, user_handle_t win,
  1754. unsigned int origin, const hw_input_t *input )
  1755. {
  1756. struct hw_msg_source source = { IMDT_UNAVAILABLE, origin };
  1757. struct hardware_msg_data *msg_data;
  1758. struct rawinput_message raw_msg;
  1759. struct message *msg;
  1760. data_size_t report_size = 0;
  1761. switch (input->hw.msg)
  1762. {
  1763. case WM_INPUT:
  1764. case WM_INPUT_DEVICE_CHANGE:
  1765. memset( &raw_msg, 0, sizeof(raw_msg) );
  1766. raw_msg.source = source;
  1767. raw_msg.time = get_tick_count();
  1768. raw_msg.message = input->hw.msg;
  1769. if (input->hw.rawinput.type == RIM_TYPEHID)
  1770. {
  1771. raw_msg.hid_report = get_req_data();
  1772. report_size = input->hw.rawinput.hid.length * input->hw.rawinput.hid.count;
  1773. }
  1774. if (report_size != get_req_data_size())
  1775. {
  1776. set_error( STATUS_INVALID_PARAMETER );
  1777. return;
  1778. }
  1779. msg_data = &raw_msg.data;
  1780. msg_data->size = sizeof(*msg_data) + report_size;
  1781. msg_data->rawinput = input->hw.rawinput;
  1782. enum_processes( queue_rawinput_message, &raw_msg );
  1783. if (raw_msg.foreground) release_object( raw_msg.foreground );
  1784. return;
  1785. }
  1786. if (!(msg = alloc_hardware_message( 0, source, get_tick_count(), 0 ))) return;
  1787. msg->win = get_user_full_handle( win );
  1788. msg->msg = input->hw.msg;
  1789. msg->wparam = 0;
  1790. msg->lparam = input->hw.lparam;
  1791. msg->x = desktop->cursor.x;
  1792. msg->y = desktop->cursor.y;
  1793. queue_hardware_message( desktop, msg, 1 );
  1794. }
  1795. /* check message filter for a hardware message */
  1796. static int check_hw_message_filter( user_handle_t win, unsigned int msg_code,
  1797. user_handle_t filter_win, unsigned int first, unsigned int last )
  1798. {
  1799. if (msg_code >= WM_KEYFIRST && msg_code <= WM_KEYLAST)
  1800. {
  1801. /* we can only test the window for a keyboard message since the
  1802. * dest window for a mouse message depends on hittest */
  1803. if (filter_win && win != filter_win && !is_child_window( filter_win, win ))
  1804. return 0;
  1805. /* the message code is final for a keyboard message, we can simply check it */
  1806. return check_msg_filter( msg_code, first, last );
  1807. }
  1808. else /* mouse message */
  1809. {
  1810. /* we need to check all possible values that the message can have in the end */
  1811. if (check_msg_filter( msg_code, first, last )) return 1;
  1812. if (msg_code == WM_MOUSEWHEEL) return 0; /* no other possible value for this one */
  1813. /* all other messages can become non-client messages */
  1814. if (check_msg_filter( msg_code + (WM_NCMOUSEFIRST - WM_MOUSEFIRST), first, last )) return 1;
  1815. /* clicks can become double-clicks or non-client double-clicks */
  1816. if (msg_code == WM_LBUTTONDOWN || msg_code == WM_MBUTTONDOWN ||
  1817. msg_code == WM_RBUTTONDOWN || msg_code == WM_XBUTTONDOWN)
  1818. {
  1819. if (check_msg_filter( msg_code + (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
  1820. if (check_msg_filter( msg_code + (WM_NCLBUTTONDBLCLK - WM_LBUTTONDOWN), first, last )) return 1;
  1821. }
  1822. return 0;
  1823. }
  1824. }
  1825. /* find a hardware message for the given queue */
  1826. static int get_hardware_message( struct thread *thread, unsigned int hw_id, user_handle_t filter_win,
  1827. unsigned int first, unsigned int last, unsigned int flags,
  1828. struct get_message_reply *reply )
  1829. {
  1830. struct thread_input *input = thread->queue->input;
  1831. struct thread *win_thread;
  1832. struct list *ptr;
  1833. user_handle_t win;
  1834. int clear_bits, got_one = 0;
  1835. unsigned int msg_code;
  1836. ptr = list_head( &input->msg_list );
  1837. if (hw_id)
  1838. {
  1839. while (ptr)
  1840. {
  1841. struct message *msg = LIST_ENTRY( ptr, struct message, entry );
  1842. if (msg->unique_id == hw_id) break;
  1843. ptr = list_next( &input->msg_list, ptr );
  1844. }
  1845. if (!ptr) ptr = list_head( &input->msg_list );
  1846. else ptr = list_next( &input->msg_list, ptr ); /* start from the next one */
  1847. }
  1848. if (ptr == list_head( &input->msg_list ))
  1849. clear_bits = QS_INPUT;
  1850. else
  1851. clear_bits = 0; /* don't clear bits if we don't go through the whole list */
  1852. while (ptr)
  1853. {
  1854. struct message *msg = LIST_ENTRY( ptr, struct message, entry );
  1855. struct hardware_msg_data *data = msg->data;
  1856. ptr = list_next( &input->msg_list, ptr );
  1857. win = find_hardware_message_window( input->desktop, input, msg, &msg_code, &win_thread );
  1858. if (!win || !win_thread)
  1859. {
  1860. /* no window at all, remove it */
  1861. update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
  1862. list_remove( &msg->entry );
  1863. free_message( msg );
  1864. continue;
  1865. }
  1866. if (win_thread != thread)
  1867. {
  1868. if (win_thread->queue->input == input)
  1869. {
  1870. /* wake the other thread */
  1871. set_queue_bits( win_thread->queue, get_hardware_msg_bit(msg) );
  1872. got_one = 1;
  1873. }
  1874. else
  1875. {
  1876. /* for another thread input, drop it */
  1877. update_input_key_state( input->desktop, input->keystate, msg->msg, msg->wparam );
  1878. list_remove( &msg->entry );
  1879. free_message( msg );
  1880. }
  1881. release_object( win_thread );
  1882. continue;
  1883. }
  1884. release_object( win_thread );
  1885. /* if we already got a message for another thread, or if it doesn't
  1886. * match the filter we skip it */
  1887. if (got_one || !check_hw_message_filter( win, msg_code, filter_win, first, last ))
  1888. {
  1889. clear_bits &= ~get_hardware_msg_bit( msg );
  1890. continue;
  1891. }
  1892. reply->total = msg->data_size;
  1893. if (msg->data_size > get_reply_max_size())
  1894. {
  1895. set_error( STATUS_BUFFER_OVERFLOW );
  1896. return 1;
  1897. }
  1898. /* now we can return it */
  1899. if (!msg->unique_id) msg->unique_id = get_unique_id();
  1900. reply->type = MSG_HARDWARE;
  1901. reply->win = win;
  1902. reply->msg = msg_code;
  1903. reply->wparam = msg->wparam;
  1904. reply->lparam = msg->lparam;
  1905. reply->x = msg->x;
  1906. reply->y = msg->y;
  1907. reply->time = msg->time;
  1908. data->hw_id = msg->unique_id;
  1909. set_reply_data( msg->data, msg->data_size );
  1910. if ((msg->msg == WM_INPUT || msg->msg == WM_INPUT_DEVICE_CHANGE) && (flags & PM_REMOVE))
  1911. release_hardware_message( current->queue, data->hw_id );
  1912. return 1;
  1913. }
  1914. /* nothing found, clear the hardware queue bits */
  1915. clear_queue_bits( thread->queue, clear_bits );
  1916. return 0;
  1917. }
  1918. /* increment (or decrement if 'incr' is negative) the queue paint count */
  1919. void inc_queue_paint_count( struct thread *thread, int incr )
  1920. {
  1921. struct msg_queue *queue = thread->queue;
  1922. assert( queue );
  1923. if ((queue->paint_count += incr) < 0) queue->paint_count = 0;
  1924. if (queue->paint_count)
  1925. set_queue_bits( queue, QS_PAINT );
  1926. else
  1927. clear_queue_bits( queue, QS_PAINT );
  1928. }
  1929. /* remove all messages and timers belonging to a certain window */
  1930. void queue_cleanup_window( struct thread *thread, user_handle_t win )
  1931. {
  1932. struct msg_queue *queue = thread->queue;
  1933. struct list *ptr;
  1934. int i;
  1935. if (!queue) return;
  1936. /* remove timers */
  1937. ptr = list_head( &queue->pending_timers );
  1938. while (ptr)
  1939. {
  1940. struct list *next = list_next( &queue->pending_timers, ptr );
  1941. struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
  1942. if (timer->win == win) free_timer( queue, timer );
  1943. ptr = next;
  1944. }
  1945. ptr = list_head( &queue->expired_timers );
  1946. while (ptr)
  1947. {
  1948. struct list *next = list_next( &queue->expired_timers, ptr );
  1949. struct timer *timer = LIST_ENTRY( ptr, struct timer, entry );
  1950. if (timer->win == win) free_timer( queue, timer );
  1951. ptr = next;
  1952. }
  1953. /* remove messages */
  1954. for (i = 0; i < NB_MSG_KINDS; i++)
  1955. {
  1956. struct list *ptr, *next;
  1957. LIST_FOR_EACH_SAFE( ptr, next, &queue->msg_list[i] )
  1958. {
  1959. struct message *msg = LIST_ENTRY( ptr, struct message, entry );
  1960. if (msg->win == win)
  1961. {
  1962. if (msg->msg == WM_QUIT && !queue->quit_message)
  1963. {
  1964. queue->quit_message = 1;
  1965. queue->exit_code = msg->wparam;
  1966. }
  1967. remove_queue_message( queue, msg, i );
  1968. }
  1969. }
  1970. }
  1971. thread_input_cleanup_window( queue, win );
  1972. }
  1973. /* post a message to a window */
  1974. void post_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
  1975. {
  1976. struct message *msg;
  1977. struct thread *thread = get_window_thread( win );
  1978. if (!thread) return;
  1979. if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
  1980. {
  1981. msg->type = MSG_POSTED;
  1982. msg->win = get_user_full_handle( win );
  1983. msg->msg = message;
  1984. msg->wparam = wparam;
  1985. msg->lparam = lparam;
  1986. msg->result = NULL;
  1987. msg->data = NULL;
  1988. msg->data_size = 0;
  1989. get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
  1990. list_add_tail( &thread->queue->msg_list[POST_MESSAGE], &msg->entry );
  1991. set_queue_bits( thread->queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
  1992. if (message == WM_HOTKEY)
  1993. {
  1994. set_queue_bits( thread->queue, QS_HOTKEY );
  1995. thread->queue->hotkey_count++;
  1996. }
  1997. }
  1998. release_object( thread );
  1999. }
  2000. /* send a notify message to a window */
  2001. void send_notify_message( user_handle_t win, unsigned int message, lparam_t wparam, lparam_t lparam )
  2002. {
  2003. struct message *msg;
  2004. struct thread *thread = get_window_thread( win );
  2005. if (!thread) return;
  2006. if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
  2007. {
  2008. msg->type = MSG_NOTIFY;
  2009. msg->win = get_user_full_handle( win );
  2010. msg->msg = message;
  2011. msg->wparam = wparam;
  2012. msg->lparam = lparam;
  2013. msg->result = NULL;
  2014. msg->data = NULL;
  2015. msg->data_size = 0;
  2016. get_message_defaults( thread->queue, &msg->x, &msg->y, &msg->time );
  2017. list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
  2018. set_queue_bits( thread->queue, QS_SENDMESSAGE );
  2019. }
  2020. release_object( thread );
  2021. }
  2022. /* post a win event */
  2023. void post_win_event( struct thread *thread, unsigned int event,
  2024. user_handle_t win, unsigned int object_id,
  2025. unsigned int child_id, client_ptr_t hook_proc,
  2026. const WCHAR *module, data_size_t module_size,
  2027. user_handle_t hook)
  2028. {
  2029. struct message *msg;
  2030. if (thread->queue && (msg = mem_alloc( sizeof(*msg) )))
  2031. {
  2032. struct winevent_msg_data *data;
  2033. msg->type = MSG_WINEVENT;
  2034. msg->win = get_user_full_handle( win );
  2035. msg->msg = event;
  2036. msg->wparam = object_id;
  2037. msg->lparam = child_id;
  2038. msg->time = get_tick_count();
  2039. msg->result = NULL;
  2040. if ((data = malloc( sizeof(*data) + module_size )))
  2041. {
  2042. data->hook = hook;
  2043. data->tid = get_thread_id( current );
  2044. data->hook_proc = hook_proc;
  2045. memcpy( data + 1, module, module_size );
  2046. msg->data = data;
  2047. msg->data_size = sizeof(*data) + module_size;
  2048. if (debug_level > 1)
  2049. fprintf( stderr, "post_win_event: tid %04x event %04x win %08x object_id %d child_id %d\n",
  2050. get_thread_id(thread), event, win, object_id, child_id );
  2051. list_add_tail( &thread->queue->msg_list[SEND_MESSAGE], &msg->entry );
  2052. set_queue_bits( thread->queue, QS_SENDMESSAGE );
  2053. }
  2054. else
  2055. free( msg );
  2056. }
  2057. }
  2058. /* free all hotkeys on a desktop, optionally filtering by window */
  2059. void free_hotkeys( struct desktop *desktop, user_handle_t window )
  2060. {
  2061. struct hotkey *hotkey, *hotkey2;
  2062. LIST_FOR_EACH_ENTRY_SAFE( hotkey, hotkey2, &desktop->hotkeys, struct hotkey, entry )
  2063. {
  2064. if (!window || hotkey->win == window)
  2065. {
  2066. list_remove( &hotkey->entry );
  2067. free( hotkey );
  2068. }
  2069. }
  2070. }
  2071. /* check if the thread owning the window is hung */
  2072. DECL_HANDLER(is_window_hung)
  2073. {
  2074. struct thread *thread;
  2075. thread = get_window_thread( req->win );
  2076. if (thread)
  2077. {
  2078. reply->is_hung = is_queue_hung( thread->queue );
  2079. release_object( thread );
  2080. }
  2081. else reply->is_hung = 0;
  2082. }
  2083. /* get the message queue of the current thread */
  2084. DECL_HANDLER(get_msg_queue)
  2085. {
  2086. struct msg_queue *queue = get_current_queue();
  2087. reply->handle = 0;
  2088. if (queue) reply->handle = alloc_handle( current->process, queue, SYNCHRONIZE, 0 );
  2089. }
  2090. /* set the file descriptor associated to the current thread queue */
  2091. DECL_HANDLER(set_queue_fd)
  2092. {
  2093. struct msg_queue *queue = get_current_queue();
  2094. struct file *file;
  2095. int unix_fd;
  2096. if (queue->fd) /* fd can only be set once */
  2097. {
  2098. set_error( STATUS_ACCESS_DENIED );
  2099. return;
  2100. }
  2101. if (!(file = get_file_obj( current->process, req->handle, SYNCHRONIZE ))) return;
  2102. if ((unix_fd = get_file_unix_fd( file )) != -1)
  2103. {
  2104. if ((unix_fd = dup( unix_fd )) != -1)
  2105. queue->fd = create_anonymous_fd( &msg_queue_fd_ops, unix_fd, &queue->obj, 0 );
  2106. else
  2107. file_set_error();
  2108. }
  2109. release_object( file );
  2110. }
  2111. /* set the current message queue wakeup mask */
  2112. DECL_HANDLER(set_queue_mask)
  2113. {
  2114. struct msg_queue *queue = get_current_queue();
  2115. if (queue)
  2116. {
  2117. queue->wake_mask = req->wake_mask;
  2118. queue->changed_mask = req->changed_mask;
  2119. reply->wake_bits = queue->wake_bits;
  2120. reply->changed_bits = queue->changed_bits;
  2121. if (is_signaled( queue ))
  2122. {
  2123. /* if skip wait is set, do what would have been done in the subsequent wait */
  2124. if (req->skip_wait) queue->wake_mask = queue->changed_mask = 0;
  2125. else wake_up( &queue->obj, 0 );
  2126. }
  2127. }
  2128. }
  2129. /* get the current message queue status */
  2130. DECL_HANDLER(get_queue_status)
  2131. {
  2132. struct msg_queue *queue = current->queue;
  2133. if (queue)
  2134. {
  2135. reply->wake_bits = queue->wake_bits;
  2136. reply->changed_bits = queue->changed_bits;
  2137. queue->changed_bits &= ~req->clear_bits;
  2138. }
  2139. else reply->wake_bits = reply->changed_bits = 0;
  2140. }
  2141. /* send a message to a thread queue */
  2142. DECL_HANDLER(send_message)
  2143. {
  2144. struct message *msg;
  2145. struct msg_queue *send_queue = get_current_queue();
  2146. struct msg_queue *recv_queue = NULL;
  2147. struct thread *thread = NULL;
  2148. if (!(thread = get_thread_from_id( req->id ))) return;
  2149. if (!(recv_queue = thread->queue))
  2150. {
  2151. set_error( STATUS_INVALID_PARAMETER );
  2152. release_object( thread );
  2153. return;
  2154. }
  2155. if ((req->flags & SEND_MSG_ABORT_IF_HUNG) && is_queue_hung(recv_queue))
  2156. {
  2157. set_error( STATUS_TIMEOUT );
  2158. release_object( thread );
  2159. return;
  2160. }
  2161. if ((msg = mem_alloc( sizeof(*msg) )))
  2162. {
  2163. msg->type = req->type;
  2164. msg->win = get_user_full_handle( req->win );
  2165. msg->msg = req->msg;
  2166. msg->wparam = req->wparam;
  2167. msg->lparam = req->lparam;
  2168. msg->result = NULL;
  2169. msg->data = NULL;
  2170. msg->data_size = get_req_data_size();
  2171. get_message_defaults( recv_queue, &msg->x, &msg->y, &msg->time );
  2172. if (msg->data_size && !(msg->data = memdup( get_req_data(), msg->data_size )))
  2173. {
  2174. free( msg );
  2175. release_object( thread );
  2176. return;
  2177. }
  2178. switch(msg->type)
  2179. {
  2180. case MSG_OTHER_PROCESS:
  2181. case MSG_ASCII:
  2182. case MSG_UNICODE:
  2183. case MSG_CALLBACK:
  2184. if (!(msg->result = alloc_message_result( send_queue, recv_queue, msg, req->timeout )))
  2185. {
  2186. free_message( msg );
  2187. break;
  2188. }
  2189. /* fall through */
  2190. case MSG_NOTIFY:
  2191. list_add_tail( &recv_queue->msg_list[SEND_MESSAGE], &msg->entry );
  2192. set_queue_bits( recv_queue, QS_SENDMESSAGE );
  2193. break;
  2194. case MSG_POSTED:
  2195. list_add_tail( &recv_queue->msg_list[POST_MESSAGE], &msg->entry );
  2196. set_queue_bits( recv_queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
  2197. if (msg->msg == WM_HOTKEY)
  2198. {
  2199. set_queue_bits( recv_queue, QS_HOTKEY );
  2200. recv_queue->hotkey_count++;
  2201. }
  2202. break;
  2203. case MSG_HARDWARE: /* should use send_hardware_message instead */
  2204. case MSG_CALLBACK_RESULT: /* cannot send this one */
  2205. case MSG_HOOK_LL: /* generated internally */
  2206. default:
  2207. set_error( STATUS_INVALID_PARAMETER );
  2208. free( msg );
  2209. break;
  2210. }
  2211. }
  2212. release_object( thread );
  2213. }
  2214. /* send a hardware message to a thread queue */
  2215. DECL_HANDLER(send_hardware_message)
  2216. {
  2217. struct thread *thread = NULL;
  2218. struct desktop *desktop;
  2219. unsigned int origin = (req->flags & SEND_HWMSG_INJECTED ? IMO_INJECTED : IMO_HARDWARE);
  2220. struct msg_queue *sender = get_current_queue();
  2221. data_size_t size = min( 256, get_reply_max_size() );
  2222. if (!(desktop = get_thread_desktop( current, 0 ))) return;
  2223. if (req->win)
  2224. {
  2225. if (!(thread = get_window_thread( req->win ))) return;
  2226. if (desktop != thread->queue->input->desktop)
  2227. {
  2228. /* don't allow queuing events to a different desktop */
  2229. release_object( desktop );
  2230. return;
  2231. }
  2232. }
  2233. reply->prev_x = desktop->cursor.x;
  2234. reply->prev_y = desktop->cursor.y;
  2235. switch (req->input.type)
  2236. {
  2237. case INPUT_MOUSE:
  2238. reply->wait = queue_mouse_message( desktop, req->win, &req->input, origin, sender );
  2239. break;
  2240. case INPUT_KEYBOARD:
  2241. reply->wait = queue_keyboard_message( desktop, req->win, &req->input, origin, sender );
  2242. break;
  2243. case INPUT_HARDWARE:
  2244. queue_custom_hardware_message( desktop, req->win, origin, &req->input );
  2245. break;
  2246. default:
  2247. set_error( STATUS_INVALID_PARAMETER );
  2248. }
  2249. if (thread) release_object( thread );
  2250. reply->new_x = desktop->cursor.x;
  2251. reply->new_y = desktop->cursor.y;
  2252. set_reply_data( desktop->keystate, size );
  2253. release_object( desktop );
  2254. }
  2255. /* post a quit message to the current queue */
  2256. DECL_HANDLER(post_quit_message)
  2257. {
  2258. struct msg_queue *queue = get_current_queue();
  2259. if (!queue)
  2260. return;
  2261. queue->quit_message = 1;
  2262. queue->exit_code = req->exit_code;
  2263. set_queue_bits( queue, QS_POSTMESSAGE|QS_ALLPOSTMESSAGE );
  2264. }
  2265. /* get a message from the current queue */
  2266. DECL_HANDLER(get_message)
  2267. {
  2268. struct timer *timer;
  2269. struct list *ptr;
  2270. struct msg_queue *queue = get_current_queue();
  2271. user_handle_t get_win = get_user_full_handle( req->get_win );
  2272. unsigned int filter = req->flags >> 16;
  2273. reply->active_hooks = get_active_hooks();
  2274. if (get_win && get_win != 1 && get_win != -1 && !get_user_object( get_win, USER_WINDOW ))
  2275. {
  2276. set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
  2277. return;
  2278. }
  2279. if (!queue) return;
  2280. queue->last_get_msg = current_time;
  2281. if (!filter) filter = QS_ALLINPUT;
  2282. /* first check for sent messages */
  2283. if ((ptr = list_head( &queue->msg_list[SEND_MESSAGE] )))
  2284. {
  2285. struct message *msg = LIST_ENTRY( ptr, struct message, entry );
  2286. receive_message( queue, msg, reply );
  2287. return;
  2288. }
  2289. /* clear changed bits so we can wait on them if we don't find a message */
  2290. if (filter & QS_POSTMESSAGE)
  2291. {
  2292. queue->changed_bits &= ~(QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER);
  2293. if (req->get_first == 0 && req->get_last == ~0U) queue->changed_bits &= ~QS_ALLPOSTMESSAGE;
  2294. }
  2295. if (filter & QS_INPUT) queue->changed_bits &= ~QS_INPUT;
  2296. if (filter & QS_PAINT) queue->changed_bits &= ~QS_PAINT;
  2297. /* then check for posted messages */
  2298. if ((filter & QS_POSTMESSAGE) &&
  2299. get_posted_message( queue, get_win, req->get_first, req->get_last, req->flags, reply ))
  2300. return;
  2301. if ((filter & QS_HOTKEY) && queue->hotkey_count &&
  2302. req->get_first <= WM_HOTKEY && req->get_last >= WM_HOTKEY &&
  2303. get_posted_message( queue, get_win, WM_HOTKEY, WM_HOTKEY, req->flags, reply ))
  2304. return;
  2305. /* only check for quit messages if not posted messages pending */
  2306. if ((filter & QS_POSTMESSAGE) && get_quit_message( queue, req->flags, reply ))
  2307. return;
  2308. /* then check for any raw hardware message */
  2309. if ((filter & QS_INPUT) &&
  2310. filter_contains_hw_range( req->get_first, req->get_last ) &&
  2311. get_hardware_message( current, req->hw_id, get_win, req->get_first, req->get_last, req->flags, reply ))
  2312. return;
  2313. /* now check for WM_PAINT */
  2314. if ((filter & QS_PAINT) &&
  2315. queue->paint_count &&
  2316. check_msg_filter( WM_PAINT, req->get_first, req->get_last ) &&
  2317. (reply->win = find_window_to_repaint( get_win, current )))
  2318. {
  2319. reply->type = MSG_POSTED;
  2320. reply->msg = WM_PAINT;
  2321. reply->wparam = 0;
  2322. reply->lparam = 0;
  2323. get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
  2324. return;
  2325. }
  2326. /* now check for timer */
  2327. if ((filter & QS_TIMER) &&
  2328. (timer = find_expired_timer( queue, get_win, req->get_first,
  2329. req->get_last, (req->flags & PM_REMOVE) )))
  2330. {
  2331. reply->type = MSG_POSTED;
  2332. reply->win = timer->win;
  2333. reply->msg = timer->msg;
  2334. reply->wparam = timer->id;
  2335. reply->lparam = timer->lparam;
  2336. get_message_defaults( queue, &reply->x, &reply->y, &reply->time );
  2337. if (!(req->flags & PM_NOYIELD) && current->process->idle_event)
  2338. set_event( current->process->idle_event );
  2339. return;
  2340. }
  2341. if (get_win == -1 && current->process->idle_event) set_event( current->process->idle_event );
  2342. queue->wake_mask = req->wake_mask;
  2343. queue->changed_mask = req->changed_mask;
  2344. set_error( STATUS_PENDING ); /* FIXME */
  2345. }
  2346. /* reply to a sent message */
  2347. DECL_HANDLER(reply_message)
  2348. {
  2349. if (!current->queue) set_error( STATUS_ACCESS_DENIED );
  2350. else if (current->queue->recv_result)
  2351. reply_message( current->queue, req->result, 0, req->remove,
  2352. get_req_data(), get_req_data_size() );
  2353. }
  2354. /* accept the current hardware message */
  2355. DECL_HANDLER(accept_hardware_message)
  2356. {
  2357. if (current->queue)
  2358. release_hardware_message( current->queue, req->hw_id );
  2359. else
  2360. set_error( STATUS_ACCESS_DENIED );
  2361. }
  2362. /* retrieve the reply for the last message sent */
  2363. DECL_HANDLER(get_message_reply)
  2364. {
  2365. struct message_result *result;
  2366. struct list *entry;
  2367. struct msg_queue *queue = current->queue;
  2368. if (queue)
  2369. {
  2370. set_error( STATUS_PENDING );
  2371. reply->result = 0;
  2372. if (!(entry = list_head( &queue->send_result ))) return; /* no reply ready */
  2373. result = LIST_ENTRY( entry, struct message_result, sender_entry );
  2374. if (result->replied || req->cancel)
  2375. {
  2376. if (result->replied)
  2377. {
  2378. reply->result = result->result;
  2379. set_error( result->error );
  2380. if (result->data)
  2381. {
  2382. data_size_t data_len = min( result->data_size, get_reply_max_size() );
  2383. set_reply_data_ptr( result->data, data_len );
  2384. result->data = NULL;
  2385. result->data_size = 0;
  2386. }
  2387. }
  2388. remove_result_from_sender( result );
  2389. entry = list_head( &queue->send_result );
  2390. if (!entry) clear_queue_bits( queue, QS_SMRESULT );
  2391. else
  2392. {
  2393. result = LIST_ENTRY( entry, struct message_result, sender_entry );
  2394. if (result->replied) set_queue_bits( queue, QS_SMRESULT );
  2395. else clear_queue_bits( queue, QS_SMRESULT );
  2396. }
  2397. }
  2398. }
  2399. else set_error( STATUS_ACCESS_DENIED );
  2400. }
  2401. /* set a window timer */
  2402. DECL_HANDLER(set_win_timer)
  2403. {
  2404. struct timer *timer;
  2405. struct msg_queue *queue;
  2406. struct thread *thread = NULL;
  2407. user_handle_t win = 0;
  2408. lparam_t id = req->id;
  2409. if (req->win)
  2410. {
  2411. if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
  2412. {
  2413. set_error( STATUS_INVALID_HANDLE );
  2414. return;
  2415. }
  2416. if (thread->process != current->process)
  2417. {
  2418. release_object( thread );
  2419. set_error( STATUS_ACCESS_DENIED );
  2420. return;
  2421. }
  2422. queue = thread->queue;
  2423. /* remove it if it existed already */
  2424. if ((timer = find_timer( queue, win, req->msg, id ))) free_timer( queue, timer );
  2425. }
  2426. else
  2427. {
  2428. queue = get_current_queue();
  2429. /* look for a timer with this id */
  2430. if (id && (timer = find_timer( queue, 0, req->msg, id )))
  2431. {
  2432. /* free and reuse id */
  2433. free_timer( queue, timer );
  2434. }
  2435. else
  2436. {
  2437. lparam_t end_id = queue->next_timer_id;
  2438. /* find a free id for it */
  2439. while (1)
  2440. {
  2441. id = queue->next_timer_id;
  2442. if (--queue->next_timer_id <= 0x100) queue->next_timer_id = 0x7fff;
  2443. if (!find_timer( queue, 0, req->msg, id )) break;
  2444. if (queue->next_timer_id == end_id)
  2445. {
  2446. set_win32_error( ERROR_NO_MORE_USER_HANDLES );
  2447. return;
  2448. }
  2449. }
  2450. }
  2451. }
  2452. if ((timer = set_timer( queue, req->rate )))
  2453. {
  2454. timer->win = win;
  2455. timer->msg = req->msg;
  2456. timer->id = id;
  2457. timer->lparam = req->lparam;
  2458. reply->id = id;
  2459. }
  2460. if (thread) release_object( thread );
  2461. }
  2462. /* kill a window timer */
  2463. DECL_HANDLER(kill_win_timer)
  2464. {
  2465. struct timer *timer;
  2466. struct thread *thread;
  2467. user_handle_t win = 0;
  2468. if (req->win)
  2469. {
  2470. if (!(win = get_user_full_handle( req->win )) || !(thread = get_window_thread( win )))
  2471. {
  2472. set_error( STATUS_INVALID_HANDLE );
  2473. return;
  2474. }
  2475. if (thread->process != current->process)
  2476. {
  2477. release_object( thread );
  2478. set_error( STATUS_ACCESS_DENIED );
  2479. return;
  2480. }
  2481. }
  2482. else thread = (struct thread *)grab_object( current );
  2483. if (thread->queue && (timer = find_timer( thread->queue, win, req->msg, req->id )))
  2484. free_timer( thread->queue, timer );
  2485. else
  2486. set_error( STATUS_INVALID_PARAMETER );
  2487. release_object( thread );
  2488. }
  2489. DECL_HANDLER(register_hotkey)
  2490. {
  2491. struct desktop *desktop;
  2492. user_handle_t win_handle = req->window;
  2493. struct hotkey *hotkey;
  2494. struct hotkey *new_hotkey = NULL;
  2495. struct thread *thread;
  2496. const int modifier_flags = MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_WIN;
  2497. if (!(desktop = get_thread_desktop( current, 0 ))) return;
  2498. if (win_handle)
  2499. {
  2500. if (!(win_handle = get_valid_window_handle( win_handle )))
  2501. {
  2502. release_object( desktop );
  2503. return;
  2504. }
  2505. thread = get_window_thread( win_handle );
  2506. if (thread) release_object( thread );
  2507. if (thread != current)
  2508. {
  2509. release_object( desktop );
  2510. set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
  2511. return;
  2512. }
  2513. }
  2514. LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
  2515. {
  2516. if (req->vkey == hotkey->vkey &&
  2517. (req->flags & modifier_flags) == (hotkey->flags & modifier_flags))
  2518. {
  2519. release_object( desktop );
  2520. set_win32_error( ERROR_HOTKEY_ALREADY_REGISTERED );
  2521. return;
  2522. }
  2523. if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
  2524. new_hotkey = hotkey;
  2525. }
  2526. if (new_hotkey)
  2527. {
  2528. reply->replaced = 1;
  2529. reply->flags = new_hotkey->flags;
  2530. reply->vkey = new_hotkey->vkey;
  2531. }
  2532. else
  2533. {
  2534. new_hotkey = mem_alloc( sizeof(*new_hotkey) );
  2535. if (new_hotkey)
  2536. {
  2537. list_add_tail( &desktop->hotkeys, &new_hotkey->entry );
  2538. new_hotkey->queue = current->queue;
  2539. new_hotkey->win = win_handle;
  2540. new_hotkey->id = req->id;
  2541. }
  2542. }
  2543. if (new_hotkey)
  2544. {
  2545. new_hotkey->flags = req->flags;
  2546. new_hotkey->vkey = req->vkey;
  2547. }
  2548. release_object( desktop );
  2549. }
  2550. DECL_HANDLER(unregister_hotkey)
  2551. {
  2552. struct desktop *desktop;
  2553. user_handle_t win_handle = req->window;
  2554. struct hotkey *hotkey;
  2555. struct thread *thread;
  2556. if (!(desktop = get_thread_desktop( current, 0 ))) return;
  2557. if (win_handle)
  2558. {
  2559. if (!(win_handle = get_valid_window_handle( win_handle )))
  2560. {
  2561. release_object( desktop );
  2562. return;
  2563. }
  2564. thread = get_window_thread( win_handle );
  2565. if (thread) release_object( thread );
  2566. if (thread != current)
  2567. {
  2568. release_object( desktop );
  2569. set_win32_error( ERROR_WINDOW_OF_OTHER_THREAD );
  2570. return;
  2571. }
  2572. }
  2573. LIST_FOR_EACH_ENTRY( hotkey, &desktop->hotkeys, struct hotkey, entry )
  2574. {
  2575. if (current->queue == hotkey->queue && win_handle == hotkey->win && req->id == hotkey->id)
  2576. goto found;
  2577. }
  2578. release_object( desktop );
  2579. set_win32_error( ERROR_HOTKEY_NOT_REGISTERED );
  2580. return;
  2581. found:
  2582. reply->flags = hotkey->flags;
  2583. reply->vkey = hotkey->vkey;
  2584. list_remove( &hotkey->entry );
  2585. free( hotkey );
  2586. release_object( desktop );
  2587. }
  2588. /* attach (or detach) thread inputs */
  2589. DECL_HANDLER(attach_thread_input)
  2590. {
  2591. struct thread *thread_from = get_thread_from_id( req->tid_from );
  2592. struct thread *thread_to = get_thread_from_id( req->tid_to );
  2593. if (!thread_from || !thread_to)
  2594. {
  2595. if (thread_from) release_object( thread_from );
  2596. if (thread_to) release_object( thread_to );
  2597. return;
  2598. }
  2599. if (thread_from != thread_to)
  2600. {
  2601. if (req->attach)
  2602. {
  2603. if ((thread_to->queue || thread_to == current) &&
  2604. (thread_from->queue || thread_from == current))
  2605. attach_thread_input( thread_from, thread_to );
  2606. else
  2607. set_error( STATUS_INVALID_PARAMETER );
  2608. }
  2609. else
  2610. {
  2611. if (thread_from->queue && thread_to->queue &&
  2612. thread_from->queue->input == thread_to->queue->input)
  2613. detach_thread_input( thread_from );
  2614. else
  2615. set_error( STATUS_ACCESS_DENIED );
  2616. }
  2617. }
  2618. else set_error( STATUS_ACCESS_DENIED );
  2619. release_object( thread_from );
  2620. release_object( thread_to );
  2621. }
  2622. /* get thread input data */
  2623. DECL_HANDLER(get_thread_input)
  2624. {
  2625. struct thread *thread = NULL;
  2626. struct desktop *desktop;
  2627. struct thread_input *input;
  2628. if (req->tid)
  2629. {
  2630. if (!(thread = get_thread_from_id( req->tid ))) return;
  2631. if (!(desktop = get_thread_desktop( thread, 0 )))
  2632. {
  2633. release_object( thread );
  2634. return;
  2635. }
  2636. input = thread->queue ? thread->queue->input : NULL;
  2637. }
  2638. else
  2639. {
  2640. if (!(desktop = get_thread_desktop( current, 0 ))) return;
  2641. input = desktop->foreground_input; /* get the foreground thread info */
  2642. }
  2643. if (input)
  2644. {
  2645. reply->focus = input->focus;
  2646. reply->capture = input->capture;
  2647. reply->active = input->active;
  2648. reply->menu_owner = input->menu_owner;
  2649. reply->move_size = input->move_size;
  2650. reply->caret = input->caret;
  2651. reply->cursor = input->cursor;
  2652. reply->show_count = input->cursor_count;
  2653. reply->rect = input->caret_rect;
  2654. }
  2655. /* foreground window is active window of foreground thread */
  2656. reply->foreground = desktop->foreground_input ? desktop->foreground_input->active : 0;
  2657. if (thread) release_object( thread );
  2658. release_object( desktop );
  2659. }
  2660. /* retrieve queue keyboard state for current thread or global async state */
  2661. DECL_HANDLER(get_key_state)
  2662. {
  2663. struct desktop *desktop;
  2664. data_size_t size = min( 256, get_reply_max_size() );
  2665. if (req->async) /* get global async key state */
  2666. {
  2667. if (!(desktop = get_thread_desktop( current, 0 ))) return;
  2668. if (req->key >= 0)
  2669. {
  2670. reply->state = desktop->keystate[req->key & 0xff];
  2671. desktop->keystate[req->key & 0xff] &= ~0x40;
  2672. }
  2673. set_reply_data( desktop->keystate, size );
  2674. release_object( desktop );
  2675. }
  2676. else if (!current->queue)
  2677. {
  2678. unsigned char *keystate;
  2679. /* fallback to desktop keystate */
  2680. if (!(desktop = get_thread_desktop( current, 0 ))) return;
  2681. if (req->key >= 0) reply->state = desktop->keystate[req->key & 0xff] & ~0x40;
  2682. if ((keystate = set_reply_data_size( size )))
  2683. {
  2684. unsigned int i;
  2685. for (i = 0; i < size; i++) keystate[i] = desktop->keystate[i] & ~0x40;
  2686. }
  2687. release_object( desktop );
  2688. }
  2689. else
  2690. {
  2691. unsigned char *keystate = current->queue->input->keystate;
  2692. if (req->key >= 0) reply->state = keystate[req->key & 0xff];
  2693. set_reply_data( keystate, size );
  2694. }
  2695. }
  2696. /* set queue keyboard state for current thread */
  2697. DECL_HANDLER(set_key_state)
  2698. {
  2699. struct desktop *desktop;
  2700. data_size_t size = min( 256, get_req_data_size() );
  2701. if (current->queue) memcpy( current->queue->input->keystate, get_req_data(), size );
  2702. if (req->async && (desktop = get_thread_desktop( current, 0 )))
  2703. {
  2704. memcpy( desktop->keystate, get_req_data(), size );
  2705. release_object( desktop );
  2706. }
  2707. }
  2708. /* set the system foreground window */
  2709. DECL_HANDLER(set_foreground_window)
  2710. {
  2711. struct thread *thread = NULL;
  2712. struct desktop *desktop;
  2713. struct msg_queue *queue = get_current_queue();
  2714. if (!(desktop = get_thread_desktop( current, 0 ))) return;
  2715. reply->previous = desktop->foreground_input ? desktop->foreground_input->active : 0;
  2716. reply->send_msg_old = (reply->previous && desktop->foreground_input != queue->input);
  2717. reply->send_msg_new = FALSE;
  2718. if (is_valid_foreground_window( req->handle ) &&
  2719. (thread = get_window_thread( req->handle )) &&
  2720. thread->queue->input->desktop == desktop)
  2721. {
  2722. set_foreground_input( desktop, thread->queue->input );
  2723. reply->send_msg_new = (desktop->foreground_input != queue->input);
  2724. }
  2725. else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
  2726. if (thread) release_object( thread );
  2727. release_object( desktop );
  2728. }
  2729. /* set the current thread focus window */
  2730. DECL_HANDLER(set_focus_window)
  2731. {
  2732. struct msg_queue *queue = get_current_queue();
  2733. reply->previous = 0;
  2734. if (queue && check_queue_input_window( queue, req->handle ))
  2735. {
  2736. reply->previous = queue->input->focus;
  2737. queue->input->focus = get_user_full_handle( req->handle );
  2738. }
  2739. }
  2740. /* set the current thread active window */
  2741. DECL_HANDLER(set_active_window)
  2742. {
  2743. struct msg_queue *queue = get_current_queue();
  2744. reply->previous = 0;
  2745. if (queue && check_queue_input_window( queue, req->handle ))
  2746. {
  2747. if (!req->handle || make_window_active( req->handle ))
  2748. {
  2749. reply->previous = queue->input->active;
  2750. queue->input->active = get_user_full_handle( req->handle );
  2751. }
  2752. else set_error( STATUS_INVALID_HANDLE );
  2753. }
  2754. }
  2755. /* set the current thread capture window */
  2756. DECL_HANDLER(set_capture_window)
  2757. {
  2758. struct msg_queue *queue = get_current_queue();
  2759. reply->previous = reply->full_handle = 0;
  2760. if (queue && check_queue_input_window( queue, req->handle ))
  2761. {
  2762. struct thread_input *input = queue->input;
  2763. /* if in menu mode, reject all requests to change focus, except if the menu bit is set */
  2764. if (input->menu_owner && !(req->flags & CAPTURE_MENU))
  2765. {
  2766. set_error(STATUS_ACCESS_DENIED);
  2767. return;
  2768. }
  2769. reply->previous = input->capture;
  2770. input->capture = get_user_full_handle( req->handle );
  2771. input->menu_owner = (req->flags & CAPTURE_MENU) ? input->capture : 0;
  2772. input->move_size = (req->flags & CAPTURE_MOVESIZE) ? input->capture : 0;
  2773. reply->full_handle = input->capture;
  2774. }
  2775. }
  2776. /* Set the current thread caret window */
  2777. DECL_HANDLER(set_caret_window)
  2778. {
  2779. struct msg_queue *queue = get_current_queue();
  2780. reply->previous = 0;
  2781. if (queue && check_queue_input_window( queue, req->handle ))
  2782. {
  2783. struct thread_input *input = queue->input;
  2784. reply->previous = input->caret;
  2785. reply->old_rect = input->caret_rect;
  2786. reply->old_hide = input->caret_hide;
  2787. reply->old_state = input->caret_state;
  2788. set_caret_window( input, get_user_full_handle(req->handle) );
  2789. input->caret_rect.right = input->caret_rect.left + req->width;
  2790. input->caret_rect.bottom = input->caret_rect.top + req->height;
  2791. }
  2792. }
  2793. /* Set the current thread caret information */
  2794. DECL_HANDLER(set_caret_info)
  2795. {
  2796. struct msg_queue *queue = get_current_queue();
  2797. struct thread_input *input;
  2798. if (!queue) return;
  2799. input = queue->input;
  2800. reply->full_handle = input->caret;
  2801. reply->old_rect = input->caret_rect;
  2802. reply->old_hide = input->caret_hide;
  2803. reply->old_state = input->caret_state;
  2804. if (req->handle && get_user_full_handle(req->handle) != input->caret)
  2805. {
  2806. set_error( STATUS_ACCESS_DENIED );
  2807. return;
  2808. }
  2809. if (req->flags & SET_CARET_POS)
  2810. {
  2811. input->caret_rect.right += req->x - input->caret_rect.left;
  2812. input->caret_rect.bottom += req->y - input->caret_rect.top;
  2813. input->caret_rect.left = req->x;
  2814. input->caret_rect.top = req->y;
  2815. }
  2816. if (req->flags & SET_CARET_HIDE)
  2817. {
  2818. input->caret_hide += req->hide;
  2819. if (input->caret_hide < 0) input->caret_hide = 0;
  2820. }
  2821. if (req->flags & SET_CARET_STATE)
  2822. {
  2823. switch (req->state)
  2824. {
  2825. case CARET_STATE_OFF: input->caret_state = 0; break;
  2826. case CARET_STATE_ON: input->caret_state = 1; break;
  2827. case CARET_STATE_TOGGLE: input->caret_state = !input->caret_state; break;
  2828. case CARET_STATE_ON_IF_MOVED:
  2829. if (req->x != reply->old_rect.left || req->y != reply->old_rect.top) input->caret_state = 1;
  2830. break;
  2831. }
  2832. }
  2833. }
  2834. /* get the time of the last input event */
  2835. DECL_HANDLER(get_last_input_time)
  2836. {
  2837. reply->time = last_input_time;
  2838. }
  2839. /* set/get the current cursor */
  2840. DECL_HANDLER(set_cursor)
  2841. {
  2842. struct msg_queue *queue = get_current_queue();
  2843. struct thread_input *input;
  2844. if (!queue) return;
  2845. input = queue->input;
  2846. reply->prev_handle = input->cursor;
  2847. reply->prev_count = input->cursor_count;
  2848. reply->prev_x = input->desktop->cursor.x;
  2849. reply->prev_y = input->desktop->cursor.y;
  2850. if (req->flags & SET_CURSOR_HANDLE)
  2851. {
  2852. if (req->handle && !get_user_object( req->handle, USER_CLIENT ))
  2853. {
  2854. set_win32_error( ERROR_INVALID_CURSOR_HANDLE );
  2855. return;
  2856. }
  2857. input->cursor = req->handle;
  2858. }
  2859. if (req->flags & SET_CURSOR_COUNT)
  2860. {
  2861. queue->cursor_count += req->show_count;
  2862. input->cursor_count += req->show_count;
  2863. }
  2864. if (req->flags & SET_CURSOR_POS)
  2865. {
  2866. set_cursor_pos( input->desktop, req->x, req->y );
  2867. }
  2868. if (req->flags & (SET_CURSOR_CLIP | SET_CURSOR_NOCLIP))
  2869. {
  2870. struct desktop *desktop = input->desktop;
  2871. /* only the desktop owner can set the message */
  2872. if (req->clip_msg && get_top_window_owner(desktop) == current->process)
  2873. desktop->cursor.clip_msg = req->clip_msg;
  2874. set_clip_rectangle( desktop, (req->flags & SET_CURSOR_NOCLIP) ? NULL : &req->clip, 0 );
  2875. }
  2876. reply->new_x = input->desktop->cursor.x;
  2877. reply->new_y = input->desktop->cursor.y;
  2878. reply->new_clip = input->desktop->cursor.clip;
  2879. reply->last_change = input->desktop->cursor.last_change;
  2880. }
  2881. /* Get the history of the 64 last cursor positions */
  2882. DECL_HANDLER(get_cursor_history)
  2883. {
  2884. cursor_pos_t *pos;
  2885. unsigned int i, count = min( 64, get_reply_max_size() / sizeof(*pos) );
  2886. if ((pos = set_reply_data_size( count * sizeof(*pos) )))
  2887. for (i = 0; i < count; i++)
  2888. pos[i] = cursor_history[(i + cursor_history_latest) % ARRAY_SIZE(cursor_history)];
  2889. }
  2890. DECL_HANDLER(get_rawinput_buffer)
  2891. {
  2892. struct thread_input *input = current->queue->input;
  2893. data_size_t size = 0, next_size = 0;
  2894. struct list *ptr;
  2895. char *buf, *cur, *tmp;
  2896. int count = 0, buf_size = 16 * sizeof(struct hardware_msg_data);
  2897. if (!req->buffer_size) buf = NULL;
  2898. else if (!(buf = mem_alloc( buf_size ))) return;
  2899. cur = buf;
  2900. ptr = list_head( &input->msg_list );
  2901. while (ptr)
  2902. {
  2903. struct message *msg = LIST_ENTRY( ptr, struct message, entry );
  2904. struct hardware_msg_data *data = msg->data;
  2905. data_size_t extra_size = data->size - sizeof(*data);
  2906. ptr = list_next( &input->msg_list, ptr );
  2907. if (msg->msg != WM_INPUT) continue;
  2908. next_size = req->rawinput_size + extra_size;
  2909. if (size + next_size > req->buffer_size) break;
  2910. if (cur + data->size > buf + get_reply_max_size()) break;
  2911. if (cur + data->size > buf + buf_size)
  2912. {
  2913. buf_size += buf_size / 2 + extra_size;
  2914. if (!(tmp = realloc( buf, buf_size )))
  2915. {
  2916. set_error( STATUS_NO_MEMORY );
  2917. return;
  2918. }
  2919. cur = tmp + (cur - buf);
  2920. buf = tmp;
  2921. }
  2922. memcpy( cur, data, data->size );
  2923. list_remove( &msg->entry );
  2924. free_message( msg );
  2925. size += next_size;
  2926. cur += sizeof(*data);
  2927. count++;
  2928. }
  2929. reply->next_size = next_size;
  2930. reply->count = count;
  2931. set_reply_data_ptr( buf, cur - buf );
  2932. }
  2933. DECL_HANDLER(update_rawinput_devices)
  2934. {
  2935. const struct rawinput_device *devices = get_req_data();
  2936. unsigned int device_count = get_req_data_size() / sizeof (*devices);
  2937. const struct rawinput_device_entry *e;
  2938. unsigned int i;
  2939. for (i = 0; i < device_count; ++i)
  2940. {
  2941. update_rawinput_device(&devices[i]);
  2942. }
  2943. e = find_rawinput_device( current->process, 1, 2 );
  2944. current->process->rawinput_mouse = e ? &e->device : NULL;
  2945. e = find_rawinput_device( current->process, 1, 6 );
  2946. current->process->rawinput_kbd = e ? &e->device : NULL;
  2947. }
  2948. DECL_HANDLER(get_rawinput_devices)
  2949. {
  2950. struct rawinput_device_entry *e;
  2951. struct rawinput_device *devices;
  2952. unsigned int i = 0, device_count = list_count( &current->process->rawinput_devices );
  2953. unsigned int size = device_count * sizeof(*devices);
  2954. reply->device_count = device_count;
  2955. /* no buffer provided, nothing else to do */
  2956. if (!get_reply_max_size()) return;
  2957. if (size > get_reply_max_size())
  2958. set_error( STATUS_BUFFER_TOO_SMALL );
  2959. else if ((devices = set_reply_data_size( size )))
  2960. {
  2961. LIST_FOR_EACH_ENTRY( e, &current->process->rawinput_devices, struct rawinput_device_entry, entry )
  2962. devices[i++] = e->device;
  2963. }
  2964. }