queue.c 107 KB

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