pterm.c 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503
  1. /*
  2. * pterm - a fusion of the PuTTY terminal emulator with a Unix pty
  3. * back end, all running as a GTK application. Wish me luck.
  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 <fcntl.h>
  15. #include <unistd.h>
  16. #include <sys/types.h>
  17. #include <sys/wait.h>
  18. #include <gtk/gtk.h>
  19. #include <gdk/gdkkeysyms.h>
  20. #include <gdk/gdkx.h>
  21. #include <X11/Xlib.h>
  22. #include <X11/Xutil.h>
  23. #include <X11/Xatom.h>
  24. #define PUTTY_DO_GLOBALS /* actually _define_ globals */
  25. #include "putty.h"
  26. #include "terminal.h"
  27. #define CAT2(x,y) x ## y
  28. #define CAT(x,y) CAT2(x,y)
  29. #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
  30. #define NCOLOURS (lenof(((Config *)0)->colours))
  31. GdkAtom compound_text_atom, utf8_string_atom;
  32. extern char **pty_argv; /* declared in pty.c */
  33. extern int use_pty_argv;
  34. struct gui_data {
  35. GtkWidget *window, *area, *sbar;
  36. GtkBox *hbox;
  37. GtkAdjustment *sbar_adjust;
  38. GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
  39. *restartitem;
  40. GtkWidget *sessionsmenu;
  41. GdkPixmap *pixmap;
  42. GdkFont *fonts[4]; /* normal, bold, wide, widebold */
  43. struct {
  44. int charset;
  45. int is_wide;
  46. } fontinfo[4];
  47. int xpos, ypos, gotpos, gravity;
  48. GdkCursor *rawcursor, *textcursor, *blankcursor, *currcursor;
  49. GdkColor cols[NCOLOURS];
  50. GdkColormap *colmap;
  51. wchar_t *pastein_data;
  52. int direct_to_font;
  53. int pastein_data_len;
  54. char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
  55. int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
  56. int font_width, font_height;
  57. int width, height;
  58. int ignore_sbar;
  59. int mouseptr_visible;
  60. guint term_paste_idle_id;
  61. int alt_keycode;
  62. int alt_digits;
  63. char wintitle[sizeof(((Config *)0)->wintitle)];
  64. char icontitle[sizeof(((Config *)0)->wintitle)];
  65. int master_fd, master_func_id;
  66. void *ldisc;
  67. Backend *back;
  68. void *backhandle;
  69. Terminal *term;
  70. void *logctx;
  71. int exited;
  72. struct unicode_data ucsdata;
  73. Config cfg;
  74. void *eventlogstuff;
  75. char *progname, **gtkargvstart;
  76. int ngtkargs;
  77. guint32 input_event_time; /* Timestamp of the most recent input event. */
  78. };
  79. struct draw_ctx {
  80. GdkGC *gc;
  81. struct gui_data *inst;
  82. };
  83. static int send_raw_mouse;
  84. static char *app_name = "pterm";
  85. static void start_backend(struct gui_data *inst);
  86. char *x_get_default(const char *key)
  87. {
  88. return XGetDefault(GDK_DISPLAY(), app_name, key);
  89. }
  90. void connection_fatal(void *frontend, char *p, ...)
  91. {
  92. struct gui_data *inst = (struct gui_data *)frontend;
  93. va_list ap;
  94. char *msg;
  95. va_start(ap, p);
  96. msg = dupvprintf(p, ap);
  97. va_end(ap);
  98. inst->exited = TRUE;
  99. fatal_message_box(inst->window, msg);
  100. sfree(msg);
  101. if (inst->cfg.close_on_exit == FORCE_ON)
  102. cleanup_exit(1);
  103. }
  104. /*
  105. * Default settings that are specific to pterm.
  106. */
  107. FontSpec platform_default_fontspec(const char *name)
  108. {
  109. FontSpec ret;
  110. if (!strcmp(name, "Font"))
  111. strcpy(ret.name, "fixed");
  112. else
  113. *ret.name = '\0';
  114. return ret;
  115. }
  116. Filename platform_default_filename(const char *name)
  117. {
  118. Filename ret;
  119. if (!strcmp(name, "LogFileName"))
  120. strcpy(ret.path, "putty.log");
  121. else
  122. *ret.path = '\0';
  123. return ret;
  124. }
  125. char *platform_default_s(const char *name)
  126. {
  127. return NULL;
  128. }
  129. int platform_default_i(const char *name, int def)
  130. {
  131. if (!strcmp(name, "CloseOnExit"))
  132. return 2; /* maps to FORCE_ON after painful rearrangement :-( */
  133. if (!strcmp(name, "WinNameAlways"))
  134. return 0; /* X natively supports icon titles, so use 'em by default */
  135. return def;
  136. }
  137. void ldisc_update(void *frontend, int echo, int edit)
  138. {
  139. /*
  140. * This is a stub in pterm. If I ever produce a Unix
  141. * command-line ssh/telnet/rlogin client (i.e. a port of plink)
  142. * then it will require some termios manoeuvring analogous to
  143. * that in the Windows plink.c, but here it's meaningless.
  144. */
  145. }
  146. int from_backend(void *frontend, int is_stderr, const char *data, int len)
  147. {
  148. struct gui_data *inst = (struct gui_data *)frontend;
  149. return term_data(inst->term, is_stderr, data, len);
  150. }
  151. void logevent(void *frontend, const char *string)
  152. {
  153. struct gui_data *inst = (struct gui_data *)frontend;
  154. log_eventlog(inst->logctx, string);
  155. logevent_dlg(inst->eventlogstuff, string);
  156. }
  157. int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
  158. {
  159. struct gui_data *inst = (struct gui_data *)frontend;
  160. if (which)
  161. return inst->font_height;
  162. else
  163. return inst->font_width;
  164. }
  165. /*
  166. * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
  167. * into a cooked one (SELECT, EXTEND, PASTE).
  168. *
  169. * In Unix, this is not configurable; the X button arrangement is
  170. * rock-solid across all applications, everyone has a three-button
  171. * mouse or a means of faking it, and there is no need to switch
  172. * buttons around at all.
  173. */
  174. static Mouse_Button translate_button(Mouse_Button button)
  175. {
  176. /* struct gui_data *inst = (struct gui_data *)frontend; */
  177. if (button == MBT_LEFT)
  178. return MBT_SELECT;
  179. if (button == MBT_MIDDLE)
  180. return MBT_PASTE;
  181. if (button == MBT_RIGHT)
  182. return MBT_EXTEND;
  183. return 0; /* shouldn't happen */
  184. }
  185. /*
  186. * Return the top-level GtkWindow associated with a particular
  187. * front end instance.
  188. */
  189. void *get_window(void *frontend)
  190. {
  191. struct gui_data *inst = (struct gui_data *)frontend;
  192. return inst->window;
  193. }
  194. /*
  195. * Minimise or restore the window in response to a server-side
  196. * request.
  197. */
  198. void set_iconic(void *frontend, int iconic)
  199. {
  200. /*
  201. * GTK 1.2 doesn't know how to do this.
  202. */
  203. #if GTK_CHECK_VERSION(2,0,0)
  204. struct gui_data *inst = (struct gui_data *)frontend;
  205. if (iconic)
  206. gtk_window_iconify(GTK_WINDOW(inst->window));
  207. else
  208. gtk_window_deiconify(GTK_WINDOW(inst->window));
  209. #endif
  210. }
  211. /*
  212. * Move the window in response to a server-side request.
  213. */
  214. void move_window(void *frontend, int x, int y)
  215. {
  216. struct gui_data *inst = (struct gui_data *)frontend;
  217. /*
  218. * I assume that when the GTK version of this call is available
  219. * we should use it. Not sure how it differs from the GDK one,
  220. * though.
  221. */
  222. #if GTK_CHECK_VERSION(2,0,0)
  223. gtk_window_move(GTK_WINDOW(inst->window), x, y);
  224. #else
  225. gdk_window_move(inst->window->window, x, y);
  226. #endif
  227. }
  228. /*
  229. * Move the window to the top or bottom of the z-order in response
  230. * to a server-side request.
  231. */
  232. void set_zorder(void *frontend, int top)
  233. {
  234. struct gui_data *inst = (struct gui_data *)frontend;
  235. if (top)
  236. gdk_window_raise(inst->window->window);
  237. else
  238. gdk_window_lower(inst->window->window);
  239. }
  240. /*
  241. * Refresh the window in response to a server-side request.
  242. */
  243. void refresh_window(void *frontend)
  244. {
  245. struct gui_data *inst = (struct gui_data *)frontend;
  246. term_invalidate(inst->term);
  247. }
  248. /*
  249. * Maximise or restore the window in response to a server-side
  250. * request.
  251. */
  252. void set_zoomed(void *frontend, int zoomed)
  253. {
  254. /*
  255. * GTK 1.2 doesn't know how to do this.
  256. */
  257. #if GTK_CHECK_VERSION(2,0,0)
  258. struct gui_data *inst = (struct gui_data *)frontend;
  259. if (iconic)
  260. gtk_window_maximize(GTK_WINDOW(inst->window));
  261. else
  262. gtk_window_unmaximize(GTK_WINDOW(inst->window));
  263. #endif
  264. }
  265. /*
  266. * Report whether the window is iconic, for terminal reports.
  267. */
  268. int is_iconic(void *frontend)
  269. {
  270. struct gui_data *inst = (struct gui_data *)frontend;
  271. return !gdk_window_is_viewable(inst->window->window);
  272. }
  273. /*
  274. * Report the window's position, for terminal reports.
  275. */
  276. void get_window_pos(void *frontend, int *x, int *y)
  277. {
  278. struct gui_data *inst = (struct gui_data *)frontend;
  279. /*
  280. * I assume that when the GTK version of this call is available
  281. * we should use it. Not sure how it differs from the GDK one,
  282. * though.
  283. */
  284. #if GTK_CHECK_VERSION(2,0,0)
  285. gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
  286. #else
  287. gdk_window_get_position(inst->window->window, x, y);
  288. #endif
  289. }
  290. /*
  291. * Report the window's pixel size, for terminal reports.
  292. */
  293. void get_window_pixels(void *frontend, int *x, int *y)
  294. {
  295. struct gui_data *inst = (struct gui_data *)frontend;
  296. /*
  297. * I assume that when the GTK version of this call is available
  298. * we should use it. Not sure how it differs from the GDK one,
  299. * though.
  300. */
  301. #if GTK_CHECK_VERSION(2,0,0)
  302. gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
  303. #else
  304. gdk_window_get_size(inst->window->window, x, y);
  305. #endif
  306. }
  307. /*
  308. * Return the window or icon title.
  309. */
  310. char *get_window_title(void *frontend, int icon)
  311. {
  312. struct gui_data *inst = (struct gui_data *)frontend;
  313. return icon ? inst->icontitle : inst->wintitle;
  314. }
  315. gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
  316. {
  317. struct gui_data *inst = (struct gui_data *)data;
  318. if (!inst->exited && inst->cfg.warn_on_close) {
  319. if (!reallyclose(inst))
  320. return TRUE;
  321. }
  322. return FALSE;
  323. }
  324. static void show_mouseptr(struct gui_data *inst, int show)
  325. {
  326. if (!inst->cfg.hide_mouseptr)
  327. show = 1;
  328. if (show)
  329. gdk_window_set_cursor(inst->area->window, inst->currcursor);
  330. else
  331. gdk_window_set_cursor(inst->area->window, inst->blankcursor);
  332. inst->mouseptr_visible = show;
  333. }
  334. void draw_backing_rect(struct gui_data *inst)
  335. {
  336. GdkGC *gc = gdk_gc_new(inst->area->window);
  337. gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
  338. gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
  339. inst->cfg.width * inst->font_width + 2*inst->cfg.window_border,
  340. inst->cfg.height * inst->font_height + 2*inst->cfg.window_border);
  341. gdk_gc_unref(gc);
  342. }
  343. gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
  344. {
  345. struct gui_data *inst = (struct gui_data *)data;
  346. int w, h, need_size = 0;
  347. /*
  348. * See if the terminal size has changed, in which case we must
  349. * let the terminal know.
  350. */
  351. w = (event->width - 2*inst->cfg.window_border) / inst->font_width;
  352. h = (event->height - 2*inst->cfg.window_border) / inst->font_height;
  353. if (w != inst->width || h != inst->height) {
  354. inst->cfg.width = inst->width = w;
  355. inst->cfg.height = inst->height = h;
  356. need_size = 1;
  357. }
  358. if (inst->pixmap) {
  359. gdk_pixmap_unref(inst->pixmap);
  360. inst->pixmap = NULL;
  361. }
  362. inst->pixmap = gdk_pixmap_new(widget->window,
  363. (inst->cfg.width * inst->font_width +
  364. 2*inst->cfg.window_border),
  365. (inst->cfg.height * inst->font_height +
  366. 2*inst->cfg.window_border), -1);
  367. draw_backing_rect(inst);
  368. if (need_size && inst->term) {
  369. term_size(inst->term, h, w, inst->cfg.savelines);
  370. }
  371. if (inst->term)
  372. term_invalidate(inst->term);
  373. return TRUE;
  374. }
  375. gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
  376. {
  377. struct gui_data *inst = (struct gui_data *)data;
  378. /*
  379. * Pass the exposed rectangle to terminal.c, which will call us
  380. * back to do the actual painting.
  381. */
  382. if (inst->pixmap) {
  383. gdk_draw_pixmap(widget->window,
  384. widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
  385. inst->pixmap,
  386. event->area.x, event->area.y,
  387. event->area.x, event->area.y,
  388. event->area.width, event->area.height);
  389. }
  390. return TRUE;
  391. }
  392. #define KEY_PRESSED(k) \
  393. (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
  394. gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
  395. {
  396. struct gui_data *inst = (struct gui_data *)data;
  397. char output[32];
  398. wchar_t ucsoutput[2];
  399. int ucsval, start, end, special, use_ucsoutput;
  400. /* Remember the timestamp. */
  401. inst->input_event_time = event->time;
  402. /* By default, nothing is generated. */
  403. end = start = 0;
  404. special = use_ucsoutput = FALSE;
  405. /*
  406. * If Alt is being released after typing an Alt+numberpad
  407. * sequence, we should generate the code that was typed.
  408. *
  409. * Note that we only do this if more than one key was actually
  410. * pressed - I don't think Alt+NumPad4 should be ^D or that
  411. * Alt+NumPad3 should be ^C, for example. There's no serious
  412. * inconvenience in having to type a zero before a single-digit
  413. * character code.
  414. */
  415. if (event->type == GDK_KEY_RELEASE &&
  416. (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
  417. event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
  418. inst->alt_keycode >= 0 && inst->alt_digits > 1) {
  419. #ifdef KEY_DEBUGGING
  420. printf("Alt key up, keycode = %d\n", inst->alt_keycode);
  421. #endif
  422. output[0] = inst->alt_keycode;
  423. end = 1;
  424. goto done;
  425. }
  426. if (event->type == GDK_KEY_PRESS) {
  427. #ifdef KEY_DEBUGGING
  428. {
  429. int i;
  430. printf("keypress: keyval = %04x, state = %08x; string =",
  431. event->keyval, event->state);
  432. for (i = 0; event->string[i]; i++)
  433. printf(" %02x", (unsigned char) event->string[i]);
  434. printf("\n");
  435. }
  436. #endif
  437. /*
  438. * NYI: Compose key (!!! requires Unicode faff before even trying)
  439. */
  440. /*
  441. * If Alt has just been pressed, we start potentially
  442. * accumulating an Alt+numberpad code. We do this by
  443. * setting alt_keycode to -1 (nothing yet but plausible).
  444. */
  445. if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
  446. event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
  447. inst->alt_keycode = -1;
  448. inst->alt_digits = 0;
  449. goto done; /* this generates nothing else */
  450. }
  451. /*
  452. * If we're seeing a numberpad key press with Mod1 down,
  453. * consider adding it to alt_keycode if that's sensible.
  454. * Anything _else_ with Mod1 down cancels any possibility
  455. * of an ALT keycode: we set alt_keycode to -2.
  456. */
  457. if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
  458. int digit = -1;
  459. switch (event->keyval) {
  460. case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
  461. case GDK_KP_1: case GDK_KP_End: digit = 1; break;
  462. case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
  463. case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
  464. case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
  465. case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
  466. case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
  467. case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
  468. case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
  469. case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
  470. }
  471. if (digit < 0)
  472. inst->alt_keycode = -2; /* it's invalid */
  473. else {
  474. #ifdef KEY_DEBUGGING
  475. printf("Adding digit %d to keycode %d", digit,
  476. inst->alt_keycode);
  477. #endif
  478. if (inst->alt_keycode == -1)
  479. inst->alt_keycode = digit; /* one-digit code */
  480. else
  481. inst->alt_keycode = inst->alt_keycode * 10 + digit;
  482. inst->alt_digits++;
  483. #ifdef KEY_DEBUGGING
  484. printf(" gives new code %d\n", inst->alt_keycode);
  485. #endif
  486. /* Having used this digit, we now do nothing more with it. */
  487. goto done;
  488. }
  489. }
  490. /*
  491. * Shift-PgUp and Shift-PgDn don't even generate keystrokes
  492. * at all.
  493. */
  494. if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
  495. term_scroll(inst->term, 0, -inst->cfg.height/2);
  496. return TRUE;
  497. }
  498. if (event->keyval == GDK_Page_Up && (event->state & GDK_CONTROL_MASK)) {
  499. term_scroll(inst->term, 0, -1);
  500. return TRUE;
  501. }
  502. if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
  503. term_scroll(inst->term, 0, +inst->cfg.height/2);
  504. return TRUE;
  505. }
  506. if (event->keyval == GDK_Page_Down && (event->state & GDK_CONTROL_MASK)) {
  507. term_scroll(inst->term, 0, +1);
  508. return TRUE;
  509. }
  510. /*
  511. * Neither does Shift-Ins.
  512. */
  513. if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
  514. request_paste(inst);
  515. return TRUE;
  516. }
  517. special = FALSE;
  518. use_ucsoutput = FALSE;
  519. /* ALT+things gives leading Escape. */
  520. output[0] = '\033';
  521. strncpy(output+1, event->string, 31);
  522. if (!*event->string &&
  523. (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
  524. ucsoutput[0] = '\033';
  525. ucsoutput[1] = ucsval;
  526. use_ucsoutput = TRUE;
  527. end = 2;
  528. } else {
  529. output[31] = '\0';
  530. end = strlen(output);
  531. }
  532. if (event->state & GDK_MOD1_MASK) {
  533. start = 0;
  534. if (end == 1) end = 0;
  535. } else
  536. start = 1;
  537. /* Control-` is the same as Control-\ (unless gtk has a better idea) */
  538. if (!event->string[0] && event->keyval == '`' &&
  539. (event->state & GDK_CONTROL_MASK)) {
  540. output[1] = '\x1C';
  541. use_ucsoutput = FALSE;
  542. end = 2;
  543. }
  544. /* Control-Break is the same as Control-C */
  545. if (event->keyval == GDK_Break &&
  546. (event->state & GDK_CONTROL_MASK)) {
  547. output[1] = '\003';
  548. use_ucsoutput = FALSE;
  549. end = 2;
  550. special = TRUE;
  551. }
  552. /* We handle Return ourselves, because it needs to be flagged as
  553. * special to ldisc. */
  554. if (event->keyval == GDK_Return) {
  555. output[1] = '\015';
  556. use_ucsoutput = FALSE;
  557. end = 2;
  558. special = TRUE;
  559. }
  560. /* Control-2, Control-Space and Control-@ are NUL */
  561. if (!event->string[0] &&
  562. (event->keyval == ' ' || event->keyval == '2' ||
  563. event->keyval == '@') &&
  564. (event->state & (GDK_SHIFT_MASK |
  565. GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
  566. output[1] = '\0';
  567. use_ucsoutput = FALSE;
  568. end = 2;
  569. }
  570. /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
  571. if (!event->string[0] && event->keyval == ' ' &&
  572. (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
  573. (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
  574. output[1] = '\240';
  575. use_ucsoutput = FALSE;
  576. end = 2;
  577. }
  578. /* We don't let GTK tell us what Backspace is! We know better. */
  579. if (event->keyval == GDK_BackSpace &&
  580. !(event->state & GDK_SHIFT_MASK)) {
  581. output[1] = inst->cfg.bksp_is_delete ? '\x7F' : '\x08';
  582. use_ucsoutput = FALSE;
  583. end = 2;
  584. special = TRUE;
  585. }
  586. /* For Shift Backspace, do opposite of what is configured. */
  587. if (event->keyval == GDK_BackSpace &&
  588. (event->state & GDK_SHIFT_MASK)) {
  589. output[1] = inst->cfg.bksp_is_delete ? '\x08' : '\x7F';
  590. use_ucsoutput = FALSE;
  591. end = 2;
  592. special = TRUE;
  593. }
  594. /* Shift-Tab is ESC [ Z */
  595. if (event->keyval == GDK_ISO_Left_Tab ||
  596. (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
  597. end = 1 + sprintf(output+1, "\033[Z");
  598. use_ucsoutput = FALSE;
  599. }
  600. /*
  601. * NetHack keypad mode.
  602. */
  603. if (inst->cfg.nethack_keypad) {
  604. char *keys = NULL;
  605. switch (event->keyval) {
  606. case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
  607. case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
  608. case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
  609. case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
  610. case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
  611. case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
  612. case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
  613. case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
  614. case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
  615. }
  616. if (keys) {
  617. end = 2;
  618. if (event->state & GDK_SHIFT_MASK)
  619. output[1] = keys[1];
  620. else
  621. output[1] = keys[0];
  622. use_ucsoutput = FALSE;
  623. goto done;
  624. }
  625. }
  626. /*
  627. * Application keypad mode.
  628. */
  629. if (inst->term->app_keypad_keys && !inst->cfg.no_applic_k) {
  630. int xkey = 0;
  631. switch (event->keyval) {
  632. case GDK_Num_Lock: xkey = 'P'; break;
  633. case GDK_KP_Divide: xkey = 'Q'; break;
  634. case GDK_KP_Multiply: xkey = 'R'; break;
  635. case GDK_KP_Subtract: xkey = 'S'; break;
  636. /*
  637. * Keypad + is tricky. It covers a space that would
  638. * be taken up on the VT100 by _two_ keys; so we
  639. * let Shift select between the two. Worse still,
  640. * in xterm function key mode we change which two...
  641. */
  642. case GDK_KP_Add:
  643. if (inst->cfg.funky_type == FUNKY_XTERM) {
  644. if (event->state & GDK_SHIFT_MASK)
  645. xkey = 'l';
  646. else
  647. xkey = 'k';
  648. } else if (event->state & GDK_SHIFT_MASK)
  649. xkey = 'm';
  650. else
  651. xkey = 'l';
  652. break;
  653. case GDK_KP_Enter: xkey = 'M'; break;
  654. case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
  655. case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
  656. case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
  657. case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
  658. case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
  659. case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
  660. case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
  661. case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
  662. case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
  663. case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
  664. case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
  665. }
  666. if (xkey) {
  667. if (inst->term->vt52_mode) {
  668. if (xkey >= 'P' && xkey <= 'S')
  669. end = 1 + sprintf(output+1, "\033%c", xkey);
  670. else
  671. end = 1 + sprintf(output+1, "\033?%c", xkey);
  672. } else
  673. end = 1 + sprintf(output+1, "\033O%c", xkey);
  674. use_ucsoutput = FALSE;
  675. goto done;
  676. }
  677. }
  678. /*
  679. * Next, all the keys that do tilde codes. (ESC '[' nn '~',
  680. * for integer decimal nn.)
  681. *
  682. * We also deal with the weird ones here. Linux VCs replace F1
  683. * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
  684. * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
  685. * respectively.
  686. */
  687. {
  688. int code = 0;
  689. switch (event->keyval) {
  690. case GDK_F1:
  691. code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
  692. break;
  693. case GDK_F2:
  694. code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
  695. break;
  696. case GDK_F3:
  697. code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
  698. break;
  699. case GDK_F4:
  700. code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
  701. break;
  702. case GDK_F5:
  703. code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
  704. break;
  705. case GDK_F6:
  706. code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
  707. break;
  708. case GDK_F7:
  709. code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
  710. break;
  711. case GDK_F8:
  712. code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
  713. break;
  714. case GDK_F9:
  715. code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
  716. break;
  717. case GDK_F10:
  718. code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
  719. break;
  720. case GDK_F11:
  721. code = 23;
  722. break;
  723. case GDK_F12:
  724. code = 24;
  725. break;
  726. case GDK_F13:
  727. code = 25;
  728. break;
  729. case GDK_F14:
  730. code = 26;
  731. break;
  732. case GDK_F15:
  733. code = 28;
  734. break;
  735. case GDK_F16:
  736. code = 29;
  737. break;
  738. case GDK_F17:
  739. code = 31;
  740. break;
  741. case GDK_F18:
  742. code = 32;
  743. break;
  744. case GDK_F19:
  745. code = 33;
  746. break;
  747. case GDK_F20:
  748. code = 34;
  749. break;
  750. }
  751. if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
  752. case GDK_Home: case GDK_KP_Home:
  753. code = 1;
  754. break;
  755. case GDK_Insert: case GDK_KP_Insert:
  756. code = 2;
  757. break;
  758. case GDK_Delete: case GDK_KP_Delete:
  759. code = 3;
  760. break;
  761. case GDK_End: case GDK_KP_End:
  762. code = 4;
  763. break;
  764. case GDK_Page_Up: case GDK_KP_Page_Up:
  765. code = 5;
  766. break;
  767. case GDK_Page_Down: case GDK_KP_Page_Down:
  768. code = 6;
  769. break;
  770. }
  771. /* Reorder edit keys to physical order */
  772. if (inst->cfg.funky_type == FUNKY_VT400 && code <= 6)
  773. code = "\0\2\1\4\5\3\6"[code];
  774. if (inst->term->vt52_mode && code > 0 && code <= 6) {
  775. end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
  776. use_ucsoutput = FALSE;
  777. goto done;
  778. }
  779. if (inst->cfg.funky_type == FUNKY_SCO && /* SCO function keys */
  780. code >= 11 && code <= 34) {
  781. char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
  782. int index = 0;
  783. switch (event->keyval) {
  784. case GDK_F1: index = 0; break;
  785. case GDK_F2: index = 1; break;
  786. case GDK_F3: index = 2; break;
  787. case GDK_F4: index = 3; break;
  788. case GDK_F5: index = 4; break;
  789. case GDK_F6: index = 5; break;
  790. case GDK_F7: index = 6; break;
  791. case GDK_F8: index = 7; break;
  792. case GDK_F9: index = 8; break;
  793. case GDK_F10: index = 9; break;
  794. case GDK_F11: index = 10; break;
  795. case GDK_F12: index = 11; break;
  796. }
  797. if (event->state & GDK_SHIFT_MASK) index += 12;
  798. if (event->state & GDK_CONTROL_MASK) index += 24;
  799. end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
  800. use_ucsoutput = FALSE;
  801. goto done;
  802. }
  803. if (inst->cfg.funky_type == FUNKY_SCO && /* SCO small keypad */
  804. code >= 1 && code <= 6) {
  805. char codes[] = "HL.FIG";
  806. if (code == 3) {
  807. output[1] = '\x7F';
  808. end = 2;
  809. } else {
  810. end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
  811. }
  812. use_ucsoutput = FALSE;
  813. goto done;
  814. }
  815. if ((inst->term->vt52_mode || inst->cfg.funky_type == FUNKY_VT100P) &&
  816. code >= 11 && code <= 24) {
  817. int offt = 0;
  818. if (code > 15)
  819. offt++;
  820. if (code > 21)
  821. offt++;
  822. if (inst->term->vt52_mode)
  823. end = 1 + sprintf(output+1,
  824. "\x1B%c", code + 'P' - 11 - offt);
  825. else
  826. end = 1 + sprintf(output+1,
  827. "\x1BO%c", code + 'P' - 11 - offt);
  828. use_ucsoutput = FALSE;
  829. goto done;
  830. }
  831. if (inst->cfg.funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
  832. end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
  833. use_ucsoutput = FALSE;
  834. goto done;
  835. }
  836. if (inst->cfg.funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
  837. if (inst->term->vt52_mode)
  838. end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
  839. else
  840. end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
  841. use_ucsoutput = FALSE;
  842. goto done;
  843. }
  844. if (inst->cfg.rxvt_homeend && (code == 1 || code == 4)) {
  845. end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
  846. use_ucsoutput = FALSE;
  847. goto done;
  848. }
  849. if (code) {
  850. end = 1 + sprintf(output+1, "\x1B[%d~", code);
  851. use_ucsoutput = FALSE;
  852. goto done;
  853. }
  854. }
  855. /*
  856. * Cursor keys. (This includes the numberpad cursor keys,
  857. * if we haven't already done them due to app keypad mode.)
  858. *
  859. * Here we also process un-numlocked un-appkeypadded KP5,
  860. * which sends ESC [ G.
  861. */
  862. {
  863. int xkey = 0;
  864. switch (event->keyval) {
  865. case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
  866. case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
  867. case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
  868. case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
  869. case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
  870. }
  871. if (xkey) {
  872. /*
  873. * The arrow keys normally do ESC [ A and so on. In
  874. * app cursor keys mode they do ESC O A instead.
  875. * Ctrl toggles the two modes.
  876. */
  877. if (inst->term->vt52_mode) {
  878. end = 1 + sprintf(output+1, "\033%c", xkey);
  879. } else if (!inst->term->app_cursor_keys ^
  880. !(event->state & GDK_CONTROL_MASK)) {
  881. end = 1 + sprintf(output+1, "\033O%c", xkey);
  882. } else {
  883. end = 1 + sprintf(output+1, "\033[%c", xkey);
  884. }
  885. use_ucsoutput = FALSE;
  886. goto done;
  887. }
  888. }
  889. goto done;
  890. }
  891. done:
  892. if (end-start > 0) {
  893. #ifdef KEY_DEBUGGING
  894. int i;
  895. printf("generating sequence:");
  896. for (i = start; i < end; i++)
  897. printf(" %02x", (unsigned char) output[i]);
  898. printf("\n");
  899. #endif
  900. if (special) {
  901. /*
  902. * For special control characters, the character set
  903. * should never matter.
  904. */
  905. output[end] = '\0'; /* NUL-terminate */
  906. if (inst->ldisc)
  907. ldisc_send(inst->ldisc, output+start, -2, 1);
  908. } else if (!inst->direct_to_font) {
  909. if (!use_ucsoutput) {
  910. /*
  911. * The stuff we've just generated is assumed to be
  912. * ISO-8859-1! This sounds insane, but `man
  913. * XLookupString' agrees: strings of this type
  914. * returned from the X server are hardcoded to
  915. * 8859-1. Strictly speaking we should be doing
  916. * this using some sort of GtkIMContext, which (if
  917. * we're lucky) would give us our data directly in
  918. * Unicode; but that's not supported in GTK 1.2 as
  919. * far as I can tell, and it's poorly documented
  920. * even in 2.0, so it'll have to wait.
  921. */
  922. if (inst->ldisc)
  923. lpage_send(inst->ldisc, CS_ISO8859_1, output+start,
  924. end-start, 1);
  925. } else {
  926. /*
  927. * We generated our own Unicode key data from the
  928. * keysym, so use that instead.
  929. */
  930. if (inst->ldisc)
  931. luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
  932. }
  933. } else {
  934. /*
  935. * In direct-to-font mode, we just send the string
  936. * exactly as we received it.
  937. */
  938. if (inst->ldisc)
  939. ldisc_send(inst->ldisc, output+start, end-start, 1);
  940. }
  941. show_mouseptr(inst, 0);
  942. term_seen_key_event(inst->term);
  943. term_out(inst->term);
  944. }
  945. return TRUE;
  946. }
  947. gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
  948. {
  949. struct gui_data *inst = (struct gui_data *)data;
  950. int shift, ctrl, alt, x, y, button, act;
  951. /* Remember the timestamp. */
  952. inst->input_event_time = event->time;
  953. show_mouseptr(inst, 1);
  954. if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
  955. term_scroll(inst->term, 0, -5);
  956. return TRUE;
  957. }
  958. if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
  959. term_scroll(inst->term, 0, +5);
  960. return TRUE;
  961. }
  962. shift = event->state & GDK_SHIFT_MASK;
  963. ctrl = event->state & GDK_CONTROL_MASK;
  964. alt = event->state & GDK_MOD1_MASK;
  965. if (event->button == 3 && ctrl) {
  966. gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
  967. event->button, event->time);
  968. return TRUE;
  969. }
  970. if (event->button == 1)
  971. button = MBT_LEFT;
  972. else if (event->button == 2)
  973. button = MBT_MIDDLE;
  974. else if (event->button == 3)
  975. button = MBT_RIGHT;
  976. else
  977. return FALSE; /* don't even know what button! */
  978. switch (event->type) {
  979. case GDK_BUTTON_PRESS: act = MA_CLICK; break;
  980. case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
  981. case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
  982. case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
  983. default: return FALSE; /* don't know this event type */
  984. }
  985. if (send_raw_mouse && !(inst->cfg.mouse_override && shift) &&
  986. act != MA_CLICK && act != MA_RELEASE)
  987. return TRUE; /* we ignore these in raw mouse mode */
  988. x = (event->x - inst->cfg.window_border) / inst->font_width;
  989. y = (event->y - inst->cfg.window_border) / inst->font_height;
  990. term_mouse(inst->term, button, translate_button(button), act,
  991. x, y, shift, ctrl, alt);
  992. return TRUE;
  993. }
  994. gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
  995. {
  996. struct gui_data *inst = (struct gui_data *)data;
  997. int shift, ctrl, alt, x, y, button;
  998. /* Remember the timestamp. */
  999. inst->input_event_time = event->time;
  1000. show_mouseptr(inst, 1);
  1001. shift = event->state & GDK_SHIFT_MASK;
  1002. ctrl = event->state & GDK_CONTROL_MASK;
  1003. alt = event->state & GDK_MOD1_MASK;
  1004. if (event->state & GDK_BUTTON1_MASK)
  1005. button = MBT_LEFT;
  1006. else if (event->state & GDK_BUTTON2_MASK)
  1007. button = MBT_MIDDLE;
  1008. else if (event->state & GDK_BUTTON3_MASK)
  1009. button = MBT_RIGHT;
  1010. else
  1011. return FALSE; /* don't even know what button! */
  1012. x = (event->x - inst->cfg.window_border) / inst->font_width;
  1013. y = (event->y - inst->cfg.window_border) / inst->font_height;
  1014. term_mouse(inst->term, button, translate_button(button), MA_DRAG,
  1015. x, y, shift, ctrl, alt);
  1016. return TRUE;
  1017. }
  1018. void frontend_keypress(void *handle)
  1019. {
  1020. struct gui_data *inst = (struct gui_data *)handle;
  1021. /*
  1022. * If our child process has exited but not closed, terminate on
  1023. * any keypress.
  1024. */
  1025. if (inst->exited)
  1026. exit(0);
  1027. }
  1028. gint timer_func(gpointer data)
  1029. {
  1030. struct gui_data *inst = (struct gui_data *)data;
  1031. int exitcode;
  1032. if (!inst->exited &&
  1033. (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
  1034. inst->exited = TRUE;
  1035. if (inst->cfg.close_on_exit == FORCE_ON ||
  1036. (inst->cfg.close_on_exit == AUTO && exitcode == 0))
  1037. exit(0); /* just go. */
  1038. if (inst->ldisc) {
  1039. ldisc_free(inst->ldisc);
  1040. inst->ldisc = NULL;
  1041. }
  1042. if (inst->back) {
  1043. inst->back->free(inst->backhandle);
  1044. inst->backhandle = NULL;
  1045. inst->back = NULL;
  1046. update_specials_menu(inst);
  1047. }
  1048. gtk_widget_show(inst->restartitem);
  1049. }
  1050. term_update(inst->term);
  1051. term_blink(inst->term, 0);
  1052. return TRUE;
  1053. }
  1054. void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
  1055. {
  1056. /*
  1057. * We must process exceptional notifications before ordinary
  1058. * readability ones, or we may go straight past the urgent
  1059. * marker.
  1060. */
  1061. if (condition & GDK_INPUT_EXCEPTION)
  1062. select_result(sourcefd, 4);
  1063. if (condition & GDK_INPUT_READ)
  1064. select_result(sourcefd, 1);
  1065. if (condition & GDK_INPUT_WRITE)
  1066. select_result(sourcefd, 2);
  1067. }
  1068. void destroy(GtkWidget *widget, gpointer data)
  1069. {
  1070. gtk_main_quit();
  1071. }
  1072. gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
  1073. {
  1074. struct gui_data *inst = (struct gui_data *)data;
  1075. inst->term->has_focus = event->in;
  1076. term_out(inst->term);
  1077. term_update(inst->term);
  1078. show_mouseptr(inst, 1);
  1079. return FALSE;
  1080. }
  1081. /*
  1082. * set or clear the "raw mouse message" mode
  1083. */
  1084. void set_raw_mouse_mode(void *frontend, int activate)
  1085. {
  1086. struct gui_data *inst = (struct gui_data *)frontend;
  1087. activate = activate && !inst->cfg.no_mouse_rep;
  1088. send_raw_mouse = activate;
  1089. if (send_raw_mouse)
  1090. inst->currcursor = inst->rawcursor;
  1091. else
  1092. inst->currcursor = inst->textcursor;
  1093. show_mouseptr(inst, inst->mouseptr_visible);
  1094. }
  1095. void request_resize(void *frontend, int w, int h)
  1096. {
  1097. struct gui_data *inst = (struct gui_data *)frontend;
  1098. int large_x, large_y;
  1099. int offset_x, offset_y;
  1100. int area_x, area_y;
  1101. GtkRequisition inner, outer;
  1102. /*
  1103. * This is a heinous hack dreamed up by the gnome-terminal
  1104. * people to get around a limitation in gtk. The problem is
  1105. * that in order to set the size correctly we really need to be
  1106. * calling gtk_window_resize - but that needs to know the size
  1107. * of the _whole window_, not the drawing area. So what we do
  1108. * is to set an artificially huge size request on the drawing
  1109. * area, recompute the resulting size request on the window,
  1110. * and look at the difference between the two. That gives us
  1111. * the x and y offsets we need to translate drawing area size
  1112. * into window size for real, and then we call
  1113. * gtk_window_resize.
  1114. */
  1115. /*
  1116. * We start by retrieving the current size of the whole window.
  1117. * Adding a bit to _that_ will give us a value we can use as a
  1118. * bogus size request which guarantees to be bigger than the
  1119. * current size of the drawing area.
  1120. */
  1121. get_window_pixels(inst, &large_x, &large_y);
  1122. large_x += 32;
  1123. large_y += 32;
  1124. #if GTK_CHECK_VERSION(2,0,0)
  1125. gtk_widget_set_size_request(inst->area, large_x, large_y);
  1126. #else
  1127. gtk_widget_set_usize(inst->area, large_x, large_y);
  1128. #endif
  1129. gtk_widget_size_request(inst->area, &inner);
  1130. gtk_widget_size_request(inst->window, &outer);
  1131. offset_x = outer.width - inner.width;
  1132. offset_y = outer.height - inner.height;
  1133. area_x = inst->font_width * w + 2*inst->cfg.window_border;
  1134. area_y = inst->font_height * h + 2*inst->cfg.window_border;
  1135. /*
  1136. * Now we must set the size request on the drawing area back to
  1137. * something sensible before we commit the real resize. Best
  1138. * way to do this, I think, is to set it to what the size is
  1139. * really going to end up being.
  1140. */
  1141. #if GTK_CHECK_VERSION(2,0,0)
  1142. gtk_widget_set_size_request(inst->area, area_x, area_y);
  1143. #else
  1144. gtk_widget_set_usize(inst->area, area_x, area_y);
  1145. gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
  1146. #endif
  1147. gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
  1148. #if GTK_CHECK_VERSION(2,0,0)
  1149. gtk_window_resize(GTK_WINDOW(inst->window),
  1150. area_x + offset_x, area_y + offset_y);
  1151. #else
  1152. gdk_window_resize(inst->window->window,
  1153. area_x + offset_x, area_y + offset_y);
  1154. #endif
  1155. }
  1156. static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
  1157. {
  1158. gboolean success[1];
  1159. inst->cols[n].red = r * 0x0101;
  1160. inst->cols[n].green = g * 0x0101;
  1161. inst->cols[n].blue = b * 0x0101;
  1162. gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
  1163. gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
  1164. FALSE, FALSE, success);
  1165. if (!success[0])
  1166. g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
  1167. n, r, g, b);
  1168. }
  1169. void set_window_background(struct gui_data *inst)
  1170. {
  1171. if (inst->area && inst->area->window)
  1172. gdk_window_set_background(inst->area->window, &inst->cols[18]);
  1173. if (inst->window && inst->window->window)
  1174. gdk_window_set_background(inst->window->window, &inst->cols[18]);
  1175. }
  1176. void palette_set(void *frontend, int n, int r, int g, int b)
  1177. {
  1178. struct gui_data *inst = (struct gui_data *)frontend;
  1179. static const int first[21] = {
  1180. 0, 2, 4, 6, 8, 10, 12, 14,
  1181. 1, 3, 5, 7, 9, 11, 13, 15,
  1182. 16, 17, 18, 20, 22
  1183. };
  1184. real_palette_set(inst, first[n], r, g, b);
  1185. if (first[n] >= 18)
  1186. real_palette_set(inst, first[n] + 1, r, g, b);
  1187. if (first[n] == 18)
  1188. set_window_background(inst);
  1189. }
  1190. void palette_reset(void *frontend)
  1191. {
  1192. struct gui_data *inst = (struct gui_data *)frontend;
  1193. /* This maps colour indices in inst->cfg to those used in inst->cols. */
  1194. static const int ww[] = {
  1195. 6, 7, 8, 9, 10, 11, 12, 13,
  1196. 14, 15, 16, 17, 18, 19, 20, 21,
  1197. 0, 1, 2, 3, 4, 5
  1198. };
  1199. gboolean success[NCOLOURS];
  1200. int i;
  1201. assert(lenof(ww) == NCOLOURS);
  1202. if (!inst->colmap) {
  1203. inst->colmap = gdk_colormap_get_system();
  1204. } else {
  1205. gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
  1206. }
  1207. for (i = 0; i < NCOLOURS; i++) {
  1208. inst->cols[i].red = inst->cfg.colours[ww[i]][0] * 0x0101;
  1209. inst->cols[i].green = inst->cfg.colours[ww[i]][1] * 0x0101;
  1210. inst->cols[i].blue = inst->cfg.colours[ww[i]][2] * 0x0101;
  1211. }
  1212. gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
  1213. FALSE, FALSE, success);
  1214. for (i = 0; i < NCOLOURS; i++) {
  1215. if (!success[i])
  1216. g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
  1217. appname, i, inst->cfg.colours[i][0],
  1218. inst->cfg.colours[i][1], inst->cfg.colours[i][2]);
  1219. }
  1220. set_window_background(inst);
  1221. }
  1222. /* Ensure that all the cut buffers exist - according to the ICCCM, we must
  1223. * do this before we start using cut buffers.
  1224. */
  1225. void init_cutbuffers()
  1226. {
  1227. XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
  1228. XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, "", 0);
  1229. XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
  1230. XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, "", 0);
  1231. XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
  1232. XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, "", 0);
  1233. XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
  1234. XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, "", 0);
  1235. XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
  1236. XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, "", 0);
  1237. XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
  1238. XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, "", 0);
  1239. XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
  1240. XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, "", 0);
  1241. XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
  1242. XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, "", 0);
  1243. }
  1244. /* Store the data in a cut-buffer. */
  1245. void store_cutbuffer(char * ptr, int len)
  1246. {
  1247. /* ICCCM says we must rotate the buffers before storing to buffer 0. */
  1248. XRotateBuffers(GDK_DISPLAY(), 1);
  1249. XStoreBytes(GDK_DISPLAY(), ptr, len);
  1250. }
  1251. /* Retrieve data from a cut-buffer.
  1252. * Returned data needs to be freed with XFree().
  1253. */
  1254. char * retrieve_cutbuffer(int * nbytes)
  1255. {
  1256. char * ptr;
  1257. ptr = XFetchBytes(GDK_DISPLAY(), nbytes);
  1258. if (*nbytes <= 0 && ptr != 0) {
  1259. XFree(ptr);
  1260. ptr = 0;
  1261. }
  1262. return ptr;
  1263. }
  1264. void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
  1265. {
  1266. struct gui_data *inst = (struct gui_data *)frontend;
  1267. if (inst->pasteout_data)
  1268. sfree(inst->pasteout_data);
  1269. if (inst->pasteout_data_ctext)
  1270. sfree(inst->pasteout_data_ctext);
  1271. if (inst->pasteout_data_utf8)
  1272. sfree(inst->pasteout_data_utf8);
  1273. /*
  1274. * Set up UTF-8 and compound text paste data. This only happens
  1275. * if we aren't in direct-to-font mode using the D800 hack.
  1276. */
  1277. if (!inst->direct_to_font) {
  1278. wchar_t *tmp = data;
  1279. int tmplen = len;
  1280. XTextProperty tp;
  1281. char *list[1];
  1282. inst->pasteout_data_utf8 = snewn(len*6, char);
  1283. inst->pasteout_data_utf8_len = len*6;
  1284. inst->pasteout_data_utf8_len =
  1285. charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
  1286. inst->pasteout_data_utf8_len,
  1287. CS_UTF8, NULL, NULL, 0);
  1288. if (inst->pasteout_data_utf8_len == 0) {
  1289. sfree(inst->pasteout_data_utf8);
  1290. inst->pasteout_data_utf8 = NULL;
  1291. } else {
  1292. inst->pasteout_data_utf8 =
  1293. sresize(inst->pasteout_data_utf8,
  1294. inst->pasteout_data_utf8_len + 1, char);
  1295. inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
  1296. }
  1297. /*
  1298. * Now let Xlib convert our UTF-8 data into compound text.
  1299. */
  1300. list[0] = inst->pasteout_data_utf8;
  1301. if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list, 1,
  1302. XCompoundTextStyle, &tp) == 0) {
  1303. inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
  1304. memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
  1305. inst->pasteout_data_ctext_len = tp.nitems;
  1306. XFree(tp.value);
  1307. } else {
  1308. inst->pasteout_data_ctext = NULL;
  1309. inst->pasteout_data_ctext_len = 0;
  1310. }
  1311. } else {
  1312. inst->pasteout_data_utf8 = NULL;
  1313. inst->pasteout_data_utf8_len = 0;
  1314. inst->pasteout_data_ctext = NULL;
  1315. inst->pasteout_data_ctext_len = 0;
  1316. }
  1317. inst->pasteout_data = snewn(len*6, char);
  1318. inst->pasteout_data_len = len*6;
  1319. inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
  1320. data, len, inst->pasteout_data,
  1321. inst->pasteout_data_len,
  1322. NULL, NULL, NULL);
  1323. if (inst->pasteout_data_len == 0) {
  1324. sfree(inst->pasteout_data);
  1325. inst->pasteout_data = NULL;
  1326. } else {
  1327. inst->pasteout_data =
  1328. sresize(inst->pasteout_data, inst->pasteout_data_len, char);
  1329. }
  1330. store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
  1331. if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
  1332. inst->input_event_time)) {
  1333. gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
  1334. GDK_SELECTION_TYPE_STRING, 1);
  1335. if (inst->pasteout_data_ctext)
  1336. gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
  1337. compound_text_atom, 1);
  1338. if (inst->pasteout_data_utf8)
  1339. gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
  1340. utf8_string_atom, 1);
  1341. }
  1342. if (must_deselect)
  1343. term_deselect(inst->term);
  1344. }
  1345. void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
  1346. guint info, guint time_stamp, gpointer data)
  1347. {
  1348. struct gui_data *inst = (struct gui_data *)data;
  1349. if (seldata->target == utf8_string_atom)
  1350. gtk_selection_data_set(seldata, seldata->target, 8,
  1351. inst->pasteout_data_utf8,
  1352. inst->pasteout_data_utf8_len);
  1353. else if (seldata->target == compound_text_atom)
  1354. gtk_selection_data_set(seldata, seldata->target, 8,
  1355. inst->pasteout_data_ctext,
  1356. inst->pasteout_data_ctext_len);
  1357. else
  1358. gtk_selection_data_set(seldata, seldata->target, 8,
  1359. inst->pasteout_data, inst->pasteout_data_len);
  1360. }
  1361. gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
  1362. gpointer data)
  1363. {
  1364. struct gui_data *inst = (struct gui_data *)data;
  1365. term_deselect(inst->term);
  1366. if (inst->pasteout_data)
  1367. sfree(inst->pasteout_data);
  1368. if (inst->pasteout_data_ctext)
  1369. sfree(inst->pasteout_data_ctext);
  1370. if (inst->pasteout_data_utf8)
  1371. sfree(inst->pasteout_data_utf8);
  1372. inst->pasteout_data = NULL;
  1373. inst->pasteout_data_len = 0;
  1374. inst->pasteout_data_ctext = NULL;
  1375. inst->pasteout_data_ctext_len = 0;
  1376. inst->pasteout_data_utf8 = NULL;
  1377. inst->pasteout_data_utf8_len = 0;
  1378. return TRUE;
  1379. }
  1380. void request_paste(void *frontend)
  1381. {
  1382. struct gui_data *inst = (struct gui_data *)frontend;
  1383. /*
  1384. * In Unix, pasting is asynchronous: all we can do at the
  1385. * moment is to call gtk_selection_convert(), and when the data
  1386. * comes back _then_ we can call term_do_paste().
  1387. */
  1388. if (!inst->direct_to_font) {
  1389. /*
  1390. * First we attempt to retrieve the selection as a UTF-8
  1391. * string (which we will convert to the correct code page
  1392. * before sending to the session, of course). If that
  1393. * fails, selection_received() will be informed and will
  1394. * fall back to an ordinary string.
  1395. */
  1396. gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
  1397. utf8_string_atom,
  1398. inst->input_event_time);
  1399. } else {
  1400. /*
  1401. * If we're in direct-to-font mode, we disable UTF-8
  1402. * pasting, and go straight to ordinary string data.
  1403. */
  1404. gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
  1405. GDK_SELECTION_TYPE_STRING,
  1406. inst->input_event_time);
  1407. }
  1408. }
  1409. gint idle_paste_func(gpointer data); /* forward ref */
  1410. void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
  1411. guint time, gpointer data)
  1412. {
  1413. struct gui_data *inst = (struct gui_data *)data;
  1414. XTextProperty tp;
  1415. char **list;
  1416. char *text;
  1417. int length, count, ret;
  1418. int free_list_required = 0;
  1419. int free_required = 0;
  1420. int charset;
  1421. if (seldata->target == utf8_string_atom && seldata->length <= 0) {
  1422. /*
  1423. * Failed to get a UTF-8 selection string. Try compound
  1424. * text next.
  1425. */
  1426. gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
  1427. compound_text_atom,
  1428. inst->input_event_time);
  1429. return;
  1430. }
  1431. if (seldata->target == compound_text_atom && seldata->length <= 0) {
  1432. /*
  1433. * Failed to get UTF-8 or compound text. Try an ordinary
  1434. * string.
  1435. */
  1436. gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
  1437. GDK_SELECTION_TYPE_STRING,
  1438. inst->input_event_time);
  1439. return;
  1440. }
  1441. /*
  1442. * If we have data, but it's not of a type we can deal with,
  1443. * we have to ignore the data.
  1444. */
  1445. if (seldata->length > 0 &&
  1446. seldata->type != GDK_SELECTION_TYPE_STRING &&
  1447. seldata->type != compound_text_atom &&
  1448. seldata->type != utf8_string_atom)
  1449. return;
  1450. /*
  1451. * If we have no data, try looking in a cut buffer.
  1452. */
  1453. if (seldata->length <= 0) {
  1454. text = retrieve_cutbuffer(&length);
  1455. if (length == 0)
  1456. return;
  1457. /* Xterm is rumoured to expect Latin-1, though I havn't checked the
  1458. * source, so use that as a de-facto standard. */
  1459. charset = CS_ISO8859_1;
  1460. free_required = 1;
  1461. } else {
  1462. /*
  1463. * Convert COMPOUND_TEXT into UTF-8.
  1464. */
  1465. if (seldata->type == compound_text_atom) {
  1466. tp.value = seldata->data;
  1467. tp.encoding = (Atom) seldata->type;
  1468. tp.format = seldata->format;
  1469. tp.nitems = seldata->length;
  1470. ret = Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp,
  1471. &list, &count);
  1472. if (ret != 0 || count != 1) {
  1473. /*
  1474. * Compound text failed; fall back to STRING.
  1475. */
  1476. gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
  1477. GDK_SELECTION_TYPE_STRING,
  1478. inst->input_event_time);
  1479. return;
  1480. }
  1481. text = list[0];
  1482. length = strlen(list[0]);
  1483. charset = CS_UTF8;
  1484. free_list_required = 1;
  1485. } else {
  1486. text = (char *)seldata->data;
  1487. length = seldata->length;
  1488. charset = (seldata->type == utf8_string_atom ?
  1489. CS_UTF8 : inst->ucsdata.line_codepage);
  1490. }
  1491. }
  1492. if (inst->pastein_data)
  1493. sfree(inst->pastein_data);
  1494. inst->pastein_data = snewn(length, wchar_t);
  1495. inst->pastein_data_len = length;
  1496. inst->pastein_data_len =
  1497. mb_to_wc(charset, 0, text, length,
  1498. inst->pastein_data, inst->pastein_data_len);
  1499. term_do_paste(inst->term);
  1500. if (term_paste_pending(inst->term))
  1501. inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
  1502. if (free_list_required)
  1503. XFreeStringList(list);
  1504. if (free_required)
  1505. XFree(text);
  1506. }
  1507. gint idle_paste_func(gpointer data)
  1508. {
  1509. struct gui_data *inst = (struct gui_data *)data;
  1510. if (term_paste_pending(inst->term))
  1511. term_paste(inst->term);
  1512. else
  1513. gtk_idle_remove(inst->term_paste_idle_id);
  1514. return TRUE;
  1515. }
  1516. void get_clip(void *frontend, wchar_t ** p, int *len)
  1517. {
  1518. struct gui_data *inst = (struct gui_data *)frontend;
  1519. if (p) {
  1520. *p = inst->pastein_data;
  1521. *len = inst->pastein_data_len;
  1522. }
  1523. }
  1524. static void set_window_titles(struct gui_data *inst)
  1525. {
  1526. /*
  1527. * We must always call set_icon_name after calling set_title,
  1528. * since set_title will write both names. Irritating, but such
  1529. * is life.
  1530. */
  1531. gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
  1532. if (!inst->cfg.win_name_always)
  1533. gdk_window_set_icon_name(inst->window->window, inst->icontitle);
  1534. }
  1535. void set_title(void *frontend, char *title)
  1536. {
  1537. struct gui_data *inst = (struct gui_data *)frontend;
  1538. strncpy(inst->wintitle, title, lenof(inst->wintitle));
  1539. inst->wintitle[lenof(inst->wintitle)-1] = '\0';
  1540. set_window_titles(inst);
  1541. }
  1542. void set_icon(void *frontend, char *title)
  1543. {
  1544. struct gui_data *inst = (struct gui_data *)frontend;
  1545. strncpy(inst->icontitle, title, lenof(inst->icontitle));
  1546. inst->icontitle[lenof(inst->icontitle)-1] = '\0';
  1547. set_window_titles(inst);
  1548. }
  1549. void set_sbar(void *frontend, int total, int start, int page)
  1550. {
  1551. struct gui_data *inst = (struct gui_data *)frontend;
  1552. if (!inst->cfg.scrollbar)
  1553. return;
  1554. inst->sbar_adjust->lower = 0;
  1555. inst->sbar_adjust->upper = total;
  1556. inst->sbar_adjust->value = start;
  1557. inst->sbar_adjust->page_size = page;
  1558. inst->sbar_adjust->step_increment = 1;
  1559. inst->sbar_adjust->page_increment = page/2;
  1560. inst->ignore_sbar = TRUE;
  1561. gtk_adjustment_changed(inst->sbar_adjust);
  1562. inst->ignore_sbar = FALSE;
  1563. }
  1564. void scrollbar_moved(GtkAdjustment *adj, gpointer data)
  1565. {
  1566. struct gui_data *inst = (struct gui_data *)data;
  1567. if (!inst->cfg.scrollbar)
  1568. return;
  1569. if (!inst->ignore_sbar)
  1570. term_scroll(inst->term, 1, (int)adj->value);
  1571. }
  1572. void sys_cursor(void *frontend, int x, int y)
  1573. {
  1574. /*
  1575. * This is meaningless under X.
  1576. */
  1577. }
  1578. /*
  1579. * This is still called when mode==BELL_VISUAL, even though the
  1580. * visual bell is handled entirely within terminal.c, because we
  1581. * may want to perform additional actions on any kind of bell (for
  1582. * example, taskbar flashing in Windows).
  1583. */
  1584. void beep(void *frontend, int mode)
  1585. {
  1586. if (mode != BELL_VISUAL)
  1587. gdk_beep();
  1588. }
  1589. int char_width(Context ctx, int uc)
  1590. {
  1591. /*
  1592. * Under X, any fixed-width font really _is_ fixed-width.
  1593. * Double-width characters will be dealt with using a separate
  1594. * font. For the moment we can simply return 1.
  1595. */
  1596. return 1;
  1597. }
  1598. Context get_ctx(void *frontend)
  1599. {
  1600. struct gui_data *inst = (struct gui_data *)frontend;
  1601. struct draw_ctx *dctx;
  1602. if (!inst->area->window)
  1603. return NULL;
  1604. dctx = snew(struct draw_ctx);
  1605. dctx->inst = inst;
  1606. dctx->gc = gdk_gc_new(inst->area->window);
  1607. return dctx;
  1608. }
  1609. void free_ctx(Context ctx)
  1610. {
  1611. struct draw_ctx *dctx = (struct draw_ctx *)ctx;
  1612. /* struct gui_data *inst = dctx->inst; */
  1613. GdkGC *gc = dctx->gc;
  1614. gdk_gc_unref(gc);
  1615. sfree(dctx);
  1616. }
  1617. /*
  1618. * Draw a line of text in the window, at given character
  1619. * coordinates, in given attributes.
  1620. *
  1621. * We are allowed to fiddle with the contents of `text'.
  1622. */
  1623. void do_text_internal(Context ctx, int x, int y, char *text, int len,
  1624. unsigned long attr, int lattr)
  1625. {
  1626. struct draw_ctx *dctx = (struct draw_ctx *)ctx;
  1627. struct gui_data *inst = dctx->inst;
  1628. GdkGC *gc = dctx->gc;
  1629. int nfg, nbg, t, fontid, shadow, rlen, widefactor;
  1630. nfg = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
  1631. nfg = 2 * (nfg & 0xF) + (nfg & 0x10 ? 1 : 0);
  1632. nbg = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
  1633. nbg = 2 * (nbg & 0xF) + (nbg & 0x10 ? 1 : 0);
  1634. if (attr & ATTR_REVERSE) {
  1635. t = nfg;
  1636. nfg = nbg;
  1637. nbg = t;
  1638. }
  1639. if (inst->cfg.bold_colour && (attr & ATTR_BOLD))
  1640. nfg |= 1;
  1641. if (inst->cfg.bold_colour && (attr & ATTR_BLINK))
  1642. nbg |= 1;
  1643. if (attr & TATTR_ACTCURS) {
  1644. nfg = NCOLOURS-2;
  1645. nbg = NCOLOURS-1;
  1646. }
  1647. fontid = shadow = 0;
  1648. if (attr & ATTR_WIDE) {
  1649. widefactor = 2;
  1650. fontid |= 2;
  1651. } else {
  1652. widefactor = 1;
  1653. }
  1654. if ((attr & ATTR_BOLD) && !inst->cfg.bold_colour) {
  1655. if (inst->fonts[fontid | 1])
  1656. fontid |= 1;
  1657. else
  1658. shadow = 1;
  1659. }
  1660. if (lattr != LATTR_NORM) {
  1661. x *= 2;
  1662. if (x >= inst->term->cols)
  1663. return;
  1664. if (x + len*2*widefactor > inst->term->cols)
  1665. len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
  1666. rlen = len * 2;
  1667. } else
  1668. rlen = len;
  1669. {
  1670. GdkRectangle r;
  1671. r.x = x*inst->font_width+inst->cfg.window_border;
  1672. r.y = y*inst->font_height+inst->cfg.window_border;
  1673. r.width = rlen*widefactor*inst->font_width;
  1674. r.height = inst->font_height;
  1675. gdk_gc_set_clip_rectangle(gc, &r);
  1676. }
  1677. gdk_gc_set_foreground(gc, &inst->cols[nbg]);
  1678. gdk_draw_rectangle(inst->pixmap, gc, 1,
  1679. x*inst->font_width+inst->cfg.window_border,
  1680. y*inst->font_height+inst->cfg.window_border,
  1681. rlen*widefactor*inst->font_width, inst->font_height);
  1682. gdk_gc_set_foreground(gc, &inst->cols[nfg]);
  1683. {
  1684. GdkWChar *gwcs;
  1685. gchar *gcs;
  1686. wchar_t *wcs;
  1687. int i;
  1688. wcs = snewn(len+1, wchar_t);
  1689. for (i = 0; i < len; i++) {
  1690. wcs[i] = (wchar_t) ((attr & CSET_MASK) + (text[i] & CHAR_MASK));
  1691. }
  1692. if (inst->fonts[fontid] == NULL) {
  1693. /*
  1694. * The font for this contingency does not exist.
  1695. * Typically this means we've been given ATTR_WIDE
  1696. * character and have no wide font. So we display
  1697. * nothing at all; such is life.
  1698. */
  1699. } else if (inst->fontinfo[fontid].is_wide) {
  1700. /*
  1701. * At least one version of gdk_draw_text_wc() has a
  1702. * weird bug whereby it reads `len' elements of the
  1703. * input string, but only draws `len/2'. Hence I'm
  1704. * going to make its input array twice as long as it
  1705. * theoretically needs to be, and pass in twice the
  1706. * actual number of characters. If a fixed gdk actually
  1707. * takes the doubled length seriously, then (a) the
  1708. * array will stand scrutiny up to the full length, (b)
  1709. * the spare elements of the array are full of zeroes
  1710. * which will probably be an empty glyph in the font,
  1711. * and (c) the clip rectangle should prevent it causing
  1712. * trouble anyway.
  1713. */
  1714. gwcs = snewn(len*2+1, GdkWChar);
  1715. memset(gwcs, 0, sizeof(GdkWChar) * (len*2+1));
  1716. /*
  1717. * FIXME: when we have a wide-char equivalent of
  1718. * from_unicode, use it instead of this.
  1719. */
  1720. for (i = 0; i <= len; i++)
  1721. gwcs[i] = wcs[i];
  1722. gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
  1723. x*inst->font_width+inst->cfg.window_border,
  1724. y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
  1725. gwcs, len*2);
  1726. sfree(gwcs);
  1727. } else {
  1728. gcs = snewn(len+1, gchar);
  1729. wc_to_mb(inst->fontinfo[fontid].charset, 0,
  1730. wcs, len, gcs, len, ".", NULL, NULL);
  1731. gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
  1732. x*inst->font_width+inst->cfg.window_border,
  1733. y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
  1734. gcs, len);
  1735. sfree(gcs);
  1736. }
  1737. sfree(wcs);
  1738. }
  1739. if (shadow) {
  1740. gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
  1741. x*inst->font_width+inst->cfg.window_border + inst->cfg.shadowboldoffset,
  1742. y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
  1743. text, len);
  1744. }
  1745. if (attr & ATTR_UNDER) {
  1746. int uheight = inst->fonts[0]->ascent + 1;
  1747. if (uheight >= inst->font_height)
  1748. uheight = inst->font_height - 1;
  1749. gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->cfg.window_border,
  1750. y*inst->font_height + uheight + inst->cfg.window_border,
  1751. (x+len)*widefactor*inst->font_width-1+inst->cfg.window_border,
  1752. y*inst->font_height + uheight + inst->cfg.window_border);
  1753. }
  1754. if (lattr != LATTR_NORM) {
  1755. /*
  1756. * I can't find any plausible StretchBlt equivalent in the
  1757. * X server, so I'm going to do this the slow and painful
  1758. * way. This will involve repeated calls to
  1759. * gdk_draw_pixmap() to stretch the text horizontally. It's
  1760. * O(N^2) in time and O(N) in network bandwidth, but you
  1761. * try thinking of a better way. :-(
  1762. */
  1763. int i;
  1764. for (i = 0; i < len * widefactor * inst->font_width; i++) {
  1765. gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
  1766. x*inst->font_width+inst->cfg.window_border + 2*i,
  1767. y*inst->font_height+inst->cfg.window_border,
  1768. x*inst->font_width+inst->cfg.window_border + 2*i+1,
  1769. y*inst->font_height+inst->cfg.window_border,
  1770. len * widefactor * inst->font_width - i, inst->font_height);
  1771. }
  1772. len *= 2;
  1773. if (lattr != LATTR_WIDE) {
  1774. int dt, db;
  1775. /* Now stretch vertically, in the same way. */
  1776. if (lattr == LATTR_BOT)
  1777. dt = 0, db = 1;
  1778. else
  1779. dt = 1, db = 0;
  1780. for (i = 0; i < inst->font_height; i+=2) {
  1781. gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
  1782. x*inst->font_width+inst->cfg.window_border,
  1783. y*inst->font_height+inst->cfg.window_border+dt*i+db,
  1784. x*inst->font_width+inst->cfg.window_border,
  1785. y*inst->font_height+inst->cfg.window_border+dt*(i+1),
  1786. len * widefactor * inst->font_width, inst->font_height-i-1);
  1787. }
  1788. }
  1789. }
  1790. }
  1791. void do_text(Context ctx, int x, int y, char *text, int len,
  1792. unsigned long attr, int lattr)
  1793. {
  1794. struct draw_ctx *dctx = (struct draw_ctx *)ctx;
  1795. struct gui_data *inst = dctx->inst;
  1796. GdkGC *gc = dctx->gc;
  1797. int widefactor;
  1798. do_text_internal(ctx, x, y, text, len, attr, lattr);
  1799. if (attr & ATTR_WIDE) {
  1800. widefactor = 2;
  1801. } else {
  1802. widefactor = 1;
  1803. }
  1804. if (lattr != LATTR_NORM) {
  1805. x *= 2;
  1806. if (x >= inst->term->cols)
  1807. return;
  1808. if (x + len*2*widefactor > inst->term->cols)
  1809. len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
  1810. len *= 2;
  1811. }
  1812. gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
  1813. x*inst->font_width+inst->cfg.window_border,
  1814. y*inst->font_height+inst->cfg.window_border,
  1815. x*inst->font_width+inst->cfg.window_border,
  1816. y*inst->font_height+inst->cfg.window_border,
  1817. len*widefactor*inst->font_width, inst->font_height);
  1818. }
  1819. void do_cursor(Context ctx, int x, int y, char *text, int len,
  1820. unsigned long attr, int lattr)
  1821. {
  1822. struct draw_ctx *dctx = (struct draw_ctx *)ctx;
  1823. struct gui_data *inst = dctx->inst;
  1824. GdkGC *gc = dctx->gc;
  1825. int active, passive, widefactor;
  1826. if (attr & TATTR_PASCURS) {
  1827. attr &= ~TATTR_PASCURS;
  1828. passive = 1;
  1829. } else
  1830. passive = 0;
  1831. if ((attr & TATTR_ACTCURS) && inst->cfg.cursor_type != 0) {
  1832. attr &= ~TATTR_ACTCURS;
  1833. active = 1;
  1834. } else
  1835. active = 0;
  1836. do_text_internal(ctx, x, y, text, len, attr, lattr);
  1837. if (attr & ATTR_WIDE) {
  1838. widefactor = 2;
  1839. } else {
  1840. widefactor = 1;
  1841. }
  1842. if (lattr != LATTR_NORM) {
  1843. x *= 2;
  1844. if (x >= inst->term->cols)
  1845. return;
  1846. if (x + len*2*widefactor > inst->term->cols)
  1847. len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
  1848. len *= 2;
  1849. }
  1850. if (inst->cfg.cursor_type == 0) {
  1851. /*
  1852. * An active block cursor will already have been done by
  1853. * the above do_text call, so we only need to do anything
  1854. * if it's passive.
  1855. */
  1856. if (passive) {
  1857. gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
  1858. gdk_draw_rectangle(inst->pixmap, gc, 0,
  1859. x*inst->font_width+inst->cfg.window_border,
  1860. y*inst->font_height+inst->cfg.window_border,
  1861. len*widefactor*inst->font_width-1, inst->font_height-1);
  1862. }
  1863. } else {
  1864. int uheight;
  1865. int startx, starty, dx, dy, length, i;
  1866. int char_width;
  1867. if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
  1868. char_width = 2*inst->font_width;
  1869. else
  1870. char_width = inst->font_width;
  1871. if (inst->cfg.cursor_type == 1) {
  1872. uheight = inst->fonts[0]->ascent + 1;
  1873. if (uheight >= inst->font_height)
  1874. uheight = inst->font_height - 1;
  1875. startx = x * inst->font_width + inst->cfg.window_border;
  1876. starty = y * inst->font_height + inst->cfg.window_border + uheight;
  1877. dx = 1;
  1878. dy = 0;
  1879. length = len * widefactor * char_width;
  1880. } else {
  1881. int xadjust = 0;
  1882. if (attr & TATTR_RIGHTCURS)
  1883. xadjust = char_width - 1;
  1884. startx = x * inst->font_width + inst->cfg.window_border + xadjust;
  1885. starty = y * inst->font_height + inst->cfg.window_border;
  1886. dx = 0;
  1887. dy = 1;
  1888. length = inst->font_height;
  1889. }
  1890. gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
  1891. if (passive) {
  1892. for (i = 0; i < length; i++) {
  1893. if (i % 2 == 0) {
  1894. gdk_draw_point(inst->pixmap, gc, startx, starty);
  1895. }
  1896. startx += dx;
  1897. starty += dy;
  1898. }
  1899. } else if (active) {
  1900. gdk_draw_line(inst->pixmap, gc, startx, starty,
  1901. startx + (length-1) * dx, starty + (length-1) * dy);
  1902. } /* else no cursor (e.g., blinked off) */
  1903. }
  1904. gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
  1905. x*inst->font_width+inst->cfg.window_border,
  1906. y*inst->font_height+inst->cfg.window_border,
  1907. x*inst->font_width+inst->cfg.window_border,
  1908. y*inst->font_height+inst->cfg.window_border,
  1909. len*widefactor*inst->font_width, inst->font_height);
  1910. }
  1911. GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
  1912. {
  1913. /*
  1914. * Truly hideous hack: GTK doesn't allow us to set the mouse
  1915. * cursor foreground and background colours unless we've _also_
  1916. * created our own cursor from bitmaps. Therefore, I need to
  1917. * load the `cursor' font and draw glyphs from it on to
  1918. * pixmaps, in order to construct my cursors with the fg and bg
  1919. * I want. This is a gross hack, but it's more self-contained
  1920. * than linking in Xlib to find the X window handle to
  1921. * inst->area and calling XRecolorCursor, and it's more
  1922. * futureproof than hard-coding the shapes as bitmap arrays.
  1923. */
  1924. static GdkFont *cursor_font = NULL;
  1925. GdkPixmap *source, *mask;
  1926. GdkGC *gc;
  1927. GdkColor cfg = { 0, 65535, 65535, 65535 };
  1928. GdkColor cbg = { 0, 0, 0, 0 };
  1929. GdkColor dfg = { 1, 65535, 65535, 65535 };
  1930. GdkColor dbg = { 0, 0, 0, 0 };
  1931. GdkCursor *ret;
  1932. gchar text[2];
  1933. gint lb, rb, wid, asc, desc, w, h, x, y;
  1934. if (cursor_val == -2) {
  1935. gdk_font_unref(cursor_font);
  1936. return NULL;
  1937. }
  1938. if (cursor_val >= 0 && !cursor_font)
  1939. cursor_font = gdk_font_load("cursor");
  1940. /*
  1941. * Get the text extent of the cursor in question. We use the
  1942. * mask character for this, because it's typically slightly
  1943. * bigger than the main character.
  1944. */
  1945. if (cursor_val >= 0) {
  1946. text[1] = '\0';
  1947. text[0] = (char)cursor_val + 1;
  1948. gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
  1949. w = rb-lb; h = asc+desc; x = -lb; y = asc;
  1950. } else {
  1951. w = h = 1;
  1952. x = y = 0;
  1953. }
  1954. source = gdk_pixmap_new(NULL, w, h, 1);
  1955. mask = gdk_pixmap_new(NULL, w, h, 1);
  1956. /*
  1957. * Draw the mask character on the mask pixmap.
  1958. */
  1959. gc = gdk_gc_new(mask);
  1960. gdk_gc_set_foreground(gc, &dbg);
  1961. gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
  1962. if (cursor_val >= 0) {
  1963. text[1] = '\0';
  1964. text[0] = (char)cursor_val + 1;
  1965. gdk_gc_set_foreground(gc, &dfg);
  1966. gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
  1967. }
  1968. gdk_gc_unref(gc);
  1969. /*
  1970. * Draw the main character on the source pixmap.
  1971. */
  1972. gc = gdk_gc_new(source);
  1973. gdk_gc_set_foreground(gc, &dbg);
  1974. gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
  1975. if (cursor_val >= 0) {
  1976. text[1] = '\0';
  1977. text[0] = (char)cursor_val;
  1978. gdk_gc_set_foreground(gc, &dfg);
  1979. gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
  1980. }
  1981. gdk_gc_unref(gc);
  1982. /*
  1983. * Create the cursor.
  1984. */
  1985. ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
  1986. /*
  1987. * Clean up.
  1988. */
  1989. gdk_pixmap_unref(source);
  1990. gdk_pixmap_unref(mask);
  1991. return ret;
  1992. }
  1993. void modalfatalbox(char *p, ...)
  1994. {
  1995. va_list ap;
  1996. fprintf(stderr, "FATAL ERROR: ");
  1997. va_start(ap, p);
  1998. vfprintf(stderr, p, ap);
  1999. va_end(ap);
  2000. fputc('\n', stderr);
  2001. exit(1);
  2002. }
  2003. void cmdline_error(char *p, ...)
  2004. {
  2005. va_list ap;
  2006. fprintf(stderr, "%s: ", appname);
  2007. va_start(ap, p);
  2008. vfprintf(stderr, p, ap);
  2009. va_end(ap);
  2010. fputc('\n', stderr);
  2011. exit(1);
  2012. }
  2013. char *get_x_display(void *frontend)
  2014. {
  2015. return gdk_get_display();
  2016. }
  2017. long get_windowid(void *frontend)
  2018. {
  2019. struct gui_data *inst = (struct gui_data *)frontend;
  2020. return (long)GDK_WINDOW_XWINDOW(inst->area->window);
  2021. }
  2022. static void help(FILE *fp) {
  2023. if(fprintf(fp,
  2024. "pterm option summary:\n"
  2025. "\n"
  2026. " --display DISPLAY Specify X display to use (note '--')\n"
  2027. " -name PREFIX Prefix when looking up resources (default: pterm)\n"
  2028. " -fn FONT Normal text font\n"
  2029. " -fb FONT Bold text font\n"
  2030. " -geometry GEOMETRY Position and size of window (size in characters)\n"
  2031. " -sl LINES Number of lines of scrollback\n"
  2032. " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
  2033. " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
  2034. " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
  2035. " -T TITLE Window title\n"
  2036. " -ut, +ut Do(default) or do not update utmp\n"
  2037. " -ls, +ls Do(default) or do not make shell a login shell\n"
  2038. " -sb, +sb Do(default) or do not display a scrollbar\n"
  2039. " -log PATH Log all output to a file\n"
  2040. " -nethack Map numeric keypad to hjklyubn direction keys\n"
  2041. " -xrm RESOURCE-STRING Set an X resource\n"
  2042. " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
  2043. ) < 0 || fflush(fp) < 0) {
  2044. perror("output error");
  2045. exit(1);
  2046. }
  2047. }
  2048. int do_cmdline(int argc, char **argv, int do_everything,
  2049. struct gui_data *inst, Config *cfg)
  2050. {
  2051. int err = 0;
  2052. char *val;
  2053. /*
  2054. * Macros to make argument handling easier. Note that because
  2055. * they need to call `continue', they cannot be contained in
  2056. * the usual do {...} while (0) wrapper to make them
  2057. * syntactically single statements; hence it is not legal to
  2058. * use one of these macros as an unbraced statement between
  2059. * `if' and `else'.
  2060. */
  2061. #define EXPECTS_ARG { \
  2062. if (--argc <= 0) { \
  2063. err = 1; \
  2064. fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
  2065. continue; \
  2066. } else \
  2067. val = *++argv; \
  2068. }
  2069. #define SECOND_PASS_ONLY { if (!do_everything) continue; }
  2070. while (--argc > 0) {
  2071. char *p = *++argv;
  2072. int ret;
  2073. /*
  2074. * Shameless cheating. Debian requires all X terminal
  2075. * emulators to support `-T title'; but
  2076. * cmdline_process_param will eat -T (it means no-pty) and
  2077. * complain that pterm doesn't support it. So, in pterm
  2078. * only, we convert -T into -title.
  2079. */
  2080. if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
  2081. !strcmp(p, "-T"))
  2082. p = "-title";
  2083. ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
  2084. do_everything ? 1 : -1, cfg);
  2085. if (ret == -2) {
  2086. cmdline_error("option \"%s\" requires an argument", p);
  2087. } else if (ret == 2) {
  2088. --argc, ++argv; /* skip next argument */
  2089. continue;
  2090. } else if (ret == 1) {
  2091. continue;
  2092. }
  2093. if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
  2094. EXPECTS_ARG;
  2095. SECOND_PASS_ONLY;
  2096. strncpy(cfg->font.name, val, sizeof(cfg->font.name));
  2097. cfg->font.name[sizeof(cfg->font.name)-1] = '\0';
  2098. } else if (!strcmp(p, "-fb")) {
  2099. EXPECTS_ARG;
  2100. SECOND_PASS_ONLY;
  2101. strncpy(cfg->boldfont.name, val, sizeof(cfg->boldfont.name));
  2102. cfg->boldfont.name[sizeof(cfg->boldfont.name)-1] = '\0';
  2103. } else if (!strcmp(p, "-fw")) {
  2104. EXPECTS_ARG;
  2105. SECOND_PASS_ONLY;
  2106. strncpy(cfg->widefont.name, val, sizeof(cfg->widefont.name));
  2107. cfg->widefont.name[sizeof(cfg->widefont.name)-1] = '\0';
  2108. } else if (!strcmp(p, "-fwb")) {
  2109. EXPECTS_ARG;
  2110. SECOND_PASS_ONLY;
  2111. strncpy(cfg->wideboldfont.name, val, sizeof(cfg->wideboldfont.name));
  2112. cfg->wideboldfont.name[sizeof(cfg->wideboldfont.name)-1] = '\0';
  2113. } else if (!strcmp(p, "-cs")) {
  2114. EXPECTS_ARG;
  2115. SECOND_PASS_ONLY;
  2116. strncpy(cfg->line_codepage, val, sizeof(cfg->line_codepage));
  2117. cfg->line_codepage[sizeof(cfg->line_codepage)-1] = '\0';
  2118. } else if (!strcmp(p, "-geometry")) {
  2119. int flags, x, y, w, h;
  2120. EXPECTS_ARG;
  2121. SECOND_PASS_ONLY;
  2122. flags = XParseGeometry(val, &x, &y, &w, &h);
  2123. if (flags & WidthValue)
  2124. cfg->width = w;
  2125. if (flags & HeightValue)
  2126. cfg->height = h;
  2127. if (flags & (XValue | YValue)) {
  2128. inst->xpos = x;
  2129. inst->ypos = y;
  2130. inst->gotpos = TRUE;
  2131. inst->gravity = ((flags & XNegative ? 1 : 0) |
  2132. (flags & YNegative ? 2 : 0));
  2133. }
  2134. } else if (!strcmp(p, "-sl")) {
  2135. EXPECTS_ARG;
  2136. SECOND_PASS_ONLY;
  2137. cfg->savelines = atoi(val);
  2138. } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
  2139. !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
  2140. !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
  2141. GdkColor col;
  2142. EXPECTS_ARG;
  2143. SECOND_PASS_ONLY;
  2144. if (!gdk_color_parse(val, &col)) {
  2145. err = 1;
  2146. fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
  2147. appname, val);
  2148. } else {
  2149. int index;
  2150. index = (!strcmp(p, "-fg") ? 0 :
  2151. !strcmp(p, "-bg") ? 2 :
  2152. !strcmp(p, "-bfg") ? 1 :
  2153. !strcmp(p, "-bbg") ? 3 :
  2154. !strcmp(p, "-cfg") ? 4 :
  2155. !strcmp(p, "-cbg") ? 5 : -1);
  2156. assert(index != -1);
  2157. cfg->colours[index][0] = col.red / 256;
  2158. cfg->colours[index][1] = col.green / 256;
  2159. cfg->colours[index][2] = col.blue / 256;
  2160. }
  2161. } else if (use_pty_argv && !strcmp(p, "-e")) {
  2162. /* This option swallows all further arguments. */
  2163. if (!do_everything)
  2164. break;
  2165. if (--argc > 0) {
  2166. int i;
  2167. pty_argv = snewn(argc+1, char *);
  2168. ++argv;
  2169. for (i = 0; i < argc; i++)
  2170. pty_argv[i] = argv[i];
  2171. pty_argv[argc] = NULL;
  2172. break; /* finished command-line processing */
  2173. } else
  2174. err = 1, fprintf(stderr, "%s: -e expects an argument\n",
  2175. appname);
  2176. } else if (!strcmp(p, "-title")) {
  2177. EXPECTS_ARG;
  2178. SECOND_PASS_ONLY;
  2179. strncpy(cfg->wintitle, val, sizeof(cfg->wintitle));
  2180. cfg->wintitle[sizeof(cfg->wintitle)-1] = '\0';
  2181. } else if (!strcmp(p, "-log")) {
  2182. EXPECTS_ARG;
  2183. SECOND_PASS_ONLY;
  2184. strncpy(cfg->logfilename.path, val, sizeof(cfg->logfilename.path));
  2185. cfg->logfilename.path[sizeof(cfg->logfilename.path)-1] = '\0';
  2186. cfg->logtype = LGTYP_DEBUG;
  2187. } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
  2188. SECOND_PASS_ONLY;
  2189. cfg->stamp_utmp = 0;
  2190. } else if (!strcmp(p, "-ut")) {
  2191. SECOND_PASS_ONLY;
  2192. cfg->stamp_utmp = 1;
  2193. } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
  2194. SECOND_PASS_ONLY;
  2195. cfg->login_shell = 0;
  2196. } else if (!strcmp(p, "-ls")) {
  2197. SECOND_PASS_ONLY;
  2198. cfg->login_shell = 1;
  2199. } else if (!strcmp(p, "-nethack")) {
  2200. SECOND_PASS_ONLY;
  2201. cfg->nethack_keypad = 1;
  2202. } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
  2203. SECOND_PASS_ONLY;
  2204. cfg->scrollbar = 0;
  2205. } else if (!strcmp(p, "-sb")) {
  2206. SECOND_PASS_ONLY;
  2207. cfg->scrollbar = 0;
  2208. } else if (!strcmp(p, "-name")) {
  2209. EXPECTS_ARG;
  2210. app_name = val;
  2211. } else if (!strcmp(p, "-xrm")) {
  2212. EXPECTS_ARG;
  2213. provide_xrm_string(val);
  2214. } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
  2215. help(stdout);
  2216. exit(0);
  2217. } else if(p[0] != '-' && (!do_everything ||
  2218. process_nonoption_arg(p, cfg))) {
  2219. /* do nothing */
  2220. } else {
  2221. err = 1;
  2222. fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
  2223. }
  2224. }
  2225. return err;
  2226. }
  2227. static void block_signal(int sig, int block_it) {
  2228. sigset_t ss;
  2229. sigemptyset(&ss);
  2230. sigaddset(&ss, sig);
  2231. if(sigprocmask(block_it ? SIG_BLOCK : SIG_UNBLOCK, &ss, 0) < 0) {
  2232. perror("sigprocmask");
  2233. exit(1);
  2234. }
  2235. }
  2236. /*
  2237. * This function retrieves the character set encoding of a font. It
  2238. * returns the character set without the X11 hack (in case the user
  2239. * asks to use the font's own encoding).
  2240. */
  2241. static int set_font_info(struct gui_data *inst, int fontid)
  2242. {
  2243. GdkFont *font = inst->fonts[fontid];
  2244. XFontStruct *xfs = GDK_FONT_XFONT(font);
  2245. Display *disp = GDK_FONT_XDISPLAY(font);
  2246. Atom charset_registry, charset_encoding;
  2247. unsigned long registry_ret, encoding_ret;
  2248. int retval = CS_NONE;
  2249. charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
  2250. charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
  2251. inst->fontinfo[fontid].charset = CS_NONE;
  2252. inst->fontinfo[fontid].is_wide = 0;
  2253. if (XGetFontProperty(xfs, charset_registry, &registry_ret) &&
  2254. XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
  2255. char *reg, *enc;
  2256. reg = XGetAtomName(disp, (Atom)registry_ret);
  2257. enc = XGetAtomName(disp, (Atom)encoding_ret);
  2258. if (reg && enc) {
  2259. char *encoding = dupcat(reg, "-", enc, NULL);
  2260. retval = inst->fontinfo[fontid].charset =
  2261. charset_from_xenc(encoding);
  2262. /* FIXME: when libcharset supports wide encodings fix this. */
  2263. if (!strcasecmp(encoding, "iso10646-1")) {
  2264. inst->fontinfo[fontid].is_wide = 1;
  2265. retval = CS_UTF8;
  2266. }
  2267. /*
  2268. * Hack for X line-drawing characters: if the primary
  2269. * font is encoded as ISO-8859-anything, and has valid
  2270. * glyphs in the first 32 char positions, it is assumed
  2271. * that those glyphs are the VT100 line-drawing
  2272. * character set.
  2273. *
  2274. * Actually, we'll hack even harder by only checking
  2275. * position 0x19 (vertical line, VT100 linedrawing
  2276. * `x'). Then we can check it easily by seeing if the
  2277. * ascent and descent differ.
  2278. */
  2279. if (inst->fontinfo[fontid].charset == CS_ISO8859_1) {
  2280. int lb, rb, wid, asc, desc;
  2281. gchar text[2];
  2282. text[1] = '\0';
  2283. text[0] = '\x12';
  2284. gdk_string_extents(inst->fonts[fontid], text,
  2285. &lb, &rb, &wid, &asc, &desc);
  2286. if (asc != desc)
  2287. inst->fontinfo[fontid].charset = CS_ISO8859_1_X11;
  2288. }
  2289. sfree(encoding);
  2290. }
  2291. }
  2292. return retval;
  2293. }
  2294. int uxsel_input_add(int fd, int rwx) {
  2295. int flags = 0;
  2296. if (rwx & 1) flags |= GDK_INPUT_READ;
  2297. if (rwx & 2) flags |= GDK_INPUT_WRITE;
  2298. if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
  2299. return gdk_input_add(fd, flags, fd_input_func, NULL);
  2300. }
  2301. void uxsel_input_remove(int id) {
  2302. gdk_input_remove(id);
  2303. }
  2304. char *guess_derived_font_name(GdkFont *font, int bold, int wide)
  2305. {
  2306. XFontStruct *xfs = GDK_FONT_XFONT(font);
  2307. Display *disp = GDK_FONT_XDISPLAY(font);
  2308. Atom fontprop = XInternAtom(disp, "FONT", False);
  2309. unsigned long ret;
  2310. if (XGetFontProperty(xfs, fontprop, &ret)) {
  2311. char *name = XGetAtomName(disp, (Atom)ret);
  2312. if (name && name[0] == '-') {
  2313. char *strings[13];
  2314. char *dupname, *extrafree = NULL, *ret;
  2315. char *p, *q;
  2316. int nstr;
  2317. p = q = dupname = dupstr(name); /* skip initial minus */
  2318. nstr = 0;
  2319. while (*p && nstr < lenof(strings)) {
  2320. if (*p == '-') {
  2321. *p = '\0';
  2322. strings[nstr++] = p+1;
  2323. }
  2324. p++;
  2325. }
  2326. if (nstr < lenof(strings))
  2327. return NULL; /* XLFD was malformed */
  2328. if (bold)
  2329. strings[2] = "bold";
  2330. if (wide) {
  2331. /* 4 is `wideness', which obviously may have changed. */
  2332. /* 5 is additional style, which may be e.g. `ja' or `ko'. */
  2333. strings[4] = strings[5] = "*";
  2334. strings[11] = extrafree = dupprintf("%d", 2*atoi(strings[11]));
  2335. }
  2336. ret = dupcat("-", strings[ 0], "-", strings[ 1], "-", strings[ 2],
  2337. "-", strings[ 3], "-", strings[ 4], "-", strings[ 5],
  2338. "-", strings[ 6], "-", strings[ 7], "-", strings[ 8],
  2339. "-", strings[ 9], "-", strings[10], "-", strings[11],
  2340. "-", strings[12], NULL);
  2341. sfree(extrafree);
  2342. sfree(dupname);
  2343. return ret;
  2344. }
  2345. }
  2346. return NULL;
  2347. }
  2348. void setup_fonts_ucs(struct gui_data *inst)
  2349. {
  2350. int font_charset;
  2351. char *name;
  2352. int guessed;
  2353. if (inst->fonts[0])
  2354. gdk_font_unref(inst->fonts[0]);
  2355. if (inst->fonts[1])
  2356. gdk_font_unref(inst->fonts[1]);
  2357. if (inst->fonts[2])
  2358. gdk_font_unref(inst->fonts[2]);
  2359. if (inst->fonts[3])
  2360. gdk_font_unref(inst->fonts[3]);
  2361. inst->fonts[0] = gdk_font_load(inst->cfg.font.name);
  2362. if (!inst->fonts[0]) {
  2363. fprintf(stderr, "%s: unable to load font \"%s\"\n", appname,
  2364. inst->cfg.font.name);
  2365. exit(1);
  2366. }
  2367. font_charset = set_font_info(inst, 0);
  2368. if (inst->cfg.shadowbold) {
  2369. inst->fonts[1] = NULL;
  2370. } else {
  2371. if (inst->cfg.boldfont.name[0]) {
  2372. name = inst->cfg.boldfont.name;
  2373. guessed = FALSE;
  2374. } else {
  2375. name = guess_derived_font_name(inst->fonts[0], TRUE, FALSE);
  2376. guessed = TRUE;
  2377. }
  2378. inst->fonts[1] = name ? gdk_font_load(name) : NULL;
  2379. if (inst->fonts[1]) {
  2380. set_font_info(inst, 1);
  2381. } else if (!guessed) {
  2382. fprintf(stderr, "%s: unable to load bold font \"%s\"\n", appname,
  2383. inst->cfg.boldfont.name);
  2384. exit(1);
  2385. }
  2386. if (guessed)
  2387. sfree(name);
  2388. }
  2389. if (inst->cfg.widefont.name[0]) {
  2390. name = inst->cfg.widefont.name;
  2391. guessed = FALSE;
  2392. } else {
  2393. name = guess_derived_font_name(inst->fonts[0], FALSE, TRUE);
  2394. guessed = TRUE;
  2395. }
  2396. inst->fonts[2] = name ? gdk_font_load(name) : NULL;
  2397. if (inst->fonts[2]) {
  2398. set_font_info(inst, 2);
  2399. } else if (!guessed) {
  2400. fprintf(stderr, "%s: unable to load wide font \"%s\"\n", appname,
  2401. inst->cfg.widefont.name);
  2402. exit(1);
  2403. }
  2404. if (guessed)
  2405. sfree(name);
  2406. if (inst->cfg.shadowbold) {
  2407. inst->fonts[3] = NULL;
  2408. } else {
  2409. if (inst->cfg.wideboldfont.name[0]) {
  2410. name = inst->cfg.wideboldfont.name;
  2411. guessed = FALSE;
  2412. } else {
  2413. /*
  2414. * Here we have some choices. We can widen the bold font,
  2415. * bolden the wide font, or widen and bolden the standard
  2416. * font. Try them all, in that order!
  2417. */
  2418. if (inst->cfg.widefont.name[0])
  2419. name = guess_derived_font_name(inst->fonts[2], TRUE, FALSE);
  2420. else if (inst->cfg.boldfont.name[0])
  2421. name = guess_derived_font_name(inst->fonts[1], FALSE, TRUE);
  2422. else
  2423. name = guess_derived_font_name(inst->fonts[0], TRUE, TRUE);
  2424. guessed = TRUE;
  2425. }
  2426. inst->fonts[3] = name ? gdk_font_load(name) : NULL;
  2427. if (inst->fonts[3]) {
  2428. set_font_info(inst, 3);
  2429. } else if (!guessed) {
  2430. fprintf(stderr, "%s: unable to load wide/bold font \"%s\"\n", appname,
  2431. inst->cfg.wideboldfont.name);
  2432. exit(1);
  2433. }
  2434. if (guessed)
  2435. sfree(name);
  2436. }
  2437. inst->font_width = gdk_char_width(inst->fonts[0], ' ');
  2438. inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
  2439. inst->direct_to_font = init_ucs(&inst->ucsdata, inst->cfg.line_codepage,
  2440. inst->cfg.utf8_override, font_charset,
  2441. inst->cfg.vtmode);
  2442. }
  2443. void set_geom_hints(struct gui_data *inst)
  2444. {
  2445. GdkGeometry geom;
  2446. geom.min_width = inst->font_width + 2*inst->cfg.window_border;
  2447. geom.min_height = inst->font_height + 2*inst->cfg.window_border;
  2448. geom.max_width = geom.max_height = -1;
  2449. geom.base_width = 2*inst->cfg.window_border;
  2450. geom.base_height = 2*inst->cfg.window_border;
  2451. geom.width_inc = inst->font_width;
  2452. geom.height_inc = inst->font_height;
  2453. geom.min_aspect = geom.max_aspect = 0;
  2454. gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
  2455. GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
  2456. GDK_HINT_RESIZE_INC);
  2457. }
  2458. void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
  2459. {
  2460. struct gui_data *inst = (struct gui_data *)data;
  2461. term_clrsb(inst->term);
  2462. }
  2463. void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
  2464. {
  2465. struct gui_data *inst = (struct gui_data *)data;
  2466. term_pwron(inst->term);
  2467. if (inst->ldisc)
  2468. ldisc_send(inst->ldisc, NULL, 0, 0);
  2469. }
  2470. void copy_all_menuitem(GtkMenuItem *item, gpointer data)
  2471. {
  2472. struct gui_data *inst = (struct gui_data *)data;
  2473. term_copyall(inst->term);
  2474. }
  2475. void special_menuitem(GtkMenuItem *item, gpointer data)
  2476. {
  2477. struct gui_data *inst = (struct gui_data *)data;
  2478. int code = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
  2479. "user-data"));
  2480. if (inst->back)
  2481. inst->back->special(inst->backhandle, code);
  2482. }
  2483. void about_menuitem(GtkMenuItem *item, gpointer data)
  2484. {
  2485. struct gui_data *inst = (struct gui_data *)data;
  2486. about_box(inst->window);
  2487. }
  2488. void event_log_menuitem(GtkMenuItem *item, gpointer data)
  2489. {
  2490. struct gui_data *inst = (struct gui_data *)data;
  2491. showeventlog(inst->eventlogstuff, inst->window);
  2492. }
  2493. void change_settings_menuitem(GtkMenuItem *item, gpointer data)
  2494. {
  2495. /* This maps colour indices in inst->cfg to those used in inst->cols. */
  2496. static const int ww[] = {
  2497. 6, 7, 8, 9, 10, 11, 12, 13,
  2498. 14, 15, 16, 17, 18, 19, 20, 21,
  2499. 0, 1, 2, 3, 4, 5
  2500. };
  2501. struct gui_data *inst = (struct gui_data *)data;
  2502. char *title = dupcat(appname, " Reconfiguration", NULL);
  2503. Config cfg2, oldcfg;
  2504. int i, need_size;
  2505. cfg2 = inst->cfg; /* structure copy */
  2506. if (do_config_box(title, &cfg2, 1)) {
  2507. oldcfg = inst->cfg; /* structure copy */
  2508. inst->cfg = cfg2; /* structure copy */
  2509. /* Pass new config data to the logging module */
  2510. log_reconfig(inst->logctx, &cfg2);
  2511. /*
  2512. * Flush the line discipline's edit buffer in the case
  2513. * where local editing has just been disabled.
  2514. */
  2515. if (inst->ldisc)
  2516. ldisc_send(inst->ldisc, NULL, 0, 0);
  2517. /* Pass new config data to the terminal */
  2518. term_reconfig(inst->term, &cfg2);
  2519. /* Pass new config data to the back end */
  2520. if (inst->back)
  2521. inst->back->reconfig(inst->backhandle, &cfg2);
  2522. /*
  2523. * Just setting inst->cfg is sufficient to cause colour
  2524. * setting changes to appear on the next ESC]R palette
  2525. * reset. But we should also check whether any colour
  2526. * settings have been changed, and revert the ones that
  2527. * have to the new default, on the assumption that the user
  2528. * is most likely to want an immediate update.
  2529. */
  2530. for (i = 0; i < NCOLOURS; i++) {
  2531. if (oldcfg.colours[ww[i]][0] != cfg2.colours[ww[i]][0] ||
  2532. oldcfg.colours[ww[i]][1] != cfg2.colours[ww[i]][1] ||
  2533. oldcfg.colours[ww[i]][2] != cfg2.colours[ww[i]][2]) {
  2534. real_palette_set(inst, i, cfg2.colours[ww[i]][0],
  2535. cfg2.colours[ww[i]][1],
  2536. cfg2.colours[ww[i]][2]);
  2537. /*
  2538. * If the default background has changed, we must
  2539. * repaint the space in between the window border
  2540. * and the text area.
  2541. */
  2542. if (i == 18) {
  2543. set_window_background(inst);
  2544. draw_backing_rect(inst);
  2545. }
  2546. }
  2547. }
  2548. /*
  2549. * If the scrollbar needs to be shown, hidden, or moved
  2550. * from one end to the other of the window, do so now.
  2551. */
  2552. if (oldcfg.scrollbar != cfg2.scrollbar) {
  2553. if (cfg2.scrollbar)
  2554. gtk_widget_show(inst->sbar);
  2555. else
  2556. gtk_widget_hide(inst->sbar);
  2557. }
  2558. if (oldcfg.scrollbar_on_left != cfg2.scrollbar_on_left) {
  2559. gtk_box_reorder_child(inst->hbox, inst->sbar,
  2560. cfg2.scrollbar_on_left ? 0 : 1);
  2561. }
  2562. /*
  2563. * Change the window title, if required.
  2564. */
  2565. if (strcmp(oldcfg.wintitle, cfg2.wintitle))
  2566. set_title(inst, cfg2.wintitle);
  2567. set_window_titles(inst);
  2568. /*
  2569. * Redo the whole tangled fonts and Unicode mess if
  2570. * necessary.
  2571. */
  2572. if (strcmp(oldcfg.font.name, cfg2.font.name) ||
  2573. strcmp(oldcfg.boldfont.name, cfg2.boldfont.name) ||
  2574. strcmp(oldcfg.widefont.name, cfg2.widefont.name) ||
  2575. strcmp(oldcfg.wideboldfont.name, cfg2.wideboldfont.name) ||
  2576. strcmp(oldcfg.line_codepage, cfg2.line_codepage) ||
  2577. oldcfg.vtmode != cfg2.vtmode ||
  2578. oldcfg.shadowbold != cfg2.shadowbold) {
  2579. setup_fonts_ucs(inst);
  2580. need_size = 1;
  2581. } else
  2582. need_size = 0;
  2583. /*
  2584. * Resize the window.
  2585. */
  2586. if (oldcfg.width != cfg2.width || oldcfg.height != cfg2.height ||
  2587. oldcfg.window_border != cfg2.window_border || need_size) {
  2588. set_geom_hints(inst);
  2589. request_resize(inst, cfg2.width, cfg2.height);
  2590. } else {
  2591. /*
  2592. * The above will have caused a call to term_size() for
  2593. * us if it happened. If the user has fiddled with only
  2594. * the scrollback size, the above will not have
  2595. * happened and we will need an explicit term_size()
  2596. * here.
  2597. */
  2598. if (oldcfg.savelines != cfg2.savelines)
  2599. term_size(inst->term, inst->term->rows, inst->term->cols,
  2600. cfg2.savelines);
  2601. }
  2602. term_invalidate(inst->term);
  2603. /*
  2604. * We do an explicit full redraw here to ensure the window
  2605. * border has been redrawn as well as the text area.
  2606. */
  2607. gtk_widget_queue_draw(inst->area);
  2608. }
  2609. sfree(title);
  2610. }
  2611. void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
  2612. {
  2613. /*
  2614. * Re-execing ourself is not an exact science under Unix. I do
  2615. * the best I can by using /proc/self/exe if available and by
  2616. * assuming argv[0] can be found on $PATH if not.
  2617. *
  2618. * Note that we also have to reconstruct the elements of the
  2619. * original argv which gtk swallowed, since the user wants the
  2620. * new session to appear on the same X display as the old one.
  2621. */
  2622. char **args;
  2623. va_list ap;
  2624. int i, n;
  2625. int pid;
  2626. /*
  2627. * Collect the arguments with which to re-exec ourself.
  2628. */
  2629. va_start(ap, fd_to_close);
  2630. n = 2; /* progname and terminating NULL */
  2631. n += inst->ngtkargs;
  2632. while (va_arg(ap, char *) != NULL)
  2633. n++;
  2634. va_end(ap);
  2635. args = snewn(n, char *);
  2636. args[0] = inst->progname;
  2637. args[n-1] = NULL;
  2638. for (i = 0; i < inst->ngtkargs; i++)
  2639. args[i+1] = inst->gtkargvstart[i];
  2640. i++;
  2641. va_start(ap, fd_to_close);
  2642. while ((args[i++] = va_arg(ap, char *)) != NULL);
  2643. va_end(ap);
  2644. assert(i == n);
  2645. /*
  2646. * Do the double fork.
  2647. */
  2648. pid = fork();
  2649. if (pid < 0) {
  2650. perror("fork");
  2651. return;
  2652. }
  2653. if (pid == 0) {
  2654. int pid2 = fork();
  2655. if (pid2 < 0) {
  2656. perror("fork");
  2657. _exit(1);
  2658. } else if (pid2 > 0) {
  2659. /*
  2660. * First child has successfully forked second child. My
  2661. * Work Here Is Done. Note the use of _exit rather than
  2662. * exit: the latter appears to cause destroy messages
  2663. * to be sent to the X server. I suspect gtk uses
  2664. * atexit.
  2665. */
  2666. _exit(0);
  2667. }
  2668. /*
  2669. * If we reach here, we are the second child, so we now
  2670. * actually perform the exec.
  2671. */
  2672. if (fd_to_close >= 0)
  2673. close(fd_to_close);
  2674. execv("/proc/self/exe", args);
  2675. execvp(inst->progname, args);
  2676. perror("exec");
  2677. _exit(127);
  2678. } else {
  2679. int status;
  2680. waitpid(pid, &status, 0);
  2681. }
  2682. }
  2683. void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
  2684. {
  2685. struct gui_data *inst = (struct gui_data *)gdata;
  2686. /*
  2687. * For this feature we must marshal cfg and (possibly) pty_argv
  2688. * into a byte stream, create a pipe, and send this byte stream
  2689. * to the child through the pipe.
  2690. */
  2691. int i, ret, size;
  2692. char *data;
  2693. char option[80];
  2694. int pipefd[2];
  2695. if (pipe(pipefd) < 0) {
  2696. perror("pipe");
  2697. return;
  2698. }
  2699. size = sizeof(inst->cfg);
  2700. if (use_pty_argv && pty_argv) {
  2701. for (i = 0; pty_argv[i]; i++)
  2702. size += strlen(pty_argv[i]) + 1;
  2703. }
  2704. data = snewn(size, char);
  2705. memcpy(data, &inst->cfg, sizeof(inst->cfg));
  2706. if (use_pty_argv && pty_argv) {
  2707. int p = sizeof(inst->cfg);
  2708. for (i = 0; pty_argv[i]; i++) {
  2709. strcpy(data + p, pty_argv[i]);
  2710. p += strlen(pty_argv[i]) + 1;
  2711. }
  2712. assert(p == size);
  2713. }
  2714. sprintf(option, "---[%d,%d]", pipefd[0], size);
  2715. fcntl(pipefd[0], F_SETFD, 0);
  2716. fork_and_exec_self(inst, pipefd[1], option, NULL);
  2717. close(pipefd[0]);
  2718. i = ret = 0;
  2719. while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
  2720. i += ret;
  2721. if (ret < 0)
  2722. perror("write to pipe");
  2723. close(pipefd[1]);
  2724. sfree(data);
  2725. }
  2726. int read_dupsession_data(struct gui_data *inst, Config *cfg, char *arg)
  2727. {
  2728. int fd, i, ret, size;
  2729. char *data;
  2730. if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
  2731. fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
  2732. exit(1);
  2733. }
  2734. data = snewn(size, char);
  2735. i = ret = 0;
  2736. while (i < size && (ret = read(fd, data + i, size - i)) > 0)
  2737. i += ret;
  2738. if (ret < 0) {
  2739. perror("read from pipe");
  2740. exit(1);
  2741. } else if (i < size) {
  2742. fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
  2743. appname);
  2744. exit(1);
  2745. }
  2746. memcpy(cfg, data, sizeof(Config));
  2747. if (use_pty_argv && size > sizeof(Config)) {
  2748. int n = 0;
  2749. i = sizeof(Config);
  2750. while (i < size) {
  2751. while (i < size && data[i]) i++;
  2752. if (i >= size) {
  2753. fprintf(stderr, "%s: malformed Duplicate Session data\n",
  2754. appname);
  2755. exit(1);
  2756. }
  2757. i++;
  2758. n++;
  2759. }
  2760. pty_argv = snewn(n+1, char *);
  2761. pty_argv[n] = NULL;
  2762. n = 0;
  2763. i = sizeof(Config);
  2764. while (i < size) {
  2765. char *p = data + i;
  2766. while (i < size && data[i]) i++;
  2767. assert(i < size);
  2768. i++;
  2769. pty_argv[n++] = dupstr(p);
  2770. }
  2771. }
  2772. return 0;
  2773. }
  2774. void new_session_menuitem(GtkMenuItem *item, gpointer data)
  2775. {
  2776. struct gui_data *inst = (struct gui_data *)data;
  2777. fork_and_exec_self(inst, -1, NULL);
  2778. }
  2779. void restart_session_menuitem(GtkMenuItem *item, gpointer data)
  2780. {
  2781. struct gui_data *inst = (struct gui_data *)data;
  2782. if (!inst->back) {
  2783. logevent(inst, "----- Session restarted -----");
  2784. start_backend(inst);
  2785. inst->exited = FALSE;
  2786. }
  2787. }
  2788. void saved_session_menuitem(GtkMenuItem *item, gpointer data)
  2789. {
  2790. struct gui_data *inst = (struct gui_data *)data;
  2791. char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
  2792. fork_and_exec_self(inst, -1, "-load", str, NULL);
  2793. }
  2794. void saved_session_freedata(GtkMenuItem *item, gpointer data)
  2795. {
  2796. char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
  2797. sfree(str);
  2798. }
  2799. void update_specials_menu(void *frontend)
  2800. {
  2801. struct gui_data *inst = (struct gui_data *)frontend;
  2802. const struct telnet_special *specials;
  2803. if (inst->back)
  2804. specials = inst->back->get_specials(inst->backhandle);
  2805. else
  2806. specials = NULL;
  2807. /* I believe this disposes of submenus too. */
  2808. gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
  2809. (GtkCallback)gtk_widget_destroy, NULL);
  2810. if (specials) {
  2811. int i;
  2812. GtkWidget *menu = inst->specialsmenu;
  2813. /* A lame "stack" for submenus that will do for now. */
  2814. GtkWidget *saved_menu = NULL;
  2815. int nesting = 1;
  2816. for (i = 0; nesting > 0; i++) {
  2817. GtkWidget *menuitem = NULL;
  2818. switch (specials[i].code) {
  2819. case TS_SUBMENU:
  2820. assert (nesting < 2);
  2821. saved_menu = menu; /* XXX lame stacking */
  2822. menu = gtk_menu_new();
  2823. menuitem = gtk_menu_item_new_with_label(specials[i].name);
  2824. gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
  2825. gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
  2826. gtk_widget_show(menuitem);
  2827. menuitem = NULL;
  2828. nesting++;
  2829. break;
  2830. case TS_EXITMENU:
  2831. nesting--;
  2832. if (nesting) {
  2833. menu = saved_menu; /* XXX lame stacking */
  2834. saved_menu = NULL;
  2835. }
  2836. break;
  2837. case TS_SEP:
  2838. menuitem = gtk_menu_item_new();
  2839. break;
  2840. default:
  2841. menuitem = gtk_menu_item_new_with_label(specials[i].name);
  2842. gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
  2843. GINT_TO_POINTER(specials[i].code));
  2844. gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
  2845. GTK_SIGNAL_FUNC(special_menuitem), inst);
  2846. break;
  2847. }
  2848. if (menuitem) {
  2849. gtk_container_add(GTK_CONTAINER(menu), menuitem);
  2850. gtk_widget_show(menuitem);
  2851. }
  2852. }
  2853. gtk_widget_show(inst->specialsitem1);
  2854. gtk_widget_show(inst->specialsitem2);
  2855. } else {
  2856. gtk_widget_hide(inst->specialsitem1);
  2857. gtk_widget_hide(inst->specialsitem2);
  2858. }
  2859. }
  2860. static void start_backend(struct gui_data *inst)
  2861. {
  2862. extern Backend *select_backend(Config *cfg);
  2863. char *realhost;
  2864. const char *error;
  2865. inst->back = select_backend(&inst->cfg);
  2866. error = inst->back->init((void *)inst, &inst->backhandle,
  2867. &inst->cfg, inst->cfg.host, inst->cfg.port,
  2868. &realhost, inst->cfg.tcp_nodelay,
  2869. inst->cfg.tcp_keepalives);
  2870. if (error) {
  2871. char *msg = dupprintf("Unable to open connection to %s:\n%s",
  2872. inst->cfg.host, error);
  2873. inst->exited = TRUE;
  2874. fatal_message_box(inst->window, msg);
  2875. sfree(msg);
  2876. exit(0);
  2877. }
  2878. if (inst->cfg.wintitle[0]) {
  2879. set_title(inst, inst->cfg.wintitle);
  2880. set_icon(inst, inst->cfg.wintitle);
  2881. } else {
  2882. char *title = make_default_wintitle(realhost);
  2883. set_title(inst, title);
  2884. set_icon(inst, title);
  2885. sfree(title);
  2886. }
  2887. inst->back->provide_logctx(inst->backhandle, inst->logctx);
  2888. term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
  2889. inst->ldisc =
  2890. ldisc_create(&inst->cfg, inst->term, inst->back, inst->backhandle,
  2891. inst);
  2892. gtk_widget_hide(inst->restartitem);
  2893. }
  2894. int pt_main(int argc, char **argv)
  2895. {
  2896. extern int cfgbox(Config *cfg);
  2897. struct gui_data *inst;
  2898. /*
  2899. * Create an instance structure and initialise to zeroes
  2900. */
  2901. inst = snew(struct gui_data);
  2902. memset(inst, 0, sizeof(*inst));
  2903. inst->alt_keycode = -1; /* this one needs _not_ to be zero */
  2904. /* defer any child exit handling until we're ready to deal with
  2905. * it */
  2906. block_signal(SIGCHLD, 1);
  2907. inst->progname = argv[0];
  2908. /*
  2909. * Copy the original argv before letting gtk_init fiddle with
  2910. * it. It will be required later.
  2911. */
  2912. {
  2913. int i, oldargc;
  2914. inst->gtkargvstart = snewn(argc-1, char *);
  2915. for (i = 1; i < argc; i++)
  2916. inst->gtkargvstart[i-1] = dupstr(argv[i]);
  2917. oldargc = argc;
  2918. gtk_init(&argc, &argv);
  2919. inst->ngtkargs = oldargc - argc;
  2920. }
  2921. if (argc > 1 && !strncmp(argv[1], "---", 3)) {
  2922. read_dupsession_data(inst, &inst->cfg, argv[1]);
  2923. /* Splatter this argument so it doesn't clutter a ps listing */
  2924. memset(argv[1], 0, strlen(argv[1]));
  2925. } else {
  2926. if (do_cmdline(argc, argv, 0, inst, &inst->cfg))
  2927. exit(1); /* pre-defaults pass to get -class */
  2928. do_defaults(NULL, &inst->cfg);
  2929. if (do_cmdline(argc, argv, 1, inst, &inst->cfg))
  2930. exit(1); /* post-defaults, do everything */
  2931. cmdline_run_saved(&inst->cfg);
  2932. if (!*inst->cfg.host && !cfgbox(&inst->cfg))
  2933. exit(0); /* config box hit Cancel */
  2934. }
  2935. if (!compound_text_atom)
  2936. compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
  2937. if (!utf8_string_atom)
  2938. utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
  2939. setup_fonts_ucs(inst);
  2940. init_cutbuffers();
  2941. inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  2942. /*
  2943. * Set up the colour map.
  2944. */
  2945. palette_reset(inst);
  2946. inst->width = inst->cfg.width;
  2947. inst->height = inst->cfg.height;
  2948. inst->area = gtk_drawing_area_new();
  2949. gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
  2950. inst->font_width * inst->cfg.width + 2*inst->cfg.window_border,
  2951. inst->font_height * inst->cfg.height + 2*inst->cfg.window_border);
  2952. inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
  2953. inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
  2954. inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
  2955. /*
  2956. * We always create the scrollbar; it remains invisible if
  2957. * unwanted, so we can pop it up quickly if it suddenly becomes
  2958. * desirable.
  2959. */
  2960. if (inst->cfg.scrollbar_on_left)
  2961. gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
  2962. gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
  2963. if (!inst->cfg.scrollbar_on_left)
  2964. gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
  2965. gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
  2966. set_geom_hints(inst);
  2967. gtk_widget_show(inst->area);
  2968. if (inst->cfg.scrollbar)
  2969. gtk_widget_show(inst->sbar);
  2970. else
  2971. gtk_widget_hide(inst->sbar);
  2972. gtk_widget_show(GTK_WIDGET(inst->hbox));
  2973. if (inst->gotpos) {
  2974. int x = inst->xpos, y = inst->ypos;
  2975. GtkRequisition req;
  2976. gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
  2977. if (inst->gravity & 1) x += gdk_screen_width() - req.width;
  2978. if (inst->gravity & 2) y += gdk_screen_height() - req.height;
  2979. gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
  2980. gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
  2981. }
  2982. gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
  2983. GTK_SIGNAL_FUNC(destroy), inst);
  2984. gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
  2985. GTK_SIGNAL_FUNC(delete_window), inst);
  2986. gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
  2987. GTK_SIGNAL_FUNC(key_event), inst);
  2988. gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
  2989. GTK_SIGNAL_FUNC(key_event), inst);
  2990. gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
  2991. GTK_SIGNAL_FUNC(focus_event), inst);
  2992. gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
  2993. GTK_SIGNAL_FUNC(focus_event), inst);
  2994. gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
  2995. GTK_SIGNAL_FUNC(configure_area), inst);
  2996. gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
  2997. GTK_SIGNAL_FUNC(expose_area), inst);
  2998. gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
  2999. GTK_SIGNAL_FUNC(button_event), inst);
  3000. gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
  3001. GTK_SIGNAL_FUNC(button_event), inst);
  3002. gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
  3003. GTK_SIGNAL_FUNC(motion_event), inst);
  3004. gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
  3005. GTK_SIGNAL_FUNC(selection_received), inst);
  3006. gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
  3007. GTK_SIGNAL_FUNC(selection_get), inst);
  3008. gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
  3009. GTK_SIGNAL_FUNC(selection_clear), inst);
  3010. if (inst->cfg.scrollbar)
  3011. gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
  3012. GTK_SIGNAL_FUNC(scrollbar_moved), inst);
  3013. gtk_timeout_add(20, timer_func, inst);
  3014. gtk_widget_add_events(GTK_WIDGET(inst->area),
  3015. GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
  3016. GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
  3017. GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
  3018. gtk_widget_show(inst->window);
  3019. set_window_background(inst);
  3020. /*
  3021. * Set up the Ctrl+rightclick context menu.
  3022. */
  3023. {
  3024. GtkWidget *menuitem;
  3025. char *s;
  3026. extern const int use_event_log, new_session, saved_sessions;
  3027. inst->menu = gtk_menu_new();
  3028. #define MKMENUITEM(title, func) do { \
  3029. menuitem = title ? gtk_menu_item_new_with_label(title) : \
  3030. gtk_menu_item_new(); \
  3031. gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
  3032. gtk_widget_show(menuitem); \
  3033. if (func != NULL) \
  3034. gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
  3035. GTK_SIGNAL_FUNC(func), inst); \
  3036. } while (0)
  3037. if (new_session)
  3038. MKMENUITEM("New Session", new_session_menuitem);
  3039. MKMENUITEM("Restart Session", restart_session_menuitem);
  3040. inst->restartitem = menuitem;
  3041. gtk_widget_hide(inst->restartitem);
  3042. MKMENUITEM("Duplicate Session", dup_session_menuitem);
  3043. if (saved_sessions) {
  3044. struct sesslist sesslist;
  3045. int i;
  3046. inst->sessionsmenu = gtk_menu_new();
  3047. get_sesslist(&sesslist, TRUE);
  3048. for (i = 1; i < sesslist.nsessions; i++) {
  3049. menuitem = gtk_menu_item_new_with_label(sesslist.sessions[i]);
  3050. gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
  3051. gtk_widget_show(menuitem);
  3052. gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
  3053. dupstr(sesslist.sessions[i]));
  3054. gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
  3055. GTK_SIGNAL_FUNC(saved_session_menuitem),
  3056. inst);
  3057. gtk_signal_connect(GTK_OBJECT(menuitem), "destroy",
  3058. GTK_SIGNAL_FUNC(saved_session_freedata),
  3059. inst);
  3060. }
  3061. get_sesslist(&sesslist, FALSE);
  3062. MKMENUITEM("Saved Sessions", NULL);
  3063. gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
  3064. inst->sessionsmenu);
  3065. }
  3066. MKMENUITEM(NULL, NULL);
  3067. MKMENUITEM("Change Settings", change_settings_menuitem);
  3068. MKMENUITEM(NULL, NULL);
  3069. if (use_event_log)
  3070. MKMENUITEM("Event Log", event_log_menuitem);
  3071. MKMENUITEM("Special Commands", NULL);
  3072. inst->specialsmenu = gtk_menu_new();
  3073. gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
  3074. inst->specialsitem1 = menuitem;
  3075. MKMENUITEM(NULL, NULL);
  3076. inst->specialsitem2 = menuitem;
  3077. gtk_widget_hide(inst->specialsitem1);
  3078. gtk_widget_hide(inst->specialsitem2);
  3079. MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
  3080. MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
  3081. MKMENUITEM("Copy All", copy_all_menuitem);
  3082. MKMENUITEM(NULL, NULL);
  3083. s = dupcat("About ", appname, NULL);
  3084. MKMENUITEM(s, about_menuitem);
  3085. sfree(s);
  3086. #undef MKMENUITEM
  3087. }
  3088. inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
  3089. inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
  3090. inst->blankcursor = make_mouse_ptr(inst, -1);
  3091. make_mouse_ptr(inst, -2); /* clean up cursor font */
  3092. inst->currcursor = inst->textcursor;
  3093. show_mouseptr(inst, 1);
  3094. inst->eventlogstuff = eventlogstuff_new();
  3095. inst->term = term_init(&inst->cfg, &inst->ucsdata, inst);
  3096. inst->logctx = log_init(inst, &inst->cfg);
  3097. term_provide_logctx(inst->term, inst->logctx);
  3098. uxsel_init();
  3099. term_size(inst->term, inst->cfg.height, inst->cfg.width, inst->cfg.savelines);
  3100. start_backend(inst);
  3101. ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
  3102. /* now we're reday to deal with the child exit handler being
  3103. * called */
  3104. block_signal(SIGCHLD, 0);
  3105. /*
  3106. * Block SIGPIPE: if we attempt Duplicate Session or similar
  3107. * and it falls over in some way, we certainly don't want
  3108. * SIGPIPE terminating the main pterm/PuTTY. Note that we do
  3109. * this _after_ (at least pterm) forks off its child process,
  3110. * since the child wants SIGPIPE handled in the usual way.
  3111. */
  3112. block_signal(SIGPIPE, 1);
  3113. inst->exited = FALSE;
  3114. gtk_main();
  3115. return 0;
  3116. }