queue.c 110 KB

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