st.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853
  1. /* See LICENSE for licence details. */
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <limits.h>
  6. #include <locale.h>
  7. #include <pwd.h>
  8. #include <stdarg.h>
  9. #include <stdbool.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <signal.h>
  14. #include <stdint.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/select.h>
  17. #include <sys/stat.h>
  18. #include <sys/time.h>
  19. #include <sys/types.h>
  20. #include <sys/wait.h>
  21. #include <time.h>
  22. #include <unistd.h>
  23. #include <libgen.h>
  24. #include <X11/Xatom.h>
  25. #include <X11/Xlib.h>
  26. #include <X11/Xutil.h>
  27. #include <X11/cursorfont.h>
  28. #include <X11/keysym.h>
  29. #include <X11/Xft/Xft.h>
  30. #include <fontconfig/fontconfig.h>
  31. #include <wchar.h>
  32. #include "arg.h"
  33. char *argv0;
  34. #define Glyph Glyph_
  35. #define Font Font_
  36. #define Draw XftDraw *
  37. #define Colour XftColor
  38. #define Colourmap Colormap
  39. #define Rectangle XRectangle
  40. #if defined(__linux)
  41. #include <pty.h>
  42. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  43. #include <util.h>
  44. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  45. #include <libutil.h>
  46. #endif
  47. /* XEMBED messages */
  48. #define XEMBED_FOCUS_IN 4
  49. #define XEMBED_FOCUS_OUT 5
  50. /* Arbitrary sizes */
  51. #define UTF_SIZ 4
  52. #define ESC_BUF_SIZ (128*UTF_SIZ)
  53. #define ESC_ARG_SIZ 16
  54. #define STR_BUF_SIZ ESC_BUF_SIZ
  55. #define STR_ARG_SIZ ESC_ARG_SIZ
  56. #define DRAW_BUF_SIZ 20*1024
  57. #define XK_ANY_MOD UINT_MAX
  58. #define XK_NO_MOD 0
  59. #define XK_SWITCH_MOD (1<<13)
  60. #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
  61. /* macros */
  62. #define SERRNO strerror(errno)
  63. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  64. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  65. #define LEN(a) (sizeof(a) / sizeof(a[0]))
  66. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  67. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  68. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  69. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
  70. #define IS_SET(flag) ((term.mode & (flag)) != 0)
  71. #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
  72. #define CEIL(x) (((x) != (int) (x)) ? (x) + 1 : (x))
  73. #define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
  74. #define IS_TRUECOL(x) (1 << 24 & (x))
  75. #define TRUERED(x) (((x) & 0xff0000) >> 8)
  76. #define TRUEGREEN(x) (((x) & 0xff00))
  77. #define TRUEBLUE(x) (((x) & 0xff) << 8)
  78. #define VT102ID "\033[?6c"
  79. enum glyph_attribute {
  80. ATTR_NULL = 0,
  81. ATTR_REVERSE = 1,
  82. ATTR_UNDERLINE = 2,
  83. ATTR_BOLD = 4,
  84. ATTR_GFX = 8,
  85. ATTR_ITALIC = 16,
  86. ATTR_BLINK = 32,
  87. ATTR_WRAP = 64,
  88. ATTR_WIDE = 128,
  89. ATTR_WDUMMY = 256,
  90. };
  91. enum cursor_movement {
  92. CURSOR_SAVE,
  93. CURSOR_LOAD
  94. };
  95. enum cursor_state {
  96. CURSOR_DEFAULT = 0,
  97. CURSOR_WRAPNEXT = 1,
  98. CURSOR_ORIGIN = 2
  99. };
  100. enum term_mode {
  101. MODE_WRAP = 1,
  102. MODE_INSERT = 2,
  103. MODE_APPKEYPAD = 4,
  104. MODE_ALTSCREEN = 8,
  105. MODE_CRLF = 16,
  106. MODE_MOUSEBTN = 32,
  107. MODE_MOUSEMOTION = 64,
  108. MODE_REVERSE = 128,
  109. MODE_KBDLOCK = 256,
  110. MODE_HIDE = 512,
  111. MODE_ECHO = 1024,
  112. MODE_APPCURSOR = 2048,
  113. MODE_MOUSESGR = 4096,
  114. MODE_8BIT = 8192,
  115. MODE_BLINK = 16384,
  116. MODE_FBLINK = 32768,
  117. MODE_FOCUS = 65536,
  118. MODE_MOUSEX10 = 131072,
  119. MODE_MOUSEMANY = 262144,
  120. MODE_BRCKTPASTE = 524288,
  121. MODE_MOUSE = MODE_MOUSEBTN|MODE_MOUSEMOTION|MODE_MOUSEX10\
  122. |MODE_MOUSEMANY,
  123. };
  124. enum charset {
  125. CS_GRAPHIC0,
  126. CS_GRAPHIC1,
  127. CS_UK,
  128. CS_USA,
  129. CS_MULTI,
  130. CS_GER,
  131. CS_FIN
  132. };
  133. enum escape_state {
  134. ESC_START = 1,
  135. ESC_CSI = 2,
  136. ESC_STR = 4, /* DSC, OSC, PM, APC */
  137. ESC_ALTCHARSET = 8,
  138. ESC_STR_END = 16, /* a final string was encountered */
  139. ESC_TEST = 32, /* Enter in test mode */
  140. };
  141. enum window_state {
  142. WIN_VISIBLE = 1,
  143. WIN_REDRAW = 2,
  144. WIN_FOCUSED = 4
  145. };
  146. enum selection_type {
  147. SEL_REGULAR = 1,
  148. SEL_RECTANGULAR = 2
  149. };
  150. enum selection_snap {
  151. SNAP_WORD = 1,
  152. SNAP_LINE = 2
  153. };
  154. typedef unsigned char uchar;
  155. typedef unsigned int uint;
  156. typedef unsigned long ulong;
  157. typedef unsigned short ushort;
  158. typedef struct {
  159. char c[UTF_SIZ]; /* character code */
  160. ushort mode; /* attribute flags */
  161. uint32_t fg; /* foreground */
  162. uint32_t bg; /* background */
  163. } Glyph;
  164. typedef Glyph *Line;
  165. typedef struct {
  166. Glyph attr; /* current char attributes */
  167. int x;
  168. int y;
  169. char state;
  170. } TCursor;
  171. /* CSI Escape sequence structs */
  172. /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
  173. typedef struct {
  174. char buf[ESC_BUF_SIZ]; /* raw string */
  175. int len; /* raw string length */
  176. char priv;
  177. int arg[ESC_ARG_SIZ];
  178. int narg; /* nb of args */
  179. char mode;
  180. } CSIEscape;
  181. /* STR Escape sequence structs */
  182. /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
  183. typedef struct {
  184. char type; /* ESC type ... */
  185. char buf[STR_BUF_SIZ]; /* raw string */
  186. int len; /* raw string length */
  187. char *args[STR_ARG_SIZ];
  188. int narg; /* nb of args */
  189. } STREscape;
  190. /* Internal representation of the screen */
  191. typedef struct {
  192. int row; /* nb row */
  193. int col; /* nb col */
  194. Line *line; /* screen */
  195. Line *alt; /* alternate screen */
  196. bool *dirty; /* dirtyness of lines */
  197. TCursor c; /* cursor */
  198. int top; /* top scroll limit */
  199. int bot; /* bottom scroll limit */
  200. int mode; /* terminal mode flags */
  201. int esc; /* escape state flags */
  202. char trantbl[4]; /* charset table translation */
  203. int charset; /* current charset */
  204. int icharset; /* selected charset for sequence */
  205. bool numlock; /* lock numbers in keyboard */
  206. bool *tabs;
  207. } Term;
  208. /* Purely graphic info */
  209. typedef struct {
  210. Display *dpy;
  211. Colourmap cmap;
  212. Window win;
  213. Drawable buf;
  214. Atom xembed, wmdeletewin, netwmname, netwmpid;
  215. XIM xim;
  216. XIC xic;
  217. Draw draw;
  218. Visual *vis;
  219. XSetWindowAttributes attrs;
  220. int scr;
  221. bool isfixed; /* is fixed geometry? */
  222. int fx, fy, fw, fh; /* fixed geometry */
  223. int tw, th; /* tty width and height */
  224. int w, h; /* window width and height */
  225. int ch; /* char height */
  226. int cw; /* char width */
  227. char state; /* focus, redraw, visible */
  228. } XWindow;
  229. typedef struct {
  230. uint b;
  231. uint mask;
  232. char *s;
  233. } Mousekey;
  234. typedef struct {
  235. KeySym k;
  236. uint mask;
  237. char *s;
  238. /* three valued logic variables: 0 indifferent, 1 on, -1 off */
  239. signed char appkey; /* application keypad */
  240. signed char appcursor; /* application cursor */
  241. signed char crlf; /* crlf mode */
  242. } Key;
  243. typedef struct {
  244. int mode;
  245. int type;
  246. int snap;
  247. /*
  248. * Selection variables:
  249. * nb – normalized coordinates of the beginning of the selection
  250. * ne – normalized coordinates of the end of the selection
  251. * ob – original coordinates of the beginning of the selection
  252. * oe – original coordinates of the end of the selection
  253. */
  254. struct {
  255. int x, y;
  256. } nb, ne, ob, oe;
  257. char *clip;
  258. Atom xtarget;
  259. bool alt;
  260. struct timeval tclick1;
  261. struct timeval tclick2;
  262. } Selection;
  263. typedef union {
  264. int i;
  265. unsigned int ui;
  266. float f;
  267. const void *v;
  268. } Arg;
  269. typedef struct {
  270. unsigned int mod;
  271. KeySym keysym;
  272. void (*func)(const Arg *);
  273. const Arg arg;
  274. } Shortcut;
  275. /* function definitions used in config.h */
  276. static void clippaste(const Arg *);
  277. static void numlock(const Arg *);
  278. static void selpaste(const Arg *);
  279. static void xzoom(const Arg *);
  280. /* Config.h for applying patches and the configuration. */
  281. #include "config.h"
  282. /* Font structure */
  283. typedef struct {
  284. int height;
  285. int width;
  286. int ascent;
  287. int descent;
  288. short lbearing;
  289. short rbearing;
  290. XftFont *match;
  291. FcFontSet *set;
  292. FcPattern *pattern;
  293. } Font;
  294. /* Drawing Context */
  295. typedef struct {
  296. Colour col[LEN(colorname) < 256 ? 256 : LEN(colorname)];
  297. Font font, bfont, ifont, ibfont;
  298. GC gc;
  299. } DC;
  300. static void die(const char *, ...);
  301. static void draw(void);
  302. static void redraw(int);
  303. static void drawregion(int, int, int, int);
  304. static void execsh(void);
  305. static void sigchld(int);
  306. static void run(void);
  307. static void csidump(void);
  308. static void csihandle(void);
  309. static void csiparse(void);
  310. static void csireset(void);
  311. static void strdump(void);
  312. static void strhandle(void);
  313. static void strparse(void);
  314. static void strreset(void);
  315. static int tattrset(int);
  316. static void tclearregion(int, int, int, int);
  317. static void tcursor(int);
  318. static void tdeletechar(int);
  319. static void tdeleteline(int);
  320. static void tinsertblank(int);
  321. static void tinsertblankline(int);
  322. static void tmoveto(int, int);
  323. static void tmoveato(int x, int y);
  324. static void tnew(int, int);
  325. static void tnewline(int);
  326. static void tputtab(bool);
  327. static void tputc(char *, int);
  328. static void treset(void);
  329. static int tresize(int, int);
  330. static void tscrollup(int, int);
  331. static void tscrolldown(int, int);
  332. static void tsetattr(int*, int);
  333. static void tsetchar(char *, Glyph *, int, int);
  334. static void tsetscroll(int, int);
  335. static void tswapscreen(void);
  336. static void tsetdirt(int, int);
  337. static void tsetdirtattr(int);
  338. static void tsetmode(bool, bool, int *, int);
  339. static void tfulldirt(void);
  340. static void techo(char *, int);
  341. static int32_t tdefcolor(int *, int *, int);
  342. static void tselcs(void);
  343. static void tdeftran(char);
  344. static inline bool match(uint, uint);
  345. static void ttynew(void);
  346. static void ttyread(void);
  347. static void ttyresize(void);
  348. static void ttysend(char *, size_t);
  349. static void ttywrite(const char *, size_t);
  350. static void xdraws(char *, Glyph, int, int, int, int);
  351. static void xhints(void);
  352. static void xclear(int, int, int, int);
  353. static void xdrawcursor(void);
  354. static void xinit(void);
  355. static void xloadcols(void);
  356. static int xsetcolorname(int, const char *);
  357. static int xloadfont(Font *, FcPattern *);
  358. static void xloadfonts(char *, double);
  359. static int xloadfontset(Font *);
  360. static void xsettitle(char *);
  361. static void xresettitle(void);
  362. static void xsetpointermotion(int);
  363. static void xseturgency(int);
  364. static void xsetsel(char*);
  365. static void xtermclear(int, int, int, int);
  366. static void xunloadfont(Font *f);
  367. static void xunloadfonts(void);
  368. static void xresize(int, int);
  369. static void expose(XEvent *);
  370. static void visibility(XEvent *);
  371. static void unmap(XEvent *);
  372. static char *kmap(KeySym, uint);
  373. static void kpress(XEvent *);
  374. static void cmessage(XEvent *);
  375. static void cresize(int, int);
  376. static void resize(XEvent *);
  377. static void focus(XEvent *);
  378. static void brelease(XEvent *);
  379. static void bpress(XEvent *);
  380. static void bmotion(XEvent *);
  381. static void selnotify(XEvent *);
  382. static void selclear(XEvent *);
  383. static void selrequest(XEvent *);
  384. static void selinit(void);
  385. static void selsort(void);
  386. static inline bool selected(int, int);
  387. static void selcopy(void);
  388. static void selscroll(int, int);
  389. static void selsnap(int, int *, int *, int);
  390. static int utf8decode(char *, long *);
  391. static int utf8encode(long *, char *);
  392. static int utf8size(char *);
  393. static int isfullutf8(char *, int);
  394. static ssize_t xwrite(int, char *, size_t);
  395. static void *xmalloc(size_t);
  396. static void *xrealloc(void *, size_t);
  397. static char *xstrdup(char *s);
  398. static void (*handler[LASTEvent])(XEvent *) = {
  399. [KeyPress] = kpress,
  400. [ClientMessage] = cmessage,
  401. [ConfigureNotify] = resize,
  402. [VisibilityNotify] = visibility,
  403. [UnmapNotify] = unmap,
  404. [Expose] = expose,
  405. [FocusIn] = focus,
  406. [FocusOut] = focus,
  407. [MotionNotify] = bmotion,
  408. [ButtonPress] = bpress,
  409. [ButtonRelease] = brelease,
  410. [SelectionClear] = selclear,
  411. [SelectionNotify] = selnotify,
  412. [SelectionRequest] = selrequest,
  413. };
  414. /* Globals */
  415. static DC dc;
  416. static XWindow xw;
  417. static Term term;
  418. static CSIEscape csiescseq;
  419. static STREscape strescseq;
  420. static int cmdfd;
  421. static pid_t pid;
  422. static Selection sel;
  423. static int iofd = -1;
  424. static char **opt_cmd = NULL;
  425. static char *opt_io = NULL;
  426. static char *opt_title = NULL;
  427. static char *opt_embed = NULL;
  428. static char *opt_class = NULL;
  429. static char *opt_font = NULL;
  430. static int oldbutton = 3; /* button event on startup: 3 = release */
  431. static char *usedfont = NULL;
  432. static double usedfontsize = 0;
  433. /* Font Ring Cache */
  434. enum {
  435. FRC_NORMAL,
  436. FRC_ITALIC,
  437. FRC_BOLD,
  438. FRC_ITALICBOLD
  439. };
  440. typedef struct {
  441. XftFont *font;
  442. int flags;
  443. } Fontcache;
  444. /* Fontcache is an array now. A new font will be appended to the array. */
  445. static Fontcache frc[16];
  446. static int frclen = 0;
  447. ssize_t
  448. xwrite(int fd, char *s, size_t len) {
  449. size_t aux = len;
  450. while(len > 0) {
  451. ssize_t r = write(fd, s, len);
  452. if(r < 0)
  453. return r;
  454. len -= r;
  455. s += r;
  456. }
  457. return aux;
  458. }
  459. void *
  460. xmalloc(size_t len) {
  461. void *p = malloc(len);
  462. if(!p)
  463. die("Out of memory\n");
  464. return p;
  465. }
  466. void *
  467. xrealloc(void *p, size_t len) {
  468. if((p = realloc(p, len)) == NULL)
  469. die("Out of memory\n");
  470. return p;
  471. }
  472. char *
  473. xstrdup(char *s) {
  474. char *p = strdup(s);
  475. if (!p)
  476. die("Out of memory\n");
  477. return p;
  478. }
  479. int
  480. utf8decode(char *s, long *u) {
  481. uchar c;
  482. int i, n, rtn;
  483. rtn = 1;
  484. c = *s;
  485. if(~c & 0x80) { /* 0xxxxxxx */
  486. *u = c;
  487. return rtn;
  488. } else if((c & 0xE0) == 0xC0) { /* 110xxxxx */
  489. *u = c & 0x1F;
  490. n = 1;
  491. } else if((c & 0xF0) == 0xE0) { /* 1110xxxx */
  492. *u = c & 0x0F;
  493. n = 2;
  494. } else if((c & 0xF8) == 0xF0) { /* 11110xxx */
  495. *u = c & 0x07;
  496. n = 3;
  497. } else {
  498. goto invalid;
  499. }
  500. for(i = n, ++s; i > 0; --i, ++rtn, ++s) {
  501. c = *s;
  502. if((c & 0xC0) != 0x80) /* 10xxxxxx */
  503. goto invalid;
  504. *u <<= 6;
  505. *u |= c & 0x3F;
  506. }
  507. if((n == 1 && *u < 0x80) ||
  508. (n == 2 && *u < 0x800) ||
  509. (n == 3 && *u < 0x10000) ||
  510. (*u >= 0xD800 && *u <= 0xDFFF)) {
  511. goto invalid;
  512. }
  513. return rtn;
  514. invalid:
  515. *u = 0xFFFD;
  516. return rtn;
  517. }
  518. int
  519. utf8encode(long *u, char *s) {
  520. uchar *sp;
  521. ulong uc;
  522. int i, n;
  523. sp = (uchar *)s;
  524. uc = *u;
  525. if(uc < 0x80) {
  526. *sp = uc; /* 0xxxxxxx */
  527. return 1;
  528. } else if(*u < 0x800) {
  529. *sp = (uc >> 6) | 0xC0; /* 110xxxxx */
  530. n = 1;
  531. } else if(uc < 0x10000) {
  532. *sp = (uc >> 12) | 0xE0; /* 1110xxxx */
  533. n = 2;
  534. } else if(uc <= 0x10FFFF) {
  535. *sp = (uc >> 18) | 0xF0; /* 11110xxx */
  536. n = 3;
  537. } else {
  538. goto invalid;
  539. }
  540. for(i=n,++sp; i>0; --i,++sp)
  541. *sp = ((uc >> 6*(i-1)) & 0x3F) | 0x80; /* 10xxxxxx */
  542. return n+1;
  543. invalid:
  544. /* U+FFFD */
  545. *s++ = '\xEF';
  546. *s++ = '\xBF';
  547. *s = '\xBD';
  548. return 3;
  549. }
  550. /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
  551. UTF-8 otherwise return 0 */
  552. int
  553. isfullutf8(char *s, int b) {
  554. uchar *c1, *c2, *c3;
  555. c1 = (uchar *)s;
  556. c2 = (uchar *)++s;
  557. c3 = (uchar *)++s;
  558. if(b < 1) {
  559. return 0;
  560. } else if((*c1 & 0xE0) == 0xC0 && b == 1) {
  561. return 0;
  562. } else if((*c1 & 0xF0) == 0xE0 &&
  563. ((b == 1) ||
  564. ((b == 2) && (*c2 & 0xC0) == 0x80))) {
  565. return 0;
  566. } else if((*c1 & 0xF8) == 0xF0 &&
  567. ((b == 1) ||
  568. ((b == 2) && (*c2 & 0xC0) == 0x80) ||
  569. ((b == 3) && (*c2 & 0xC0) == 0x80 && (*c3 & 0xC0) == 0x80))) {
  570. return 0;
  571. } else {
  572. return 1;
  573. }
  574. }
  575. int
  576. utf8size(char *s) {
  577. uchar c = *s;
  578. if(~c & 0x80) {
  579. return 1;
  580. } else if((c & 0xE0) == 0xC0) {
  581. return 2;
  582. } else if((c & 0xF0) == 0xE0) {
  583. return 3;
  584. } else {
  585. return 4;
  586. }
  587. }
  588. static void
  589. selinit(void) {
  590. memset(&sel.tclick1, 0, sizeof(sel.tclick1));
  591. memset(&sel.tclick2, 0, sizeof(sel.tclick2));
  592. sel.mode = 0;
  593. sel.ob.x = -1;
  594. sel.clip = NULL;
  595. sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
  596. if(sel.xtarget == None)
  597. sel.xtarget = XA_STRING;
  598. }
  599. static int
  600. x2col(int x) {
  601. x -= borderpx;
  602. x /= xw.cw;
  603. return LIMIT(x, 0, term.col-1);
  604. }
  605. static int
  606. y2row(int y) {
  607. y -= borderpx;
  608. y /= xw.ch;
  609. return LIMIT(y, 0, term.row-1);
  610. }
  611. static void
  612. selsort(void) {
  613. if(sel.ob.y == sel.oe.y) {
  614. sel.nb.x = MIN(sel.ob.x, sel.oe.x);
  615. sel.ne.x = MAX(sel.ob.x, sel.oe.x);
  616. } else {
  617. sel.nb.x = sel.ob.y < sel.oe.y ? sel.ob.x : sel.oe.x;
  618. sel.ne.x = sel.ob.y < sel.oe.y ? sel.oe.x : sel.ob.x;
  619. }
  620. sel.nb.y = MIN(sel.ob.y, sel.oe.y);
  621. sel.ne.y = MAX(sel.ob.y, sel.oe.y);
  622. }
  623. static inline bool
  624. selected(int x, int y) {
  625. if(sel.ne.y == y && sel.nb.y == y)
  626. return BETWEEN(x, sel.nb.x, sel.ne.x);
  627. if(sel.type == SEL_RECTANGULAR) {
  628. return ((sel.nb.y <= y && y <= sel.ne.y)
  629. && (sel.nb.x <= x && x <= sel.ne.x));
  630. }
  631. return ((sel.nb.y < y && y < sel.ne.y)
  632. || (y == sel.ne.y && x <= sel.ne.x))
  633. || (y == sel.nb.y && x >= sel.nb.x
  634. && (x <= sel.ne.x || sel.nb.y != sel.ne.y));
  635. }
  636. void
  637. selsnap(int mode, int *x, int *y, int direction) {
  638. int i;
  639. switch(mode) {
  640. case SNAP_WORD:
  641. /*
  642. * Snap around if the word wraps around at the end or
  643. * beginning of a line.
  644. */
  645. for(;;) {
  646. if(direction < 0 && *x <= 0) {
  647. if(*y > 0 && term.line[*y - 1][term.col-1].mode
  648. & ATTR_WRAP) {
  649. *y -= 1;
  650. *x = term.col-1;
  651. } else {
  652. break;
  653. }
  654. }
  655. if(direction > 0 && *x >= term.col-1) {
  656. if(*y < term.row-1 && term.line[*y][*x].mode
  657. & ATTR_WRAP) {
  658. *y += 1;
  659. *x = 0;
  660. } else {
  661. break;
  662. }
  663. }
  664. if(term.line[*y][*x+direction].mode & ATTR_WDUMMY) {
  665. *x += direction;
  666. continue;
  667. }
  668. if(strchr(worddelimiters,
  669. term.line[*y][*x+direction].c[0])) {
  670. break;
  671. }
  672. *x += direction;
  673. }
  674. break;
  675. case SNAP_LINE:
  676. /*
  677. * Snap around if the the previous line or the current one
  678. * has set ATTR_WRAP at its end. Then the whole next or
  679. * previous line will be selected.
  680. */
  681. *x = (direction < 0) ? 0 : term.col - 1;
  682. if(direction < 0 && *y > 0) {
  683. for(; *y > 0; *y += direction) {
  684. if(!(term.line[*y-1][term.col-1].mode
  685. & ATTR_WRAP)) {
  686. break;
  687. }
  688. }
  689. } else if(direction > 0 && *y < term.row-1) {
  690. for(; *y < term.row; *y += direction) {
  691. if(!(term.line[*y][term.col-1].mode
  692. & ATTR_WRAP)) {
  693. break;
  694. }
  695. }
  696. }
  697. break;
  698. default:
  699. /*
  700. * Select the whole line when the end of line is reached.
  701. */
  702. if(direction > 0) {
  703. i = term.col;
  704. while(--i > 0 && term.line[*y][i].c[0] == ' ')
  705. /* nothing */;
  706. if(i > 0 && i < *x)
  707. *x = term.col - 1;
  708. }
  709. break;
  710. }
  711. }
  712. void
  713. getbuttoninfo(XEvent *e) {
  714. int type;
  715. uint state = e->xbutton.state &~Button1Mask;
  716. sel.alt = IS_SET(MODE_ALTSCREEN);
  717. sel.oe.x = x2col(e->xbutton.x);
  718. sel.oe.y = y2row(e->xbutton.y);
  719. if(sel.ob.y < sel.oe.y
  720. || (sel.ob.y == sel.oe.y && sel.ob.x < sel.oe.x)) {
  721. selsnap(sel.snap, &sel.ob.x, &sel.ob.y, -1);
  722. selsnap(sel.snap, &sel.oe.x, &sel.oe.y, +1);
  723. } else {
  724. selsnap(sel.snap, &sel.oe.x, &sel.oe.y, -1);
  725. selsnap(sel.snap, &sel.ob.x, &sel.ob.y, +1);
  726. }
  727. selsort();
  728. sel.type = SEL_REGULAR;
  729. for(type = 1; type < LEN(selmasks); ++type) {
  730. if(match(selmasks[type], state)) {
  731. sel.type = type;
  732. break;
  733. }
  734. }
  735. }
  736. void
  737. mousereport(XEvent *e) {
  738. int x = x2col(e->xbutton.x), y = y2row(e->xbutton.y),
  739. button = e->xbutton.button, state = e->xbutton.state,
  740. len;
  741. char buf[40];
  742. static int ox, oy;
  743. /* from urxvt */
  744. if(e->xbutton.type == MotionNotify) {
  745. if(x == ox && y == oy)
  746. return;
  747. if(!IS_SET(MODE_MOUSEMOTION) && !IS_SET(MODE_MOUSEMANY))
  748. return;
  749. /* MOUSE_MOTION: no reporting if no button is pressed */
  750. if(IS_SET(MODE_MOUSEMOTION) && oldbutton == 3)
  751. return;
  752. button = oldbutton + 32;
  753. ox = x;
  754. oy = y;
  755. } else {
  756. if(!IS_SET(MODE_MOUSESGR) && e->xbutton.type == ButtonRelease) {
  757. button = 3;
  758. } else {
  759. button -= Button1;
  760. if(button >= 3)
  761. button += 64 - 3;
  762. }
  763. if(e->xbutton.type == ButtonPress) {
  764. oldbutton = button;
  765. ox = x;
  766. oy = y;
  767. } else if(e->xbutton.type == ButtonRelease) {
  768. oldbutton = 3;
  769. /* MODE_MOUSEX10: no button release reporting */
  770. if(IS_SET(MODE_MOUSEX10))
  771. return;
  772. }
  773. }
  774. if(!IS_SET(MODE_MOUSEX10)) {
  775. button += (state & ShiftMask ? 4 : 0)
  776. + (state & Mod4Mask ? 8 : 0)
  777. + (state & ControlMask ? 16 : 0);
  778. }
  779. len = 0;
  780. if(IS_SET(MODE_MOUSESGR)) {
  781. len = snprintf(buf, sizeof(buf), "\033[<%d;%d;%d%c",
  782. button, x+1, y+1,
  783. e->xbutton.type == ButtonRelease ? 'm' : 'M');
  784. } else if(x < 223 && y < 223) {
  785. len = snprintf(buf, sizeof(buf), "\033[M%c%c%c",
  786. 32+button, 32+x+1, 32+y+1);
  787. } else {
  788. return;
  789. }
  790. ttywrite(buf, len);
  791. }
  792. void
  793. bpress(XEvent *e) {
  794. struct timeval now;
  795. Mousekey *mk;
  796. if(IS_SET(MODE_MOUSE)) {
  797. mousereport(e);
  798. return;
  799. }
  800. for(mk = mshortcuts; mk < mshortcuts + LEN(mshortcuts); mk++) {
  801. if(e->xbutton.button == mk->b
  802. && match(mk->mask, e->xbutton.state)) {
  803. ttysend(mk->s, strlen(mk->s));
  804. return;
  805. }
  806. }
  807. if(e->xbutton.button == Button1) {
  808. gettimeofday(&now, NULL);
  809. /* Clear previous selection, logically and visually. */
  810. selclear(NULL);
  811. sel.mode = 1;
  812. sel.type = SEL_REGULAR;
  813. sel.oe.x = sel.ob.x = x2col(e->xbutton.x);
  814. sel.oe.y = sel.ob.y = y2row(e->xbutton.y);
  815. /*
  816. * If the user clicks below predefined timeouts specific
  817. * snapping behaviour is exposed.
  818. */
  819. if(TIMEDIFF(now, sel.tclick2) <= tripleclicktimeout) {
  820. sel.snap = SNAP_LINE;
  821. } else if(TIMEDIFF(now, sel.tclick1) <= doubleclicktimeout) {
  822. sel.snap = SNAP_WORD;
  823. } else {
  824. sel.snap = 0;
  825. }
  826. selsnap(sel.snap, &sel.ob.x, &sel.ob.y, -1);
  827. selsnap(sel.snap, &sel.oe.x, &sel.oe.y, +1);
  828. selsort();
  829. /*
  830. * Draw selection, unless it's regular and we don't want to
  831. * make clicks visible
  832. */
  833. if(sel.snap != 0) {
  834. sel.mode++;
  835. tsetdirt(sel.nb.y, sel.ne.y);
  836. }
  837. sel.tclick2 = sel.tclick1;
  838. sel.tclick1 = now;
  839. }
  840. }
  841. void
  842. selcopy(void) {
  843. char *str, *ptr;
  844. int x, y, bufsize, size, i, ex;
  845. Glyph *gp, *last;
  846. if(sel.ob.x == -1) {
  847. str = NULL;
  848. } else {
  849. bufsize = (term.col+1) * (sel.ne.y-sel.nb.y+1) * UTF_SIZ;
  850. ptr = str = xmalloc(bufsize);
  851. /* append every set & selected glyph to the selection */
  852. for(y = sel.nb.y; y < sel.ne.y + 1; y++) {
  853. gp = &term.line[y][0];
  854. last = &gp[term.col-1];
  855. while(last >= gp && !(selected(last - gp, y) &&
  856. strcmp(last->c, " ") != 0)) {
  857. --last;
  858. }
  859. for(x = 0; gp <= last; x++, ++gp) {
  860. if(!selected(x, y) || (gp->mode & ATTR_WDUMMY))
  861. continue;
  862. size = utf8size(gp->c);
  863. memcpy(ptr, gp->c, size);
  864. ptr += size;
  865. }
  866. /*
  867. * Copy and pasting of line endings is inconsistent
  868. * in the inconsistent terminal and GUI world.
  869. * The best solution seems like to produce '\n' when
  870. * something is copied from st and convert '\n' to
  871. * '\r', when something to be pasted is received by
  872. * st.
  873. * FIXME: Fix the computer world.
  874. */
  875. if(y < sel.ne.y && x > 0 && !((gp-1)->mode & ATTR_WRAP))
  876. *ptr++ = '\n';
  877. /*
  878. * If the last selected line expands in the selection
  879. * after the visible text '\n' is appended.
  880. */
  881. if(y == sel.ne.y) {
  882. i = term.col;
  883. while(--i > 0 && term.line[y][i].c[0] == ' ')
  884. /* nothing */;
  885. ex = sel.ne.x;
  886. if(sel.nb.y == sel.ne.y && sel.ne.x < sel.nb.x)
  887. ex = sel.nb.x;
  888. if(i < ex)
  889. *ptr++ = '\n';
  890. }
  891. }
  892. *ptr = 0;
  893. }
  894. xsetsel(str);
  895. }
  896. void
  897. selnotify(XEvent *e) {
  898. ulong nitems, ofs, rem;
  899. int format;
  900. uchar *data, *last, *repl;
  901. Atom type;
  902. ofs = 0;
  903. do {
  904. if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
  905. False, AnyPropertyType, &type, &format,
  906. &nitems, &rem, &data)) {
  907. fprintf(stderr, "Clipboard allocation failed\n");
  908. return;
  909. }
  910. /*
  911. * As seen in selcopy:
  912. * Line endings are inconsistent in the terminal and GUI world
  913. * copy and pasting. When receiving some selection data,
  914. * replace all '\n' with '\r'.
  915. * FIXME: Fix the computer world.
  916. */
  917. repl = data;
  918. last = data + nitems * format / 8;
  919. while((repl = memchr(repl, '\n', last - repl))) {
  920. *repl++ = '\r';
  921. }
  922. if(IS_SET(MODE_BRCKTPASTE))
  923. ttywrite("\033[200~", 6);
  924. ttysend((char *)data, nitems * format / 8);
  925. if(IS_SET(MODE_BRCKTPASTE))
  926. ttywrite("\033[201~", 6);
  927. XFree(data);
  928. /* number of 32-bit chunks returned */
  929. ofs += nitems * format / 32;
  930. } while(rem > 0);
  931. }
  932. void
  933. selpaste(const Arg *dummy) {
  934. XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY,
  935. xw.win, CurrentTime);
  936. }
  937. void
  938. clippaste(const Arg *dummy) {
  939. Atom clipboard;
  940. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  941. XConvertSelection(xw.dpy, clipboard, sel.xtarget, XA_PRIMARY,
  942. xw.win, CurrentTime);
  943. }
  944. void
  945. selclear(XEvent *e) {
  946. if(sel.ob.x == -1)
  947. return;
  948. sel.ob.x = -1;
  949. tsetdirt(sel.nb.y, sel.ne.y);
  950. }
  951. void
  952. selrequest(XEvent *e) {
  953. XSelectionRequestEvent *xsre;
  954. XSelectionEvent xev;
  955. Atom xa_targets, string;
  956. xsre = (XSelectionRequestEvent *) e;
  957. xev.type = SelectionNotify;
  958. xev.requestor = xsre->requestor;
  959. xev.selection = xsre->selection;
  960. xev.target = xsre->target;
  961. xev.time = xsre->time;
  962. /* reject */
  963. xev.property = None;
  964. xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
  965. if(xsre->target == xa_targets) {
  966. /* respond with the supported type */
  967. string = sel.xtarget;
  968. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  969. XA_ATOM, 32, PropModeReplace,
  970. (uchar *) &string, 1);
  971. xev.property = xsre->property;
  972. } else if(xsre->target == sel.xtarget && sel.clip != NULL) {
  973. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  974. xsre->target, 8, PropModeReplace,
  975. (uchar *) sel.clip, strlen(sel.clip));
  976. xev.property = xsre->property;
  977. }
  978. /* all done, send a notification to the listener */
  979. if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
  980. fprintf(stderr, "Error sending SelectionNotify event\n");
  981. }
  982. void
  983. xsetsel(char *str) {
  984. /* register the selection for both the clipboard and the primary */
  985. Atom clipboard;
  986. free(sel.clip);
  987. sel.clip = str;
  988. XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
  989. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  990. XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
  991. }
  992. void
  993. brelease(XEvent *e) {
  994. if(IS_SET(MODE_MOUSE)) {
  995. mousereport(e);
  996. return;
  997. }
  998. if(e->xbutton.button == Button2) {
  999. selpaste(NULL);
  1000. } else if(e->xbutton.button == Button1) {
  1001. if(sel.mode < 2) {
  1002. selclear(NULL);
  1003. } else {
  1004. getbuttoninfo(e);
  1005. selcopy();
  1006. }
  1007. sel.mode = 0;
  1008. tsetdirt(sel.nb.y, sel.ne.y);
  1009. }
  1010. }
  1011. void
  1012. bmotion(XEvent *e) {
  1013. int oldey, oldex, oldsby, oldsey;
  1014. if(IS_SET(MODE_MOUSE)) {
  1015. mousereport(e);
  1016. return;
  1017. }
  1018. if(!sel.mode)
  1019. return;
  1020. sel.mode++;
  1021. oldey = sel.oe.y;
  1022. oldex = sel.oe.x;
  1023. oldsby = sel.nb.y;
  1024. oldsey = sel.ne.y;
  1025. getbuttoninfo(e);
  1026. if(oldey != sel.oe.y || oldex != sel.oe.x)
  1027. tsetdirt(MIN(sel.nb.y, oldsby), MAX(sel.ne.y, oldsey));
  1028. }
  1029. void
  1030. die(const char *errstr, ...) {
  1031. va_list ap;
  1032. va_start(ap, errstr);
  1033. vfprintf(stderr, errstr, ap);
  1034. va_end(ap);
  1035. exit(EXIT_FAILURE);
  1036. }
  1037. void
  1038. execsh(void) {
  1039. char **args;
  1040. char *envshell = getenv("SHELL");
  1041. const struct passwd *pass = getpwuid(getuid());
  1042. char buf[sizeof(long) * 8 + 1];
  1043. unsetenv("COLUMNS");
  1044. unsetenv("LINES");
  1045. unsetenv("TERMCAP");
  1046. if(pass) {
  1047. setenv("LOGNAME", pass->pw_name, 1);
  1048. setenv("USER", pass->pw_name, 1);
  1049. setenv("SHELL", pass->pw_shell, 0);
  1050. setenv("HOME", pass->pw_dir, 0);
  1051. }
  1052. snprintf(buf, sizeof(buf), "%lu", xw.win);
  1053. setenv("WINDOWID", buf, 1);
  1054. signal(SIGCHLD, SIG_DFL);
  1055. signal(SIGHUP, SIG_DFL);
  1056. signal(SIGINT, SIG_DFL);
  1057. signal(SIGQUIT, SIG_DFL);
  1058. signal(SIGTERM, SIG_DFL);
  1059. signal(SIGALRM, SIG_DFL);
  1060. DEFAULT(envshell, shell);
  1061. setenv("TERM", termname, 1);
  1062. args = opt_cmd ? opt_cmd : (char *[]){envshell, "-i", NULL};
  1063. execvp(args[0], args);
  1064. exit(EXIT_FAILURE);
  1065. }
  1066. void
  1067. sigchld(int a) {
  1068. int stat = 0;
  1069. if(waitpid(pid, &stat, 0) < 0)
  1070. die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
  1071. if(WIFEXITED(stat)) {
  1072. exit(WEXITSTATUS(stat));
  1073. } else {
  1074. exit(EXIT_FAILURE);
  1075. }
  1076. }
  1077. void
  1078. ttynew(void) {
  1079. int m, s;
  1080. struct winsize w = {term.row, term.col, 0, 0};
  1081. /* seems to work fine on linux, openbsd and freebsd */
  1082. if(openpty(&m, &s, NULL, NULL, &w) < 0)
  1083. die("openpty failed: %s\n", SERRNO);
  1084. switch(pid = fork()) {
  1085. case -1:
  1086. die("fork failed\n");
  1087. break;
  1088. case 0:
  1089. setsid(); /* create a new process group */
  1090. dup2(s, STDIN_FILENO);
  1091. dup2(s, STDOUT_FILENO);
  1092. dup2(s, STDERR_FILENO);
  1093. if(ioctl(s, TIOCSCTTY, NULL) < 0)
  1094. die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
  1095. close(s);
  1096. close(m);
  1097. execsh();
  1098. break;
  1099. default:
  1100. close(s);
  1101. cmdfd = m;
  1102. signal(SIGCHLD, sigchld);
  1103. if(opt_io) {
  1104. iofd = (!strcmp(opt_io, "-")) ?
  1105. STDOUT_FILENO :
  1106. open(opt_io, O_WRONLY | O_CREAT, 0666);
  1107. if(iofd < 0) {
  1108. fprintf(stderr, "Error opening %s:%s\n",
  1109. opt_io, strerror(errno));
  1110. }
  1111. }
  1112. }
  1113. }
  1114. void
  1115. dump(char c) {
  1116. static int col;
  1117. fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
  1118. if(++col % 10 == 0)
  1119. fprintf(stderr, "\n");
  1120. }
  1121. void
  1122. ttyread(void) {
  1123. static char buf[BUFSIZ];
  1124. static int buflen = 0;
  1125. char *ptr;
  1126. char s[UTF_SIZ];
  1127. int charsize; /* size of utf8 char in bytes */
  1128. long utf8c;
  1129. int ret;
  1130. /* append read bytes to unprocessed bytes */
  1131. if((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
  1132. die("Couldn't read from shell: %s\n", SERRNO);
  1133. /* process every complete utf8 char */
  1134. buflen += ret;
  1135. ptr = buf;
  1136. while(buflen >= UTF_SIZ || isfullutf8(ptr,buflen)) {
  1137. charsize = utf8decode(ptr, &utf8c);
  1138. utf8encode(&utf8c, s);
  1139. tputc(s, charsize);
  1140. ptr += charsize;
  1141. buflen -= charsize;
  1142. }
  1143. /* keep any uncomplete utf8 char for the next call */
  1144. memmove(buf, ptr, buflen);
  1145. }
  1146. void
  1147. ttywrite(const char *s, size_t n) {
  1148. if(write(cmdfd, s, n) == -1)
  1149. die("write error on tty: %s\n", SERRNO);
  1150. }
  1151. void
  1152. ttysend(char *s, size_t n) {
  1153. ttywrite(s, n);
  1154. if(IS_SET(MODE_ECHO))
  1155. techo(s, n);
  1156. }
  1157. void
  1158. ttyresize(void) {
  1159. struct winsize w;
  1160. w.ws_row = term.row;
  1161. w.ws_col = term.col;
  1162. w.ws_xpixel = xw.tw;
  1163. w.ws_ypixel = xw.th;
  1164. if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  1165. fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
  1166. }
  1167. int
  1168. tattrset(int attr) {
  1169. int i, j;
  1170. for(i = 0; i < term.row-1; i++) {
  1171. for(j = 0; j < term.col-1; j++) {
  1172. if(term.line[i][j].mode & attr)
  1173. return 1;
  1174. }
  1175. }
  1176. return 0;
  1177. }
  1178. void
  1179. tsetdirt(int top, int bot) {
  1180. int i;
  1181. LIMIT(top, 0, term.row-1);
  1182. LIMIT(bot, 0, term.row-1);
  1183. for(i = top; i <= bot; i++)
  1184. term.dirty[i] = 1;
  1185. }
  1186. void
  1187. tsetdirtattr(int attr) {
  1188. int i, j;
  1189. for(i = 0; i < term.row-1; i++) {
  1190. for(j = 0; j < term.col-1; j++) {
  1191. if(term.line[i][j].mode & attr) {
  1192. tsetdirt(i, i);
  1193. break;
  1194. }
  1195. }
  1196. }
  1197. }
  1198. void
  1199. tfulldirt(void) {
  1200. tsetdirt(0, term.row-1);
  1201. }
  1202. void
  1203. tcursor(int mode) {
  1204. static TCursor c[2];
  1205. bool alt = IS_SET(MODE_ALTSCREEN);
  1206. if(mode == CURSOR_SAVE) {
  1207. c[alt] = term.c;
  1208. } else if(mode == CURSOR_LOAD) {
  1209. term.c = c[alt];
  1210. tmoveto(c[alt].x, c[alt].y);
  1211. }
  1212. }
  1213. void
  1214. treset(void) {
  1215. uint i;
  1216. term.c = (TCursor){{
  1217. .mode = ATTR_NULL,
  1218. .fg = defaultfg,
  1219. .bg = defaultbg
  1220. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  1221. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  1222. for(i = tabspaces; i < term.col; i += tabspaces)
  1223. term.tabs[i] = 1;
  1224. term.top = 0;
  1225. term.bot = term.row - 1;
  1226. term.mode = MODE_WRAP;
  1227. memset(term.trantbl, sizeof(term.trantbl), CS_USA);
  1228. term.charset = 0;
  1229. tclearregion(0, 0, term.col-1, term.row-1);
  1230. tmoveto(0, 0);
  1231. tcursor(CURSOR_SAVE);
  1232. }
  1233. void
  1234. tnew(int col, int row) {
  1235. term = (Term){ .c = { .attr = { .fg = defaultfg, .bg = defaultbg } } };
  1236. tresize(col, row);
  1237. term.numlock = 1;
  1238. treset();
  1239. }
  1240. void
  1241. tswapscreen(void) {
  1242. Line *tmp = term.line;
  1243. term.line = term.alt;
  1244. term.alt = tmp;
  1245. term.mode ^= MODE_ALTSCREEN;
  1246. tfulldirt();
  1247. }
  1248. void
  1249. tscrolldown(int orig, int n) {
  1250. int i;
  1251. Line temp;
  1252. LIMIT(n, 0, term.bot-orig+1);
  1253. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  1254. for(i = term.bot; i >= orig+n; i--) {
  1255. temp = term.line[i];
  1256. term.line[i] = term.line[i-n];
  1257. term.line[i-n] = temp;
  1258. term.dirty[i] = 1;
  1259. term.dirty[i-n] = 1;
  1260. }
  1261. selscroll(orig, n);
  1262. }
  1263. void
  1264. tscrollup(int orig, int n) {
  1265. int i;
  1266. Line temp;
  1267. LIMIT(n, 0, term.bot-orig+1);
  1268. tclearregion(0, orig, term.col-1, orig+n-1);
  1269. for(i = orig; i <= term.bot-n; i++) {
  1270. temp = term.line[i];
  1271. term.line[i] = term.line[i+n];
  1272. term.line[i+n] = temp;
  1273. term.dirty[i] = 1;
  1274. term.dirty[i+n] = 1;
  1275. }
  1276. selscroll(orig, -n);
  1277. }
  1278. void
  1279. selscroll(int orig, int n) {
  1280. if(sel.ob.x == -1)
  1281. return;
  1282. if(BETWEEN(sel.ob.y, orig, term.bot) || BETWEEN(sel.oe.y, orig, term.bot)) {
  1283. if((sel.ob.y += n) > term.bot || (sel.oe.y += n) < term.top) {
  1284. selclear(NULL);
  1285. return;
  1286. }
  1287. if(sel.type == SEL_RECTANGULAR) {
  1288. if(sel.ob.y < term.top)
  1289. sel.ob.y = term.top;
  1290. if(sel.oe.y > term.bot)
  1291. sel.oe.y = term.bot;
  1292. } else {
  1293. if(sel.ob.y < term.top) {
  1294. sel.ob.y = term.top;
  1295. sel.ob.x = 0;
  1296. }
  1297. if(sel.oe.y > term.bot) {
  1298. sel.oe.y = term.bot;
  1299. sel.oe.x = term.col;
  1300. }
  1301. }
  1302. selsort();
  1303. }
  1304. }
  1305. void
  1306. tnewline(int first_col) {
  1307. int y = term.c.y;
  1308. if(y == term.bot) {
  1309. tscrollup(term.top, 1);
  1310. } else {
  1311. y++;
  1312. }
  1313. tmoveto(first_col ? 0 : term.c.x, y);
  1314. }
  1315. void
  1316. csiparse(void) {
  1317. char *p = csiescseq.buf, *np;
  1318. long int v;
  1319. csiescseq.narg = 0;
  1320. if(*p == '?') {
  1321. csiescseq.priv = 1;
  1322. p++;
  1323. }
  1324. csiescseq.buf[csiescseq.len] = '\0';
  1325. while(p < csiescseq.buf+csiescseq.len) {
  1326. np = NULL;
  1327. v = strtol(p, &np, 10);
  1328. if(np == p)
  1329. v = 0;
  1330. if(v == LONG_MAX || v == LONG_MIN)
  1331. v = -1;
  1332. csiescseq.arg[csiescseq.narg++] = v;
  1333. p = np;
  1334. if(*p != ';' || csiescseq.narg == ESC_ARG_SIZ)
  1335. break;
  1336. p++;
  1337. }
  1338. csiescseq.mode = *p;
  1339. }
  1340. /* for absolute user moves, when decom is set */
  1341. void
  1342. tmoveato(int x, int y) {
  1343. tmoveto(x, y + ((term.c.state & CURSOR_ORIGIN) ? term.top: 0));
  1344. }
  1345. void
  1346. tmoveto(int x, int y) {
  1347. int miny, maxy;
  1348. if(term.c.state & CURSOR_ORIGIN) {
  1349. miny = term.top;
  1350. maxy = term.bot;
  1351. } else {
  1352. miny = 0;
  1353. maxy = term.row - 1;
  1354. }
  1355. LIMIT(x, 0, term.col-1);
  1356. LIMIT(y, miny, maxy);
  1357. term.c.state &= ~CURSOR_WRAPNEXT;
  1358. term.c.x = x;
  1359. term.c.y = y;
  1360. }
  1361. void
  1362. tsetchar(char *c, Glyph *attr, int x, int y) {
  1363. static char *vt100_0[62] = { /* 0x41 - 0x7e */
  1364. "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
  1365. 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
  1366. 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
  1367. 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
  1368. "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
  1369. "␤", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
  1370. "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
  1371. "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
  1372. };
  1373. /*
  1374. * The table is proudly stolen from rxvt.
  1375. */
  1376. if(attr->mode & ATTR_GFX) {
  1377. if(c[0] >= 0x41 && c[0] <= 0x7e
  1378. && vt100_0[c[0] - 0x41]) {
  1379. c = vt100_0[c[0] - 0x41];
  1380. }
  1381. }
  1382. if(term.line[y][x].mode & ATTR_WIDE) {
  1383. if(x+1 < term.col) {
  1384. term.line[y][x+1].c[0] = ' ';
  1385. term.line[y][x+1].mode &= ~ATTR_WDUMMY;
  1386. }
  1387. } else if(term.line[y][x].mode & ATTR_WDUMMY) {
  1388. term.line[y][x-1].c[0] = ' ';
  1389. term.line[y][x-1].mode &= ~ATTR_WIDE;
  1390. }
  1391. term.dirty[y] = 1;
  1392. term.line[y][x] = *attr;
  1393. memcpy(term.line[y][x].c, c, UTF_SIZ);
  1394. }
  1395. void
  1396. tclearregion(int x1, int y1, int x2, int y2) {
  1397. int x, y, temp;
  1398. if(x1 > x2)
  1399. temp = x1, x1 = x2, x2 = temp;
  1400. if(y1 > y2)
  1401. temp = y1, y1 = y2, y2 = temp;
  1402. LIMIT(x1, 0, term.col-1);
  1403. LIMIT(x2, 0, term.col-1);
  1404. LIMIT(y1, 0, term.row-1);
  1405. LIMIT(y2, 0, term.row-1);
  1406. for(y = y1; y <= y2; y++) {
  1407. term.dirty[y] = 1;
  1408. for(x = x1; x <= x2; x++) {
  1409. if(selected(x, y))
  1410. selclear(NULL);
  1411. term.line[y][x] = term.c.attr;
  1412. memcpy(term.line[y][x].c, " ", 2);
  1413. }
  1414. }
  1415. }
  1416. void
  1417. tdeletechar(int n) {
  1418. int src = term.c.x + n;
  1419. int dst = term.c.x;
  1420. int size = term.col - src;
  1421. term.dirty[term.c.y] = 1;
  1422. if(src >= term.col) {
  1423. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1424. return;
  1425. }
  1426. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src],
  1427. size * sizeof(Glyph));
  1428. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  1429. }
  1430. void
  1431. tinsertblank(int n) {
  1432. int src = term.c.x;
  1433. int dst = src + n;
  1434. int size = term.col - dst;
  1435. term.dirty[term.c.y] = 1;
  1436. if(dst >= term.col) {
  1437. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1438. return;
  1439. }
  1440. memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src],
  1441. size * sizeof(Glyph));
  1442. tclearregion(src, term.c.y, dst - 1, term.c.y);
  1443. }
  1444. void
  1445. tinsertblankline(int n) {
  1446. if(term.c.y < term.top || term.c.y > term.bot)
  1447. return;
  1448. tscrolldown(term.c.y, n);
  1449. }
  1450. void
  1451. tdeleteline(int n) {
  1452. if(term.c.y < term.top || term.c.y > term.bot)
  1453. return;
  1454. tscrollup(term.c.y, n);
  1455. }
  1456. int32_t
  1457. tdefcolor(int *attr, int *npar, int l) {
  1458. int32_t idx = -1;
  1459. uint r, g, b;
  1460. switch (attr[*npar + 1]) {
  1461. case 2: /* direct colour in RGB space */
  1462. if (*npar + 4 >= l) {
  1463. fprintf(stderr,
  1464. "erresc(38): Incorrect number of parameters (%d)\n",
  1465. *npar);
  1466. break;
  1467. }
  1468. r = attr[*npar + 2];
  1469. g = attr[*npar + 3];
  1470. b = attr[*npar + 4];
  1471. *npar += 4;
  1472. if(!BETWEEN(r, 0, 255) || !BETWEEN(g, 0, 255) || !BETWEEN(b, 0, 255))
  1473. fprintf(stderr, "erresc: bad rgb color (%d,%d,%d)\n",
  1474. r, g, b);
  1475. else
  1476. idx = TRUECOLOR(r, g, b);
  1477. break;
  1478. case 5: /* indexed colour */
  1479. if (*npar + 2 >= l) {
  1480. fprintf(stderr,
  1481. "erresc(38): Incorrect number of parameters (%d)\n",
  1482. *npar);
  1483. break;
  1484. }
  1485. *npar += 2;
  1486. if(!BETWEEN(attr[*npar], 0, 255))
  1487. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[*npar]);
  1488. else
  1489. idx = attr[*npar];
  1490. break;
  1491. case 0: /* implemented defined (only foreground) */
  1492. case 1: /* transparent */
  1493. case 3: /* direct colour in CMY space */
  1494. case 4: /* direct colour in CMYK space */
  1495. default:
  1496. fprintf(stderr,
  1497. "erresc(38): gfx attr %d unknown\n", attr[*npar]);
  1498. }
  1499. return idx;
  1500. }
  1501. void
  1502. tsetattr(int *attr, int l) {
  1503. int i;
  1504. int32_t idx;
  1505. for(i = 0; i < l; i++) {
  1506. switch(attr[i]) {
  1507. case 0:
  1508. term.c.attr.mode &= ~(ATTR_REVERSE | ATTR_UNDERLINE \
  1509. | ATTR_BOLD | ATTR_ITALIC \
  1510. | ATTR_BLINK);
  1511. term.c.attr.fg = defaultfg;
  1512. term.c.attr.bg = defaultbg;
  1513. break;
  1514. case 1:
  1515. term.c.attr.mode |= ATTR_BOLD;
  1516. break;
  1517. case 3:
  1518. term.c.attr.mode |= ATTR_ITALIC;
  1519. break;
  1520. case 4:
  1521. term.c.attr.mode |= ATTR_UNDERLINE;
  1522. break;
  1523. case 5: /* slow blink */
  1524. case 6: /* rapid blink */
  1525. term.c.attr.mode |= ATTR_BLINK;
  1526. break;
  1527. case 7:
  1528. term.c.attr.mode |= ATTR_REVERSE;
  1529. break;
  1530. case 21:
  1531. case 22:
  1532. term.c.attr.mode &= ~ATTR_BOLD;
  1533. break;
  1534. case 23:
  1535. term.c.attr.mode &= ~ATTR_ITALIC;
  1536. break;
  1537. case 24:
  1538. term.c.attr.mode &= ~ATTR_UNDERLINE;
  1539. break;
  1540. case 25:
  1541. case 26:
  1542. term.c.attr.mode &= ~ATTR_BLINK;
  1543. break;
  1544. case 27:
  1545. term.c.attr.mode &= ~ATTR_REVERSE;
  1546. break;
  1547. case 38:
  1548. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1549. term.c.attr.fg = idx;
  1550. break;
  1551. case 39:
  1552. term.c.attr.fg = defaultfg;
  1553. break;
  1554. case 48:
  1555. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1556. term.c.attr.bg = idx;
  1557. break;
  1558. case 49:
  1559. term.c.attr.bg = defaultbg;
  1560. break;
  1561. default:
  1562. if(BETWEEN(attr[i], 30, 37)) {
  1563. term.c.attr.fg = attr[i] - 30;
  1564. } else if(BETWEEN(attr[i], 40, 47)) {
  1565. term.c.attr.bg = attr[i] - 40;
  1566. } else if(BETWEEN(attr[i], 90, 97)) {
  1567. term.c.attr.fg = attr[i] - 90 + 8;
  1568. } else if(BETWEEN(attr[i], 100, 107)) {
  1569. term.c.attr.bg = attr[i] - 100 + 8;
  1570. } else {
  1571. fprintf(stderr,
  1572. "erresc(default): gfx attr %d unknown\n",
  1573. attr[i]), csidump();
  1574. }
  1575. break;
  1576. }
  1577. }
  1578. }
  1579. void
  1580. tsetscroll(int t, int b) {
  1581. int temp;
  1582. LIMIT(t, 0, term.row-1);
  1583. LIMIT(b, 0, term.row-1);
  1584. if(t > b) {
  1585. temp = t;
  1586. t = b;
  1587. b = temp;
  1588. }
  1589. term.top = t;
  1590. term.bot = b;
  1591. }
  1592. #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
  1593. void
  1594. tsetmode(bool priv, bool set, int *args, int narg) {
  1595. int *lim, mode;
  1596. bool alt;
  1597. for(lim = args + narg; args < lim; ++args) {
  1598. if(priv) {
  1599. switch(*args) {
  1600. break;
  1601. case 1: /* DECCKM -- Cursor key */
  1602. MODBIT(term.mode, set, MODE_APPCURSOR);
  1603. break;
  1604. case 5: /* DECSCNM -- Reverse video */
  1605. mode = term.mode;
  1606. MODBIT(term.mode, set, MODE_REVERSE);
  1607. if(mode != term.mode)
  1608. redraw(REDRAW_TIMEOUT);
  1609. break;
  1610. case 6: /* DECOM -- Origin */
  1611. MODBIT(term.c.state, set, CURSOR_ORIGIN);
  1612. tmoveato(0, 0);
  1613. break;
  1614. case 7: /* DECAWM -- Auto wrap */
  1615. MODBIT(term.mode, set, MODE_WRAP);
  1616. break;
  1617. case 0: /* Error (IGNORED) */
  1618. case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
  1619. case 3: /* DECCOLM -- Column (IGNORED) */
  1620. case 4: /* DECSCLM -- Scroll (IGNORED) */
  1621. case 8: /* DECARM -- Auto repeat (IGNORED) */
  1622. case 18: /* DECPFF -- Printer feed (IGNORED) */
  1623. case 19: /* DECPEX -- Printer extent (IGNORED) */
  1624. case 42: /* DECNRCM -- National characters (IGNORED) */
  1625. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1626. break;
  1627. case 25: /* DECTCEM -- Text Cursor Enable Mode */
  1628. MODBIT(term.mode, !set, MODE_HIDE);
  1629. break;
  1630. case 9: /* X10 mouse compatibility mode */
  1631. xsetpointermotion(0);
  1632. MODBIT(term.mode, 0, MODE_MOUSE);
  1633. MODBIT(term.mode, set, MODE_MOUSEX10);
  1634. break;
  1635. case 1000: /* 1000: report button press */
  1636. xsetpointermotion(0);
  1637. MODBIT(term.mode, 0, MODE_MOUSE);
  1638. MODBIT(term.mode, set, MODE_MOUSEBTN);
  1639. break;
  1640. case 1002: /* 1002: report motion on button press */
  1641. xsetpointermotion(0);
  1642. MODBIT(term.mode, 0, MODE_MOUSE);
  1643. MODBIT(term.mode, set, MODE_MOUSEMOTION);
  1644. break;
  1645. case 1003: /* 1003: enable all mouse motions */
  1646. xsetpointermotion(set);
  1647. MODBIT(term.mode, 0, MODE_MOUSE);
  1648. MODBIT(term.mode, set, MODE_MOUSEMANY);
  1649. break;
  1650. case 1004: /* 1004: send focus events to tty */
  1651. MODBIT(term.mode, set, MODE_FOCUS);
  1652. break;
  1653. case 1006: /* 1006: extended reporting mode */
  1654. MODBIT(term.mode, set, MODE_MOUSESGR);
  1655. break;
  1656. case 1034:
  1657. MODBIT(term.mode, set, MODE_8BIT);
  1658. break;
  1659. case 1049: /* swap screen & set/restore cursor as xterm */
  1660. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1661. case 47: /* swap screen */
  1662. case 1047:
  1663. if (!allowaltscreen)
  1664. break;
  1665. alt = IS_SET(MODE_ALTSCREEN);
  1666. if(alt) {
  1667. tclearregion(0, 0, term.col-1,
  1668. term.row-1);
  1669. }
  1670. if(set ^ alt) /* set is always 1 or 0 */
  1671. tswapscreen();
  1672. if(*args != 1049)
  1673. break;
  1674. /* FALLTRU */
  1675. case 1048:
  1676. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1677. break;
  1678. case 2004: /* 2004: bracketed paste mode */
  1679. MODBIT(term.mode, set, MODE_BRCKTPASTE);
  1680. break;
  1681. /* Not implemented mouse modes. See comments there. */
  1682. case 1001: /* mouse highlight mode; can hang the
  1683. terminal by design when implemented. */
  1684. case 1005: /* UTF-8 mouse mode; will confuse
  1685. applications not supporting UTF-8
  1686. and luit. */
  1687. case 1015: /* urxvt mangled mouse mode; incompatible
  1688. and can be mistaken for other control
  1689. codes. */
  1690. default:
  1691. fprintf(stderr,
  1692. "erresc: unknown private set/reset mode %d\n",
  1693. *args);
  1694. break;
  1695. }
  1696. } else {
  1697. switch(*args) {
  1698. case 0: /* Error (IGNORED) */
  1699. break;
  1700. case 2: /* KAM -- keyboard action */
  1701. MODBIT(term.mode, set, MODE_KBDLOCK);
  1702. break;
  1703. case 4: /* IRM -- Insertion-replacement */
  1704. MODBIT(term.mode, set, MODE_INSERT);
  1705. break;
  1706. case 12: /* SRM -- Send/Receive */
  1707. MODBIT(term.mode, !set, MODE_ECHO);
  1708. break;
  1709. case 20: /* LNM -- Linefeed/new line */
  1710. MODBIT(term.mode, set, MODE_CRLF);
  1711. break;
  1712. default:
  1713. fprintf(stderr,
  1714. "erresc: unknown set/reset mode %d\n",
  1715. *args);
  1716. break;
  1717. }
  1718. }
  1719. }
  1720. }
  1721. void
  1722. csihandle(void) {
  1723. char buf[40];
  1724. int len;
  1725. switch(csiescseq.mode) {
  1726. default:
  1727. unknown:
  1728. fprintf(stderr, "erresc: unknown csi ");
  1729. csidump();
  1730. /* die(""); */
  1731. break;
  1732. case '@': /* ICH -- Insert <n> blank char */
  1733. DEFAULT(csiescseq.arg[0], 1);
  1734. tinsertblank(csiescseq.arg[0]);
  1735. break;
  1736. case 'A': /* CUU -- Cursor <n> Up */
  1737. DEFAULT(csiescseq.arg[0], 1);
  1738. tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
  1739. break;
  1740. case 'B': /* CUD -- Cursor <n> Down */
  1741. case 'e': /* VPR --Cursor <n> Down */
  1742. DEFAULT(csiescseq.arg[0], 1);
  1743. tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
  1744. break;
  1745. case 'c': /* DA -- Device Attributes */
  1746. if(csiescseq.arg[0] == 0)
  1747. ttywrite(VT102ID, sizeof(VT102ID) - 1);
  1748. break;
  1749. case 'C': /* CUF -- Cursor <n> Forward */
  1750. case 'a': /* HPR -- Cursor <n> Forward */
  1751. DEFAULT(csiescseq.arg[0], 1);
  1752. tmoveto(term.c.x+csiescseq.arg[0], term.c.y);
  1753. break;
  1754. case 'D': /* CUB -- Cursor <n> Backward */
  1755. DEFAULT(csiescseq.arg[0], 1);
  1756. tmoveto(term.c.x-csiescseq.arg[0], term.c.y);
  1757. break;
  1758. case 'E': /* CNL -- Cursor <n> Down and first col */
  1759. DEFAULT(csiescseq.arg[0], 1);
  1760. tmoveto(0, term.c.y+csiescseq.arg[0]);
  1761. break;
  1762. case 'F': /* CPL -- Cursor <n> Up and first col */
  1763. DEFAULT(csiescseq.arg[0], 1);
  1764. tmoveto(0, term.c.y-csiescseq.arg[0]);
  1765. break;
  1766. case 'g': /* TBC -- Tabulation clear */
  1767. switch(csiescseq.arg[0]) {
  1768. case 0: /* clear current tab stop */
  1769. term.tabs[term.c.x] = 0;
  1770. break;
  1771. case 3: /* clear all the tabs */
  1772. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  1773. break;
  1774. default:
  1775. goto unknown;
  1776. }
  1777. break;
  1778. case 'G': /* CHA -- Move to <col> */
  1779. case '`': /* HPA */
  1780. DEFAULT(csiescseq.arg[0], 1);
  1781. tmoveto(csiescseq.arg[0]-1, term.c.y);
  1782. break;
  1783. case 'H': /* CUP -- Move to <row> <col> */
  1784. case 'f': /* HVP */
  1785. DEFAULT(csiescseq.arg[0], 1);
  1786. DEFAULT(csiescseq.arg[1], 1);
  1787. tmoveato(csiescseq.arg[1]-1, csiescseq.arg[0]-1);
  1788. break;
  1789. case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
  1790. DEFAULT(csiescseq.arg[0], 1);
  1791. while(csiescseq.arg[0]--)
  1792. tputtab(1);
  1793. break;
  1794. case 'J': /* ED -- Clear screen */
  1795. selclear(NULL);
  1796. switch(csiescseq.arg[0]) {
  1797. case 0: /* below */
  1798. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1799. if(term.c.y < term.row-1) {
  1800. tclearregion(0, term.c.y+1, term.col-1,
  1801. term.row-1);
  1802. }
  1803. break;
  1804. case 1: /* above */
  1805. if(term.c.y > 1)
  1806. tclearregion(0, 0, term.col-1, term.c.y-1);
  1807. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1808. break;
  1809. case 2: /* all */
  1810. tclearregion(0, 0, term.col-1, term.row-1);
  1811. break;
  1812. default:
  1813. goto unknown;
  1814. }
  1815. break;
  1816. case 'K': /* EL -- Clear line */
  1817. switch(csiescseq.arg[0]) {
  1818. case 0: /* right */
  1819. tclearregion(term.c.x, term.c.y, term.col-1,
  1820. term.c.y);
  1821. break;
  1822. case 1: /* left */
  1823. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1824. break;
  1825. case 2: /* all */
  1826. tclearregion(0, term.c.y, term.col-1, term.c.y);
  1827. break;
  1828. }
  1829. break;
  1830. case 'S': /* SU -- Scroll <n> line up */
  1831. DEFAULT(csiescseq.arg[0], 1);
  1832. tscrollup(term.top, csiescseq.arg[0]);
  1833. break;
  1834. case 'T': /* SD -- Scroll <n> line down */
  1835. DEFAULT(csiescseq.arg[0], 1);
  1836. tscrolldown(term.top, csiescseq.arg[0]);
  1837. break;
  1838. case 'L': /* IL -- Insert <n> blank lines */
  1839. DEFAULT(csiescseq.arg[0], 1);
  1840. tinsertblankline(csiescseq.arg[0]);
  1841. break;
  1842. case 'l': /* RM -- Reset Mode */
  1843. tsetmode(csiescseq.priv, 0, csiescseq.arg, csiescseq.narg);
  1844. break;
  1845. case 'M': /* DL -- Delete <n> lines */
  1846. DEFAULT(csiescseq.arg[0], 1);
  1847. tdeleteline(csiescseq.arg[0]);
  1848. break;
  1849. case 'X': /* ECH -- Erase <n> char */
  1850. DEFAULT(csiescseq.arg[0], 1);
  1851. tclearregion(term.c.x, term.c.y,
  1852. term.c.x + csiescseq.arg[0] - 1, term.c.y);
  1853. break;
  1854. case 'P': /* DCH -- Delete <n> char */
  1855. DEFAULT(csiescseq.arg[0], 1);
  1856. tdeletechar(csiescseq.arg[0]);
  1857. break;
  1858. case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
  1859. DEFAULT(csiescseq.arg[0], 1);
  1860. while(csiescseq.arg[0]--)
  1861. tputtab(0);
  1862. break;
  1863. case 'd': /* VPA -- Move to <row> */
  1864. DEFAULT(csiescseq.arg[0], 1);
  1865. tmoveato(term.c.x, csiescseq.arg[0]-1);
  1866. break;
  1867. case 'h': /* SM -- Set terminal mode */
  1868. tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg);
  1869. break;
  1870. case 'm': /* SGR -- Terminal attribute (color) */
  1871. tsetattr(csiescseq.arg, csiescseq.narg);
  1872. break;
  1873. case 'n': /* DSR – Device Status Report (cursor position) */
  1874. if (csiescseq.arg[0] == 6) {
  1875. len = snprintf(buf, sizeof(buf),"\033[%i;%iR",
  1876. term.c.y+1, term.c.x+1);
  1877. ttywrite(buf, len);
  1878. break;
  1879. }
  1880. case 'r': /* DECSTBM -- Set Scrolling Region */
  1881. if(csiescseq.priv) {
  1882. goto unknown;
  1883. } else {
  1884. DEFAULT(csiescseq.arg[0], 1);
  1885. DEFAULT(csiescseq.arg[1], term.row);
  1886. tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1);
  1887. tmoveato(0, 0);
  1888. }
  1889. break;
  1890. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  1891. tcursor(CURSOR_SAVE);
  1892. break;
  1893. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  1894. tcursor(CURSOR_LOAD);
  1895. break;
  1896. }
  1897. }
  1898. void
  1899. csidump(void) {
  1900. int i;
  1901. uint c;
  1902. printf("ESC[");
  1903. for(i = 0; i < csiescseq.len; i++) {
  1904. c = csiescseq.buf[i] & 0xff;
  1905. if(isprint(c)) {
  1906. putchar(c);
  1907. } else if(c == '\n') {
  1908. printf("(\\n)");
  1909. } else if(c == '\r') {
  1910. printf("(\\r)");
  1911. } else if(c == 0x1b) {
  1912. printf("(\\e)");
  1913. } else {
  1914. printf("(%02x)", c);
  1915. }
  1916. }
  1917. putchar('\n');
  1918. }
  1919. void
  1920. csireset(void) {
  1921. memset(&csiescseq, 0, sizeof(csiescseq));
  1922. }
  1923. void
  1924. strhandle(void) {
  1925. char *p = NULL;
  1926. int j, narg, par;
  1927. strparse();
  1928. narg = strescseq.narg;
  1929. par = atoi(strescseq.args[0]);
  1930. switch(strescseq.type) {
  1931. case ']': /* OSC -- Operating System Command */
  1932. switch(par) {
  1933. case 0:
  1934. case 1:
  1935. case 2:
  1936. if(narg > 1)
  1937. xsettitle(strescseq.args[1]);
  1938. return;
  1939. case 4: /* color set */
  1940. if(narg < 3)
  1941. break;
  1942. p = strescseq.args[2];
  1943. /* fall through */
  1944. case 104: /* color reset, here p = NULL */
  1945. j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
  1946. if (!xsetcolorname(j, p)) {
  1947. fprintf(stderr, "erresc: invalid color %s\n", p);
  1948. } else {
  1949. /*
  1950. * TODO if defaultbg color is changed, borders
  1951. * are dirty
  1952. */
  1953. redraw(0);
  1954. }
  1955. return;
  1956. }
  1957. break;
  1958. case 'k': /* old title set compatibility */
  1959. xsettitle(strescseq.args[0]);
  1960. return;
  1961. case 'P': /* DSC -- Device Control String */
  1962. case '_': /* APC -- Application Program Command */
  1963. case '^': /* PM -- Privacy Message */
  1964. return;
  1965. }
  1966. fprintf(stderr, "erresc: unknown str ");
  1967. strdump();
  1968. }
  1969. void
  1970. strparse(void) {
  1971. char *p = strescseq.buf;
  1972. strescseq.narg = 0;
  1973. strescseq.buf[strescseq.len] = '\0';
  1974. while(p && strescseq.narg < STR_ARG_SIZ)
  1975. strescseq.args[strescseq.narg++] = strsep(&p, ";");
  1976. }
  1977. void
  1978. strdump(void) {
  1979. int i;
  1980. uint c;
  1981. printf("ESC%c", strescseq.type);
  1982. for(i = 0; i < strescseq.len; i++) {
  1983. c = strescseq.buf[i] & 0xff;
  1984. if(c == '\0') {
  1985. return;
  1986. } else if(isprint(c)) {
  1987. putchar(c);
  1988. } else if(c == '\n') {
  1989. printf("(\\n)");
  1990. } else if(c == '\r') {
  1991. printf("(\\r)");
  1992. } else if(c == 0x1b) {
  1993. printf("(\\e)");
  1994. } else {
  1995. printf("(%02x)", c);
  1996. }
  1997. }
  1998. printf("ESC\\\n");
  1999. }
  2000. void
  2001. strreset(void) {
  2002. memset(&strescseq, 0, sizeof(strescseq));
  2003. }
  2004. void
  2005. tputtab(bool forward) {
  2006. uint x = term.c.x;
  2007. if(forward) {
  2008. if(x == term.col)
  2009. return;
  2010. for(++x; x < term.col && !term.tabs[x]; ++x)
  2011. /* nothing */ ;
  2012. } else {
  2013. if(x == 0)
  2014. return;
  2015. for(--x; x > 0 && !term.tabs[x]; --x)
  2016. /* nothing */ ;
  2017. }
  2018. tmoveto(x, term.c.y);
  2019. }
  2020. void
  2021. techo(char *buf, int len) {
  2022. for(; len > 0; buf++, len--) {
  2023. char c = *buf;
  2024. if(c == '\033') { /* escape */
  2025. tputc("^", 1);
  2026. tputc("[", 1);
  2027. } else if(c < '\x20') { /* control code */
  2028. if(c != '\n' && c != '\r' && c != '\t') {
  2029. c |= '\x40';
  2030. tputc("^", 1);
  2031. }
  2032. tputc(&c, 1);
  2033. } else {
  2034. break;
  2035. }
  2036. }
  2037. if(len)
  2038. tputc(buf, len);
  2039. }
  2040. void
  2041. tdeftran(char ascii) {
  2042. char c, (*bp)[2];
  2043. static char tbl[][2] = {
  2044. {'0', CS_GRAPHIC0}, {'1', CS_GRAPHIC1}, {'A', CS_UK},
  2045. {'B', CS_USA}, {'<', CS_MULTI}, {'K', CS_GER},
  2046. {'5', CS_FIN}, {'C', CS_FIN},
  2047. {0, 0}
  2048. };
  2049. for (bp = &tbl[0]; (c = (*bp)[0]) && c != ascii; ++bp)
  2050. /* nothing */;
  2051. if (c == 0)
  2052. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  2053. else
  2054. term.trantbl[term.icharset] = (*bp)[1];
  2055. }
  2056. void
  2057. tselcs(void) {
  2058. if (term.trantbl[term.charset] == CS_GRAPHIC0)
  2059. term.c.attr.mode |= ATTR_GFX;
  2060. else
  2061. term.c.attr.mode &= ~ATTR_GFX;
  2062. }
  2063. void
  2064. tputc(char *c, int len) {
  2065. uchar ascii = *c;
  2066. bool control = ascii < '\x20' || ascii == 0177;
  2067. long u8char;
  2068. int width;
  2069. if(len == 1) {
  2070. width = 1;
  2071. } else {
  2072. utf8decode(c, &u8char);
  2073. width = wcwidth(u8char);
  2074. }
  2075. if(iofd != -1) {
  2076. if(xwrite(iofd, c, len) < 0) {
  2077. fprintf(stderr, "Error writing in %s:%s\n",
  2078. opt_io, strerror(errno));
  2079. close(iofd);
  2080. iofd = -1;
  2081. }
  2082. }
  2083. /*
  2084. * STR sequences must be checked before anything else
  2085. * because it can use some control codes as part of the sequence.
  2086. */
  2087. if(term.esc & ESC_STR) {
  2088. switch(ascii) {
  2089. case '\033':
  2090. term.esc = ESC_START | ESC_STR_END;
  2091. break;
  2092. case '\a': /* backwards compatibility to xterm */
  2093. term.esc = 0;
  2094. strhandle();
  2095. break;
  2096. default:
  2097. if(strescseq.len + len < sizeof(strescseq.buf) - 1) {
  2098. memmove(&strescseq.buf[strescseq.len], c, len);
  2099. strescseq.len += len;
  2100. } else {
  2101. /*
  2102. * Here is a bug in terminals. If the user never sends
  2103. * some code to stop the str or esc command, then st
  2104. * will stop responding. But this is better than
  2105. * silently failing with unknown characters. At least
  2106. * then users will report back.
  2107. *
  2108. * In the case users ever get fixed, here is the code:
  2109. */
  2110. /*
  2111. * term.esc = 0;
  2112. * strhandle();
  2113. */
  2114. }
  2115. }
  2116. return;
  2117. }
  2118. /*
  2119. * Actions of control codes must be performed as soon they arrive
  2120. * because they can be embedded inside a control sequence, and
  2121. * they must not cause conflicts with sequences.
  2122. */
  2123. if(control) {
  2124. switch(ascii) {
  2125. case '\t': /* HT */
  2126. tputtab(1);
  2127. return;
  2128. case '\b': /* BS */
  2129. tmoveto(term.c.x-1, term.c.y);
  2130. return;
  2131. case '\r': /* CR */
  2132. tmoveto(0, term.c.y);
  2133. return;
  2134. case '\f': /* LF */
  2135. case '\v': /* VT */
  2136. case '\n': /* LF */
  2137. /* go to first col if the mode is set */
  2138. tnewline(IS_SET(MODE_CRLF));
  2139. return;
  2140. case '\a': /* BEL */
  2141. if(!(xw.state & WIN_FOCUSED))
  2142. xseturgency(1);
  2143. if (bellvolume)
  2144. XBell(xw.dpy, bellvolume);
  2145. return;
  2146. case '\033': /* ESC */
  2147. csireset();
  2148. term.esc = ESC_START;
  2149. return;
  2150. case '\016': /* SO */
  2151. term.charset = 0;
  2152. tselcs();
  2153. return;
  2154. case '\017': /* SI */
  2155. term.charset = 1;
  2156. tselcs();
  2157. return;
  2158. case '\032': /* SUB */
  2159. case '\030': /* CAN */
  2160. csireset();
  2161. return;
  2162. case '\005': /* ENQ (IGNORED) */
  2163. case '\000': /* NUL (IGNORED) */
  2164. case '\021': /* XON (IGNORED) */
  2165. case '\023': /* XOFF (IGNORED) */
  2166. case 0177: /* DEL (IGNORED) */
  2167. return;
  2168. }
  2169. } else if(term.esc & ESC_START) {
  2170. if(term.esc & ESC_CSI) {
  2171. csiescseq.buf[csiescseq.len++] = ascii;
  2172. if(BETWEEN(ascii, 0x40, 0x7E)
  2173. || csiescseq.len >= \
  2174. sizeof(csiescseq.buf)-1) {
  2175. term.esc = 0;
  2176. csiparse();
  2177. csihandle();
  2178. }
  2179. } else if(term.esc & ESC_STR_END) {
  2180. term.esc = 0;
  2181. if(ascii == '\\')
  2182. strhandle();
  2183. } else if(term.esc & ESC_ALTCHARSET) {
  2184. tdeftran(ascii);
  2185. tselcs();
  2186. term.esc = 0;
  2187. } else if(term.esc & ESC_TEST) {
  2188. if(ascii == '8') { /* DEC screen alignment test. */
  2189. char E[UTF_SIZ] = "E";
  2190. int x, y;
  2191. for(x = 0; x < term.col; ++x) {
  2192. for(y = 0; y < term.row; ++y)
  2193. tsetchar(E, &term.c.attr, x, y);
  2194. }
  2195. }
  2196. term.esc = 0;
  2197. } else {
  2198. switch(ascii) {
  2199. case '[':
  2200. term.esc |= ESC_CSI;
  2201. break;
  2202. case '#':
  2203. term.esc |= ESC_TEST;
  2204. break;
  2205. case 'P': /* DCS -- Device Control String */
  2206. case '_': /* APC -- Application Program Command */
  2207. case '^': /* PM -- Privacy Message */
  2208. case ']': /* OSC -- Operating System Command */
  2209. case 'k': /* old title set compatibility */
  2210. strreset();
  2211. strescseq.type = ascii;
  2212. term.esc |= ESC_STR;
  2213. break;
  2214. case '(': /* set primary charset G0 */
  2215. case ')': /* set secondary charset G1 */
  2216. case '*': /* set tertiary charset G2 */
  2217. case '+': /* set quaternary charset G3 */
  2218. term.icharset = ascii - '(';
  2219. term.esc |= ESC_ALTCHARSET;
  2220. break;
  2221. case 'D': /* IND -- Linefeed */
  2222. if(term.c.y == term.bot) {
  2223. tscrollup(term.top, 1);
  2224. } else {
  2225. tmoveto(term.c.x, term.c.y+1);
  2226. }
  2227. term.esc = 0;
  2228. break;
  2229. case 'E': /* NEL -- Next line */
  2230. tnewline(1); /* always go to first col */
  2231. term.esc = 0;
  2232. break;
  2233. case 'H': /* HTS -- Horizontal tab stop */
  2234. term.tabs[term.c.x] = 1;
  2235. term.esc = 0;
  2236. break;
  2237. case 'M': /* RI -- Reverse index */
  2238. if(term.c.y == term.top) {
  2239. tscrolldown(term.top, 1);
  2240. } else {
  2241. tmoveto(term.c.x, term.c.y-1);
  2242. }
  2243. term.esc = 0;
  2244. break;
  2245. case 'Z': /* DECID -- Identify Terminal */
  2246. ttywrite(VT102ID, sizeof(VT102ID) - 1);
  2247. term.esc = 0;
  2248. break;
  2249. case 'c': /* RIS -- Reset to inital state */
  2250. treset();
  2251. term.esc = 0;
  2252. xresettitle();
  2253. xloadcols();
  2254. break;
  2255. case '=': /* DECPAM -- Application keypad */
  2256. term.mode |= MODE_APPKEYPAD;
  2257. term.esc = 0;
  2258. break;
  2259. case '>': /* DECPNM -- Normal keypad */
  2260. term.mode &= ~MODE_APPKEYPAD;
  2261. term.esc = 0;
  2262. break;
  2263. case '7': /* DECSC -- Save Cursor */
  2264. tcursor(CURSOR_SAVE);
  2265. term.esc = 0;
  2266. break;
  2267. case '8': /* DECRC -- Restore Cursor */
  2268. tcursor(CURSOR_LOAD);
  2269. term.esc = 0;
  2270. break;
  2271. case '\\': /* ST -- Stop */
  2272. term.esc = 0;
  2273. break;
  2274. default:
  2275. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  2276. (uchar) ascii, isprint(ascii)? ascii:'.');
  2277. term.esc = 0;
  2278. }
  2279. }
  2280. /*
  2281. * All characters which form part of a sequence are not
  2282. * printed
  2283. */
  2284. return;
  2285. }
  2286. /*
  2287. * Display control codes only if we are in graphic mode
  2288. */
  2289. if(control && !(term.c.attr.mode & ATTR_GFX))
  2290. return;
  2291. if(sel.ob.x != -1 && BETWEEN(term.c.y, sel.ob.y, sel.oe.y))
  2292. selclear(NULL);
  2293. if(IS_SET(MODE_WRAP) && (term.c.state & CURSOR_WRAPNEXT)) {
  2294. term.line[term.c.y][term.c.x].mode |= ATTR_WRAP;
  2295. tnewline(1);
  2296. }
  2297. if(IS_SET(MODE_INSERT) && term.c.x+1 < term.col) {
  2298. memmove(&term.line[term.c.y][term.c.x+1],
  2299. &term.line[term.c.y][term.c.x],
  2300. (term.col - term.c.x - 1) * sizeof(Glyph));
  2301. }
  2302. if(term.c.x+width > term.col)
  2303. tnewline(1);
  2304. tsetchar(c, &term.c.attr, term.c.x, term.c.y);
  2305. if(width == 2) {
  2306. term.line[term.c.y][term.c.x].mode |= ATTR_WIDE;
  2307. if(term.c.x+1 < term.col) {
  2308. term.line[term.c.y][term.c.x+1].c[0] = '\0';
  2309. term.line[term.c.y][term.c.x+1].mode = ATTR_WDUMMY;
  2310. }
  2311. }
  2312. if(term.c.x+width < term.col) {
  2313. tmoveto(term.c.x+width, term.c.y);
  2314. } else {
  2315. term.c.state |= CURSOR_WRAPNEXT;
  2316. }
  2317. }
  2318. int
  2319. tresize(int col, int row) {
  2320. int i;
  2321. int minrow = MIN(row, term.row);
  2322. int mincol = MIN(col, term.col);
  2323. int slide = term.c.y - row + 1;
  2324. bool *bp;
  2325. Line *orig;
  2326. if(col < 1 || row < 1)
  2327. return 0;
  2328. /* free unneeded rows */
  2329. i = 0;
  2330. if(slide > 0) {
  2331. /*
  2332. * slide screen to keep cursor where we expect it -
  2333. * tscrollup would work here, but we can optimize to
  2334. * memmove because we're freeing the earlier lines
  2335. */
  2336. for(/* i = 0 */; i < slide; i++) {
  2337. free(term.line[i]);
  2338. free(term.alt[i]);
  2339. }
  2340. memmove(term.line, term.line + slide, row * sizeof(Line));
  2341. memmove(term.alt, term.alt + slide, row * sizeof(Line));
  2342. }
  2343. for(i += row; i < term.row; i++) {
  2344. free(term.line[i]);
  2345. free(term.alt[i]);
  2346. }
  2347. /* resize to new height */
  2348. term.line = xrealloc(term.line, row * sizeof(Line));
  2349. term.alt = xrealloc(term.alt, row * sizeof(Line));
  2350. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  2351. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  2352. /* resize each row to new width, zero-pad if needed */
  2353. for(i = 0; i < minrow; i++) {
  2354. term.dirty[i] = 1;
  2355. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  2356. term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
  2357. }
  2358. /* allocate any new rows */
  2359. for(/* i == minrow */; i < row; i++) {
  2360. term.dirty[i] = 1;
  2361. term.line[i] = xmalloc(col * sizeof(Glyph));
  2362. term.alt[i] = xmalloc(col * sizeof(Glyph));
  2363. }
  2364. if(col > term.col) {
  2365. bp = term.tabs + term.col;
  2366. memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
  2367. while(--bp > term.tabs && !*bp)
  2368. /* nothing */ ;
  2369. for(bp += tabspaces; bp < term.tabs + col; bp += tabspaces)
  2370. *bp = 1;
  2371. }
  2372. /* update terminal size */
  2373. term.col = col;
  2374. term.row = row;
  2375. /* reset scrolling region */
  2376. tsetscroll(0, row-1);
  2377. /* make use of the LIMIT in tmoveto */
  2378. tmoveto(term.c.x, term.c.y);
  2379. /* Clearing both screens */
  2380. orig = term.line;
  2381. do {
  2382. if(mincol < col && 0 < minrow) {
  2383. tclearregion(mincol, 0, col - 1, minrow - 1);
  2384. }
  2385. if(0 < col && minrow < row) {
  2386. tclearregion(0, minrow, col - 1, row - 1);
  2387. }
  2388. tswapscreen();
  2389. } while(orig != term.line);
  2390. return (slide > 0);
  2391. }
  2392. void
  2393. xresize(int col, int row) {
  2394. xw.tw = MAX(1, col * xw.cw);
  2395. xw.th = MAX(1, row * xw.ch);
  2396. XFreePixmap(xw.dpy, xw.buf);
  2397. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
  2398. DefaultDepth(xw.dpy, xw.scr));
  2399. XftDrawChange(xw.draw, xw.buf);
  2400. xclear(0, 0, xw.w, xw.h);
  2401. }
  2402. static inline ushort
  2403. sixd_to_16bit(int x) {
  2404. return x == 0 ? 0 : 0x3737 + 0x2828 * x;
  2405. }
  2406. void
  2407. xloadcols(void) {
  2408. int i, r, g, b;
  2409. XRenderColor color = { .alpha = 0xffff };
  2410. static bool loaded;
  2411. Colour *cp;
  2412. if(loaded) {
  2413. for (cp = dc.col; cp < dc.col + LEN(dc.col); ++cp)
  2414. XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
  2415. }
  2416. /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
  2417. for(i = 0; i < LEN(colorname); i++) {
  2418. if(!colorname[i])
  2419. continue;
  2420. if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, colorname[i], &dc.col[i])) {
  2421. die("Could not allocate color '%s'\n", colorname[i]);
  2422. }
  2423. }
  2424. /* load colors [16-255] ; same colors as xterm */
  2425. for(i = 16, r = 0; r < 6; r++) {
  2426. for(g = 0; g < 6; g++) {
  2427. for(b = 0; b < 6; b++) {
  2428. color.red = sixd_to_16bit(r);
  2429. color.green = sixd_to_16bit(g);
  2430. color.blue = sixd_to_16bit(b);
  2431. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &dc.col[i])) {
  2432. die("Could not allocate color %d\n", i);
  2433. }
  2434. i++;
  2435. }
  2436. }
  2437. }
  2438. for(r = 0; r < 24; r++, i++) {
  2439. color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
  2440. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color,
  2441. &dc.col[i])) {
  2442. die("Could not allocate color %d\n", i);
  2443. }
  2444. }
  2445. loaded = true;
  2446. }
  2447. int
  2448. xsetcolorname(int x, const char *name) {
  2449. XRenderColor color = { .alpha = 0xffff };
  2450. Colour colour;
  2451. if (x < 0 || x > LEN(colorname))
  2452. return -1;
  2453. if(!name) {
  2454. if(16 <= x && x < 16 + 216) {
  2455. int r = (x - 16) / 36, g = ((x - 16) % 36) / 6, b = (x - 16) % 6;
  2456. color.red = sixd_to_16bit(r);
  2457. color.green = sixd_to_16bit(g);
  2458. color.blue = sixd_to_16bit(b);
  2459. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &colour))
  2460. return 0; /* something went wrong */
  2461. dc.col[x] = colour;
  2462. return 1;
  2463. } else if (16 + 216 <= x && x < 256) {
  2464. color.red = color.green = color.blue = 0x0808 + 0x0a0a * (x - (16 + 216));
  2465. if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &colour))
  2466. return 0; /* something went wrong */
  2467. dc.col[x] = colour;
  2468. return 1;
  2469. } else {
  2470. name = colorname[x];
  2471. }
  2472. }
  2473. if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, &colour))
  2474. return 0;
  2475. dc.col[x] = colour;
  2476. return 1;
  2477. }
  2478. void
  2479. xtermclear(int col1, int row1, int col2, int row2) {
  2480. XftDrawRect(xw.draw,
  2481. &dc.col[IS_SET(MODE_REVERSE) ? defaultfg : defaultbg],
  2482. borderpx + col1 * xw.cw,
  2483. borderpx + row1 * xw.ch,
  2484. (col2-col1+1) * xw.cw,
  2485. (row2-row1+1) * xw.ch);
  2486. }
  2487. /*
  2488. * Absolute coordinates.
  2489. */
  2490. void
  2491. xclear(int x1, int y1, int x2, int y2) {
  2492. XftDrawRect(xw.draw,
  2493. &dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
  2494. x1, y1, x2-x1, y2-y1);
  2495. }
  2496. void
  2497. xhints(void) {
  2498. XClassHint class = {opt_class ? opt_class : termname, termname};
  2499. XWMHints wm = {.flags = InputHint, .input = 1};
  2500. XSizeHints *sizeh = NULL;
  2501. sizeh = XAllocSizeHints();
  2502. if(xw.isfixed == False) {
  2503. sizeh->flags = PSize | PResizeInc | PBaseSize;
  2504. sizeh->height = xw.h;
  2505. sizeh->width = xw.w;
  2506. sizeh->height_inc = xw.ch;
  2507. sizeh->width_inc = xw.cw;
  2508. sizeh->base_height = 2 * borderpx;
  2509. sizeh->base_width = 2 * borderpx;
  2510. } else {
  2511. sizeh->flags = PMaxSize | PMinSize;
  2512. sizeh->min_width = sizeh->max_width = xw.fw;
  2513. sizeh->min_height = sizeh->max_height = xw.fh;
  2514. }
  2515. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm, &class);
  2516. XFree(sizeh);
  2517. }
  2518. int
  2519. xloadfont(Font *f, FcPattern *pattern) {
  2520. FcPattern *match;
  2521. FcResult result;
  2522. match = FcFontMatch(NULL, pattern, &result);
  2523. if(!match)
  2524. return 1;
  2525. if(!(f->match = XftFontOpenPattern(xw.dpy, match))) {
  2526. FcPatternDestroy(match);
  2527. return 1;
  2528. }
  2529. f->set = NULL;
  2530. f->pattern = FcPatternDuplicate(pattern);
  2531. f->ascent = f->match->ascent;
  2532. f->descent = f->match->descent;
  2533. f->lbearing = 0;
  2534. f->rbearing = f->match->max_advance_width;
  2535. f->height = f->ascent + f->descent;
  2536. f->width = f->lbearing + f->rbearing;
  2537. return 0;
  2538. }
  2539. void
  2540. xloadfonts(char *fontstr, double fontsize) {
  2541. FcPattern *pattern;
  2542. FcResult r_sz, r_psz;
  2543. double fontval;
  2544. if(fontstr[0] == '-') {
  2545. pattern = XftXlfdParse(fontstr, False, False);
  2546. } else {
  2547. pattern = FcNameParse((FcChar8 *)fontstr);
  2548. }
  2549. if(!pattern)
  2550. die("st: can't open font %s\n", fontstr);
  2551. if(fontsize > 0) {
  2552. FcPatternDel(pattern, FC_PIXEL_SIZE);
  2553. FcPatternDel(pattern, FC_SIZE);
  2554. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
  2555. usedfontsize = fontsize;
  2556. } else {
  2557. r_psz = FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval);
  2558. r_sz = FcPatternGetDouble(pattern, FC_SIZE, 0, &fontval);
  2559. if(r_psz == FcResultMatch) {
  2560. usedfontsize = fontval;
  2561. } else if(r_sz == FcResultMatch) {
  2562. usedfontsize = -1;
  2563. } else {
  2564. /*
  2565. * Default font size is 12, if none given. This is to
  2566. * have a known usedfontsize value.
  2567. */
  2568. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
  2569. usedfontsize = 12;
  2570. }
  2571. }
  2572. FcConfigSubstitute(0, pattern, FcMatchPattern);
  2573. FcDefaultSubstitute(pattern);
  2574. if(xloadfont(&dc.font, pattern))
  2575. die("st: can't open font %s\n", fontstr);
  2576. if(usedfontsize < 0) {
  2577. FcPatternGetDouble(dc.font.match->pattern,
  2578. FC_PIXEL_SIZE, 0, &fontval);
  2579. usedfontsize = fontval;
  2580. }
  2581. /* Setting character width and height. */
  2582. xw.cw = CEIL(dc.font.width * cwscale);
  2583. xw.ch = CEIL(dc.font.height * chscale);
  2584. FcPatternDel(pattern, FC_SLANT);
  2585. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
  2586. if(xloadfont(&dc.ifont, pattern))
  2587. die("st: can't open font %s\n", fontstr);
  2588. FcPatternDel(pattern, FC_WEIGHT);
  2589. FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
  2590. if(xloadfont(&dc.ibfont, pattern))
  2591. die("st: can't open font %s\n", fontstr);
  2592. FcPatternDel(pattern, FC_SLANT);
  2593. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
  2594. if(xloadfont(&dc.bfont, pattern))
  2595. die("st: can't open font %s\n", fontstr);
  2596. FcPatternDestroy(pattern);
  2597. }
  2598. int
  2599. xloadfontset(Font *f) {
  2600. FcResult result;
  2601. if(!(f->set = FcFontSort(0, f->pattern, FcTrue, 0, &result)))
  2602. return 1;
  2603. return 0;
  2604. }
  2605. void
  2606. xunloadfont(Font *f) {
  2607. XftFontClose(xw.dpy, f->match);
  2608. FcPatternDestroy(f->pattern);
  2609. if(f->set)
  2610. FcFontSetDestroy(f->set);
  2611. }
  2612. void
  2613. xunloadfonts(void) {
  2614. int i;
  2615. /* Free the loaded fonts in the font cache. */
  2616. for(i = 0; i < frclen; i++) {
  2617. XftFontClose(xw.dpy, frc[i].font);
  2618. }
  2619. frclen = 0;
  2620. xunloadfont(&dc.font);
  2621. xunloadfont(&dc.bfont);
  2622. xunloadfont(&dc.ifont);
  2623. xunloadfont(&dc.ibfont);
  2624. }
  2625. void
  2626. xzoom(const Arg *arg) {
  2627. xunloadfonts();
  2628. xloadfonts(usedfont, usedfontsize + arg->i);
  2629. cresize(0, 0);
  2630. redraw(0);
  2631. }
  2632. void
  2633. xinit(void) {
  2634. XGCValues gcvalues;
  2635. Cursor cursor;
  2636. Window parent;
  2637. int sw, sh;
  2638. pid_t thispid = getpid();
  2639. if(!(xw.dpy = XOpenDisplay(NULL)))
  2640. die("Can't open display\n");
  2641. xw.scr = XDefaultScreen(xw.dpy);
  2642. xw.vis = XDefaultVisual(xw.dpy, xw.scr);
  2643. /* font */
  2644. if(!FcInit())
  2645. die("Could not init fontconfig.\n");
  2646. usedfont = (opt_font == NULL)? font : opt_font;
  2647. xloadfonts(usedfont, 0);
  2648. /* colors */
  2649. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  2650. xloadcols();
  2651. /* adjust fixed window geometry */
  2652. if(xw.isfixed) {
  2653. sw = DisplayWidth(xw.dpy, xw.scr);
  2654. sh = DisplayHeight(xw.dpy, xw.scr);
  2655. if(xw.fx < 0)
  2656. xw.fx = sw + xw.fx - xw.fw - 1;
  2657. if(xw.fy < 0)
  2658. xw.fy = sh + xw.fy - xw.fh - 1;
  2659. xw.h = xw.fh;
  2660. xw.w = xw.fw;
  2661. } else {
  2662. /* window - default size */
  2663. xw.h = 2 * borderpx + term.row * xw.ch;
  2664. xw.w = 2 * borderpx + term.col * xw.cw;
  2665. xw.fx = 0;
  2666. xw.fy = 0;
  2667. }
  2668. /* Events */
  2669. xw.attrs.background_pixel = dc.col[defaultbg].pixel;
  2670. xw.attrs.border_pixel = dc.col[defaultbg].pixel;
  2671. xw.attrs.bit_gravity = NorthWestGravity;
  2672. xw.attrs.event_mask = FocusChangeMask | KeyPressMask
  2673. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  2674. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  2675. xw.attrs.colormap = xw.cmap;
  2676. parent = opt_embed ? strtol(opt_embed, NULL, 0) : \
  2677. XRootWindow(xw.dpy, xw.scr);
  2678. xw.win = XCreateWindow(xw.dpy, parent, xw.fx, xw.fy,
  2679. xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
  2680. xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
  2681. | CWEventMask | CWColormap, &xw.attrs);
  2682. memset(&gcvalues, 0, sizeof(gcvalues));
  2683. gcvalues.graphics_exposures = False;
  2684. dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
  2685. &gcvalues);
  2686. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
  2687. DefaultDepth(xw.dpy, xw.scr));
  2688. XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
  2689. XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, xw.w, xw.h);
  2690. /* Xft rendering context */
  2691. xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
  2692. /* input methods */
  2693. if((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  2694. XSetLocaleModifiers("@im=local");
  2695. if((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  2696. XSetLocaleModifiers("@im=");
  2697. if((xw.xim = XOpenIM(xw.dpy,
  2698. NULL, NULL, NULL)) == NULL) {
  2699. die("XOpenIM failed. Could not open input"
  2700. " device.\n");
  2701. }
  2702. }
  2703. }
  2704. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  2705. | XIMStatusNothing, XNClientWindow, xw.win,
  2706. XNFocusWindow, xw.win, NULL);
  2707. if(xw.xic == NULL)
  2708. die("XCreateIC failed. Could not obtain input method.\n");
  2709. /* white cursor, black outline */
  2710. cursor = XCreateFontCursor(xw.dpy, XC_xterm);
  2711. XDefineCursor(xw.dpy, xw.win, cursor);
  2712. XRecolorCursor(xw.dpy, cursor,
  2713. &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
  2714. &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
  2715. xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
  2716. xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
  2717. xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False);
  2718. XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
  2719. xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False);
  2720. XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL, 32,
  2721. PropModeReplace, (unsigned char *)&thispid, 1);
  2722. xresettitle();
  2723. XMapWindow(xw.dpy, xw.win);
  2724. xhints();
  2725. XSync(xw.dpy, 0);
  2726. }
  2727. void
  2728. xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
  2729. int winx = borderpx + x * xw.cw, winy = borderpx + y * xw.ch,
  2730. width = charlen * xw.cw, xp, i;
  2731. int frcflags;
  2732. int u8fl, u8fblen, u8cblen, doesexist;
  2733. char *u8c, *u8fs;
  2734. long u8char;
  2735. Font *font = &dc.font;
  2736. FcResult fcres;
  2737. FcPattern *fcpattern, *fontpattern;
  2738. FcFontSet *fcsets[] = { NULL };
  2739. FcCharSet *fccharset;
  2740. Colour *fg, *bg, *temp, revfg, revbg, truefg, truebg;
  2741. XRenderColor colfg, colbg;
  2742. Rectangle r;
  2743. int oneatatime;
  2744. frcflags = FRC_NORMAL;
  2745. if(base.mode & ATTR_ITALIC) {
  2746. if(base.fg == defaultfg)
  2747. base.fg = defaultitalic;
  2748. font = &dc.ifont;
  2749. frcflags = FRC_ITALIC;
  2750. } else if((base.mode & ATTR_ITALIC) && (base.mode & ATTR_BOLD)) {
  2751. if(base.fg == defaultfg)
  2752. base.fg = defaultitalic;
  2753. font = &dc.ibfont;
  2754. frcflags = FRC_ITALICBOLD;
  2755. } else if(base.mode & ATTR_UNDERLINE) {
  2756. if(base.fg == defaultfg)
  2757. base.fg = defaultunderline;
  2758. }
  2759. if(IS_TRUECOL(base.fg)) {
  2760. colfg.alpha = 0xffff;
  2761. colfg.red = TRUERED(base.fg);
  2762. colfg.green = TRUEGREEN(base.fg);
  2763. colfg.blue = TRUEBLUE(base.fg);
  2764. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &truefg);
  2765. fg = &truefg;
  2766. } else {
  2767. fg = &dc.col[base.fg];
  2768. }
  2769. if(IS_TRUECOL(base.bg)) {
  2770. colbg.alpha = 0xffff;
  2771. colbg.green = TRUEGREEN(base.bg);
  2772. colbg.red = TRUERED(base.bg);
  2773. colbg.blue = TRUEBLUE(base.bg);
  2774. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &truebg);
  2775. bg = &truebg;
  2776. } else {
  2777. bg = &dc.col[base.bg];
  2778. }
  2779. if(base.mode & ATTR_BOLD) {
  2780. if(BETWEEN(base.fg, 0, 7)) {
  2781. /* basic system colors */
  2782. fg = &dc.col[base.fg + 8];
  2783. } else if(BETWEEN(base.fg, 16, 195)) {
  2784. /* 256 colors */
  2785. fg = &dc.col[base.fg + 36];
  2786. } else if(BETWEEN(base.fg, 232, 251)) {
  2787. /* greyscale */
  2788. fg = &dc.col[base.fg + 4];
  2789. }
  2790. /*
  2791. * Those ranges will not be brightened:
  2792. * 8 - 15 – bright system colors
  2793. * 196 - 231 – highest 256 color cube
  2794. * 252 - 255 – brightest colors in greyscale
  2795. */
  2796. font = &dc.bfont;
  2797. frcflags = FRC_BOLD;
  2798. }
  2799. if(IS_SET(MODE_REVERSE)) {
  2800. if(fg == &dc.col[defaultfg]) {
  2801. fg = &dc.col[defaultbg];
  2802. } else {
  2803. colfg.red = ~fg->color.red;
  2804. colfg.green = ~fg->color.green;
  2805. colfg.blue = ~fg->color.blue;
  2806. colfg.alpha = fg->color.alpha;
  2807. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
  2808. fg = &revfg;
  2809. }
  2810. if(bg == &dc.col[defaultbg]) {
  2811. bg = &dc.col[defaultfg];
  2812. } else {
  2813. colbg.red = ~bg->color.red;
  2814. colbg.green = ~bg->color.green;
  2815. colbg.blue = ~bg->color.blue;
  2816. colbg.alpha = bg->color.alpha;
  2817. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &revbg);
  2818. bg = &revbg;
  2819. }
  2820. }
  2821. if(base.mode & ATTR_REVERSE) {
  2822. temp = fg;
  2823. fg = bg;
  2824. bg = temp;
  2825. }
  2826. if(base.mode & ATTR_BLINK && term.mode & MODE_BLINK)
  2827. fg = bg;
  2828. /* Intelligent cleaning up of the borders. */
  2829. if(x == 0) {
  2830. xclear(0, (y == 0)? 0 : winy, borderpx,
  2831. winy + xw.ch + ((y >= term.row-1)? xw.h : 0));
  2832. }
  2833. if(x + charlen >= term.col) {
  2834. xclear(winx + width, (y == 0)? 0 : winy, xw.w,
  2835. ((y >= term.row-1)? xw.h : (winy + xw.ch)));
  2836. }
  2837. if(y == 0)
  2838. xclear(winx, 0, winx + width, borderpx);
  2839. if(y == term.row-1)
  2840. xclear(winx, winy + xw.ch, winx + width, xw.h);
  2841. /* Clean up the region we want to draw to. */
  2842. XftDrawRect(xw.draw, bg, winx, winy, width, xw.ch);
  2843. /* Set the clip region because Xft is sometimes dirty. */
  2844. r.x = 0;
  2845. r.y = 0;
  2846. r.height = xw.ch;
  2847. r.width = width;
  2848. XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1);
  2849. for(xp = winx; bytelen > 0;) {
  2850. /*
  2851. * Search for the range in the to be printed string of glyphs
  2852. * that are in the main font. Then print that range. If
  2853. * some glyph is found that is not in the font, do the
  2854. * fallback dance.
  2855. */
  2856. u8fs = s;
  2857. u8fblen = 0;
  2858. u8fl = 0;
  2859. oneatatime = font->width != xw.cw;
  2860. for(;;) {
  2861. u8c = s;
  2862. u8cblen = utf8decode(s, &u8char);
  2863. s += u8cblen;
  2864. bytelen -= u8cblen;
  2865. doesexist = XftCharExists(xw.dpy, font->match, u8char);
  2866. if(oneatatime || !doesexist || bytelen <= 0) {
  2867. if(oneatatime || bytelen <= 0) {
  2868. if(doesexist) {
  2869. u8fl++;
  2870. u8fblen += u8cblen;
  2871. }
  2872. }
  2873. if(u8fl > 0) {
  2874. XftDrawStringUtf8(xw.draw, fg,
  2875. font->match, xp,
  2876. winy + font->ascent,
  2877. (FcChar8 *)u8fs,
  2878. u8fblen);
  2879. xp += xw.cw * u8fl;
  2880. }
  2881. break;
  2882. }
  2883. u8fl++;
  2884. u8fblen += u8cblen;
  2885. }
  2886. if(doesexist) {
  2887. if (oneatatime)
  2888. continue;
  2889. break;
  2890. }
  2891. /* Search the font cache. */
  2892. for(i = 0; i < frclen; i++) {
  2893. if(XftCharExists(xw.dpy, frc[i].font, u8char)
  2894. && frc[i].flags == frcflags) {
  2895. break;
  2896. }
  2897. }
  2898. /* Nothing was found. */
  2899. if(i >= frclen) {
  2900. if(!font->set)
  2901. xloadfontset(font);
  2902. fcsets[0] = font->set;
  2903. /*
  2904. * Nothing was found in the cache. Now use
  2905. * some dozen of Fontconfig calls to get the
  2906. * font for one single character.
  2907. */
  2908. fcpattern = FcPatternDuplicate(font->pattern);
  2909. fccharset = FcCharSetCreate();
  2910. FcCharSetAddChar(fccharset, u8char);
  2911. FcPatternAddCharSet(fcpattern, FC_CHARSET,
  2912. fccharset);
  2913. FcPatternAddBool(fcpattern, FC_SCALABLE,
  2914. FcTrue);
  2915. FcConfigSubstitute(0, fcpattern,
  2916. FcMatchPattern);
  2917. FcDefaultSubstitute(fcpattern);
  2918. fontpattern = FcFontSetMatch(0, fcsets,
  2919. FcTrue, fcpattern, &fcres);
  2920. /*
  2921. * Overwrite or create the new cache entry.
  2922. */
  2923. if(frclen >= LEN(frc)) {
  2924. frclen = LEN(frc) - 1;
  2925. XftFontClose(xw.dpy, frc[frclen].font);
  2926. }
  2927. frc[frclen].font = XftFontOpenPattern(xw.dpy,
  2928. fontpattern);
  2929. frc[frclen].flags = frcflags;
  2930. i = frclen;
  2931. frclen++;
  2932. FcPatternDestroy(fcpattern);
  2933. FcCharSetDestroy(fccharset);
  2934. }
  2935. XftDrawStringUtf8(xw.draw, fg, frc[i].font,
  2936. xp, winy + frc[i].font->ascent,
  2937. (FcChar8 *)u8c, u8cblen);
  2938. xp += xw.cw * wcwidth(u8char);
  2939. }
  2940. /*
  2941. XftDrawStringUtf8(xw.draw, fg, font->set, winx,
  2942. winy + font->ascent, (FcChar8 *)s, bytelen);
  2943. */
  2944. if(base.mode & ATTR_UNDERLINE) {
  2945. XftDrawRect(xw.draw, fg, winx, winy + font->ascent + 1,
  2946. width, 1);
  2947. }
  2948. /* Reset clip to none. */
  2949. XftDrawSetClip(xw.draw, 0);
  2950. }
  2951. void
  2952. xdrawcursor(void) {
  2953. static int oldx = 0, oldy = 0;
  2954. int sl, width, curx;
  2955. Glyph g = {{' '}, ATTR_NULL, defaultbg, defaultcs};
  2956. LIMIT(oldx, 0, term.col-1);
  2957. LIMIT(oldy, 0, term.row-1);
  2958. curx = term.c.x;
  2959. /* adjust position if in dummy */
  2960. if(term.line[oldy][oldx].mode & ATTR_WDUMMY)
  2961. oldx--;
  2962. if(term.line[term.c.y][curx].mode & ATTR_WDUMMY)
  2963. curx--;
  2964. memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
  2965. /* remove the old cursor */
  2966. sl = utf8size(term.line[oldy][oldx].c);
  2967. width = (term.line[oldy][oldx].mode & ATTR_WIDE)? 2 : 1;
  2968. xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx,
  2969. oldy, width, sl);
  2970. /* draw the new one */
  2971. if(!(IS_SET(MODE_HIDE))) {
  2972. if(xw.state & WIN_FOCUSED) {
  2973. if(IS_SET(MODE_REVERSE)) {
  2974. g.mode |= ATTR_REVERSE;
  2975. g.fg = defaultcs;
  2976. g.bg = defaultfg;
  2977. }
  2978. sl = utf8size(g.c);
  2979. width = (term.line[term.c.y][curx].mode & ATTR_WIDE)\
  2980. ? 2 : 1;
  2981. xdraws(g.c, g, term.c.x, term.c.y, width, sl);
  2982. } else {
  2983. XftDrawRect(xw.draw, &dc.col[defaultcs],
  2984. borderpx + curx * xw.cw,
  2985. borderpx + term.c.y * xw.ch,
  2986. xw.cw - 1, 1);
  2987. XftDrawRect(xw.draw, &dc.col[defaultcs],
  2988. borderpx + curx * xw.cw,
  2989. borderpx + term.c.y * xw.ch,
  2990. 1, xw.ch - 1);
  2991. XftDrawRect(xw.draw, &dc.col[defaultcs],
  2992. borderpx + (curx + 1) * xw.cw - 1,
  2993. borderpx + term.c.y * xw.ch,
  2994. 1, xw.ch - 1);
  2995. XftDrawRect(xw.draw, &dc.col[defaultcs],
  2996. borderpx + curx * xw.cw,
  2997. borderpx + (term.c.y + 1) * xw.ch - 1,
  2998. xw.cw, 1);
  2999. }
  3000. oldx = curx, oldy = term.c.y;
  3001. }
  3002. }
  3003. void
  3004. xsettitle(char *p) {
  3005. XTextProperty prop;
  3006. Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
  3007. &prop);
  3008. XSetWMName(xw.dpy, xw.win, &prop);
  3009. XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmname);
  3010. XFree(prop.value);
  3011. }
  3012. void
  3013. xresettitle(void) {
  3014. xsettitle(opt_title ? opt_title : "st");
  3015. }
  3016. void
  3017. redraw(int timeout) {
  3018. struct timespec tv = {0, timeout * 1000};
  3019. tfulldirt();
  3020. draw();
  3021. if(timeout > 0) {
  3022. nanosleep(&tv, NULL);
  3023. XSync(xw.dpy, False); /* necessary for a good tput flash */
  3024. }
  3025. }
  3026. void
  3027. draw(void) {
  3028. drawregion(0, 0, term.col, term.row);
  3029. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.w,
  3030. xw.h, 0, 0);
  3031. XSetForeground(xw.dpy, dc.gc,
  3032. dc.col[IS_SET(MODE_REVERSE)?
  3033. defaultfg : defaultbg].pixel);
  3034. }
  3035. void
  3036. drawregion(int x1, int y1, int x2, int y2) {
  3037. int ic, ib, x, y, ox, sl;
  3038. Glyph base, new;
  3039. char buf[DRAW_BUF_SIZ];
  3040. bool ena_sel = sel.ob.x != -1;
  3041. long u8char;
  3042. if(sel.alt ^ IS_SET(MODE_ALTSCREEN))
  3043. ena_sel = 0;
  3044. if(!(xw.state & WIN_VISIBLE))
  3045. return;
  3046. for(y = y1; y < y2; y++) {
  3047. if(!term.dirty[y])
  3048. continue;
  3049. xtermclear(0, y, term.col, y);
  3050. term.dirty[y] = 0;
  3051. base = term.line[y][0];
  3052. ic = ib = ox = 0;
  3053. for(x = x1; x < x2; x++) {
  3054. new = term.line[y][x];
  3055. if(new.mode == ATTR_WDUMMY)
  3056. continue;
  3057. if(ena_sel && selected(x, y))
  3058. new.mode ^= ATTR_REVERSE;
  3059. if(ib > 0 && (ATTRCMP(base, new)
  3060. || ib >= DRAW_BUF_SIZ-UTF_SIZ)) {
  3061. xdraws(buf, base, ox, y, ic, ib);
  3062. ic = ib = 0;
  3063. }
  3064. if(ib == 0) {
  3065. ox = x;
  3066. base = new;
  3067. }
  3068. sl = utf8decode(new.c, &u8char);
  3069. memcpy(buf+ib, new.c, sl);
  3070. ib += sl;
  3071. ic += (new.mode & ATTR_WIDE)? 2 : 1;
  3072. }
  3073. if(ib > 0)
  3074. xdraws(buf, base, ox, y, ic, ib);
  3075. }
  3076. xdrawcursor();
  3077. }
  3078. void
  3079. expose(XEvent *ev) {
  3080. XExposeEvent *e = &ev->xexpose;
  3081. if(xw.state & WIN_REDRAW) {
  3082. if(!e->count)
  3083. xw.state &= ~WIN_REDRAW;
  3084. }
  3085. redraw(0);
  3086. }
  3087. void
  3088. visibility(XEvent *ev) {
  3089. XVisibilityEvent *e = &ev->xvisibility;
  3090. if(e->state == VisibilityFullyObscured) {
  3091. xw.state &= ~WIN_VISIBLE;
  3092. } else if(!(xw.state & WIN_VISIBLE)) {
  3093. /* need a full redraw for next Expose, not just a buf copy */
  3094. xw.state |= WIN_VISIBLE | WIN_REDRAW;
  3095. }
  3096. }
  3097. void
  3098. unmap(XEvent *ev) {
  3099. xw.state &= ~WIN_VISIBLE;
  3100. }
  3101. void
  3102. xsetpointermotion(int set) {
  3103. MODBIT(xw.attrs.event_mask, set, PointerMotionMask);
  3104. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs);
  3105. }
  3106. void
  3107. xseturgency(int add) {
  3108. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  3109. h->flags = add ? (h->flags | XUrgencyHint) : (h->flags & ~XUrgencyHint);
  3110. XSetWMHints(xw.dpy, xw.win, h);
  3111. XFree(h);
  3112. }
  3113. void
  3114. focus(XEvent *ev) {
  3115. XFocusChangeEvent *e = &ev->xfocus;
  3116. if(e->mode == NotifyGrab)
  3117. return;
  3118. if(ev->type == FocusIn) {
  3119. XSetICFocus(xw.xic);
  3120. xw.state |= WIN_FOCUSED;
  3121. xseturgency(0);
  3122. if(IS_SET(MODE_FOCUS))
  3123. ttywrite("\033[I", 3);
  3124. } else {
  3125. XUnsetICFocus(xw.xic);
  3126. xw.state &= ~WIN_FOCUSED;
  3127. if(IS_SET(MODE_FOCUS))
  3128. ttywrite("\033[O", 3);
  3129. }
  3130. }
  3131. static inline bool
  3132. match(uint mask, uint state) {
  3133. return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
  3134. }
  3135. void
  3136. numlock(const Arg *dummy) {
  3137. term.numlock ^= 1;
  3138. }
  3139. char*
  3140. kmap(KeySym k, uint state) {
  3141. Key *kp;
  3142. int i;
  3143. /* Check for mapped keys out of X11 function keys. */
  3144. for(i = 0; i < LEN(mappedkeys); i++) {
  3145. if(mappedkeys[i] == k)
  3146. break;
  3147. }
  3148. if(i == LEN(mappedkeys)) {
  3149. if((k & 0xFFFF) < 0xFD00)
  3150. return NULL;
  3151. }
  3152. for(kp = key; kp < key + LEN(key); kp++) {
  3153. if(kp->k != k)
  3154. continue;
  3155. if(!match(kp->mask, state))
  3156. continue;
  3157. if(IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0)
  3158. continue;
  3159. if(term.numlock && kp->appkey == 2)
  3160. continue;
  3161. if(IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0)
  3162. continue;
  3163. if(IS_SET(MODE_CRLF) ? kp->crlf < 0 : kp->crlf > 0)
  3164. continue;
  3165. return kp->s;
  3166. }
  3167. return NULL;
  3168. }
  3169. void
  3170. kpress(XEvent *ev) {
  3171. XKeyEvent *e = &ev->xkey;
  3172. KeySym ksym;
  3173. char buf[32], *customkey;
  3174. int len;
  3175. long c;
  3176. Status status;
  3177. Shortcut *bp;
  3178. if(IS_SET(MODE_KBDLOCK))
  3179. return;
  3180. len = XmbLookupString(xw.xic, e, buf, sizeof buf, &ksym, &status);
  3181. /* 1. shortcuts */
  3182. for(bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
  3183. if(ksym == bp->keysym && match(bp->mod, e->state)) {
  3184. bp->func(&(bp->arg));
  3185. return;
  3186. }
  3187. }
  3188. /* 2. custom keys from config.h */
  3189. if((customkey = kmap(ksym, e->state))) {
  3190. ttysend(customkey, strlen(customkey));
  3191. return;
  3192. }
  3193. /* 3. composed string from input method */
  3194. if(len == 0)
  3195. return;
  3196. if(len == 1 && e->state & Mod1Mask) {
  3197. if(IS_SET(MODE_8BIT)) {
  3198. if(*buf < 0177) {
  3199. c = *buf | 0x80;
  3200. len = utf8encode(&c, buf);
  3201. }
  3202. } else {
  3203. buf[1] = buf[0];
  3204. buf[0] = '\033';
  3205. len = 2;
  3206. }
  3207. }
  3208. ttysend(buf, len);
  3209. }
  3210. void
  3211. cmessage(XEvent *e) {
  3212. /*
  3213. * See xembed specs
  3214. * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
  3215. */
  3216. if(e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
  3217. if(e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
  3218. xw.state |= WIN_FOCUSED;
  3219. xseturgency(0);
  3220. } else if(e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
  3221. xw.state &= ~WIN_FOCUSED;
  3222. }
  3223. } else if(e->xclient.data.l[0] == xw.wmdeletewin) {
  3224. /* Send SIGHUP to shell */
  3225. kill(pid, SIGHUP);
  3226. exit(EXIT_SUCCESS);
  3227. }
  3228. }
  3229. void
  3230. cresize(int width, int height) {
  3231. int col, row;
  3232. if(width != 0)
  3233. xw.w = width;
  3234. if(height != 0)
  3235. xw.h = height;
  3236. col = (xw.w - 2 * borderpx) / xw.cw;
  3237. row = (xw.h - 2 * borderpx) / xw.ch;
  3238. tresize(col, row);
  3239. xresize(col, row);
  3240. ttyresize();
  3241. }
  3242. void
  3243. resize(XEvent *e) {
  3244. if(e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  3245. return;
  3246. cresize(e->xconfigure.width, e->xconfigure.height);
  3247. }
  3248. void
  3249. run(void) {
  3250. XEvent ev;
  3251. int w = xw.w, h = xw.h;
  3252. fd_set rfd;
  3253. int xfd = XConnectionNumber(xw.dpy), xev, blinkset = 0, dodraw = 0;
  3254. struct timeval drawtimeout, *tv = NULL, now, last, lastblink;
  3255. /* Waiting for window mapping */
  3256. while(1) {
  3257. XNextEvent(xw.dpy, &ev);
  3258. if(ev.type == ConfigureNotify) {
  3259. w = ev.xconfigure.width;
  3260. h = ev.xconfigure.height;
  3261. } else if(ev.type == MapNotify) {
  3262. break;
  3263. }
  3264. }
  3265. ttynew();
  3266. if(!xw.isfixed)
  3267. cresize(w, h);
  3268. else
  3269. cresize(xw.fw, xw.fh);
  3270. gettimeofday(&lastblink, NULL);
  3271. gettimeofday(&last, NULL);
  3272. for(xev = actionfps;;) {
  3273. long deltatime;
  3274. FD_ZERO(&rfd);
  3275. FD_SET(cmdfd, &rfd);
  3276. FD_SET(xfd, &rfd);
  3277. if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv) < 0) {
  3278. if(errno == EINTR)
  3279. continue;
  3280. die("select failed: %s\n", SERRNO);
  3281. }
  3282. if(FD_ISSET(cmdfd, &rfd)) {
  3283. ttyread();
  3284. if(blinktimeout) {
  3285. blinkset = tattrset(ATTR_BLINK);
  3286. if(!blinkset)
  3287. MODBIT(term.mode, 0, MODE_BLINK);
  3288. }
  3289. }
  3290. if(FD_ISSET(xfd, &rfd))
  3291. xev = actionfps;
  3292. gettimeofday(&now, NULL);
  3293. drawtimeout.tv_sec = 0;
  3294. drawtimeout.tv_usec = (1000/xfps) * 1000;
  3295. tv = &drawtimeout;
  3296. dodraw = 0;
  3297. if(blinktimeout && TIMEDIFF(now, lastblink) > blinktimeout) {
  3298. tsetdirtattr(ATTR_BLINK);
  3299. term.mode ^= MODE_BLINK;
  3300. gettimeofday(&lastblink, NULL);
  3301. dodraw = 1;
  3302. }
  3303. deltatime = TIMEDIFF(now, last);
  3304. if(deltatime > (xev? (1000/xfps) : (1000/actionfps))
  3305. || deltatime < 0) {
  3306. dodraw = 1;
  3307. last = now;
  3308. }
  3309. if(dodraw) {
  3310. while(XPending(xw.dpy)) {
  3311. XNextEvent(xw.dpy, &ev);
  3312. if(XFilterEvent(&ev, None))
  3313. continue;
  3314. if(handler[ev.type])
  3315. (handler[ev.type])(&ev);
  3316. }
  3317. draw();
  3318. XFlush(xw.dpy);
  3319. if(xev && !FD_ISSET(xfd, &rfd))
  3320. xev--;
  3321. if(!FD_ISSET(cmdfd, &rfd) && !FD_ISSET(xfd, &rfd)) {
  3322. if(blinkset) {
  3323. if(TIMEDIFF(now, lastblink) \
  3324. > blinktimeout) {
  3325. drawtimeout.tv_usec = 1;
  3326. } else {
  3327. drawtimeout.tv_usec = (1000 * \
  3328. (blinktimeout - \
  3329. TIMEDIFF(now,
  3330. lastblink)));
  3331. }
  3332. } else {
  3333. tv = NULL;
  3334. }
  3335. }
  3336. }
  3337. }
  3338. }
  3339. void
  3340. usage(void) {
  3341. die("%s " VERSION " (c) 2010-2013 st engineers\n" \
  3342. "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
  3343. " [-t title] [-w windowid] [-e command ...]\n", argv0);
  3344. }
  3345. int
  3346. main(int argc, char *argv[]) {
  3347. int bitm, xr, yr;
  3348. uint wr, hr;
  3349. char *titles;
  3350. xw.fw = xw.fh = xw.fx = xw.fy = 0;
  3351. xw.isfixed = False;
  3352. ARGBEGIN {
  3353. case 'a':
  3354. allowaltscreen = false;
  3355. break;
  3356. case 'c':
  3357. opt_class = EARGF(usage());
  3358. break;
  3359. case 'e':
  3360. /* eat all remaining arguments */
  3361. if(argc > 1) {
  3362. opt_cmd = &argv[1];
  3363. if(argv[1] != NULL && opt_title == NULL) {
  3364. titles = xstrdup(argv[1]);
  3365. opt_title = basename(titles);
  3366. }
  3367. }
  3368. goto run;
  3369. case 'f':
  3370. opt_font = EARGF(usage());
  3371. break;
  3372. case 'g':
  3373. bitm = XParseGeometry(EARGF(usage()), &xr, &yr, &wr, &hr);
  3374. if(bitm & XValue)
  3375. xw.fx = xr;
  3376. if(bitm & YValue)
  3377. xw.fy = yr;
  3378. if(bitm & WidthValue)
  3379. xw.fw = (int)wr;
  3380. if(bitm & HeightValue)
  3381. xw.fh = (int)hr;
  3382. if(bitm & XNegative && xw.fx == 0)
  3383. xw.fx = -1;
  3384. if(bitm & YNegative && xw.fy == 0)
  3385. xw.fy = -1;
  3386. if(xw.fh != 0 && xw.fw != 0)
  3387. xw.isfixed = True;
  3388. break;
  3389. case 'o':
  3390. opt_io = EARGF(usage());
  3391. break;
  3392. case 't':
  3393. opt_title = EARGF(usage());
  3394. break;
  3395. case 'w':
  3396. opt_embed = EARGF(usage());
  3397. break;
  3398. case 'v':
  3399. default:
  3400. usage();
  3401. } ARGEND;
  3402. run:
  3403. setlocale(LC_CTYPE, "");
  3404. XSetLocaleModifiers("");
  3405. tnew(80, 24);
  3406. xinit();
  3407. selinit();
  3408. run();
  3409. return 0;
  3410. }