gtkwin.c 139 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500
  1. /*
  2. * gtkwin.c: the main code that runs a PuTTY terminal emulator and
  3. * backend in a GTK window.
  4. */
  5. #define _GNU_SOURCE
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <signal.h>
  11. #include <stdio.h>
  12. #include <time.h>
  13. #include <errno.h>
  14. #include <locale.h>
  15. #include <fcntl.h>
  16. #include <unistd.h>
  17. #include <sys/types.h>
  18. #include <sys/wait.h>
  19. #include <gtk/gtk.h>
  20. #if !GTK_CHECK_VERSION(3,0,0)
  21. #include <gdk/gdkkeysyms.h>
  22. #endif
  23. #if GTK_CHECK_VERSION(2,0,0)
  24. #include <gtk/gtkimmodule.h>
  25. #endif
  26. #define PUTTY_DO_GLOBALS /* actually _define_ globals */
  27. #define MAY_REFER_TO_GTK_IN_HEADERS
  28. #include "putty.h"
  29. #include "terminal.h"
  30. #include "gtkcompat.h"
  31. #include "gtkfont.h"
  32. #include "gtkmisc.h"
  33. #ifndef NOT_X_WINDOWS
  34. #include <gdk/gdkx.h>
  35. #include <X11/Xlib.h>
  36. #include <X11/Xutil.h>
  37. #include <X11/Xatom.h>
  38. #endif
  39. #include "x11misc.h"
  40. /* Colours come in two flavours: configurable, and xterm-extended. */
  41. #define NEXTCOLOURS 240 /* 216 colour-cube plus 24 shades of grey */
  42. #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
  43. GdkAtom compound_text_atom, utf8_string_atom;
  44. struct clipboard_data_instance;
  45. struct gui_data {
  46. GtkWidget *window, *area, *sbar;
  47. gboolean sbar_visible;
  48. GtkBox *hbox;
  49. GtkAdjustment *sbar_adjust;
  50. GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
  51. *restartitem;
  52. GtkWidget *sessionsmenu;
  53. #ifndef NO_BACKING_PIXMAPS
  54. /*
  55. * Server-side pixmap which we use to cache the terminal window's
  56. * contents. When we draw text in the terminal, we draw it to this
  57. * pixmap first, and then blit from there to the actual window;
  58. * this way, X expose events can be handled with an absolute
  59. * minimum of network traffic, by just sending a command to
  60. * re-blit an appropriate rectangle from this pixmap.
  61. */
  62. GdkPixmap *pixmap;
  63. #endif
  64. #ifdef DRAW_TEXT_CAIRO
  65. /*
  66. * If we're drawing using Cairo, we cache the same image on the
  67. * client side in a Cairo surface.
  68. *
  69. * In GTK2+Cairo, this happens _as well_ as having the server-side
  70. * pixmap cache above; in GTK3+Cairo, server-side pixmaps are
  71. * deprecated, so we _just_ have this client-side cache. In the
  72. * latter case that means we have to transmit a big wodge of
  73. * bitmap data over the X connection on every expose event; but
  74. * GTK3 apparently deliberately provides no way to avoid that
  75. * inefficiency, and at least this way we don't _also_ have to
  76. * redo any font rendering just because the window was temporarily
  77. * covered.
  78. */
  79. cairo_surface_t *surface;
  80. #endif
  81. #if GTK_CHECK_VERSION(2,0,0)
  82. GtkIMContext *imc;
  83. #endif
  84. unifont *fonts[4]; /* normal, bold, wide, widebold */
  85. int xpos, ypos, gotpos, gravity;
  86. GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor;
  87. GdkColor cols[NALLCOLOURS];
  88. #if !GTK_CHECK_VERSION(3,0,0)
  89. GdkColormap *colmap;
  90. #endif
  91. int direct_to_font;
  92. wchar_t *pastein_data;
  93. int pastein_data_len;
  94. #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
  95. GtkClipboard *clipboard;
  96. struct clipboard_data_instance *current_cdi;
  97. #else
  98. char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
  99. int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
  100. #endif
  101. int font_width, font_height;
  102. int width, height;
  103. int ignore_sbar;
  104. int mouseptr_visible;
  105. int busy_status;
  106. int alt_keycode;
  107. int alt_digits;
  108. char *wintitle;
  109. char *icontitle;
  110. int master_fd, master_func_id;
  111. void *ldisc;
  112. Backend *back;
  113. void *backhandle;
  114. Terminal *term;
  115. void *logctx;
  116. int exited;
  117. struct unicode_data ucsdata;
  118. Conf *conf;
  119. void *eventlogstuff;
  120. guint32 input_event_time; /* Timestamp of the most recent input event. */
  121. int reconfiguring;
  122. #if GTK_CHECK_VERSION(3,4,0)
  123. gdouble cumulative_scroll;
  124. #endif
  125. /* Cached things out of conf that we refer to a lot */
  126. int bold_style;
  127. int window_border;
  128. int cursor_type;
  129. int drawtype;
  130. int meta_mod_mask;
  131. };
  132. static void cache_conf_values(struct gui_data *inst)
  133. {
  134. inst->bold_style = conf_get_int(inst->conf, CONF_bold_style);
  135. inst->window_border = conf_get_int(inst->conf, CONF_window_border);
  136. inst->cursor_type = conf_get_int(inst->conf, CONF_cursor_type);
  137. #ifdef OSX_META_KEY_CONFIG
  138. inst->meta_mod_mask = 0;
  139. if (conf_get_int(inst->conf, CONF_osx_option_meta))
  140. inst->meta_mod_mask |= GDK_MOD1_MASK;
  141. if (conf_get_int(inst->conf, CONF_osx_command_meta))
  142. inst->meta_mod_mask |= GDK_MOD2_MASK;
  143. #else
  144. inst->meta_mod_mask = GDK_MOD1_MASK;
  145. #endif
  146. }
  147. struct draw_ctx {
  148. struct gui_data *inst;
  149. unifont_drawctx uctx;
  150. };
  151. static int send_raw_mouse;
  152. static void start_backend(struct gui_data *inst);
  153. static void exit_callback(void *vinst);
  154. void connection_fatal(void *frontend, const char *p, ...)
  155. {
  156. struct gui_data *inst = (struct gui_data *)frontend;
  157. va_list ap;
  158. char *msg;
  159. va_start(ap, p);
  160. msg = dupvprintf(p, ap);
  161. va_end(ap);
  162. fatal_message_box(inst->window, msg);
  163. sfree(msg);
  164. queue_toplevel_callback(exit_callback, inst);
  165. }
  166. /*
  167. * Default settings that are specific to pterm.
  168. */
  169. FontSpec *platform_default_fontspec(const char *name)
  170. {
  171. if (!strcmp(name, "Font"))
  172. return fontspec_new(DEFAULT_GTK_FONT);
  173. else
  174. return fontspec_new("");
  175. }
  176. Filename *platform_default_filename(const char *name)
  177. {
  178. if (!strcmp(name, "LogFileName"))
  179. return filename_from_str("putty.log");
  180. else
  181. return filename_from_str("");
  182. }
  183. char *platform_default_s(const char *name)
  184. {
  185. if (!strcmp(name, "SerialLine"))
  186. return dupstr("/dev/ttyS0");
  187. return NULL;
  188. }
  189. int platform_default_i(const char *name, int def)
  190. {
  191. if (!strcmp(name, "CloseOnExit"))
  192. return 2; /* maps to FORCE_ON after painful rearrangement :-( */
  193. if (!strcmp(name, "WinNameAlways"))
  194. return 0; /* X natively supports icon titles, so use 'em by default */
  195. return def;
  196. }
  197. /* Dummy routine, only required in plink. */
  198. void frontend_echoedit_update(void *frontend, int echo, int edit)
  199. {
  200. }
  201. char *get_ttymode(void *frontend, const char *mode)
  202. {
  203. struct gui_data *inst = (struct gui_data *)frontend;
  204. return term_get_ttymode(inst->term, mode);
  205. }
  206. int from_backend(void *frontend, int is_stderr, const char *data, int len)
  207. {
  208. struct gui_data *inst = (struct gui_data *)frontend;
  209. return term_data(inst->term, is_stderr, data, len);
  210. }
  211. int from_backend_untrusted(void *frontend, const char *data, int len)
  212. {
  213. struct gui_data *inst = (struct gui_data *)frontend;
  214. return term_data_untrusted(inst->term, data, len);
  215. }
  216. int from_backend_eof(void *frontend)
  217. {
  218. return TRUE; /* do respond to incoming EOF with outgoing */
  219. }
  220. int get_userpass_input(prompts_t *p, const unsigned char *in, int inlen)
  221. {
  222. struct gui_data *inst = (struct gui_data *)p->frontend;
  223. int ret;
  224. ret = cmdline_get_passwd_input(p, in, inlen);
  225. if (ret == -1)
  226. ret = term_get_userpass_input(inst->term, p, in, inlen);
  227. return ret;
  228. }
  229. void logevent(void *frontend, const char *string)
  230. {
  231. struct gui_data *inst = (struct gui_data *)frontend;
  232. log_eventlog(inst->logctx, string);
  233. logevent_dlg(inst->eventlogstuff, string);
  234. }
  235. int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
  236. {
  237. struct gui_data *inst = (struct gui_data *)frontend;
  238. if (which)
  239. return inst->font_height;
  240. else
  241. return inst->font_width;
  242. }
  243. /*
  244. * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
  245. * into a cooked one (SELECT, EXTEND, PASTE).
  246. *
  247. * In Unix, this is not configurable; the X button arrangement is
  248. * rock-solid across all applications, everyone has a three-button
  249. * mouse or a means of faking it, and there is no need to switch
  250. * buttons around at all.
  251. */
  252. static Mouse_Button translate_button(Mouse_Button button)
  253. {
  254. /* struct gui_data *inst = (struct gui_data *)frontend; */
  255. if (button == MBT_LEFT)
  256. return MBT_SELECT;
  257. if (button == MBT_MIDDLE)
  258. return MBT_PASTE;
  259. if (button == MBT_RIGHT)
  260. return MBT_EXTEND;
  261. return 0; /* shouldn't happen */
  262. }
  263. /*
  264. * Return the top-level GtkWindow associated with a particular
  265. * front end instance.
  266. */
  267. void *get_window(void *frontend)
  268. {
  269. struct gui_data *inst = (struct gui_data *)frontend;
  270. return inst->window;
  271. }
  272. /*
  273. * Minimise or restore the window in response to a server-side
  274. * request.
  275. */
  276. void set_iconic(void *frontend, int iconic)
  277. {
  278. /*
  279. * GTK 1.2 doesn't know how to do this.
  280. */
  281. #if GTK_CHECK_VERSION(2,0,0)
  282. struct gui_data *inst = (struct gui_data *)frontend;
  283. if (iconic)
  284. gtk_window_iconify(GTK_WINDOW(inst->window));
  285. else
  286. gtk_window_deiconify(GTK_WINDOW(inst->window));
  287. #endif
  288. }
  289. /*
  290. * Move the window in response to a server-side request.
  291. */
  292. void move_window(void *frontend, int x, int y)
  293. {
  294. struct gui_data *inst = (struct gui_data *)frontend;
  295. /*
  296. * I assume that when the GTK version of this call is available
  297. * we should use it. Not sure how it differs from the GDK one,
  298. * though.
  299. */
  300. #if GTK_CHECK_VERSION(2,0,0)
  301. /* in case we reset this at startup due to a geometry string */
  302. gtk_window_set_gravity(GTK_WINDOW(inst->window), GDK_GRAVITY_NORTH_EAST);
  303. gtk_window_move(GTK_WINDOW(inst->window), x, y);
  304. #else
  305. gdk_window_move(gtk_widget_get_window(inst->window), x, y);
  306. #endif
  307. }
  308. /*
  309. * Move the window to the top or bottom of the z-order in response
  310. * to a server-side request.
  311. */
  312. void set_zorder(void *frontend, int top)
  313. {
  314. struct gui_data *inst = (struct gui_data *)frontend;
  315. if (top)
  316. gdk_window_raise(gtk_widget_get_window(inst->window));
  317. else
  318. gdk_window_lower(gtk_widget_get_window(inst->window));
  319. }
  320. /*
  321. * Refresh the window in response to a server-side request.
  322. */
  323. void refresh_window(void *frontend)
  324. {
  325. struct gui_data *inst = (struct gui_data *)frontend;
  326. term_invalidate(inst->term);
  327. }
  328. /*
  329. * Maximise or restore the window in response to a server-side
  330. * request.
  331. */
  332. void set_zoomed(void *frontend, int zoomed)
  333. {
  334. /*
  335. * GTK 1.2 doesn't know how to do this.
  336. */
  337. #if GTK_CHECK_VERSION(2,0,0)
  338. struct gui_data *inst = (struct gui_data *)frontend;
  339. if (zoomed)
  340. gtk_window_maximize(GTK_WINDOW(inst->window));
  341. else
  342. gtk_window_unmaximize(GTK_WINDOW(inst->window));
  343. #endif
  344. }
  345. /*
  346. * Report whether the window is iconic, for terminal reports.
  347. */
  348. int is_iconic(void *frontend)
  349. {
  350. struct gui_data *inst = (struct gui_data *)frontend;
  351. return !gdk_window_is_viewable(gtk_widget_get_window(inst->window));
  352. }
  353. /*
  354. * Report the window's position, for terminal reports.
  355. */
  356. void get_window_pos(void *frontend, int *x, int *y)
  357. {
  358. struct gui_data *inst = (struct gui_data *)frontend;
  359. /*
  360. * I assume that when the GTK version of this call is available
  361. * we should use it. Not sure how it differs from the GDK one,
  362. * though.
  363. */
  364. #if GTK_CHECK_VERSION(2,0,0)
  365. gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
  366. #else
  367. gdk_window_get_position(gtk_widget_get_window(inst->window), x, y);
  368. #endif
  369. }
  370. /*
  371. * Report the window's pixel size, for terminal reports.
  372. */
  373. void get_window_pixels(void *frontend, int *x, int *y)
  374. {
  375. struct gui_data *inst = (struct gui_data *)frontend;
  376. /*
  377. * I assume that when the GTK version of this call is available
  378. * we should use it. Not sure how it differs from the GDK one,
  379. * though.
  380. */
  381. #if GTK_CHECK_VERSION(2,0,0)
  382. gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
  383. #else
  384. gdk_window_get_size(gtk_widget_get_window(inst->window), x, y);
  385. #endif
  386. }
  387. /*
  388. * Return the window or icon title.
  389. */
  390. char *get_window_title(void *frontend, int icon)
  391. {
  392. struct gui_data *inst = (struct gui_data *)frontend;
  393. return icon ? inst->icontitle : inst->wintitle;
  394. }
  395. gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
  396. {
  397. struct gui_data *inst = (struct gui_data *)data;
  398. if (!inst->exited && conf_get_int(inst->conf, CONF_warn_on_close)) {
  399. if (!reallyclose(inst))
  400. return TRUE;
  401. }
  402. return FALSE;
  403. }
  404. static void update_mouseptr(struct gui_data *inst)
  405. {
  406. switch (inst->busy_status) {
  407. case BUSY_NOT:
  408. if (!inst->mouseptr_visible) {
  409. gdk_window_set_cursor(gtk_widget_get_window(inst->area),
  410. inst->blankcursor);
  411. } else if (send_raw_mouse) {
  412. gdk_window_set_cursor(gtk_widget_get_window(inst->area),
  413. inst->rawcursor);
  414. } else {
  415. gdk_window_set_cursor(gtk_widget_get_window(inst->area),
  416. inst->textcursor);
  417. }
  418. break;
  419. case BUSY_WAITING: /* XXX can we do better? */
  420. case BUSY_CPU:
  421. /* We always display these cursors. */
  422. gdk_window_set_cursor(gtk_widget_get_window(inst->area),
  423. inst->waitcursor);
  424. break;
  425. default:
  426. assert(0);
  427. }
  428. }
  429. static void show_mouseptr(struct gui_data *inst, int show)
  430. {
  431. if (!conf_get_int(inst->conf, CONF_hide_mouseptr))
  432. show = 1;
  433. inst->mouseptr_visible = show;
  434. update_mouseptr(inst);
  435. }
  436. static void draw_backing_rect(struct gui_data *inst);
  437. gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
  438. {
  439. struct gui_data *inst = (struct gui_data *)data;
  440. int w, h, need_size = 0;
  441. /*
  442. * See if the terminal size has changed, in which case we must
  443. * let the terminal know.
  444. */
  445. w = (event->width - 2*inst->window_border) / inst->font_width;
  446. h = (event->height - 2*inst->window_border) / inst->font_height;
  447. if (w != inst->width || h != inst->height) {
  448. inst->width = w;
  449. inst->height = h;
  450. conf_set_int(inst->conf, CONF_width, inst->width);
  451. conf_set_int(inst->conf, CONF_height, inst->height);
  452. need_size = 1;
  453. }
  454. {
  455. int backing_w = w * inst->font_width + 2*inst->window_border;
  456. int backing_h = h * inst->font_height + 2*inst->window_border;
  457. #ifndef NO_BACKING_PIXMAPS
  458. if (inst->pixmap) {
  459. gdk_pixmap_unref(inst->pixmap);
  460. inst->pixmap = NULL;
  461. }
  462. inst->pixmap = gdk_pixmap_new(gtk_widget_get_window(widget),
  463. backing_w, backing_h, -1);
  464. #endif
  465. #ifdef DRAW_TEXT_CAIRO
  466. if (inst->surface) {
  467. cairo_surface_destroy(inst->surface);
  468. inst->surface = NULL;
  469. }
  470. inst->surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
  471. backing_w, backing_h);
  472. #endif
  473. }
  474. draw_backing_rect(inst);
  475. if (need_size && inst->term) {
  476. term_size(inst->term, h, w, conf_get_int(inst->conf, CONF_savelines));
  477. }
  478. if (inst->term)
  479. term_invalidate(inst->term);
  480. #if GTK_CHECK_VERSION(2,0,0)
  481. gtk_im_context_set_client_window(inst->imc, gtk_widget_get_window(widget));
  482. #endif
  483. return TRUE;
  484. }
  485. #ifdef DRAW_TEXT_CAIRO
  486. static void cairo_setup_dctx(struct draw_ctx *dctx)
  487. {
  488. cairo_get_matrix(dctx->uctx.u.cairo.cr,
  489. &dctx->uctx.u.cairo.origmatrix);
  490. cairo_set_line_width(dctx->uctx.u.cairo.cr, 1.0);
  491. cairo_set_line_cap(dctx->uctx.u.cairo.cr, CAIRO_LINE_CAP_SQUARE);
  492. cairo_set_line_join(dctx->uctx.u.cairo.cr, CAIRO_LINE_JOIN_MITER);
  493. /* This antialiasing setting appears to be ignored for Pango
  494. * font rendering but honoured for stroking and filling paths;
  495. * I don't quite understand the logic of that, but I won't
  496. * complain since it's exactly what I happen to want */
  497. cairo_set_antialias(dctx->uctx.u.cairo.cr, CAIRO_ANTIALIAS_NONE);
  498. }
  499. #endif
  500. #if GTK_CHECK_VERSION(3,0,0)
  501. static gint draw_area(GtkWidget *widget, cairo_t *cr, gpointer data)
  502. {
  503. struct gui_data *inst = (struct gui_data *)data;
  504. /*
  505. * GTK3 window redraw: we always expect Cairo to be enabled, so
  506. * that inst->surface exists, and pixmaps to be disabled, so that
  507. * inst->pixmap does not exist. Hence, we just blit from
  508. * inst->surface to the window.
  509. */
  510. if (inst->surface) {
  511. GdkRectangle dirtyrect;
  512. gdk_cairo_get_clip_rectangle(cr, &dirtyrect);
  513. cairo_set_source_surface(cr, inst->surface, 0, 0);
  514. cairo_rectangle(cr, dirtyrect.x, dirtyrect.y,
  515. dirtyrect.width, dirtyrect.height);
  516. cairo_fill(cr);
  517. }
  518. return TRUE;
  519. }
  520. #else
  521. gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
  522. {
  523. struct gui_data *inst = (struct gui_data *)data;
  524. #ifndef NO_BACKING_PIXMAPS
  525. /*
  526. * Draw to the exposed part of the window from the server-side
  527. * backing pixmap.
  528. */
  529. if (inst->pixmap) {
  530. gdk_draw_pixmap(gtk_widget_get_window(widget),
  531. (gtk_widget_get_style(widget)->fg_gc
  532. [gtk_widget_get_state(widget)]),
  533. inst->pixmap,
  534. event->area.x, event->area.y,
  535. event->area.x, event->area.y,
  536. event->area.width, event->area.height);
  537. }
  538. #else
  539. /*
  540. * Failing that, draw from the client-side Cairo surface. (We
  541. * should never be compiled in a context where we have _neither_
  542. * inst->surface nor inst->pixmap.)
  543. */
  544. if (inst->surface) {
  545. cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(widget));
  546. cairo_set_source_surface(cr, inst->surface, 0, 0);
  547. cairo_rectangle(cr, event->area.x, event->area.y,
  548. event->area.width, event->area.height);
  549. cairo_fill(cr);
  550. cairo_destroy(cr);
  551. }
  552. #endif
  553. return TRUE;
  554. }
  555. #endif
  556. #define KEY_PRESSED(k) \
  557. (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
  558. #ifdef KEY_EVENT_DIAGNOSTICS
  559. char *dup_keyval_name(guint keyval)
  560. {
  561. const char *name = gdk_keyval_name(keyval);
  562. if (name)
  563. return dupstr(name);
  564. else
  565. return dupprintf("UNKNOWN[%u]", (unsigned)keyval);
  566. }
  567. #endif
  568. gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
  569. {
  570. struct gui_data *inst = (struct gui_data *)data;
  571. char output[256];
  572. wchar_t ucsoutput[2];
  573. int ucsval, start, end, special, output_charset, use_ucsoutput;
  574. int nethack_mode, app_keypad_mode;
  575. /* Remember the timestamp. */
  576. inst->input_event_time = event->time;
  577. /* By default, nothing is generated. */
  578. end = start = 0;
  579. special = use_ucsoutput = FALSE;
  580. output_charset = CS_ISO8859_1;
  581. #ifdef KEY_EVENT_DIAGNOSTICS
  582. {
  583. char *type_string, *state_string, *keyval_string, *string_string;
  584. type_string = (event->type == GDK_KEY_PRESS ? dupstr("PRESS") :
  585. event->type == GDK_KEY_RELEASE ? dupstr("RELEASE") :
  586. dupprintf("UNKNOWN[%d]", (int)event->type));
  587. {
  588. static const struct {
  589. int mod_bit;
  590. const char *name;
  591. } mod_bits[] = {
  592. {GDK_SHIFT_MASK, "SHIFT"},
  593. {GDK_LOCK_MASK, "LOCK"},
  594. {GDK_CONTROL_MASK, "CONTROL"},
  595. {GDK_MOD1_MASK, "MOD1"},
  596. {GDK_MOD2_MASK, "MOD2"},
  597. {GDK_MOD3_MASK, "MOD3"},
  598. {GDK_MOD4_MASK, "MOD4"},
  599. {GDK_MOD5_MASK, "MOD5"},
  600. {GDK_SUPER_MASK, "SUPER"},
  601. {GDK_HYPER_MASK, "HYPER"},
  602. {GDK_META_MASK, "META"},
  603. };
  604. int i;
  605. int val = event->state;
  606. state_string = dupstr("");
  607. for (i = 0; i < lenof(mod_bits); i++) {
  608. if (val & mod_bits[i].mod_bit) {
  609. char *old = state_string;
  610. state_string = dupcat(state_string,
  611. state_string[0] ? "|" : "",
  612. mod_bits[i].name,
  613. (char *)NULL);
  614. sfree(old);
  615. val &= ~mod_bits[i].mod_bit;
  616. }
  617. }
  618. if (val || !state_string[0]) {
  619. char *old = state_string;
  620. state_string = dupprintf("%s%s%d", state_string,
  621. state_string[0] ? "|" : "", val);
  622. sfree(old);
  623. }
  624. }
  625. keyval_string = dup_keyval_name(event->keyval);
  626. string_string = dupstr("");
  627. {
  628. int i;
  629. for (i = 0; event->string[i]; i++) {
  630. char *old = string_string;
  631. string_string = dupprintf("%s%s%02x", string_string,
  632. string_string[0] ? " " : "",
  633. (unsigned)event->string[i] & 0xFF);
  634. sfree(old);
  635. }
  636. }
  637. debug(("key_event: type=%s keyval=%s state=%s "
  638. "hardware_keycode=%d is_modifier=%s string=[%s]\n",
  639. type_string, keyval_string, state_string,
  640. (int)event->hardware_keycode,
  641. event->is_modifier ? "TRUE" : "FALSE",
  642. string_string));
  643. sfree(type_string);
  644. sfree(state_string);
  645. sfree(keyval_string);
  646. sfree(string_string);
  647. }
  648. #endif /* KEY_EVENT_DIAGNOSTICS */
  649. /*
  650. * If Alt is being released after typing an Alt+numberpad
  651. * sequence, we should generate the code that was typed.
  652. *
  653. * Note that we only do this if more than one key was actually
  654. * pressed - I don't think Alt+NumPad4 should be ^D or that
  655. * Alt+NumPad3 should be ^C, for example. There's no serious
  656. * inconvenience in having to type a zero before a single-digit
  657. * character code.
  658. */
  659. if (event->type == GDK_KEY_RELEASE) {
  660. if ((event->keyval == GDK_KEY_Meta_L ||
  661. event->keyval == GDK_KEY_Meta_R ||
  662. event->keyval == GDK_KEY_Alt_L ||
  663. event->keyval == GDK_KEY_Alt_R) &&
  664. inst->alt_keycode >= 0 && inst->alt_digits > 1) {
  665. #ifdef KEY_EVENT_DIAGNOSTICS
  666. debug((" - modifier release terminates Alt+numberpad input, "
  667. "keycode = %d\n", inst->alt_keycode));
  668. #endif
  669. /*
  670. * FIXME: we might usefully try to do something clever here
  671. * about interpreting the generated key code in a way that's
  672. * appropriate to the line code page.
  673. */
  674. output[0] = inst->alt_keycode;
  675. end = 1;
  676. goto done;
  677. }
  678. #if GTK_CHECK_VERSION(2,0,0)
  679. #ifdef KEY_EVENT_DIAGNOSTICS
  680. debug((" - key release, passing to IM\n"));
  681. #endif
  682. if (gtk_im_context_filter_keypress(inst->imc, event)) {
  683. #ifdef KEY_EVENT_DIAGNOSTICS
  684. debug((" - key release accepted by IM\n"));
  685. #endif
  686. return TRUE;
  687. } else {
  688. #ifdef KEY_EVENT_DIAGNOSTICS
  689. debug((" - key release not accepted by IM\n"));
  690. #endif
  691. }
  692. #endif
  693. }
  694. if (event->type == GDK_KEY_PRESS) {
  695. /*
  696. * If Alt has just been pressed, we start potentially
  697. * accumulating an Alt+numberpad code. We do this by
  698. * setting alt_keycode to -1 (nothing yet but plausible).
  699. */
  700. if ((event->keyval == GDK_KEY_Meta_L ||
  701. event->keyval == GDK_KEY_Meta_R ||
  702. event->keyval == GDK_KEY_Alt_L ||
  703. event->keyval == GDK_KEY_Alt_R)) {
  704. #ifdef KEY_EVENT_DIAGNOSTICS
  705. debug((" - modifier press potentially begins Alt+numberpad "
  706. "input\n"));
  707. #endif
  708. inst->alt_keycode = -1;
  709. inst->alt_digits = 0;
  710. goto done; /* this generates nothing else */
  711. }
  712. /*
  713. * If we're seeing a numberpad key press with Meta down,
  714. * consider adding it to alt_keycode if that's sensible.
  715. * Anything _else_ with Meta down cancels any possibility
  716. * of an ALT keycode: we set alt_keycode to -2.
  717. */
  718. if ((event->state & inst->meta_mod_mask) && inst->alt_keycode != -2) {
  719. int digit = -1;
  720. switch (event->keyval) {
  721. case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: digit = 0; break;
  722. case GDK_KEY_KP_1: case GDK_KEY_KP_End: digit = 1; break;
  723. case GDK_KEY_KP_2: case GDK_KEY_KP_Down: digit = 2; break;
  724. case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: digit = 3; break;
  725. case GDK_KEY_KP_4: case GDK_KEY_KP_Left: digit = 4; break;
  726. case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: digit = 5; break;
  727. case GDK_KEY_KP_6: case GDK_KEY_KP_Right: digit = 6; break;
  728. case GDK_KEY_KP_7: case GDK_KEY_KP_Home: digit = 7; break;
  729. case GDK_KEY_KP_8: case GDK_KEY_KP_Up: digit = 8; break;
  730. case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: digit = 9; break;
  731. }
  732. if (digit < 0)
  733. inst->alt_keycode = -2; /* it's invalid */
  734. else {
  735. #ifdef KEY_EVENT_DIAGNOSTICS
  736. int old_keycode = inst->alt_keycode;
  737. #endif
  738. if (inst->alt_keycode == -1)
  739. inst->alt_keycode = digit; /* one-digit code */
  740. else
  741. inst->alt_keycode = inst->alt_keycode * 10 + digit;
  742. inst->alt_digits++;
  743. #ifdef KEY_EVENT_DIAGNOSTICS
  744. debug((" - Alt+numberpad digit %d added to keycode %d"
  745. " gives %d\n", digit, old_keycode, inst->alt_keycode));
  746. #endif
  747. /* Having used this digit, we now do nothing more with it. */
  748. goto done;
  749. }
  750. }
  751. /*
  752. * Shift-PgUp and Shift-PgDn don't even generate keystrokes
  753. * at all.
  754. */
  755. if (event->keyval == GDK_KEY_Page_Up &&
  756. (event->state & GDK_SHIFT_MASK)) {
  757. #ifdef KEY_EVENT_DIAGNOSTICS
  758. debug((" - Shift-PgUp scroll\n"));
  759. #endif
  760. term_scroll(inst->term, 0, -inst->height/2);
  761. return TRUE;
  762. }
  763. if (event->keyval == GDK_KEY_Page_Up &&
  764. (event->state & GDK_CONTROL_MASK)) {
  765. #ifdef KEY_EVENT_DIAGNOSTICS
  766. debug((" - Ctrl-PgUp scroll\n"));
  767. #endif
  768. term_scroll(inst->term, 0, -1);
  769. return TRUE;
  770. }
  771. if (event->keyval == GDK_KEY_Page_Down &&
  772. (event->state & GDK_SHIFT_MASK)) {
  773. #ifdef KEY_EVENT_DIAGNOSTICS
  774. debug((" - Shift-PgDn scroll\n"));
  775. #endif
  776. term_scroll(inst->term, 0, +inst->height/2);
  777. return TRUE;
  778. }
  779. if (event->keyval == GDK_KEY_Page_Down &&
  780. (event->state & GDK_CONTROL_MASK)) {
  781. #ifdef KEY_EVENT_DIAGNOSTICS
  782. debug((" - Ctrl-PgDn scroll\n"));
  783. #endif
  784. term_scroll(inst->term, 0, +1);
  785. return TRUE;
  786. }
  787. /*
  788. * Neither does Shift-Ins.
  789. */
  790. if (event->keyval == GDK_KEY_Insert &&
  791. (event->state & GDK_SHIFT_MASK)) {
  792. #ifdef KEY_EVENT_DIAGNOSTICS
  793. debug((" - Shift-Insert paste\n"));
  794. #endif
  795. request_paste(inst);
  796. return TRUE;
  797. }
  798. special = FALSE;
  799. use_ucsoutput = FALSE;
  800. nethack_mode = conf_get_int(inst->conf, CONF_nethack_keypad);
  801. app_keypad_mode = (inst->term->app_keypad_keys &&
  802. !conf_get_int(inst->conf, CONF_no_applic_k));
  803. /* ALT+things gives leading Escape. */
  804. output[0] = '\033';
  805. #if !GTK_CHECK_VERSION(2,0,0)
  806. /*
  807. * In vanilla X, and hence also GDK 1.2, the string received
  808. * as part of a keyboard event is assumed to be in
  809. * ISO-8859-1. (Seems woefully shortsighted in i18n terms,
  810. * but it's true: see the man page for XLookupString(3) for
  811. * confirmation.)
  812. */
  813. output_charset = CS_ISO8859_1;
  814. strncpy(output+1, event->string, lenof(output)-1);
  815. #else /* !GTK_CHECK_VERSION(2,0,0) */
  816. /*
  817. * Most things can now be passed to
  818. * gtk_im_context_filter_keypress without breaking anything
  819. * below this point. An exception is the numeric keypad if
  820. * we're in Nethack or application mode: the IM will eat
  821. * numeric keypad presses if Num Lock is on, but we don't want
  822. * it to.
  823. */
  824. if (app_keypad_mode &&
  825. (event->keyval == GDK_KEY_Num_Lock ||
  826. event->keyval == GDK_KEY_KP_Divide ||
  827. event->keyval == GDK_KEY_KP_Multiply ||
  828. event->keyval == GDK_KEY_KP_Subtract ||
  829. event->keyval == GDK_KEY_KP_Add ||
  830. event->keyval == GDK_KEY_KP_Enter ||
  831. event->keyval == GDK_KEY_KP_0 ||
  832. event->keyval == GDK_KEY_KP_Insert ||
  833. event->keyval == GDK_KEY_KP_1 ||
  834. event->keyval == GDK_KEY_KP_End ||
  835. event->keyval == GDK_KEY_KP_2 ||
  836. event->keyval == GDK_KEY_KP_Down ||
  837. event->keyval == GDK_KEY_KP_3 ||
  838. event->keyval == GDK_KEY_KP_Page_Down ||
  839. event->keyval == GDK_KEY_KP_4 ||
  840. event->keyval == GDK_KEY_KP_Left ||
  841. event->keyval == GDK_KEY_KP_5 ||
  842. event->keyval == GDK_KEY_KP_Begin ||
  843. event->keyval == GDK_KEY_KP_6 ||
  844. event->keyval == GDK_KEY_KP_Right ||
  845. event->keyval == GDK_KEY_KP_7 ||
  846. event->keyval == GDK_KEY_KP_Home ||
  847. event->keyval == GDK_KEY_KP_8 ||
  848. event->keyval == GDK_KEY_KP_Up ||
  849. event->keyval == GDK_KEY_KP_9 ||
  850. event->keyval == GDK_KEY_KP_Page_Up ||
  851. event->keyval == GDK_KEY_KP_Decimal ||
  852. event->keyval == GDK_KEY_KP_Delete)) {
  853. /* app keypad; do nothing */
  854. } else if (nethack_mode &&
  855. (event->keyval == GDK_KEY_KP_1 ||
  856. event->keyval == GDK_KEY_KP_End ||
  857. event->keyval == GDK_KEY_KP_2 ||
  858. event->keyval == GDK_KEY_KP_Down ||
  859. event->keyval == GDK_KEY_KP_3 ||
  860. event->keyval == GDK_KEY_KP_Page_Down ||
  861. event->keyval == GDK_KEY_KP_4 ||
  862. event->keyval == GDK_KEY_KP_Left ||
  863. event->keyval == GDK_KEY_KP_5 ||
  864. event->keyval == GDK_KEY_KP_Begin ||
  865. event->keyval == GDK_KEY_KP_6 ||
  866. event->keyval == GDK_KEY_KP_Right ||
  867. event->keyval == GDK_KEY_KP_7 ||
  868. event->keyval == GDK_KEY_KP_Home ||
  869. event->keyval == GDK_KEY_KP_8 ||
  870. event->keyval == GDK_KEY_KP_Up ||
  871. event->keyval == GDK_KEY_KP_9 ||
  872. event->keyval == GDK_KEY_KP_Page_Up)) {
  873. /* nethack mode; do nothing */
  874. } else {
  875. int try_filter = TRUE;
  876. #ifdef META_MANUAL_MASK
  877. if (event->state & META_MANUAL_MASK & inst->meta_mod_mask) {
  878. /*
  879. * If this key event had a Meta modifier bit set which
  880. * is also in META_MANUAL_MASK, that means passing
  881. * such an event to the GtkIMContext will be unhelpful
  882. * (it will eat the keystroke and turn it into
  883. * something not what we wanted).
  884. */
  885. #ifdef KEY_EVENT_DIAGNOSTICS
  886. debug((" - Meta modifier requiring manual intervention, "
  887. "suppressing IM filtering\n"));
  888. #endif
  889. try_filter = FALSE;
  890. }
  891. #endif
  892. if (try_filter) {
  893. #ifdef KEY_EVENT_DIAGNOSTICS
  894. debug((" - general key press, passing to IM\n"));
  895. #endif
  896. if (gtk_im_context_filter_keypress(inst->imc, event)) {
  897. #ifdef KEY_EVENT_DIAGNOSTICS
  898. debug((" - key press accepted by IM\n"));
  899. #endif
  900. return TRUE;
  901. } else {
  902. #ifdef KEY_EVENT_DIAGNOSTICS
  903. debug((" - key press not accepted by IM\n"));
  904. #endif
  905. }
  906. }
  907. }
  908. /*
  909. * GDK 2.0 arranges to have done some translation for us: in
  910. * GDK 2.0, event->string is encoded in the current locale.
  911. *
  912. * So we use the standard C library function mbstowcs() to
  913. * convert from the current locale into Unicode; from there
  914. * we can convert to whatever PuTTY is currently working in.
  915. * (In fact I convert straight back to UTF-8 from
  916. * wide-character Unicode, for the sake of simplicity: that
  917. * way we can still use exactly the same code to manipulate
  918. * the string, such as prefixing ESC.)
  919. */
  920. output_charset = CS_UTF8;
  921. {
  922. wchar_t widedata[32];
  923. const wchar_t *wp;
  924. int wlen;
  925. int ulen;
  926. wlen = mb_to_wc(DEFAULT_CODEPAGE, 0,
  927. event->string, strlen(event->string),
  928. widedata, lenof(widedata)-1);
  929. #ifdef KEY_EVENT_DIAGNOSTICS
  930. {
  931. char *string_string = dupstr("");
  932. int i;
  933. for (i = 0; i < wlen; i++) {
  934. char *old = string_string;
  935. string_string = dupprintf("%s%s%04x", string_string,
  936. string_string[0] ? " " : "",
  937. (unsigned)widedata[i]);
  938. sfree(old);
  939. }
  940. debug((" - string translated into Unicode = [%s]\n",
  941. string_string));
  942. sfree(string_string);
  943. }
  944. #endif
  945. wp = widedata;
  946. ulen = charset_from_unicode(&wp, &wlen, output+1, lenof(output)-2,
  947. CS_UTF8, NULL, NULL, 0);
  948. #ifdef KEY_EVENT_DIAGNOSTICS
  949. {
  950. char *string_string = dupstr("");
  951. int i;
  952. for (i = 0; i < ulen; i++) {
  953. char *old = string_string;
  954. string_string = dupprintf("%s%s%02x", string_string,
  955. string_string[0] ? " " : "",
  956. (unsigned)output[i+1] & 0xFF);
  957. sfree(old);
  958. }
  959. debug((" - string translated into UTF-8 = [%s]\n",
  960. string_string));
  961. sfree(string_string);
  962. }
  963. #endif
  964. output[1+ulen] = '\0';
  965. }
  966. #endif /* !GTK_CHECK_VERSION(2,0,0) */
  967. if (!output[1] &&
  968. (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
  969. ucsoutput[0] = '\033';
  970. ucsoutput[1] = ucsval;
  971. #ifdef KEY_EVENT_DIAGNOSTICS
  972. debug((" - keysym_to_unicode gave %04x\n",
  973. (unsigned)ucsoutput[1]));
  974. #endif
  975. use_ucsoutput = TRUE;
  976. end = 2;
  977. } else {
  978. output[lenof(output)-1] = '\0';
  979. end = strlen(output);
  980. }
  981. if (event->state & inst->meta_mod_mask) {
  982. start = 0;
  983. if (end == 1) end = 0;
  984. #ifdef META_MANUAL_MASK
  985. if (event->state & META_MANUAL_MASK) {
  986. /*
  987. * Key events which have a META_MANUAL_MASK meta bit
  988. * set may have a keyval reflecting that, e.g. on OS X
  989. * the Option key acts as an AltGr-like modifier and
  990. * causes different Unicode characters to be output.
  991. *
  992. * To work around this, we clear the dangerous
  993. * modifier bit and retranslate from the hardware
  994. * keycode as if the key had been pressed without that
  995. * modifier. Then we prefix Esc to *that*.
  996. */
  997. guint new_keyval;
  998. GdkModifierType consumed;
  999. if (gdk_keymap_translate_keyboard_state
  1000. (gdk_keymap_get_for_display(gdk_display_get_default()),
  1001. event->hardware_keycode, event->state & ~META_MANUAL_MASK,
  1002. 0, &new_keyval, NULL, NULL, &consumed)) {
  1003. ucsoutput[0] = '\033';
  1004. ucsoutput[1] = gdk_keyval_to_unicode(new_keyval);
  1005. #ifdef KEY_EVENT_DIAGNOSTICS
  1006. {
  1007. char *keyval_name = dup_keyval_name(new_keyval);
  1008. debug((" - retranslation for manual Meta: "
  1009. "new keyval = %s, Unicode = %04x\n",
  1010. keyval_name, (unsigned)ucsoutput[1]));
  1011. sfree(keyval_name);
  1012. }
  1013. #endif
  1014. use_ucsoutput = TRUE;
  1015. end = 2;
  1016. }
  1017. }
  1018. #endif
  1019. } else
  1020. start = 1;
  1021. /* Control-` is the same as Control-\ (unless gtk has a better idea) */
  1022. if (!output[1] && event->keyval == '`' &&
  1023. (event->state & GDK_CONTROL_MASK)) {
  1024. #ifdef KEY_EVENT_DIAGNOSTICS
  1025. debug((" - Ctrl-` special case, translating as 1c\n"));
  1026. #endif
  1027. output[1] = '\x1C';
  1028. use_ucsoutput = FALSE;
  1029. end = 2;
  1030. }
  1031. /* Some GTK backends (e.g. Quartz) do not change event->string
  1032. * in response to the Control modifier. So we do it ourselves
  1033. * here, if it's not already happened.
  1034. *
  1035. * The translations below are in line with X11 policy as far
  1036. * as I know. */
  1037. if ((event->state & GDK_CONTROL_MASK) && end == 2) {
  1038. #ifdef KEY_EVENT_DIAGNOSTICS
  1039. int orig = output[1];
  1040. #endif
  1041. if (output[1] >= '3' && output[1] <= '7') {
  1042. /* ^3,...,^7 map to 0x1B,...,0x1F */
  1043. output[1] += '\x1B' - '3';
  1044. } else if (output[1] == '2' || output[1] == ' ') {
  1045. /* ^2 and ^Space are both ^@, i.e. \0 */
  1046. output[1] = '\0';
  1047. } else if (output[1] == '8') {
  1048. /* ^8 is DEL */
  1049. output[1] = '\x7F';
  1050. } else if (output[1] == '/') {
  1051. /* ^/ is the same as ^_ */
  1052. output[1] = '\x1F';
  1053. } else if (output[1] >= 0x40 && output[1] < 0x7F) {
  1054. /* Everything anywhere near the alphabetics just gets
  1055. * masked. */
  1056. output[1] &= 0x1F;
  1057. }
  1058. /* Anything else, e.g. '0', is unchanged. */
  1059. #ifdef KEY_EVENT_DIAGNOSTICS
  1060. if (orig == output[1])
  1061. debug((" - manual Ctrl key handling did nothing\n"));
  1062. else
  1063. debug((" - manual Ctrl key handling: %02x -> %02x\n",
  1064. (unsigned)orig, (unsigned)output[1]));
  1065. #endif
  1066. }
  1067. /* Control-Break sends a Break special to the backend */
  1068. if (event->keyval == GDK_KEY_Break &&
  1069. (event->state & GDK_CONTROL_MASK)) {
  1070. #ifdef KEY_EVENT_DIAGNOSTICS
  1071. debug((" - Ctrl-Break special case, sending TS_BRK\n"));
  1072. #endif
  1073. if (inst->back)
  1074. inst->back->special(inst->backhandle, TS_BRK);
  1075. return TRUE;
  1076. }
  1077. /* We handle Return ourselves, because it needs to be flagged as
  1078. * special to ldisc. */
  1079. if (event->keyval == GDK_KEY_Return) {
  1080. #ifdef KEY_EVENT_DIAGNOSTICS
  1081. debug((" - Return special case, translating as 0d + special\n"));
  1082. #endif
  1083. output[1] = '\015';
  1084. use_ucsoutput = FALSE;
  1085. end = 2;
  1086. special = TRUE;
  1087. }
  1088. /* Control-2, Control-Space and Control-@ are NUL */
  1089. if (!output[1] &&
  1090. (event->keyval == ' ' || event->keyval == '2' ||
  1091. event->keyval == '@') &&
  1092. (event->state & (GDK_SHIFT_MASK |
  1093. GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
  1094. #ifdef KEY_EVENT_DIAGNOSTICS
  1095. debug((" - Ctrl-{space,2,@} special case, translating as 00\n"));
  1096. #endif
  1097. output[1] = '\0';
  1098. use_ucsoutput = FALSE;
  1099. end = 2;
  1100. }
  1101. /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
  1102. if (!output[1] && event->keyval == ' ' &&
  1103. (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
  1104. (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
  1105. #ifdef KEY_EVENT_DIAGNOSTICS
  1106. debug((" - Ctrl-Shift-space special case, translating as 00a0\n"));
  1107. #endif
  1108. output[1] = '\240';
  1109. output_charset = CS_ISO8859_1;
  1110. use_ucsoutput = FALSE;
  1111. end = 2;
  1112. }
  1113. /* We don't let GTK tell us what Backspace is! We know better. */
  1114. if (event->keyval == GDK_KEY_BackSpace &&
  1115. !(event->state & GDK_SHIFT_MASK)) {
  1116. output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
  1117. '\x7F' : '\x08';
  1118. #ifdef KEY_EVENT_DIAGNOSTICS
  1119. debug((" - Backspace, translating as %02x\n",
  1120. (unsigned)output[1]));
  1121. #endif
  1122. use_ucsoutput = FALSE;
  1123. end = 2;
  1124. special = TRUE;
  1125. }
  1126. /* For Shift Backspace, do opposite of what is configured. */
  1127. if (event->keyval == GDK_KEY_BackSpace &&
  1128. (event->state & GDK_SHIFT_MASK)) {
  1129. output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
  1130. '\x08' : '\x7F';
  1131. #ifdef KEY_EVENT_DIAGNOSTICS
  1132. debug((" - Shift-Backspace, translating as %02x\n",
  1133. (unsigned)output[1]));
  1134. #endif
  1135. use_ucsoutput = FALSE;
  1136. end = 2;
  1137. special = TRUE;
  1138. }
  1139. /* Shift-Tab is ESC [ Z */
  1140. if (event->keyval == GDK_KEY_ISO_Left_Tab ||
  1141. (event->keyval == GDK_KEY_Tab &&
  1142. (event->state & GDK_SHIFT_MASK))) {
  1143. #ifdef KEY_EVENT_DIAGNOSTICS
  1144. debug((" - Shift-Tab, translating as ESC [ Z\n"));
  1145. #endif
  1146. end = 1 + sprintf(output+1, "\033[Z");
  1147. use_ucsoutput = FALSE;
  1148. }
  1149. /* And normal Tab is Tab, if the keymap hasn't already told us.
  1150. * (Curiously, at least one version of the MacOS 10.5 X server
  1151. * doesn't translate Tab for us. */
  1152. if (event->keyval == GDK_KEY_Tab && end <= 1) {
  1153. #ifdef KEY_EVENT_DIAGNOSTICS
  1154. debug((" - Tab, translating as 09\n"));
  1155. #endif
  1156. output[1] = '\t';
  1157. end = 2;
  1158. }
  1159. /*
  1160. * NetHack keypad mode.
  1161. */
  1162. if (nethack_mode) {
  1163. const char *keys = NULL;
  1164. switch (event->keyval) {
  1165. case GDK_KEY_KP_1: case GDK_KEY_KP_End:
  1166. keys = "bB\002"; break;
  1167. case GDK_KEY_KP_2: case GDK_KEY_KP_Down:
  1168. keys = "jJ\012"; break;
  1169. case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down:
  1170. keys = "nN\016"; break;
  1171. case GDK_KEY_KP_4: case GDK_KEY_KP_Left:
  1172. keys = "hH\010"; break;
  1173. case GDK_KEY_KP_5: case GDK_KEY_KP_Begin:
  1174. keys = "..."; break;
  1175. case GDK_KEY_KP_6: case GDK_KEY_KP_Right:
  1176. keys = "lL\014"; break;
  1177. case GDK_KEY_KP_7: case GDK_KEY_KP_Home:
  1178. keys = "yY\031"; break;
  1179. case GDK_KEY_KP_8: case GDK_KEY_KP_Up:
  1180. keys = "kK\013"; break;
  1181. case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up:
  1182. keys = "uU\025"; break;
  1183. }
  1184. if (keys) {
  1185. end = 2;
  1186. if (event->state & GDK_CONTROL_MASK)
  1187. output[1] = keys[2];
  1188. else if (event->state & GDK_SHIFT_MASK)
  1189. output[1] = keys[1];
  1190. else
  1191. output[1] = keys[0];
  1192. #ifdef KEY_EVENT_DIAGNOSTICS
  1193. debug((" - Nethack-mode key"));
  1194. #endif
  1195. use_ucsoutput = FALSE;
  1196. goto done;
  1197. }
  1198. }
  1199. /*
  1200. * Application keypad mode.
  1201. */
  1202. if (app_keypad_mode) {
  1203. int xkey = 0;
  1204. switch (event->keyval) {
  1205. case GDK_KEY_Num_Lock: xkey = 'P'; break;
  1206. case GDK_KEY_KP_Divide: xkey = 'Q'; break;
  1207. case GDK_KEY_KP_Multiply: xkey = 'R'; break;
  1208. case GDK_KEY_KP_Subtract: xkey = 'S'; break;
  1209. /*
  1210. * Keypad + is tricky. It covers a space that would
  1211. * be taken up on the VT100 by _two_ keys; so we
  1212. * let Shift select between the two. Worse still,
  1213. * in xterm function key mode we change which two...
  1214. */
  1215. case GDK_KEY_KP_Add:
  1216. if (conf_get_int(inst->conf, CONF_funky_type) == FUNKY_XTERM) {
  1217. if (event->state & GDK_SHIFT_MASK)
  1218. xkey = 'l';
  1219. else
  1220. xkey = 'k';
  1221. } else if (event->state & GDK_SHIFT_MASK)
  1222. xkey = 'm';
  1223. else
  1224. xkey = 'l';
  1225. break;
  1226. case GDK_KEY_KP_Enter: xkey = 'M'; break;
  1227. case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: xkey = 'p'; break;
  1228. case GDK_KEY_KP_1: case GDK_KEY_KP_End: xkey = 'q'; break;
  1229. case GDK_KEY_KP_2: case GDK_KEY_KP_Down: xkey = 'r'; break;
  1230. case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: xkey = 's'; break;
  1231. case GDK_KEY_KP_4: case GDK_KEY_KP_Left: xkey = 't'; break;
  1232. case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: xkey = 'u'; break;
  1233. case GDK_KEY_KP_6: case GDK_KEY_KP_Right: xkey = 'v'; break;
  1234. case GDK_KEY_KP_7: case GDK_KEY_KP_Home: xkey = 'w'; break;
  1235. case GDK_KEY_KP_8: case GDK_KEY_KP_Up: xkey = 'x'; break;
  1236. case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: xkey = 'y'; break;
  1237. case GDK_KEY_KP_Decimal: case GDK_KEY_KP_Delete:
  1238. xkey = 'n'; break;
  1239. }
  1240. if (xkey) {
  1241. if (inst->term->vt52_mode) {
  1242. if (xkey >= 'P' && xkey <= 'S')
  1243. end = 1 + sprintf(output+1, "\033%c", xkey);
  1244. else
  1245. end = 1 + sprintf(output+1, "\033?%c", xkey);
  1246. } else
  1247. end = 1 + sprintf(output+1, "\033O%c", xkey);
  1248. use_ucsoutput = FALSE;
  1249. #ifdef KEY_EVENT_DIAGNOSTICS
  1250. debug((" - Application keypad mode key"));
  1251. #endif
  1252. goto done;
  1253. }
  1254. }
  1255. /*
  1256. * Next, all the keys that do tilde codes. (ESC '[' nn '~',
  1257. * for integer decimal nn.)
  1258. *
  1259. * We also deal with the weird ones here. Linux VCs replace F1
  1260. * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
  1261. * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
  1262. * respectively.
  1263. */
  1264. {
  1265. int code = 0;
  1266. int funky_type = conf_get_int(inst->conf, CONF_funky_type);
  1267. switch (event->keyval) {
  1268. case GDK_KEY_F1:
  1269. code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
  1270. break;
  1271. case GDK_KEY_F2:
  1272. code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
  1273. break;
  1274. case GDK_KEY_F3:
  1275. code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
  1276. break;
  1277. case GDK_KEY_F4:
  1278. code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
  1279. break;
  1280. case GDK_KEY_F5:
  1281. code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
  1282. break;
  1283. case GDK_KEY_F6:
  1284. code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
  1285. break;
  1286. case GDK_KEY_F7:
  1287. code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
  1288. break;
  1289. case GDK_KEY_F8:
  1290. code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
  1291. break;
  1292. case GDK_KEY_F9:
  1293. code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
  1294. break;
  1295. case GDK_KEY_F10:
  1296. code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
  1297. break;
  1298. case GDK_KEY_F11:
  1299. code = 23;
  1300. break;
  1301. case GDK_KEY_F12:
  1302. code = 24;
  1303. break;
  1304. case GDK_KEY_F13:
  1305. code = 25;
  1306. break;
  1307. case GDK_KEY_F14:
  1308. code = 26;
  1309. break;
  1310. case GDK_KEY_F15:
  1311. code = 28;
  1312. break;
  1313. case GDK_KEY_F16:
  1314. code = 29;
  1315. break;
  1316. case GDK_KEY_F17:
  1317. code = 31;
  1318. break;
  1319. case GDK_KEY_F18:
  1320. code = 32;
  1321. break;
  1322. case GDK_KEY_F19:
  1323. code = 33;
  1324. break;
  1325. case GDK_KEY_F20:
  1326. code = 34;
  1327. break;
  1328. }
  1329. if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
  1330. case GDK_KEY_Home: case GDK_KEY_KP_Home:
  1331. code = 1;
  1332. break;
  1333. case GDK_KEY_Insert: case GDK_KEY_KP_Insert:
  1334. code = 2;
  1335. break;
  1336. case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
  1337. code = 3;
  1338. break;
  1339. case GDK_KEY_End: case GDK_KEY_KP_End:
  1340. code = 4;
  1341. break;
  1342. case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
  1343. code = 5;
  1344. break;
  1345. case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
  1346. code = 6;
  1347. break;
  1348. }
  1349. /* Reorder edit keys to physical order */
  1350. if (funky_type == FUNKY_VT400 && code <= 6)
  1351. code = "\0\2\1\4\5\3\6"[code];
  1352. if (inst->term->vt52_mode && code > 0 && code <= 6) {
  1353. end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
  1354. #ifdef KEY_EVENT_DIAGNOSTICS
  1355. debug((" - VT52 mode small keypad key"));
  1356. #endif
  1357. use_ucsoutput = FALSE;
  1358. goto done;
  1359. }
  1360. if (funky_type == FUNKY_SCO && /* SCO function keys */
  1361. code >= 11 && code <= 34) {
  1362. char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
  1363. int index = 0;
  1364. switch (event->keyval) {
  1365. case GDK_KEY_F1: index = 0; break;
  1366. case GDK_KEY_F2: index = 1; break;
  1367. case GDK_KEY_F3: index = 2; break;
  1368. case GDK_KEY_F4: index = 3; break;
  1369. case GDK_KEY_F5: index = 4; break;
  1370. case GDK_KEY_F6: index = 5; break;
  1371. case GDK_KEY_F7: index = 6; break;
  1372. case GDK_KEY_F8: index = 7; break;
  1373. case GDK_KEY_F9: index = 8; break;
  1374. case GDK_KEY_F10: index = 9; break;
  1375. case GDK_KEY_F11: index = 10; break;
  1376. case GDK_KEY_F12: index = 11; break;
  1377. }
  1378. if (event->state & GDK_SHIFT_MASK) index += 12;
  1379. if (event->state & GDK_CONTROL_MASK) index += 24;
  1380. end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
  1381. #ifdef KEY_EVENT_DIAGNOSTICS
  1382. debug((" - SCO mode function key"));
  1383. #endif
  1384. use_ucsoutput = FALSE;
  1385. goto done;
  1386. }
  1387. if (funky_type == FUNKY_SCO && /* SCO small keypad */
  1388. code >= 1 && code <= 6) {
  1389. char codes[] = "HL.FIG";
  1390. if (code == 3) {
  1391. output[1] = '\x7F';
  1392. end = 2;
  1393. } else {
  1394. end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
  1395. }
  1396. #ifdef KEY_EVENT_DIAGNOSTICS
  1397. debug((" - SCO mode small keypad key"));
  1398. #endif
  1399. use_ucsoutput = FALSE;
  1400. goto done;
  1401. }
  1402. if ((inst->term->vt52_mode || funky_type == FUNKY_VT100P) &&
  1403. code >= 11 && code <= 24) {
  1404. int offt = 0;
  1405. if (code > 15)
  1406. offt++;
  1407. if (code > 21)
  1408. offt++;
  1409. if (inst->term->vt52_mode) {
  1410. #ifdef KEY_EVENT_DIAGNOSTICS
  1411. debug((" - VT52 mode function key"));
  1412. #endif
  1413. end = 1 + sprintf(output+1,
  1414. "\x1B%c", code + 'P' - 11 - offt);
  1415. } else {
  1416. #ifdef KEY_EVENT_DIAGNOSTICS
  1417. debug((" - VT100+ mode function key"));
  1418. #endif
  1419. end = 1 + sprintf(output+1,
  1420. "\x1BO%c", code + 'P' - 11 - offt);
  1421. }
  1422. use_ucsoutput = FALSE;
  1423. goto done;
  1424. }
  1425. if (funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
  1426. end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
  1427. #ifdef KEY_EVENT_DIAGNOSTICS
  1428. debug((" - Linux mode F1-F5 function key"));
  1429. #endif
  1430. use_ucsoutput = FALSE;
  1431. goto done;
  1432. }
  1433. if (funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
  1434. if (inst->term->vt52_mode) {
  1435. #ifdef KEY_EVENT_DIAGNOSTICS
  1436. debug((" - VT52 mode (overriding xterm) F1-F4 function"
  1437. " key"));
  1438. #endif
  1439. end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
  1440. } else {
  1441. #ifdef KEY_EVENT_DIAGNOSTICS
  1442. debug((" - xterm mode F1-F4 function key"));
  1443. #endif
  1444. end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
  1445. }
  1446. use_ucsoutput = FALSE;
  1447. goto done;
  1448. }
  1449. if ((code == 1 || code == 4) &&
  1450. conf_get_int(inst->conf, CONF_rxvt_homeend)) {
  1451. #ifdef KEY_EVENT_DIAGNOSTICS
  1452. debug((" - rxvt style Home/End"));
  1453. #endif
  1454. end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
  1455. use_ucsoutput = FALSE;
  1456. goto done;
  1457. }
  1458. if (code) {
  1459. #ifdef KEY_EVENT_DIAGNOSTICS
  1460. debug((" - ordinary function key encoding"));
  1461. #endif
  1462. end = 1 + sprintf(output+1, "\x1B[%d~", code);
  1463. use_ucsoutput = FALSE;
  1464. goto done;
  1465. }
  1466. }
  1467. /*
  1468. * Cursor keys. (This includes the numberpad cursor keys,
  1469. * if we haven't already done them due to app keypad mode.)
  1470. *
  1471. * Here we also process un-numlocked un-appkeypadded KP5,
  1472. * which sends ESC [ G.
  1473. */
  1474. {
  1475. int xkey = 0;
  1476. switch (event->keyval) {
  1477. case GDK_KEY_Up: case GDK_KEY_KP_Up: xkey = 'A'; break;
  1478. case GDK_KEY_Down: case GDK_KEY_KP_Down: xkey = 'B'; break;
  1479. case GDK_KEY_Right: case GDK_KEY_KP_Right: xkey = 'C'; break;
  1480. case GDK_KEY_Left: case GDK_KEY_KP_Left: xkey = 'D'; break;
  1481. case GDK_KEY_Begin: case GDK_KEY_KP_Begin: xkey = 'G'; break;
  1482. }
  1483. if (xkey) {
  1484. end = 1 + format_arrow_key(output+1, inst->term, xkey,
  1485. event->state & GDK_CONTROL_MASK);
  1486. #ifdef KEY_EVENT_DIAGNOSTICS
  1487. debug((" - arrow key"));
  1488. #endif
  1489. use_ucsoutput = FALSE;
  1490. goto done;
  1491. }
  1492. }
  1493. goto done;
  1494. }
  1495. done:
  1496. if (end-start > 0) {
  1497. if (special) {
  1498. #ifdef KEY_EVENT_DIAGNOSTICS
  1499. char *string_string = dupstr("");
  1500. int i;
  1501. for (i = start; i < end; i++) {
  1502. char *old = string_string;
  1503. string_string = dupprintf("%s%s%02x", string_string,
  1504. string_string[0] ? " " : "",
  1505. (unsigned)output[i] & 0xFF);
  1506. sfree(old);
  1507. }
  1508. debug((" - final output, special, generic encoding = [%s]\n",
  1509. charset_to_localenc(output_charset), string_string));
  1510. sfree(string_string);
  1511. #endif
  1512. /*
  1513. * For special control characters, the character set
  1514. * should never matter.
  1515. */
  1516. output[end] = '\0'; /* NUL-terminate */
  1517. if (inst->ldisc)
  1518. ldisc_send(inst->ldisc, output+start, -2, 1);
  1519. } else if (!inst->direct_to_font) {
  1520. if (!use_ucsoutput) {
  1521. #ifdef KEY_EVENT_DIAGNOSTICS
  1522. char *string_string = dupstr("");
  1523. int i;
  1524. for (i = start; i < end; i++) {
  1525. char *old = string_string;
  1526. string_string = dupprintf("%s%s%02x", string_string,
  1527. string_string[0] ? " " : "",
  1528. (unsigned)output[i] & 0xFF);
  1529. sfree(old);
  1530. }
  1531. debug((" - final output in %s = [%s]\n",
  1532. charset_to_localenc(output_charset), string_string));
  1533. sfree(string_string);
  1534. #endif
  1535. if (inst->ldisc)
  1536. lpage_send(inst->ldisc, output_charset, output+start,
  1537. end-start, 1);
  1538. } else {
  1539. #ifdef KEY_EVENT_DIAGNOSTICS
  1540. char *string_string = dupstr("");
  1541. int i;
  1542. for (i = start; i < end; i++) {
  1543. char *old = string_string;
  1544. string_string = dupprintf("%s%s%04x", string_string,
  1545. string_string[0] ? " " : "",
  1546. (unsigned)ucsoutput[i]);
  1547. sfree(old);
  1548. }
  1549. debug((" - final output in Unicode = [%s]\n",
  1550. string_string));
  1551. sfree(string_string);
  1552. #endif
  1553. /*
  1554. * We generated our own Unicode key data from the
  1555. * keysym, so use that instead.
  1556. */
  1557. if (inst->ldisc)
  1558. luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
  1559. }
  1560. } else {
  1561. /*
  1562. * In direct-to-font mode, we just send the string
  1563. * exactly as we received it.
  1564. */
  1565. #ifdef KEY_EVENT_DIAGNOSTICS
  1566. char *string_string = dupstr("");
  1567. int i;
  1568. for (i = start; i < end; i++) {
  1569. char *old = string_string;
  1570. string_string = dupprintf("%s%s%02x", string_string,
  1571. string_string[0] ? " " : "",
  1572. (unsigned)output[i] & 0xFF);
  1573. sfree(old);
  1574. }
  1575. debug((" - final output in direct-to-font encoding = [%s]\n",
  1576. string_string));
  1577. sfree(string_string);
  1578. #endif
  1579. if (inst->ldisc)
  1580. ldisc_send(inst->ldisc, output+start, end-start, 1);
  1581. }
  1582. show_mouseptr(inst, 0);
  1583. term_seen_key_event(inst->term);
  1584. }
  1585. return TRUE;
  1586. }
  1587. #if GTK_CHECK_VERSION(2,0,0)
  1588. void input_method_commit_event(GtkIMContext *imc, gchar *str, gpointer data)
  1589. {
  1590. struct gui_data *inst = (struct gui_data *)data;
  1591. #ifdef KEY_EVENT_DIAGNOSTICS
  1592. char *string_string = dupstr("");
  1593. int i;
  1594. for (i = 0; str[i]; i++) {
  1595. char *old = string_string;
  1596. string_string = dupprintf("%s%s%02x", string_string,
  1597. string_string[0] ? " " : "",
  1598. (unsigned)str[i] & 0xFF);
  1599. sfree(old);
  1600. }
  1601. debug((" - IM commit event in UTF-8 = [%s]\n", string_string));
  1602. sfree(string_string);
  1603. #endif
  1604. if (inst->ldisc)
  1605. lpage_send(inst->ldisc, CS_UTF8, str, strlen(str), 1);
  1606. show_mouseptr(inst, 0);
  1607. term_seen_key_event(inst->term);
  1608. }
  1609. #endif
  1610. #define SCROLL_INCREMENT_LINES 5
  1611. #if GTK_CHECK_VERSION(3,4,0)
  1612. gboolean scroll_internal(struct gui_data *inst, gdouble delta, guint state,
  1613. gdouble ex, gdouble ey)
  1614. {
  1615. int shift, ctrl, alt, x, y, raw_mouse_mode;
  1616. show_mouseptr(inst, 1);
  1617. shift = state & GDK_SHIFT_MASK;
  1618. ctrl = state & GDK_CONTROL_MASK;
  1619. alt = state & inst->meta_mod_mask;
  1620. x = (ex - inst->window_border) / inst->font_width;
  1621. y = (ey - inst->window_border) / inst->font_height;
  1622. raw_mouse_mode =
  1623. send_raw_mouse && !(shift && conf_get_int(inst->conf,
  1624. CONF_mouse_override));
  1625. inst->cumulative_scroll += delta * SCROLL_INCREMENT_LINES;
  1626. if (!raw_mouse_mode) {
  1627. int scroll_lines = (int)inst->cumulative_scroll; /* rounds toward 0 */
  1628. if (scroll_lines) {
  1629. term_scroll(inst->term, 0, scroll_lines);
  1630. inst->cumulative_scroll -= scroll_lines;
  1631. }
  1632. return TRUE;
  1633. } else {
  1634. int scroll_events = (int)(inst->cumulative_scroll /
  1635. SCROLL_INCREMENT_LINES);
  1636. if (scroll_events) {
  1637. int button;
  1638. inst->cumulative_scroll -= scroll_events * SCROLL_INCREMENT_LINES;
  1639. if (scroll_events > 0) {
  1640. button = MBT_WHEEL_DOWN;
  1641. } else {
  1642. button = MBT_WHEEL_UP;
  1643. scroll_events = -scroll_events;
  1644. }
  1645. while (scroll_events-- > 0) {
  1646. term_mouse(inst->term, button, translate_button(button),
  1647. MA_CLICK, x, y, shift, ctrl, alt);
  1648. }
  1649. }
  1650. return TRUE;
  1651. }
  1652. }
  1653. #endif
  1654. gboolean button_internal(struct gui_data *inst, guint32 timestamp,
  1655. GdkEventType type, guint ebutton, guint state,
  1656. gdouble ex, gdouble ey)
  1657. {
  1658. int shift, ctrl, alt, x, y, button, act, raw_mouse_mode;
  1659. /* Remember the timestamp. */
  1660. inst->input_event_time = timestamp;
  1661. show_mouseptr(inst, 1);
  1662. shift = state & GDK_SHIFT_MASK;
  1663. ctrl = state & GDK_CONTROL_MASK;
  1664. alt = state & inst->meta_mod_mask;
  1665. raw_mouse_mode =
  1666. send_raw_mouse && !(shift && conf_get_int(inst->conf,
  1667. CONF_mouse_override));
  1668. if (!raw_mouse_mode) {
  1669. if (ebutton == 4 && type == GDK_BUTTON_PRESS) {
  1670. term_scroll(inst->term, 0, -SCROLL_INCREMENT_LINES);
  1671. return TRUE;
  1672. }
  1673. if (ebutton == 5 && type == GDK_BUTTON_PRESS) {
  1674. term_scroll(inst->term, 0, +SCROLL_INCREMENT_LINES);
  1675. return TRUE;
  1676. }
  1677. }
  1678. if (ebutton == 3 && ctrl) {
  1679. gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
  1680. ebutton, timestamp);
  1681. return TRUE;
  1682. }
  1683. if (ebutton == 1)
  1684. button = MBT_LEFT;
  1685. else if (ebutton == 2)
  1686. button = MBT_MIDDLE;
  1687. else if (ebutton == 3)
  1688. button = MBT_RIGHT;
  1689. else if (ebutton == 4)
  1690. button = MBT_WHEEL_UP;
  1691. else if (ebutton == 5)
  1692. button = MBT_WHEEL_DOWN;
  1693. else
  1694. return FALSE; /* don't even know what button! */
  1695. switch (type) {
  1696. case GDK_BUTTON_PRESS: act = MA_CLICK; break;
  1697. case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
  1698. case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
  1699. case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
  1700. default: return FALSE; /* don't know this event type */
  1701. }
  1702. if (raw_mouse_mode && act != MA_CLICK && act != MA_RELEASE)
  1703. return TRUE; /* we ignore these in raw mouse mode */
  1704. x = (ex - inst->window_border) / inst->font_width;
  1705. y = (ey - inst->window_border) / inst->font_height;
  1706. term_mouse(inst->term, button, translate_button(button), act,
  1707. x, y, shift, ctrl, alt);
  1708. return TRUE;
  1709. }
  1710. gboolean button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
  1711. {
  1712. struct gui_data *inst = (struct gui_data *)data;
  1713. return button_internal(inst, event->time, event->type, event->button,
  1714. event->state, event->x, event->y);
  1715. }
  1716. #if GTK_CHECK_VERSION(2,0,0)
  1717. /*
  1718. * In GTK 2, mouse wheel events have become a new type of event.
  1719. * This handler translates them back into button-4 and button-5
  1720. * presses so that I don't have to change my old code too much :-)
  1721. */
  1722. gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer data)
  1723. {
  1724. struct gui_data *inst = (struct gui_data *)data;
  1725. #if GTK_CHECK_VERSION(3,4,0)
  1726. gdouble dx, dy;
  1727. if (gdk_event_get_scroll_deltas((GdkEvent *)event, &dx, &dy)) {
  1728. return scroll_internal(inst, dy, event->state, event->x, event->y);
  1729. } else
  1730. return FALSE;
  1731. #else
  1732. guint button;
  1733. if (event->direction == GDK_SCROLL_UP)
  1734. button = 4;
  1735. else if (event->direction == GDK_SCROLL_DOWN)
  1736. button = 5;
  1737. else
  1738. return FALSE;
  1739. return button_internal(inst, event->time, GDK_BUTTON_PRESS,
  1740. button, event->state, event->x, event->y);
  1741. #endif
  1742. }
  1743. #endif
  1744. gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
  1745. {
  1746. struct gui_data *inst = (struct gui_data *)data;
  1747. int shift, ctrl, alt, x, y, button;
  1748. /* Remember the timestamp. */
  1749. inst->input_event_time = event->time;
  1750. show_mouseptr(inst, 1);
  1751. shift = event->state & GDK_SHIFT_MASK;
  1752. ctrl = event->state & GDK_CONTROL_MASK;
  1753. alt = event->state & inst->meta_mod_mask;
  1754. if (event->state & GDK_BUTTON1_MASK)
  1755. button = MBT_LEFT;
  1756. else if (event->state & GDK_BUTTON2_MASK)
  1757. button = MBT_MIDDLE;
  1758. else if (event->state & GDK_BUTTON3_MASK)
  1759. button = MBT_RIGHT;
  1760. else
  1761. return FALSE; /* don't even know what button! */
  1762. x = (event->x - inst->window_border) / inst->font_width;
  1763. y = (event->y - inst->window_border) / inst->font_height;
  1764. term_mouse(inst->term, button, translate_button(button), MA_DRAG,
  1765. x, y, shift, ctrl, alt);
  1766. return TRUE;
  1767. }
  1768. void frontend_keypress(void *handle)
  1769. {
  1770. struct gui_data *inst = (struct gui_data *)handle;
  1771. /*
  1772. * If our child process has exited but not closed, terminate on
  1773. * any keypress.
  1774. */
  1775. if (inst->exited)
  1776. cleanup_exit(0);
  1777. }
  1778. static void exit_callback(void *vinst)
  1779. {
  1780. struct gui_data *inst = (struct gui_data *)vinst;
  1781. int exitcode, close_on_exit;
  1782. if (!inst->exited &&
  1783. (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
  1784. inst->exited = TRUE;
  1785. close_on_exit = conf_get_int(inst->conf, CONF_close_on_exit);
  1786. if (close_on_exit == FORCE_ON ||
  1787. (close_on_exit == AUTO && exitcode == 0))
  1788. gtk_main_quit(); /* just go */
  1789. if (inst->ldisc) {
  1790. ldisc_free(inst->ldisc);
  1791. inst->ldisc = NULL;
  1792. }
  1793. inst->back->free(inst->backhandle);
  1794. inst->backhandle = NULL;
  1795. inst->back = NULL;
  1796. term_provide_resize_fn(inst->term, NULL, NULL);
  1797. update_specials_menu(inst);
  1798. gtk_widget_set_sensitive(inst->restartitem, TRUE);
  1799. }
  1800. }
  1801. void notify_remote_exit(void *frontend)
  1802. {
  1803. struct gui_data *inst = (struct gui_data *)frontend;
  1804. queue_toplevel_callback(exit_callback, inst);
  1805. }
  1806. void destroy(GtkWidget *widget, gpointer data)
  1807. {
  1808. gtk_main_quit();
  1809. }
  1810. gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
  1811. {
  1812. struct gui_data *inst = (struct gui_data *)data;
  1813. term_set_focus(inst->term, event->in);
  1814. term_update(inst->term);
  1815. show_mouseptr(inst, 1);
  1816. return FALSE;
  1817. }
  1818. void set_busy_status(void *frontend, int status)
  1819. {
  1820. struct gui_data *inst = (struct gui_data *)frontend;
  1821. inst->busy_status = status;
  1822. update_mouseptr(inst);
  1823. }
  1824. /*
  1825. * set or clear the "raw mouse message" mode
  1826. */
  1827. void set_raw_mouse_mode(void *frontend, int activate)
  1828. {
  1829. struct gui_data *inst = (struct gui_data *)frontend;
  1830. activate = activate && !conf_get_int(inst->conf, CONF_no_mouse_rep);
  1831. send_raw_mouse = activate;
  1832. update_mouseptr(inst);
  1833. }
  1834. #if GTK_CHECK_VERSION(2,0,0)
  1835. static void compute_whole_window_size(struct gui_data *inst,
  1836. int wchars, int hchars,
  1837. int *wpix, int *hpix);
  1838. #endif
  1839. void request_resize(void *frontend, int w, int h)
  1840. {
  1841. struct gui_data *inst = (struct gui_data *)frontend;
  1842. #if !GTK_CHECK_VERSION(3,0,0)
  1843. int large_x, large_y;
  1844. int offset_x, offset_y;
  1845. int area_x, area_y;
  1846. GtkRequisition inner, outer;
  1847. /*
  1848. * This is a heinous hack dreamed up by the gnome-terminal
  1849. * people to get around a limitation in gtk. The problem is
  1850. * that in order to set the size correctly we really need to be
  1851. * calling gtk_window_resize - but that needs to know the size
  1852. * of the _whole window_, not the drawing area. So what we do
  1853. * is to set an artificially huge size request on the drawing
  1854. * area, recompute the resulting size request on the window,
  1855. * and look at the difference between the two. That gives us
  1856. * the x and y offsets we need to translate drawing area size
  1857. * into window size for real, and then we call
  1858. * gtk_window_resize.
  1859. */
  1860. /*
  1861. * We start by retrieving the current size of the whole window.
  1862. * Adding a bit to _that_ will give us a value we can use as a
  1863. * bogus size request which guarantees to be bigger than the
  1864. * current size of the drawing area.
  1865. */
  1866. get_window_pixels(inst, &large_x, &large_y);
  1867. large_x += 32;
  1868. large_y += 32;
  1869. gtk_widget_set_size_request(inst->area, large_x, large_y);
  1870. gtk_widget_size_request(inst->area, &inner);
  1871. gtk_widget_size_request(inst->window, &outer);
  1872. offset_x = outer.width - inner.width;
  1873. offset_y = outer.height - inner.height;
  1874. area_x = inst->font_width * w + 2*inst->window_border;
  1875. area_y = inst->font_height * h + 2*inst->window_border;
  1876. /*
  1877. * Now we must set the size request on the drawing area back to
  1878. * something sensible before we commit the real resize. Best
  1879. * way to do this, I think, is to set it to what the size is
  1880. * really going to end up being.
  1881. */
  1882. gtk_widget_set_size_request(inst->area, area_x, area_y);
  1883. #if GTK_CHECK_VERSION(2,0,0)
  1884. gtk_window_resize(GTK_WINDOW(inst->window),
  1885. area_x + offset_x, area_y + offset_y);
  1886. #else
  1887. gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
  1888. /*
  1889. * I can no longer remember what this call to
  1890. * gtk_container_dequeue_resize_handler is for. It was
  1891. * introduced in r3092 with no comment, and the commit log
  1892. * message was uninformative. I'm _guessing_ its purpose is to
  1893. * prevent gratuitous resize processing on the window given
  1894. * that we're about to resize it anyway, but I have no idea
  1895. * why that's so incredibly vital.
  1896. *
  1897. * I've tried removing the call, and nothing seems to go
  1898. * wrong. I've backtracked to r3092 and tried removing the
  1899. * call there, and still nothing goes wrong. So I'm going to
  1900. * adopt the working hypothesis that it's superfluous; I won't
  1901. * actually remove it from the GTK 1.2 code, but I won't
  1902. * attempt to replicate its functionality in the GTK 2 code
  1903. * above.
  1904. */
  1905. gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
  1906. gdk_window_resize(gtk_widget_get_window(inst->window),
  1907. area_x + offset_x, area_y + offset_y);
  1908. #endif
  1909. #else /* GTK_CHECK_VERSION(3,0,0) */
  1910. int wp, hp;
  1911. compute_whole_window_size(inst, w, h, &wp, &hp);
  1912. gtk_window_resize(GTK_WINDOW(inst->window), wp, hp);
  1913. #endif
  1914. }
  1915. static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
  1916. {
  1917. inst->cols[n].red = r * 0x0101;
  1918. inst->cols[n].green = g * 0x0101;
  1919. inst->cols[n].blue = b * 0x0101;
  1920. #if !GTK_CHECK_VERSION(3,0,0)
  1921. {
  1922. gboolean success[1];
  1923. gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
  1924. gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
  1925. FALSE, TRUE, success);
  1926. if (!success[0])
  1927. g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
  1928. appname, n, r, g, b);
  1929. }
  1930. #endif
  1931. }
  1932. void set_gdk_window_background(GdkWindow *win, const GdkColor *col)
  1933. {
  1934. #if GTK_CHECK_VERSION(3,0,0)
  1935. /* gdk_window_set_background is deprecated; work around its
  1936. * absence. */
  1937. GdkRGBA rgba;
  1938. rgba.red = col->red / 65535.0;
  1939. rgba.green = col->green / 65535.0;
  1940. rgba.blue = col->blue / 65535.0;
  1941. rgba.alpha = 1.0;
  1942. gdk_window_set_background_rgba(win, &rgba);
  1943. #else
  1944. {
  1945. /* For GTK1, which doesn't have a 'const' on
  1946. * gdk_window_set_background's second parameter type. */
  1947. GdkColor col_mutable = *col;
  1948. gdk_window_set_background(win, &col_mutable);
  1949. }
  1950. #endif
  1951. }
  1952. void set_window_background(struct gui_data *inst)
  1953. {
  1954. if (inst->area && gtk_widget_get_window(inst->area))
  1955. set_gdk_window_background(gtk_widget_get_window(inst->area),
  1956. &inst->cols[258]);
  1957. if (inst->window && gtk_widget_get_window(inst->window))
  1958. set_gdk_window_background(gtk_widget_get_window(inst->window),
  1959. &inst->cols[258]);
  1960. }
  1961. void palette_set(void *frontend, int n, int r, int g, int b)
  1962. {
  1963. struct gui_data *inst = (struct gui_data *)frontend;
  1964. if (n >= 16)
  1965. n += 256 - 16;
  1966. if (n >= NALLCOLOURS)
  1967. return;
  1968. real_palette_set(inst, n, r, g, b);
  1969. if (n == 258) {
  1970. /* Default Background changed. Ensure space between text area and
  1971. * window border is redrawn */
  1972. set_window_background(inst);
  1973. draw_backing_rect(inst);
  1974. gtk_widget_queue_draw(inst->area);
  1975. }
  1976. }
  1977. void palette_reset(void *frontend)
  1978. {
  1979. struct gui_data *inst = (struct gui_data *)frontend;
  1980. /* This maps colour indices in inst->conf to those used in inst->cols. */
  1981. static const int ww[] = {
  1982. 256, 257, 258, 259, 260, 261,
  1983. 0, 8, 1, 9, 2, 10, 3, 11,
  1984. 4, 12, 5, 13, 6, 14, 7, 15
  1985. };
  1986. int i;
  1987. assert(lenof(ww) == NCFGCOLOURS);
  1988. #if !GTK_CHECK_VERSION(3,0,0)
  1989. if (!inst->colmap) {
  1990. inst->colmap = gdk_colormap_get_system();
  1991. } else {
  1992. gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
  1993. }
  1994. #endif
  1995. for (i = 0; i < NCFGCOLOURS; i++) {
  1996. inst->cols[ww[i]].red =
  1997. conf_get_int_int(inst->conf, CONF_colours, i*3+0) * 0x0101;
  1998. inst->cols[ww[i]].green =
  1999. conf_get_int_int(inst->conf, CONF_colours, i*3+1) * 0x0101;
  2000. inst->cols[ww[i]].blue =
  2001. conf_get_int_int(inst->conf, CONF_colours, i*3+2) * 0x0101;
  2002. }
  2003. for (i = 0; i < NEXTCOLOURS; i++) {
  2004. if (i < 216) {
  2005. int r = i / 36, g = (i / 6) % 6, b = i % 6;
  2006. inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
  2007. inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
  2008. inst->cols[i+16].blue = b ? b * 0x2828 + 0x3737 : 0;
  2009. } else {
  2010. int shade = i - 216;
  2011. shade = shade * 0x0a0a + 0x0808;
  2012. inst->cols[i+16].red = inst->cols[i+16].green =
  2013. inst->cols[i+16].blue = shade;
  2014. }
  2015. }
  2016. #if !GTK_CHECK_VERSION(3,0,0)
  2017. {
  2018. gboolean success[NALLCOLOURS];
  2019. gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
  2020. FALSE, TRUE, success);
  2021. for (i = 0; i < NALLCOLOURS; i++) {
  2022. if (!success[i])
  2023. g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
  2024. appname, i,
  2025. conf_get_int_int(inst->conf, CONF_colours, i*3+0),
  2026. conf_get_int_int(inst->conf, CONF_colours, i*3+1),
  2027. conf_get_int_int(inst->conf, CONF_colours, i*3+2));
  2028. }
  2029. }
  2030. #endif
  2031. /* Since Default Background may have changed, ensure that space
  2032. * between text area and window border is refreshed. */
  2033. set_window_background(inst);
  2034. if (inst->area && gtk_widget_get_window(inst->area)) {
  2035. draw_backing_rect(inst);
  2036. gtk_widget_queue_draw(inst->area);
  2037. }
  2038. }
  2039. #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
  2040. /* ----------------------------------------------------------------------
  2041. * Clipboard handling, using the high-level GtkClipboard interface in
  2042. * as hands-off a way as possible. We write and read the clipboard as
  2043. * UTF-8 text, and let GTK deal with converting to any other text
  2044. * formats it feels like.
  2045. */
  2046. void init_clipboard(struct gui_data *inst)
  2047. {
  2048. inst->clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(),
  2049. DEFAULT_CLIPBOARD);
  2050. }
  2051. /*
  2052. * Because calling gtk_clipboard_set_with_data triggers a call to the
  2053. * clipboard_clear function from the last time, we need to arrange a
  2054. * way to distinguish a real call to clipboard_clear for the _new_
  2055. * instance of the clipboard data from the leftover call for the
  2056. * outgoing one. We do this by setting the user data field in our
  2057. * gtk_clipboard_set_with_data() call, instead of the obvious pointer
  2058. * to 'inst', to one of these.
  2059. */
  2060. struct clipboard_data_instance {
  2061. struct gui_data *inst;
  2062. char *pasteout_data_utf8;
  2063. int pasteout_data_utf8_len;
  2064. };
  2065. static void clipboard_provide_data(GtkClipboard *clipboard,
  2066. GtkSelectionData *selection_data,
  2067. guint info, gpointer data)
  2068. {
  2069. struct clipboard_data_instance *cdi =
  2070. (struct clipboard_data_instance *)data;
  2071. struct gui_data *inst = cdi->inst;
  2072. if (inst->current_cdi == cdi) {
  2073. gtk_selection_data_set_text(selection_data, cdi->pasteout_data_utf8,
  2074. cdi->pasteout_data_utf8_len);
  2075. }
  2076. }
  2077. static void clipboard_clear(GtkClipboard *clipboard, gpointer data)
  2078. {
  2079. struct clipboard_data_instance *cdi =
  2080. (struct clipboard_data_instance *)data;
  2081. struct gui_data *inst = cdi->inst;
  2082. if (inst->current_cdi == cdi) {
  2083. term_deselect(inst->term);
  2084. inst->current_cdi = NULL;
  2085. }
  2086. sfree(cdi->pasteout_data_utf8);
  2087. sfree(cdi);
  2088. }
  2089. void write_clip(void *frontend, wchar_t *data, int *attr, int len,
  2090. int must_deselect)
  2091. {
  2092. struct gui_data *inst = (struct gui_data *)frontend;
  2093. struct clipboard_data_instance *cdi;
  2094. if (inst->direct_to_font) {
  2095. /* In this clipboard mode, we just can't paste if we're in
  2096. * direct-to-font mode. Fortunately, that shouldn't be
  2097. * important, because we'll only use this clipboard handling
  2098. * code on systems where that kind of font doesn't exist
  2099. * anyway. */
  2100. return;
  2101. }
  2102. cdi = snew(struct clipboard_data_instance);
  2103. cdi->inst = inst;
  2104. inst->current_cdi = cdi;
  2105. cdi->pasteout_data_utf8 = snewn(len*6, char);
  2106. {
  2107. const wchar_t *tmp = data;
  2108. int tmplen = len;
  2109. cdi->pasteout_data_utf8_len =
  2110. charset_from_unicode(&tmp, &tmplen, cdi->pasteout_data_utf8,
  2111. len*6, CS_UTF8, NULL, NULL, 0);
  2112. }
  2113. /*
  2114. * It would be nice to just call gtk_clipboard_set_text() in place
  2115. * of all of the faffing below. Unfortunately, that won't give me
  2116. * access to the clipboard-clear event, which we use to visually
  2117. * deselect text in the terminal.
  2118. */
  2119. {
  2120. GtkTargetList *targetlist;
  2121. GtkTargetEntry *targettable;
  2122. gint n_targets;
  2123. targetlist = gtk_target_list_new(NULL, 0);
  2124. gtk_target_list_add_text_targets(targetlist, 0);
  2125. targettable = gtk_target_table_new_from_list(targetlist, &n_targets);
  2126. gtk_clipboard_set_with_data(inst->clipboard, targettable, n_targets,
  2127. clipboard_provide_data, clipboard_clear,
  2128. cdi);
  2129. gtk_target_table_free(targettable, n_targets);
  2130. gtk_target_list_unref(targetlist);
  2131. }
  2132. }
  2133. static void clipboard_text_received(GtkClipboard *clipboard,
  2134. const gchar *text, gpointer data)
  2135. {
  2136. struct gui_data *inst = (struct gui_data *)data;
  2137. int length;
  2138. if (!text)
  2139. return;
  2140. length = strlen(text);
  2141. if (inst->pastein_data)
  2142. sfree(inst->pastein_data);
  2143. inst->pastein_data = snewn(length, wchar_t);
  2144. inst->pastein_data_len = mb_to_wc(CS_UTF8, 0, text, length,
  2145. inst->pastein_data, length);
  2146. term_do_paste(inst->term);
  2147. }
  2148. void request_paste(void *frontend)
  2149. {
  2150. struct gui_data *inst = (struct gui_data *)frontend;
  2151. gtk_clipboard_request_text(inst->clipboard, clipboard_text_received, inst);
  2152. }
  2153. #else /* JUST_USE_GTK_CLIPBOARD_UTF8 */
  2154. /* ----------------------------------------------------------------------
  2155. * Clipboard handling for X, using the low-level gtk_selection_*
  2156. * interface, handling conversions to fiddly things like compound text
  2157. * ourselves, and storing in X cut buffers too.
  2158. *
  2159. * This version of the clipboard code has to be kept around for GTK1,
  2160. * which doesn't have the higher-level GtkClipboard interface at all.
  2161. * And since it works on GTK2 and GTK3 too and has had a good few
  2162. * years of shakedown and bug fixing, we might as well keep using it
  2163. * where it's applicable.
  2164. *
  2165. * It's _possible_ that we might be able to replicate all the
  2166. * important wrinkles of this code in GtkClipboard. (In particular,
  2167. * cut buffers or local analogue look as if they might be accessible
  2168. * via gtk_clipboard_set_can_store(), and delivering text in
  2169. * non-Unicode formats only in the direct-to-font case ought to be
  2170. * possible if we can figure out the right set of things to put in the
  2171. * GtkTargetList.) But that work can wait until there's a need for it!
  2172. */
  2173. /* Store the data in a cut-buffer. */
  2174. static void store_cutbuffer(char * ptr, int len)
  2175. {
  2176. #ifndef NOT_X_WINDOWS
  2177. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  2178. /* ICCCM says we must rotate the buffers before storing to buffer 0. */
  2179. XRotateBuffers(disp, 1);
  2180. XStoreBytes(disp, ptr, len);
  2181. #endif
  2182. }
  2183. /* Retrieve data from a cut-buffer.
  2184. * Returned data needs to be freed with XFree().
  2185. */
  2186. static char *retrieve_cutbuffer(int *nbytes)
  2187. {
  2188. #ifndef NOT_X_WINDOWS
  2189. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  2190. char * ptr;
  2191. ptr = XFetchBytes(disp, nbytes);
  2192. if (*nbytes <= 0 && ptr != 0) {
  2193. XFree(ptr);
  2194. ptr = 0;
  2195. }
  2196. return ptr;
  2197. #else
  2198. return NULL;
  2199. #endif
  2200. }
  2201. void write_clip(void *frontend, wchar_t *data, int *attr, int len,
  2202. int must_deselect)
  2203. {
  2204. struct gui_data *inst = (struct gui_data *)frontend;
  2205. if (inst->pasteout_data)
  2206. sfree(inst->pasteout_data);
  2207. if (inst->pasteout_data_ctext)
  2208. sfree(inst->pasteout_data_ctext);
  2209. if (inst->pasteout_data_utf8)
  2210. sfree(inst->pasteout_data_utf8);
  2211. /*
  2212. * Set up UTF-8 and compound text paste data. This only happens
  2213. * if we aren't in direct-to-font mode using the D800 hack.
  2214. */
  2215. if (!inst->direct_to_font) {
  2216. const wchar_t *tmp = data;
  2217. int tmplen = len;
  2218. #ifndef NOT_X_WINDOWS
  2219. XTextProperty tp;
  2220. char *list[1];
  2221. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  2222. #endif
  2223. inst->pasteout_data_utf8 = snewn(len*6, char);
  2224. inst->pasteout_data_utf8_len = len*6;
  2225. inst->pasteout_data_utf8_len =
  2226. charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
  2227. inst->pasteout_data_utf8_len,
  2228. CS_UTF8, NULL, NULL, 0);
  2229. if (inst->pasteout_data_utf8_len == 0) {
  2230. sfree(inst->pasteout_data_utf8);
  2231. inst->pasteout_data_utf8 = NULL;
  2232. } else {
  2233. inst->pasteout_data_utf8 =
  2234. sresize(inst->pasteout_data_utf8,
  2235. inst->pasteout_data_utf8_len + 1, char);
  2236. inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
  2237. }
  2238. /*
  2239. * Now let Xlib convert our UTF-8 data into compound text.
  2240. */
  2241. #ifndef NOT_X_WINDOWS
  2242. list[0] = inst->pasteout_data_utf8;
  2243. if (Xutf8TextListToTextProperty(disp, list, 1,
  2244. XCompoundTextStyle, &tp) == 0) {
  2245. inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
  2246. memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
  2247. inst->pasteout_data_ctext_len = tp.nitems;
  2248. XFree(tp.value);
  2249. } else
  2250. #endif
  2251. {
  2252. inst->pasteout_data_ctext = NULL;
  2253. inst->pasteout_data_ctext_len = 0;
  2254. }
  2255. } else {
  2256. inst->pasteout_data_utf8 = NULL;
  2257. inst->pasteout_data_utf8_len = 0;
  2258. inst->pasteout_data_ctext = NULL;
  2259. inst->pasteout_data_ctext_len = 0;
  2260. }
  2261. inst->pasteout_data = snewn(len*6, char);
  2262. inst->pasteout_data_len = len*6;
  2263. inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
  2264. data, len, inst->pasteout_data,
  2265. inst->pasteout_data_len,
  2266. NULL, NULL, NULL);
  2267. if (inst->pasteout_data_len == 0) {
  2268. sfree(inst->pasteout_data);
  2269. inst->pasteout_data = NULL;
  2270. } else {
  2271. inst->pasteout_data =
  2272. sresize(inst->pasteout_data, inst->pasteout_data_len, char);
  2273. }
  2274. store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
  2275. if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
  2276. inst->input_event_time)) {
  2277. #if GTK_CHECK_VERSION(2,0,0)
  2278. gtk_selection_clear_targets(inst->area, GDK_SELECTION_PRIMARY);
  2279. #endif
  2280. gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
  2281. GDK_SELECTION_TYPE_STRING, 1);
  2282. if (inst->pasteout_data_ctext)
  2283. gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
  2284. compound_text_atom, 1);
  2285. if (inst->pasteout_data_utf8)
  2286. gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
  2287. utf8_string_atom, 1);
  2288. }
  2289. if (must_deselect)
  2290. term_deselect(inst->term);
  2291. }
  2292. static void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
  2293. guint info, guint time_stamp, gpointer data)
  2294. {
  2295. struct gui_data *inst = (struct gui_data *)data;
  2296. GdkAtom target = gtk_selection_data_get_target(seldata);
  2297. if (target == utf8_string_atom)
  2298. gtk_selection_data_set(seldata, target, 8,
  2299. (unsigned char *)inst->pasteout_data_utf8,
  2300. inst->pasteout_data_utf8_len);
  2301. else if (target == compound_text_atom)
  2302. gtk_selection_data_set(seldata, target, 8,
  2303. (unsigned char *)inst->pasteout_data_ctext,
  2304. inst->pasteout_data_ctext_len);
  2305. else
  2306. gtk_selection_data_set(seldata, target, 8,
  2307. (unsigned char *)inst->pasteout_data,
  2308. inst->pasteout_data_len);
  2309. }
  2310. static gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
  2311. gpointer data)
  2312. {
  2313. struct gui_data *inst = (struct gui_data *)data;
  2314. term_deselect(inst->term);
  2315. if (inst->pasteout_data)
  2316. sfree(inst->pasteout_data);
  2317. if (inst->pasteout_data_ctext)
  2318. sfree(inst->pasteout_data_ctext);
  2319. if (inst->pasteout_data_utf8)
  2320. sfree(inst->pasteout_data_utf8);
  2321. inst->pasteout_data = NULL;
  2322. inst->pasteout_data_len = 0;
  2323. inst->pasteout_data_ctext = NULL;
  2324. inst->pasteout_data_ctext_len = 0;
  2325. inst->pasteout_data_utf8 = NULL;
  2326. inst->pasteout_data_utf8_len = 0;
  2327. return TRUE;
  2328. }
  2329. void request_paste(void *frontend)
  2330. {
  2331. struct gui_data *inst = (struct gui_data *)frontend;
  2332. /*
  2333. * In Unix, pasting is asynchronous: all we can do at the
  2334. * moment is to call gtk_selection_convert(), and when the data
  2335. * comes back _then_ we can call term_do_paste().
  2336. */
  2337. if (!inst->direct_to_font) {
  2338. /*
  2339. * First we attempt to retrieve the selection as a UTF-8
  2340. * string (which we will convert to the correct code page
  2341. * before sending to the session, of course). If that
  2342. * fails, selection_received() will be informed and will
  2343. * fall back to an ordinary string.
  2344. */
  2345. gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
  2346. utf8_string_atom,
  2347. inst->input_event_time);
  2348. } else {
  2349. /*
  2350. * If we're in direct-to-font mode, we disable UTF-8
  2351. * pasting, and go straight to ordinary string data.
  2352. */
  2353. gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
  2354. GDK_SELECTION_TYPE_STRING,
  2355. inst->input_event_time);
  2356. }
  2357. }
  2358. static void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
  2359. guint time, gpointer data)
  2360. {
  2361. struct gui_data *inst = (struct gui_data *)data;
  2362. char *text;
  2363. int length;
  2364. #ifndef NOT_X_WINDOWS
  2365. char **list;
  2366. int free_list_required = 0;
  2367. int free_required = 0;
  2368. #endif
  2369. int charset;
  2370. GdkAtom seldata_target = gtk_selection_data_get_target(seldata);
  2371. GdkAtom seldata_type = gtk_selection_data_get_data_type(seldata);
  2372. const guchar *seldata_data = gtk_selection_data_get_data(seldata);
  2373. gint seldata_length = gtk_selection_data_get_length(seldata);
  2374. if (seldata_target == utf8_string_atom && seldata_length <= 0) {
  2375. /*
  2376. * Failed to get a UTF-8 selection string. Try compound
  2377. * text next.
  2378. */
  2379. gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
  2380. compound_text_atom,
  2381. inst->input_event_time);
  2382. return;
  2383. }
  2384. if (seldata_target == compound_text_atom && seldata_length <= 0) {
  2385. /*
  2386. * Failed to get UTF-8 or compound text. Try an ordinary
  2387. * string.
  2388. */
  2389. gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
  2390. GDK_SELECTION_TYPE_STRING,
  2391. inst->input_event_time);
  2392. return;
  2393. }
  2394. /*
  2395. * If we have data, but it's not of a type we can deal with,
  2396. * we have to ignore the data.
  2397. */
  2398. if (seldata_length > 0 &&
  2399. seldata_type != GDK_SELECTION_TYPE_STRING &&
  2400. seldata_type != compound_text_atom &&
  2401. seldata_type != utf8_string_atom)
  2402. return;
  2403. /*
  2404. * If we have no data, try looking in a cut buffer.
  2405. */
  2406. if (seldata_length <= 0) {
  2407. #ifndef NOT_X_WINDOWS
  2408. text = retrieve_cutbuffer(&length);
  2409. if (length == 0)
  2410. return;
  2411. /* Xterm is rumoured to expect Latin-1, though I havn't checked the
  2412. * source, so use that as a de-facto standard. */
  2413. charset = CS_ISO8859_1;
  2414. free_required = 1;
  2415. #else
  2416. return;
  2417. #endif
  2418. } else {
  2419. /*
  2420. * Convert COMPOUND_TEXT into UTF-8.
  2421. */
  2422. if (seldata_type == compound_text_atom) {
  2423. #ifndef NOT_X_WINDOWS
  2424. XTextProperty tp;
  2425. int ret, count;
  2426. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  2427. tp.value = (unsigned char *)seldata_data;
  2428. tp.encoding = (Atom) seldata_type;
  2429. tp.format = gtk_selection_data_get_format(seldata);
  2430. tp.nitems = seldata_length;
  2431. ret = Xutf8TextPropertyToTextList(disp, &tp, &list, &count);
  2432. if (ret == 0 && count == 1) {
  2433. text = list[0];
  2434. length = strlen(list[0]);
  2435. charset = CS_UTF8;
  2436. free_list_required = 1;
  2437. } else
  2438. #endif
  2439. {
  2440. /*
  2441. * Compound text failed; fall back to STRING.
  2442. */
  2443. gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
  2444. GDK_SELECTION_TYPE_STRING,
  2445. inst->input_event_time);
  2446. return;
  2447. }
  2448. } else {
  2449. text = (char *)seldata_data;
  2450. length = seldata_length;
  2451. charset = (seldata_type == utf8_string_atom ?
  2452. CS_UTF8 : inst->ucsdata.line_codepage);
  2453. }
  2454. }
  2455. if (inst->pastein_data)
  2456. sfree(inst->pastein_data);
  2457. inst->pastein_data = snewn(length, wchar_t);
  2458. inst->pastein_data_len = length;
  2459. inst->pastein_data_len =
  2460. mb_to_wc(charset, 0, text, length,
  2461. inst->pastein_data, inst->pastein_data_len);
  2462. term_do_paste(inst->term);
  2463. #ifndef NOT_X_WINDOWS
  2464. if (free_list_required)
  2465. XFreeStringList(list);
  2466. if (free_required)
  2467. XFree(text);
  2468. #endif
  2469. }
  2470. void init_clipboard(struct gui_data *inst)
  2471. {
  2472. #ifndef NOT_X_WINDOWS
  2473. /*
  2474. * Ensure that all the cut buffers exist - according to the ICCCM,
  2475. * we must do this before we start using cut buffers.
  2476. */
  2477. unsigned char empty[] = "";
  2478. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  2479. x11_ignore_error(disp, BadMatch);
  2480. XChangeProperty(disp, GDK_ROOT_WINDOW(),
  2481. XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
  2482. x11_ignore_error(disp, BadMatch);
  2483. XChangeProperty(disp, GDK_ROOT_WINDOW(),
  2484. XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
  2485. x11_ignore_error(disp, BadMatch);
  2486. XChangeProperty(disp, GDK_ROOT_WINDOW(),
  2487. XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
  2488. x11_ignore_error(disp, BadMatch);
  2489. XChangeProperty(disp, GDK_ROOT_WINDOW(),
  2490. XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
  2491. x11_ignore_error(disp, BadMatch);
  2492. XChangeProperty(disp, GDK_ROOT_WINDOW(),
  2493. XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
  2494. x11_ignore_error(disp, BadMatch);
  2495. XChangeProperty(disp, GDK_ROOT_WINDOW(),
  2496. XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
  2497. x11_ignore_error(disp, BadMatch);
  2498. XChangeProperty(disp, GDK_ROOT_WINDOW(),
  2499. XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
  2500. x11_ignore_error(disp, BadMatch);
  2501. XChangeProperty(disp, GDK_ROOT_WINDOW(),
  2502. XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
  2503. #endif
  2504. g_signal_connect(G_OBJECT(inst->area), "selection_received",
  2505. G_CALLBACK(selection_received), inst);
  2506. g_signal_connect(G_OBJECT(inst->area), "selection_get",
  2507. G_CALLBACK(selection_get), inst);
  2508. g_signal_connect(G_OBJECT(inst->area), "selection_clear_event",
  2509. G_CALLBACK(selection_clear), inst);
  2510. }
  2511. /*
  2512. * End of selection/clipboard handling.
  2513. * ----------------------------------------------------------------------
  2514. */
  2515. #endif /* JUST_USE_GTK_CLIPBOARD_UTF8 */
  2516. void get_clip(void *frontend, wchar_t ** p, int *len)
  2517. {
  2518. struct gui_data *inst = (struct gui_data *)frontend;
  2519. if (p) {
  2520. *p = inst->pastein_data;
  2521. *len = inst->pastein_data_len;
  2522. }
  2523. }
  2524. static void set_window_titles(struct gui_data *inst)
  2525. {
  2526. /*
  2527. * We must always call set_icon_name after calling set_title,
  2528. * since set_title will write both names. Irritating, but such
  2529. * is life.
  2530. */
  2531. gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
  2532. if (!conf_get_int(inst->conf, CONF_win_name_always))
  2533. gdk_window_set_icon_name(gtk_widget_get_window(inst->window),
  2534. inst->icontitle);
  2535. }
  2536. void set_title(void *frontend, char *title)
  2537. {
  2538. struct gui_data *inst = (struct gui_data *)frontend;
  2539. sfree(inst->wintitle);
  2540. inst->wintitle = dupstr(title);
  2541. set_window_titles(inst);
  2542. }
  2543. void set_icon(void *frontend, char *title)
  2544. {
  2545. struct gui_data *inst = (struct gui_data *)frontend;
  2546. sfree(inst->icontitle);
  2547. inst->icontitle = dupstr(title);
  2548. set_window_titles(inst);
  2549. }
  2550. void set_title_and_icon(void *frontend, char *title, char *icon)
  2551. {
  2552. struct gui_data *inst = (struct gui_data *)frontend;
  2553. sfree(inst->wintitle);
  2554. inst->wintitle = dupstr(title);
  2555. sfree(inst->icontitle);
  2556. inst->icontitle = dupstr(icon);
  2557. set_window_titles(inst);
  2558. }
  2559. void set_sbar(void *frontend, int total, int start, int page)
  2560. {
  2561. struct gui_data *inst = (struct gui_data *)frontend;
  2562. if (!conf_get_int(inst->conf, CONF_scrollbar))
  2563. return;
  2564. gtk_adjustment_set_lower(inst->sbar_adjust, 0);
  2565. gtk_adjustment_set_upper(inst->sbar_adjust, total);
  2566. gtk_adjustment_set_value(inst->sbar_adjust, start);
  2567. gtk_adjustment_set_page_size(inst->sbar_adjust, page);
  2568. gtk_adjustment_set_step_increment(inst->sbar_adjust, 1);
  2569. gtk_adjustment_set_page_increment(inst->sbar_adjust, page/2);
  2570. inst->ignore_sbar = TRUE;
  2571. #if !GTK_CHECK_VERSION(3,18,0)
  2572. gtk_adjustment_changed(inst->sbar_adjust);
  2573. #endif
  2574. inst->ignore_sbar = FALSE;
  2575. }
  2576. void scrollbar_moved(GtkAdjustment *adj, gpointer data)
  2577. {
  2578. struct gui_data *inst = (struct gui_data *)data;
  2579. if (!conf_get_int(inst->conf, CONF_scrollbar))
  2580. return;
  2581. if (!inst->ignore_sbar)
  2582. term_scroll(inst->term, 1, (int)gtk_adjustment_get_value(adj));
  2583. }
  2584. static void show_scrollbar(struct gui_data *inst, gboolean visible)
  2585. {
  2586. inst->sbar_visible = visible;
  2587. if (visible)
  2588. gtk_widget_show(inst->sbar);
  2589. else
  2590. gtk_widget_hide(inst->sbar);
  2591. }
  2592. void sys_cursor(void *frontend, int x, int y)
  2593. {
  2594. /*
  2595. * This is meaningless under X.
  2596. */
  2597. }
  2598. /*
  2599. * This is still called when mode==BELL_VISUAL, even though the
  2600. * visual bell is handled entirely within terminal.c, because we
  2601. * may want to perform additional actions on any kind of bell (for
  2602. * example, taskbar flashing in Windows).
  2603. */
  2604. void do_beep(void *frontend, int mode)
  2605. {
  2606. if (mode == BELL_DEFAULT)
  2607. gdk_beep();
  2608. }
  2609. int char_width(Context ctx, int uc)
  2610. {
  2611. /*
  2612. * In this front end, double-width characters are handled using a
  2613. * separate font, so this can safely just return 1 always.
  2614. */
  2615. return 1;
  2616. }
  2617. Context get_ctx(void *frontend)
  2618. {
  2619. struct gui_data *inst = (struct gui_data *)frontend;
  2620. struct draw_ctx *dctx;
  2621. if (!gtk_widget_get_window(inst->area))
  2622. return NULL;
  2623. dctx = snew(struct draw_ctx);
  2624. dctx->inst = inst;
  2625. dctx->uctx.type = inst->drawtype;
  2626. #ifdef DRAW_TEXT_GDK
  2627. if (dctx->uctx.type == DRAWTYPE_GDK) {
  2628. /* If we're doing GDK-based drawing, then we also expect
  2629. * inst->pixmap to exist. */
  2630. dctx->uctx.u.gdk.target = inst->pixmap;
  2631. dctx->uctx.u.gdk.gc = gdk_gc_new(gtk_widget_get_window(inst->area));
  2632. }
  2633. #endif
  2634. #ifdef DRAW_TEXT_CAIRO
  2635. if (dctx->uctx.type == DRAWTYPE_CAIRO) {
  2636. dctx->uctx.u.cairo.widget = GTK_WIDGET(inst->area);
  2637. /* If we're doing Cairo drawing, we expect inst->surface to
  2638. * exist, and we draw to that first, regardless of whether we
  2639. * subsequently copy the results to inst->pixmap. */
  2640. dctx->uctx.u.cairo.cr = cairo_create(inst->surface);
  2641. cairo_setup_dctx(dctx);
  2642. }
  2643. #endif
  2644. return dctx;
  2645. }
  2646. void free_ctx(Context ctx)
  2647. {
  2648. struct draw_ctx *dctx = (struct draw_ctx *)ctx;
  2649. /* struct gui_data *inst = dctx->inst; */
  2650. #ifdef DRAW_TEXT_GDK
  2651. if (dctx->uctx.type == DRAWTYPE_GDK) {
  2652. gdk_gc_unref(dctx->uctx.u.gdk.gc);
  2653. }
  2654. #endif
  2655. #ifdef DRAW_TEXT_CAIRO
  2656. if (dctx->uctx.type == DRAWTYPE_CAIRO) {
  2657. cairo_destroy(dctx->uctx.u.cairo.cr);
  2658. }
  2659. #endif
  2660. sfree(dctx);
  2661. }
  2662. static void draw_update(struct draw_ctx *dctx, int x, int y, int w, int h)
  2663. {
  2664. #if defined DRAW_TEXT_CAIRO && !defined NO_BACKING_PIXMAPS
  2665. if (dctx->uctx.type == DRAWTYPE_CAIRO) {
  2666. /*
  2667. * If inst->surface and inst->pixmap both exist, then we've
  2668. * just drawn new content to the former which we must copy to
  2669. * the latter.
  2670. */
  2671. cairo_t *cr = gdk_cairo_create(dctx->inst->pixmap);
  2672. cairo_set_source_surface(cr, dctx->inst->surface, 0, 0);
  2673. cairo_rectangle(cr, x, y, w, h);
  2674. cairo_fill(cr);
  2675. cairo_destroy(cr);
  2676. }
  2677. #endif
  2678. /*
  2679. * Now we just queue a window redraw, which will cause
  2680. * inst->surface or inst->pixmap (whichever is appropriate for our
  2681. * compile mode) to be copied to the real window when we receive
  2682. * the resulting "expose" or "draw" event.
  2683. *
  2684. * Amazingly, this one API call is actually valid in all versions
  2685. * of GTK :-)
  2686. */
  2687. gtk_widget_queue_draw_area(dctx->inst->area, x, y, w, h);
  2688. }
  2689. static void draw_set_colour(struct draw_ctx *dctx, int col)
  2690. {
  2691. #ifdef DRAW_TEXT_GDK
  2692. if (dctx->uctx.type == DRAWTYPE_GDK) {
  2693. gdk_gc_set_foreground(dctx->uctx.u.gdk.gc, &dctx->inst->cols[col]);
  2694. }
  2695. #endif
  2696. #ifdef DRAW_TEXT_CAIRO
  2697. if (dctx->uctx.type == DRAWTYPE_CAIRO) {
  2698. cairo_set_source_rgb(dctx->uctx.u.cairo.cr,
  2699. dctx->inst->cols[col].red / 65535.0,
  2700. dctx->inst->cols[col].green / 65535.0,
  2701. dctx->inst->cols[col].blue / 65535.0);
  2702. }
  2703. #endif
  2704. }
  2705. static void draw_rectangle(struct draw_ctx *dctx, int filled,
  2706. int x, int y, int w, int h)
  2707. {
  2708. #ifdef DRAW_TEXT_GDK
  2709. if (dctx->uctx.type == DRAWTYPE_GDK) {
  2710. gdk_draw_rectangle(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc,
  2711. filled, x, y, w, h);
  2712. }
  2713. #endif
  2714. #ifdef DRAW_TEXT_CAIRO
  2715. if (dctx->uctx.type == DRAWTYPE_CAIRO) {
  2716. cairo_new_path(dctx->uctx.u.cairo.cr);
  2717. if (filled) {
  2718. cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, w, h);
  2719. cairo_fill(dctx->uctx.u.cairo.cr);
  2720. } else {
  2721. cairo_rectangle(dctx->uctx.u.cairo.cr,
  2722. x + 0.5, y + 0.5, w, h);
  2723. cairo_close_path(dctx->uctx.u.cairo.cr);
  2724. cairo_stroke(dctx->uctx.u.cairo.cr);
  2725. }
  2726. }
  2727. #endif
  2728. }
  2729. static void draw_clip(struct draw_ctx *dctx, int x, int y, int w, int h)
  2730. {
  2731. #ifdef DRAW_TEXT_GDK
  2732. if (dctx->uctx.type == DRAWTYPE_GDK) {
  2733. GdkRectangle r;
  2734. r.x = x;
  2735. r.y = y;
  2736. r.width = w;
  2737. r.height = h;
  2738. gdk_gc_set_clip_rectangle(dctx->uctx.u.gdk.gc, &r);
  2739. }
  2740. #endif
  2741. #ifdef DRAW_TEXT_CAIRO
  2742. if (dctx->uctx.type == DRAWTYPE_CAIRO) {
  2743. cairo_reset_clip(dctx->uctx.u.cairo.cr);
  2744. cairo_new_path(dctx->uctx.u.cairo.cr);
  2745. cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, w, h);
  2746. cairo_clip(dctx->uctx.u.cairo.cr);
  2747. }
  2748. #endif
  2749. }
  2750. static void draw_point(struct draw_ctx *dctx, int x, int y)
  2751. {
  2752. #ifdef DRAW_TEXT_GDK
  2753. if (dctx->uctx.type == DRAWTYPE_GDK) {
  2754. gdk_draw_point(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc, x, y);
  2755. }
  2756. #endif
  2757. #ifdef DRAW_TEXT_CAIRO
  2758. if (dctx->uctx.type == DRAWTYPE_CAIRO) {
  2759. cairo_new_path(dctx->uctx.u.cairo.cr);
  2760. cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, 1, 1);
  2761. cairo_fill(dctx->uctx.u.cairo.cr);
  2762. }
  2763. #endif
  2764. }
  2765. static void draw_line(struct draw_ctx *dctx, int x0, int y0, int x1, int y1)
  2766. {
  2767. #ifdef DRAW_TEXT_GDK
  2768. if (dctx->uctx.type == DRAWTYPE_GDK) {
  2769. gdk_draw_line(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc,
  2770. x0, y0, x1, y1);
  2771. }
  2772. #endif
  2773. #ifdef DRAW_TEXT_CAIRO
  2774. if (dctx->uctx.type == DRAWTYPE_CAIRO) {
  2775. cairo_new_path(dctx->uctx.u.cairo.cr);
  2776. cairo_move_to(dctx->uctx.u.cairo.cr, x0 + 0.5, y0 + 0.5);
  2777. cairo_line_to(dctx->uctx.u.cairo.cr, x1 + 0.5, y1 + 0.5);
  2778. cairo_stroke(dctx->uctx.u.cairo.cr);
  2779. }
  2780. #endif
  2781. }
  2782. static void draw_stretch_before(struct draw_ctx *dctx, int x, int y,
  2783. int w, int wdouble,
  2784. int h, int hdouble, int hbothalf)
  2785. {
  2786. #ifdef DRAW_TEXT_CAIRO
  2787. if (dctx->uctx.type == DRAWTYPE_CAIRO) {
  2788. cairo_matrix_t matrix;
  2789. matrix.xy = 0;
  2790. matrix.yx = 0;
  2791. if (wdouble) {
  2792. matrix.xx = 2;
  2793. matrix.x0 = -x;
  2794. } else {
  2795. matrix.xx = 1;
  2796. matrix.x0 = 0;
  2797. }
  2798. if (hdouble) {
  2799. matrix.yy = 2;
  2800. if (hbothalf) {
  2801. matrix.y0 = -(y+h);
  2802. } else {
  2803. matrix.y0 = -y;
  2804. }
  2805. } else {
  2806. matrix.yy = 1;
  2807. matrix.y0 = 0;
  2808. }
  2809. cairo_transform(dctx->uctx.u.cairo.cr, &matrix);
  2810. }
  2811. #endif
  2812. }
  2813. static void draw_stretch_after(struct draw_ctx *dctx, int x, int y,
  2814. int w, int wdouble,
  2815. int h, int hdouble, int hbothalf)
  2816. {
  2817. #ifdef DRAW_TEXT_GDK
  2818. #ifndef NO_BACKING_PIXMAPS
  2819. if (dctx->uctx.type == DRAWTYPE_GDK) {
  2820. /*
  2821. * I can't find any plausible StretchBlt equivalent in the X
  2822. * server, so I'm going to do this the slow and painful way.
  2823. * This will involve repeated calls to gdk_draw_pixmap() to
  2824. * stretch the text horizontally. It's O(N^2) in time and O(N)
  2825. * in network bandwidth, but you try thinking of a better way.
  2826. * :-(
  2827. */
  2828. int i;
  2829. if (wdouble) {
  2830. for (i = 0; i < w; i++) {
  2831. gdk_draw_pixmap(dctx->uctx.u.gdk.target,
  2832. dctx->uctx.u.gdk.gc,
  2833. dctx->uctx.u.gdk.target,
  2834. x + 2*i, y,
  2835. x + 2*i+1, y,
  2836. w - i, h);
  2837. }
  2838. w *= 2;
  2839. }
  2840. if (hdouble) {
  2841. int dt, db;
  2842. /* Now stretch vertically, in the same way. */
  2843. if (hbothalf)
  2844. dt = 0, db = 1;
  2845. else
  2846. dt = 1, db = 0;
  2847. for (i = 0; i < h; i += 2) {
  2848. gdk_draw_pixmap(dctx->uctx.u.gdk.target,
  2849. dctx->uctx.u.gdk.gc,
  2850. dctx->uctx.u.gdk.target,
  2851. x, y + dt*i + db,
  2852. x, y + dt*(i+1),
  2853. w, h-i-1);
  2854. }
  2855. }
  2856. }
  2857. #else
  2858. #error No way to implement stretching in GDK without a reliable backing pixmap
  2859. #endif
  2860. #endif /* DRAW_TEXT_GDK */
  2861. #ifdef DRAW_TEXT_CAIRO
  2862. if (dctx->uctx.type == DRAWTYPE_CAIRO) {
  2863. cairo_set_matrix(dctx->uctx.u.cairo.cr,
  2864. &dctx->uctx.u.cairo.origmatrix);
  2865. }
  2866. #endif
  2867. }
  2868. static void draw_backing_rect(struct gui_data *inst)
  2869. {
  2870. struct draw_ctx *dctx = get_ctx(inst);
  2871. int w = inst->width * inst->font_width + 2*inst->window_border;
  2872. int h = inst->height * inst->font_height + 2*inst->window_border;
  2873. draw_set_colour(dctx, 258);
  2874. draw_rectangle(dctx, 1, 0, 0, w, h);
  2875. draw_update(dctx, 0, 0, w, h);
  2876. free_ctx(dctx);
  2877. }
  2878. /*
  2879. * Draw a line of text in the window, at given character
  2880. * coordinates, in given attributes.
  2881. *
  2882. * We are allowed to fiddle with the contents of `text'.
  2883. */
  2884. void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
  2885. unsigned long attr, int lattr)
  2886. {
  2887. struct draw_ctx *dctx = (struct draw_ctx *)ctx;
  2888. struct gui_data *inst = dctx->inst;
  2889. int ncombining;
  2890. int nfg, nbg, t, fontid, shadow, rlen, widefactor, bold;
  2891. int monochrome =
  2892. gdk_visual_get_depth(gtk_widget_get_visual(inst->area)) == 1;
  2893. if (attr & TATTR_COMBINING) {
  2894. ncombining = len;
  2895. len = 1;
  2896. } else
  2897. ncombining = 1;
  2898. nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
  2899. nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
  2900. if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
  2901. t = nfg;
  2902. nfg = nbg;
  2903. nbg = t;
  2904. }
  2905. if ((inst->bold_style & 2) && (attr & ATTR_BOLD)) {
  2906. if (nfg < 16) nfg |= 8;
  2907. else if (nfg >= 256) nfg |= 1;
  2908. }
  2909. if ((inst->bold_style & 2) && (attr & ATTR_BLINK)) {
  2910. if (nbg < 16) nbg |= 8;
  2911. else if (nbg >= 256) nbg |= 1;
  2912. }
  2913. if ((attr & TATTR_ACTCURS) && !monochrome) {
  2914. nfg = 260;
  2915. nbg = 261;
  2916. }
  2917. fontid = shadow = 0;
  2918. if (attr & ATTR_WIDE) {
  2919. widefactor = 2;
  2920. fontid |= 2;
  2921. } else {
  2922. widefactor = 1;
  2923. }
  2924. if ((attr & ATTR_BOLD) && (inst->bold_style & 1)) {
  2925. bold = 1;
  2926. fontid |= 1;
  2927. } else {
  2928. bold = 0;
  2929. }
  2930. if (!inst->fonts[fontid]) {
  2931. int i;
  2932. /*
  2933. * Fall back through font ids with subsets of this one's
  2934. * set bits, in order.
  2935. */
  2936. for (i = fontid; i-- > 0 ;) {
  2937. if (i & ~fontid)
  2938. continue; /* some other bit is set */
  2939. if (inst->fonts[i]) {
  2940. fontid = i;
  2941. break;
  2942. }
  2943. }
  2944. assert(inst->fonts[fontid]); /* we should at least have hit zero */
  2945. }
  2946. if ((lattr & LATTR_MODE) != LATTR_NORM) {
  2947. x *= 2;
  2948. if (x >= inst->term->cols)
  2949. return;
  2950. if (x + len*2*widefactor > inst->term->cols)
  2951. len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
  2952. rlen = len * 2;
  2953. } else
  2954. rlen = len;
  2955. draw_clip(dctx,
  2956. x*inst->font_width+inst->window_border,
  2957. y*inst->font_height+inst->window_border,
  2958. rlen*widefactor*inst->font_width,
  2959. inst->font_height);
  2960. if ((lattr & LATTR_MODE) != LATTR_NORM) {
  2961. draw_stretch_before(dctx,
  2962. x*inst->font_width+inst->window_border,
  2963. y*inst->font_height+inst->window_border,
  2964. rlen*widefactor*inst->font_width, TRUE,
  2965. inst->font_height,
  2966. ((lattr & LATTR_MODE) != LATTR_WIDE),
  2967. ((lattr & LATTR_MODE) == LATTR_BOT));
  2968. }
  2969. draw_set_colour(dctx, nbg);
  2970. draw_rectangle(dctx, TRUE,
  2971. x*inst->font_width+inst->window_border,
  2972. y*inst->font_height+inst->window_border,
  2973. rlen*widefactor*inst->font_width, inst->font_height);
  2974. draw_set_colour(dctx, nfg);
  2975. if (ncombining > 1) {
  2976. assert(len == 1);
  2977. unifont_draw_combining(&dctx->uctx, inst->fonts[fontid],
  2978. x*inst->font_width+inst->window_border,
  2979. (y*inst->font_height+inst->window_border+
  2980. inst->fonts[0]->ascent),
  2981. text, ncombining, widefactor > 1,
  2982. bold, inst->font_width);
  2983. } else {
  2984. unifont_draw_text(&dctx->uctx, inst->fonts[fontid],
  2985. x*inst->font_width+inst->window_border,
  2986. (y*inst->font_height+inst->window_border+
  2987. inst->fonts[0]->ascent),
  2988. text, len, widefactor > 1,
  2989. bold, inst->font_width);
  2990. }
  2991. if (attr & ATTR_UNDER) {
  2992. int uheight = inst->fonts[0]->ascent + 1;
  2993. if (uheight >= inst->font_height)
  2994. uheight = inst->font_height - 1;
  2995. draw_line(dctx, x*inst->font_width+inst->window_border,
  2996. y*inst->font_height + uheight + inst->window_border,
  2997. (x+len)*widefactor*inst->font_width-1+inst->window_border,
  2998. y*inst->font_height + uheight + inst->window_border);
  2999. }
  3000. if ((lattr & LATTR_MODE) != LATTR_NORM) {
  3001. draw_stretch_after(dctx,
  3002. x*inst->font_width+inst->window_border,
  3003. y*inst->font_height+inst->window_border,
  3004. rlen*widefactor*inst->font_width, TRUE,
  3005. inst->font_height,
  3006. ((lattr & LATTR_MODE) != LATTR_WIDE),
  3007. ((lattr & LATTR_MODE) == LATTR_BOT));
  3008. }
  3009. }
  3010. void do_text(Context ctx, int x, int y, wchar_t *text, int len,
  3011. unsigned long attr, int lattr)
  3012. {
  3013. struct draw_ctx *dctx = (struct draw_ctx *)ctx;
  3014. struct gui_data *inst = dctx->inst;
  3015. int widefactor;
  3016. do_text_internal(ctx, x, y, text, len, attr, lattr);
  3017. if (attr & ATTR_WIDE) {
  3018. widefactor = 2;
  3019. } else {
  3020. widefactor = 1;
  3021. }
  3022. if ((lattr & LATTR_MODE) != LATTR_NORM) {
  3023. x *= 2;
  3024. if (x >= inst->term->cols)
  3025. return;
  3026. if (x + len*2*widefactor > inst->term->cols)
  3027. len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
  3028. len *= 2;
  3029. }
  3030. draw_update(dctx,
  3031. x*inst->font_width+inst->window_border,
  3032. y*inst->font_height+inst->window_border,
  3033. len*widefactor*inst->font_width, inst->font_height);
  3034. }
  3035. void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
  3036. unsigned long attr, int lattr)
  3037. {
  3038. struct draw_ctx *dctx = (struct draw_ctx *)ctx;
  3039. struct gui_data *inst = dctx->inst;
  3040. int active, passive, widefactor;
  3041. if (attr & TATTR_PASCURS) {
  3042. attr &= ~TATTR_PASCURS;
  3043. passive = 1;
  3044. } else
  3045. passive = 0;
  3046. if ((attr & TATTR_ACTCURS) && inst->cursor_type != 0) {
  3047. attr &= ~TATTR_ACTCURS;
  3048. active = 1;
  3049. } else
  3050. active = 0;
  3051. do_text_internal(ctx, x, y, text, len, attr, lattr);
  3052. if (attr & TATTR_COMBINING)
  3053. len = 1;
  3054. if (attr & ATTR_WIDE) {
  3055. widefactor = 2;
  3056. } else {
  3057. widefactor = 1;
  3058. }
  3059. if ((lattr & LATTR_MODE) != LATTR_NORM) {
  3060. x *= 2;
  3061. if (x >= inst->term->cols)
  3062. return;
  3063. if (x + len*2*widefactor > inst->term->cols)
  3064. len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
  3065. len *= 2;
  3066. }
  3067. if (inst->cursor_type == 0) {
  3068. /*
  3069. * An active block cursor will already have been done by
  3070. * the above do_text call, so we only need to do anything
  3071. * if it's passive.
  3072. */
  3073. if (passive) {
  3074. draw_set_colour(dctx, 261);
  3075. draw_rectangle(dctx, FALSE,
  3076. x*inst->font_width+inst->window_border,
  3077. y*inst->font_height+inst->window_border,
  3078. len*widefactor*inst->font_width-1,
  3079. inst->font_height-1);
  3080. }
  3081. } else {
  3082. int uheight;
  3083. int startx, starty, dx, dy, length, i;
  3084. int char_width;
  3085. if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
  3086. char_width = 2*inst->font_width;
  3087. else
  3088. char_width = inst->font_width;
  3089. if (inst->cursor_type == 1) {
  3090. uheight = inst->fonts[0]->ascent + 1;
  3091. if (uheight >= inst->font_height)
  3092. uheight = inst->font_height - 1;
  3093. startx = x * inst->font_width + inst->window_border;
  3094. starty = y * inst->font_height + inst->window_border + uheight;
  3095. dx = 1;
  3096. dy = 0;
  3097. length = len * widefactor * char_width;
  3098. } else {
  3099. int xadjust = 0;
  3100. if (attr & TATTR_RIGHTCURS)
  3101. xadjust = char_width - 1;
  3102. startx = x * inst->font_width + inst->window_border + xadjust;
  3103. starty = y * inst->font_height + inst->window_border;
  3104. dx = 0;
  3105. dy = 1;
  3106. length = inst->font_height;
  3107. }
  3108. draw_set_colour(dctx, 261);
  3109. if (passive) {
  3110. for (i = 0; i < length; i++) {
  3111. if (i % 2 == 0) {
  3112. draw_point(dctx, startx, starty);
  3113. }
  3114. startx += dx;
  3115. starty += dy;
  3116. }
  3117. } else if (active) {
  3118. draw_line(dctx, startx, starty,
  3119. startx + (length-1) * dx, starty + (length-1) * dy);
  3120. } /* else no cursor (e.g., blinked off) */
  3121. }
  3122. draw_update(dctx,
  3123. x*inst->font_width+inst->window_border,
  3124. y*inst->font_height+inst->window_border,
  3125. len*widefactor*inst->font_width, inst->font_height);
  3126. #if GTK_CHECK_VERSION(2,0,0)
  3127. {
  3128. GdkRectangle cursorrect;
  3129. cursorrect.x = x*inst->font_width+inst->window_border;
  3130. cursorrect.y = y*inst->font_height+inst->window_border;
  3131. cursorrect.width = len*widefactor*inst->font_width;
  3132. cursorrect.height = inst->font_height;
  3133. gtk_im_context_set_cursor_location(inst->imc, &cursorrect);
  3134. }
  3135. #endif
  3136. }
  3137. GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
  3138. {
  3139. if (cursor_val == -1) {
  3140. #if GTK_CHECK_VERSION(2,16,0)
  3141. cursor_val = GDK_BLANK_CURSOR;
  3142. #else
  3143. /*
  3144. * Work around absence of GDK_BLANK_CURSOR by inventing a
  3145. * blank pixmap.
  3146. */
  3147. GdkCursor *ret;
  3148. GdkColor bg = { 0, 0, 0, 0 };
  3149. GdkPixmap *pm = gdk_pixmap_new(NULL, 1, 1, 1);
  3150. GdkGC *gc = gdk_gc_new(pm);
  3151. gdk_gc_set_foreground(gc, &bg);
  3152. gdk_draw_rectangle(pm, gc, 1, 0, 0, 1, 1);
  3153. gdk_gc_unref(gc);
  3154. ret = gdk_cursor_new_from_pixmap(pm, pm, &bg, &bg, 1, 1);
  3155. gdk_pixmap_unref(pm);
  3156. return ret;
  3157. #endif
  3158. }
  3159. return gdk_cursor_new(cursor_val);
  3160. }
  3161. void modalfatalbox(const char *p, ...)
  3162. {
  3163. va_list ap;
  3164. fprintf(stderr, "FATAL ERROR: ");
  3165. va_start(ap, p);
  3166. vfprintf(stderr, p, ap);
  3167. va_end(ap);
  3168. fputc('\n', stderr);
  3169. exit(1);
  3170. }
  3171. void cmdline_error(const char *p, ...)
  3172. {
  3173. va_list ap;
  3174. fprintf(stderr, "%s: ", appname);
  3175. va_start(ap, p);
  3176. vfprintf(stderr, p, ap);
  3177. va_end(ap);
  3178. fputc('\n', stderr);
  3179. exit(1);
  3180. }
  3181. const char *get_x_display(void *frontend)
  3182. {
  3183. return gdk_get_display();
  3184. }
  3185. #ifndef NOT_X_WINDOWS
  3186. long get_windowid(void *frontend)
  3187. {
  3188. struct gui_data *inst = (struct gui_data *)frontend;
  3189. return (long)GDK_WINDOW_XID(gtk_widget_get_window(inst->area));
  3190. }
  3191. #endif
  3192. int frontend_is_utf8(void *frontend)
  3193. {
  3194. struct gui_data *inst = (struct gui_data *)frontend;
  3195. return inst->ucsdata.line_codepage == CS_UTF8;
  3196. }
  3197. char *setup_fonts_ucs(struct gui_data *inst)
  3198. {
  3199. int shadowbold = conf_get_int(inst->conf, CONF_shadowbold);
  3200. int shadowboldoffset = conf_get_int(inst->conf, CONF_shadowboldoffset);
  3201. FontSpec *fs;
  3202. unifont *fonts[4];
  3203. int i;
  3204. fs = conf_get_fontspec(inst->conf, CONF_font);
  3205. fonts[0] = multifont_create(inst->area, fs->name, FALSE, FALSE,
  3206. shadowboldoffset, shadowbold);
  3207. if (!fonts[0]) {
  3208. return dupprintf("unable to load font \"%s\"", fs->name);
  3209. }
  3210. fs = conf_get_fontspec(inst->conf, CONF_boldfont);
  3211. if (shadowbold || !fs->name[0]) {
  3212. fonts[1] = NULL;
  3213. } else {
  3214. fonts[1] = multifont_create(inst->area, fs->name, FALSE, TRUE,
  3215. shadowboldoffset, shadowbold);
  3216. if (!fonts[1]) {
  3217. if (fonts[0])
  3218. unifont_destroy(fonts[0]);
  3219. return dupprintf("unable to load bold font \"%s\"", fs->name);
  3220. }
  3221. }
  3222. fs = conf_get_fontspec(inst->conf, CONF_widefont);
  3223. if (fs->name[0]) {
  3224. fonts[2] = multifont_create(inst->area, fs->name, TRUE, FALSE,
  3225. shadowboldoffset, shadowbold);
  3226. if (!fonts[2]) {
  3227. for (i = 0; i < 2; i++)
  3228. if (fonts[i])
  3229. unifont_destroy(fonts[i]);
  3230. return dupprintf("unable to load wide font \"%s\"", fs->name);
  3231. }
  3232. } else {
  3233. fonts[2] = NULL;
  3234. }
  3235. fs = conf_get_fontspec(inst->conf, CONF_wideboldfont);
  3236. if (shadowbold || !fs->name[0]) {
  3237. fonts[3] = NULL;
  3238. } else {
  3239. fonts[3] = multifont_create(inst->area, fs->name, TRUE, TRUE,
  3240. shadowboldoffset, shadowbold);
  3241. if (!fonts[3]) {
  3242. for (i = 0; i < 3; i++)
  3243. if (fonts[i])
  3244. unifont_destroy(fonts[i]);
  3245. return dupprintf("unable to load wide bold font \"%s\"", fs->name);
  3246. }
  3247. }
  3248. /*
  3249. * Now we've got past all the possible error conditions, we can
  3250. * actually update our state.
  3251. */
  3252. for (i = 0; i < 4; i++) {
  3253. if (inst->fonts[i])
  3254. unifont_destroy(inst->fonts[i]);
  3255. inst->fonts[i] = fonts[i];
  3256. }
  3257. inst->font_width = inst->fonts[0]->width;
  3258. inst->font_height = inst->fonts[0]->height;
  3259. inst->direct_to_font = init_ucs(&inst->ucsdata,
  3260. conf_get_str(inst->conf, CONF_line_codepage),
  3261. conf_get_int(inst->conf, CONF_utf8_override),
  3262. inst->fonts[0]->public_charset,
  3263. conf_get_int(inst->conf, CONF_vtmode));
  3264. inst->drawtype = inst->fonts[0]->preferred_drawtype;
  3265. return NULL;
  3266. }
  3267. #if GTK_CHECK_VERSION(3,0,0)
  3268. struct find_app_menu_bar_ctx {
  3269. GtkWidget *area, *menubar;
  3270. };
  3271. static void find_app_menu_bar(GtkWidget *widget, gpointer data)
  3272. {
  3273. struct find_app_menu_bar_ctx *ctx = (struct find_app_menu_bar_ctx *)data;
  3274. if (widget != ctx->area && GTK_IS_MENU_BAR(widget))
  3275. ctx->menubar = widget;
  3276. }
  3277. #endif
  3278. static void compute_geom_hints(struct gui_data *inst, GdkGeometry *geom)
  3279. {
  3280. /*
  3281. * Unused fields in geom.
  3282. */
  3283. geom->max_width = geom->max_height = -1;
  3284. geom->min_aspect = geom->max_aspect = 0;
  3285. /*
  3286. * Set up the geometry fields we care about, with reference to
  3287. * just the drawing area. We'll correct for other widgets in a
  3288. * moment.
  3289. */
  3290. geom->min_width = inst->font_width + 2*inst->window_border;
  3291. geom->min_height = inst->font_height + 2*inst->window_border;
  3292. geom->base_width = 2*inst->window_border;
  3293. geom->base_height = 2*inst->window_border;
  3294. geom->width_inc = inst->font_width;
  3295. geom->height_inc = inst->font_height;
  3296. /*
  3297. * If we've got a scrollbar visible, then we must include its
  3298. * width as part of the base and min width, and also ensure that
  3299. * our window's minimum height is at least the height required by
  3300. * the scrollbar.
  3301. *
  3302. * In the latter case, we must also take care to arrange that
  3303. * (geom->min_height - geom->base_height) is an integer multiple of
  3304. * geom->height_inc, because if it's not, then some window managers
  3305. * (we know of xfwm4) get confused, with the effect that they
  3306. * resize our window to a height based on min_height instead of
  3307. * base_height, which we then round down and the window ends up
  3308. * too short.
  3309. */
  3310. if (inst->sbar_visible) {
  3311. GtkRequisition req;
  3312. int min_sb_height;
  3313. #if GTK_CHECK_VERSION(3,0,0)
  3314. gtk_widget_get_preferred_size(inst->sbar, &req, NULL);
  3315. #else
  3316. gtk_widget_size_request(inst->sbar, &req);
  3317. #endif
  3318. /* Compute rounded-up scrollbar height. */
  3319. min_sb_height = req.height;
  3320. min_sb_height += geom->height_inc - 1;
  3321. min_sb_height -= ((min_sb_height - geom->base_height%geom->height_inc)
  3322. % geom->height_inc);
  3323. geom->min_width += req.width;
  3324. geom->base_width += req.width;
  3325. if (geom->min_height < min_sb_height)
  3326. geom->min_height = min_sb_height;
  3327. }
  3328. #if GTK_CHECK_VERSION(3,0,0)
  3329. /*
  3330. * And if we're running a gtkapp.c based program and
  3331. * GtkApplicationWindow has given us a menu bar inside the window,
  3332. * then we must take that into account as well.
  3333. *
  3334. * In its unbounded wisdom, GtkApplicationWindow doesn't actually
  3335. * give us a direct function call to _find_ the menu bar widget.
  3336. * Fortunately, we can find it by enumerating the children of the
  3337. * top-level window and looking for one we didn't put there
  3338. * ourselves.
  3339. */
  3340. {
  3341. struct find_app_menu_bar_ctx actx, *ctx = &actx;
  3342. ctx->area = inst->area;
  3343. ctx->menubar = NULL;
  3344. gtk_container_foreach(GTK_CONTAINER(inst->window),
  3345. find_app_menu_bar, ctx);
  3346. if (ctx->menubar) {
  3347. GtkRequisition req;
  3348. int min_menu_width;
  3349. gtk_widget_get_preferred_size(ctx->menubar, NULL, &req);
  3350. /*
  3351. * This time, the height adjustment is easy (the menu bar
  3352. * sits above everything), but we have to take care with
  3353. * the _width_ to ensure we keep min_width and base_width
  3354. * congruent modulo width_inc.
  3355. */
  3356. geom->min_height += req.height;
  3357. geom->base_height += req.height;
  3358. min_menu_width = req.width;
  3359. min_menu_width += geom->width_inc - 1;
  3360. min_menu_width -=
  3361. ((min_menu_width - geom->base_width%geom->width_inc)
  3362. % geom->width_inc);
  3363. if (geom->min_width < min_menu_width)
  3364. geom->min_width = min_menu_width;
  3365. }
  3366. }
  3367. #endif
  3368. }
  3369. void set_geom_hints(struct gui_data *inst)
  3370. {
  3371. GdkGeometry geom;
  3372. gint flags = GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE | GDK_HINT_RESIZE_INC;
  3373. compute_geom_hints(inst, &geom);
  3374. #if GTK_CHECK_VERSION(2,0,0)
  3375. if (inst->gotpos)
  3376. flags |= GDK_HINT_USER_POS;
  3377. #endif
  3378. gtk_window_set_geometry_hints(GTK_WINDOW(inst->window),
  3379. NULL, &geom, flags);
  3380. }
  3381. #if GTK_CHECK_VERSION(2,0,0)
  3382. static void compute_whole_window_size(struct gui_data *inst,
  3383. int wchars, int hchars,
  3384. int *wpix, int *hpix)
  3385. {
  3386. GdkGeometry geom;
  3387. compute_geom_hints(inst, &geom);
  3388. if (wpix) *wpix = geom.base_width + wchars * geom.width_inc;
  3389. if (hpix) *hpix = geom.base_height + hchars * geom.height_inc;
  3390. }
  3391. #endif
  3392. void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
  3393. {
  3394. struct gui_data *inst = (struct gui_data *)data;
  3395. term_clrsb(inst->term);
  3396. }
  3397. void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
  3398. {
  3399. struct gui_data *inst = (struct gui_data *)data;
  3400. term_pwron(inst->term, TRUE);
  3401. if (inst->ldisc)
  3402. ldisc_echoedit_update(inst->ldisc);
  3403. }
  3404. void copy_all_menuitem(GtkMenuItem *item, gpointer data)
  3405. {
  3406. struct gui_data *inst = (struct gui_data *)data;
  3407. term_copyall(inst->term);
  3408. }
  3409. void special_menuitem(GtkMenuItem *item, gpointer data)
  3410. {
  3411. struct gui_data *inst = (struct gui_data *)data;
  3412. int code = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item),
  3413. "user-data"));
  3414. if (inst->back)
  3415. inst->back->special(inst->backhandle, code);
  3416. }
  3417. void about_menuitem(GtkMenuItem *item, gpointer data)
  3418. {
  3419. struct gui_data *inst = (struct gui_data *)data;
  3420. about_box(inst->window);
  3421. }
  3422. void event_log_menuitem(GtkMenuItem *item, gpointer data)
  3423. {
  3424. struct gui_data *inst = (struct gui_data *)data;
  3425. showeventlog(inst->eventlogstuff, inst->window);
  3426. }
  3427. void change_settings_menuitem(GtkMenuItem *item, gpointer data)
  3428. {
  3429. /* This maps colour indices in inst->conf to those used in inst->cols. */
  3430. static const int ww[] = {
  3431. 256, 257, 258, 259, 260, 261,
  3432. 0, 8, 1, 9, 2, 10, 3, 11,
  3433. 4, 12, 5, 13, 6, 14, 7, 15
  3434. };
  3435. struct gui_data *inst = (struct gui_data *)data;
  3436. char *title;
  3437. Conf *oldconf, *newconf;
  3438. int i, j, need_size;
  3439. assert(lenof(ww) == NCFGCOLOURS);
  3440. if (inst->reconfiguring)
  3441. return;
  3442. else
  3443. inst->reconfiguring = TRUE;
  3444. title = dupcat(appname, " Reconfiguration", NULL);
  3445. oldconf = inst->conf;
  3446. newconf = conf_copy(inst->conf);
  3447. if (do_config_box(title, newconf, 1,
  3448. inst->back?inst->back->cfg_info(inst->backhandle):0)) {
  3449. inst->conf = newconf;
  3450. /* Pass new config data to the logging module */
  3451. log_reconfig(inst->logctx, inst->conf);
  3452. /*
  3453. * Flush the line discipline's edit buffer in the case
  3454. * where local editing has just been disabled.
  3455. */
  3456. if (inst->ldisc) {
  3457. ldisc_configure(inst->ldisc, inst->conf);
  3458. ldisc_echoedit_update(inst->ldisc);
  3459. }
  3460. /* Pass new config data to the terminal */
  3461. term_reconfig(inst->term, inst->conf);
  3462. /* Pass new config data to the back end */
  3463. if (inst->back)
  3464. inst->back->reconfig(inst->backhandle, inst->conf);
  3465. cache_conf_values(inst);
  3466. /*
  3467. * Just setting inst->conf is sufficient to cause colour
  3468. * setting changes to appear on the next ESC]R palette
  3469. * reset. But we should also check whether any colour
  3470. * settings have been changed, and revert the ones that have
  3471. * to the new default, on the assumption that the user is
  3472. * most likely to want an immediate update.
  3473. */
  3474. for (i = 0; i < NCFGCOLOURS; i++) {
  3475. for (j = 0; j < 3; j++)
  3476. if (conf_get_int_int(oldconf, CONF_colours, i*3+j) !=
  3477. conf_get_int_int(newconf, CONF_colours, i*3+j))
  3478. break;
  3479. if (j < 3) {
  3480. real_palette_set(inst, ww[i],
  3481. conf_get_int_int(newconf,CONF_colours,i*3+0),
  3482. conf_get_int_int(newconf,CONF_colours,i*3+1),
  3483. conf_get_int_int(newconf,CONF_colours,i*3+2));
  3484. /*
  3485. * If the default background has changed, we must
  3486. * repaint the space in between the window border
  3487. * and the text area.
  3488. */
  3489. if (ww[i] == 258) {
  3490. set_window_background(inst);
  3491. draw_backing_rect(inst);
  3492. }
  3493. }
  3494. }
  3495. need_size = FALSE;
  3496. /*
  3497. * If the scrollbar needs to be shown, hidden, or moved
  3498. * from one end to the other of the window, do so now.
  3499. */
  3500. if (conf_get_int(oldconf, CONF_scrollbar) !=
  3501. conf_get_int(newconf, CONF_scrollbar)) {
  3502. show_scrollbar(inst, conf_get_int(newconf, CONF_scrollbar));
  3503. need_size = TRUE;
  3504. }
  3505. if (conf_get_int(oldconf, CONF_scrollbar_on_left) !=
  3506. conf_get_int(newconf, CONF_scrollbar_on_left)) {
  3507. gtk_box_reorder_child(inst->hbox, inst->sbar,
  3508. conf_get_int(newconf, CONF_scrollbar_on_left)
  3509. ? 0 : 1);
  3510. }
  3511. /*
  3512. * Change the window title, if required.
  3513. */
  3514. if (strcmp(conf_get_str(oldconf, CONF_wintitle),
  3515. conf_get_str(newconf, CONF_wintitle)))
  3516. set_title(inst, conf_get_str(newconf, CONF_wintitle));
  3517. set_window_titles(inst);
  3518. /*
  3519. * Redo the whole tangled fonts and Unicode mess if
  3520. * necessary.
  3521. */
  3522. if (strcmp(conf_get_fontspec(oldconf, CONF_font)->name,
  3523. conf_get_fontspec(newconf, CONF_font)->name) ||
  3524. strcmp(conf_get_fontspec(oldconf, CONF_boldfont)->name,
  3525. conf_get_fontspec(newconf, CONF_boldfont)->name) ||
  3526. strcmp(conf_get_fontspec(oldconf, CONF_widefont)->name,
  3527. conf_get_fontspec(newconf, CONF_widefont)->name) ||
  3528. strcmp(conf_get_fontspec(oldconf, CONF_wideboldfont)->name,
  3529. conf_get_fontspec(newconf, CONF_wideboldfont)->name) ||
  3530. strcmp(conf_get_str(oldconf, CONF_line_codepage),
  3531. conf_get_str(newconf, CONF_line_codepage)) ||
  3532. conf_get_int(oldconf, CONF_utf8_override) !=
  3533. conf_get_int(newconf, CONF_utf8_override) ||
  3534. conf_get_int(oldconf, CONF_vtmode) !=
  3535. conf_get_int(newconf, CONF_vtmode) ||
  3536. conf_get_int(oldconf, CONF_shadowbold) !=
  3537. conf_get_int(newconf, CONF_shadowbold) ||
  3538. conf_get_int(oldconf, CONF_shadowboldoffset) !=
  3539. conf_get_int(newconf, CONF_shadowboldoffset)) {
  3540. char *errmsg = setup_fonts_ucs(inst);
  3541. if (errmsg) {
  3542. char *msgboxtext =
  3543. dupprintf("Could not change fonts in terminal window: %s\n",
  3544. errmsg);
  3545. messagebox(inst->window, "Font setup error", msgboxtext,
  3546. string_width("Could not change fonts in terminal window:"),
  3547. FALSE, "OK", 'o', +1, 1,
  3548. NULL);
  3549. sfree(msgboxtext);
  3550. sfree(errmsg);
  3551. } else {
  3552. need_size = TRUE;
  3553. }
  3554. }
  3555. /*
  3556. * Resize the window.
  3557. */
  3558. if (conf_get_int(oldconf, CONF_width) !=
  3559. conf_get_int(newconf, CONF_width) ||
  3560. conf_get_int(oldconf, CONF_height) !=
  3561. conf_get_int(newconf, CONF_height) ||
  3562. conf_get_int(oldconf, CONF_window_border) !=
  3563. conf_get_int(newconf, CONF_window_border) ||
  3564. need_size) {
  3565. set_geom_hints(inst);
  3566. request_resize(inst, conf_get_int(newconf, CONF_width),
  3567. conf_get_int(newconf, CONF_height));
  3568. } else {
  3569. /*
  3570. * The above will have caused a call to term_size() for
  3571. * us if it happened. If the user has fiddled with only
  3572. * the scrollback size, the above will not have
  3573. * happened and we will need an explicit term_size()
  3574. * here.
  3575. */
  3576. if (conf_get_int(oldconf, CONF_savelines) !=
  3577. conf_get_int(newconf, CONF_savelines))
  3578. term_size(inst->term, inst->term->rows, inst->term->cols,
  3579. conf_get_int(newconf, CONF_savelines));
  3580. }
  3581. term_invalidate(inst->term);
  3582. /*
  3583. * We do an explicit full redraw here to ensure the window
  3584. * border has been redrawn as well as the text area.
  3585. */
  3586. gtk_widget_queue_draw(inst->area);
  3587. conf_free(oldconf);
  3588. } else {
  3589. conf_free(newconf);
  3590. }
  3591. sfree(title);
  3592. inst->reconfiguring = FALSE;
  3593. }
  3594. void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
  3595. {
  3596. struct gui_data *inst = (struct gui_data *)gdata;
  3597. launch_duplicate_session(inst->conf);
  3598. }
  3599. void new_session_menuitem(GtkMenuItem *item, gpointer data)
  3600. {
  3601. launch_new_session();
  3602. }
  3603. void restart_session_menuitem(GtkMenuItem *item, gpointer data)
  3604. {
  3605. struct gui_data *inst = (struct gui_data *)data;
  3606. if (!inst->back) {
  3607. logevent(inst, "----- Session restarted -----");
  3608. term_pwron(inst->term, FALSE);
  3609. start_backend(inst);
  3610. inst->exited = FALSE;
  3611. }
  3612. }
  3613. void saved_session_menuitem(GtkMenuItem *item, gpointer data)
  3614. {
  3615. char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
  3616. launch_saved_session(str);
  3617. }
  3618. void saved_session_freedata(GtkMenuItem *item, gpointer data)
  3619. {
  3620. char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
  3621. sfree(str);
  3622. }
  3623. static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
  3624. {
  3625. struct gui_data *inst = (struct gui_data *)data;
  3626. struct sesslist sesslist;
  3627. int i;
  3628. gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
  3629. (GtkCallback)gtk_widget_destroy, NULL);
  3630. get_sesslist(&sesslist, TRUE);
  3631. /* skip sesslist.sessions[0] == Default Settings */
  3632. for (i = 1; i < sesslist.nsessions; i++) {
  3633. GtkWidget *menuitem =
  3634. gtk_menu_item_new_with_label(sesslist.sessions[i]);
  3635. gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
  3636. gtk_widget_show(menuitem);
  3637. g_object_set_data(G_OBJECT(menuitem), "user-data",
  3638. dupstr(sesslist.sessions[i]));
  3639. g_signal_connect(G_OBJECT(menuitem), "activate",
  3640. G_CALLBACK(saved_session_menuitem),
  3641. inst);
  3642. g_signal_connect(G_OBJECT(menuitem), "destroy",
  3643. G_CALLBACK(saved_session_freedata),
  3644. inst);
  3645. }
  3646. if (sesslist.nsessions <= 1) {
  3647. GtkWidget *menuitem =
  3648. gtk_menu_item_new_with_label("(No sessions)");
  3649. gtk_widget_set_sensitive(menuitem, FALSE);
  3650. gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
  3651. gtk_widget_show(menuitem);
  3652. }
  3653. get_sesslist(&sesslist, FALSE); /* free up */
  3654. }
  3655. void set_window_icon(GtkWidget *window, const char *const *const *icon,
  3656. int n_icon)
  3657. {
  3658. #if GTK_CHECK_VERSION(2,0,0)
  3659. GList *iconlist;
  3660. int n;
  3661. #else
  3662. GdkPixmap *iconpm;
  3663. GdkBitmap *iconmask;
  3664. #endif
  3665. if (!n_icon)
  3666. return;
  3667. gtk_widget_realize(window);
  3668. #if GTK_CHECK_VERSION(2,0,0)
  3669. gtk_window_set_icon(GTK_WINDOW(window),
  3670. gdk_pixbuf_new_from_xpm_data((const gchar **)icon[0]));
  3671. #else
  3672. iconpm = gdk_pixmap_create_from_xpm_d(gtk_widget_get_window(window),
  3673. &iconmask, NULL, (gchar **)icon[0]);
  3674. gdk_window_set_icon(gtk_widget_get_window(window), NULL, iconpm, iconmask);
  3675. #endif
  3676. #if GTK_CHECK_VERSION(2,0,0)
  3677. iconlist = NULL;
  3678. for (n = 0; n < n_icon; n++) {
  3679. iconlist =
  3680. g_list_append(iconlist,
  3681. gdk_pixbuf_new_from_xpm_data((const gchar **)
  3682. icon[n]));
  3683. }
  3684. gtk_window_set_icon_list(GTK_WINDOW(window), iconlist);
  3685. #endif
  3686. }
  3687. void update_specials_menu(void *frontend)
  3688. {
  3689. struct gui_data *inst = (struct gui_data *)frontend;
  3690. const struct telnet_special *specials;
  3691. if (inst->back)
  3692. specials = inst->back->get_specials(inst->backhandle);
  3693. else
  3694. specials = NULL;
  3695. /* I believe this disposes of submenus too. */
  3696. gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
  3697. (GtkCallback)gtk_widget_destroy, NULL);
  3698. if (specials) {
  3699. int i;
  3700. GtkWidget *menu = inst->specialsmenu;
  3701. /* A lame "stack" for submenus that will do for now. */
  3702. GtkWidget *saved_menu = NULL;
  3703. int nesting = 1;
  3704. for (i = 0; nesting > 0; i++) {
  3705. GtkWidget *menuitem = NULL;
  3706. switch (specials[i].code) {
  3707. case TS_SUBMENU:
  3708. assert (nesting < 2);
  3709. saved_menu = menu; /* XXX lame stacking */
  3710. menu = gtk_menu_new();
  3711. menuitem = gtk_menu_item_new_with_label(specials[i].name);
  3712. gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
  3713. gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
  3714. gtk_widget_show(menuitem);
  3715. menuitem = NULL;
  3716. nesting++;
  3717. break;
  3718. case TS_EXITMENU:
  3719. nesting--;
  3720. if (nesting) {
  3721. menu = saved_menu; /* XXX lame stacking */
  3722. saved_menu = NULL;
  3723. }
  3724. break;
  3725. case TS_SEP:
  3726. menuitem = gtk_menu_item_new();
  3727. break;
  3728. default:
  3729. menuitem = gtk_menu_item_new_with_label(specials[i].name);
  3730. g_object_set_data(G_OBJECT(menuitem), "user-data",
  3731. GINT_TO_POINTER(specials[i].code));
  3732. g_signal_connect(G_OBJECT(menuitem), "activate",
  3733. G_CALLBACK(special_menuitem), inst);
  3734. break;
  3735. }
  3736. if (menuitem) {
  3737. gtk_container_add(GTK_CONTAINER(menu), menuitem);
  3738. gtk_widget_show(menuitem);
  3739. }
  3740. }
  3741. gtk_widget_show(inst->specialsitem1);
  3742. gtk_widget_show(inst->specialsitem2);
  3743. } else {
  3744. gtk_widget_hide(inst->specialsitem1);
  3745. gtk_widget_hide(inst->specialsitem2);
  3746. }
  3747. }
  3748. static void start_backend(struct gui_data *inst)
  3749. {
  3750. extern Backend *select_backend(Conf *conf);
  3751. char *realhost;
  3752. const char *error;
  3753. char *s;
  3754. inst->back = select_backend(inst->conf);
  3755. error = inst->back->init((void *)inst, &inst->backhandle,
  3756. inst->conf,
  3757. conf_get_str(inst->conf, CONF_host),
  3758. conf_get_int(inst->conf, CONF_port),
  3759. &realhost,
  3760. conf_get_int(inst->conf, CONF_tcp_nodelay),
  3761. conf_get_int(inst->conf, CONF_tcp_keepalives));
  3762. if (error) {
  3763. char *msg = dupprintf("Unable to open connection to %s:\n%s",
  3764. conf_get_str(inst->conf, CONF_host), error);
  3765. inst->exited = TRUE;
  3766. fatal_message_box(inst->window, msg);
  3767. sfree(msg);
  3768. exit(0);
  3769. }
  3770. s = conf_get_str(inst->conf, CONF_wintitle);
  3771. if (s[0]) {
  3772. set_title_and_icon(inst, s, s);
  3773. } else {
  3774. char *title = make_default_wintitle(realhost);
  3775. set_title_and_icon(inst, title, title);
  3776. sfree(title);
  3777. }
  3778. sfree(realhost);
  3779. inst->back->provide_logctx(inst->backhandle, inst->logctx);
  3780. term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
  3781. inst->ldisc =
  3782. ldisc_create(inst->conf, inst->term, inst->back, inst->backhandle,
  3783. inst);
  3784. gtk_widget_set_sensitive(inst->restartitem, FALSE);
  3785. }
  3786. struct gui_data *new_session_window(Conf *conf, const char *geometry_string)
  3787. {
  3788. struct gui_data *inst;
  3789. /*
  3790. * Create an instance structure and initialise to zeroes
  3791. */
  3792. inst = snew(struct gui_data);
  3793. memset(inst, 0, sizeof(*inst));
  3794. inst->alt_keycode = -1; /* this one needs _not_ to be zero */
  3795. inst->busy_status = BUSY_NOT;
  3796. inst->conf = conf;
  3797. inst->wintitle = inst->icontitle = NULL;
  3798. inst->drawtype = DRAWTYPE_DEFAULT;
  3799. #if GTK_CHECK_VERSION(3,4,0)
  3800. inst->cumulative_scroll = 0.0;
  3801. #endif
  3802. #ifndef NOT_X_WINDOWS
  3803. if (geometry_string) {
  3804. int flags, x, y;
  3805. unsigned int w, h;
  3806. flags = XParseGeometry(geometry_string, &x, &y, &w, &h);
  3807. if (flags & WidthValue)
  3808. conf_set_int(conf, CONF_width, w);
  3809. if (flags & HeightValue)
  3810. conf_set_int(conf, CONF_height, h);
  3811. if (flags & (XValue | YValue)) {
  3812. inst->xpos = x;
  3813. inst->ypos = y;
  3814. inst->gotpos = TRUE;
  3815. inst->gravity = ((flags & XNegative ? 1 : 0) |
  3816. (flags & YNegative ? 2 : 0));
  3817. }
  3818. }
  3819. #endif
  3820. if (!compound_text_atom)
  3821. compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
  3822. if (!utf8_string_atom)
  3823. utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
  3824. inst->area = gtk_drawing_area_new();
  3825. #if GTK_CHECK_VERSION(2,0,0)
  3826. inst->imc = gtk_im_multicontext_new();
  3827. #endif
  3828. {
  3829. char *errmsg = setup_fonts_ucs(inst);
  3830. if (errmsg) {
  3831. fprintf(stderr, "%s: %s\n", appname, errmsg);
  3832. exit(1);
  3833. }
  3834. }
  3835. inst->window = make_gtk_toplevel_window(inst);
  3836. {
  3837. const char *winclass = conf_get_str(inst->conf, CONF_winclass);
  3838. if (*winclass)
  3839. gtk_window_set_wmclass(GTK_WINDOW(inst->window),
  3840. winclass, winclass);
  3841. }
  3842. /*
  3843. * Set up the colour map.
  3844. */
  3845. palette_reset(inst);
  3846. inst->width = conf_get_int(inst->conf, CONF_width);
  3847. inst->height = conf_get_int(inst->conf, CONF_height);
  3848. cache_conf_values(inst);
  3849. init_clipboard(inst);
  3850. inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
  3851. inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
  3852. inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
  3853. /*
  3854. * We always create the scrollbar; it remains invisible if
  3855. * unwanted, so we can pop it up quickly if it suddenly becomes
  3856. * desirable.
  3857. */
  3858. if (conf_get_int(inst->conf, CONF_scrollbar_on_left))
  3859. gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
  3860. gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
  3861. if (!conf_get_int(inst->conf, CONF_scrollbar_on_left))
  3862. gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
  3863. gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
  3864. gtk_widget_show(inst->area);
  3865. show_scrollbar(inst, conf_get_int(inst->conf, CONF_scrollbar));
  3866. gtk_widget_show(GTK_WIDGET(inst->hbox));
  3867. /*
  3868. * We must call gtk_widget_realize before setting up the geometry
  3869. * hints, so that GtkApplicationWindow will have actually created
  3870. * its menu bar (if it's going to) and hence compute_geom_hints
  3871. * can find it to take its size into account.
  3872. */
  3873. gtk_widget_realize(inst->window);
  3874. set_geom_hints(inst);
  3875. #if GTK_CHECK_VERSION(3,0,0)
  3876. {
  3877. int wp, hp;
  3878. compute_whole_window_size(inst, inst->width, inst->height, &wp, &hp);
  3879. gtk_window_set_default_size(GTK_WINDOW(inst->window), wp, hp);
  3880. }
  3881. #else
  3882. {
  3883. int w = inst->font_width * inst->width + 2*inst->window_border;
  3884. int h = inst->font_height * inst->height + 2*inst->window_border;
  3885. #if GTK_CHECK_VERSION(2,0,0)
  3886. gtk_widget_set_size_request(inst->area, w, h);
  3887. #else
  3888. gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), w, h);
  3889. #endif
  3890. }
  3891. #endif
  3892. #if GTK_CHECK_VERSION(2,0,0)
  3893. if (inst->gotpos) {
  3894. static const GdkGravity gravities[] = {
  3895. GDK_GRAVITY_NORTH_WEST,
  3896. GDK_GRAVITY_NORTH_EAST,
  3897. GDK_GRAVITY_SOUTH_WEST,
  3898. GDK_GRAVITY_SOUTH_EAST,
  3899. };
  3900. int x = inst->xpos, y = inst->ypos;
  3901. int wp, hp;
  3902. compute_whole_window_size(inst, inst->width, inst->height, &wp, &hp);
  3903. if (inst->gravity & 1) x += (gdk_screen_width() - wp);
  3904. if (inst->gravity & 2) y += (gdk_screen_height() - hp);
  3905. gtk_window_set_gravity(GTK_WINDOW(inst->window),
  3906. gravities[inst->gravity & 3]);
  3907. gtk_window_move(GTK_WINDOW(inst->window), x, y);
  3908. }
  3909. #else
  3910. if (inst->gotpos) {
  3911. int x = inst->xpos, y = inst->ypos;
  3912. GtkRequisition req;
  3913. gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
  3914. if (inst->gravity & 1) x += gdk_screen_width() - req.width;
  3915. if (inst->gravity & 2) y += gdk_screen_height() - req.height;
  3916. gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
  3917. gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
  3918. }
  3919. #endif
  3920. g_signal_connect(G_OBJECT(inst->window), "destroy",
  3921. G_CALLBACK(destroy), inst);
  3922. g_signal_connect(G_OBJECT(inst->window), "delete_event",
  3923. G_CALLBACK(delete_window), inst);
  3924. g_signal_connect(G_OBJECT(inst->window), "key_press_event",
  3925. G_CALLBACK(key_event), inst);
  3926. g_signal_connect(G_OBJECT(inst->window), "key_release_event",
  3927. G_CALLBACK(key_event), inst);
  3928. g_signal_connect(G_OBJECT(inst->window), "focus_in_event",
  3929. G_CALLBACK(focus_event), inst);
  3930. g_signal_connect(G_OBJECT(inst->window), "focus_out_event",
  3931. G_CALLBACK(focus_event), inst);
  3932. g_signal_connect(G_OBJECT(inst->area), "configure_event",
  3933. G_CALLBACK(configure_area), inst);
  3934. #if GTK_CHECK_VERSION(3,0,0)
  3935. g_signal_connect(G_OBJECT(inst->area), "draw",
  3936. G_CALLBACK(draw_area), inst);
  3937. #else
  3938. g_signal_connect(G_OBJECT(inst->area), "expose_event",
  3939. G_CALLBACK(expose_area), inst);
  3940. #endif
  3941. g_signal_connect(G_OBJECT(inst->area), "button_press_event",
  3942. G_CALLBACK(button_event), inst);
  3943. g_signal_connect(G_OBJECT(inst->area), "button_release_event",
  3944. G_CALLBACK(button_event), inst);
  3945. #if GTK_CHECK_VERSION(2,0,0)
  3946. g_signal_connect(G_OBJECT(inst->area), "scroll_event",
  3947. G_CALLBACK(scroll_event), inst);
  3948. #endif
  3949. g_signal_connect(G_OBJECT(inst->area), "motion_notify_event",
  3950. G_CALLBACK(motion_event), inst);
  3951. #if GTK_CHECK_VERSION(2,0,0)
  3952. g_signal_connect(G_OBJECT(inst->imc), "commit",
  3953. G_CALLBACK(input_method_commit_event), inst);
  3954. #endif
  3955. if (conf_get_int(inst->conf, CONF_scrollbar))
  3956. g_signal_connect(G_OBJECT(inst->sbar_adjust), "value_changed",
  3957. G_CALLBACK(scrollbar_moved), inst);
  3958. gtk_widget_add_events(GTK_WIDGET(inst->area),
  3959. GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
  3960. GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
  3961. GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK
  3962. #if GTK_CHECK_VERSION(3,4,0)
  3963. | GDK_SMOOTH_SCROLL_MASK
  3964. #endif
  3965. );
  3966. {
  3967. extern const char *const *const main_icon[];
  3968. extern const int n_main_icon;
  3969. set_window_icon(inst->window, main_icon, n_main_icon);
  3970. }
  3971. gtk_widget_show(inst->window);
  3972. set_window_background(inst);
  3973. /*
  3974. * Set up the Ctrl+rightclick context menu.
  3975. */
  3976. {
  3977. GtkWidget *menuitem;
  3978. char *s;
  3979. extern const int use_event_log, new_session, saved_sessions;
  3980. inst->menu = gtk_menu_new();
  3981. #define MKMENUITEM(title, func) do \
  3982. { \
  3983. menuitem = gtk_menu_item_new_with_label(title); \
  3984. gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
  3985. gtk_widget_show(menuitem); \
  3986. g_signal_connect(G_OBJECT(menuitem), "activate", \
  3987. G_CALLBACK(func), inst); \
  3988. } while (0)
  3989. #define MKSUBMENU(title) do \
  3990. { \
  3991. menuitem = gtk_menu_item_new_with_label(title); \
  3992. gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
  3993. gtk_widget_show(menuitem); \
  3994. } while (0)
  3995. #define MKSEP() do \
  3996. { \
  3997. menuitem = gtk_menu_item_new(); \
  3998. gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
  3999. gtk_widget_show(menuitem); \
  4000. } while (0)
  4001. if (new_session)
  4002. MKMENUITEM("New Session...", new_session_menuitem);
  4003. MKMENUITEM("Restart Session", restart_session_menuitem);
  4004. inst->restartitem = menuitem;
  4005. gtk_widget_set_sensitive(inst->restartitem, FALSE);
  4006. MKMENUITEM("Duplicate Session", dup_session_menuitem);
  4007. if (saved_sessions) {
  4008. inst->sessionsmenu = gtk_menu_new();
  4009. /* sessionsmenu will be updated when it's invoked */
  4010. /* XXX is this the right way to do dynamic menus in Gtk? */
  4011. MKMENUITEM("Saved Sessions", update_savedsess_menu);
  4012. gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
  4013. inst->sessionsmenu);
  4014. }
  4015. MKSEP();
  4016. MKMENUITEM("Change Settings...", change_settings_menuitem);
  4017. MKSEP();
  4018. if (use_event_log)
  4019. MKMENUITEM("Event Log", event_log_menuitem);
  4020. MKSUBMENU("Special Commands");
  4021. inst->specialsmenu = gtk_menu_new();
  4022. gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
  4023. inst->specialsitem1 = menuitem;
  4024. MKSEP();
  4025. inst->specialsitem2 = menuitem;
  4026. gtk_widget_hide(inst->specialsitem1);
  4027. gtk_widget_hide(inst->specialsitem2);
  4028. MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
  4029. MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
  4030. MKMENUITEM("Copy All", copy_all_menuitem);
  4031. MKSEP();
  4032. s = dupcat("About ", appname, NULL);
  4033. MKMENUITEM(s, about_menuitem);
  4034. sfree(s);
  4035. #undef MKMENUITEM
  4036. #undef MKSUBMENU
  4037. #undef MKSEP
  4038. }
  4039. inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
  4040. inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
  4041. inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
  4042. inst->blankcursor = make_mouse_ptr(inst, -1);
  4043. inst->currcursor = inst->textcursor;
  4044. show_mouseptr(inst, 1);
  4045. inst->eventlogstuff = eventlogstuff_new();
  4046. inst->term = term_init(inst->conf, &inst->ucsdata, inst);
  4047. inst->logctx = log_init(inst, inst->conf);
  4048. term_provide_logctx(inst->term, inst->logctx);
  4049. term_size(inst->term, inst->height, inst->width,
  4050. conf_get_int(inst->conf, CONF_savelines));
  4051. start_backend(inst);
  4052. ldisc_echoedit_update(inst->ldisc); /* cause ldisc to notice changes */
  4053. inst->exited = FALSE;
  4054. return inst;
  4055. }