queue.c 114 KB

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