conhost.c 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807
  1. /*
  2. * Copyright 1998 Alexandre Julliard
  3. * Copyright 2001 Eric Pouech
  4. * Copyright 2012 Detlef Riekenberg
  5. * Copyright 2020 Jacek Caban
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include <assert.h>
  22. #include <limits.h>
  23. #include "conhost.h"
  24. #include "wine/server.h"
  25. #include "wine/debug.h"
  26. WINE_DEFAULT_DEBUG_CHANNEL(console);
  27. static const char_info_t empty_char_info = { ' ', 0x0007 }; /* white on black space */
  28. static CRITICAL_SECTION console_section;
  29. static CRITICAL_SECTION_DEBUG critsect_debug =
  30. {
  31. 0, 0, &console_section,
  32. { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
  33. 0, 0, { (DWORD_PTR)(__FILE__ ": console_section") }
  34. };
  35. static CRITICAL_SECTION console_section = { &critsect_debug, -1, 0, 0, 0, 0 };
  36. static void *ioctl_buffer;
  37. static size_t ioctl_buffer_size;
  38. static void *alloc_ioctl_buffer( size_t size )
  39. {
  40. if (size > ioctl_buffer_size)
  41. {
  42. void *new_buffer;
  43. if (!(new_buffer = realloc( ioctl_buffer, size ))) return NULL;
  44. ioctl_buffer = new_buffer;
  45. ioctl_buffer_size = size;
  46. }
  47. return ioctl_buffer;
  48. }
  49. static int screen_buffer_compare_id( const void *key, const struct wine_rb_entry *entry )
  50. {
  51. struct screen_buffer *screen_buffer = WINE_RB_ENTRY_VALUE( entry, struct screen_buffer, entry );
  52. return PtrToLong(key) - screen_buffer->id;
  53. }
  54. static struct wine_rb_tree screen_buffer_map = { screen_buffer_compare_id };
  55. static void destroy_screen_buffer( struct screen_buffer *screen_buffer )
  56. {
  57. if (screen_buffer->console->active == screen_buffer)
  58. screen_buffer->console->active = NULL;
  59. wine_rb_remove( &screen_buffer_map, &screen_buffer->entry );
  60. free( screen_buffer->data );
  61. free( screen_buffer );
  62. }
  63. static struct screen_buffer *create_screen_buffer( struct console *console, int id, int width, int height )
  64. {
  65. struct screen_buffer *screen_buffer;
  66. unsigned int i;
  67. if (!(screen_buffer = calloc( 1, sizeof(*screen_buffer) ))) return NULL;
  68. screen_buffer->console = console;
  69. screen_buffer->id = id;
  70. screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
  71. screen_buffer->cursor_size = 25;
  72. screen_buffer->cursor_visible = 1;
  73. screen_buffer->width = width;
  74. screen_buffer->height = height;
  75. screen_buffer->attr = 0x07;
  76. screen_buffer->popup_attr = 0xf5;
  77. screen_buffer->font.weight = FW_NORMAL;
  78. screen_buffer->font.pitch_family = FIXED_PITCH | FF_DONTCARE;
  79. if (console->active)
  80. {
  81. screen_buffer->max_width = console->active->max_width;
  82. screen_buffer->max_height = console->active->max_height;
  83. screen_buffer->win.right = console->active->win.right - console->active->win.left;
  84. screen_buffer->win.bottom = console->active->win.bottom - console->active->win.top;
  85. }
  86. else
  87. {
  88. screen_buffer->max_width = width;
  89. screen_buffer->max_height = height;
  90. screen_buffer->win.right = width - 1;
  91. screen_buffer->win.bottom = height - 1;
  92. }
  93. if (wine_rb_put( &screen_buffer_map, LongToPtr(id), &screen_buffer->entry ))
  94. {
  95. free( screen_buffer );
  96. ERR( "id %x already exists\n", id );
  97. return NULL;
  98. }
  99. if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
  100. sizeof(*screen_buffer->data) )))
  101. {
  102. destroy_screen_buffer( screen_buffer );
  103. return NULL;
  104. }
  105. /* clear the first row */
  106. for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
  107. /* and copy it to all other rows */
  108. for (i = 1; i < screen_buffer->height; i++)
  109. memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
  110. screen_buffer->width * sizeof(char_info_t) );
  111. return screen_buffer;
  112. }
  113. static BOOL is_active( struct screen_buffer *screen_buffer )
  114. {
  115. return screen_buffer == screen_buffer->console->active;
  116. }
  117. static unsigned int get_tty_cp( struct console *console )
  118. {
  119. return console->is_unix ? CP_UNIXCP : CP_UTF8;
  120. }
  121. static void tty_flush( struct console *console )
  122. {
  123. if (!console->tty_output || !console->tty_buffer_count) return;
  124. TRACE("%s\n", debugstr_an(console->tty_buffer, console->tty_buffer_count));
  125. if (!WriteFile( console->tty_output, console->tty_buffer, console->tty_buffer_count,
  126. NULL, NULL ))
  127. WARN( "write failed: %u\n", GetLastError() );
  128. console->tty_buffer_count = 0;
  129. }
  130. static void tty_write( struct console *console, const char *buffer, size_t size )
  131. {
  132. if (!size || !console->tty_output) return;
  133. if (console->tty_buffer_count + size > sizeof(console->tty_buffer))
  134. tty_flush( console );
  135. if (console->tty_buffer_count + size <= sizeof(console->tty_buffer))
  136. {
  137. memcpy( console->tty_buffer + console->tty_buffer_count, buffer, size );
  138. console->tty_buffer_count += size;
  139. }
  140. else
  141. {
  142. assert( !console->tty_buffer_count );
  143. if (!WriteFile( console->tty_output, buffer, size, NULL, NULL ))
  144. WARN( "write failed: %u\n", GetLastError() );
  145. }
  146. }
  147. static void *tty_alloc_buffer( struct console *console, size_t size )
  148. {
  149. void *ret;
  150. if (console->tty_buffer_count + size > sizeof(console->tty_buffer)) return NULL;
  151. ret = console->tty_buffer + console->tty_buffer_count;
  152. console->tty_buffer_count += size;
  153. return ret;
  154. }
  155. static void hide_tty_cursor( struct console *console )
  156. {
  157. if (console->tty_cursor_visible)
  158. {
  159. tty_write( console, "\x1b[?25l", 6 );
  160. console->tty_cursor_visible = FALSE;
  161. }
  162. }
  163. static void set_tty_cursor( struct console *console, unsigned int x, unsigned int y )
  164. {
  165. char buf[64];
  166. if (console->tty_cursor_x == x && console->tty_cursor_y == y) return;
  167. if (!x && y == console->tty_cursor_y + 1) strcpy( buf, "\r\n" );
  168. else if (!x && y == console->tty_cursor_y) strcpy( buf, "\r" );
  169. else if (y == console->tty_cursor_y)
  170. {
  171. if (console->tty_cursor_x >= console->active->width)
  172. {
  173. if (console->is_unix)
  174. {
  175. /* Unix will usually have the cursor at width-1 in this case. instead of depending
  176. * on the exact behaviour, move the cursor to the first column and move forward
  177. * from there. */
  178. tty_write( console, "\r", 1 );
  179. console->tty_cursor_x = 0;
  180. }
  181. else if (console->active->mode & ENABLE_WRAP_AT_EOL_OUTPUT)
  182. {
  183. console->tty_cursor_x--;
  184. }
  185. if (console->tty_cursor_x == x) return;
  186. }
  187. if (x + 1 == console->tty_cursor_x) strcpy( buf, "\b" );
  188. else if (x > console->tty_cursor_x) sprintf( buf, "\x1b[%uC", x - console->tty_cursor_x );
  189. else sprintf( buf, "\x1b[%uD", console->tty_cursor_x - x );
  190. }
  191. else if (x || y)
  192. {
  193. hide_tty_cursor( console );
  194. sprintf( buf, "\x1b[%u;%uH", y + 1, x + 1);
  195. }
  196. else strcpy( buf, "\x1b[H" );
  197. console->tty_cursor_x = x;
  198. console->tty_cursor_y = y;
  199. tty_write( console, buf, strlen(buf) );
  200. }
  201. static void set_tty_cursor_relative( struct console *console, unsigned int x, unsigned int y )
  202. {
  203. if (y < console->tty_cursor_y)
  204. {
  205. char buf[64];
  206. sprintf( buf, "\x1b[%uA", console->tty_cursor_y - y );
  207. tty_write( console, buf, strlen(buf) );
  208. console->tty_cursor_y = y;
  209. }
  210. else
  211. {
  212. while (console->tty_cursor_y < y)
  213. {
  214. console->tty_cursor_x = 0;
  215. console->tty_cursor_y++;
  216. tty_write( console, "\r\n", 2 );
  217. }
  218. }
  219. set_tty_cursor( console, x, y );
  220. }
  221. static void set_tty_attr( struct console *console, unsigned int attr )
  222. {
  223. char buf[8];
  224. if ((attr & 0x0f) != (console->tty_attr & 0x0f))
  225. {
  226. if ((attr & 0x0f) != 7)
  227. {
  228. unsigned int n = 30;
  229. if (attr & FOREGROUND_BLUE) n += 4;
  230. if (attr & FOREGROUND_GREEN) n += 2;
  231. if (attr & FOREGROUND_RED) n += 1;
  232. if (attr & FOREGROUND_INTENSITY) n += 60;
  233. sprintf(buf, "\x1b[%um", n);
  234. tty_write( console, buf, strlen(buf) );
  235. }
  236. else tty_write( console, "\x1b[m", 3 );
  237. }
  238. if ((attr & 0xf0) != (console->tty_attr & 0xf0) && attr != 7)
  239. {
  240. unsigned int n = 40;
  241. if (attr & BACKGROUND_BLUE) n += 4;
  242. if (attr & BACKGROUND_GREEN) n += 2;
  243. if (attr & BACKGROUND_RED) n += 1;
  244. if (attr & BACKGROUND_INTENSITY) n += 60;
  245. sprintf(buf, "\x1b[%um", n);
  246. tty_write( console, buf, strlen(buf) );
  247. }
  248. console->tty_attr = attr;
  249. }
  250. static void tty_sync( struct console *console )
  251. {
  252. if (!console->tty_output) return;
  253. if (console->active->cursor_visible)
  254. {
  255. set_tty_cursor( console, get_bounded_cursor_x( console->active ), console->active->cursor_y );
  256. if (!console->tty_cursor_visible)
  257. {
  258. tty_write( console, "\x1b[?25h", 6 ); /* show cursor */
  259. console->tty_cursor_visible = TRUE;
  260. }
  261. }
  262. else if (console->tty_cursor_visible)
  263. hide_tty_cursor( console );
  264. tty_flush( console );
  265. }
  266. static void init_tty_output( struct console *console )
  267. {
  268. if (!console->is_unix)
  269. {
  270. /* initialize tty output, but don't flush */
  271. tty_write( console, "\x1b[2J", 4 ); /* clear screen */
  272. set_tty_attr( console, console->active->attr );
  273. tty_write( console, "\x1b[H", 3 ); /* move cursor to (0,0) */
  274. }
  275. else console->tty_attr = empty_char_info.attr;
  276. console->tty_cursor_visible = TRUE;
  277. }
  278. static void scroll_to_cursor( struct screen_buffer *screen_buffer )
  279. {
  280. unsigned int cursor_x = get_bounded_cursor_x( screen_buffer );
  281. int w = screen_buffer->win.right - screen_buffer->win.left + 1;
  282. int h = screen_buffer->win.bottom - screen_buffer->win.top + 1;
  283. if (cursor_x < screen_buffer->win.left)
  284. screen_buffer->win.left = min( cursor_x, screen_buffer->width - w );
  285. else if (cursor_x > screen_buffer->win.right)
  286. screen_buffer->win.left = max( cursor_x, w ) - w + 1;
  287. screen_buffer->win.right = screen_buffer->win.left + w - 1;
  288. if (screen_buffer->cursor_y < screen_buffer->win.top)
  289. screen_buffer->win.top = min( screen_buffer->cursor_y, screen_buffer->height - h );
  290. else if (screen_buffer->cursor_y > screen_buffer->win.bottom)
  291. screen_buffer->win.top = max( screen_buffer->cursor_y, h ) - h + 1;
  292. screen_buffer->win.bottom = screen_buffer->win.top + h - 1;
  293. }
  294. static void update_output( struct screen_buffer *screen_buffer, RECT *rect )
  295. {
  296. int x, y, size, trailing_spaces;
  297. char_info_t *ch;
  298. char buf[8];
  299. if (!is_active( screen_buffer ) || rect->top > rect->bottom || rect->right < rect->left)
  300. return;
  301. TRACE( "%s\n", wine_dbgstr_rect( rect ));
  302. if (screen_buffer->console->win)
  303. {
  304. update_window_region( screen_buffer->console, rect );
  305. return;
  306. }
  307. if (!screen_buffer->console->tty_output) return;
  308. hide_tty_cursor( screen_buffer->console );
  309. for (y = rect->top; y <= rect->bottom; y++)
  310. {
  311. for (trailing_spaces = 0; trailing_spaces < screen_buffer->width; trailing_spaces++)
  312. {
  313. ch = &screen_buffer->data[(y + 1) * screen_buffer->width - trailing_spaces - 1];
  314. if (ch->ch != ' ' || ch->attr != 7) break;
  315. }
  316. if (trailing_spaces < 4) trailing_spaces = 0;
  317. for (x = rect->left; x <= rect->right; x++)
  318. {
  319. ch = &screen_buffer->data[y * screen_buffer->width + x];
  320. set_tty_attr( screen_buffer->console, ch->attr );
  321. set_tty_cursor( screen_buffer->console, x, y );
  322. if (x + trailing_spaces >= screen_buffer->width)
  323. {
  324. tty_write( screen_buffer->console, "\x1b[K", 3 );
  325. break;
  326. }
  327. size = WideCharToMultiByte( get_tty_cp( screen_buffer->console ), 0,
  328. &ch->ch, 1, buf, sizeof(buf), NULL, NULL );
  329. tty_write( screen_buffer->console, buf, size );
  330. screen_buffer->console->tty_cursor_x++;
  331. }
  332. }
  333. empty_update_rect( screen_buffer, rect );
  334. }
  335. static void new_line( struct screen_buffer *screen_buffer, RECT *update_rect )
  336. {
  337. unsigned int i;
  338. assert( screen_buffer->cursor_y >= screen_buffer->height );
  339. screen_buffer->cursor_y = screen_buffer->height - 1;
  340. if (screen_buffer->console->tty_output)
  341. update_output( screen_buffer, update_rect );
  342. else
  343. SetRect( update_rect, 0, 0, screen_buffer->width - 1, screen_buffer->height - 1 );
  344. memmove( screen_buffer->data, screen_buffer->data + screen_buffer->width,
  345. screen_buffer->width * (screen_buffer->height - 1) * sizeof(*screen_buffer->data) );
  346. for (i = 0; i < screen_buffer->width; i++)
  347. screen_buffer->data[screen_buffer->width * (screen_buffer->height - 1) + i] = empty_char_info;
  348. if (is_active( screen_buffer ))
  349. {
  350. screen_buffer->console->tty_cursor_y--;
  351. if (screen_buffer->console->tty_cursor_y != screen_buffer->height - 2)
  352. set_tty_cursor( screen_buffer->console, 0, screen_buffer->height - 2 );
  353. set_tty_cursor( screen_buffer->console, 0, screen_buffer->height - 1 );
  354. }
  355. }
  356. static void write_char( struct screen_buffer *screen_buffer, WCHAR ch, RECT *update_rect, unsigned int *home_y )
  357. {
  358. if (screen_buffer->cursor_x == screen_buffer->width)
  359. {
  360. screen_buffer->cursor_x = 0;
  361. screen_buffer->cursor_y++;
  362. }
  363. if (screen_buffer->cursor_y == screen_buffer->height)
  364. {
  365. if (home_y)
  366. {
  367. if (!*home_y) return;
  368. (*home_y)--;
  369. }
  370. new_line( screen_buffer, update_rect );
  371. }
  372. screen_buffer->data[screen_buffer->cursor_y * screen_buffer->width + screen_buffer->cursor_x].ch = ch;
  373. screen_buffer->data[screen_buffer->cursor_y * screen_buffer->width + screen_buffer->cursor_x].attr = screen_buffer->attr;
  374. update_rect->left = min( update_rect->left, screen_buffer->cursor_x );
  375. update_rect->top = min( update_rect->top, screen_buffer->cursor_y );
  376. update_rect->right = max( update_rect->right, screen_buffer->cursor_x );
  377. update_rect->bottom = max( update_rect->bottom, screen_buffer->cursor_y );
  378. screen_buffer->cursor_x++;
  379. }
  380. static NTSTATUS read_complete( struct console *console, NTSTATUS status, const void *buf, size_t size, int signal )
  381. {
  382. SERVER_START_REQ( get_next_console_request )
  383. {
  384. req->handle = wine_server_obj_handle( console->server );
  385. req->signal = signal;
  386. req->read = 1;
  387. req->status = status;
  388. wine_server_add_data( req, buf, size );
  389. status = wine_server_call( req );
  390. }
  391. SERVER_END_REQ;
  392. if (status && (console->read_ioctl || status != STATUS_INVALID_HANDLE)) ERR( "failed: %#x\n", status );
  393. console->signaled = signal;
  394. console->read_ioctl = 0;
  395. console->pending_read = 0;
  396. return status;
  397. }
  398. static NTSTATUS read_console_input( struct console *console, size_t out_size )
  399. {
  400. size_t count = min( out_size / sizeof(INPUT_RECORD), console->record_count );
  401. TRACE("count %u\n", count);
  402. read_complete( console, STATUS_SUCCESS, console->records, count * sizeof(*console->records),
  403. console->record_count > count );
  404. if (count < console->record_count)
  405. memmove( console->records, console->records + count,
  406. (console->record_count - count) * sizeof(*console->records) );
  407. console->record_count -= count;
  408. return STATUS_SUCCESS;
  409. }
  410. static void read_from_buffer( struct console *console, size_t out_size )
  411. {
  412. size_t len, read_len = 0;
  413. char *buf = NULL;
  414. switch( console->read_ioctl )
  415. {
  416. case IOCTL_CONDRV_READ_CONSOLE:
  417. out_size = min( out_size, console->read_buffer_count * sizeof(WCHAR) );
  418. read_complete( console, STATUS_SUCCESS, console->read_buffer, out_size, console->record_count != 0 );
  419. read_len = out_size / sizeof(WCHAR);
  420. break;
  421. case IOCTL_CONDRV_READ_FILE:
  422. read_len = len = 0;
  423. while (read_len < console->read_buffer_count && len < out_size)
  424. {
  425. len += WideCharToMultiByte( console->input_cp, 0, console->read_buffer + read_len, 1, NULL, 0, NULL, NULL );
  426. read_len++;
  427. }
  428. if (len)
  429. {
  430. if (!(buf = malloc( len )))
  431. {
  432. read_complete( console, STATUS_NO_MEMORY, NULL, 0, console->record_count != 0 );
  433. return;
  434. }
  435. WideCharToMultiByte( console->input_cp, 0, console->read_buffer, read_len, buf, len, NULL, NULL );
  436. }
  437. len = min( out_size, len );
  438. read_complete( console, STATUS_SUCCESS, buf, len, console->record_count != 0 );
  439. free( buf );
  440. break;
  441. }
  442. if (read_len < console->read_buffer_count)
  443. {
  444. memmove( console->read_buffer, console->read_buffer + read_len,
  445. (console->read_buffer_count - read_len) * sizeof(WCHAR) );
  446. }
  447. if (!(console->read_buffer_count -= read_len))
  448. free( console->read_buffer );
  449. }
  450. static void append_input_history( struct console *console, const WCHAR *str, size_t len )
  451. {
  452. struct history_line *ptr;
  453. if (!console->history_size) return;
  454. /* don't duplicate entry */
  455. if (console->history_mode && console->history_index &&
  456. console->history[console->history_index - 1]->len == len &&
  457. !memcmp( console->history[console->history_index - 1]->text, str, len ))
  458. return;
  459. if (!(ptr = malloc( offsetof( struct history_line, text[len / sizeof(WCHAR)] )))) return;
  460. ptr->len = len;
  461. memcpy( ptr->text, str, len );
  462. if (console->history_index < console->history_size)
  463. {
  464. console->history[console->history_index++] = ptr;
  465. }
  466. else
  467. {
  468. free( console->history[0]) ;
  469. memmove( &console->history[0], &console->history[1],
  470. (console->history_size - 1) * sizeof(*console->history) );
  471. console->history[console->history_size - 1] = ptr;
  472. }
  473. }
  474. static void edit_line_update( struct console *console, unsigned int begin, unsigned int length )
  475. {
  476. struct edit_line *ctx = &console->edit_line;
  477. if (!length) return;
  478. ctx->update_begin = min( ctx->update_begin, begin );
  479. ctx->update_end = max( ctx->update_end, begin + length - 1 );
  480. }
  481. static BOOL edit_line_grow( struct console *console, size_t length )
  482. {
  483. struct edit_line *ctx = &console->edit_line;
  484. WCHAR *new_buf;
  485. size_t new_size;
  486. if (ctx->len + length < ctx->size) return TRUE;
  487. /* round up size to 32 byte-WCHAR boundary */
  488. new_size = (ctx->len + length + 32) & ~31;
  489. if (!(new_buf = realloc( ctx->buf, sizeof(WCHAR) * new_size )))
  490. {
  491. ctx->status = STATUS_NO_MEMORY;
  492. return FALSE;
  493. }
  494. ctx->buf = new_buf;
  495. ctx->size = new_size;
  496. return TRUE;
  497. }
  498. static void edit_line_delete( struct console *console, int begin, int end )
  499. {
  500. struct edit_line *ctx = &console->edit_line;
  501. unsigned int len = end - begin;
  502. edit_line_update( console, begin, ctx->len - begin );
  503. if (end < ctx->len)
  504. memmove( &ctx->buf[begin], &ctx->buf[end], (ctx->len - end) * sizeof(WCHAR));
  505. ctx->len -= len;
  506. edit_line_update( console, 0, ctx->len );
  507. ctx->buf[ctx->len] = 0;
  508. }
  509. static void edit_line_insert( struct console *console, const WCHAR *str, unsigned int len )
  510. {
  511. struct edit_line *ctx = &console->edit_line;
  512. unsigned int update_len;
  513. if (!len) return;
  514. if (ctx->insert_mode)
  515. {
  516. if (!edit_line_grow( console, len )) return;
  517. if (ctx->len > ctx->cursor)
  518. memmove( &ctx->buf[ctx->cursor + len], &ctx->buf[ctx->cursor],
  519. (ctx->len - ctx->cursor) * sizeof(WCHAR) );
  520. ctx->len += len;
  521. update_len = ctx->len - ctx->cursor;
  522. }
  523. else
  524. {
  525. if (ctx->cursor + len > ctx->len)
  526. {
  527. if (!edit_line_grow( console, (ctx->cursor + len) - ctx->len) )
  528. return;
  529. ctx->len = ctx->cursor + len;
  530. }
  531. update_len = len;
  532. }
  533. memcpy( &ctx->buf[ctx->cursor], str, len * sizeof(WCHAR) );
  534. ctx->buf[ctx->len] = 0;
  535. edit_line_update( console, ctx->cursor, update_len );
  536. ctx->cursor += len;
  537. }
  538. static void edit_line_save_yank( struct console *console, unsigned int begin, unsigned int end )
  539. {
  540. struct edit_line *ctx = &console->edit_line;
  541. unsigned int len = end - begin;
  542. if (len <= 0) return;
  543. free(ctx->yanked);
  544. ctx->yanked = malloc( (len + 1) * sizeof(WCHAR) );
  545. if (!ctx->yanked)
  546. {
  547. ctx->status = STATUS_NO_MEMORY;
  548. return;
  549. }
  550. memcpy( ctx->yanked, &ctx->buf[begin], len * sizeof(WCHAR) );
  551. ctx->yanked[len] = 0;
  552. }
  553. static int edit_line_left_word_transition( struct console *console, int offset )
  554. {
  555. offset--;
  556. while (offset >= 0 && !iswalnum( console->edit_line.buf[offset] )) offset--;
  557. while (offset >= 0 && iswalnum( console->edit_line.buf[offset] )) offset--;
  558. if (offset >= 0) offset++;
  559. return max( offset, 0 );
  560. }
  561. static int edit_line_right_word_transition( struct console *console, int offset )
  562. {
  563. offset++;
  564. while (offset <= console->edit_line.len && iswalnum( console->edit_line.buf[offset] ))
  565. offset++;
  566. while (offset <= console->edit_line.len && !iswalnum( console->edit_line.buf[offset] ))
  567. offset++;
  568. return min(offset, console->edit_line.len);
  569. }
  570. static WCHAR *edit_line_history( struct console *console, unsigned int index )
  571. {
  572. WCHAR *ptr = NULL;
  573. if (index < console->history_index)
  574. {
  575. if ((ptr = malloc( console->history[index]->len + sizeof(WCHAR) )))
  576. {
  577. memcpy( ptr, console->history[index]->text, console->history[index]->len );
  578. ptr[console->history[index]->len / sizeof(WCHAR)] = 0;
  579. }
  580. }
  581. else if(console->edit_line.current_history)
  582. {
  583. if ((ptr = malloc( (lstrlenW(console->edit_line.current_history) + 1) * sizeof(WCHAR) )))
  584. lstrcpyW( ptr, console->edit_line.current_history );
  585. }
  586. return ptr;
  587. }
  588. static void edit_line_move_to_history( struct console *console, int index )
  589. {
  590. struct edit_line *ctx = &console->edit_line;
  591. WCHAR *line = edit_line_history(console, index);
  592. size_t len = line ? lstrlenW(line) : 0;
  593. /* save current line edition for recall when needed */
  594. if (ctx->history_index == console->history_index)
  595. {
  596. free( ctx->current_history );
  597. ctx->current_history = malloc( (ctx->len + 1) * sizeof(WCHAR) );
  598. if (ctx->current_history)
  599. {
  600. memcpy( ctx->current_history, ctx->buf, (ctx->len + 1) * sizeof(WCHAR) );
  601. }
  602. else
  603. {
  604. ctx->status = STATUS_NO_MEMORY;
  605. return;
  606. }
  607. }
  608. /* need to clean also the screen if new string is shorter than old one */
  609. edit_line_delete(console, 0, ctx->len);
  610. ctx->cursor = 0;
  611. /* insert new string */
  612. if (edit_line_grow(console, len + 1))
  613. {
  614. edit_line_insert( console, line, len );
  615. ctx->history_index = index;
  616. }
  617. free(line);
  618. }
  619. static void edit_line_find_in_history( struct console *console )
  620. {
  621. struct edit_line *ctx = &console->edit_line;
  622. int start_pos = ctx->history_index;
  623. unsigned int len, oldoffset;
  624. WCHAR *line;
  625. if (!console->history_index) return;
  626. if (ctx->history_index && ctx->history_index == console->history_index)
  627. {
  628. start_pos--;
  629. ctx->history_index--;
  630. }
  631. do
  632. {
  633. line = edit_line_history(console, ctx->history_index);
  634. if (ctx->history_index) ctx->history_index--;
  635. else ctx->history_index = console->history_index - 1;
  636. len = lstrlenW(line) + 1;
  637. if (len >= ctx->cursor && !memcmp( ctx->buf, line, ctx->cursor * sizeof(WCHAR) ))
  638. {
  639. /* need to clean also the screen if new string is shorter than old one */
  640. edit_line_delete(console, 0, ctx->len);
  641. if (edit_line_grow(console, len))
  642. {
  643. oldoffset = ctx->cursor;
  644. ctx->cursor = 0;
  645. edit_line_insert( console, line, len - 1 );
  646. ctx->cursor = oldoffset;
  647. free(line);
  648. return;
  649. }
  650. }
  651. free(line);
  652. }
  653. while (ctx->history_index != start_pos);
  654. }
  655. static void edit_line_move_left( struct console *console )
  656. {
  657. if (console->edit_line.cursor > 0) console->edit_line.cursor--;
  658. }
  659. static void edit_line_move_right( struct console *console )
  660. {
  661. struct edit_line *ctx = &console->edit_line;
  662. if (ctx->cursor < ctx->len) ctx->cursor++;
  663. }
  664. static void edit_line_move_left_word( struct console *console )
  665. {
  666. console->edit_line.cursor = edit_line_left_word_transition( console, console->edit_line.cursor );
  667. }
  668. static void edit_line_move_right_word( struct console *console )
  669. {
  670. console->edit_line.cursor = edit_line_right_word_transition( console, console->edit_line.cursor );
  671. }
  672. static void edit_line_move_home( struct console *console )
  673. {
  674. console->edit_line.cursor = 0;
  675. }
  676. static void edit_line_move_end( struct console *console )
  677. {
  678. console->edit_line.cursor = console->edit_line.len;
  679. }
  680. static void edit_line_set_mark( struct console *console )
  681. {
  682. console->edit_line.mark = console->edit_line.cursor;
  683. }
  684. static void edit_line_exchange_mark( struct console *console )
  685. {
  686. struct edit_line *ctx = &console->edit_line;
  687. unsigned int cursor;
  688. if (ctx->mark > ctx->len) return;
  689. cursor = ctx->cursor;
  690. ctx->cursor = ctx->mark;
  691. ctx->mark = cursor;
  692. }
  693. static void edit_line_copy_marked_zone( struct console *console )
  694. {
  695. struct edit_line *ctx = &console->edit_line;
  696. unsigned int begin, end;
  697. if (ctx->mark > ctx->len || ctx->mark == ctx->cursor) return;
  698. if (ctx->mark > ctx->cursor)
  699. {
  700. begin = ctx->cursor;
  701. end = ctx->mark;
  702. }
  703. else
  704. {
  705. begin = ctx->mark;
  706. end = ctx->cursor;
  707. }
  708. edit_line_save_yank( console, begin, end );
  709. }
  710. static void edit_line_transpose_char( struct console *console )
  711. {
  712. struct edit_line *ctx = &console->edit_line;
  713. WCHAR c;
  714. if (!ctx->cursor || ctx->cursor == ctx->len) return;
  715. c = ctx->buf[ctx->cursor];
  716. ctx->buf[ctx->cursor] = ctx->buf[ctx->cursor - 1];
  717. ctx->buf[ctx->cursor - 1] = c;
  718. edit_line_update( console, ctx->cursor - 1, 2 );
  719. ctx->cursor++;
  720. }
  721. static void edit_line_transpose_words( struct console *console )
  722. {
  723. struct edit_line *ctx = &console->edit_line;
  724. unsigned int left_offset = edit_line_left_word_transition( console, ctx->cursor );
  725. unsigned int right_offset = edit_line_right_word_transition( console, ctx->cursor );
  726. if (left_offset < ctx->cursor && right_offset > ctx->cursor)
  727. {
  728. unsigned int len_r = right_offset - ctx->cursor;
  729. unsigned int len_l = ctx->cursor - left_offset;
  730. char *tmp = malloc( len_r * sizeof(WCHAR) );
  731. if (!tmp)
  732. {
  733. ctx->status = STATUS_NO_MEMORY;
  734. return;
  735. }
  736. memcpy( tmp, &ctx->buf[ctx->cursor], len_r * sizeof(WCHAR) );
  737. memmove( &ctx->buf[left_offset + len_r], &ctx->buf[left_offset],
  738. len_l * sizeof(WCHAR) );
  739. memcpy( &ctx->buf[left_offset], tmp, len_r * sizeof(WCHAR) );
  740. free(tmp);
  741. edit_line_update( console, left_offset, len_l + len_r );
  742. ctx->cursor = right_offset;
  743. }
  744. }
  745. static void edit_line_lower_case_word( struct console *console )
  746. {
  747. struct edit_line *ctx = &console->edit_line;
  748. unsigned int new_offset = edit_line_right_word_transition( console, ctx->cursor );
  749. if (new_offset != ctx->cursor)
  750. {
  751. CharLowerBuffW( ctx->buf + ctx->cursor, new_offset - ctx->cursor + 1 );
  752. edit_line_update( console, ctx->cursor, new_offset - ctx->cursor + 1 );
  753. ctx->cursor = new_offset;
  754. }
  755. }
  756. static void edit_line_upper_case_word( struct console *console )
  757. {
  758. struct edit_line *ctx = &console->edit_line;
  759. unsigned int new_offset = edit_line_right_word_transition( console, ctx->cursor );
  760. if (new_offset != ctx->cursor)
  761. {
  762. CharUpperBuffW( ctx->buf + ctx->cursor, new_offset - ctx->cursor + 1 );
  763. edit_line_update( console, ctx->cursor, new_offset - ctx->cursor + 1 );
  764. ctx->cursor = new_offset;
  765. }
  766. }
  767. static void edit_line_capitalize_word( struct console *console )
  768. {
  769. struct edit_line *ctx = &console->edit_line;
  770. unsigned int new_offset = edit_line_right_word_transition( console, ctx->cursor );
  771. if (new_offset != ctx->cursor)
  772. {
  773. CharUpperBuffW( ctx->buf + ctx->cursor, 1 );
  774. CharLowerBuffW( ctx->buf + ctx->cursor + 1, new_offset - ctx->cursor );
  775. edit_line_update( console, ctx->cursor, new_offset - ctx->cursor + 1 );
  776. ctx->cursor = new_offset;
  777. }
  778. }
  779. static void edit_line_yank( struct console *console )
  780. {
  781. struct edit_line *ctx = &console->edit_line;
  782. if (ctx->yanked) edit_line_insert( console, ctx->yanked, wcslen(ctx->yanked) );
  783. }
  784. static void edit_line_kill_suffix( struct console *console )
  785. {
  786. struct edit_line *ctx = &console->edit_line;
  787. edit_line_save_yank( console, ctx->cursor, ctx->len );
  788. edit_line_delete( console, ctx->cursor, ctx->len );
  789. }
  790. static void edit_line_kill_prefix( struct console *console )
  791. {
  792. struct edit_line *ctx = &console->edit_line;
  793. if (ctx->cursor)
  794. {
  795. edit_line_save_yank( console, 0, ctx->cursor );
  796. edit_line_delete( console, 0, ctx->cursor );
  797. ctx->cursor = 0;
  798. }
  799. }
  800. static void edit_line_kill_marked_zone( struct console *console )
  801. {
  802. struct edit_line *ctx = &console->edit_line;
  803. unsigned int begin, end;
  804. if (ctx->mark > ctx->len || ctx->mark == ctx->cursor)
  805. return;
  806. if (ctx->mark > ctx->cursor)
  807. {
  808. begin = ctx->cursor;
  809. end = ctx->mark;
  810. }
  811. else
  812. {
  813. begin = ctx->mark;
  814. end = ctx->cursor;
  815. }
  816. edit_line_save_yank( console, begin, end );
  817. edit_line_delete( console, begin, end );
  818. ctx->cursor = begin;
  819. }
  820. static void edit_line_delete_prev( struct console *console )
  821. {
  822. struct edit_line *ctx = &console->edit_line;
  823. if (ctx->cursor)
  824. {
  825. edit_line_delete( console, ctx->cursor - 1, ctx->cursor );
  826. ctx->cursor--;
  827. }
  828. }
  829. static void edit_line_delete_char( struct console *console )
  830. {
  831. struct edit_line *ctx = &console->edit_line;
  832. if (ctx->cursor < ctx->len)
  833. edit_line_delete( console, ctx->cursor, ctx->cursor + 1 );
  834. }
  835. static void edit_line_delete_left_word( struct console *console )
  836. {
  837. struct edit_line *ctx = &console->edit_line;
  838. unsigned int new_offset = edit_line_left_word_transition( console, ctx->cursor );
  839. if (new_offset != ctx->cursor)
  840. {
  841. edit_line_delete( console, new_offset, ctx->cursor );
  842. ctx->cursor = new_offset;
  843. }
  844. }
  845. static void edit_line_delete_right_word( struct console *console )
  846. {
  847. struct edit_line *ctx = &console->edit_line;
  848. unsigned int new_offset = edit_line_right_word_transition( console, ctx->cursor );
  849. if (new_offset != ctx->cursor)
  850. {
  851. edit_line_delete( console, ctx->cursor, new_offset );
  852. }
  853. }
  854. static void edit_line_move_to_prev_hist( struct console *console )
  855. {
  856. if (console->edit_line.history_index)
  857. edit_line_move_to_history( console, console->edit_line.history_index - 1 );
  858. }
  859. static void edit_line_move_to_next_hist( struct console *console )
  860. {
  861. if (console->edit_line.history_index < console->history_index)
  862. edit_line_move_to_history( console, console->edit_line.history_index + 1 );
  863. }
  864. static void edit_line_move_to_first_hist( struct console *console )
  865. {
  866. if (console->edit_line.history_index)
  867. edit_line_move_to_history( console, 0 );
  868. }
  869. static void edit_line_move_to_last_hist( struct console *console )
  870. {
  871. if (console->edit_line.history_index != console->history_index)
  872. edit_line_move_to_history( console, console->history_index );
  873. }
  874. static void edit_line_redraw( struct console *console )
  875. {
  876. if (console->mode & ENABLE_ECHO_INPUT)
  877. edit_line_update( console, 0, console->edit_line.len );
  878. }
  879. static void edit_line_toggle_insert( struct console *console )
  880. {
  881. struct edit_line *ctx = &console->edit_line;
  882. ctx->insert_key = !ctx->insert_key;
  883. console->active->cursor_size = ctx->insert_key ? 100 : 25;
  884. }
  885. static void edit_line_done( struct console *console )
  886. {
  887. console->edit_line.status = STATUS_SUCCESS;
  888. }
  889. struct edit_line_key_entry
  890. {
  891. WCHAR val; /* vk or unicode char */
  892. void (*func)( struct console *console );
  893. };
  894. struct edit_line_key_map
  895. {
  896. DWORD key_state; /* keyState (from INPUT_RECORD) to match */
  897. BOOL is_char; /* check vk or char */
  898. const struct edit_line_key_entry *entries;
  899. };
  900. #define CTRL(x) ((x) - '@')
  901. static const struct edit_line_key_entry std_key_map[] =
  902. {
  903. { VK_BACK, edit_line_delete_prev },
  904. { VK_RETURN, edit_line_done },
  905. { VK_DELETE, edit_line_delete_char },
  906. { 0 }
  907. };
  908. static const struct edit_line_key_entry emacs_key_map_ctrl[] =
  909. {
  910. { CTRL('@'), edit_line_set_mark },
  911. { CTRL('A'), edit_line_move_home },
  912. { CTRL('B'), edit_line_move_left },
  913. { CTRL('D'), edit_line_delete_char },
  914. { CTRL('E'), edit_line_move_end },
  915. { CTRL('F'), edit_line_move_right },
  916. { CTRL('H'), edit_line_delete_prev },
  917. { CTRL('J'), edit_line_done },
  918. { CTRL('K'), edit_line_kill_suffix },
  919. { CTRL('L'), edit_line_redraw },
  920. { CTRL('M'), edit_line_done },
  921. { CTRL('N'), edit_line_move_to_next_hist },
  922. { CTRL('P'), edit_line_move_to_prev_hist },
  923. { CTRL('T'), edit_line_transpose_char },
  924. { CTRL('W'), edit_line_kill_marked_zone },
  925. { CTRL('X'), edit_line_exchange_mark },
  926. { CTRL('Y'), edit_line_yank },
  927. { 0 }
  928. };
  929. static const struct edit_line_key_entry emacs_key_map_alt[] =
  930. {
  931. { 0x7f, edit_line_delete_left_word },
  932. { '<', edit_line_move_to_first_hist },
  933. { '>', edit_line_move_to_last_hist },
  934. { 'b', edit_line_move_left_word },
  935. { 'c', edit_line_capitalize_word },
  936. { 'd', edit_line_delete_right_word },
  937. { 'f', edit_line_move_right_word },
  938. { 'l', edit_line_lower_case_word },
  939. { 't', edit_line_transpose_words },
  940. { 'u', edit_line_upper_case_word },
  941. { 'w', edit_line_copy_marked_zone },
  942. { 0 }
  943. };
  944. static const struct edit_line_key_entry emacs_std_key_map[] =
  945. {
  946. { VK_PRIOR, edit_line_move_to_prev_hist },
  947. { VK_NEXT, edit_line_move_to_next_hist },
  948. { VK_END, edit_line_move_end },
  949. { VK_HOME, edit_line_move_home },
  950. { VK_RIGHT, edit_line_move_right },
  951. { VK_LEFT, edit_line_move_left },
  952. { VK_INSERT, edit_line_toggle_insert },
  953. { 0 }
  954. };
  955. static const struct edit_line_key_map emacs_key_map[] =
  956. {
  957. { 0, 0, std_key_map },
  958. { 0, 0, emacs_std_key_map },
  959. { RIGHT_ALT_PRESSED, 1, emacs_key_map_alt },
  960. { LEFT_ALT_PRESSED, 1, emacs_key_map_alt },
  961. { RIGHT_CTRL_PRESSED, 1, emacs_key_map_ctrl },
  962. { LEFT_CTRL_PRESSED, 1, emacs_key_map_ctrl },
  963. { 0 }
  964. };
  965. static const struct edit_line_key_entry win32_std_key_map[] =
  966. {
  967. { VK_LEFT, edit_line_move_left },
  968. { VK_RIGHT, edit_line_move_right },
  969. { VK_HOME, edit_line_move_home },
  970. { VK_END, edit_line_move_end },
  971. { VK_UP, edit_line_move_to_prev_hist },
  972. { VK_DOWN, edit_line_move_to_next_hist },
  973. { VK_INSERT, edit_line_toggle_insert },
  974. { VK_F8, edit_line_find_in_history },
  975. { 0 }
  976. };
  977. static const struct edit_line_key_entry win32_key_map_ctrl[] =
  978. {
  979. { VK_LEFT, edit_line_move_left_word },
  980. { VK_RIGHT, edit_line_move_right_word },
  981. { VK_END, edit_line_kill_suffix },
  982. { VK_HOME, edit_line_kill_prefix },
  983. { 0 }
  984. };
  985. static const struct edit_line_key_map win32_key_map[] =
  986. {
  987. { 0, 0, std_key_map },
  988. { SHIFT_PRESSED, 0, std_key_map },
  989. { 0, 0, win32_std_key_map },
  990. { RIGHT_CTRL_PRESSED, 0, win32_key_map_ctrl },
  991. { LEFT_CTRL_PRESSED, 0, win32_key_map_ctrl },
  992. { 0 }
  993. };
  994. #undef CTRL
  995. static unsigned int edit_line_string_width( const WCHAR *str, unsigned int len)
  996. {
  997. unsigned int i, offset = 0;
  998. for (i = 0; i < len; i++) offset += str[i] < ' ' ? 2 : 1;
  999. return offset;
  1000. }
  1001. static void update_read_output( struct console *console )
  1002. {
  1003. struct screen_buffer *screen_buffer = console->active;
  1004. struct edit_line *ctx = &console->edit_line;
  1005. int offset = 0, j, end_offset;
  1006. RECT update_rect;
  1007. empty_update_rect( screen_buffer, &update_rect );
  1008. if (ctx->update_end >= ctx->update_begin)
  1009. {
  1010. TRACE( "update %d-%d %s\n", ctx->update_begin, ctx->update_end,
  1011. debugstr_wn( ctx->buf + ctx->update_begin, ctx->update_end - ctx->update_begin + 1 ));
  1012. hide_tty_cursor( screen_buffer->console );
  1013. offset = edit_line_string_width( ctx->buf, ctx->update_begin );
  1014. screen_buffer->cursor_x = (ctx->home_x + offset) % screen_buffer->width;
  1015. screen_buffer->cursor_y = ctx->home_y + (ctx->home_x + offset) / screen_buffer->width;
  1016. for (j = ctx->update_begin; j <= ctx->update_end; j++)
  1017. {
  1018. if (screen_buffer->cursor_y >= screen_buffer->height && !ctx->home_y) break;
  1019. if (j >= ctx->len) break;
  1020. if (ctx->buf[j] < ' ')
  1021. {
  1022. write_char( screen_buffer, '^', &update_rect, &ctx->home_y );
  1023. write_char( screen_buffer, '@' + ctx->buf[j], &update_rect, &ctx->home_y );
  1024. offset += 2;
  1025. }
  1026. else
  1027. {
  1028. write_char( screen_buffer, ctx->buf[j], &update_rect, &ctx->home_y );
  1029. offset++;
  1030. }
  1031. }
  1032. end_offset = ctx->end_offset;
  1033. ctx->end_offset = offset;
  1034. if (j >= ctx->len)
  1035. {
  1036. /* clear trailing characters if buffer was shortened */
  1037. while (offset < end_offset && screen_buffer->cursor_y < screen_buffer->height)
  1038. {
  1039. write_char( screen_buffer, ' ', &update_rect, &ctx->home_y );
  1040. offset++;
  1041. }
  1042. }
  1043. }
  1044. if (!ctx->status)
  1045. {
  1046. offset = edit_line_string_width( ctx->buf, ctx->len );
  1047. screen_buffer->cursor_x = 0;
  1048. screen_buffer->cursor_y = ctx->home_y + (ctx->home_x + offset) / screen_buffer->width;
  1049. if (++screen_buffer->cursor_y >= screen_buffer->height)
  1050. new_line( screen_buffer, &update_rect );
  1051. }
  1052. else
  1053. {
  1054. offset = edit_line_string_width( ctx->buf, ctx->cursor );
  1055. screen_buffer->cursor_y = ctx->home_y + (ctx->home_x + offset) / screen_buffer->width;
  1056. if (screen_buffer->cursor_y < screen_buffer->height)
  1057. {
  1058. screen_buffer->cursor_x = (ctx->home_x + offset) % screen_buffer->width;
  1059. }
  1060. else
  1061. {
  1062. screen_buffer->cursor_x = screen_buffer->width - 1;
  1063. screen_buffer->cursor_y = screen_buffer->height - 1;
  1064. }
  1065. }
  1066. /* always try to use relative cursor positions in UNIX mode so that it works even if cursor
  1067. * position is out of sync */
  1068. if (update_rect.left <= update_rect.right && update_rect.top <= update_rect.bottom)
  1069. {
  1070. if (console->is_unix)
  1071. set_tty_cursor_relative( screen_buffer->console, update_rect.left, update_rect.top );
  1072. update_output( screen_buffer, &update_rect );
  1073. scroll_to_cursor( screen_buffer );
  1074. }
  1075. if (console->is_unix)
  1076. set_tty_cursor_relative( screen_buffer->console, screen_buffer->cursor_x, screen_buffer->cursor_y );
  1077. tty_sync( screen_buffer->console );
  1078. update_window_config( screen_buffer->console, TRUE );
  1079. }
  1080. static NTSTATUS process_console_input( struct console *console )
  1081. {
  1082. struct edit_line *ctx = &console->edit_line;
  1083. unsigned int i;
  1084. switch (console->read_ioctl)
  1085. {
  1086. case IOCTL_CONDRV_READ_INPUT:
  1087. if (console->record_count) read_console_input( console, console->pending_read );
  1088. return STATUS_SUCCESS;
  1089. case IOCTL_CONDRV_READ_CONSOLE:
  1090. case IOCTL_CONDRV_READ_FILE:
  1091. break;
  1092. default:
  1093. assert( !console->read_ioctl );
  1094. if (console->record_count && !console->signaled)
  1095. read_complete( console, STATUS_PENDING, NULL, 0, TRUE ); /* signal server */
  1096. return STATUS_SUCCESS;
  1097. }
  1098. ctx->update_begin = ctx->len + 1;
  1099. ctx->update_end = 0;
  1100. for (i = 0; i < console->record_count && ctx->status == STATUS_PENDING; i++)
  1101. {
  1102. void (*func)( struct console *console ) = NULL;
  1103. INPUT_RECORD ir = console->records[i];
  1104. if (ir.EventType != KEY_EVENT || !ir.Event.KeyEvent.bKeyDown) continue;
  1105. TRACE( "key code=%02x scan=%02x char=%02x state=%08x\n",
  1106. ir.Event.KeyEvent.wVirtualKeyCode, ir.Event.KeyEvent.wVirtualScanCode,
  1107. ir.Event.KeyEvent.uChar.UnicodeChar, ir.Event.KeyEvent.dwControlKeyState );
  1108. if (console->mode & ENABLE_LINE_INPUT)
  1109. {
  1110. const struct edit_line_key_entry *entry;
  1111. const struct edit_line_key_map *map;
  1112. unsigned int state;
  1113. /* mask out some bits which don't interest us */
  1114. state = ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON|ENHANCED_KEY);
  1115. func = NULL;
  1116. for (map = console->edition_mode ? emacs_key_map : win32_key_map; map->entries != NULL; map++)
  1117. {
  1118. if (map->key_state != state)
  1119. continue;
  1120. if (map->is_char)
  1121. {
  1122. for (entry = &map->entries[0]; entry->func != 0; entry++)
  1123. if (entry->val == ir.Event.KeyEvent.uChar.UnicodeChar) break;
  1124. }
  1125. else
  1126. {
  1127. for (entry = &map->entries[0]; entry->func != 0; entry++)
  1128. if (entry->val == ir.Event.KeyEvent.wVirtualKeyCode) break;
  1129. }
  1130. if (entry->func)
  1131. {
  1132. func = entry->func;
  1133. break;
  1134. }
  1135. }
  1136. }
  1137. ctx->insert_mode = ((console->mode & (ENABLE_INSERT_MODE | ENABLE_EXTENDED_FLAGS)) ==
  1138. (ENABLE_INSERT_MODE | ENABLE_EXTENDED_FLAGS))
  1139. ^ ctx->insert_key;
  1140. if (func) func( console );
  1141. else if (ir.Event.KeyEvent.uChar.UnicodeChar)
  1142. edit_line_insert( console, &ir.Event.KeyEvent.uChar.UnicodeChar, 1 );
  1143. if (!(console->mode & ENABLE_LINE_INPUT) && ctx->status == STATUS_PENDING)
  1144. {
  1145. if (console->read_ioctl == IOCTL_CONDRV_READ_FILE)
  1146. {
  1147. if (WideCharToMultiByte(console->input_cp, 0, ctx->buf, ctx->len, NULL, 0, NULL, NULL)
  1148. >= console->pending_read)
  1149. ctx->status = STATUS_SUCCESS;
  1150. }
  1151. else if (ctx->len >= console->pending_read / sizeof(WCHAR))
  1152. ctx->status = STATUS_SUCCESS;
  1153. }
  1154. }
  1155. if (console->record_count > i) memmove( console->records, console->records + i,
  1156. (console->record_count - i) * sizeof(*console->records) );
  1157. console->record_count -= i;
  1158. if (ctx->status == STATUS_PENDING && !(console->mode & ENABLE_LINE_INPUT) && ctx->len)
  1159. ctx->status = STATUS_SUCCESS;
  1160. if (console->mode & ENABLE_ECHO_INPUT) update_read_output( console );
  1161. if (ctx->status == STATUS_PENDING) return STATUS_SUCCESS;
  1162. if (!ctx->status && (console->mode & ENABLE_LINE_INPUT))
  1163. {
  1164. if (ctx->len) append_input_history( console, ctx->buf, ctx->len * sizeof(WCHAR) );
  1165. if (edit_line_grow(console, 2))
  1166. {
  1167. ctx->buf[ctx->len++] = '\r';
  1168. ctx->buf[ctx->len++] = '\n';
  1169. ctx->buf[ctx->len] = 0;
  1170. TRACE( "return %s\n", debugstr_wn( ctx->buf, ctx->len ));
  1171. }
  1172. }
  1173. console->read_buffer = ctx->buf;
  1174. console->read_buffer_count = ctx->len;
  1175. console->read_buffer_size = ctx->size;
  1176. if (ctx->status) read_complete( console, ctx->status, NULL, 0, console->record_count );
  1177. else read_from_buffer( console, console->pending_read );
  1178. /* reset context */
  1179. free( ctx->yanked );
  1180. free( ctx->current_history );
  1181. memset( &console->edit_line, 0, sizeof(console->edit_line) );
  1182. return STATUS_SUCCESS;
  1183. }
  1184. static NTSTATUS read_console( struct console *console, unsigned int ioctl, size_t out_size )
  1185. {
  1186. TRACE("\n");
  1187. if (out_size > INT_MAX)
  1188. {
  1189. read_complete( console, STATUS_NO_MEMORY, NULL, 0, console->record_count );
  1190. return STATUS_NO_MEMORY;
  1191. }
  1192. console->read_ioctl = ioctl;
  1193. if (!out_size || console->read_buffer_count)
  1194. {
  1195. read_from_buffer( console, out_size );
  1196. return STATUS_SUCCESS;
  1197. }
  1198. console->edit_line.history_index = console->history_index;
  1199. console->edit_line.home_x = console->active->cursor_x;
  1200. console->edit_line.home_y = console->active->cursor_y;
  1201. console->edit_line.status = STATUS_PENDING;
  1202. if (edit_line_grow( console, 1 )) console->edit_line.buf[0] = 0;
  1203. console->pending_read = out_size;
  1204. return process_console_input( console );
  1205. }
  1206. /* add input events to a console input queue */
  1207. NTSTATUS write_console_input( struct console *console, const INPUT_RECORD *records,
  1208. unsigned int count, BOOL flush )
  1209. {
  1210. TRACE( "%u\n", count );
  1211. if (!count) return STATUS_SUCCESS;
  1212. if (console->record_count + count > console->record_size)
  1213. {
  1214. INPUT_RECORD *new_rec;
  1215. if (!(new_rec = realloc( console->records, (console->record_size * 2 + count) * sizeof(INPUT_RECORD) )))
  1216. return STATUS_NO_MEMORY;
  1217. console->records = new_rec;
  1218. console->record_size = console->record_size * 2 + count;
  1219. }
  1220. memcpy( console->records + console->record_count, records, count * sizeof(INPUT_RECORD) );
  1221. if (console->mode & ENABLE_PROCESSED_INPUT)
  1222. {
  1223. unsigned int i = 0;
  1224. while (i < count)
  1225. {
  1226. if (records[i].EventType == KEY_EVENT &&
  1227. records[i].Event.KeyEvent.uChar.UnicodeChar == 'C' - 64 &&
  1228. !(records[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
  1229. {
  1230. if (i != count - 1)
  1231. memcpy( &console->records[console->record_count + i],
  1232. &console->records[console->record_count + i + 1],
  1233. (count - i - 1) * sizeof(INPUT_RECORD) );
  1234. count--;
  1235. if (records[i].Event.KeyEvent.bKeyDown)
  1236. {
  1237. struct condrv_ctrl_event ctrl_event;
  1238. IO_STATUS_BLOCK io;
  1239. ctrl_event.event = CTRL_C_EVENT;
  1240. ctrl_event.group_id = 0;
  1241. NtDeviceIoControlFile( console->server, NULL, NULL, NULL, &io, IOCTL_CONDRV_CTRL_EVENT,
  1242. &ctrl_event, sizeof(ctrl_event), NULL, 0 );
  1243. }
  1244. }
  1245. else i++;
  1246. }
  1247. }
  1248. console->record_count += count;
  1249. return flush ? process_console_input( console ) : STATUS_SUCCESS;
  1250. }
  1251. static void set_key_input_record( INPUT_RECORD *record, WCHAR ch, unsigned int vk, BOOL is_down, unsigned int ctrl_state )
  1252. {
  1253. record->EventType = KEY_EVENT;
  1254. record->Event.KeyEvent.bKeyDown = is_down;
  1255. record->Event.KeyEvent.wRepeatCount = 1;
  1256. record->Event.KeyEvent.uChar.UnicodeChar = ch;
  1257. record->Event.KeyEvent.wVirtualKeyCode = vk;
  1258. record->Event.KeyEvent.wVirtualScanCode = MapVirtualKeyW( vk, MAPVK_VK_TO_VSC );
  1259. record->Event.KeyEvent.dwControlKeyState = ctrl_state;
  1260. }
  1261. static NTSTATUS key_press( struct console *console, WCHAR ch, unsigned int vk, unsigned int ctrl_state )
  1262. {
  1263. INPUT_RECORD records[8];
  1264. unsigned int count = 0, ctrl = 0;
  1265. if (ctrl_state & SHIFT_PRESSED)
  1266. {
  1267. ctrl |= SHIFT_PRESSED;
  1268. set_key_input_record( &records[count++], 0, VK_SHIFT, TRUE, ctrl );
  1269. }
  1270. if (ctrl_state & LEFT_ALT_PRESSED)
  1271. {
  1272. ctrl |= LEFT_ALT_PRESSED;
  1273. set_key_input_record( &records[count++], 0, VK_MENU, TRUE, ctrl );
  1274. }
  1275. if (ctrl_state & LEFT_CTRL_PRESSED)
  1276. {
  1277. ctrl |= LEFT_CTRL_PRESSED;
  1278. set_key_input_record( &records[count++], 0, VK_CONTROL, TRUE, ctrl );
  1279. }
  1280. set_key_input_record( &records[count++], ch, vk, TRUE, ctrl );
  1281. set_key_input_record( &records[count++], ch, vk, FALSE, ctrl );
  1282. if (ctrl & LEFT_CTRL_PRESSED)
  1283. {
  1284. ctrl &= ~LEFT_CTRL_PRESSED;
  1285. set_key_input_record( &records[count++], 0, VK_CONTROL, FALSE, ctrl );
  1286. }
  1287. if (ctrl & LEFT_ALT_PRESSED)
  1288. {
  1289. ctrl &= ~LEFT_ALT_PRESSED;
  1290. set_key_input_record( &records[count++], 0, VK_MENU, FALSE, ctrl );
  1291. }
  1292. if (ctrl & SHIFT_PRESSED)
  1293. {
  1294. ctrl &= ~SHIFT_PRESSED;
  1295. set_key_input_record( &records[count++], 0, VK_SHIFT, FALSE, ctrl );
  1296. }
  1297. return write_console_input( console, records, count, FALSE );
  1298. }
  1299. static void char_key_press( struct console *console, WCHAR ch, unsigned int ctrl )
  1300. {
  1301. unsigned int vk = VkKeyScanW( ch );
  1302. if (vk == ~0) vk = 0;
  1303. if (vk & 0x0100) ctrl |= SHIFT_PRESSED;
  1304. if (vk & 0x0200) ctrl |= LEFT_CTRL_PRESSED;
  1305. if (vk & 0x0400) ctrl |= LEFT_ALT_PRESSED;
  1306. vk &= 0xff;
  1307. key_press( console, ch, vk, ctrl );
  1308. }
  1309. static unsigned int escape_char_to_vk( WCHAR ch )
  1310. {
  1311. switch (ch)
  1312. {
  1313. case 'A': return VK_UP;
  1314. case 'B': return VK_DOWN;
  1315. case 'C': return VK_RIGHT;
  1316. case 'D': return VK_LEFT;
  1317. case 'H': return VK_HOME;
  1318. case 'F': return VK_END;
  1319. case 'P': return VK_F1;
  1320. case 'Q': return VK_F2;
  1321. case 'R': return VK_F3;
  1322. case 'S': return VK_F4;
  1323. default: return 0;
  1324. }
  1325. }
  1326. static unsigned int escape_number_to_vk( unsigned int n )
  1327. {
  1328. switch(n)
  1329. {
  1330. case 2: return VK_INSERT;
  1331. case 3: return VK_DELETE;
  1332. case 5: return VK_PRIOR;
  1333. case 6: return VK_NEXT;
  1334. case 15: return VK_F5;
  1335. case 17: return VK_F6;
  1336. case 18: return VK_F7;
  1337. case 19: return VK_F8;
  1338. case 20: return VK_F9;
  1339. case 21: return VK_F10;
  1340. case 23: return VK_F11;
  1341. case 24: return VK_F12;
  1342. default: return 0;
  1343. }
  1344. }
  1345. static unsigned int convert_modifiers( unsigned int n )
  1346. {
  1347. unsigned int ctrl = 0;
  1348. if (!n || n > 16) return 0;
  1349. n--;
  1350. if (n & 1) ctrl |= SHIFT_PRESSED;
  1351. if (n & 2) ctrl |= LEFT_ALT_PRESSED;
  1352. if (n & 4) ctrl |= LEFT_CTRL_PRESSED;
  1353. return ctrl;
  1354. }
  1355. static unsigned int process_csi_sequence( struct console *console, const WCHAR *buf, size_t size )
  1356. {
  1357. unsigned int n, count = 0, params[8], params_cnt = 0, vk;
  1358. for (;;)
  1359. {
  1360. n = 0;
  1361. while (count < size && '0' <= buf[count] && buf[count] <= '9')
  1362. n = n * 10 + buf[count++] - '0';
  1363. if (params_cnt < ARRAY_SIZE(params)) params[params_cnt++] = n;
  1364. else FIXME( "too many params, skipping %u\n", n );
  1365. if (count == size) return 0;
  1366. if (buf[count] != ';') break;
  1367. if (++count == size) return 0;
  1368. }
  1369. if ((vk = escape_char_to_vk( buf[count] )))
  1370. {
  1371. key_press( console, 0, vk, params_cnt >= 2 ? convert_modifiers( params[1] ) : 0 );
  1372. return count + 1;
  1373. }
  1374. switch (buf[count])
  1375. {
  1376. case '~':
  1377. vk = escape_number_to_vk( params[0] );
  1378. key_press( console, 0, vk, params_cnt == 2 ? convert_modifiers( params[1] ) : 0 );
  1379. return count + 1;
  1380. default:
  1381. FIXME( "unhandled sequence %s\n", debugstr_wn( buf, size ));
  1382. return 0;
  1383. }
  1384. }
  1385. static unsigned int process_input_escape( struct console *console, const WCHAR *buf, size_t size )
  1386. {
  1387. unsigned int vk = 0, count = 0, nlen;
  1388. if (!size)
  1389. {
  1390. key_press( console, 0, VK_ESCAPE, 0 );
  1391. return 0;
  1392. }
  1393. switch(buf[0])
  1394. {
  1395. case '[':
  1396. if (++count == size) break;
  1397. if ((nlen = process_csi_sequence( console, buf + 1, size - 1 ))) return count + nlen;
  1398. break;
  1399. case 'O':
  1400. if (++count == size) break;
  1401. vk = escape_char_to_vk( buf[1] );
  1402. if (vk)
  1403. {
  1404. key_press( console, 0, vk, 0 );
  1405. return count + 1;
  1406. }
  1407. }
  1408. char_key_press( console, buf[0], LEFT_ALT_PRESSED );
  1409. return 1;
  1410. }
  1411. static DWORD WINAPI tty_input( void *param )
  1412. {
  1413. struct console *console = param;
  1414. IO_STATUS_BLOCK io;
  1415. HANDLE event;
  1416. char read_buf[4096];
  1417. WCHAR buf[4096];
  1418. DWORD count, i;
  1419. BOOL signaled;
  1420. NTSTATUS status;
  1421. if (console->is_unix)
  1422. {
  1423. unsigned int h = condrv_handle( console->tty_input );
  1424. status = NtDeviceIoControlFile( console->server, NULL, NULL, NULL, &io, IOCTL_CONDRV_SETUP_INPUT,
  1425. &h, sizeof(h), NULL, 0 );
  1426. if (status) ERR( "input setup failed: %#x\n", status );
  1427. }
  1428. event = CreateEventW( NULL, TRUE, FALSE, NULL );
  1429. for (;;)
  1430. {
  1431. status = NtReadFile( console->tty_input, event, NULL, NULL, &io, read_buf, sizeof(read_buf), NULL, NULL );
  1432. if (status == STATUS_PENDING)
  1433. {
  1434. if ((status = NtWaitForSingleObject( event, FALSE, NULL ))) break;
  1435. status = io.Status;
  1436. }
  1437. if (status) break;
  1438. EnterCriticalSection( &console_section );
  1439. signaled = console->record_count != 0;
  1440. /* FIXME: Handle partial char read */
  1441. count = MultiByteToWideChar( get_tty_cp( console ), 0, read_buf, io.Information, buf, ARRAY_SIZE(buf) );
  1442. TRACE( "%s\n", debugstr_wn(buf, count) );
  1443. for (i = 0; i < count; i++)
  1444. {
  1445. WCHAR ch = buf[i];
  1446. switch (ch)
  1447. {
  1448. case 3: /* end of text */
  1449. LeaveCriticalSection( &console_section );
  1450. goto done;
  1451. case '\n':
  1452. key_press( console, '\n', VK_RETURN, LEFT_CTRL_PRESSED );
  1453. break;
  1454. case '\b':
  1455. key_press( console, ch, 'H', LEFT_CTRL_PRESSED );
  1456. break;
  1457. case 0x1b:
  1458. i += process_input_escape( console, buf + i + 1, count - i - 1 );
  1459. break;
  1460. case 0x7f:
  1461. key_press( console, '\b', VK_BACK, 0 );
  1462. break;
  1463. default:
  1464. char_key_press( console, ch, 0 );
  1465. }
  1466. }
  1467. process_console_input( console );
  1468. if (!signaled && console->record_count)
  1469. {
  1470. assert( !console->read_ioctl );
  1471. read_complete( console, STATUS_SUCCESS, NULL, 0, TRUE ); /* signal console */
  1472. }
  1473. LeaveCriticalSection( &console_section );
  1474. }
  1475. TRACE( "NtReadFile failed: %#x\n", status );
  1476. done:
  1477. EnterCriticalSection( &console_section );
  1478. if (console->read_ioctl) read_complete( console, status, NULL, 0, FALSE );
  1479. if (console->is_unix)
  1480. {
  1481. unsigned int h = 0;
  1482. status = NtDeviceIoControlFile( console->server, NULL, NULL, NULL, &io, IOCTL_CONDRV_SETUP_INPUT,
  1483. &h, sizeof(h), NULL, 0 );
  1484. if (status) ERR( "input restore failed: %#x\n", status );
  1485. }
  1486. CloseHandle( console->input_thread );
  1487. console->input_thread = NULL;
  1488. LeaveCriticalSection( &console_section );
  1489. return 0;
  1490. }
  1491. static BOOL ensure_tty_input_thread( struct console *console )
  1492. {
  1493. if (!console->tty_input) return TRUE;
  1494. if (!console->input_thread)
  1495. console->input_thread = CreateThread( NULL, 0, tty_input, console, 0, NULL );
  1496. return console->input_thread != NULL;
  1497. }
  1498. static NTSTATUS screen_buffer_activate( struct screen_buffer *screen_buffer )
  1499. {
  1500. RECT update_rect;
  1501. TRACE( "%p\n", screen_buffer );
  1502. screen_buffer->console->active = screen_buffer;
  1503. SetRect( &update_rect, 0, 0, screen_buffer->width - 1, screen_buffer->height - 1 );
  1504. update_output( screen_buffer, &update_rect );
  1505. tty_sync( screen_buffer->console );
  1506. update_window_config( screen_buffer->console, FALSE );
  1507. return STATUS_SUCCESS;
  1508. }
  1509. static NTSTATUS get_output_info( struct screen_buffer *screen_buffer, size_t *out_size )
  1510. {
  1511. struct condrv_output_info *info;
  1512. *out_size = min( *out_size, sizeof(*info) + screen_buffer->font.face_len );
  1513. if (!(info = alloc_ioctl_buffer( *out_size ))) return STATUS_NO_MEMORY;
  1514. info->cursor_size = screen_buffer->cursor_size;
  1515. info->cursor_visible = screen_buffer->cursor_visible;
  1516. info->cursor_x = get_bounded_cursor_x( screen_buffer );
  1517. info->cursor_y = screen_buffer->cursor_y;
  1518. info->width = screen_buffer->width;
  1519. info->height = screen_buffer->height;
  1520. info->attr = screen_buffer->attr;
  1521. info->popup_attr = screen_buffer->popup_attr;
  1522. info->win_left = screen_buffer->win.left;
  1523. info->win_top = screen_buffer->win.top;
  1524. info->win_right = screen_buffer->win.right;
  1525. info->win_bottom = screen_buffer->win.bottom;
  1526. info->max_width = screen_buffer->max_width;
  1527. info->max_height = screen_buffer->max_height;
  1528. info->font_width = screen_buffer->font.width;
  1529. info->font_height = screen_buffer->font.height;
  1530. info->font_weight = screen_buffer->font.weight;
  1531. info->font_pitch_family = screen_buffer->font.pitch_family;
  1532. memcpy( info->color_map, screen_buffer->color_map, sizeof(info->color_map) );
  1533. if (*out_size > sizeof(*info)) memcpy( info + 1, screen_buffer->font.face_name, *out_size - sizeof(*info) );
  1534. TRACE( "%p cursor_size=%u cursor_visible=%x cursor=(%u,%u) width=%u height=%u win=%s attr=%x popup_attr=%x"
  1535. " font_width=%u font_height=%u %s\n", screen_buffer, info->cursor_size, info->cursor_visible,
  1536. info->cursor_x, info->cursor_y, info->width, info->height, wine_dbgstr_rect(&screen_buffer->win),
  1537. info->attr, info->popup_attr, info->font_width, info->font_height,
  1538. debugstr_wn( (const WCHAR *)(info + 1), (*out_size - sizeof(*info)) / sizeof(WCHAR) ) );
  1539. return STATUS_SUCCESS;
  1540. }
  1541. void notify_screen_buffer_size( struct screen_buffer *screen_buffer )
  1542. {
  1543. if (is_active( screen_buffer ) && screen_buffer->console->mode & ENABLE_WINDOW_INPUT)
  1544. {
  1545. INPUT_RECORD ir;
  1546. ir.EventType = WINDOW_BUFFER_SIZE_EVENT;
  1547. ir.Event.WindowBufferSizeEvent.dwSize.X = screen_buffer->width;
  1548. ir.Event.WindowBufferSizeEvent.dwSize.Y = screen_buffer->height;
  1549. write_console_input( screen_buffer->console, &ir, 1, TRUE );
  1550. }
  1551. }
  1552. NTSTATUS change_screen_buffer_size( struct screen_buffer *screen_buffer, int new_width, int new_height )
  1553. {
  1554. int i, old_width, old_height, copy_width, copy_height;
  1555. char_info_t *new_data;
  1556. if (!(new_data = malloc( new_width * new_height * sizeof(*new_data) ))) return STATUS_NO_MEMORY;
  1557. old_width = screen_buffer->width;
  1558. old_height = screen_buffer->height;
  1559. copy_width = min( old_width, new_width );
  1560. copy_height = min( old_height, new_height );
  1561. /* copy all the rows */
  1562. for (i = 0; i < copy_height; i++)
  1563. {
  1564. memcpy( &new_data[i * new_width], &screen_buffer->data[i * old_width],
  1565. copy_width * sizeof(char_info_t) );
  1566. }
  1567. /* clear the end of each row */
  1568. if (new_width > old_width)
  1569. {
  1570. /* fill first row */
  1571. for (i = old_width; i < new_width; i++) new_data[i] = empty_char_info;
  1572. /* and blast it to the other rows */
  1573. for (i = 1; i < copy_height; i++)
  1574. memcpy( &new_data[i * new_width + old_width], &new_data[old_width],
  1575. (new_width - old_width) * sizeof(char_info_t) );
  1576. }
  1577. /* clear remaining rows */
  1578. if (new_height > old_height)
  1579. {
  1580. /* fill first row */
  1581. for (i = 0; i < new_width; i++) new_data[old_height * new_width + i] = empty_char_info;
  1582. /* and blast it to the other rows */
  1583. for (i = old_height+1; i < new_height; i++)
  1584. memcpy( &new_data[i * new_width], &new_data[old_height * new_width],
  1585. new_width * sizeof(char_info_t) );
  1586. }
  1587. free( screen_buffer->data );
  1588. screen_buffer->data = new_data;
  1589. screen_buffer->width = new_width;
  1590. screen_buffer->height = new_height;
  1591. return STATUS_SUCCESS;
  1592. }
  1593. static NTSTATUS set_output_info( struct screen_buffer *screen_buffer,
  1594. const struct condrv_output_info_params *params, size_t extra_size )
  1595. {
  1596. const struct condrv_output_info *info = &params->info;
  1597. NTSTATUS status;
  1598. TRACE( "%p\n", screen_buffer );
  1599. extra_size -= sizeof(*params);
  1600. if (params->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM)
  1601. {
  1602. if (info->cursor_size < 1 || info->cursor_size > 100) return STATUS_INVALID_PARAMETER;
  1603. screen_buffer->cursor_size = info->cursor_size;
  1604. screen_buffer->cursor_visible = !!info->cursor_visible;
  1605. }
  1606. if (params->mask & SET_CONSOLE_OUTPUT_INFO_CURSOR_POS)
  1607. {
  1608. if (info->cursor_x < 0 || info->cursor_x >= screen_buffer->width ||
  1609. info->cursor_y < 0 || info->cursor_y >= screen_buffer->height)
  1610. {
  1611. return STATUS_INVALID_PARAMETER;
  1612. }
  1613. if (screen_buffer->cursor_x != info->cursor_x || screen_buffer->cursor_y != info->cursor_y)
  1614. {
  1615. screen_buffer->cursor_x = info->cursor_x;
  1616. screen_buffer->cursor_y = info->cursor_y;
  1617. scroll_to_cursor( screen_buffer );
  1618. }
  1619. }
  1620. if (params->mask & SET_CONSOLE_OUTPUT_INFO_SIZE)
  1621. {
  1622. /* new screen-buffer cannot be smaller than actual window */
  1623. if (info->width < screen_buffer->win.right - screen_buffer->win.left + 1 ||
  1624. info->height < screen_buffer->win.bottom - screen_buffer->win.top + 1)
  1625. {
  1626. return STATUS_INVALID_PARAMETER;
  1627. }
  1628. /* FIXME: there are also some basic minimum and max size to deal with */
  1629. if ((status = change_screen_buffer_size( screen_buffer, info->width, info->height ))) return status;
  1630. /* scroll window to display sb */
  1631. if (screen_buffer->win.right >= info->width)
  1632. {
  1633. screen_buffer->win.right -= screen_buffer->win.left;
  1634. screen_buffer->win.left = 0;
  1635. }
  1636. if (screen_buffer->win.bottom >= info->height)
  1637. {
  1638. screen_buffer->win.bottom -= screen_buffer->win.top;
  1639. screen_buffer->win.top = 0;
  1640. }
  1641. if (screen_buffer->cursor_x >= info->width) screen_buffer->cursor_x = info->width - 1;
  1642. if (screen_buffer->cursor_y >= info->height) screen_buffer->cursor_y = info->height - 1;
  1643. notify_screen_buffer_size( screen_buffer );
  1644. }
  1645. if (params->mask & SET_CONSOLE_OUTPUT_INFO_ATTR)
  1646. {
  1647. screen_buffer->attr = info->attr;
  1648. }
  1649. if (params->mask & SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR)
  1650. {
  1651. screen_buffer->popup_attr = info->popup_attr;
  1652. }
  1653. if (params->mask & SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW)
  1654. {
  1655. if (info->win_left < 0 || info->win_left > info->win_right ||
  1656. info->win_right >= screen_buffer->width ||
  1657. info->win_top < 0 || info->win_top > info->win_bottom ||
  1658. info->win_bottom >= screen_buffer->height)
  1659. {
  1660. return STATUS_INVALID_PARAMETER;
  1661. }
  1662. if (screen_buffer->win.left != info->win_left || screen_buffer->win.top != info->win_top ||
  1663. screen_buffer->win.right != info->win_right || screen_buffer->win.bottom != info->win_bottom)
  1664. {
  1665. screen_buffer->win.left = info->win_left;
  1666. screen_buffer->win.top = info->win_top;
  1667. screen_buffer->win.right = info->win_right;
  1668. screen_buffer->win.bottom = info->win_bottom;
  1669. }
  1670. }
  1671. if (params->mask & SET_CONSOLE_OUTPUT_INFO_MAX_SIZE)
  1672. {
  1673. screen_buffer->max_width = info->max_width;
  1674. screen_buffer->max_height = info->max_height;
  1675. }
  1676. if (is_active( screen_buffer ))
  1677. {
  1678. tty_sync( screen_buffer->console );
  1679. update_window_config( screen_buffer->console, FALSE );
  1680. }
  1681. return STATUS_SUCCESS;
  1682. }
  1683. static NTSTATUS write_console( struct screen_buffer *screen_buffer, const WCHAR *buffer, size_t len )
  1684. {
  1685. RECT update_rect;
  1686. size_t i, j;
  1687. TRACE( "%s\n", debugstr_wn(buffer, len) );
  1688. empty_update_rect( screen_buffer, &update_rect );
  1689. for (i = 0; i < len; i++)
  1690. {
  1691. if (screen_buffer->mode & ENABLE_PROCESSED_OUTPUT)
  1692. {
  1693. switch (buffer[i])
  1694. {
  1695. case '\b':
  1696. screen_buffer->cursor_x = get_bounded_cursor_x( screen_buffer );
  1697. if (screen_buffer->cursor_x) screen_buffer->cursor_x--;
  1698. continue;
  1699. case '\t':
  1700. j = min( screen_buffer->width - screen_buffer->cursor_x, 8 - (screen_buffer->cursor_x % 8) );
  1701. if (!j) j = 8;
  1702. while (j--) write_char( screen_buffer, ' ', &update_rect, NULL );
  1703. continue;
  1704. case '\n':
  1705. screen_buffer->cursor_x = 0;
  1706. if (++screen_buffer->cursor_y == screen_buffer->height)
  1707. new_line( screen_buffer, &update_rect );
  1708. else if (screen_buffer->mode & ENABLE_WRAP_AT_EOL_OUTPUT)
  1709. {
  1710. update_output( screen_buffer, &update_rect );
  1711. set_tty_cursor( screen_buffer->console, screen_buffer->cursor_x, screen_buffer->cursor_y );
  1712. }
  1713. continue;
  1714. case '\a':
  1715. FIXME( "beep\n" );
  1716. continue;
  1717. case '\r':
  1718. screen_buffer->cursor_x = 0;
  1719. continue;
  1720. }
  1721. }
  1722. if (screen_buffer->cursor_x == screen_buffer->width && !(screen_buffer->mode & ENABLE_WRAP_AT_EOL_OUTPUT))
  1723. screen_buffer->cursor_x = update_rect.left;
  1724. write_char( screen_buffer, buffer[i], &update_rect, NULL );
  1725. }
  1726. if (screen_buffer->cursor_x == screen_buffer->width)
  1727. {
  1728. if (screen_buffer->mode & ENABLE_WRAP_AT_EOL_OUTPUT)
  1729. {
  1730. if (!(screen_buffer->mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
  1731. {
  1732. screen_buffer->cursor_x = 0;
  1733. if (++screen_buffer->cursor_y == screen_buffer->height)
  1734. new_line( screen_buffer, &update_rect );
  1735. }
  1736. }
  1737. else screen_buffer->cursor_x = update_rect.left;
  1738. }
  1739. scroll_to_cursor( screen_buffer );
  1740. update_output( screen_buffer, &update_rect );
  1741. tty_sync( screen_buffer->console );
  1742. update_window_config( screen_buffer->console, TRUE );
  1743. return STATUS_SUCCESS;
  1744. }
  1745. static NTSTATUS write_output( struct screen_buffer *screen_buffer, const struct condrv_output_params *params,
  1746. size_t in_size, size_t *out_size )
  1747. {
  1748. unsigned int i, entry_size, entry_cnt, x, y;
  1749. char_info_t *dest;
  1750. char *src;
  1751. if (*out_size == sizeof(SMALL_RECT) && !params->width) return STATUS_INVALID_PARAMETER;
  1752. entry_size = params->mode == CHAR_INFO_MODE_TEXTATTR ? sizeof(char_info_t) : sizeof(WCHAR);
  1753. entry_cnt = (in_size - sizeof(*params)) / entry_size;
  1754. TRACE( "(%u,%u) cnt %u\n", params->x, params->y, entry_cnt );
  1755. if (params->x >= screen_buffer->width)
  1756. {
  1757. *out_size = 0;
  1758. return STATUS_SUCCESS;
  1759. }
  1760. for (i = 0, src = (char *)(params + 1); i < entry_cnt; i++, src += entry_size)
  1761. {
  1762. if (params->width)
  1763. {
  1764. x = params->x + i % params->width;
  1765. y = params->y + i / params->width;
  1766. if (x >= screen_buffer->width) continue;
  1767. }
  1768. else
  1769. {
  1770. x = (params->x + i) % screen_buffer->width;
  1771. y = params->y + (params->x + i) / screen_buffer->width;
  1772. }
  1773. if (y >= screen_buffer->height) break;
  1774. dest = &screen_buffer->data[y * screen_buffer->width + x];
  1775. switch(params->mode)
  1776. {
  1777. case CHAR_INFO_MODE_TEXT:
  1778. dest->ch = *(const WCHAR *)src;
  1779. break;
  1780. case CHAR_INFO_MODE_ATTR:
  1781. dest->attr = *(const unsigned short *)src;
  1782. break;
  1783. case CHAR_INFO_MODE_TEXTATTR:
  1784. *dest = *(const char_info_t *)src;
  1785. break;
  1786. default:
  1787. return STATUS_INVALID_PARAMETER;
  1788. }
  1789. }
  1790. if (i && is_active( screen_buffer ))
  1791. {
  1792. RECT update_rect;
  1793. update_rect.left = params->x;
  1794. update_rect.top = params->y;
  1795. if (params->width)
  1796. {
  1797. update_rect.bottom = min( params->y + entry_cnt / params->width, screen_buffer->height ) - 1;
  1798. update_rect.right = min( params->x + params->width, screen_buffer->width ) - 1;
  1799. }
  1800. else
  1801. {
  1802. update_rect.bottom = params->y + (params->x + i - 1) / screen_buffer->width;
  1803. if (update_rect.bottom != params->y)
  1804. {
  1805. update_rect.left = 0;
  1806. update_rect.right = screen_buffer->width - 1;
  1807. }
  1808. else
  1809. {
  1810. update_rect.right = params->x + i - 1;
  1811. }
  1812. }
  1813. update_output( screen_buffer, &update_rect );
  1814. tty_sync( screen_buffer->console );
  1815. }
  1816. if (*out_size == sizeof(SMALL_RECT))
  1817. {
  1818. SMALL_RECT *region;
  1819. unsigned int width = params->width;
  1820. x = params->x;
  1821. y = params->y;
  1822. if (!(region = alloc_ioctl_buffer( sizeof(*region )))) return STATUS_NO_MEMORY;
  1823. region->Left = x;
  1824. region->Top = y;
  1825. region->Right = min( x + width, screen_buffer->width ) - 1;
  1826. region->Bottom = min( y + entry_cnt / width, screen_buffer->height ) - 1;
  1827. }
  1828. else
  1829. {
  1830. DWORD *result;
  1831. if (!(result = alloc_ioctl_buffer( sizeof(*result )))) return STATUS_NO_MEMORY;
  1832. *result = i;
  1833. }
  1834. return STATUS_SUCCESS;
  1835. }
  1836. static NTSTATUS read_output( struct screen_buffer *screen_buffer, const struct condrv_output_params *params,
  1837. size_t *out_size )
  1838. {
  1839. enum char_info_mode mode;
  1840. unsigned int x, y, width;
  1841. unsigned int i, count;
  1842. x = params->x;
  1843. y = params->y;
  1844. mode = params->mode;
  1845. width = params->width;
  1846. TRACE( "(%u %u) mode %u width %u\n", x, y, mode, width );
  1847. switch(mode)
  1848. {
  1849. case CHAR_INFO_MODE_TEXT:
  1850. {
  1851. WCHAR *data;
  1852. char_info_t *src;
  1853. if (x >= screen_buffer->width || y >= screen_buffer->height)
  1854. {
  1855. *out_size = 0;
  1856. return STATUS_SUCCESS;
  1857. }
  1858. src = screen_buffer->data + y * screen_buffer->width + x;
  1859. count = min( screen_buffer->data + screen_buffer->height * screen_buffer->width - src,
  1860. *out_size / sizeof(*data) );
  1861. *out_size = count * sizeof(*data);
  1862. if (!(data = alloc_ioctl_buffer( *out_size ))) return STATUS_NO_MEMORY;
  1863. for (i = 0; i < count; i++) data[i] = src[i].ch;
  1864. }
  1865. break;
  1866. case CHAR_INFO_MODE_ATTR:
  1867. {
  1868. unsigned short *data;
  1869. char_info_t *src;
  1870. if (x >= screen_buffer->width || y >= screen_buffer->height)
  1871. {
  1872. *out_size = 0;
  1873. return STATUS_SUCCESS;
  1874. }
  1875. src = screen_buffer->data + y * screen_buffer->width + x;
  1876. count = min( screen_buffer->data + screen_buffer->height * screen_buffer->width - src,
  1877. *out_size / sizeof(*data) );
  1878. *out_size = count * sizeof(*data);
  1879. if (!(data = alloc_ioctl_buffer( *out_size ))) return STATUS_NO_MEMORY;
  1880. for (i = 0; i < count; i++) data[i] = src[i].attr;
  1881. }
  1882. break;
  1883. case CHAR_INFO_MODE_TEXTATTR:
  1884. {
  1885. SMALL_RECT *region;
  1886. char_info_t *data;
  1887. if (!width || *out_size < sizeof(*region) || x >= screen_buffer->width || y >= screen_buffer->height)
  1888. return STATUS_INVALID_PARAMETER;
  1889. count = min( (*out_size - sizeof(*region)) / (width * sizeof(*data)), screen_buffer->height - y );
  1890. width = min( width, screen_buffer->width - x );
  1891. *out_size = sizeof(*region) + width * count * sizeof(*data);
  1892. if (!(region = alloc_ioctl_buffer( *out_size ))) return STATUS_NO_MEMORY;
  1893. region->Left = x;
  1894. region->Top = y;
  1895. region->Right = x + width - 1;
  1896. region->Bottom = y + count - 1;
  1897. data = (char_info_t *)(region + 1);
  1898. for (i = 0; i < count; i++)
  1899. {
  1900. memcpy( &data[i * width], &screen_buffer->data[(y + i) * screen_buffer->width + x],
  1901. width * sizeof(*data) );
  1902. }
  1903. }
  1904. break;
  1905. default:
  1906. return STATUS_INVALID_PARAMETER;
  1907. }
  1908. return STATUS_SUCCESS;
  1909. }
  1910. static NTSTATUS fill_output( struct screen_buffer *screen_buffer, const struct condrv_fill_output_params *params )
  1911. {
  1912. char_info_t *end, *dest;
  1913. DWORD i, count, *result;
  1914. TRACE( "(%u %u) mode %u\n", params->x, params->y, params->mode );
  1915. if (params->y >= screen_buffer->height) return STATUS_SUCCESS;
  1916. dest = screen_buffer->data + min( params->y * screen_buffer->width + params->x,
  1917. screen_buffer->height * screen_buffer->width );
  1918. end = screen_buffer->data + screen_buffer->height * screen_buffer->width;
  1919. count = params->count;
  1920. if (count > end - dest) count = end - dest;
  1921. switch(params->mode)
  1922. {
  1923. case CHAR_INFO_MODE_TEXT:
  1924. for (i = 0; i < count; i++) dest[i].ch = params->ch;
  1925. break;
  1926. case CHAR_INFO_MODE_ATTR:
  1927. for (i = 0; i < count; i++) dest[i].attr = params->attr;
  1928. break;
  1929. case CHAR_INFO_MODE_TEXTATTR:
  1930. for (i = 0; i < count; i++)
  1931. {
  1932. dest[i].ch = params->ch;
  1933. dest[i].attr = params->attr;
  1934. }
  1935. break;
  1936. default:
  1937. return STATUS_INVALID_PARAMETER;
  1938. }
  1939. if (count && is_active(screen_buffer))
  1940. {
  1941. RECT update_rect;
  1942. SetRect( &update_rect,
  1943. params->x % screen_buffer->width,
  1944. params->y + params->x / screen_buffer->width,
  1945. (params->x + i - 1) % screen_buffer->width,
  1946. params->y + (params->x + i - 1) / screen_buffer->width );
  1947. update_output( screen_buffer, &update_rect );
  1948. tty_sync( screen_buffer->console );
  1949. }
  1950. if (!(result = alloc_ioctl_buffer( sizeof(*result) ))) return STATUS_NO_MEMORY;
  1951. *result = count;
  1952. return STATUS_SUCCESS;
  1953. }
  1954. static NTSTATUS scroll_output( struct screen_buffer *screen_buffer, const struct condrv_scroll_params *params )
  1955. {
  1956. int x, y, xsrc, ysrc, w, h;
  1957. char_info_t *psrc, *pdst;
  1958. SMALL_RECT src, dst;
  1959. RECT update_rect;
  1960. SMALL_RECT clip;
  1961. xsrc = params->scroll.Left;
  1962. ysrc = params->scroll.Top;
  1963. w = params->scroll.Right - params->scroll.Left + 1;
  1964. h = params->scroll.Bottom - params->scroll.Top + 1;
  1965. TRACE( "(%d %d) -> (%u %u) w %u h %u\n", xsrc, ysrc, params->origin.X, params->origin.Y, w, h );
  1966. clip.Left = max( params->clip.Left, 0 );
  1967. clip.Top = max( params->clip.Top, 0 );
  1968. clip.Right = min( params->clip.Right, screen_buffer->width - 1 );
  1969. clip.Bottom = min( params->clip.Bottom, screen_buffer->height - 1 );
  1970. if (clip.Left > clip.Right || clip.Top > clip.Bottom || params->scroll.Left < 0 || params->scroll.Top < 0 ||
  1971. params->scroll.Right >= screen_buffer->width || params->scroll.Bottom >= screen_buffer->height ||
  1972. params->scroll.Right < params->scroll.Left || params->scroll.Top > params->scroll.Bottom ||
  1973. params->origin.X < 0 || params->origin.X >= screen_buffer->width || params->origin.Y < 0 ||
  1974. params->origin.Y >= screen_buffer->height)
  1975. return STATUS_INVALID_PARAMETER;
  1976. src.Left = max( xsrc, clip.Left );
  1977. src.Top = max( ysrc, clip.Top );
  1978. src.Right = min( xsrc + w - 1, clip.Right );
  1979. src.Bottom = min( ysrc + h - 1, clip.Bottom );
  1980. dst.Left = params->origin.X;
  1981. dst.Top = params->origin.Y;
  1982. dst.Right = params->origin.X + w - 1;
  1983. dst.Bottom = params->origin.Y + h - 1;
  1984. if (dst.Left < clip.Left)
  1985. {
  1986. xsrc += clip.Left - dst.Left;
  1987. w -= clip.Left - dst.Left;
  1988. dst.Left = clip.Left;
  1989. }
  1990. if (dst.Top < clip.Top)
  1991. {
  1992. ysrc += clip.Top - dst.Top;
  1993. h -= clip.Top - dst.Top;
  1994. dst.Top = clip.Top;
  1995. }
  1996. if (dst.Right > clip.Right) w -= dst.Right - clip.Right;
  1997. if (dst.Bottom > clip.Bottom) h -= dst.Bottom - clip.Bottom;
  1998. if (w > 0 && h > 0)
  1999. {
  2000. if (ysrc < dst.Top)
  2001. {
  2002. psrc = &screen_buffer->data[(ysrc + h - 1) * screen_buffer->width + xsrc];
  2003. pdst = &screen_buffer->data[(dst.Top + h - 1) * screen_buffer->width + dst.Left];
  2004. for (y = h; y > 0; y--)
  2005. {
  2006. memcpy( pdst, psrc, w * sizeof(*pdst) );
  2007. pdst -= screen_buffer->width;
  2008. psrc -= screen_buffer->width;
  2009. }
  2010. }
  2011. else
  2012. {
  2013. psrc = &screen_buffer->data[ysrc * screen_buffer->width + xsrc];
  2014. pdst = &screen_buffer->data[dst.Top * screen_buffer->width + dst.Left];
  2015. for (y = 0; y < h; y++)
  2016. {
  2017. /* we use memmove here because when psrc and pdst are the same,
  2018. * copies are done on the same row, so the dst and src blocks
  2019. * can overlap */
  2020. memmove( pdst, psrc, w * sizeof(*pdst) );
  2021. pdst += screen_buffer->width;
  2022. psrc += screen_buffer->width;
  2023. }
  2024. }
  2025. }
  2026. for (y = src.Top; y <= src.Bottom; y++)
  2027. {
  2028. int left = src.Left;
  2029. int right = src.Right;
  2030. if (dst.Top <= y && y <= dst.Bottom)
  2031. {
  2032. if (dst.Left <= src.Left) left = max( left, dst.Right + 1 );
  2033. if (dst.Left >= src.Left) right = min( right, dst.Left - 1 );
  2034. }
  2035. for (x = left; x <= right; x++) screen_buffer->data[y * screen_buffer->width + x] = params->fill;
  2036. }
  2037. SetRect( &update_rect, min( src.Left, dst.Left ), min( src.Top, dst.Top ),
  2038. max( src.Right, dst.Right ), max( src.Bottom, dst.Bottom ));
  2039. update_output( screen_buffer, &update_rect );
  2040. tty_sync( screen_buffer->console );
  2041. return STATUS_SUCCESS;
  2042. }
  2043. static NTSTATUS set_console_title( struct console *console, const WCHAR *in_title, size_t size )
  2044. {
  2045. WCHAR *title = NULL;
  2046. TRACE( "%s\n", debugstr_wn(in_title, size / sizeof(WCHAR)) );
  2047. if (size)
  2048. {
  2049. if (!(title = malloc( size + sizeof(WCHAR) ))) return STATUS_NO_MEMORY;
  2050. memcpy( title, in_title, size );
  2051. title[size / sizeof(WCHAR)] = 0;
  2052. }
  2053. free( console->title );
  2054. console->title = title;
  2055. if (console->tty_output)
  2056. {
  2057. size_t len;
  2058. char *vt;
  2059. tty_write( console, "\x1b]0;", 4 );
  2060. len = WideCharToMultiByte( get_tty_cp( console ), 0, console->title, size / sizeof(WCHAR),
  2061. NULL, 0, NULL, NULL);
  2062. if ((vt = tty_alloc_buffer( console, len )))
  2063. WideCharToMultiByte( get_tty_cp( console ), 0, console->title, size / sizeof(WCHAR),
  2064. vt, len, NULL, NULL );
  2065. tty_write( console, "\x07", 1 );
  2066. tty_sync( console );
  2067. }
  2068. if (console->win)
  2069. SetWindowTextW( console->win, console->title );
  2070. return STATUS_SUCCESS;
  2071. }
  2072. static NTSTATUS screen_buffer_ioctl( struct screen_buffer *screen_buffer, unsigned int code,
  2073. const void *in_data, size_t in_size, size_t *out_size )
  2074. {
  2075. switch (code)
  2076. {
  2077. case IOCTL_CONDRV_CLOSE_OUTPUT:
  2078. if (in_size || *out_size) return STATUS_INVALID_PARAMETER;
  2079. destroy_screen_buffer( screen_buffer );
  2080. return STATUS_SUCCESS;
  2081. case IOCTL_CONDRV_ACTIVATE:
  2082. if (in_size || *out_size) return STATUS_INVALID_PARAMETER;
  2083. return screen_buffer_activate( screen_buffer );
  2084. case IOCTL_CONDRV_GET_MODE:
  2085. {
  2086. DWORD *mode;
  2087. TRACE( "returning mode %x\n", screen_buffer->mode );
  2088. if (in_size || *out_size != sizeof(*mode)) return STATUS_INVALID_PARAMETER;
  2089. if (!(mode = alloc_ioctl_buffer( *out_size ))) return STATUS_NO_MEMORY;
  2090. *mode = screen_buffer->mode;
  2091. return STATUS_SUCCESS;
  2092. }
  2093. case IOCTL_CONDRV_SET_MODE:
  2094. if (in_size != sizeof(unsigned int) || *out_size) return STATUS_INVALID_PARAMETER;
  2095. screen_buffer->mode = *(unsigned int *)in_data;
  2096. TRACE( "set %x mode\n", screen_buffer->mode );
  2097. return STATUS_SUCCESS;
  2098. case IOCTL_CONDRV_IS_UNIX:
  2099. return screen_buffer->console->is_unix ? STATUS_SUCCESS : STATUS_NOT_SUPPORTED;
  2100. case IOCTL_CONDRV_WRITE_CONSOLE:
  2101. if (in_size % sizeof(WCHAR) || *out_size) return STATUS_INVALID_PARAMETER;
  2102. return write_console( screen_buffer, in_data, in_size / sizeof(WCHAR) );
  2103. case IOCTL_CONDRV_WRITE_FILE:
  2104. {
  2105. unsigned int len;
  2106. WCHAR *buf;
  2107. NTSTATUS status;
  2108. len = MultiByteToWideChar( screen_buffer->console->output_cp, 0, in_data, in_size,
  2109. NULL, 0 );
  2110. if (!len) return STATUS_SUCCESS;
  2111. if (!(buf = malloc( len * sizeof(WCHAR) ))) return STATUS_NO_MEMORY;
  2112. MultiByteToWideChar( screen_buffer->console->output_cp, 0, in_data, in_size, buf, len );
  2113. status = write_console( screen_buffer, buf, len );
  2114. free( buf );
  2115. return status;
  2116. }
  2117. case IOCTL_CONDRV_WRITE_OUTPUT:
  2118. if ((*out_size != sizeof(DWORD) && *out_size != sizeof(SMALL_RECT)) ||
  2119. in_size < sizeof(struct condrv_output_params))
  2120. return STATUS_INVALID_PARAMETER;
  2121. return write_output( screen_buffer, in_data, in_size, out_size );
  2122. case IOCTL_CONDRV_READ_OUTPUT:
  2123. if (in_size != sizeof(struct condrv_output_params)) return STATUS_INVALID_PARAMETER;
  2124. return read_output( screen_buffer, in_data, out_size );
  2125. case IOCTL_CONDRV_GET_OUTPUT_INFO:
  2126. if (in_size || *out_size < sizeof(struct condrv_output_info)) return STATUS_INVALID_PARAMETER;
  2127. return get_output_info( screen_buffer, out_size );
  2128. case IOCTL_CONDRV_SET_OUTPUT_INFO:
  2129. if (in_size < sizeof(struct condrv_output_info) || *out_size) return STATUS_INVALID_PARAMETER;
  2130. return set_output_info( screen_buffer, in_data, in_size );
  2131. case IOCTL_CONDRV_FILL_OUTPUT:
  2132. if (in_size != sizeof(struct condrv_fill_output_params) || *out_size != sizeof(DWORD))
  2133. return STATUS_INVALID_PARAMETER;
  2134. return fill_output( screen_buffer, in_data );
  2135. case IOCTL_CONDRV_SCROLL:
  2136. if (in_size != sizeof(struct condrv_scroll_params) || *out_size)
  2137. return STATUS_INVALID_PARAMETER;
  2138. return scroll_output( screen_buffer, in_data );
  2139. default:
  2140. WARN( "invalid ioctl %x\n", code );
  2141. return STATUS_INVALID_HANDLE;
  2142. }
  2143. }
  2144. static NTSTATUS console_input_ioctl( struct console *console, unsigned int code, const void *in_data,
  2145. size_t in_size, size_t *out_size )
  2146. {
  2147. NTSTATUS status;
  2148. switch (code)
  2149. {
  2150. case IOCTL_CONDRV_GET_MODE:
  2151. {
  2152. DWORD *mode;
  2153. TRACE( "returning mode %x\n", console->mode );
  2154. if (in_size || *out_size != sizeof(*mode)) return STATUS_INVALID_PARAMETER;
  2155. if (!(mode = alloc_ioctl_buffer( *out_size ))) return STATUS_NO_MEMORY;
  2156. *mode = console->mode;
  2157. return STATUS_SUCCESS;
  2158. }
  2159. case IOCTL_CONDRV_SET_MODE:
  2160. if (in_size != sizeof(unsigned int) || *out_size) return STATUS_INVALID_PARAMETER;
  2161. console->mode = *(unsigned int *)in_data;
  2162. TRACE( "set %x mode\n", console->mode );
  2163. return STATUS_SUCCESS;
  2164. case IOCTL_CONDRV_IS_UNIX:
  2165. return console->is_unix ? STATUS_SUCCESS : STATUS_NOT_SUPPORTED;
  2166. case IOCTL_CONDRV_READ_CONSOLE:
  2167. if (in_size || *out_size % sizeof(WCHAR)) return STATUS_INVALID_PARAMETER;
  2168. ensure_tty_input_thread( console );
  2169. status = read_console( console, code, *out_size );
  2170. *out_size = 0;
  2171. return status;
  2172. case IOCTL_CONDRV_READ_FILE:
  2173. ensure_tty_input_thread( console );
  2174. status = read_console( console, code, *out_size );
  2175. *out_size = 0;
  2176. return status;
  2177. case IOCTL_CONDRV_READ_INPUT:
  2178. {
  2179. if (in_size) return STATUS_INVALID_PARAMETER;
  2180. ensure_tty_input_thread( console );
  2181. if (!console->record_count && *out_size)
  2182. {
  2183. TRACE( "pending read\n" );
  2184. console->read_ioctl = IOCTL_CONDRV_READ_INPUT;
  2185. console->pending_read = *out_size;
  2186. return STATUS_PENDING;
  2187. }
  2188. status = read_console_input( console, *out_size );
  2189. *out_size = 0;
  2190. return status;
  2191. }
  2192. case IOCTL_CONDRV_WRITE_INPUT:
  2193. if (in_size % sizeof(INPUT_RECORD) || *out_size) return STATUS_INVALID_PARAMETER;
  2194. return write_console_input( console, in_data, in_size / sizeof(INPUT_RECORD), TRUE );
  2195. case IOCTL_CONDRV_PEEK:
  2196. {
  2197. void *result;
  2198. TRACE( "peek\n" );
  2199. if (in_size) return STATUS_INVALID_PARAMETER;
  2200. ensure_tty_input_thread( console );
  2201. *out_size = min( *out_size, console->record_count * sizeof(INPUT_RECORD) );
  2202. if (!(result = alloc_ioctl_buffer( *out_size ))) return STATUS_NO_MEMORY;
  2203. if (*out_size) memcpy( result, console->records, *out_size );
  2204. return STATUS_SUCCESS;
  2205. }
  2206. case IOCTL_CONDRV_GET_INPUT_INFO:
  2207. {
  2208. struct condrv_input_info *info;
  2209. TRACE( "get info\n" );
  2210. if (in_size || *out_size != sizeof(*info)) return STATUS_INVALID_PARAMETER;
  2211. if (!(info = alloc_ioctl_buffer( sizeof(*info )))) return STATUS_NO_MEMORY;
  2212. info->input_cp = console->input_cp;
  2213. info->output_cp = console->output_cp;
  2214. info->win = condrv_handle( console->win );
  2215. info->input_count = console->record_count;
  2216. return STATUS_SUCCESS;
  2217. }
  2218. case IOCTL_CONDRV_SET_INPUT_INFO:
  2219. {
  2220. const struct condrv_input_info_params *params = in_data;
  2221. TRACE( "set info\n" );
  2222. if (in_size != sizeof(*params) || *out_size) return STATUS_INVALID_PARAMETER;
  2223. if (params->mask & SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE)
  2224. {
  2225. if (!IsValidCodePage( params->info.input_cp )) return STATUS_INVALID_PARAMETER;
  2226. console->input_cp = params->info.input_cp;
  2227. }
  2228. if (params->mask & SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE)
  2229. {
  2230. if (!IsValidCodePage( params->info.output_cp )) return STATUS_INVALID_PARAMETER;
  2231. console->output_cp = params->info.output_cp;
  2232. }
  2233. return STATUS_SUCCESS;
  2234. }
  2235. case IOCTL_CONDRV_GET_TITLE:
  2236. {
  2237. WCHAR *result;
  2238. if (in_size) return STATUS_INVALID_PARAMETER;
  2239. TRACE( "returning title %s\n", debugstr_w(console->title) );
  2240. *out_size = min( *out_size, console->title ? wcslen( console->title ) * sizeof(WCHAR) : 0 );
  2241. if (!(result = alloc_ioctl_buffer( *out_size ))) return STATUS_NO_MEMORY;
  2242. if (*out_size) memcpy( result, console->title, *out_size );
  2243. return STATUS_SUCCESS;
  2244. }
  2245. case IOCTL_CONDRV_SET_TITLE:
  2246. if (in_size % sizeof(WCHAR) || *out_size) return STATUS_INVALID_PARAMETER;
  2247. return set_console_title( console, in_data, in_size );
  2248. case IOCTL_CONDRV_BEEP:
  2249. if (in_size || *out_size) return STATUS_INVALID_PARAMETER;
  2250. if (console->is_unix)
  2251. {
  2252. tty_write( console, "\a", 1 );
  2253. tty_sync( console );
  2254. }
  2255. return STATUS_SUCCESS;
  2256. case IOCTL_CONDRV_FLUSH:
  2257. if (in_size || *out_size) return STATUS_INVALID_PARAMETER;
  2258. TRACE( "flush\n" );
  2259. console->record_count = 0;
  2260. return STATUS_SUCCESS;
  2261. default:
  2262. WARN( "unsupported ioctl %x\n", code );
  2263. return STATUS_INVALID_HANDLE;
  2264. }
  2265. }
  2266. static NTSTATUS process_console_ioctls( struct console *console )
  2267. {
  2268. size_t out_size = 0, in_size;
  2269. unsigned int code;
  2270. int output;
  2271. NTSTATUS status = STATUS_SUCCESS;
  2272. for (;;)
  2273. {
  2274. if (status) out_size = 0;
  2275. console->signaled = console->record_count != 0;
  2276. SERVER_START_REQ( get_next_console_request )
  2277. {
  2278. req->handle = wine_server_obj_handle( console->server );
  2279. req->status = status;
  2280. req->signal = console->signaled;
  2281. wine_server_add_data( req, ioctl_buffer, out_size );
  2282. wine_server_set_reply( req, ioctl_buffer, ioctl_buffer_size );
  2283. status = wine_server_call( req );
  2284. code = reply->code;
  2285. output = reply->output;
  2286. out_size = reply->out_size;
  2287. in_size = wine_server_reply_size( reply );
  2288. }
  2289. SERVER_END_REQ;
  2290. if (status == STATUS_PENDING) return STATUS_SUCCESS;
  2291. if (status == STATUS_BUFFER_OVERFLOW)
  2292. {
  2293. if (!alloc_ioctl_buffer( out_size )) return STATUS_NO_MEMORY;
  2294. status = STATUS_SUCCESS;
  2295. continue;
  2296. }
  2297. if (status)
  2298. {
  2299. TRACE( "failed to get next request: %#x\n", status );
  2300. return status;
  2301. }
  2302. if (code == IOCTL_CONDRV_INIT_OUTPUT)
  2303. {
  2304. TRACE( "initializing output %x\n", output );
  2305. if (console->active)
  2306. create_screen_buffer( console, output, console->active->width, console->active->height );
  2307. else
  2308. create_screen_buffer( console, output, 80, 150 );
  2309. }
  2310. else if (!output)
  2311. {
  2312. status = console_input_ioctl( console, code, ioctl_buffer, in_size, &out_size );
  2313. }
  2314. else
  2315. {
  2316. struct wine_rb_entry *entry;
  2317. if (!(entry = wine_rb_get( &screen_buffer_map, LongToPtr(output) )))
  2318. {
  2319. ERR( "invalid screen buffer id %x\n", output );
  2320. status = STATUS_INVALID_HANDLE;
  2321. }
  2322. else
  2323. {
  2324. status = screen_buffer_ioctl( WINE_RB_ENTRY_VALUE( entry, struct screen_buffer, entry ), code,
  2325. ioctl_buffer, in_size, &out_size );
  2326. }
  2327. }
  2328. }
  2329. }
  2330. static int main_loop( struct console *console, HANDLE signal )
  2331. {
  2332. HANDLE signal_event = NULL;
  2333. HANDLE wait_handles[3];
  2334. unsigned int wait_cnt = 0;
  2335. unsigned short signal_id;
  2336. IO_STATUS_BLOCK signal_io;
  2337. NTSTATUS status;
  2338. BOOL pump_msgs;
  2339. DWORD res;
  2340. if (signal)
  2341. {
  2342. if (!(signal_event = CreateEventW( NULL, TRUE, FALSE, NULL ))) return 1;
  2343. status = NtReadFile( signal, signal_event, NULL, NULL, &signal_io, &signal_id,
  2344. sizeof(signal_id), NULL, NULL );
  2345. if (status && status != STATUS_PENDING) return 1;
  2346. }
  2347. if (!alloc_ioctl_buffer( 4096 )) return 1;
  2348. wait_handles[wait_cnt++] = console->server;
  2349. if (signal) wait_handles[wait_cnt++] = signal_event;
  2350. if (console->input_thread) wait_handles[wait_cnt++] = console->input_thread;
  2351. pump_msgs = console->win != NULL;
  2352. for (;;)
  2353. {
  2354. if (pump_msgs)
  2355. res = MsgWaitForMultipleObjects( wait_cnt, wait_handles, FALSE, INFINITE, QS_ALLINPUT );
  2356. else
  2357. res = WaitForMultipleObjects( wait_cnt, wait_handles, FALSE, INFINITE );
  2358. if (res == WAIT_OBJECT_0 + wait_cnt)
  2359. {
  2360. MSG msg;
  2361. while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
  2362. {
  2363. if (msg.message == WM_QUIT) return 0;
  2364. DispatchMessageW(&msg);
  2365. }
  2366. continue;
  2367. }
  2368. switch (res)
  2369. {
  2370. case WAIT_OBJECT_0:
  2371. EnterCriticalSection( &console_section );
  2372. status = process_console_ioctls( console );
  2373. LeaveCriticalSection( &console_section );
  2374. if (status) return 0;
  2375. break;
  2376. case WAIT_OBJECT_0 + 1:
  2377. if (signal_io.Status || signal_io.Information != sizeof(signal_id))
  2378. {
  2379. TRACE( "signaled quit\n" );
  2380. return 0;
  2381. }
  2382. FIXME( "unimplemented signal %x\n", signal_id );
  2383. status = NtReadFile( signal, signal_event, NULL, NULL, &signal_io, &signal_id,
  2384. sizeof(signal_id), NULL, NULL );
  2385. if (status && status != STATUS_PENDING) return 1;
  2386. break;
  2387. default:
  2388. TRACE( "wait failed, quit\n");
  2389. return 0;
  2390. }
  2391. }
  2392. return 0;
  2393. }
  2394. int __cdecl wmain(int argc, WCHAR *argv[])
  2395. {
  2396. int headless = 0, i, width = 0, height = 0;
  2397. HANDLE signal = NULL;
  2398. WCHAR *end;
  2399. static struct console console;
  2400. for (i = 0; i < argc; i++) TRACE("%s ", wine_dbgstr_w(argv[i]));
  2401. TRACE("\n");
  2402. console.mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
  2403. ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
  2404. ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS | ENABLE_AUTO_POSITION;
  2405. console.input_cp = console.output_cp = GetOEMCP();
  2406. console.history_size = 50;
  2407. if (!(console.history = calloc( console.history_size, sizeof(*console.history) ))) return 1;
  2408. for (i = 1; i < argc; i++)
  2409. {
  2410. if (!wcscmp( argv[i], L"--headless"))
  2411. {
  2412. headless = 1;
  2413. continue;
  2414. }
  2415. if (!wcscmp( argv[i], L"--unix"))
  2416. {
  2417. console.is_unix = 1;
  2418. headless = 1;
  2419. continue;
  2420. }
  2421. if (!wcscmp( argv[i], L"--width" ))
  2422. {
  2423. if (++i == argc) return 1;
  2424. width = wcstol( argv[i], &end, 0 );
  2425. if ((!width && !console.is_unix) || width > 0xffff || *end) return 1;
  2426. continue;
  2427. }
  2428. if (!wcscmp( argv[i], L"--height" ))
  2429. {
  2430. if (++i == argc) return 1;
  2431. height = wcstol( argv[i], &end, 0 );
  2432. if ((!height && !console.is_unix) || height > 0xffff || *end) return 1;
  2433. continue;
  2434. }
  2435. if (!wcscmp( argv[i], L"--signal" ))
  2436. {
  2437. if (++i == argc) return 1;
  2438. signal = ULongToHandle( wcstol( argv[i], &end, 0 ));
  2439. if (*end) return 1;
  2440. continue;
  2441. }
  2442. if (!wcscmp( argv[i], L"--server" ))
  2443. {
  2444. if (++i == argc) return 1;
  2445. console.server = ULongToHandle( wcstol( argv[i], &end, 0 ));
  2446. if (*end) return 1;
  2447. continue;
  2448. }
  2449. FIXME( "unknown option %s\n", debugstr_w(argv[i]) );
  2450. return 1;
  2451. }
  2452. if (!console.server)
  2453. {
  2454. ERR( "no server handle\n" );
  2455. return 1;
  2456. }
  2457. if (!width) width = 80;
  2458. if (!height) height = 150;
  2459. if (!(console.active = create_screen_buffer( &console, 1, width, height ))) return 1;
  2460. if (headless)
  2461. {
  2462. console.tty_input = GetStdHandle( STD_INPUT_HANDLE );
  2463. console.tty_output = GetStdHandle( STD_OUTPUT_HANDLE );
  2464. init_tty_output( &console );
  2465. if (!console.is_unix && !ensure_tty_input_thread( &console )) return 1;
  2466. }
  2467. else
  2468. {
  2469. STARTUPINFOW si;
  2470. if (!init_window( &console )) return 1;
  2471. GetStartupInfoW( &si );
  2472. set_console_title( &console, si.lpTitle, wcslen( si.lpTitle ) * sizeof(WCHAR) );
  2473. ShowWindow( console.win, (si.dwFlags & STARTF_USESHOWWINDOW) ? si.wShowWindow : SW_SHOW );
  2474. }
  2475. return main_loop( &console, signal );
  2476. }