winstation.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * Server-side window stations and desktops handling
  3. *
  4. * Copyright (C) 2002, 2005 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 <stdio.h>
  22. #include <stdarg.h>
  23. #include <sys/types.h>
  24. #include "ntstatus.h"
  25. #define WIN32_NO_STATUS
  26. #include "windef.h"
  27. #include "winbase.h"
  28. #include "winuser.h"
  29. #include "winternl.h"
  30. #include "object.h"
  31. #include "handle.h"
  32. #include "request.h"
  33. #include "process.h"
  34. #include "user.h"
  35. #include "file.h"
  36. #include "security.h"
  37. #define DESKTOP_ALL_ACCESS 0x01ff
  38. static struct list winstation_list = LIST_INIT(winstation_list);
  39. static void winstation_dump( struct object *obj, int verbose );
  40. static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
  41. static struct object *winstation_lookup_name( struct object *obj, struct unicode_str *name,
  42. unsigned int attr, struct object *root );
  43. static void winstation_destroy( struct object *obj );
  44. static void desktop_dump( struct object *obj, int verbose );
  45. static int desktop_link_name( struct object *obj, struct object_name *name, struct object *parent );
  46. static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
  47. static void desktop_destroy( struct object *obj );
  48. static const WCHAR winstation_name[] = {'W','i','n','d','o','w','S','t','a','t','i','o','n'};
  49. struct type_descr winstation_type =
  50. {
  51. { winstation_name, sizeof(winstation_name) }, /* name */
  52. STANDARD_RIGHTS_REQUIRED | WINSTA_ALL_ACCESS, /* valid_access */
  53. { /* mapping */
  54. STANDARD_RIGHTS_READ | WINSTA_READSCREEN | WINSTA_ENUMERATE | WINSTA_READATTRIBUTES | WINSTA_ENUMDESKTOPS,
  55. STANDARD_RIGHTS_WRITE | WINSTA_WRITEATTRIBUTES | WINSTA_CREATEDESKTOP | WINSTA_ACCESSCLIPBOARD,
  56. STANDARD_RIGHTS_EXECUTE | WINSTA_EXITWINDOWS | WINSTA_ACCESSGLOBALATOMS,
  57. STANDARD_RIGHTS_REQUIRED | WINSTA_ALL_ACCESS
  58. },
  59. };
  60. static const struct object_ops winstation_ops =
  61. {
  62. sizeof(struct winstation), /* size */
  63. &winstation_type, /* type */
  64. winstation_dump, /* dump */
  65. no_add_queue, /* add_queue */
  66. NULL, /* remove_queue */
  67. NULL, /* signaled */
  68. NULL, /* satisfied */
  69. no_signal, /* signal */
  70. no_get_fd, /* get_fd */
  71. default_map_access, /* map_access */
  72. default_get_sd, /* get_sd */
  73. default_set_sd, /* set_sd */
  74. default_get_full_name, /* get_full_name */
  75. winstation_lookup_name, /* lookup_name */
  76. directory_link_name, /* link_name */
  77. default_unlink_name, /* unlink_name */
  78. no_open_file, /* open_file */
  79. no_kernel_obj_list, /* get_kernel_obj_list */
  80. winstation_close_handle, /* close_handle */
  81. winstation_destroy /* destroy */
  82. };
  83. static const WCHAR desktop_name[] = {'D','e','s','k','t','o','p'};
  84. struct type_descr desktop_type =
  85. {
  86. { desktop_name, sizeof(desktop_name) }, /* name */
  87. STANDARD_RIGHTS_REQUIRED | DESKTOP_ALL_ACCESS, /* valid_access */
  88. { /* mapping */
  89. STANDARD_RIGHTS_READ | DESKTOP_ENUMERATE | DESKTOP_READOBJECTS,
  90. STANDARD_RIGHTS_WRITE | DESKTOP_WRITEOBJECTS | DESKTOP_JOURNALPLAYBACK | DESKTOP_JOURNALRECORD
  91. | DESKTOP_HOOKCONTROL | DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW,
  92. STANDARD_RIGHTS_EXECUTE | DESKTOP_SWITCHDESKTOP,
  93. STANDARD_RIGHTS_REQUIRED | DESKTOP_ALL_ACCESS
  94. },
  95. };
  96. static const struct object_ops desktop_ops =
  97. {
  98. sizeof(struct desktop), /* size */
  99. &desktop_type, /* type */
  100. desktop_dump, /* dump */
  101. no_add_queue, /* add_queue */
  102. NULL, /* remove_queue */
  103. NULL, /* signaled */
  104. NULL, /* satisfied */
  105. no_signal, /* signal */
  106. no_get_fd, /* get_fd */
  107. default_map_access, /* map_access */
  108. default_get_sd, /* get_sd */
  109. default_set_sd, /* set_sd */
  110. default_get_full_name, /* get_full_name */
  111. no_lookup_name, /* lookup_name */
  112. desktop_link_name, /* link_name */
  113. default_unlink_name, /* unlink_name */
  114. no_open_file, /* open_file */
  115. no_kernel_obj_list, /* get_kernel_obj_list */
  116. desktop_close_handle, /* close_handle */
  117. desktop_destroy /* destroy */
  118. };
  119. /* create a winstation object */
  120. static struct winstation *create_winstation( struct object *root, const struct unicode_str *name,
  121. unsigned int attr, unsigned int flags )
  122. {
  123. struct winstation *winstation;
  124. if ((winstation = create_named_object( root, &winstation_ops, name, attr, NULL )))
  125. {
  126. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  127. {
  128. /* initialize it if it didn't already exist */
  129. winstation->flags = flags;
  130. winstation->clipboard = NULL;
  131. winstation->atom_table = NULL;
  132. list_add_tail( &winstation_list, &winstation->entry );
  133. list_init( &winstation->desktops );
  134. if (!(winstation->desktop_names = create_namespace( 7 )))
  135. {
  136. release_object( winstation );
  137. return NULL;
  138. }
  139. }
  140. else clear_error();
  141. }
  142. return winstation;
  143. }
  144. static void winstation_dump( struct object *obj, int verbose )
  145. {
  146. struct winstation *winstation = (struct winstation *)obj;
  147. fprintf( stderr, "Winstation flags=%x clipboard=%p atoms=%p\n",
  148. winstation->flags, winstation->clipboard, winstation->atom_table );
  149. }
  150. static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
  151. {
  152. return (process->winstation != handle);
  153. }
  154. static struct object *winstation_lookup_name( struct object *obj, struct unicode_str *name,
  155. unsigned int attr, struct object *root )
  156. {
  157. struct winstation *winstation = (struct winstation *)obj;
  158. struct object *found;
  159. assert( obj->ops == &winstation_ops );
  160. if (!name) return NULL; /* open the winstation itself */
  161. if (get_path_element( name->str, name->len ) < name->len) /* no backslash allowed in name */
  162. {
  163. set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
  164. return NULL;
  165. }
  166. if ((found = find_object( winstation->desktop_names, name, attr )))
  167. name->len = 0;
  168. return found;
  169. }
  170. static void winstation_destroy( struct object *obj )
  171. {
  172. struct winstation *winstation = (struct winstation *)obj;
  173. list_remove( &winstation->entry );
  174. if (winstation->clipboard) release_object( winstation->clipboard );
  175. if (winstation->atom_table) release_object( winstation->atom_table );
  176. free( winstation->desktop_names );
  177. }
  178. /* retrieve the process window station, checking the handle access rights */
  179. struct winstation *get_process_winstation( struct process *process, unsigned int access )
  180. {
  181. return (struct winstation *)get_handle_obj( process, process->winstation,
  182. access, &winstation_ops );
  183. }
  184. /* retrieve a pointer to a desktop object */
  185. struct desktop *get_desktop_obj( struct process *process, obj_handle_t handle, unsigned int access )
  186. {
  187. return (struct desktop *)get_handle_obj( process, handle, access, &desktop_ops );
  188. }
  189. /* create a desktop object */
  190. static struct desktop *create_desktop( const struct unicode_str *name, unsigned int attr,
  191. unsigned int flags, struct winstation *winstation )
  192. {
  193. struct desktop *desktop;
  194. if ((desktop = create_named_object( &winstation->obj, &desktop_ops, name, attr, NULL )))
  195. {
  196. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  197. {
  198. /* initialize it if it didn't already exist */
  199. desktop->flags = flags;
  200. desktop->winstation = (struct winstation *)grab_object( winstation );
  201. desktop->top_window = NULL;
  202. desktop->msg_window = NULL;
  203. desktop->global_hooks = NULL;
  204. desktop->close_timeout = NULL;
  205. desktop->foreground_input = NULL;
  206. desktop->users = 0;
  207. memset( &desktop->cursor, 0, sizeof(desktop->cursor) );
  208. memset( desktop->keystate, 0, sizeof(desktop->keystate) );
  209. list_add_tail( &winstation->desktops, &desktop->entry );
  210. list_init( &desktop->hotkeys );
  211. }
  212. else clear_error();
  213. }
  214. return desktop;
  215. }
  216. static void desktop_dump( struct object *obj, int verbose )
  217. {
  218. struct desktop *desktop = (struct desktop *)obj;
  219. fprintf( stderr, "Desktop flags=%x winstation=%p top_win=%p hooks=%p\n",
  220. desktop->flags, desktop->winstation, desktop->top_window, desktop->global_hooks );
  221. }
  222. static int desktop_link_name( struct object *obj, struct object_name *name, struct object *parent )
  223. {
  224. struct winstation *winstation = (struct winstation *)parent;
  225. if (parent->ops != &winstation_ops)
  226. {
  227. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  228. return 0;
  229. }
  230. if (get_path_element( name->name, name->len ) < name->len) /* no backslash allowed in name */
  231. {
  232. set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
  233. return 0;
  234. }
  235. namespace_add( winstation->desktop_names, name );
  236. return 1;
  237. }
  238. static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
  239. {
  240. struct thread *thread;
  241. /* check if the handle is currently used by the process or one of its threads */
  242. if (process->desktop == handle) return 0;
  243. LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
  244. if (thread->desktop == handle) return 0;
  245. return 1;
  246. }
  247. static void desktop_destroy( struct object *obj )
  248. {
  249. struct desktop *desktop = (struct desktop *)obj;
  250. free_hotkeys( desktop, 0 );
  251. if (desktop->top_window) destroy_window( desktop->top_window );
  252. if (desktop->msg_window) destroy_window( desktop->msg_window );
  253. if (desktop->global_hooks) release_object( desktop->global_hooks );
  254. if (desktop->close_timeout) remove_timeout_user( desktop->close_timeout );
  255. list_remove( &desktop->entry );
  256. release_object( desktop->winstation );
  257. }
  258. /* retrieve the thread desktop, checking the handle access rights */
  259. struct desktop *get_thread_desktop( struct thread *thread, unsigned int access )
  260. {
  261. return get_desktop_obj( thread->process, thread->desktop, access );
  262. }
  263. static void close_desktop_timeout( void *private )
  264. {
  265. struct desktop *desktop = private;
  266. desktop->close_timeout = NULL;
  267. unlink_named_object( &desktop->obj ); /* make sure no other process can open it */
  268. post_desktop_message( desktop, WM_CLOSE, 0, 0 ); /* and signal the owner to quit */
  269. }
  270. /* add a user of the desktop and cancel the close timeout */
  271. static void add_desktop_user( struct desktop *desktop )
  272. {
  273. desktop->users++;
  274. if (desktop->close_timeout)
  275. {
  276. remove_timeout_user( desktop->close_timeout );
  277. desktop->close_timeout = NULL;
  278. }
  279. }
  280. /* remove a user of the desktop and start the close timeout if necessary */
  281. static void remove_desktop_user( struct desktop *desktop )
  282. {
  283. struct process *process;
  284. assert( desktop->users > 0 );
  285. desktop->users--;
  286. /* if we have one remaining user, it has to be the manager of the desktop window */
  287. if ((process = get_top_window_owner( desktop )) && desktop->users == process->running_threads && !desktop->close_timeout)
  288. desktop->close_timeout = add_timeout_user( -TICKS_PER_SEC, close_desktop_timeout, desktop );
  289. }
  290. /* set the thread default desktop handle */
  291. void set_thread_default_desktop( struct thread *thread, struct desktop *desktop, obj_handle_t handle )
  292. {
  293. if (thread->desktop) return; /* nothing to do */
  294. thread->desktop = handle;
  295. if (!thread->process->is_system) add_desktop_user( desktop );
  296. }
  297. /* set the process default desktop handle */
  298. void set_process_default_desktop( struct process *process, struct desktop *desktop,
  299. obj_handle_t handle )
  300. {
  301. struct thread *thread;
  302. if (process->desktop == handle) return; /* nothing to do */
  303. process->desktop = handle;
  304. /* set desktop for threads that don't have one yet */
  305. LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
  306. set_thread_default_desktop( thread, desktop, handle );
  307. }
  308. /* connect a process to its window station */
  309. void connect_process_winstation( struct process *process, struct thread *parent_thread,
  310. struct process *parent_process )
  311. {
  312. struct winstation *winstation = NULL;
  313. struct desktop *desktop = NULL;
  314. obj_handle_t handle;
  315. /* check for an inherited winstation handle (don't ask...) */
  316. if ((handle = find_inherited_handle( process, &winstation_ops )))
  317. {
  318. winstation = (struct winstation *)get_handle_obj( process, handle, 0, &winstation_ops );
  319. }
  320. else if (parent_process->winstation)
  321. {
  322. handle = duplicate_handle( parent_process, parent_process->winstation,
  323. process, 0, 0, DUPLICATE_SAME_ACCESS );
  324. winstation = (struct winstation *)get_handle_obj( process, handle, 0, &winstation_ops );
  325. }
  326. if (!winstation) goto done;
  327. process->winstation = handle;
  328. if ((handle = find_inherited_handle( process, &desktop_ops )))
  329. {
  330. desktop = get_desktop_obj( process, handle, 0 );
  331. if (!desktop || desktop->winstation != winstation) goto done;
  332. }
  333. else
  334. {
  335. if (parent_thread && parent_thread->desktop)
  336. handle = parent_thread->desktop;
  337. else if (parent_process->desktop)
  338. handle = parent_process->desktop;
  339. else
  340. goto done;
  341. desktop = get_desktop_obj( parent_process, handle, 0 );
  342. if (!desktop || desktop->winstation != winstation) goto done;
  343. handle = duplicate_handle( parent_process, handle, process, 0, 0, DUPLICATE_SAME_ACCESS );
  344. }
  345. if (handle) set_process_default_desktop( process, desktop, handle );
  346. done:
  347. if (desktop) release_object( desktop );
  348. if (winstation) release_object( winstation );
  349. clear_error();
  350. }
  351. /* close the desktop of a given process */
  352. void close_process_desktop( struct process *process )
  353. {
  354. obj_handle_t handle;
  355. if (!(handle = process->desktop)) return;
  356. process->desktop = 0;
  357. close_handle( process, handle );
  358. }
  359. /* release (and eventually close) the desktop of a given thread */
  360. void release_thread_desktop( struct thread *thread, int close )
  361. {
  362. struct desktop *desktop;
  363. obj_handle_t handle;
  364. if (!(handle = thread->desktop)) return;
  365. if (!thread->process->is_system)
  366. {
  367. if (!(desktop = get_desktop_obj( thread->process, handle, 0 ))) clear_error(); /* ignore errors */
  368. else
  369. {
  370. remove_desktop_user( desktop );
  371. release_object( desktop );
  372. }
  373. }
  374. if (close)
  375. {
  376. thread->desktop = 0;
  377. close_handle( thread->process, handle );
  378. }
  379. }
  380. /* create a window station */
  381. DECL_HANDLER(create_winstation)
  382. {
  383. struct winstation *winstation;
  384. struct unicode_str name = get_req_unicode_str();
  385. struct object *root = NULL;
  386. reply->handle = 0;
  387. if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir ))) return;
  388. if ((winstation = create_winstation( root, &name, req->attributes, req->flags )))
  389. {
  390. reply->handle = alloc_handle( current->process, winstation, req->access, req->attributes );
  391. release_object( winstation );
  392. }
  393. if (root) release_object( root );
  394. }
  395. /* open a handle to a window station */
  396. DECL_HANDLER(open_winstation)
  397. {
  398. struct unicode_str name = get_req_unicode_str();
  399. reply->handle = open_object( current->process, req->rootdir, req->access,
  400. &winstation_ops, &name, req->attributes );
  401. }
  402. /* close a window station */
  403. DECL_HANDLER(close_winstation)
  404. {
  405. struct winstation *winstation;
  406. if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
  407. 0, &winstation_ops )))
  408. {
  409. if (close_handle( current->process, req->handle )) set_error( STATUS_ACCESS_DENIED );
  410. release_object( winstation );
  411. }
  412. }
  413. /* get the process current window station */
  414. DECL_HANDLER(get_process_winstation)
  415. {
  416. reply->handle = current->process->winstation;
  417. }
  418. /* set the process current window station */
  419. DECL_HANDLER(set_process_winstation)
  420. {
  421. struct winstation *winstation;
  422. if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
  423. 0, &winstation_ops )))
  424. {
  425. /* FIXME: should we close the old one? */
  426. current->process->winstation = req->handle;
  427. release_object( winstation );
  428. }
  429. }
  430. /* create a desktop */
  431. DECL_HANDLER(create_desktop)
  432. {
  433. struct desktop *desktop;
  434. struct winstation *winstation;
  435. struct unicode_str name = get_req_unicode_str();
  436. reply->handle = 0;
  437. if (!name.len)
  438. {
  439. set_error( STATUS_INVALID_HANDLE );
  440. return;
  441. }
  442. if ((winstation = get_process_winstation( current->process, WINSTA_CREATEDESKTOP )))
  443. {
  444. if ((desktop = create_desktop( &name, req->attributes, req->flags, winstation )))
  445. {
  446. reply->handle = alloc_handle( current->process, desktop, req->access, req->attributes );
  447. release_object( desktop );
  448. }
  449. release_object( winstation );
  450. }
  451. }
  452. /* open a handle to a desktop */
  453. DECL_HANDLER(open_desktop)
  454. {
  455. struct winstation *winstation;
  456. struct object *obj;
  457. struct unicode_str name = get_req_unicode_str();
  458. /* FIXME: check access rights */
  459. if (!req->winsta)
  460. winstation = get_process_winstation( current->process, 0 );
  461. else
  462. winstation = (struct winstation *)get_handle_obj( current->process, req->winsta, 0, &winstation_ops );
  463. if (!winstation) return;
  464. if ((obj = open_named_object( &winstation->obj, &desktop_ops, &name, req->attributes )))
  465. {
  466. reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
  467. release_object( obj );
  468. }
  469. release_object( winstation );
  470. }
  471. /* open a handle to current input desktop */
  472. DECL_HANDLER(open_input_desktop)
  473. {
  474. /* FIXME: check access rights */
  475. struct winstation *winstation = get_process_winstation( current->process, 0 );
  476. struct desktop *desktop;
  477. if (!winstation) return;
  478. if (!(winstation->flags & WSF_VISIBLE))
  479. {
  480. set_error( STATUS_ILLEGAL_FUNCTION );
  481. release_object( winstation );
  482. return;
  483. }
  484. if ((desktop = get_desktop_obj( current->process, current->process->desktop, 0 )))
  485. {
  486. reply->handle = alloc_handle( current->process, desktop, req->access, req->attributes );
  487. release_object( desktop );
  488. }
  489. release_object( winstation );
  490. }
  491. /* close a desktop */
  492. DECL_HANDLER(close_desktop)
  493. {
  494. struct desktop *desktop;
  495. /* make sure it is a desktop handle */
  496. if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
  497. 0, &desktop_ops )))
  498. {
  499. if (close_handle( current->process, req->handle )) set_error( STATUS_DEVICE_BUSY );
  500. release_object( desktop );
  501. }
  502. }
  503. /* get the thread current desktop */
  504. DECL_HANDLER(get_thread_desktop)
  505. {
  506. struct thread *thread;
  507. if (!(thread = get_thread_from_id( req->tid ))) return;
  508. reply->handle = thread->desktop;
  509. release_object( thread );
  510. }
  511. /* set the thread current desktop */
  512. DECL_HANDLER(set_thread_desktop)
  513. {
  514. struct desktop *old_desktop, *new_desktop;
  515. struct winstation *winstation;
  516. if (!(winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
  517. return;
  518. if (!(new_desktop = get_desktop_obj( current->process, req->handle, 0 )))
  519. {
  520. release_object( winstation );
  521. return;
  522. }
  523. if (new_desktop->winstation != winstation)
  524. {
  525. set_error( STATUS_ACCESS_DENIED );
  526. release_object( new_desktop );
  527. release_object( winstation );
  528. return;
  529. }
  530. /* check if we are changing to a new desktop */
  531. if (!(old_desktop = get_desktop_obj( current->process, current->desktop, 0)))
  532. clear_error(); /* ignore error */
  533. /* when changing desktop, we can't have any users on the current one */
  534. if (old_desktop != new_desktop && current->desktop_users > 0)
  535. set_error( STATUS_DEVICE_BUSY );
  536. else
  537. {
  538. current->desktop = req->handle; /* FIXME: should we close the old one? */
  539. if (!current->process->is_system && old_desktop != new_desktop)
  540. {
  541. add_desktop_user( new_desktop );
  542. if (old_desktop) remove_desktop_user( old_desktop );
  543. }
  544. }
  545. if (!current->process->desktop)
  546. set_process_default_desktop( current->process, new_desktop, req->handle );
  547. if (old_desktop != new_desktop && current->queue) detach_thread_input( current );
  548. if (old_desktop) release_object( old_desktop );
  549. release_object( new_desktop );
  550. release_object( winstation );
  551. }
  552. /* get/set information about a user object (window station or desktop) */
  553. DECL_HANDLER(set_user_object_info)
  554. {
  555. struct object *obj;
  556. data_size_t len;
  557. if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
  558. if (obj->ops == &desktop_ops)
  559. {
  560. struct desktop *desktop = (struct desktop *)obj;
  561. reply->is_desktop = 1;
  562. reply->old_obj_flags = desktop->flags;
  563. if (req->flags & SET_USER_OBJECT_SET_FLAGS) desktop->flags = req->obj_flags;
  564. }
  565. else if (obj->ops == &winstation_ops)
  566. {
  567. struct winstation *winstation = (struct winstation *)obj;
  568. reply->is_desktop = 0;
  569. reply->old_obj_flags = winstation->flags;
  570. if (req->flags & SET_USER_OBJECT_SET_FLAGS) winstation->flags = req->obj_flags;
  571. }
  572. else
  573. {
  574. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  575. release_object( obj );
  576. return;
  577. }
  578. if (obj->ops == &desktop_ops && get_reply_max_size() && (req->flags & SET_USER_OBJECT_GET_FULL_NAME))
  579. {
  580. struct desktop *desktop = (struct desktop *)obj;
  581. data_size_t winstation_len, desktop_len;
  582. const WCHAR *winstation_name = get_object_name( &desktop->winstation->obj, &winstation_len );
  583. const WCHAR *desktop_name = get_object_name( obj, &desktop_len );
  584. WCHAR *full_name;
  585. if (!winstation_name) winstation_len = 0;
  586. if (!desktop_name) desktop_len = 0;
  587. len = winstation_len + desktop_len + sizeof(WCHAR);
  588. if ((full_name = mem_alloc( len )))
  589. {
  590. memcpy( full_name, winstation_name, winstation_len );
  591. full_name[winstation_len / sizeof(WCHAR)] = '\\';
  592. memcpy( full_name + winstation_len / sizeof(WCHAR) + 1, desktop_name, desktop_len );
  593. set_reply_data_ptr( full_name, min( len, get_reply_max_size() ));
  594. }
  595. }
  596. else
  597. {
  598. const WCHAR *name = get_object_name( obj, &len );
  599. if (name) set_reply_data( name, min( len, get_reply_max_size() ));
  600. }
  601. release_object( obj );
  602. }
  603. /* enumerate window stations */
  604. DECL_HANDLER(enum_winstation)
  605. {
  606. unsigned int index = 0;
  607. struct winstation *winsta;
  608. const WCHAR *name;
  609. data_size_t len;
  610. LIST_FOR_EACH_ENTRY( winsta, &winstation_list, struct winstation, entry )
  611. {
  612. unsigned int access = WINSTA_ENUMERATE;
  613. if (req->index > index++) continue;
  614. if (!check_object_access( NULL, &winsta->obj, &access )) continue;
  615. clear_error();
  616. reply->next = index;
  617. if ((name = get_object_name( &winsta->obj, &len )))
  618. set_reply_data( name, min( len, get_reply_max_size() ));
  619. return;
  620. }
  621. set_error( STATUS_NO_MORE_ENTRIES );
  622. }
  623. /* enumerate desktops */
  624. DECL_HANDLER(enum_desktop)
  625. {
  626. struct winstation *winstation;
  627. struct desktop *desktop;
  628. unsigned int index = 0;
  629. const WCHAR *name;
  630. data_size_t len;
  631. if (!(winstation = (struct winstation *)get_handle_obj( current->process, req->winstation,
  632. WINSTA_ENUMDESKTOPS, &winstation_ops )))
  633. return;
  634. LIST_FOR_EACH_ENTRY( desktop, &winstation->desktops, struct desktop, entry )
  635. {
  636. unsigned int access = DESKTOP_ENUMERATE;
  637. if (req->index > index++) continue;
  638. if (!desktop->obj.name) continue;
  639. if (!check_object_access( NULL, &desktop->obj, &access )) continue;
  640. if ((name = get_object_name( &desktop->obj, &len )))
  641. set_reply_data( name, min( len, get_reply_max_size() ));
  642. release_object( winstation );
  643. clear_error();
  644. reply->next = index;
  645. return;
  646. }
  647. release_object( winstation );
  648. set_error( STATUS_NO_MORE_ENTRIES );
  649. }