console.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. /*
  2. * Server-side console management
  3. *
  4. * Copyright (C) 1998 Alexandre Julliard
  5. * 2001 Eric Pouech
  6. *
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include "config.h"
  23. #include "wine/port.h"
  24. #include <assert.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include <signal.h>
  29. #include "handle.h"
  30. #include "process.h"
  31. #include "request.h"
  32. #include "unicode.h"
  33. #include "console.h"
  34. static void console_input_dump( struct object *obj, int verbose );
  35. static void console_input_destroy( struct object *obj );
  36. static const struct object_ops console_input_ops =
  37. {
  38. sizeof(struct console_input), /* size */
  39. console_input_dump, /* dump */
  40. no_add_queue, /* add_queue */
  41. NULL, /* remove_queue */
  42. NULL, /* signaled */
  43. no_satisfied, /* satisfied */
  44. no_get_fd, /* get_fd */
  45. console_input_destroy /* destroy */
  46. };
  47. static void console_input_events_dump( struct object *obj, int verbose );
  48. static void console_input_events_destroy( struct object *obj );
  49. static int console_input_events_signaled( struct object *obj, struct thread *thread );
  50. struct console_input_events
  51. {
  52. struct object obj; /* object header */
  53. int num_alloc; /* number of allocated events */
  54. int num_used; /* number of actually used events */
  55. struct console_renderer_event* events;
  56. };
  57. static const struct object_ops console_input_events_ops =
  58. {
  59. sizeof(struct console_input_events), /* size */
  60. console_input_events_dump, /* dump */
  61. add_queue, /* add_queue */
  62. remove_queue, /* remove_queue */
  63. console_input_events_signaled, /* signaled */
  64. no_satisfied, /* satisfied */
  65. no_get_fd, /* get_fd */
  66. console_input_events_destroy /* destroy */
  67. };
  68. struct screen_buffer
  69. {
  70. struct object obj; /* object header */
  71. struct screen_buffer *next; /* linked list of all screen buffers */
  72. struct screen_buffer *prev;
  73. struct console_input *input; /* associated console input */
  74. int mode; /* output mode */
  75. int cursor_size; /* size of cursor (percentage filled) */
  76. int cursor_visible;/* cursor visibility flag */
  77. int cursor_x; /* position of cursor */
  78. int cursor_y; /* position of cursor */
  79. int width; /* size (w-h) of the screen buffer */
  80. int height;
  81. int max_width; /* size (w-h) of the window given font size */
  82. int max_height;
  83. char_info_t *data; /* the data for each cell - a width x height matrix */
  84. unsigned short attr; /* default attribute for screen buffer */
  85. rectangle_t win; /* current visible window on the screen buffer *
  86. * as seen in wineconsole */
  87. };
  88. static void screen_buffer_dump( struct object *obj, int verbose );
  89. static void screen_buffer_destroy( struct object *obj );
  90. static const struct object_ops screen_buffer_ops =
  91. {
  92. sizeof(struct screen_buffer), /* size */
  93. screen_buffer_dump, /* dump */
  94. no_add_queue, /* add_queue */
  95. NULL, /* remove_queue */
  96. NULL, /* signaled */
  97. NULL, /* satisfied */
  98. no_get_fd, /* get_fd */
  99. screen_buffer_destroy /* destroy */
  100. };
  101. static struct screen_buffer *screen_buffer_list;
  102. static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
  103. /* dumps the renderer events of a console */
  104. static void console_input_events_dump( struct object *obj, int verbose )
  105. {
  106. struct console_input_events *evts = (struct console_input_events *)obj;
  107. assert( obj->ops == &console_input_events_ops );
  108. fprintf( stderr, "Console input events: %d/%d events\n",
  109. evts->num_used, evts->num_alloc );
  110. }
  111. /* destroys the renderer events of a console */
  112. static void console_input_events_destroy( struct object *obj )
  113. {
  114. struct console_input_events *evts = (struct console_input_events *)obj;
  115. assert( obj->ops == &console_input_events_ops );
  116. free( evts->events );
  117. }
  118. /* the renderer events list is signaled when it's not empty */
  119. static int console_input_events_signaled( struct object *obj, struct thread *thread )
  120. {
  121. struct console_input_events *evts = (struct console_input_events *)obj;
  122. assert( obj->ops == &console_input_events_ops );
  123. return (evts->num_used != 0);
  124. }
  125. /* add an event to the console's renderer events list */
  126. static void console_input_events_append( struct console_input_events* evts,
  127. struct console_renderer_event* evt)
  128. {
  129. int collapsed = FALSE;
  130. /* to be done even when evt has been generated by the rendere ? */
  131. /* try to collapse evt into current queue's events */
  132. if (evts->num_used)
  133. {
  134. struct console_renderer_event* last = &evts->events[evts->num_used - 1];
  135. if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
  136. evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
  137. {
  138. /* if two update events overlap, collapse them into a single one */
  139. if (last->u.update.bottom + 1 >= evt->u.update.top &&
  140. evt->u.update.bottom + 1 >= last->u.update.top)
  141. {
  142. last->u.update.top = min(last->u.update.top, evt->u.update.top);
  143. last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
  144. collapsed = TRUE;
  145. }
  146. }
  147. }
  148. if (!collapsed)
  149. {
  150. if (evts->num_used == evts->num_alloc)
  151. {
  152. evts->num_alloc += 16;
  153. evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
  154. assert(evts->events);
  155. }
  156. evts->events[evts->num_used++] = *evt;
  157. }
  158. wake_up( &evts->obj, 0 );
  159. }
  160. /* retrieves events from the console's renderer events list */
  161. static void console_input_events_get( struct console_input_events* evts )
  162. {
  163. size_t num = get_reply_max_size() / sizeof(evts->events[0]);
  164. if (num > evts->num_used) num = evts->num_used;
  165. set_reply_data( evts->events, num * sizeof(evts->events[0]) );
  166. if (num < evts->num_used)
  167. {
  168. memmove( &evts->events[0], &evts->events[num],
  169. (evts->num_used - num) * sizeof(evts->events[0]) );
  170. }
  171. evts->num_used -= num;
  172. }
  173. static struct console_input_events *create_console_input_events(void)
  174. {
  175. struct console_input_events* evt;
  176. if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
  177. evt->num_alloc = evt->num_used = 0;
  178. evt->events = NULL;
  179. return evt;
  180. }
  181. static struct object *create_console_input( struct thread* renderer )
  182. {
  183. struct console_input *console_input;
  184. if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
  185. console_input->renderer = renderer;
  186. console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
  187. ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
  188. console_input->num_proc = 0;
  189. console_input->active = NULL;
  190. console_input->recnum = 0;
  191. console_input->records = NULL;
  192. console_input->evt = create_console_input_events();
  193. console_input->title = NULL;
  194. console_input->history_size = 50;
  195. console_input->history = calloc( console_input->history_size, sizeof(WCHAR*) );
  196. console_input->history_index = 0;
  197. console_input->history_mode = 0;
  198. console_input->edition_mode = 0;
  199. console_input->event = create_event( NULL, 0, 1, 0 );
  200. if (!console_input->history || !console_input->evt)
  201. {
  202. release_object( console_input );
  203. return NULL;
  204. }
  205. return &console_input->obj;
  206. }
  207. static struct screen_buffer *create_console_output( struct console_input *console_input )
  208. {
  209. struct screen_buffer *screen_buffer;
  210. struct console_renderer_event evt;
  211. int i;
  212. if (!(screen_buffer = alloc_object( &screen_buffer_ops ))) return NULL;
  213. screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
  214. screen_buffer->input = console_input;
  215. screen_buffer->cursor_size = 100;
  216. screen_buffer->cursor_visible = 1;
  217. screen_buffer->width = 80;
  218. screen_buffer->height = 150;
  219. screen_buffer->max_width = 80;
  220. screen_buffer->max_height = 25;
  221. screen_buffer->cursor_x = 0;
  222. screen_buffer->cursor_y = 0;
  223. screen_buffer->attr = 0x0F;
  224. screen_buffer->win.left = 0;
  225. screen_buffer->win.right = screen_buffer->max_width - 1;
  226. screen_buffer->win.top = 0;
  227. screen_buffer->win.bottom = screen_buffer->max_height - 1;
  228. if ((screen_buffer->next = screen_buffer_list)) screen_buffer->next->prev = screen_buffer;
  229. screen_buffer->prev = NULL;
  230. screen_buffer_list = screen_buffer;
  231. if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
  232. sizeof(*screen_buffer->data) )))
  233. {
  234. release_object( screen_buffer );
  235. return NULL;
  236. }
  237. /* clear the first row */
  238. for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
  239. /* and copy it to all other rows */
  240. for (i = 1; i < screen_buffer->height; i++)
  241. memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
  242. screen_buffer->width * sizeof(char_info_t) );
  243. if (!console_input->active)
  244. {
  245. console_input->active = (struct screen_buffer*)grab_object( screen_buffer );
  246. /* generate the initial events */
  247. evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
  248. console_input_events_append( console_input->evt, &evt );
  249. evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
  250. evt.u.resize.width = screen_buffer->width;
  251. evt.u.resize.height = screen_buffer->height;
  252. console_input_events_append( console_input->evt, &evt );
  253. evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
  254. evt.u.display.left = screen_buffer->win.left;
  255. evt.u.display.top = screen_buffer->win.top;
  256. evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
  257. evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
  258. console_input_events_append( console_input->evt, &evt );
  259. evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
  260. evt.u.update.top = 0;
  261. evt.u.update.bottom = screen_buffer->height - 1;
  262. console_input_events_append( console_input->evt, &evt );
  263. evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
  264. evt.u.cursor_geom.size = screen_buffer->cursor_size;
  265. evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
  266. console_input_events_append( console_input->evt, &evt );
  267. evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
  268. evt.u.cursor_pos.x = screen_buffer->cursor_x;
  269. evt.u.cursor_pos.y = screen_buffer->cursor_y;
  270. console_input_events_append( console_input->evt, &evt );
  271. }
  272. return screen_buffer;
  273. }
  274. /* free the console for this process */
  275. int free_console( struct process *process )
  276. {
  277. struct console_input* console = process->console;
  278. if (!console || !console->renderer) return 0;
  279. process->console = NULL;
  280. if (--console->num_proc == 0)
  281. {
  282. /* all processes have terminated... tell the renderer to terminate too */
  283. struct console_renderer_event evt;
  284. evt.event = CONSOLE_RENDERER_EXIT_EVENT;
  285. console_input_events_append( console->evt, &evt );
  286. }
  287. release_object( console );
  288. return 1;
  289. }
  290. /* let process inherit the console from parent... this handle two cases :
  291. * 1/ generic console inheritance
  292. * 2/ parent is a renderer which launches process, and process should attach to the console
  293. * renderered by parent
  294. */
  295. void inherit_console(struct thread *parent_thread, struct process *process, obj_handle_t hconin)
  296. {
  297. int done = 0;
  298. struct process* parent = parent_thread->process;
  299. /* if parent is a renderer, then attach current process to its console
  300. * a bit hacky....
  301. */
  302. if (hconin)
  303. {
  304. struct console_input* console;
  305. /* FIXME: should we check some access rights ? */
  306. if ((console = (struct console_input*)get_handle_obj( parent, hconin,
  307. 0, &console_input_ops )))
  308. {
  309. if (console->renderer == parent_thread)
  310. {
  311. process->console = (struct console_input*)grab_object( console );
  312. process->console->num_proc++;
  313. done = 1;
  314. }
  315. release_object( console );
  316. }
  317. }
  318. /* otherwise, if parent has a console, attach child to this console */
  319. if (!done && parent->console)
  320. {
  321. assert(parent->console->renderer);
  322. process->console = (struct console_input*)grab_object( parent->console );
  323. process->console->num_proc++;
  324. }
  325. }
  326. static struct console_input* console_input_get( obj_handle_t handle, unsigned access )
  327. {
  328. struct console_input* console = 0;
  329. if (handle)
  330. console = (struct console_input *)get_handle_obj( current->process, handle,
  331. access, &console_input_ops );
  332. else if (current->process->console)
  333. {
  334. assert( current->process->console->renderer );
  335. console = (struct console_input *)grab_object( current->process->console );
  336. }
  337. if (!console && !get_error()) set_error(STATUS_INVALID_PARAMETER);
  338. return console;
  339. }
  340. struct console_signal_info
  341. {
  342. struct console_input *console;
  343. process_id_t group;
  344. int signal;
  345. };
  346. static int propagate_console_signal_cb(struct process *process, void *user)
  347. {
  348. struct console_signal_info* csi = (struct console_signal_info*)user;
  349. if (process->console == csi->console && process->running_threads &&
  350. (!csi->group || process->group_id == csi->group))
  351. {
  352. /* find a suitable thread to signal */
  353. struct thread *thread;
  354. for (thread = process->thread_list; thread; thread = thread->proc_next)
  355. {
  356. if (send_thread_signal( thread, csi->signal )) break;
  357. }
  358. }
  359. return FALSE;
  360. }
  361. static void propagate_console_signal( struct console_input *console,
  362. int sig, process_id_t group_id )
  363. {
  364. struct console_signal_info csi;
  365. if (!console)
  366. {
  367. set_error( STATUS_INVALID_PARAMETER );
  368. return;
  369. }
  370. /* FIXME: should support the other events (like CTRL_BREAK) */
  371. if (sig != CTRL_C_EVENT)
  372. {
  373. set_error( STATUS_NOT_IMPLEMENTED );
  374. return;
  375. }
  376. csi.console = console;
  377. csi.signal = SIGINT;
  378. csi.group = group_id;
  379. enum_processes(propagate_console_signal_cb, &csi);
  380. }
  381. static int get_console_mode( obj_handle_t handle )
  382. {
  383. struct object *obj;
  384. int ret = 0;
  385. if ((obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL )))
  386. {
  387. if (obj->ops == &console_input_ops)
  388. ret = ((struct console_input *)obj)->mode;
  389. else if (obj->ops == &screen_buffer_ops)
  390. ret = ((struct screen_buffer *)obj)->mode;
  391. else
  392. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  393. release_object( obj );
  394. }
  395. return ret;
  396. }
  397. /* changes the mode of either a console input or a screen buffer */
  398. static int set_console_mode( obj_handle_t handle, int mode )
  399. {
  400. struct object *obj;
  401. int ret = 0;
  402. if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL )))
  403. return 0;
  404. if (obj->ops == &console_input_ops)
  405. {
  406. /* FIXME: if we remove the edit mode bits, we need (???) to clean up the history */
  407. ((struct console_input *)obj)->mode = mode;
  408. ret = 1;
  409. }
  410. else if (obj->ops == &screen_buffer_ops)
  411. {
  412. ((struct screen_buffer *)obj)->mode = mode;
  413. ret = 1;
  414. }
  415. else set_error( STATUS_OBJECT_TYPE_MISMATCH );
  416. release_object( obj );
  417. return ret;
  418. }
  419. /* add input events to a console input queue */
  420. static int write_console_input( struct console_input* console, int count,
  421. const INPUT_RECORD *records )
  422. {
  423. INPUT_RECORD *new_rec;
  424. if (!count) return 0;
  425. if (!(new_rec = realloc( console->records,
  426. (console->recnum + count) * sizeof(INPUT_RECORD) )))
  427. {
  428. set_error( STATUS_NO_MEMORY );
  429. release_object( console );
  430. return -1;
  431. }
  432. console->records = new_rec;
  433. memcpy( new_rec + console->recnum, records, count * sizeof(INPUT_RECORD) );
  434. if (console->mode & ENABLE_PROCESSED_INPUT)
  435. {
  436. int i = 0;
  437. while (i < count)
  438. {
  439. if (records[i].EventType == KEY_EVENT &&
  440. records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
  441. !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
  442. {
  443. if (i != count - 1)
  444. memcpy( &console->records[console->recnum + i],
  445. &console->records[console->recnum + i + 1],
  446. (count - i - 1) * sizeof(INPUT_RECORD) );
  447. count--;
  448. if (records[i].Event.KeyEvent.bKeyDown)
  449. {
  450. /* send SIGINT to all processes attached to this console */
  451. propagate_console_signal( console, CTRL_C_EVENT, 0 );
  452. }
  453. }
  454. else i++;
  455. }
  456. }
  457. if (!console->recnum && count) set_event( console->event );
  458. console->recnum += count;
  459. return count;
  460. }
  461. /* retrieve a pointer to the console input records */
  462. static int read_console_input( obj_handle_t handle, int count, int flush )
  463. {
  464. struct console_input *console;
  465. if (!(console = (struct console_input *)get_handle_obj( current->process, handle,
  466. GENERIC_READ, &console_input_ops )))
  467. return -1;
  468. if (!count)
  469. {
  470. /* special case: do not retrieve anything, but return
  471. * the total number of records available */
  472. count = console->recnum;
  473. }
  474. else
  475. {
  476. if (count > console->recnum) count = console->recnum;
  477. set_reply_data( console->records, count * sizeof(INPUT_RECORD) );
  478. }
  479. if (flush)
  480. {
  481. int i;
  482. for (i = count; i < console->recnum; i++)
  483. console->records[i-count] = console->records[i];
  484. if ((console->recnum -= count) > 0)
  485. {
  486. INPUT_RECORD *new_rec = realloc( console->records,
  487. console->recnum * sizeof(INPUT_RECORD) );
  488. if (new_rec) console->records = new_rec;
  489. }
  490. else
  491. {
  492. free( console->records );
  493. console->records = NULL;
  494. reset_event( console->event );
  495. }
  496. }
  497. release_object( console );
  498. return count;
  499. }
  500. /* set misc console input information */
  501. static int set_console_input_info( const struct set_console_input_info_request *req,
  502. const WCHAR *title, size_t len )
  503. {
  504. struct console_input *console;
  505. struct console_renderer_event evt;
  506. if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) goto error;
  507. if (req->mask & SET_CONSOLE_INPUT_INFO_ACTIVE_SB)
  508. {
  509. struct screen_buffer *screen_buffer;
  510. screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->active_sb,
  511. GENERIC_READ, &screen_buffer_ops );
  512. if (!screen_buffer || screen_buffer->input != console)
  513. {
  514. set_error( STATUS_INVALID_PARAMETER );
  515. if (screen_buffer) release_object( screen_buffer );
  516. goto error;
  517. }
  518. if (screen_buffer != console->active)
  519. {
  520. if (console->active) release_object( console->active );
  521. console->active = screen_buffer;
  522. evt.event = CONSOLE_RENDERER_ACTIVE_SB_EVENT;
  523. console_input_events_append( console->evt, &evt );
  524. }
  525. else
  526. release_object( screen_buffer );
  527. }
  528. if (req->mask & SET_CONSOLE_INPUT_INFO_TITLE)
  529. {
  530. WCHAR *new_title = mem_alloc( len + sizeof(WCHAR) );
  531. if (new_title)
  532. {
  533. memcpy( new_title, title, len );
  534. new_title[len / sizeof(WCHAR)] = 0;
  535. if (console->title) free( console->title );
  536. console->title = new_title;
  537. evt.event = CONSOLE_RENDERER_TITLE_EVENT;
  538. console_input_events_append( console->evt, &evt );
  539. }
  540. }
  541. if (req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_MODE)
  542. {
  543. console->history_mode = req->history_mode;
  544. }
  545. if ((req->mask & SET_CONSOLE_INPUT_INFO_HISTORY_SIZE) &&
  546. console->history_size != req->history_size)
  547. {
  548. WCHAR** mem = NULL;
  549. int i;
  550. int delta;
  551. if (req->history_size)
  552. {
  553. mem = mem_alloc( req->history_size * sizeof(WCHAR*) );
  554. if (!mem) goto error;
  555. memset( mem, 0, req->history_size * sizeof(WCHAR*) );
  556. }
  557. delta = (console->history_index > req->history_size) ?
  558. (console->history_index - req->history_size) : 0;
  559. for (i = delta; i < console->history_index; i++)
  560. {
  561. mem[i - delta] = console->history[i];
  562. console->history[i] = NULL;
  563. }
  564. console->history_index -= delta;
  565. for (i = 0; i < console->history_size; i++)
  566. if (console->history[i]) free( console->history[i] );
  567. free( console->history );
  568. console->history = mem;
  569. console->history_size = req->history_size;
  570. }
  571. if (req->mask & SET_CONSOLE_INPUT_INFO_EDITION_MODE)
  572. {
  573. console->edition_mode = req->edition_mode;
  574. }
  575. release_object( console );
  576. return 1;
  577. error:
  578. if (console) release_object( console );
  579. return 0;
  580. }
  581. /* resize a screen buffer */
  582. static int change_screen_buffer_size( struct screen_buffer *screen_buffer,
  583. int new_width, int new_height )
  584. {
  585. int i, old_width, old_height, copy_width, copy_height;
  586. char_info_t *new_data;
  587. if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) )))
  588. {
  589. set_error( STATUS_NO_MEMORY );
  590. return 0;
  591. }
  592. old_width = screen_buffer->width;
  593. old_height = screen_buffer->height;
  594. copy_width = min( old_width, new_width );
  595. copy_height = min( old_height, new_height );
  596. /* copy all the rows */
  597. for (i = 0; i < copy_height; i++)
  598. {
  599. memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
  600. copy_width * sizeof(char_info_t) );
  601. }
  602. /* clear the end of each row */
  603. if (new_width > old_width)
  604. {
  605. /* fill first row */
  606. for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
  607. /* and blast it to the other rows */
  608. for (i = 1; i < copy_height; i++)
  609. memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
  610. (new_width - old_width) * sizeof(char_info_t) );
  611. }
  612. /* clear remaining rows */
  613. if (new_height > old_height)
  614. {
  615. /* fill first row */
  616. for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
  617. /* and blast it to the other rows */
  618. for (i = old_height+1; i < new_height; i++)
  619. memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
  620. new_width * sizeof(char_info_t) );
  621. }
  622. free( screen_buffer->data );
  623. screen_buffer->data = new_data;
  624. screen_buffer->width = new_width;
  625. screen_buffer->height = new_height;
  626. return 1;
  627. }
  628. /* set misc screen buffer information */
  629. static int set_console_output_info( struct screen_buffer *screen_buffer,
  630. const struct set_console_output_info_request *req )
  631. {
  632. struct console_renderer_event evt;
  633. if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
  634. {
  635. if (req->cursor_size < 1 || req->cursor_size > 100)
  636. {
  637. set_error( STATUS_INVALID_PARAMETER );
  638. return 0;
  639. }
  640. if (screen_buffer->cursor_size != req->cursor_size ||
  641. screen_buffer->cursor_visible != req->cursor_visible)
  642. {
  643. screen_buffer->cursor_size = req->cursor_size;
  644. screen_buffer->cursor_visible = req->cursor_visible;
  645. evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
  646. evt.u.cursor_geom.size = req->cursor_size;
  647. evt.u.cursor_geom.visible = req->cursor_visible;
  648. console_input_events_append( screen_buffer->input->evt, &evt );
  649. }
  650. }
  651. if (req->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
  652. {
  653. if (req->cursor_x < 0 || req->cursor_x >= screen_buffer->width ||
  654. req->cursor_y < 0 || req->cursor_y >= screen_buffer->height)
  655. {
  656. set_error( STATUS_INVALID_PARAMETER );
  657. return 0;
  658. }
  659. if (screen_buffer->cursor_x != req->cursor_x || screen_buffer->cursor_y != req->cursor_y)
  660. {
  661. screen_buffer->cursor_x = req->cursor_x;
  662. screen_buffer->cursor_y = req->cursor_y;
  663. evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
  664. evt.u.cursor_pos.x = req->cursor_x;
  665. evt.u.cursor_pos.y = req->cursor_y;
  666. console_input_events_append( screen_buffer->input->evt, &evt );
  667. }
  668. }
  669. if (req->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
  670. {
  671. unsigned cc;
  672. /* new screen-buffer cannot be smaller than actual window */
  673. if (req->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
  674. req->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
  675. {
  676. set_error( STATUS_INVALID_PARAMETER );
  677. return 0;
  678. }
  679. /* FIXME: there are also some basic minimum and max size to deal with */
  680. if (!change_screen_buffer_size( screen_buffer, req->width, req->height )) return 0;
  681. evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
  682. evt.u.resize.width = req->width;
  683. evt.u.resize.height = req->height;
  684. console_input_events_append( screen_buffer->input->evt, &evt );
  685. evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
  686. evt.u.update.top = 0;
  687. evt.u.update.bottom = screen_buffer->height - 1;
  688. console_input_events_append( screen_buffer->input->evt, &evt );
  689. /* scroll window to display sb */
  690. if (screen_buffer->win.right >= req->width)
  691. {
  692. screen_buffer->win.right -= screen_buffer->win.left;
  693. screen_buffer->win.left = 0;
  694. }
  695. if (screen_buffer->win.bottom >= req->height)
  696. {
  697. screen_buffer->win.bottom -= screen_buffer->win.top;
  698. screen_buffer->win.top = 0;
  699. }
  700. /* reset cursor if needed (normally, if cursor was outside of new sb, the
  701. * window has been shifted so that the new position of the cursor will be
  702. * visible */
  703. cc = 0;
  704. if (screen_buffer->cursor_x >= req->width)
  705. {
  706. screen_buffer->cursor_x = req->width - 1;
  707. cc++;
  708. }
  709. if (screen_buffer->cursor_y >= req->height)
  710. {
  711. screen_buffer->cursor_y = req->height - 1;
  712. cc++;
  713. }
  714. if (cc)
  715. {
  716. evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
  717. evt.u.cursor_pos.x = req->cursor_x;
  718. evt.u.cursor_pos.y = req->cursor_y;
  719. console_input_events_append( screen_buffer->input->evt, &evt );
  720. }
  721. if (screen_buffer == screen_buffer->input->active &&
  722. screen_buffer->input->mode & ENABLE_WINDOW_INPUT)
  723. {
  724. INPUT_RECORD ir;
  725. ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
  726. ir.Event.WindowBufferSizeEvent.dwSize.X = req->width;
  727. ir.Event.WindowBufferSizeEvent.dwSize.Y = req->height;
  728. write_console_input( screen_buffer->input, 1, &ir );
  729. }
  730. }
  731. if (req->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
  732. {
  733. screen_buffer->attr = req->attr;
  734. }
  735. if (req->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
  736. {
  737. if (req->win_left < 0 || req->win_left > req->win_right ||
  738. req->win_right >= screen_buffer->width ||
  739. req->win_top < 0 || req->win_top > req->win_bottom ||
  740. req->win_bottom >= screen_buffer->height)
  741. {
  742. set_error( STATUS_INVALID_PARAMETER );
  743. return 0;
  744. }
  745. if (screen_buffer->win.left != req->win_left || screen_buffer->win.top != req->win_top ||
  746. screen_buffer->win.right != req->win_right || screen_buffer->win.bottom != req->win_bottom)
  747. {
  748. screen_buffer->win.left = req->win_left;
  749. screen_buffer->win.top = req->win_top;
  750. screen_buffer->win.right = req->win_right;
  751. screen_buffer->win.bottom = req->win_bottom;
  752. evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
  753. evt.u.display.left = req->win_left;
  754. evt.u.display.top = req->win_top;
  755. evt.u.display.width = req->win_right - req->win_left + 1;
  756. evt.u.display.height = req->win_bottom - req->win_top + 1;
  757. console_input_events_append( screen_buffer->input->evt, &evt );
  758. }
  759. }
  760. if (req->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
  761. {
  762. /* can only be done by renderer */
  763. if (current->process->console != screen_buffer->input)
  764. {
  765. set_error( STATUS_INVALID_PARAMETER );
  766. return 0;
  767. }
  768. screen_buffer->max_width = req->max_width;
  769. screen_buffer->max_height = req->max_height;
  770. }
  771. return 1;
  772. }
  773. /* appends a new line to history (history is a fixed size array) */
  774. static void console_input_append_hist( struct console_input* console, const WCHAR* buf, size_t len )
  775. {
  776. WCHAR* ptr = mem_alloc( (len + 1) * sizeof(WCHAR) );
  777. if (!ptr)
  778. {
  779. set_error( STATUS_NO_MEMORY );
  780. return;
  781. }
  782. if (!console || !console->history_size)
  783. {
  784. set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
  785. return;
  786. }
  787. memcpy( ptr, buf, len * sizeof(WCHAR) );
  788. ptr[len] = 0;
  789. if (console->history_mode && console->history_index &&
  790. strncmpW( console->history[console->history_index - 1], ptr, len * sizeof(WCHAR) ) == 0)
  791. {
  792. /* ok, mode ask us to not use twice the same string...
  793. * so just free mem and returns
  794. */
  795. set_error( STATUS_ALIAS_EXISTS );
  796. free(ptr);
  797. return;
  798. }
  799. if (console->history_index < console->history_size)
  800. {
  801. console->history[console->history_index++] = ptr;
  802. }
  803. else
  804. {
  805. free( console->history[0]) ;
  806. memmove( &console->history[0], &console->history[1],
  807. (console->history_size - 1) * sizeof(WCHAR*) );
  808. console->history[console->history_size - 1] = ptr;
  809. }
  810. }
  811. /* returns a line from the cache */
  812. static size_t console_input_get_hist( struct console_input *console, int index )
  813. {
  814. size_t ret = 0;
  815. if (index >= console->history_index) set_error( STATUS_INVALID_PARAMETER );
  816. else
  817. {
  818. ret = strlenW( console->history[index] ) * sizeof(WCHAR);
  819. set_reply_data( console->history[index], min( ret, get_reply_max_size() ));
  820. }
  821. return ret;
  822. }
  823. /* dumb dump */
  824. static void console_input_dump( struct object *obj, int verbose )
  825. {
  826. struct console_input *console = (struct console_input *)obj;
  827. assert( obj->ops == &console_input_ops );
  828. fprintf( stderr, "Console input active=%p evt=%p\n",
  829. console->active, console->evt );
  830. }
  831. static void console_input_destroy( struct object *obj )
  832. {
  833. struct console_input* console_in = (struct console_input *)obj;
  834. struct screen_buffer* curr;
  835. int i;
  836. assert( obj->ops == &console_input_ops );
  837. if (console_in->title) free( console_in->title );
  838. if (console_in->records) free( console_in->records );
  839. if (console_in->active) release_object( console_in->active );
  840. console_in->active = NULL;
  841. for (curr = screen_buffer_list; curr; curr = curr->next)
  842. {
  843. if (curr->input == console_in) curr->input = NULL;
  844. }
  845. release_object( console_in->evt );
  846. console_in->evt = NULL;
  847. release_object( console_in->event );
  848. for (i = 0; i < console_in->history_size; i++)
  849. if (console_in->history[i]) free( console_in->history[i] );
  850. if (console_in->history) free( console_in->history );
  851. }
  852. static void screen_buffer_dump( struct object *obj, int verbose )
  853. {
  854. struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
  855. assert( obj->ops == &screen_buffer_ops );
  856. fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
  857. }
  858. static void screen_buffer_destroy( struct object *obj )
  859. {
  860. struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
  861. assert( obj->ops == &screen_buffer_ops );
  862. if (screen_buffer->next) screen_buffer->next->prev = screen_buffer->prev;
  863. if (screen_buffer->prev) screen_buffer->prev->next = screen_buffer->next;
  864. else screen_buffer_list = screen_buffer->next;
  865. if (screen_buffer->input && screen_buffer->input->active == screen_buffer)
  866. {
  867. struct screen_buffer* sb;
  868. for (sb = screen_buffer_list; sb && sb->input != screen_buffer->input; sb = sb->next);
  869. screen_buffer->input->active = sb;
  870. }
  871. if (screen_buffer->data) free( screen_buffer->data );
  872. }
  873. /* write data into a screen buffer */
  874. static int write_console_output( struct screen_buffer *screen_buffer, size_t size,
  875. const void* data, enum char_info_mode mode,
  876. int x, int y, int wrap )
  877. {
  878. int i;
  879. char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
  880. if (y >= screen_buffer->height) return 0;
  881. if (wrap)
  882. end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
  883. else
  884. end = screen_buffer->data + (y+1) * screen_buffer->width;
  885. switch(mode)
  886. {
  887. case CHAR_INFO_MODE_TEXT:
  888. {
  889. const WCHAR *ptr = data;
  890. for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->ch = ptr[i];
  891. }
  892. break;
  893. case CHAR_INFO_MODE_ATTR:
  894. {
  895. const unsigned short *ptr = data;
  896. for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) dest->attr = ptr[i];
  897. }
  898. break;
  899. case CHAR_INFO_MODE_TEXTATTR:
  900. {
  901. const char_info_t *ptr = data;
  902. for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++) *dest = ptr[i];
  903. }
  904. break;
  905. case CHAR_INFO_MODE_TEXTSTDATTR:
  906. {
  907. const WCHAR *ptr = data;
  908. for (i = 0; i < size/sizeof(*ptr) && dest < end; dest++, i++)
  909. {
  910. dest->ch = ptr[i];
  911. dest->attr = screen_buffer->attr;
  912. }
  913. }
  914. break;
  915. default:
  916. set_error( STATUS_INVALID_PARAMETER );
  917. return 0;
  918. }
  919. if (i && screen_buffer == screen_buffer->input->active)
  920. {
  921. struct console_renderer_event evt;
  922. evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
  923. evt.u.update.top = y + x / screen_buffer->width;
  924. evt.u.update.bottom = y + (x + i - 1) / screen_buffer->width;
  925. console_input_events_append( screen_buffer->input->evt, &evt );
  926. }
  927. return i;
  928. }
  929. /* fill a screen buffer with uniform data */
  930. static int fill_console_output( struct screen_buffer *screen_buffer, char_info_t data,
  931. enum char_info_mode mode, int x, int y, int count, int wrap )
  932. {
  933. int i;
  934. char_info_t *end, *dest = screen_buffer->data + y * screen_buffer->width + x;
  935. if (y >= screen_buffer->height) return 0;
  936. if (wrap)
  937. end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
  938. else
  939. end = screen_buffer->data + (y+1) * screen_buffer->width;
  940. if (count > end - dest) count = end - dest;
  941. switch(mode)
  942. {
  943. case CHAR_INFO_MODE_TEXT:
  944. for (i = 0; i < count; i++) dest[i].ch = data.ch;
  945. break;
  946. case CHAR_INFO_MODE_ATTR:
  947. for (i = 0; i < count; i++) dest[i].attr = data.attr;
  948. break;
  949. case CHAR_INFO_MODE_TEXTATTR:
  950. for (i = 0; i < count; i++) dest[i] = data;
  951. break;
  952. case CHAR_INFO_MODE_TEXTSTDATTR:
  953. for (i = 0; i < count; i++)
  954. {
  955. dest[i].ch = data.ch;
  956. dest[i].attr = screen_buffer->attr;
  957. }
  958. break;
  959. default:
  960. set_error( STATUS_INVALID_PARAMETER );
  961. return 0;
  962. }
  963. if (count && screen_buffer == screen_buffer->input->active)
  964. {
  965. struct console_renderer_event evt;
  966. evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
  967. evt.u.update.top = y;
  968. evt.u.update.bottom = (y * screen_buffer->width + x + count - 1) / screen_buffer->width;
  969. console_input_events_append( screen_buffer->input->evt, &evt );
  970. }
  971. return i;
  972. }
  973. /* read data from a screen buffer */
  974. static void read_console_output( struct screen_buffer *screen_buffer, int x, int y,
  975. enum char_info_mode mode, int wrap )
  976. {
  977. int i;
  978. char_info_t *end, *src = screen_buffer->data + y * screen_buffer->width + x;
  979. if (y >= screen_buffer->height) return;
  980. if (wrap)
  981. end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
  982. else
  983. end = screen_buffer->data + (y+1) * screen_buffer->width;
  984. switch(mode)
  985. {
  986. case CHAR_INFO_MODE_TEXT:
  987. {
  988. WCHAR *data;
  989. int count = min( end - src, get_reply_max_size() / sizeof(*data) );
  990. if ((data = set_reply_data_size( count * sizeof(*data) )))
  991. {
  992. for (i = 0; i < count; i++) data[i] = src[i].ch;
  993. }
  994. }
  995. break;
  996. case CHAR_INFO_MODE_ATTR:
  997. {
  998. unsigned short *data;
  999. int count = min( end - src, get_reply_max_size() / sizeof(*data) );
  1000. if ((data = set_reply_data_size( count * sizeof(*data) )))
  1001. {
  1002. for (i = 0; i < count; i++) data[i] = src[i].attr;
  1003. }
  1004. }
  1005. break;
  1006. case CHAR_INFO_MODE_TEXTATTR:
  1007. {
  1008. char_info_t *data;
  1009. int count = min( end - src, get_reply_max_size() / sizeof(*data) );
  1010. if ((data = set_reply_data_size( count * sizeof(*data) )))
  1011. {
  1012. for (i = 0; i < count; i++) data[i] = src[i];
  1013. }
  1014. }
  1015. break;
  1016. default:
  1017. set_error( STATUS_INVALID_PARAMETER );
  1018. break;
  1019. }
  1020. }
  1021. /* scroll parts of a screen buffer */
  1022. static void scroll_console_output( obj_handle_t handle, int xsrc, int ysrc, int xdst, int ydst,
  1023. int w, int h )
  1024. {
  1025. struct screen_buffer *screen_buffer;
  1026. int j;
  1027. char_info_t *psrc, *pdst;
  1028. struct console_renderer_event evt;
  1029. if (!(screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, handle,
  1030. GENERIC_READ, &screen_buffer_ops )))
  1031. return;
  1032. if (xsrc < 0 || ysrc < 0 || xdst < 0 || ydst < 0 ||
  1033. xsrc + w > screen_buffer->width ||
  1034. xdst + w > screen_buffer->width ||
  1035. ysrc + h > screen_buffer->height ||
  1036. ydst + h > screen_buffer->height ||
  1037. w == 0 || h == 0)
  1038. {
  1039. set_error( STATUS_INVALID_PARAMETER );
  1040. release_object( screen_buffer );
  1041. return;
  1042. }
  1043. if (ysrc < ydst)
  1044. {
  1045. psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
  1046. pdst = &screen_buffer->data[(ydst + h - 1) * screen_buffer->width + xdst];
  1047. for (j = h; j > 0; j--)
  1048. {
  1049. memcpy(pdst, psrc, w * sizeof(*pdst) );
  1050. pdst -= screen_buffer->width;
  1051. psrc -= screen_buffer->width;
  1052. }
  1053. }
  1054. else
  1055. {
  1056. psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
  1057. pdst = &screen_buffer->data[ydst * screen_buffer->width + xdst];
  1058. for (j = 0; j < h; j++)
  1059. {
  1060. /* we use memmove here because when psrc and pdst are the same,
  1061. * copies are done on the same row, so the dst and src blocks
  1062. * can overlap */
  1063. memmove( pdst, psrc, w * sizeof(*pdst) );
  1064. pdst += screen_buffer->width;
  1065. psrc += screen_buffer->width;
  1066. }
  1067. }
  1068. /* FIXME: this could be enhanced, by signalling scroll */
  1069. evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
  1070. evt.u.update.top = min(ysrc, ydst);
  1071. evt.u.update.bottom = max(ysrc, ydst) + h - 1;
  1072. console_input_events_append( screen_buffer->input->evt, &evt );
  1073. release_object( screen_buffer );
  1074. }
  1075. /* allocate a console for the renderer */
  1076. DECL_HANDLER(alloc_console)
  1077. {
  1078. obj_handle_t in = 0;
  1079. obj_handle_t evt = 0;
  1080. struct process *process;
  1081. struct process *renderer = current->process;
  1082. struct console_input *console;
  1083. process = (req->pid) ? get_process_from_id( req->pid ) :
  1084. (struct process *)grab_object( renderer->parent );
  1085. reply->handle_in = 0;
  1086. reply->event = 0;
  1087. if (!process) return;
  1088. if (process != renderer && process->console)
  1089. {
  1090. set_error( STATUS_ACCESS_DENIED );
  1091. goto the_end;
  1092. }
  1093. if ((console = (struct console_input*)create_console_input( current )))
  1094. {
  1095. if ((in = alloc_handle( renderer, console, req->access, req->inherit )))
  1096. {
  1097. if ((evt = alloc_handle( renderer, console->evt,
  1098. SYNCHRONIZE|GENERIC_READ|GENERIC_WRITE, FALSE )))
  1099. {
  1100. if (process != renderer)
  1101. {
  1102. process->console = (struct console_input*)grab_object( console );
  1103. console->num_proc++;
  1104. }
  1105. reply->handle_in = in;
  1106. reply->event = evt;
  1107. release_object( console );
  1108. goto the_end;
  1109. }
  1110. close_handle( renderer, in, NULL );
  1111. }
  1112. free_console( process );
  1113. }
  1114. the_end:
  1115. release_object( process );
  1116. }
  1117. /* free the console of the current process */
  1118. DECL_HANDLER(free_console)
  1119. {
  1120. free_console( current->process );
  1121. }
  1122. /* let the renderer peek the events it's waiting on */
  1123. DECL_HANDLER(get_console_renderer_events)
  1124. {
  1125. struct console_input_events *evt;
  1126. evt = (struct console_input_events *)get_handle_obj( current->process, req->handle,
  1127. GENERIC_WRITE, &console_input_events_ops );
  1128. if (!evt) return;
  1129. console_input_events_get( evt );
  1130. release_object( evt );
  1131. }
  1132. /* open a handle to the process console */
  1133. DECL_HANDLER(open_console)
  1134. {
  1135. struct object *obj = NULL;
  1136. reply->handle = 0;
  1137. switch (req->from)
  1138. {
  1139. case 0:
  1140. if (current->process->console && current->process->console->renderer)
  1141. obj = grab_object( (struct object*)current->process->console );
  1142. break;
  1143. case 1:
  1144. if (current->process->console && current->process->console->renderer &&
  1145. current->process->console->active)
  1146. obj = grab_object( (struct object*)current->process->console->active );
  1147. break;
  1148. default:
  1149. if ((obj = get_handle_obj( current->process, (obj_handle_t)req->from,
  1150. GENERIC_READ|GENERIC_WRITE, &console_input_ops )))
  1151. {
  1152. struct console_input* console = (struct console_input*)obj;
  1153. obj = (console->active) ? grab_object( console->active ) : NULL;
  1154. release_object( console );
  1155. }
  1156. break;
  1157. }
  1158. /* FIXME: req->share is not used (as in screen buffer creation) */
  1159. if (obj)
  1160. {
  1161. reply->handle = alloc_handle( current->process, obj, req->access, req->inherit );
  1162. release_object( obj );
  1163. }
  1164. else if (!get_error()) set_error( STATUS_ACCESS_DENIED );
  1165. }
  1166. /* set info about a console input */
  1167. DECL_HANDLER(set_console_input_info)
  1168. {
  1169. set_console_input_info( req, get_req_data(), get_req_data_size() );
  1170. }
  1171. /* get info about a console (output only) */
  1172. DECL_HANDLER(get_console_input_info)
  1173. {
  1174. struct console_input *console;
  1175. if (!(console = console_input_get( req->handle, GENERIC_READ ))) return;
  1176. if (console->title)
  1177. {
  1178. size_t len = strlenW( console->title ) * sizeof(WCHAR);
  1179. if (len > get_reply_max_size()) len = get_reply_max_size();
  1180. set_reply_data( console->title, len );
  1181. }
  1182. reply->history_mode = console->history_mode;
  1183. reply->history_size = console->history_size;
  1184. reply->history_index = console->history_index;
  1185. reply->edition_mode = console->edition_mode;
  1186. release_object( console );
  1187. }
  1188. /* get a console mode (input or output) */
  1189. DECL_HANDLER(get_console_mode)
  1190. {
  1191. reply->mode = get_console_mode( req->handle );
  1192. }
  1193. /* set a console mode (input or output) */
  1194. DECL_HANDLER(set_console_mode)
  1195. {
  1196. set_console_mode( req->handle, req->mode );
  1197. }
  1198. /* add input records to a console input queue */
  1199. DECL_HANDLER(write_console_input)
  1200. {
  1201. struct console_input *console;
  1202. reply->written = 0;
  1203. if (!(console = (struct console_input *)get_handle_obj( current->process, req->handle,
  1204. GENERIC_WRITE, &console_input_ops )))
  1205. return;
  1206. reply->written = write_console_input( console, get_req_data_size() / sizeof(INPUT_RECORD),
  1207. get_req_data() );
  1208. release_object( console );
  1209. }
  1210. /* fetch input records from a console input queue */
  1211. DECL_HANDLER(read_console_input)
  1212. {
  1213. int count = get_reply_max_size() / sizeof(INPUT_RECORD);
  1214. reply->read = read_console_input( req->handle, count, req->flush );
  1215. }
  1216. /* appends a string to console's history */
  1217. DECL_HANDLER(append_console_input_history)
  1218. {
  1219. struct console_input *console;
  1220. if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
  1221. console_input_append_hist( console, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
  1222. release_object( console );
  1223. }
  1224. /* appends a string to console's history */
  1225. DECL_HANDLER(get_console_input_history)
  1226. {
  1227. struct console_input *console;
  1228. if (!(console = console_input_get( req->handle, GENERIC_WRITE ))) return;
  1229. reply->total = console_input_get_hist( console, req->index );
  1230. release_object( console );
  1231. }
  1232. /* creates a screen buffer */
  1233. DECL_HANDLER(create_console_output)
  1234. {
  1235. struct console_input* console;
  1236. struct screen_buffer* screen_buffer;
  1237. if (!(console = console_input_get( req->handle_in, GENERIC_WRITE))) return;
  1238. screen_buffer = create_console_output( console );
  1239. if (screen_buffer)
  1240. {
  1241. /* FIXME: should store sharing and test it when opening the CONOUT$ device
  1242. * see file.c on how this could be done */
  1243. reply->handle_out = alloc_handle( current->process, screen_buffer,
  1244. req->access, req->inherit );
  1245. release_object( screen_buffer );
  1246. }
  1247. release_object( console );
  1248. }
  1249. /* set info about a console screen buffer */
  1250. DECL_HANDLER(set_console_output_info)
  1251. {
  1252. struct screen_buffer *screen_buffer;
  1253. if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
  1254. GENERIC_WRITE, &screen_buffer_ops)))
  1255. {
  1256. set_console_output_info( screen_buffer, req );
  1257. release_object( screen_buffer );
  1258. }
  1259. }
  1260. /* get info about a console screen buffer */
  1261. DECL_HANDLER(get_console_output_info)
  1262. {
  1263. struct screen_buffer *screen_buffer;
  1264. if ((screen_buffer = (struct screen_buffer *)get_handle_obj( current->process, req->handle,
  1265. GENERIC_READ, &screen_buffer_ops)))
  1266. {
  1267. reply->cursor_size = screen_buffer->cursor_size;
  1268. reply->cursor_visible = screen_buffer->cursor_visible;
  1269. reply->cursor_x = screen_buffer->cursor_x;
  1270. reply->cursor_y = screen_buffer->cursor_y;
  1271. reply->width = screen_buffer->width;
  1272. reply->height = screen_buffer->height;
  1273. reply->attr = screen_buffer->attr;
  1274. reply->win_left = screen_buffer->win.left;
  1275. reply->win_top = screen_buffer->win.top;
  1276. reply->win_right = screen_buffer->win.right;
  1277. reply->win_bottom = screen_buffer->win.bottom;
  1278. reply->max_width = screen_buffer->max_width;
  1279. reply->max_height = screen_buffer->max_height;
  1280. release_object( screen_buffer );
  1281. }
  1282. }
  1283. /* read data (chars & attrs) from a screen buffer */
  1284. DECL_HANDLER(read_console_output)
  1285. {
  1286. struct screen_buffer *screen_buffer;
  1287. if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
  1288. GENERIC_READ, &screen_buffer_ops )))
  1289. {
  1290. read_console_output( screen_buffer, req->x, req->y, req->mode, req->wrap );
  1291. reply->width = screen_buffer->width;
  1292. reply->height = screen_buffer->height;
  1293. release_object( screen_buffer );
  1294. }
  1295. }
  1296. /* write data (char and/or attrs) to a screen buffer */
  1297. DECL_HANDLER(write_console_output)
  1298. {
  1299. struct screen_buffer *screen_buffer;
  1300. if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
  1301. GENERIC_WRITE, &screen_buffer_ops)))
  1302. {
  1303. reply->written = write_console_output( screen_buffer, get_req_data_size(), get_req_data(),
  1304. req->mode, req->x, req->y, req->wrap );
  1305. reply->width = screen_buffer->width;
  1306. reply->height = screen_buffer->height;
  1307. release_object( screen_buffer );
  1308. }
  1309. }
  1310. /* fill a screen buffer with constant data (chars and/or attributes) */
  1311. DECL_HANDLER(fill_console_output)
  1312. {
  1313. struct screen_buffer *screen_buffer;
  1314. if ((screen_buffer = (struct screen_buffer*)get_handle_obj( current->process, req->handle,
  1315. GENERIC_WRITE, &screen_buffer_ops)))
  1316. {
  1317. reply->written = fill_console_output( screen_buffer, req->data, req->mode,
  1318. req->x, req->y, req->count, req->wrap );
  1319. release_object( screen_buffer );
  1320. }
  1321. }
  1322. /* move a rect of data in a screen buffer */
  1323. DECL_HANDLER(move_console_output)
  1324. {
  1325. scroll_console_output( req->handle, req->x_src, req->y_src, req->x_dst, req->y_dst,
  1326. req->w, req->h );
  1327. }
  1328. /* sends a signal to a console (process, group...) */
  1329. DECL_HANDLER(send_console_signal)
  1330. {
  1331. process_id_t group;
  1332. group = req->group_id ? req->group_id : current->process->group_id;
  1333. if (!group)
  1334. set_error( STATUS_INVALID_PARAMETER );
  1335. else
  1336. propagate_console_signal( current->process->console, req->signal, group );
  1337. }
  1338. /* get console which renderer is 'current' */
  1339. static int cgwe_enum( struct process* process, void* user)
  1340. {
  1341. if (process->console && process->console->renderer == current)
  1342. {
  1343. *(struct console_input**)user = process->console;
  1344. return 1;
  1345. }
  1346. return 0;
  1347. }
  1348. DECL_HANDLER(get_console_wait_event)
  1349. {
  1350. struct console_input* console = NULL;
  1351. if (current->process->console && current->process->console->renderer)
  1352. console = (struct console_input*)grab_object( (struct object*)current->process->console );
  1353. else enum_processes(cgwe_enum, &console);
  1354. if (console)
  1355. {
  1356. reply->handle = alloc_handle( current->process, console->event,
  1357. EVENT_ALL_ACCESS, FALSE);
  1358. release_object( console );
  1359. }
  1360. else set_error( STATUS_INVALID_PARAMETER );
  1361. }