termbox.h 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310
  1. /*
  2. MIT License
  3. Copyright (c) 2010-2020 nsf <no.smile.face@gmail.com>
  4. 2015-2023 Adam Saponara <as@php.net>
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21. #ifndef __TERMBOX_H
  22. #define __TERMBOX_H
  23. #ifndef _XOPEN_SOURCE
  24. #define _XOPEN_SOURCE
  25. #endif
  26. #ifndef _DEFAULT_SOURCE
  27. #define _DEFAULT_SOURCE
  28. #endif
  29. #include <errno.h>
  30. #include <fcntl.h>
  31. #include <limits.h>
  32. #include <signal.h>
  33. #include <stdarg.h>
  34. #include <stdint.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <sys/ioctl.h>
  39. #include <sys/select.h>
  40. #include <sys/stat.h>
  41. #include <sys/time.h>
  42. #include <sys/types.h>
  43. #include <termios.h>
  44. #include <unistd.h>
  45. #include <wchar.h>
  46. #ifdef PATH_MAX
  47. #define TB_PATH_MAX PATH_MAX
  48. #else
  49. #define TB_PATH_MAX 4096
  50. #endif
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. // __ffi_start
  55. #define TB_VERSION_STR "2.2.0-dev"
  56. #if defined(TB_LIB_OPTS) || 0 // __tb_lib_opts
  57. // Ensure consistent compile-time options when using as a library
  58. #undef TB_OPT_TRUECOLOR
  59. #undef TB_OPT_EGC
  60. #undef TB_OPT_PRINTF_BUF
  61. #undef TB_OPT_READ_BUF
  62. #define TB_OPT_TRUECOLOR
  63. #define TB_OPT_EGC
  64. #endif
  65. /* ASCII key constants (tb_event.key) */
  66. #define TB_KEY_CTRL_TILDE 0x00
  67. #define TB_KEY_CTRL_2 0x00 /* clash with 'CTRL_TILDE' */
  68. #define TB_KEY_CTRL_A 0x01
  69. #define TB_KEY_CTRL_B 0x02
  70. #define TB_KEY_CTRL_C 0x03
  71. #define TB_KEY_CTRL_D 0x04
  72. #define TB_KEY_CTRL_E 0x05
  73. #define TB_KEY_CTRL_F 0x06
  74. #define TB_KEY_CTRL_G 0x07
  75. #define TB_KEY_BACKSPACE 0x08
  76. #define TB_KEY_CTRL_H 0x08 /* clash with 'CTRL_BACKSPACE' */
  77. #define TB_KEY_TAB 0x09
  78. #define TB_KEY_CTRL_I 0x09 /* clash with 'TAB' */
  79. #define TB_KEY_CTRL_J 0x0a
  80. #define TB_KEY_CTRL_K 0x0b
  81. #define TB_KEY_CTRL_L 0x0c
  82. #define TB_KEY_ENTER 0x0d
  83. #define TB_KEY_CTRL_M 0x0d /* clash with 'ENTER' */
  84. #define TB_KEY_CTRL_N 0x0e
  85. #define TB_KEY_CTRL_O 0x0f
  86. #define TB_KEY_CTRL_P 0x10
  87. #define TB_KEY_CTRL_Q 0x11
  88. #define TB_KEY_CTRL_R 0x12
  89. #define TB_KEY_CTRL_S 0x13
  90. #define TB_KEY_CTRL_T 0x14
  91. #define TB_KEY_CTRL_U 0x15
  92. #define TB_KEY_CTRL_V 0x16
  93. #define TB_KEY_CTRL_W 0x17
  94. #define TB_KEY_CTRL_X 0x18
  95. #define TB_KEY_CTRL_Y 0x19
  96. #define TB_KEY_CTRL_Z 0x1a
  97. #define TB_KEY_ESC 0x1b
  98. #define TB_KEY_CTRL_LSQ_BRACKET 0x1b /* clash with 'ESC' */
  99. #define TB_KEY_CTRL_3 0x1b /* clash with 'ESC' */
  100. #define TB_KEY_CTRL_4 0x1c
  101. #define TB_KEY_CTRL_BACKSLASH 0x1c /* clash with 'CTRL_4' */
  102. #define TB_KEY_CTRL_5 0x1d
  103. #define TB_KEY_CTRL_RSQ_BRACKET 0x1d /* clash with 'CTRL_5' */
  104. #define TB_KEY_CTRL_6 0x1e
  105. #define TB_KEY_CTRL_7 0x1f
  106. #define TB_KEY_CTRL_SLASH 0x1f /* clash with 'CTRL_7' */
  107. #define TB_KEY_CTRL_UNDERSCORE 0x1f /* clash with 'CTRL_7' */
  108. #define TB_KEY_SPACE 0x20
  109. #define TB_KEY_BACKSPACE2 0x7f
  110. #define TB_KEY_CTRL_8 0x7f /* clash with 'BACKSPACE2' */
  111. #define tb_key_i(i) 0xffff - (i)
  112. /* Terminal-dependent key constants (tb_event.key) and terminfo capabilities */
  113. /* BEGIN codegen h */
  114. /* Produced by ./codegen.sh on Sun, 19 Sep 2021 01:02:02 +0000 */
  115. #define TB_KEY_F1 (0xffff - 0)
  116. #define TB_KEY_F2 (0xffff - 1)
  117. #define TB_KEY_F3 (0xffff - 2)
  118. #define TB_KEY_F4 (0xffff - 3)
  119. #define TB_KEY_F5 (0xffff - 4)
  120. #define TB_KEY_F6 (0xffff - 5)
  121. #define TB_KEY_F7 (0xffff - 6)
  122. #define TB_KEY_F8 (0xffff - 7)
  123. #define TB_KEY_F9 (0xffff - 8)
  124. #define TB_KEY_F10 (0xffff - 9)
  125. #define TB_KEY_F11 (0xffff - 10)
  126. #define TB_KEY_F12 (0xffff - 11)
  127. #define TB_KEY_INSERT (0xffff - 12)
  128. #define TB_KEY_DELETE (0xffff - 13)
  129. #define TB_KEY_HOME (0xffff - 14)
  130. #define TB_KEY_END (0xffff - 15)
  131. #define TB_KEY_PGUP (0xffff - 16)
  132. #define TB_KEY_PGDN (0xffff - 17)
  133. #define TB_KEY_ARROW_UP (0xffff - 18)
  134. #define TB_KEY_ARROW_DOWN (0xffff - 19)
  135. #define TB_KEY_ARROW_LEFT (0xffff - 20)
  136. #define TB_KEY_ARROW_RIGHT (0xffff - 21)
  137. #define TB_KEY_BACK_TAB (0xffff - 22)
  138. #define TB_KEY_MOUSE_LEFT (0xffff - 23)
  139. #define TB_KEY_MOUSE_RIGHT (0xffff - 24)
  140. #define TB_KEY_MOUSE_MIDDLE (0xffff - 25)
  141. #define TB_KEY_MOUSE_RELEASE (0xffff - 26)
  142. #define TB_KEY_MOUSE_WHEEL_UP (0xffff - 27)
  143. #define TB_KEY_MOUSE_WHEEL_DOWN (0xffff - 28)
  144. #define TB_CAP_F1 0
  145. #define TB_CAP_F2 1
  146. #define TB_CAP_F3 2
  147. #define TB_CAP_F4 3
  148. #define TB_CAP_F5 4
  149. #define TB_CAP_F6 5
  150. #define TB_CAP_F7 6
  151. #define TB_CAP_F8 7
  152. #define TB_CAP_F9 8
  153. #define TB_CAP_F10 9
  154. #define TB_CAP_F11 10
  155. #define TB_CAP_F12 11
  156. #define TB_CAP_INSERT 12
  157. #define TB_CAP_DELETE 13
  158. #define TB_CAP_HOME 14
  159. #define TB_CAP_END 15
  160. #define TB_CAP_PGUP 16
  161. #define TB_CAP_PGDN 17
  162. #define TB_CAP_ARROW_UP 18
  163. #define TB_CAP_ARROW_DOWN 19
  164. #define TB_CAP_ARROW_LEFT 20
  165. #define TB_CAP_ARROW_RIGHT 21
  166. #define TB_CAP_BACK_TAB 22
  167. #define TB_CAP__COUNT_KEYS 23
  168. #define TB_CAP_ENTER_CA 23
  169. #define TB_CAP_EXIT_CA 24
  170. #define TB_CAP_SHOW_CURSOR 25
  171. #define TB_CAP_HIDE_CURSOR 26
  172. #define TB_CAP_CLEAR_SCREEN 27
  173. #define TB_CAP_SGR0 28
  174. #define TB_CAP_UNDERLINE 29
  175. #define TB_CAP_BOLD 30
  176. #define TB_CAP_BLINK 31
  177. #define TB_CAP_ITALIC 32
  178. #define TB_CAP_REVERSE 33
  179. #define TB_CAP_ENTER_KEYPAD 34
  180. #define TB_CAP_EXIT_KEYPAD 35
  181. #define TB_CAP__COUNT 36
  182. /* END codegen h */
  183. /* Some hard-coded caps */
  184. #define TB_HARDCAP_ENTER_MOUSE "\x1b[?1000h\x1b[?1002h\x1b[?1015h\x1b[?1006h"
  185. #define TB_HARDCAP_EXIT_MOUSE "\x1b[?1006l\x1b[?1015l\x1b[?1002l\x1b[?1000l"
  186. /* Colors (numeric) and attributes (bitwise) (tb_cell.fg, tb_cell.bg) */
  187. #define TB_DEFAULT 0x0000
  188. #define TB_BLACK 0x0001
  189. #define TB_RED 0x0002
  190. #define TB_GREEN 0x0003
  191. #define TB_YELLOW 0x0004
  192. #define TB_BLUE 0x0005
  193. #define TB_MAGENTA 0x0006
  194. #define TB_CYAN 0x0007
  195. #define TB_WHITE 0x0008
  196. #define TB_BOLD 0x0100
  197. #define TB_UNDERLINE 0x0200
  198. #define TB_REVERSE 0x0400
  199. #define TB_ITALIC 0x0800
  200. #define TB_BLINK 0x1000
  201. #define TB_256_BLACK 0x2000
  202. #ifdef TB_OPT_TRUECOLOR
  203. #define TB_TRUECOLOR_BOLD 0x01000000
  204. #define TB_TRUECOLOR_UNDERLINE 0x02000000
  205. #define TB_TRUECOLOR_REVERSE 0x04000000
  206. #define TB_TRUECOLOR_ITALIC 0x08000000
  207. #define TB_TRUECOLOR_BLINK 0x10000000
  208. #define TB_TRUECOLOR_BLACK 0x20000000
  209. #endif
  210. /* Event types (tb_event.type) */
  211. #define TB_EVENT_KEY 1
  212. #define TB_EVENT_RESIZE 2
  213. #define TB_EVENT_MOUSE 3
  214. /* Key modifiers (bitwise) (tb_event.mod) */
  215. #define TB_MOD_ALT 1
  216. #define TB_MOD_CTRL 2
  217. #define TB_MOD_SHIFT 4
  218. #define TB_MOD_MOTION 8
  219. /* Input modes (bitwise) (tb_set_input_mode) */
  220. #define TB_INPUT_CURRENT 0
  221. #define TB_INPUT_ESC 1
  222. #define TB_INPUT_ALT 2
  223. #define TB_INPUT_MOUSE 4
  224. /* Output modes (tb_set_output_mode) */
  225. #define TB_OUTPUT_CURRENT 0
  226. #define TB_OUTPUT_NORMAL 1
  227. #define TB_OUTPUT_256 2
  228. #define TB_OUTPUT_216 3
  229. #define TB_OUTPUT_GRAYSCALE 4
  230. #ifdef TB_OPT_TRUECOLOR
  231. #define TB_OUTPUT_TRUECOLOR 5
  232. #endif
  233. /* Common function return values unless otherwise noted.
  234. *
  235. * Library behavior is undefined after receiving TB_ERR_MEM. Callers may
  236. * attempt reinitializing by freeing memory, invoking tb_shutdown, then
  237. * tb_init.
  238. */
  239. #define TB_OK 0
  240. #define TB_ERR -1
  241. #define TB_ERR_NEED_MORE -2
  242. #define TB_ERR_INIT_ALREADY -3
  243. #define TB_ERR_INIT_OPEN -4
  244. #define TB_ERR_MEM -5
  245. #define TB_ERR_NO_EVENT -6
  246. #define TB_ERR_NO_TERM -7
  247. #define TB_ERR_NOT_INIT -8
  248. #define TB_ERR_OUT_OF_BOUNDS -9
  249. #define TB_ERR_READ -10
  250. #define TB_ERR_RESIZE_IOCTL -11
  251. #define TB_ERR_RESIZE_PIPE -12
  252. #define TB_ERR_RESIZE_SIGACTION -13
  253. #define TB_ERR_POLL -14
  254. #define TB_ERR_TCGETATTR -15
  255. #define TB_ERR_TCSETATTR -16
  256. #define TB_ERR_UNSUPPORTED_TERM -17
  257. #define TB_ERR_RESIZE_WRITE -18
  258. #define TB_ERR_RESIZE_POLL -19
  259. #define TB_ERR_RESIZE_READ -20
  260. #define TB_ERR_RESIZE_SSCANF -21
  261. #define TB_ERR_CAP_COLLISION -22
  262. #define TB_ERR_SELECT TB_ERR_POLL
  263. #define TB_ERR_RESIZE_SELECT TB_ERR_RESIZE_POLL
  264. /* Function types to be used with tb_set_func() */
  265. #define TB_FUNC_EXTRACT_PRE 0
  266. #define TB_FUNC_EXTRACT_POST 1
  267. /* Define this to set the size of the buffer used in tb_printf()
  268. * and tb_sendf()
  269. */
  270. #ifndef TB_OPT_PRINTF_BUF
  271. #define TB_OPT_PRINTF_BUF 4096
  272. #endif
  273. /* Define this to set the size of the read buffer used when reading
  274. * from the tty
  275. */
  276. #ifndef TB_OPT_READ_BUF
  277. #define TB_OPT_READ_BUF 64
  278. #endif
  279. /* Define this for limited back compat with termbox v1 */
  280. #ifdef TB_OPT_V1_COMPAT
  281. #define tb_change_cell tb_set_cell
  282. #define tb_put_cell(x, y, c) tb_set_cell((x), (y), (c)->ch, (c)->fg, (c)->bg)
  283. #define tb_set_clear_attributes tb_set_clear_attrs
  284. #define tb_select_input_mode tb_set_input_mode
  285. #define tb_select_output_mode tb_set_output_mode
  286. #endif
  287. /* Define these to swap in a different allocator */
  288. #ifndef tb_malloc
  289. #define tb_malloc malloc
  290. #define tb_realloc realloc
  291. #define tb_free free
  292. #endif
  293. #ifdef TB_OPT_TRUECOLOR
  294. typedef uint32_t uintattr_t;
  295. #else
  296. typedef uint16_t uintattr_t;
  297. #endif
  298. /* The terminal screen is represented as 2d array of cells. The structure is
  299. * optimized for dealing with single-width (wcwidth()==1) Unicode code points,
  300. * however some support for grapheme clusters (e.g., combining diacritical
  301. * marks) and wide code points (e.g., Hiragana) is provided through ech, nech,
  302. * cech via tb_set_cell_ex(). ech is only valid when nech>0, otherwise ch is
  303. * used.
  304. *
  305. * For non-single-width code points, given N=wcwidth(ch)/wcswidth(ech):
  306. *
  307. * when N==0: termbox forces a single-width cell. Callers should avoid this
  308. * if aiming to render text accurately.
  309. *
  310. * when N>1: termbox zeroes out the following N-1 cells and skips sending
  311. * them to the tty. So, e.g., if the caller sets x=0,y=0 to an N==2
  312. * code point, the caller's next set should be at x=2,y=0. Anything
  313. * set at x=1,y=0 will be ignored. If there are not enough columns
  314. * remaining on the line to render N width, spaces are sent
  315. * instead.
  316. *
  317. * See tb_present() for implementation.
  318. */
  319. struct tb_cell {
  320. uint32_t ch; /* a Unicode character */
  321. uintattr_t fg; /* bitwise foreground attributes */
  322. uintattr_t bg; /* bitwise background attributes */
  323. #ifdef TB_OPT_EGC
  324. uint32_t *ech; /* a grapheme cluster of Unicode code points */
  325. size_t nech; /* length in bytes of ech, 0 means use ch instead of ech */
  326. size_t cech; /* capacity in bytes of ech */
  327. #endif
  328. };
  329. /* An incoming event from the tty.
  330. *
  331. * Given the event type, the following fields are relevant:
  332. *
  333. * when TB_EVENT_KEY: (key XOR ch, one will be zero), mod. Note there is
  334. * overlap between TB_MOD_CTRL and TB_KEY_CTRL_*.
  335. * TB_MOD_CTRL and TB_MOD_SHIFT are only set as
  336. * modifiers to TB_KEY_ARROW_*.
  337. *
  338. * when TB_EVENT_RESIZE: w, h
  339. *
  340. * when TB_EVENT_MOUSE: key (TB_KEY_MOUSE_*), x, y
  341. */
  342. struct tb_event {
  343. uint8_t type; /* one of TB_EVENT_* constants */
  344. uint8_t mod; /* bitwise TB_MOD_* constants */
  345. uint16_t key; /* one of TB_KEY_* constants */
  346. uint32_t ch; /* a Unicode code point */
  347. int32_t w; /* resize width */
  348. int32_t h; /* resize height */
  349. int32_t x; /* mouse x */
  350. int32_t y; /* mouse y */
  351. };
  352. /* Initializes the termbox library. This function should be called before any
  353. * other functions. tb_init() is equivalent to tb_init_file("/dev/tty"). After
  354. * successful initialization, the library must be finalized using the
  355. * tb_shutdown() function.
  356. */
  357. int tb_init(void);
  358. int tb_init_file(const char *path);
  359. int tb_init_fd(int ttyfd);
  360. int tb_init_rwfd(int rfd, int wfd);
  361. int tb_shutdown(void);
  362. /* Returns the size of the internal back buffer (which is the same as terminal's
  363. * window size in rows and columns). The internal buffer can be resized after
  364. * tb_clear() or tb_present() function calls. Both dimensions have an
  365. * unspecified negative value when called before tb_init() or after
  366. * tb_shutdown().
  367. */
  368. int tb_width(void);
  369. int tb_height(void);
  370. /* Clears the internal back buffer using TB_DEFAULT color or the
  371. * color/attributes set by tb_set_clear_attrs() function.
  372. */
  373. int tb_clear(void);
  374. int tb_set_clear_attrs(uintattr_t fg, uintattr_t bg);
  375. /* Synchronizes the internal back buffer with the terminal by writing to tty. */
  376. int tb_present(void);
  377. /* Clears the internal front buffer effectively forcing a complete re-render of
  378. * the back buffer to the tty. It is not necessary to call this under normal
  379. * circumstances. */
  380. int tb_invalidate(void);
  381. /* Sets the position of the cursor. Upper-left character is (0, 0). */
  382. int tb_set_cursor(int cx, int cy);
  383. int tb_hide_cursor(void);
  384. /* Set cell contents in the internal back buffer at the specified position.
  385. *
  386. * Use tb_set_cell_ex() for rendering grapheme clusters (e.g., combining
  387. * diacritical marks).
  388. *
  389. * Function tb_set_cell(x, y, ch, fg, bg) is equivalent to
  390. * tb_set_cell_ex(x, y, &ch, 1, fg, bg).
  391. *
  392. * Function tb_extend_cell() is a shortcut for appending 1 code point to
  393. * cell->ech.
  394. */
  395. int tb_set_cell(int x, int y, uint32_t ch, uintattr_t fg, uintattr_t bg);
  396. int tb_set_cell_ex(int x, int y, uint32_t *ch, size_t nch, uintattr_t fg,
  397. uintattr_t bg);
  398. int tb_extend_cell(int x, int y, uint32_t ch);
  399. /* Sets the input mode. Termbox has two input modes:
  400. *
  401. * 1. TB_INPUT_ESC
  402. * When escape (\x1b) is in the buffer and there's no match for an escape
  403. * sequence, a key event for TB_KEY_ESC is returned.
  404. *
  405. * 2. TB_INPUT_ALT
  406. * When escape (\x1b) is in the buffer and there's no match for an escape
  407. * sequence, the next keyboard event is returned with a TB_MOD_ALT modifier.
  408. *
  409. * You can also apply TB_INPUT_MOUSE via bitwise OR operation to either of the
  410. * modes (e.g., TB_INPUT_ESC | TB_INPUT_MOUSE) to receive TB_EVENT_MOUSE events.
  411. * If none of the main two modes were set, but the mouse mode was, TB_INPUT_ESC
  412. * mode is used. If for some reason you've decided to use
  413. * (TB_INPUT_ESC | TB_INPUT_ALT) combination, it will behave as if only
  414. * TB_INPUT_ESC was selected.
  415. *
  416. * If mode is TB_INPUT_CURRENT, the function returns the current input mode.
  417. *
  418. * The default input mode is TB_INPUT_ESC.
  419. */
  420. int tb_set_input_mode(int mode);
  421. /* Sets the termbox output mode. Termbox has multiple output modes:
  422. *
  423. * 1. TB_OUTPUT_NORMAL => [0..8]
  424. *
  425. * This mode provides 8 different colors:
  426. * TB_BLACK, TB_RED, TB_GREEN, TB_YELLOW,
  427. * TB_BLUE, TB_MAGENTA, TB_CYAN, TB_WHITE
  428. *
  429. * Plus TB_DEFAULT which skips sending a color code (i.e., uses the
  430. * terminal's default color).
  431. *
  432. * Colors (including TB_DEFAULT) may be bitwise OR'd with attributes:
  433. * TB_BOLD, TB_UNDERLINE, TB_REVERSE, TB_ITALIC, TB_BLINK
  434. *
  435. * As in all modes, the value 0 is interpreted as TB_DEFAULT for
  436. * convenience.
  437. *
  438. * Some notes: TB_REVERSE can be applied as either fg or bg attributes for
  439. * the same effect. TB_BOLD, TB_UNDERLINE, TB_ITALIC, TB_BLINK apply as fg
  440. * attributes only, and are ignored as bg attributes.
  441. *
  442. * Example usage:
  443. * tb_set_cell(x, y, '@', TB_BLACK | TB_BOLD, TB_RED);
  444. *
  445. * 2. TB_OUTPUT_256 => [0..255] + TB_256_BLACK
  446. *
  447. * In this mode you get 256 distinct colors (plus default):
  448. * 0x00 (1): TB_DEFAULT
  449. * TB_256_BLACK (1): TB_BLACK in TB_OUTPUT_NORMAL
  450. * 0x01..0x07 (7): the next 7 colors as in TB_OUTPUT_NORMAL
  451. * 0x08..0x0f (8): bright versions of the above
  452. * 0x10..0xe7 (216): 216 different colors
  453. * 0xe8..0xff (24): 24 different shades of gray
  454. *
  455. * Attributes may be bitwise OR'd as in TB_OUTPUT_NORMAL.
  456. *
  457. * Note TB_256_BLACK must be used for black, as 0x00 represents default.
  458. *
  459. * 3. TB_OUTPUT_216 => [0..216]
  460. *
  461. * This mode supports the 216-color range of TB_OUTPUT_256 only, but you
  462. * don't need to provide an offset:
  463. * 0x00 (1): TB_DEFAULT
  464. * 0x01..0xd8 (216): 216 different colors
  465. *
  466. * 4. TB_OUTPUT_GRAYSCALE => [0..24]
  467. *
  468. * This mode supports the 24-color range of TB_OUTPUT_256 only, but you
  469. * don't need to provide an offset:
  470. * 0x00 (1): TB_DEFAULT
  471. * 0x01..0x18 (24): 24 different shades of gray
  472. *
  473. * 5. TB_OUTPUT_TRUECOLOR => [0x000000..0xffffff] + TB_TRUECOLOR_BLACK
  474. *
  475. * This mode provides 24-bit color on supported terminals. The format is
  476. * 0xRRGGBB. Colors may be bitwise OR'd with TB_TRUECOLOR_* attributes.
  477. *
  478. * Note TB_TRUECOLOR_BLACK must be used for black, as 0x000000 represents
  479. * default.
  480. *
  481. * If mode is TB_OUTPUT_CURRENT, the function returns the current output mode.
  482. *
  483. * The default output mode is TB_OUTPUT_NORMAL.
  484. *
  485. * To use the terminal default color (i.e., to not send an escape code), pass
  486. * TB_DEFAULT. For convenience, the value 0 is interpreted as TB_DEFAULT in
  487. * all modes.
  488. *
  489. * Note, cell attributes persist after switching output modes. Any translation
  490. * between, for example, TB_OUTPUT_NORMAL's TB_RED and TB_OUTPUT_TRUECOLOR's
  491. * 0xff0000 must be performed by the caller. Also note that cells previously
  492. * rendered in one mode may persist unchanged until the front buffer is cleared
  493. * (such as after a resize event) at which point it will be re-interpreted and
  494. * flushed according to the current mode. Callers may invoke tb_invalidate if
  495. * it is desirable to immediately re-interpret and flush the entire screen
  496. * according to the current mode.
  497. *
  498. * Note, not all terminals support all output modes, especially beyond
  499. * TB_OUTPUT_NORMAL. There is also no very reliable way to determine color
  500. * support dynamically. If portability is desired, callers are recommended to
  501. * use TB_OUTPUT_NORMAL or make output mode end-user configurable.
  502. */
  503. int tb_set_output_mode(int mode);
  504. /* Wait for an event up to timeout_ms milliseconds and fill the event structure
  505. * with it. If no event is available within the timeout period, TB_ERR_NO_EVENT
  506. * is returned. On a resize event, the underlying select(2) call may be
  507. * interrupted, yielding a return code of TB_ERR_POLL. In this case, you may
  508. * check errno via tb_last_errno(). If it's EINTR, you can safely ignore that
  509. * and call tb_peek_event() again.
  510. */
  511. int tb_peek_event(struct tb_event *event, int timeout_ms);
  512. /* Same as tb_peek_event except no timeout. */
  513. int tb_poll_event(struct tb_event *event);
  514. /* Internal termbox FDs that can be used with poll() / select(). Must call
  515. * tb_poll_event() / tb_peek_event() if activity is detected. */
  516. int tb_get_fds(int *ttyfd, int *resizefd);
  517. /* Print and printf functions. Specify param out_w to determine width of printed
  518. * string.
  519. */
  520. int tb_print(int x, int y, uintattr_t fg, uintattr_t bg, const char *str);
  521. int tb_printf(int x, int y, uintattr_t fg, uintattr_t bg, const char *fmt, ...);
  522. int tb_print_ex(int x, int y, uintattr_t fg, uintattr_t bg, size_t *out_w,
  523. const char *str);
  524. int tb_printf_ex(int x, int y, uintattr_t fg, uintattr_t bg, size_t *out_w,
  525. const char *fmt, ...);
  526. /* Send raw bytes to terminal. */
  527. int tb_send(const char *buf, size_t nbuf);
  528. int tb_sendf(const char *fmt, ...);
  529. /* Set custom functions. fn_type is one of TB_FUNC_* constants, fn is a
  530. * compatible function pointer, or NULL to clear.
  531. *
  532. * TB_FUNC_EXTRACT_PRE:
  533. * If specified, invoke this function BEFORE termbox tries to extract any
  534. * escape sequences from the input buffer.
  535. *
  536. * TB_FUNC_EXTRACT_POST:
  537. * If specified, invoke this function AFTER termbox tries (and fails) to
  538. * extract any escape sequences from the input buffer.
  539. */
  540. int tb_set_func(int fn_type, int (*fn)(struct tb_event *, size_t *));
  541. /* Utility functions. */
  542. int tb_utf8_char_length(char c);
  543. int tb_utf8_char_to_unicode(uint32_t *out, const char *c);
  544. int tb_utf8_unicode_to_char(char *out, uint32_t c);
  545. int tb_last_errno(void);
  546. const char *tb_strerror(int err);
  547. struct tb_cell *tb_cell_buffer(void);
  548. int tb_has_truecolor(void);
  549. int tb_has_egc(void);
  550. const char *tb_version(void);
  551. #ifdef __cplusplus
  552. }
  553. #endif
  554. #endif /* __TERMBOX_H */
  555. #ifdef TB_IMPL
  556. #define if_err_return(rv, expr) \
  557. if (((rv) = (expr)) != TB_OK) return (rv)
  558. #define if_err_break(rv, expr) \
  559. if (((rv) = (expr)) != TB_OK) break
  560. #define if_ok_return(rv, expr) \
  561. if (((rv) = (expr)) == TB_OK) return (rv)
  562. #define if_ok_or_need_more_return(rv, expr) \
  563. if (((rv) = (expr)) == TB_OK || (rv) == TB_ERR_NEED_MORE) return (rv)
  564. #define send_literal(rv, a) \
  565. if_err_return((rv), bytebuf_nputs(&global.out, (a), sizeof(a) - 1))
  566. #define send_num(rv, nbuf, n) \
  567. if_err_return((rv), \
  568. bytebuf_nputs(&global.out, (nbuf), convert_num((n), (nbuf))))
  569. #define snprintf_or_return(rv, str, sz, fmt, ...) \
  570. do { \
  571. (rv) = snprintf((str), (sz), (fmt), __VA_ARGS__); \
  572. if ((rv) < 0 || (rv) >= (int)(sz)) return TB_ERR; \
  573. } while (0)
  574. #define if_not_init_return() \
  575. if (!global.initialized) return TB_ERR_NOT_INIT
  576. struct bytebuf_t {
  577. char *buf;
  578. size_t len;
  579. size_t cap;
  580. };
  581. struct cellbuf_t {
  582. int width;
  583. int height;
  584. struct tb_cell *cells;
  585. };
  586. struct cap_trie_t {
  587. char c;
  588. struct cap_trie_t *children;
  589. size_t nchildren;
  590. int is_leaf;
  591. uint16_t key;
  592. uint8_t mod;
  593. };
  594. struct tb_global_t {
  595. int ttyfd;
  596. int rfd;
  597. int wfd;
  598. int ttyfd_open;
  599. int resize_pipefd[2];
  600. int width;
  601. int height;
  602. int cursor_x;
  603. int cursor_y;
  604. int last_x;
  605. int last_y;
  606. uintattr_t fg;
  607. uintattr_t bg;
  608. uintattr_t last_fg;
  609. uintattr_t last_bg;
  610. int input_mode;
  611. int output_mode;
  612. char *terminfo;
  613. size_t nterminfo;
  614. const char *caps[TB_CAP__COUNT];
  615. struct cap_trie_t cap_trie;
  616. struct bytebuf_t in;
  617. struct bytebuf_t out;
  618. struct cellbuf_t back;
  619. struct cellbuf_t front;
  620. struct termios orig_tios;
  621. int has_orig_tios;
  622. int last_errno;
  623. int initialized;
  624. int (*fn_extract_esc_pre)(struct tb_event *, size_t *);
  625. int (*fn_extract_esc_post)(struct tb_event *, size_t *);
  626. char errbuf[1024];
  627. };
  628. static struct tb_global_t global = {0};
  629. /* BEGIN codegen c */
  630. /* Produced by ./codegen.sh on Sun, 19 Sep 2021 01:02:03 +0000 */
  631. static const int16_t terminfo_cap_indexes[] = {
  632. 66, // kf1 (TB_CAP_F1)
  633. 68, // kf2 (TB_CAP_F2)
  634. 69, // kf3 (TB_CAP_F3)
  635. 70, // kf4 (TB_CAP_F4)
  636. 71, // kf5 (TB_CAP_F5)
  637. 72, // kf6 (TB_CAP_F6)
  638. 73, // kf7 (TB_CAP_F7)
  639. 74, // kf8 (TB_CAP_F8)
  640. 75, // kf9 (TB_CAP_F9)
  641. 67, // kf10 (TB_CAP_F10)
  642. 216, // kf11 (TB_CAP_F11)
  643. 217, // kf12 (TB_CAP_F12)
  644. 77, // kich1 (TB_CAP_INSERT)
  645. 59, // kdch1 (TB_CAP_DELETE)
  646. 76, // khome (TB_CAP_HOME)
  647. 164, // kend (TB_CAP_END)
  648. 82, // kpp (TB_CAP_PGUP)
  649. 81, // knp (TB_CAP_PGDN)
  650. 87, // kcuu1 (TB_CAP_ARROW_UP)
  651. 61, // kcud1 (TB_CAP_ARROW_DOWN)
  652. 79, // kcub1 (TB_CAP_ARROW_LEFT)
  653. 83, // kcuf1 (TB_CAP_ARROW_RIGHT)
  654. 148, // kcbt (TB_CAP_BACK_TAB)
  655. 28, // smcup (TB_CAP_ENTER_CA)
  656. 40, // rmcup (TB_CAP_EXIT_CA)
  657. 16, // cnorm (TB_CAP_SHOW_CURSOR)
  658. 13, // civis (TB_CAP_HIDE_CURSOR)
  659. 5, // clear (TB_CAP_CLEAR_SCREEN)
  660. 39, // sgr0 (TB_CAP_SGR0)
  661. 36, // smul (TB_CAP_UNDERLINE)
  662. 27, // bold (TB_CAP_BOLD)
  663. 26, // blink (TB_CAP_BLINK)
  664. 311, // sitm (TB_CAP_ITALIC)
  665. 34, // rev (TB_CAP_REVERSE)
  666. 89, // smkx (TB_CAP_ENTER_KEYPAD)
  667. 88, // rmkx (TB_CAP_EXIT_KEYPAD)
  668. };
  669. // xterm
  670. static const char *xterm_caps[] = {
  671. "\033OP", // kf1 (TB_CAP_F1)
  672. "\033OQ", // kf2 (TB_CAP_F2)
  673. "\033OR", // kf3 (TB_CAP_F3)
  674. "\033OS", // kf4 (TB_CAP_F4)
  675. "\033[15~", // kf5 (TB_CAP_F5)
  676. "\033[17~", // kf6 (TB_CAP_F6)
  677. "\033[18~", // kf7 (TB_CAP_F7)
  678. "\033[19~", // kf8 (TB_CAP_F8)
  679. "\033[20~", // kf9 (TB_CAP_F9)
  680. "\033[21~", // kf10 (TB_CAP_F10)
  681. "\033[23~", // kf11 (TB_CAP_F11)
  682. "\033[24~", // kf12 (TB_CAP_F12)
  683. "\033[2~", // kich1 (TB_CAP_INSERT)
  684. "\033[3~", // kdch1 (TB_CAP_DELETE)
  685. "\033OH", // khome (TB_CAP_HOME)
  686. "\033OF", // kend (TB_CAP_END)
  687. "\033[5~", // kpp (TB_CAP_PGUP)
  688. "\033[6~", // knp (TB_CAP_PGDN)
  689. "\033OA", // kcuu1 (TB_CAP_ARROW_UP)
  690. "\033OB", // kcud1 (TB_CAP_ARROW_DOWN)
  691. "\033OD", // kcub1 (TB_CAP_ARROW_LEFT)
  692. "\033OC", // kcuf1 (TB_CAP_ARROW_RIGHT)
  693. "\033[Z", // kcbt (TB_CAP_BACK_TAB)
  694. "\033[?1049h\033[22;0;0t", // smcup (TB_CAP_ENTER_CA)
  695. "\033[?1049l\033[23;0;0t", // rmcup (TB_CAP_EXIT_CA)
  696. "\033[?12l\033[?25h", // cnorm (TB_CAP_SHOW_CURSOR)
  697. "\033[?25l", // civis (TB_CAP_HIDE_CURSOR)
  698. "\033[H\033[2J", // clear (TB_CAP_CLEAR_SCREEN)
  699. "\033(B\033[m", // sgr0 (TB_CAP_SGR0)
  700. "\033[4m", // smul (TB_CAP_UNDERLINE)
  701. "\033[1m", // bold (TB_CAP_BOLD)
  702. "\033[5m", // blink (TB_CAP_BLINK)
  703. "\033[3m", // sitm (TB_CAP_ITALIC)
  704. "\033[7m", // rev (TB_CAP_REVERSE)
  705. "\033[?1h\033=", // smkx (TB_CAP_ENTER_KEYPAD)
  706. "\033[?1l\033>", // rmkx (TB_CAP_EXIT_KEYPAD)
  707. };
  708. // linux
  709. static const char *linux_caps[] = {
  710. "\033[[A", // kf1 (TB_CAP_F1)
  711. "\033[[B", // kf2 (TB_CAP_F2)
  712. "\033[[C", // kf3 (TB_CAP_F3)
  713. "\033[[D", // kf4 (TB_CAP_F4)
  714. "\033[[E", // kf5 (TB_CAP_F5)
  715. "\033[17~", // kf6 (TB_CAP_F6)
  716. "\033[18~", // kf7 (TB_CAP_F7)
  717. "\033[19~", // kf8 (TB_CAP_F8)
  718. "\033[20~", // kf9 (TB_CAP_F9)
  719. "\033[21~", // kf10 (TB_CAP_F10)
  720. "\033[23~", // kf11 (TB_CAP_F11)
  721. "\033[24~", // kf12 (TB_CAP_F12)
  722. "\033[2~", // kich1 (TB_CAP_INSERT)
  723. "\033[3~", // kdch1 (TB_CAP_DELETE)
  724. "\033[1~", // khome (TB_CAP_HOME)
  725. "\033[4~", // kend (TB_CAP_END)
  726. "\033[5~", // kpp (TB_CAP_PGUP)
  727. "\033[6~", // knp (TB_CAP_PGDN)
  728. "\033[A", // kcuu1 (TB_CAP_ARROW_UP)
  729. "\033[B", // kcud1 (TB_CAP_ARROW_DOWN)
  730. "\033[D", // kcub1 (TB_CAP_ARROW_LEFT)
  731. "\033[C", // kcuf1 (TB_CAP_ARROW_RIGHT)
  732. "\033[Z", // kcbt (TB_CAP_BACK_TAB)
  733. "", // smcup (TB_CAP_ENTER_CA)
  734. "", // rmcup (TB_CAP_EXIT_CA)
  735. "\033[?25h\033[?0c", // cnorm (TB_CAP_SHOW_CURSOR)
  736. "\033[?25l\033[?1c", // civis (TB_CAP_HIDE_CURSOR)
  737. "\033[H\033[J", // clear (TB_CAP_CLEAR_SCREEN)
  738. "\033[m\017", // sgr0 (TB_CAP_SGR0)
  739. "\033[4m", // smul (TB_CAP_UNDERLINE)
  740. "\033[1m", // bold (TB_CAP_BOLD)
  741. "\033[5m", // blink (TB_CAP_BLINK)
  742. "", // sitm (TB_CAP_ITALIC)
  743. "\033[7m", // rev (TB_CAP_REVERSE)
  744. "", // smkx (TB_CAP_ENTER_KEYPAD)
  745. "", // rmkx (TB_CAP_EXIT_KEYPAD)
  746. };
  747. // screen
  748. static const char *screen_caps[] = {
  749. "\033OP", // kf1 (TB_CAP_F1)
  750. "\033OQ", // kf2 (TB_CAP_F2)
  751. "\033OR", // kf3 (TB_CAP_F3)
  752. "\033OS", // kf4 (TB_CAP_F4)
  753. "\033[15~", // kf5 (TB_CAP_F5)
  754. "\033[17~", // kf6 (TB_CAP_F6)
  755. "\033[18~", // kf7 (TB_CAP_F7)
  756. "\033[19~", // kf8 (TB_CAP_F8)
  757. "\033[20~", // kf9 (TB_CAP_F9)
  758. "\033[21~", // kf10 (TB_CAP_F10)
  759. "\033[23~", // kf11 (TB_CAP_F11)
  760. "\033[24~", // kf12 (TB_CAP_F12)
  761. "\033[2~", // kich1 (TB_CAP_INSERT)
  762. "\033[3~", // kdch1 (TB_CAP_DELETE)
  763. "\033[1~", // khome (TB_CAP_HOME)
  764. "\033[4~", // kend (TB_CAP_END)
  765. "\033[5~", // kpp (TB_CAP_PGUP)
  766. "\033[6~", // knp (TB_CAP_PGDN)
  767. "\033OA", // kcuu1 (TB_CAP_ARROW_UP)
  768. "\033OB", // kcud1 (TB_CAP_ARROW_DOWN)
  769. "\033OD", // kcub1 (TB_CAP_ARROW_LEFT)
  770. "\033OC", // kcuf1 (TB_CAP_ARROW_RIGHT)
  771. "\033[Z", // kcbt (TB_CAP_BACK_TAB)
  772. "\033[?1049h", // smcup (TB_CAP_ENTER_CA)
  773. "\033[?1049l", // rmcup (TB_CAP_EXIT_CA)
  774. "\033[34h\033[?25h", // cnorm (TB_CAP_SHOW_CURSOR)
  775. "\033[?25l", // civis (TB_CAP_HIDE_CURSOR)
  776. "\033[H\033[J", // clear (TB_CAP_CLEAR_SCREEN)
  777. "\033[m\017", // sgr0 (TB_CAP_SGR0)
  778. "\033[4m", // smul (TB_CAP_UNDERLINE)
  779. "\033[1m", // bold (TB_CAP_BOLD)
  780. "\033[5m", // blink (TB_CAP_BLINK)
  781. "", // sitm (TB_CAP_ITALIC)
  782. "\033[7m", // rev (TB_CAP_REVERSE)
  783. "\033[?1h\033=", // smkx (TB_CAP_ENTER_KEYPAD)
  784. "\033[?1l\033>", // rmkx (TB_CAP_EXIT_KEYPAD)
  785. };
  786. // rxvt-256color
  787. static const char *rxvt_256color_caps[] = {
  788. "\033[11~", // kf1 (TB_CAP_F1)
  789. "\033[12~", // kf2 (TB_CAP_F2)
  790. "\033[13~", // kf3 (TB_CAP_F3)
  791. "\033[14~", // kf4 (TB_CAP_F4)
  792. "\033[15~", // kf5 (TB_CAP_F5)
  793. "\033[17~", // kf6 (TB_CAP_F6)
  794. "\033[18~", // kf7 (TB_CAP_F7)
  795. "\033[19~", // kf8 (TB_CAP_F8)
  796. "\033[20~", // kf9 (TB_CAP_F9)
  797. "\033[21~", // kf10 (TB_CAP_F10)
  798. "\033[23~", // kf11 (TB_CAP_F11)
  799. "\033[24~", // kf12 (TB_CAP_F12)
  800. "\033[2~", // kich1 (TB_CAP_INSERT)
  801. "\033[3~", // kdch1 (TB_CAP_DELETE)
  802. "\033[7~", // khome (TB_CAP_HOME)
  803. "\033[8~", // kend (TB_CAP_END)
  804. "\033[5~", // kpp (TB_CAP_PGUP)
  805. "\033[6~", // knp (TB_CAP_PGDN)
  806. "\033[A", // kcuu1 (TB_CAP_ARROW_UP)
  807. "\033[B", // kcud1 (TB_CAP_ARROW_DOWN)
  808. "\033[D", // kcub1 (TB_CAP_ARROW_LEFT)
  809. "\033[C", // kcuf1 (TB_CAP_ARROW_RIGHT)
  810. "\033[Z", // kcbt (TB_CAP_BACK_TAB)
  811. "\0337\033[?47h", // smcup (TB_CAP_ENTER_CA)
  812. "\033[2J\033[?47l\0338", // rmcup (TB_CAP_EXIT_CA)
  813. "\033[?25h", // cnorm (TB_CAP_SHOW_CURSOR)
  814. "\033[?25l", // civis (TB_CAP_HIDE_CURSOR)
  815. "\033[H\033[2J", // clear (TB_CAP_CLEAR_SCREEN)
  816. "\033[m\017", // sgr0 (TB_CAP_SGR0)
  817. "\033[4m", // smul (TB_CAP_UNDERLINE)
  818. "\033[1m", // bold (TB_CAP_BOLD)
  819. "\033[5m", // blink (TB_CAP_BLINK)
  820. "", // sitm (TB_CAP_ITALIC)
  821. "\033[7m", // rev (TB_CAP_REVERSE)
  822. "\033=", // smkx (TB_CAP_ENTER_KEYPAD)
  823. "\033>", // rmkx (TB_CAP_EXIT_KEYPAD)
  824. };
  825. // rxvt-unicode
  826. static const char *rxvt_unicode_caps[] = {
  827. "\033[11~", // kf1 (TB_CAP_F1)
  828. "\033[12~", // kf2 (TB_CAP_F2)
  829. "\033[13~", // kf3 (TB_CAP_F3)
  830. "\033[14~", // kf4 (TB_CAP_F4)
  831. "\033[15~", // kf5 (TB_CAP_F5)
  832. "\033[17~", // kf6 (TB_CAP_F6)
  833. "\033[18~", // kf7 (TB_CAP_F7)
  834. "\033[19~", // kf8 (TB_CAP_F8)
  835. "\033[20~", // kf9 (TB_CAP_F9)
  836. "\033[21~", // kf10 (TB_CAP_F10)
  837. "\033[23~", // kf11 (TB_CAP_F11)
  838. "\033[24~", // kf12 (TB_CAP_F12)
  839. "\033[2~", // kich1 (TB_CAP_INSERT)
  840. "\033[3~", // kdch1 (TB_CAP_DELETE)
  841. "\033[7~", // khome (TB_CAP_HOME)
  842. "\033[8~", // kend (TB_CAP_END)
  843. "\033[5~", // kpp (TB_CAP_PGUP)
  844. "\033[6~", // knp (TB_CAP_PGDN)
  845. "\033[A", // kcuu1 (TB_CAP_ARROW_UP)
  846. "\033[B", // kcud1 (TB_CAP_ARROW_DOWN)
  847. "\033[D", // kcub1 (TB_CAP_ARROW_LEFT)
  848. "\033[C", // kcuf1 (TB_CAP_ARROW_RIGHT)
  849. "\033[Z", // kcbt (TB_CAP_BACK_TAB)
  850. "\033[?1049h", // smcup (TB_CAP_ENTER_CA)
  851. "\033[r\033[?1049l", // rmcup (TB_CAP_EXIT_CA)
  852. "\033[?12l\033[?25h", // cnorm (TB_CAP_SHOW_CURSOR)
  853. "\033[?25l", // civis (TB_CAP_HIDE_CURSOR)
  854. "\033[H\033[2J", // clear (TB_CAP_CLEAR_SCREEN)
  855. "\033[m\033(B", // sgr0 (TB_CAP_SGR0)
  856. "\033[4m", // smul (TB_CAP_UNDERLINE)
  857. "\033[1m", // bold (TB_CAP_BOLD)
  858. "\033[5m", // blink (TB_CAP_BLINK)
  859. "\033[3m", // sitm (TB_CAP_ITALIC)
  860. "\033[7m", // rev (TB_CAP_REVERSE)
  861. "\033=", // smkx (TB_CAP_ENTER_KEYPAD)
  862. "\033>", // rmkx (TB_CAP_EXIT_KEYPAD)
  863. };
  864. // Eterm
  865. static const char *eterm_caps[] = {
  866. "\033[11~", // kf1 (TB_CAP_F1)
  867. "\033[12~", // kf2 (TB_CAP_F2)
  868. "\033[13~", // kf3 (TB_CAP_F3)
  869. "\033[14~", // kf4 (TB_CAP_F4)
  870. "\033[15~", // kf5 (TB_CAP_F5)
  871. "\033[17~", // kf6 (TB_CAP_F6)
  872. "\033[18~", // kf7 (TB_CAP_F7)
  873. "\033[19~", // kf8 (TB_CAP_F8)
  874. "\033[20~", // kf9 (TB_CAP_F9)
  875. "\033[21~", // kf10 (TB_CAP_F10)
  876. "\033[23~", // kf11 (TB_CAP_F11)
  877. "\033[24~", // kf12 (TB_CAP_F12)
  878. "\033[2~", // kich1 (TB_CAP_INSERT)
  879. "\033[3~", // kdch1 (TB_CAP_DELETE)
  880. "\033[7~", // khome (TB_CAP_HOME)
  881. "\033[8~", // kend (TB_CAP_END)
  882. "\033[5~", // kpp (TB_CAP_PGUP)
  883. "\033[6~", // knp (TB_CAP_PGDN)
  884. "\033[A", // kcuu1 (TB_CAP_ARROW_UP)
  885. "\033[B", // kcud1 (TB_CAP_ARROW_DOWN)
  886. "\033[D", // kcub1 (TB_CAP_ARROW_LEFT)
  887. "\033[C", // kcuf1 (TB_CAP_ARROW_RIGHT)
  888. "", // kcbt (TB_CAP_BACK_TAB)
  889. "\0337\033[?47h", // smcup (TB_CAP_ENTER_CA)
  890. "\033[2J\033[?47l\0338", // rmcup (TB_CAP_EXIT_CA)
  891. "\033[?25h", // cnorm (TB_CAP_SHOW_CURSOR)
  892. "\033[?25l", // civis (TB_CAP_HIDE_CURSOR)
  893. "\033[H\033[2J", // clear (TB_CAP_CLEAR_SCREEN)
  894. "\033[m\017", // sgr0 (TB_CAP_SGR0)
  895. "\033[4m", // smul (TB_CAP_UNDERLINE)
  896. "\033[1m", // bold (TB_CAP_BOLD)
  897. "\033[5m", // blink (TB_CAP_BLINK)
  898. "", // sitm (TB_CAP_ITALIC)
  899. "\033[7m", // rev (TB_CAP_REVERSE)
  900. "", // smkx (TB_CAP_ENTER_KEYPAD)
  901. "", // rmkx (TB_CAP_EXIT_KEYPAD)
  902. };
  903. static struct {
  904. const char *name;
  905. const char **caps;
  906. const char *alias;
  907. } builtin_terms[] = {
  908. {"xterm", xterm_caps, "" },
  909. {"linux", linux_caps, "" },
  910. {"screen", screen_caps, "tmux"},
  911. {"rxvt-256color", rxvt_256color_caps, "" },
  912. {"rxvt-unicode", rxvt_unicode_caps, "rxvt"},
  913. {"Eterm", eterm_caps, "" },
  914. {"wsvt25", linux_caps, "" },
  915. {NULL, NULL, NULL },
  916. };
  917. /* END codegen c */
  918. static struct {
  919. const char *cap;
  920. const uint16_t key;
  921. const uint8_t mod;
  922. } builtin_mod_caps[] = {
  923. // xterm arrows
  924. {"\x1b[1;2A", TB_KEY_ARROW_UP, TB_MOD_SHIFT },
  925. {"\x1b[1;3A", TB_KEY_ARROW_UP, TB_MOD_ALT },
  926. {"\x1b[1;4A", TB_KEY_ARROW_UP, TB_MOD_ALT | TB_MOD_SHIFT },
  927. {"\x1b[1;5A", TB_KEY_ARROW_UP, TB_MOD_CTRL },
  928. {"\x1b[1;6A", TB_KEY_ARROW_UP, TB_MOD_CTRL | TB_MOD_SHIFT },
  929. {"\x1b[1;7A", TB_KEY_ARROW_UP, TB_MOD_CTRL | TB_MOD_ALT },
  930. {"\x1b[1;8A", TB_KEY_ARROW_UP, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  931. {"\x1b[1;2B", TB_KEY_ARROW_DOWN, TB_MOD_SHIFT },
  932. {"\x1b[1;3B", TB_KEY_ARROW_DOWN, TB_MOD_ALT },
  933. {"\x1b[1;4B", TB_KEY_ARROW_DOWN, TB_MOD_ALT | TB_MOD_SHIFT },
  934. {"\x1b[1;5B", TB_KEY_ARROW_DOWN, TB_MOD_CTRL },
  935. {"\x1b[1;6B", TB_KEY_ARROW_DOWN, TB_MOD_CTRL | TB_MOD_SHIFT },
  936. {"\x1b[1;7B", TB_KEY_ARROW_DOWN, TB_MOD_CTRL | TB_MOD_ALT },
  937. {"\x1b[1;8B", TB_KEY_ARROW_DOWN, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  938. {"\x1b[1;2C", TB_KEY_ARROW_RIGHT, TB_MOD_SHIFT },
  939. {"\x1b[1;3C", TB_KEY_ARROW_RIGHT, TB_MOD_ALT },
  940. {"\x1b[1;4C", TB_KEY_ARROW_RIGHT, TB_MOD_ALT | TB_MOD_SHIFT },
  941. {"\x1b[1;5C", TB_KEY_ARROW_RIGHT, TB_MOD_CTRL },
  942. {"\x1b[1;6C", TB_KEY_ARROW_RIGHT, TB_MOD_CTRL | TB_MOD_SHIFT },
  943. {"\x1b[1;7C", TB_KEY_ARROW_RIGHT, TB_MOD_CTRL | TB_MOD_ALT },
  944. {"\x1b[1;8C", TB_KEY_ARROW_RIGHT, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  945. {"\x1b[1;2D", TB_KEY_ARROW_LEFT, TB_MOD_SHIFT },
  946. {"\x1b[1;3D", TB_KEY_ARROW_LEFT, TB_MOD_ALT },
  947. {"\x1b[1;4D", TB_KEY_ARROW_LEFT, TB_MOD_ALT | TB_MOD_SHIFT },
  948. {"\x1b[1;5D", TB_KEY_ARROW_LEFT, TB_MOD_CTRL },
  949. {"\x1b[1;6D", TB_KEY_ARROW_LEFT, TB_MOD_CTRL | TB_MOD_SHIFT },
  950. {"\x1b[1;7D", TB_KEY_ARROW_LEFT, TB_MOD_CTRL | TB_MOD_ALT },
  951. {"\x1b[1;8D", TB_KEY_ARROW_LEFT, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  952. // xterm keys
  953. {"\x1b[1;2H", TB_KEY_HOME, TB_MOD_SHIFT },
  954. {"\x1b[1;3H", TB_KEY_HOME, TB_MOD_ALT },
  955. {"\x1b[1;4H", TB_KEY_HOME, TB_MOD_ALT | TB_MOD_SHIFT },
  956. {"\x1b[1;5H", TB_KEY_HOME, TB_MOD_CTRL },
  957. {"\x1b[1;6H", TB_KEY_HOME, TB_MOD_CTRL | TB_MOD_SHIFT },
  958. {"\x1b[1;7H", TB_KEY_HOME, TB_MOD_CTRL | TB_MOD_ALT },
  959. {"\x1b[1;8H", TB_KEY_HOME, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  960. {"\x1b[1;2F", TB_KEY_END, TB_MOD_SHIFT },
  961. {"\x1b[1;3F", TB_KEY_END, TB_MOD_ALT },
  962. {"\x1b[1;4F", TB_KEY_END, TB_MOD_ALT | TB_MOD_SHIFT },
  963. {"\x1b[1;5F", TB_KEY_END, TB_MOD_CTRL },
  964. {"\x1b[1;6F", TB_KEY_END, TB_MOD_CTRL | TB_MOD_SHIFT },
  965. {"\x1b[1;7F", TB_KEY_END, TB_MOD_CTRL | TB_MOD_ALT },
  966. {"\x1b[1;8F", TB_KEY_END, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  967. {"\x1b[2;2~", TB_KEY_INSERT, TB_MOD_SHIFT },
  968. {"\x1b[2;3~", TB_KEY_INSERT, TB_MOD_ALT },
  969. {"\x1b[2;4~", TB_KEY_INSERT, TB_MOD_ALT | TB_MOD_SHIFT },
  970. {"\x1b[2;5~", TB_KEY_INSERT, TB_MOD_CTRL },
  971. {"\x1b[2;6~", TB_KEY_INSERT, TB_MOD_CTRL | TB_MOD_SHIFT },
  972. {"\x1b[2;7~", TB_KEY_INSERT, TB_MOD_CTRL | TB_MOD_ALT },
  973. {"\x1b[2;8~", TB_KEY_INSERT, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  974. {"\x1b[3;2~", TB_KEY_DELETE, TB_MOD_SHIFT },
  975. {"\x1b[3;3~", TB_KEY_DELETE, TB_MOD_ALT },
  976. {"\x1b[3;4~", TB_KEY_DELETE, TB_MOD_ALT | TB_MOD_SHIFT },
  977. {"\x1b[3;5~", TB_KEY_DELETE, TB_MOD_CTRL },
  978. {"\x1b[3;6~", TB_KEY_DELETE, TB_MOD_CTRL | TB_MOD_SHIFT },
  979. {"\x1b[3;7~", TB_KEY_DELETE, TB_MOD_CTRL | TB_MOD_ALT },
  980. {"\x1b[3;8~", TB_KEY_DELETE, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  981. {"\x1b[5;2~", TB_KEY_PGUP, TB_MOD_SHIFT },
  982. {"\x1b[5;3~", TB_KEY_PGUP, TB_MOD_ALT },
  983. {"\x1b[5;4~", TB_KEY_PGUP, TB_MOD_ALT | TB_MOD_SHIFT },
  984. {"\x1b[5;5~", TB_KEY_PGUP, TB_MOD_CTRL },
  985. {"\x1b[5;6~", TB_KEY_PGUP, TB_MOD_CTRL | TB_MOD_SHIFT },
  986. {"\x1b[5;7~", TB_KEY_PGUP, TB_MOD_CTRL | TB_MOD_ALT },
  987. {"\x1b[5;8~", TB_KEY_PGUP, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  988. {"\x1b[6;2~", TB_KEY_PGDN, TB_MOD_SHIFT },
  989. {"\x1b[6;3~", TB_KEY_PGDN, TB_MOD_ALT },
  990. {"\x1b[6;4~", TB_KEY_PGDN, TB_MOD_ALT | TB_MOD_SHIFT },
  991. {"\x1b[6;5~", TB_KEY_PGDN, TB_MOD_CTRL },
  992. {"\x1b[6;6~", TB_KEY_PGDN, TB_MOD_CTRL | TB_MOD_SHIFT },
  993. {"\x1b[6;7~", TB_KEY_PGDN, TB_MOD_CTRL | TB_MOD_ALT },
  994. {"\x1b[6;8~", TB_KEY_PGDN, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  995. {"\x1b[1;2P", TB_KEY_F1, TB_MOD_SHIFT },
  996. {"\x1b[1;3P", TB_KEY_F1, TB_MOD_ALT },
  997. {"\x1b[1;4P", TB_KEY_F1, TB_MOD_ALT | TB_MOD_SHIFT },
  998. {"\x1b[1;5P", TB_KEY_F1, TB_MOD_CTRL },
  999. {"\x1b[1;6P", TB_KEY_F1, TB_MOD_CTRL | TB_MOD_SHIFT },
  1000. {"\x1b[1;7P", TB_KEY_F1, TB_MOD_CTRL | TB_MOD_ALT },
  1001. {"\x1b[1;8P", TB_KEY_F1, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1002. {"\x1b[1;2Q", TB_KEY_F2, TB_MOD_SHIFT },
  1003. {"\x1b[1;3Q", TB_KEY_F2, TB_MOD_ALT },
  1004. {"\x1b[1;4Q", TB_KEY_F2, TB_MOD_ALT | TB_MOD_SHIFT },
  1005. {"\x1b[1;5Q", TB_KEY_F2, TB_MOD_CTRL },
  1006. {"\x1b[1;6Q", TB_KEY_F2, TB_MOD_CTRL | TB_MOD_SHIFT },
  1007. {"\x1b[1;7Q", TB_KEY_F2, TB_MOD_CTRL | TB_MOD_ALT },
  1008. {"\x1b[1;8Q", TB_KEY_F2, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1009. {"\x1b[1;2R", TB_KEY_F3, TB_MOD_SHIFT },
  1010. {"\x1b[1;3R", TB_KEY_F3, TB_MOD_ALT },
  1011. {"\x1b[1;4R", TB_KEY_F3, TB_MOD_ALT | TB_MOD_SHIFT },
  1012. {"\x1b[1;5R", TB_KEY_F3, TB_MOD_CTRL },
  1013. {"\x1b[1;6R", TB_KEY_F3, TB_MOD_CTRL | TB_MOD_SHIFT },
  1014. {"\x1b[1;7R", TB_KEY_F3, TB_MOD_CTRL | TB_MOD_ALT },
  1015. {"\x1b[1;8R", TB_KEY_F3, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1016. {"\x1b[1;2S", TB_KEY_F4, TB_MOD_SHIFT },
  1017. {"\x1b[1;3S", TB_KEY_F4, TB_MOD_ALT },
  1018. {"\x1b[1;4S", TB_KEY_F4, TB_MOD_ALT | TB_MOD_SHIFT },
  1019. {"\x1b[1;5S", TB_KEY_F4, TB_MOD_CTRL },
  1020. {"\x1b[1;6S", TB_KEY_F4, TB_MOD_CTRL | TB_MOD_SHIFT },
  1021. {"\x1b[1;7S", TB_KEY_F4, TB_MOD_CTRL | TB_MOD_ALT },
  1022. {"\x1b[1;8S", TB_KEY_F4, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1023. {"\x1b[15;2~", TB_KEY_F5, TB_MOD_SHIFT },
  1024. {"\x1b[15;3~", TB_KEY_F5, TB_MOD_ALT },
  1025. {"\x1b[15;4~", TB_KEY_F5, TB_MOD_ALT | TB_MOD_SHIFT },
  1026. {"\x1b[15;5~", TB_KEY_F5, TB_MOD_CTRL },
  1027. {"\x1b[15;6~", TB_KEY_F5, TB_MOD_CTRL | TB_MOD_SHIFT },
  1028. {"\x1b[15;7~", TB_KEY_F5, TB_MOD_CTRL | TB_MOD_ALT },
  1029. {"\x1b[15;8~", TB_KEY_F5, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1030. {"\x1b[17;2~", TB_KEY_F6, TB_MOD_SHIFT },
  1031. {"\x1b[17;3~", TB_KEY_F6, TB_MOD_ALT },
  1032. {"\x1b[17;4~", TB_KEY_F6, TB_MOD_ALT | TB_MOD_SHIFT },
  1033. {"\x1b[17;5~", TB_KEY_F6, TB_MOD_CTRL },
  1034. {"\x1b[17;6~", TB_KEY_F6, TB_MOD_CTRL | TB_MOD_SHIFT },
  1035. {"\x1b[17;7~", TB_KEY_F6, TB_MOD_CTRL | TB_MOD_ALT },
  1036. {"\x1b[17;8~", TB_KEY_F6, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1037. {"\x1b[18;2~", TB_KEY_F7, TB_MOD_SHIFT },
  1038. {"\x1b[18;3~", TB_KEY_F7, TB_MOD_ALT },
  1039. {"\x1b[18;4~", TB_KEY_F7, TB_MOD_ALT | TB_MOD_SHIFT },
  1040. {"\x1b[18;5~", TB_KEY_F7, TB_MOD_CTRL },
  1041. {"\x1b[18;6~", TB_KEY_F7, TB_MOD_CTRL | TB_MOD_SHIFT },
  1042. {"\x1b[18;7~", TB_KEY_F7, TB_MOD_CTRL | TB_MOD_ALT },
  1043. {"\x1b[18;8~", TB_KEY_F7, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1044. {"\x1b[19;2~", TB_KEY_F8, TB_MOD_SHIFT },
  1045. {"\x1b[19;3~", TB_KEY_F8, TB_MOD_ALT },
  1046. {"\x1b[19;4~", TB_KEY_F8, TB_MOD_ALT | TB_MOD_SHIFT },
  1047. {"\x1b[19;5~", TB_KEY_F8, TB_MOD_CTRL },
  1048. {"\x1b[19;6~", TB_KEY_F8, TB_MOD_CTRL | TB_MOD_SHIFT },
  1049. {"\x1b[19;7~", TB_KEY_F8, TB_MOD_CTRL | TB_MOD_ALT },
  1050. {"\x1b[19;8~", TB_KEY_F8, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1051. {"\x1b[20;2~", TB_KEY_F9, TB_MOD_SHIFT },
  1052. {"\x1b[20;3~", TB_KEY_F9, TB_MOD_ALT },
  1053. {"\x1b[20;4~", TB_KEY_F9, TB_MOD_ALT | TB_MOD_SHIFT },
  1054. {"\x1b[20;5~", TB_KEY_F9, TB_MOD_CTRL },
  1055. {"\x1b[20;6~", TB_KEY_F9, TB_MOD_CTRL | TB_MOD_SHIFT },
  1056. {"\x1b[20;7~", TB_KEY_F9, TB_MOD_CTRL | TB_MOD_ALT },
  1057. {"\x1b[20;8~", TB_KEY_F9, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1058. {"\x1b[21;2~", TB_KEY_F10, TB_MOD_SHIFT },
  1059. {"\x1b[21;3~", TB_KEY_F10, TB_MOD_ALT },
  1060. {"\x1b[21;4~", TB_KEY_F10, TB_MOD_ALT | TB_MOD_SHIFT },
  1061. {"\x1b[21;5~", TB_KEY_F10, TB_MOD_CTRL },
  1062. {"\x1b[21;6~", TB_KEY_F10, TB_MOD_CTRL | TB_MOD_SHIFT },
  1063. {"\x1b[21;7~", TB_KEY_F10, TB_MOD_CTRL | TB_MOD_ALT },
  1064. {"\x1b[21;8~", TB_KEY_F10, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1065. {"\x1b[23;2~", TB_KEY_F11, TB_MOD_SHIFT },
  1066. {"\x1b[23;3~", TB_KEY_F11, TB_MOD_ALT },
  1067. {"\x1b[23;4~", TB_KEY_F11, TB_MOD_ALT | TB_MOD_SHIFT },
  1068. {"\x1b[23;5~", TB_KEY_F11, TB_MOD_CTRL },
  1069. {"\x1b[23;6~", TB_KEY_F11, TB_MOD_CTRL | TB_MOD_SHIFT },
  1070. {"\x1b[23;7~", TB_KEY_F11, TB_MOD_CTRL | TB_MOD_ALT },
  1071. {"\x1b[23;8~", TB_KEY_F11, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1072. {"\x1b[24;2~", TB_KEY_F12, TB_MOD_SHIFT },
  1073. {"\x1b[24;3~", TB_KEY_F12, TB_MOD_ALT },
  1074. {"\x1b[24;4~", TB_KEY_F12, TB_MOD_ALT | TB_MOD_SHIFT },
  1075. {"\x1b[24;5~", TB_KEY_F12, TB_MOD_CTRL },
  1076. {"\x1b[24;6~", TB_KEY_F12, TB_MOD_CTRL | TB_MOD_SHIFT },
  1077. {"\x1b[24;7~", TB_KEY_F12, TB_MOD_CTRL | TB_MOD_ALT },
  1078. {"\x1b[24;8~", TB_KEY_F12, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1079. // rxvt arrows
  1080. {"\x1b[a", TB_KEY_ARROW_UP, TB_MOD_SHIFT },
  1081. {"\x1b\x1b[A", TB_KEY_ARROW_UP, TB_MOD_ALT },
  1082. {"\x1b\x1b[a", TB_KEY_ARROW_UP, TB_MOD_ALT | TB_MOD_SHIFT },
  1083. {"\x1bOa", TB_KEY_ARROW_UP, TB_MOD_CTRL },
  1084. {"\x1b\x1bOa", TB_KEY_ARROW_UP, TB_MOD_CTRL | TB_MOD_ALT },
  1085. {"\x1b[b", TB_KEY_ARROW_DOWN, TB_MOD_SHIFT },
  1086. {"\x1b\x1b[B", TB_KEY_ARROW_DOWN, TB_MOD_ALT },
  1087. {"\x1b\x1b[b", TB_KEY_ARROW_DOWN, TB_MOD_ALT | TB_MOD_SHIFT },
  1088. {"\x1bOb", TB_KEY_ARROW_DOWN, TB_MOD_CTRL },
  1089. {"\x1b\x1bOb", TB_KEY_ARROW_DOWN, TB_MOD_CTRL | TB_MOD_ALT },
  1090. {"\x1b[c", TB_KEY_ARROW_RIGHT, TB_MOD_SHIFT },
  1091. {"\x1b\x1b[C", TB_KEY_ARROW_RIGHT, TB_MOD_ALT },
  1092. {"\x1b\x1b[c", TB_KEY_ARROW_RIGHT, TB_MOD_ALT | TB_MOD_SHIFT },
  1093. {"\x1bOc", TB_KEY_ARROW_RIGHT, TB_MOD_CTRL },
  1094. {"\x1b\x1bOc", TB_KEY_ARROW_RIGHT, TB_MOD_CTRL | TB_MOD_ALT },
  1095. {"\x1b[d", TB_KEY_ARROW_LEFT, TB_MOD_SHIFT },
  1096. {"\x1b\x1b[D", TB_KEY_ARROW_LEFT, TB_MOD_ALT },
  1097. {"\x1b\x1b[d", TB_KEY_ARROW_LEFT, TB_MOD_ALT | TB_MOD_SHIFT },
  1098. {"\x1bOd", TB_KEY_ARROW_LEFT, TB_MOD_CTRL },
  1099. {"\x1b\x1bOd", TB_KEY_ARROW_LEFT, TB_MOD_CTRL | TB_MOD_ALT },
  1100. // rxvt keys
  1101. {"\x1b[7$", TB_KEY_HOME, TB_MOD_SHIFT },
  1102. {"\x1b\x1b[7~", TB_KEY_HOME, TB_MOD_ALT },
  1103. {"\x1b\x1b[7$", TB_KEY_HOME, TB_MOD_ALT | TB_MOD_SHIFT },
  1104. {"\x1b[7^", TB_KEY_HOME, TB_MOD_CTRL },
  1105. {"\x1b[7@", TB_KEY_HOME, TB_MOD_CTRL | TB_MOD_SHIFT },
  1106. {"\x1b\x1b[7^", TB_KEY_HOME, TB_MOD_CTRL | TB_MOD_ALT },
  1107. {"\x1b\x1b[7@", TB_KEY_HOME, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1108. {"\x1b\x1b[8~", TB_KEY_END, TB_MOD_ALT },
  1109. {"\x1b\x1b[8$", TB_KEY_END, TB_MOD_ALT | TB_MOD_SHIFT },
  1110. {"\x1b[8^", TB_KEY_END, TB_MOD_CTRL },
  1111. {"\x1b\x1b[8^", TB_KEY_END, TB_MOD_CTRL | TB_MOD_ALT },
  1112. {"\x1b\x1b[8@", TB_KEY_END, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1113. {"\x1b[8@", TB_KEY_END, TB_MOD_CTRL | TB_MOD_SHIFT },
  1114. {"\x1b[8$", TB_KEY_END, TB_MOD_SHIFT },
  1115. {"\x1b\x1b[2~", TB_KEY_INSERT, TB_MOD_ALT },
  1116. {"\x1b\x1b[2$", TB_KEY_INSERT, TB_MOD_ALT | TB_MOD_SHIFT },
  1117. {"\x1b[2^", TB_KEY_INSERT, TB_MOD_CTRL },
  1118. {"\x1b\x1b[2^", TB_KEY_INSERT, TB_MOD_CTRL | TB_MOD_ALT },
  1119. {"\x1b\x1b[2@", TB_KEY_INSERT, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1120. {"\x1b[2@", TB_KEY_INSERT, TB_MOD_CTRL | TB_MOD_SHIFT },
  1121. {"\x1b[2$", TB_KEY_INSERT, TB_MOD_SHIFT },
  1122. {"\x1b\x1b[3~", TB_KEY_DELETE, TB_MOD_ALT },
  1123. {"\x1b\x1b[3$", TB_KEY_DELETE, TB_MOD_ALT | TB_MOD_SHIFT },
  1124. {"\x1b[3^", TB_KEY_DELETE, TB_MOD_CTRL },
  1125. {"\x1b\x1b[3^", TB_KEY_DELETE, TB_MOD_CTRL | TB_MOD_ALT },
  1126. {"\x1b\x1b[3@", TB_KEY_DELETE, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1127. {"\x1b[3@", TB_KEY_DELETE, TB_MOD_CTRL | TB_MOD_SHIFT },
  1128. {"\x1b[3$", TB_KEY_DELETE, TB_MOD_SHIFT },
  1129. {"\x1b\x1b[5~", TB_KEY_PGUP, TB_MOD_ALT },
  1130. {"\x1b\x1b[5$", TB_KEY_PGUP, TB_MOD_ALT | TB_MOD_SHIFT },
  1131. {"\x1b[5^", TB_KEY_PGUP, TB_MOD_CTRL },
  1132. {"\x1b\x1b[5^", TB_KEY_PGUP, TB_MOD_CTRL | TB_MOD_ALT },
  1133. {"\x1b\x1b[5@", TB_KEY_PGUP, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1134. {"\x1b[5@", TB_KEY_PGUP, TB_MOD_CTRL | TB_MOD_SHIFT },
  1135. {"\x1b[5$", TB_KEY_PGUP, TB_MOD_SHIFT },
  1136. {"\x1b\x1b[6~", TB_KEY_PGDN, TB_MOD_ALT },
  1137. {"\x1b\x1b[6$", TB_KEY_PGDN, TB_MOD_ALT | TB_MOD_SHIFT },
  1138. {"\x1b[6^", TB_KEY_PGDN, TB_MOD_CTRL },
  1139. {"\x1b\x1b[6^", TB_KEY_PGDN, TB_MOD_CTRL | TB_MOD_ALT },
  1140. {"\x1b\x1b[6@", TB_KEY_PGDN, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1141. {"\x1b[6@", TB_KEY_PGDN, TB_MOD_CTRL | TB_MOD_SHIFT },
  1142. {"\x1b[6$", TB_KEY_PGDN, TB_MOD_SHIFT },
  1143. {"\x1b\x1b[11~", TB_KEY_F1, TB_MOD_ALT },
  1144. {"\x1b\x1b[23~", TB_KEY_F1, TB_MOD_ALT | TB_MOD_SHIFT },
  1145. {"\x1b[11^", TB_KEY_F1, TB_MOD_CTRL },
  1146. {"\x1b\x1b[11^", TB_KEY_F1, TB_MOD_CTRL | TB_MOD_ALT },
  1147. {"\x1b\x1b[23^", TB_KEY_F1, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1148. {"\x1b[23^", TB_KEY_F1, TB_MOD_CTRL | TB_MOD_SHIFT },
  1149. {"\x1b[23~", TB_KEY_F1, TB_MOD_SHIFT },
  1150. {"\x1b\x1b[12~", TB_KEY_F2, TB_MOD_ALT },
  1151. {"\x1b\x1b[24~", TB_KEY_F2, TB_MOD_ALT | TB_MOD_SHIFT },
  1152. {"\x1b[12^", TB_KEY_F2, TB_MOD_CTRL },
  1153. {"\x1b\x1b[12^", TB_KEY_F2, TB_MOD_CTRL | TB_MOD_ALT },
  1154. {"\x1b\x1b[24^", TB_KEY_F2, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1155. {"\x1b[24^", TB_KEY_F2, TB_MOD_CTRL | TB_MOD_SHIFT },
  1156. {"\x1b[24~", TB_KEY_F2, TB_MOD_SHIFT },
  1157. {"\x1b\x1b[13~", TB_KEY_F3, TB_MOD_ALT },
  1158. {"\x1b\x1b[25~", TB_KEY_F3, TB_MOD_ALT | TB_MOD_SHIFT },
  1159. {"\x1b[13^", TB_KEY_F3, TB_MOD_CTRL },
  1160. {"\x1b\x1b[13^", TB_KEY_F3, TB_MOD_CTRL | TB_MOD_ALT },
  1161. {"\x1b\x1b[25^", TB_KEY_F3, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1162. {"\x1b[25^", TB_KEY_F3, TB_MOD_CTRL | TB_MOD_SHIFT },
  1163. {"\x1b[25~", TB_KEY_F3, TB_MOD_SHIFT },
  1164. {"\x1b\x1b[14~", TB_KEY_F4, TB_MOD_ALT },
  1165. {"\x1b\x1b[26~", TB_KEY_F4, TB_MOD_ALT | TB_MOD_SHIFT },
  1166. {"\x1b[14^", TB_KEY_F4, TB_MOD_CTRL },
  1167. {"\x1b\x1b[14^", TB_KEY_F4, TB_MOD_CTRL | TB_MOD_ALT },
  1168. {"\x1b\x1b[26^", TB_KEY_F4, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1169. {"\x1b[26^", TB_KEY_F4, TB_MOD_CTRL | TB_MOD_SHIFT },
  1170. {"\x1b[26~", TB_KEY_F4, TB_MOD_SHIFT },
  1171. {"\x1b\x1b[15~", TB_KEY_F5, TB_MOD_ALT },
  1172. {"\x1b\x1b[28~", TB_KEY_F5, TB_MOD_ALT | TB_MOD_SHIFT },
  1173. {"\x1b[15^", TB_KEY_F5, TB_MOD_CTRL },
  1174. {"\x1b\x1b[15^", TB_KEY_F5, TB_MOD_CTRL | TB_MOD_ALT },
  1175. {"\x1b\x1b[28^", TB_KEY_F5, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1176. {"\x1b[28^", TB_KEY_F5, TB_MOD_CTRL | TB_MOD_SHIFT },
  1177. {"\x1b[28~", TB_KEY_F5, TB_MOD_SHIFT },
  1178. {"\x1b\x1b[17~", TB_KEY_F6, TB_MOD_ALT },
  1179. {"\x1b\x1b[29~", TB_KEY_F6, TB_MOD_ALT | TB_MOD_SHIFT },
  1180. {"\x1b[17^", TB_KEY_F6, TB_MOD_CTRL },
  1181. {"\x1b\x1b[17^", TB_KEY_F6, TB_MOD_CTRL | TB_MOD_ALT },
  1182. {"\x1b\x1b[29^", TB_KEY_F6, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1183. {"\x1b[29^", TB_KEY_F6, TB_MOD_CTRL | TB_MOD_SHIFT },
  1184. {"\x1b[29~", TB_KEY_F6, TB_MOD_SHIFT },
  1185. {"\x1b\x1b[18~", TB_KEY_F7, TB_MOD_ALT },
  1186. {"\x1b\x1b[31~", TB_KEY_F7, TB_MOD_ALT | TB_MOD_SHIFT },
  1187. {"\x1b[18^", TB_KEY_F7, TB_MOD_CTRL },
  1188. {"\x1b\x1b[18^", TB_KEY_F7, TB_MOD_CTRL | TB_MOD_ALT },
  1189. {"\x1b\x1b[31^", TB_KEY_F7, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1190. {"\x1b[31^", TB_KEY_F7, TB_MOD_CTRL | TB_MOD_SHIFT },
  1191. {"\x1b[31~", TB_KEY_F7, TB_MOD_SHIFT },
  1192. {"\x1b\x1b[19~", TB_KEY_F8, TB_MOD_ALT },
  1193. {"\x1b\x1b[32~", TB_KEY_F8, TB_MOD_ALT | TB_MOD_SHIFT },
  1194. {"\x1b[19^", TB_KEY_F8, TB_MOD_CTRL },
  1195. {"\x1b\x1b[19^", TB_KEY_F8, TB_MOD_CTRL | TB_MOD_ALT },
  1196. {"\x1b\x1b[32^", TB_KEY_F8, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1197. {"\x1b[32^", TB_KEY_F8, TB_MOD_CTRL | TB_MOD_SHIFT },
  1198. {"\x1b[32~", TB_KEY_F8, TB_MOD_SHIFT },
  1199. {"\x1b\x1b[20~", TB_KEY_F9, TB_MOD_ALT },
  1200. {"\x1b\x1b[33~", TB_KEY_F9, TB_MOD_ALT | TB_MOD_SHIFT },
  1201. {"\x1b[20^", TB_KEY_F9, TB_MOD_CTRL },
  1202. {"\x1b\x1b[20^", TB_KEY_F9, TB_MOD_CTRL | TB_MOD_ALT },
  1203. {"\x1b\x1b[33^", TB_KEY_F9, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1204. {"\x1b[33^", TB_KEY_F9, TB_MOD_CTRL | TB_MOD_SHIFT },
  1205. {"\x1b[33~", TB_KEY_F9, TB_MOD_SHIFT },
  1206. {"\x1b\x1b[21~", TB_KEY_F10, TB_MOD_ALT },
  1207. {"\x1b\x1b[34~", TB_KEY_F10, TB_MOD_ALT | TB_MOD_SHIFT },
  1208. {"\x1b[21^", TB_KEY_F10, TB_MOD_CTRL },
  1209. {"\x1b\x1b[21^", TB_KEY_F10, TB_MOD_CTRL | TB_MOD_ALT },
  1210. {"\x1b\x1b[34^", TB_KEY_F10, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1211. {"\x1b[34^", TB_KEY_F10, TB_MOD_CTRL | TB_MOD_SHIFT },
  1212. {"\x1b[34~", TB_KEY_F10, TB_MOD_SHIFT },
  1213. {"\x1b\x1b[23~", TB_KEY_F11, TB_MOD_ALT },
  1214. {"\x1b\x1b[23$", TB_KEY_F11, TB_MOD_ALT | TB_MOD_SHIFT },
  1215. {"\x1b[23^", TB_KEY_F11, TB_MOD_CTRL },
  1216. {"\x1b\x1b[23^", TB_KEY_F11, TB_MOD_CTRL | TB_MOD_ALT },
  1217. {"\x1b\x1b[23@", TB_KEY_F11, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1218. {"\x1b[23@", TB_KEY_F11, TB_MOD_CTRL | TB_MOD_SHIFT },
  1219. {"\x1b[23$", TB_KEY_F11, TB_MOD_SHIFT },
  1220. {"\x1b\x1b[24~", TB_KEY_F12, TB_MOD_ALT },
  1221. {"\x1b\x1b[24$", TB_KEY_F12, TB_MOD_ALT | TB_MOD_SHIFT },
  1222. {"\x1b[24^", TB_KEY_F12, TB_MOD_CTRL },
  1223. {"\x1b\x1b[24^", TB_KEY_F12, TB_MOD_CTRL | TB_MOD_ALT },
  1224. {"\x1b\x1b[24@", TB_KEY_F12, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
  1225. {"\x1b[24@", TB_KEY_F12, TB_MOD_CTRL | TB_MOD_SHIFT },
  1226. {"\x1b[24$", TB_KEY_F12, TB_MOD_SHIFT },
  1227. // linux console/putty arrows
  1228. {"\x1b[A", TB_KEY_ARROW_UP, TB_MOD_SHIFT },
  1229. {"\x1b[B", TB_KEY_ARROW_DOWN, TB_MOD_SHIFT },
  1230. {"\x1b[C", TB_KEY_ARROW_RIGHT, TB_MOD_SHIFT },
  1231. {"\x1b[D", TB_KEY_ARROW_LEFT, TB_MOD_SHIFT },
  1232. // more putty arrows
  1233. {"\x1bOA", TB_KEY_ARROW_UP, TB_MOD_CTRL },
  1234. {"\x1b\x1bOA", TB_KEY_ARROW_UP, TB_MOD_CTRL | TB_MOD_ALT },
  1235. {"\x1bOB", TB_KEY_ARROW_DOWN, TB_MOD_CTRL },
  1236. {"\x1b\x1bOB", TB_KEY_ARROW_DOWN, TB_MOD_CTRL | TB_MOD_ALT },
  1237. {"\x1bOC", TB_KEY_ARROW_RIGHT, TB_MOD_CTRL },
  1238. {"\x1b\x1bOC", TB_KEY_ARROW_RIGHT, TB_MOD_CTRL | TB_MOD_ALT },
  1239. {"\x1bOD", TB_KEY_ARROW_LEFT, TB_MOD_CTRL },
  1240. {"\x1b\x1bOD", TB_KEY_ARROW_LEFT, TB_MOD_CTRL | TB_MOD_ALT },
  1241. {NULL, 0, 0 },
  1242. };
  1243. static const unsigned char utf8_length[256] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1244. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1245. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1246. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1247. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1248. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1249. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1250. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1251. 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1252. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  1253. 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1};
  1254. static const unsigned char utf8_mask[6] = {0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01};
  1255. static int tb_reset(void);
  1256. static int tb_printf_inner(int x, int y, uintattr_t fg, uintattr_t bg,
  1257. size_t *out_w, const char *fmt, va_list vl);
  1258. static int init_term_attrs(void);
  1259. static int init_term_caps(void);
  1260. static int init_cap_trie(void);
  1261. static int cap_trie_add(const char *cap, uint16_t key, uint8_t mod);
  1262. static int cap_trie_find(const char *buf, size_t nbuf, struct cap_trie_t **last,
  1263. size_t *depth);
  1264. static int cap_trie_deinit(struct cap_trie_t *node);
  1265. static int init_resize_handler(void);
  1266. static int send_init_escape_codes(void);
  1267. static int send_clear(void);
  1268. static int update_term_size(void);
  1269. static int update_term_size_via_esc(void);
  1270. static int init_cellbuf(void);
  1271. static int tb_deinit(void);
  1272. static int load_terminfo(void);
  1273. static int load_terminfo_from_path(const char *path, const char *term);
  1274. static int read_terminfo_path(const char *path);
  1275. static int parse_terminfo_caps(void);
  1276. static int load_builtin_caps(void);
  1277. static const char *get_terminfo_string(int16_t str_offsets_pos,
  1278. int16_t str_table_pos, int16_t str_table_len, int16_t str_index);
  1279. static int wait_event(struct tb_event *event, int timeout);
  1280. static int extract_event(struct tb_event *event);
  1281. static int extract_esc(struct tb_event *event);
  1282. static int extract_esc_user(struct tb_event *event, int is_post);
  1283. static int extract_esc_cap(struct tb_event *event);
  1284. static int extract_esc_mouse(struct tb_event *event);
  1285. static int resize_cellbufs(void);
  1286. static void handle_resize(int sig);
  1287. static int send_attr(uintattr_t fg, uintattr_t bg);
  1288. static int send_sgr(uintattr_t fg, uintattr_t bg, int fg_is_default,
  1289. int bg_is_default);
  1290. static int send_cursor_if(int x, int y);
  1291. static int send_char(int x, int y, uint32_t ch);
  1292. static int send_cluster(int x, int y, uint32_t *ch, size_t nch);
  1293. static int convert_num(uint32_t num, char *buf);
  1294. static int cell_cmp(struct tb_cell *a, struct tb_cell *b);
  1295. static int cell_copy(struct tb_cell *dst, struct tb_cell *src);
  1296. static int cell_set(struct tb_cell *cell, uint32_t *ch, size_t nch,
  1297. uintattr_t fg, uintattr_t bg);
  1298. static int cell_reserve_ech(struct tb_cell *cell, size_t n);
  1299. static int cell_free(struct tb_cell *cell);
  1300. static int cellbuf_init(struct cellbuf_t *c, int w, int h);
  1301. static int cellbuf_free(struct cellbuf_t *c);
  1302. static int cellbuf_clear(struct cellbuf_t *c);
  1303. static int cellbuf_get(struct cellbuf_t *c, int x, int y, struct tb_cell **out);
  1304. static int cellbuf_resize(struct cellbuf_t *c, int w, int h);
  1305. static int bytebuf_puts(struct bytebuf_t *b, const char *str);
  1306. static int bytebuf_nputs(struct bytebuf_t *b, const char *str, size_t nstr);
  1307. static int bytebuf_shift(struct bytebuf_t *b, size_t n);
  1308. static int bytebuf_flush(struct bytebuf_t *b, int fd);
  1309. static int bytebuf_reserve(struct bytebuf_t *b, size_t sz);
  1310. static int bytebuf_free(struct bytebuf_t *b);
  1311. int tb_init(void) {
  1312. return tb_init_file("/dev/tty");
  1313. }
  1314. int tb_init_file(const char *path) {
  1315. if (global.initialized) {
  1316. return TB_ERR_INIT_ALREADY;
  1317. }
  1318. int ttyfd = open(path, O_RDWR);
  1319. if (ttyfd < 0) {
  1320. global.last_errno = errno;
  1321. return TB_ERR_INIT_OPEN;
  1322. }
  1323. global.ttyfd_open = 1;
  1324. return tb_init_fd(ttyfd);
  1325. }
  1326. int tb_init_fd(int ttyfd) {
  1327. return tb_init_rwfd(ttyfd, ttyfd);
  1328. }
  1329. int tb_init_rwfd(int rfd, int wfd) {
  1330. int rv;
  1331. tb_reset();
  1332. global.ttyfd = rfd == wfd && isatty(rfd) ? rfd : -1;
  1333. global.rfd = rfd;
  1334. global.wfd = wfd;
  1335. do {
  1336. if_err_break(rv, init_term_attrs());
  1337. if_err_break(rv, init_term_caps());
  1338. if_err_break(rv, init_cap_trie());
  1339. if_err_break(rv, init_resize_handler());
  1340. if_err_break(rv, send_init_escape_codes());
  1341. if_err_break(rv, send_clear());
  1342. if_err_break(rv, update_term_size());
  1343. if_err_break(rv, init_cellbuf());
  1344. global.initialized = 1;
  1345. } while (0);
  1346. if (rv != TB_OK) {
  1347. tb_deinit();
  1348. }
  1349. return rv;
  1350. }
  1351. int tb_shutdown(void) {
  1352. if_not_init_return();
  1353. tb_deinit();
  1354. return TB_OK;
  1355. }
  1356. int tb_width(void) {
  1357. if_not_init_return();
  1358. return global.width;
  1359. }
  1360. int tb_height(void) {
  1361. if_not_init_return();
  1362. return global.height;
  1363. }
  1364. int tb_clear(void) {
  1365. if_not_init_return();
  1366. return cellbuf_clear(&global.back);
  1367. }
  1368. int tb_set_clear_attrs(uintattr_t fg, uintattr_t bg) {
  1369. if_not_init_return();
  1370. global.fg = fg;
  1371. global.bg = bg;
  1372. return TB_OK;
  1373. }
  1374. int tb_present(void) {
  1375. if_not_init_return();
  1376. int rv;
  1377. // TODO Assert global.back.(width,height) == global.front.(width,height)
  1378. global.last_x = -1;
  1379. global.last_y = -1;
  1380. int x, y, i;
  1381. for (y = 0; y < global.front.height; y++) {
  1382. for (x = 0; x < global.front.width;) {
  1383. struct tb_cell *back, *front;
  1384. if_err_return(rv, cellbuf_get(&global.back, x, y, &back));
  1385. if_err_return(rv, cellbuf_get(&global.front, x, y, &front));
  1386. int w;
  1387. {
  1388. #ifdef TB_OPT_EGC
  1389. if (back->nech > 0)
  1390. w = wcswidth((wchar_t *)back->ech, back->nech);
  1391. else
  1392. #endif
  1393. /* wcwidth() simply returns -1 on overflow of wchar_t */
  1394. w = wcwidth((wchar_t)back->ch);
  1395. }
  1396. if (w < 1) {
  1397. w = 1;
  1398. }
  1399. if (cell_cmp(back, front) != 0) {
  1400. cell_copy(front, back);
  1401. send_attr(back->fg, back->bg);
  1402. if (w > 1 && x >= global.front.width - (w - 1)) {
  1403. for (i = x; i < global.front.width; i++) {
  1404. send_char(i, y, ' ');
  1405. }
  1406. } else {
  1407. {
  1408. #ifdef TB_OPT_EGC
  1409. if (back->nech > 0)
  1410. send_cluster(x, y, back->ech, back->nech);
  1411. else
  1412. #endif
  1413. send_char(x, y, back->ch);
  1414. }
  1415. for (i = 1; i < w; i++) {
  1416. struct tb_cell *front_wide;
  1417. if_err_return(rv,
  1418. cellbuf_get(&global.front, x + i, y, &front_wide));
  1419. if_err_return(rv,
  1420. cell_set(front_wide, 0, 1, back->fg, back->bg));
  1421. }
  1422. }
  1423. }
  1424. x += w;
  1425. }
  1426. }
  1427. if_err_return(rv, send_cursor_if(global.cursor_x, global.cursor_y));
  1428. if_err_return(rv, bytebuf_flush(&global.out, global.wfd));
  1429. return TB_OK;
  1430. }
  1431. int tb_invalidate(void) {
  1432. int rv;
  1433. if_not_init_return();
  1434. if_err_return(rv, resize_cellbufs());
  1435. return TB_OK;
  1436. }
  1437. int tb_set_cursor(int cx, int cy) {
  1438. if_not_init_return();
  1439. int rv;
  1440. if (cx < 0) cx = 0;
  1441. if (cy < 0) cy = 0;
  1442. if (global.cursor_x == -1) {
  1443. if_err_return(rv,
  1444. bytebuf_puts(&global.out, global.caps[TB_CAP_SHOW_CURSOR]));
  1445. }
  1446. if_err_return(rv, send_cursor_if(cx, cy));
  1447. global.cursor_x = cx;
  1448. global.cursor_y = cy;
  1449. return TB_OK;
  1450. }
  1451. int tb_hide_cursor(void) {
  1452. if_not_init_return();
  1453. int rv;
  1454. if (global.cursor_x >= 0) {
  1455. if_err_return(rv,
  1456. bytebuf_puts(&global.out, global.caps[TB_CAP_HIDE_CURSOR]));
  1457. }
  1458. global.cursor_x = -1;
  1459. global.cursor_y = -1;
  1460. return TB_OK;
  1461. }
  1462. int tb_set_cell(int x, int y, uint32_t ch, uintattr_t fg, uintattr_t bg) {
  1463. if_not_init_return();
  1464. return tb_set_cell_ex(x, y, &ch, 1, fg, bg);
  1465. }
  1466. int tb_set_cell_ex(int x, int y, uint32_t *ch, size_t nch, uintattr_t fg,
  1467. uintattr_t bg) {
  1468. if_not_init_return();
  1469. int rv;
  1470. struct tb_cell *cell;
  1471. if_err_return(rv, cellbuf_get(&global.back, x, y, &cell));
  1472. if_err_return(rv, cell_set(cell, ch, nch, fg, bg));
  1473. return TB_OK;
  1474. }
  1475. int tb_extend_cell(int x, int y, uint32_t ch) {
  1476. if_not_init_return();
  1477. #ifdef TB_OPT_EGC
  1478. int rv;
  1479. struct tb_cell *cell;
  1480. size_t nech;
  1481. if_err_return(rv, cellbuf_get(&global.back, x, y, &cell));
  1482. if (cell->nech > 0) { // append to ech
  1483. nech = cell->nech + 1;
  1484. if_err_return(rv, cell_reserve_ech(cell, nech));
  1485. cell->ech[nech - 1] = ch;
  1486. } else { // make new ech
  1487. nech = 2;
  1488. if_err_return(rv, cell_reserve_ech(cell, nech));
  1489. cell->ech[0] = cell->ch;
  1490. cell->ech[1] = ch;
  1491. }
  1492. cell->ech[nech] = '\0';
  1493. cell->nech = nech;
  1494. return TB_OK;
  1495. #else
  1496. (void)x;
  1497. (void)y;
  1498. (void)ch;
  1499. return TB_ERR;
  1500. #endif
  1501. }
  1502. int tb_set_input_mode(int mode) {
  1503. if_not_init_return();
  1504. if (mode == TB_INPUT_CURRENT) {
  1505. return global.input_mode;
  1506. }
  1507. if ((mode & (TB_INPUT_ESC | TB_INPUT_ALT)) == 0) {
  1508. mode |= TB_INPUT_ESC;
  1509. }
  1510. if ((mode & (TB_INPUT_ESC | TB_INPUT_ALT)) == (TB_INPUT_ESC | TB_INPUT_ALT))
  1511. {
  1512. mode &= ~TB_INPUT_ALT;
  1513. }
  1514. if (mode & TB_INPUT_MOUSE) {
  1515. bytebuf_puts(&global.out, TB_HARDCAP_ENTER_MOUSE);
  1516. bytebuf_flush(&global.out, global.wfd);
  1517. } else {
  1518. bytebuf_puts(&global.out, TB_HARDCAP_EXIT_MOUSE);
  1519. bytebuf_flush(&global.out, global.wfd);
  1520. }
  1521. global.input_mode = mode;
  1522. return TB_OK;
  1523. }
  1524. int tb_set_output_mode(int mode) {
  1525. if_not_init_return();
  1526. switch (mode) {
  1527. case TB_OUTPUT_CURRENT:
  1528. return global.output_mode;
  1529. case TB_OUTPUT_NORMAL:
  1530. case TB_OUTPUT_256:
  1531. case TB_OUTPUT_216:
  1532. case TB_OUTPUT_GRAYSCALE:
  1533. #ifdef TB_OPT_TRUECOLOR
  1534. case TB_OUTPUT_TRUECOLOR:
  1535. #endif
  1536. global.last_fg = ~global.fg;
  1537. global.last_bg = ~global.bg;
  1538. global.output_mode = mode;
  1539. return TB_OK;
  1540. }
  1541. return TB_ERR;
  1542. }
  1543. int tb_peek_event(struct tb_event *event, int timeout_ms) {
  1544. if_not_init_return();
  1545. return wait_event(event, timeout_ms);
  1546. }
  1547. int tb_poll_event(struct tb_event *event) {
  1548. if_not_init_return();
  1549. return wait_event(event, -1);
  1550. }
  1551. int tb_get_fds(int *ttyfd, int *resizefd) {
  1552. if_not_init_return();
  1553. *ttyfd = global.rfd;
  1554. *resizefd = global.resize_pipefd[0];
  1555. return TB_OK;
  1556. }
  1557. int tb_print(int x, int y, uintattr_t fg, uintattr_t bg, const char *str) {
  1558. return tb_print_ex(x, y, fg, bg, NULL, str);
  1559. }
  1560. int tb_print_ex(int x, int y, uintattr_t fg, uintattr_t bg, size_t *out_w,
  1561. const char *str) {
  1562. int rv;
  1563. uint32_t uni;
  1564. int w, ix = x;
  1565. if (out_w) {
  1566. *out_w = 0;
  1567. }
  1568. while (*str) {
  1569. str += tb_utf8_char_to_unicode(&uni, str);
  1570. w = wcwidth((wchar_t)uni);
  1571. if (w < 0) {
  1572. w = 1;
  1573. }
  1574. if (w == 0 && x > ix) {
  1575. if_err_return(rv, tb_extend_cell(x - 1, y, uni));
  1576. } else {
  1577. if_err_return(rv, tb_set_cell(x, y, uni, fg, bg));
  1578. }
  1579. x += w;
  1580. if (out_w) {
  1581. *out_w += w;
  1582. }
  1583. }
  1584. return TB_OK;
  1585. }
  1586. int tb_printf(int x, int y, uintattr_t fg, uintattr_t bg, const char *fmt,
  1587. ...) {
  1588. int rv;
  1589. va_list vl;
  1590. va_start(vl, fmt);
  1591. rv = tb_printf_inner(x, y, fg, bg, NULL, fmt, vl);
  1592. va_end(vl);
  1593. return rv;
  1594. }
  1595. int tb_printf_ex(int x, int y, uintattr_t fg, uintattr_t bg, size_t *out_w,
  1596. const char *fmt, ...) {
  1597. int rv;
  1598. va_list vl;
  1599. va_start(vl, fmt);
  1600. rv = tb_printf_inner(x, y, fg, bg, out_w, fmt, vl);
  1601. va_end(vl);
  1602. return rv;
  1603. }
  1604. int tb_send(const char *buf, size_t nbuf) {
  1605. return bytebuf_nputs(&global.out, buf, nbuf);
  1606. }
  1607. int tb_sendf(const char *fmt, ...) {
  1608. int rv;
  1609. char buf[TB_OPT_PRINTF_BUF];
  1610. va_list vl;
  1611. va_start(vl, fmt);
  1612. rv = vsnprintf(buf, sizeof(buf), fmt, vl);
  1613. va_end(vl);
  1614. if (rv < 0 || rv >= (int)sizeof(buf)) {
  1615. return TB_ERR;
  1616. }
  1617. return tb_send(buf, (size_t)rv);
  1618. }
  1619. int tb_set_func(int fn_type, int (*fn)(struct tb_event *, size_t *)) {
  1620. switch (fn_type) {
  1621. case TB_FUNC_EXTRACT_PRE:
  1622. global.fn_extract_esc_pre = fn;
  1623. return TB_OK;
  1624. case TB_FUNC_EXTRACT_POST:
  1625. global.fn_extract_esc_post = fn;
  1626. return TB_OK;
  1627. }
  1628. return TB_ERR;
  1629. }
  1630. struct tb_cell *tb_cell_buffer(void) {
  1631. if (!global.initialized) return NULL;
  1632. return global.back.cells;
  1633. }
  1634. int tb_utf8_char_length(char c) {
  1635. return utf8_length[(unsigned char)c];
  1636. }
  1637. int tb_utf8_char_to_unicode(uint32_t *out, const char *c) {
  1638. if (*c == 0) {
  1639. return TB_ERR;
  1640. }
  1641. int i;
  1642. unsigned char len = tb_utf8_char_length(*c);
  1643. unsigned char mask = utf8_mask[len - 1];
  1644. uint32_t result = c[0] & mask;
  1645. for (i = 1; i < len; ++i) {
  1646. result <<= 6;
  1647. result |= c[i] & 0x3f;
  1648. }
  1649. *out = result;
  1650. return (int)len;
  1651. }
  1652. int tb_utf8_unicode_to_char(char *out, uint32_t c) {
  1653. int len = 0;
  1654. int first;
  1655. int i;
  1656. if (c < 0x80) {
  1657. first = 0;
  1658. len = 1;
  1659. } else if (c < 0x800) {
  1660. first = 0xc0;
  1661. len = 2;
  1662. } else if (c < 0x10000) {
  1663. first = 0xe0;
  1664. len = 3;
  1665. } else if (c < 0x200000) {
  1666. first = 0xf0;
  1667. len = 4;
  1668. } else if (c < 0x4000000) {
  1669. first = 0xf8;
  1670. len = 5;
  1671. } else {
  1672. first = 0xfc;
  1673. len = 6;
  1674. }
  1675. for (i = len - 1; i > 0; --i) {
  1676. out[i] = (c & 0x3f) | 0x80;
  1677. c >>= 6;
  1678. }
  1679. out[0] = c | first;
  1680. return len;
  1681. }
  1682. int tb_last_errno(void) {
  1683. return global.last_errno;
  1684. }
  1685. const char *tb_strerror(int err) {
  1686. switch (err) {
  1687. case TB_OK:
  1688. return "Success";
  1689. case TB_ERR_NEED_MORE:
  1690. return "Not enough input";
  1691. case TB_ERR_INIT_ALREADY:
  1692. return "Termbox initialized already";
  1693. case TB_ERR_MEM:
  1694. return "Out of memory";
  1695. case TB_ERR_NO_EVENT:
  1696. return "No event";
  1697. case TB_ERR_NO_TERM:
  1698. return "No TERM in environment";
  1699. case TB_ERR_NOT_INIT:
  1700. return "Termbox not initialized";
  1701. case TB_ERR_OUT_OF_BOUNDS:
  1702. return "Out of bounds";
  1703. case TB_ERR_UNSUPPORTED_TERM:
  1704. return "Unsupported terminal";
  1705. case TB_ERR_CAP_COLLISION:
  1706. return "Termcaps collision";
  1707. case TB_ERR_RESIZE_SSCANF:
  1708. return "Terminal width/height not received by sscanf() after "
  1709. "resize";
  1710. case TB_ERR:
  1711. case TB_ERR_INIT_OPEN:
  1712. case TB_ERR_READ:
  1713. case TB_ERR_RESIZE_IOCTL:
  1714. case TB_ERR_RESIZE_PIPE:
  1715. case TB_ERR_RESIZE_SIGACTION:
  1716. case TB_ERR_POLL:
  1717. case TB_ERR_TCGETATTR:
  1718. case TB_ERR_TCSETATTR:
  1719. case TB_ERR_RESIZE_WRITE:
  1720. case TB_ERR_RESIZE_POLL:
  1721. case TB_ERR_RESIZE_READ:
  1722. default:
  1723. strerror_r(global.last_errno, global.errbuf, sizeof(global.errbuf));
  1724. return (const char *)global.errbuf;
  1725. }
  1726. }
  1727. int tb_has_truecolor(void) {
  1728. #ifdef TB_OPT_TRUECOLOR
  1729. return 1;
  1730. #else
  1731. return 0;
  1732. #endif
  1733. }
  1734. int tb_has_egc(void) {
  1735. #ifdef TB_OPT_EGC
  1736. return 1;
  1737. #else
  1738. return 0;
  1739. #endif
  1740. }
  1741. const char *tb_version(void) {
  1742. return TB_VERSION_STR;
  1743. }
  1744. static int tb_reset(void) {
  1745. int ttyfd_open = global.ttyfd_open;
  1746. memset(&global, 0, sizeof(global));
  1747. global.ttyfd = -1;
  1748. global.rfd = -1;
  1749. global.wfd = -1;
  1750. global.ttyfd_open = ttyfd_open;
  1751. global.resize_pipefd[0] = -1;
  1752. global.resize_pipefd[1] = -1;
  1753. global.width = -1;
  1754. global.height = -1;
  1755. global.cursor_x = -1;
  1756. global.cursor_y = -1;
  1757. global.last_x = -1;
  1758. global.last_y = -1;
  1759. global.fg = TB_DEFAULT;
  1760. global.bg = TB_DEFAULT;
  1761. global.last_fg = ~global.fg;
  1762. global.last_bg = ~global.bg;
  1763. global.input_mode = TB_INPUT_ESC;
  1764. global.output_mode = TB_OUTPUT_NORMAL;
  1765. return TB_OK;
  1766. }
  1767. static int init_term_attrs(void) {
  1768. if (global.ttyfd < 0) {
  1769. return TB_OK;
  1770. }
  1771. if (tcgetattr(global.ttyfd, &global.orig_tios) != 0) {
  1772. global.last_errno = errno;
  1773. return TB_ERR_TCGETATTR;
  1774. }
  1775. struct termios tios;
  1776. memcpy(&tios, &global.orig_tios, sizeof(tios));
  1777. global.has_orig_tios = 1;
  1778. cfmakeraw(&tios);
  1779. tios.c_cc[VMIN] = 1;
  1780. tios.c_cc[VTIME] = 0;
  1781. if (tcsetattr(global.ttyfd, TCSAFLUSH, &tios) != 0) {
  1782. global.last_errno = errno;
  1783. return TB_ERR_TCSETATTR;
  1784. }
  1785. return TB_OK;
  1786. }
  1787. int tb_printf_inner(int x, int y, uintattr_t fg, uintattr_t bg, size_t *out_w,
  1788. const char *fmt, va_list vl) {
  1789. int rv;
  1790. char buf[TB_OPT_PRINTF_BUF];
  1791. rv = vsnprintf(buf, sizeof(buf), fmt, vl);
  1792. if (rv < 0 || rv >= (int)sizeof(buf)) {
  1793. return TB_ERR;
  1794. }
  1795. return tb_print_ex(x, y, fg, bg, out_w, buf);
  1796. }
  1797. static int init_term_caps(void) {
  1798. if (load_terminfo() == TB_OK) {
  1799. return parse_terminfo_caps();
  1800. }
  1801. return load_builtin_caps();
  1802. }
  1803. static int init_cap_trie(void) {
  1804. int rv, i;
  1805. // Add caps from terminfo or built-in
  1806. for (i = 0; i < TB_CAP__COUNT_KEYS; i++) {
  1807. if_err_return(rv, cap_trie_add(global.caps[i], tb_key_i(i), 0));
  1808. }
  1809. // Add built-in mod caps
  1810. for (i = 0; builtin_mod_caps[i].cap != NULL; i++) {
  1811. rv = cap_trie_add(builtin_mod_caps[i].cap, builtin_mod_caps[i].key,
  1812. builtin_mod_caps[i].mod);
  1813. // Collisions are OK. This can happen if global.caps collides with
  1814. // builtin_mod_caps. It is desirable to give precedence to global.caps
  1815. // here.
  1816. if (rv != TB_OK && rv != TB_ERR_CAP_COLLISION) {
  1817. return rv;
  1818. }
  1819. }
  1820. return TB_OK;
  1821. }
  1822. static int cap_trie_add(const char *cap, uint16_t key, uint8_t mod) {
  1823. struct cap_trie_t *next, *node = &global.cap_trie;
  1824. size_t i, j;
  1825. for (i = 0; cap[i] != '\0'; i++) {
  1826. char c = cap[i];
  1827. next = NULL;
  1828. // Check if c is already a child of node
  1829. for (j = 0; j < node->nchildren; j++) {
  1830. if (node->children[j].c == c) {
  1831. next = &node->children[j];
  1832. break;
  1833. }
  1834. }
  1835. if (!next) {
  1836. // We need to add a new child to node
  1837. node->nchildren += 1;
  1838. node->children =
  1839. tb_realloc(node->children, sizeof(*node) * node->nchildren);
  1840. if (!node->children) {
  1841. return TB_ERR_MEM;
  1842. }
  1843. next = &node->children[node->nchildren - 1];
  1844. memset(next, 0, sizeof(*next));
  1845. next->c = c;
  1846. }
  1847. // Continue
  1848. node = next;
  1849. }
  1850. if (node->is_leaf) {
  1851. // Already a leaf here
  1852. return TB_ERR_CAP_COLLISION;
  1853. }
  1854. node->is_leaf = 1;
  1855. node->key = key;
  1856. node->mod = mod;
  1857. return TB_OK;
  1858. }
  1859. static int cap_trie_find(const char *buf, size_t nbuf, struct cap_trie_t **last,
  1860. size_t *depth) {
  1861. struct cap_trie_t *next, *node = &global.cap_trie;
  1862. size_t i, j;
  1863. *last = node;
  1864. *depth = 0;
  1865. for (i = 0; i < nbuf; i++) {
  1866. char c = buf[i];
  1867. next = NULL;
  1868. // Find c in node.children
  1869. for (j = 0; j < node->nchildren; j++) {
  1870. if (node->children[j].c == c) {
  1871. next = &node->children[j];
  1872. break;
  1873. }
  1874. }
  1875. if (!next) {
  1876. // Not found
  1877. return TB_OK;
  1878. }
  1879. node = next;
  1880. *last = node;
  1881. *depth += 1;
  1882. if (node->is_leaf && node->nchildren < 1) {
  1883. break;
  1884. }
  1885. }
  1886. return TB_OK;
  1887. }
  1888. static int cap_trie_deinit(struct cap_trie_t *node) {
  1889. size_t j;
  1890. for (j = 0; j < node->nchildren; j++) {
  1891. cap_trie_deinit(&node->children[j]);
  1892. }
  1893. if (node->children) {
  1894. tb_free(node->children);
  1895. }
  1896. memset(node, 0, sizeof(*node));
  1897. return TB_OK;
  1898. }
  1899. static int init_resize_handler(void) {
  1900. if (pipe(global.resize_pipefd) != 0) {
  1901. global.last_errno = errno;
  1902. return TB_ERR_RESIZE_PIPE;
  1903. }
  1904. struct sigaction sa;
  1905. memset(&sa, 0, sizeof(sa));
  1906. sa.sa_handler = handle_resize;
  1907. if (sigaction(SIGWINCH, &sa, NULL) != 0) {
  1908. global.last_errno = errno;
  1909. return TB_ERR_RESIZE_SIGACTION;
  1910. }
  1911. return TB_OK;
  1912. }
  1913. static int send_init_escape_codes(void) {
  1914. int rv;
  1915. if_err_return(rv, bytebuf_puts(&global.out, global.caps[TB_CAP_ENTER_CA]));
  1916. if_err_return(rv,
  1917. bytebuf_puts(&global.out, global.caps[TB_CAP_ENTER_KEYPAD]));
  1918. if_err_return(rv,
  1919. bytebuf_puts(&global.out, global.caps[TB_CAP_HIDE_CURSOR]));
  1920. return TB_OK;
  1921. }
  1922. static int send_clear(void) {
  1923. int rv;
  1924. if_err_return(rv, send_attr(global.fg, global.bg));
  1925. if_err_return(rv,
  1926. bytebuf_puts(&global.out, global.caps[TB_CAP_CLEAR_SCREEN]));
  1927. if_err_return(rv, send_cursor_if(global.cursor_x, global.cursor_y));
  1928. if_err_return(rv, bytebuf_flush(&global.out, global.wfd));
  1929. global.last_x = -1;
  1930. global.last_y = -1;
  1931. return TB_OK;
  1932. }
  1933. static int update_term_size(void) {
  1934. int rv, ioctl_errno;
  1935. if (global.ttyfd < 0) {
  1936. return TB_OK;
  1937. }
  1938. struct winsize sz;
  1939. memset(&sz, 0, sizeof(sz));
  1940. // Try ioctl TIOCGWINSZ
  1941. if (ioctl(global.ttyfd, TIOCGWINSZ, &sz) == 0) {
  1942. global.width = sz.ws_col;
  1943. global.height = sz.ws_row;
  1944. return TB_OK;
  1945. }
  1946. ioctl_errno = errno;
  1947. // Try >cursor(9999,9999), >u7, <u6
  1948. if_ok_return(rv, update_term_size_via_esc());
  1949. global.last_errno = ioctl_errno;
  1950. return TB_ERR_RESIZE_IOCTL;
  1951. }
  1952. static int update_term_size_via_esc(void) {
  1953. #ifndef TB_RESIZE_FALLBACK_MS
  1954. #define TB_RESIZE_FALLBACK_MS 1000
  1955. #endif
  1956. char *move_and_report = "\x1b[9999;9999H\x1b[6n";
  1957. ssize_t write_rv =
  1958. write(global.wfd, move_and_report, strlen(move_and_report));
  1959. if (write_rv != (ssize_t)strlen(move_and_report)) {
  1960. return TB_ERR_RESIZE_WRITE;
  1961. }
  1962. fd_set fds;
  1963. FD_ZERO(&fds);
  1964. FD_SET(global.rfd, &fds);
  1965. struct timeval timeout;
  1966. timeout.tv_sec = 0;
  1967. timeout.tv_usec = TB_RESIZE_FALLBACK_MS * 1000;
  1968. int select_rv = select(global.rfd + 1, &fds, NULL, NULL, &timeout);
  1969. if (select_rv != 1) {
  1970. global.last_errno = errno;
  1971. return TB_ERR_RESIZE_POLL;
  1972. }
  1973. char buf[TB_OPT_READ_BUF];
  1974. ssize_t read_rv = read(global.rfd, buf, sizeof(buf) - 1);
  1975. if (read_rv < 1) {
  1976. global.last_errno = errno;
  1977. return TB_ERR_RESIZE_READ;
  1978. }
  1979. buf[read_rv] = '\0';
  1980. int rw, rh;
  1981. if (sscanf(buf, "\x1b[%d;%dR", &rh, &rw) != 2) {
  1982. return TB_ERR_RESIZE_SSCANF;
  1983. }
  1984. global.width = rw;
  1985. global.height = rh;
  1986. return TB_OK;
  1987. }
  1988. static int init_cellbuf(void) {
  1989. int rv;
  1990. if_err_return(rv, cellbuf_init(&global.back, global.width, global.height));
  1991. if_err_return(rv, cellbuf_init(&global.front, global.width, global.height));
  1992. if_err_return(rv, cellbuf_clear(&global.back));
  1993. if_err_return(rv, cellbuf_clear(&global.front));
  1994. return TB_OK;
  1995. }
  1996. static int tb_deinit(void) {
  1997. if (global.caps[0] != NULL && global.wfd >= 0) {
  1998. bytebuf_puts(&global.out, global.caps[TB_CAP_SHOW_CURSOR]);
  1999. bytebuf_puts(&global.out, global.caps[TB_CAP_SGR0]);
  2000. bytebuf_puts(&global.out, global.caps[TB_CAP_CLEAR_SCREEN]);
  2001. bytebuf_puts(&global.out, global.caps[TB_CAP_EXIT_CA]);
  2002. bytebuf_puts(&global.out, global.caps[TB_CAP_EXIT_KEYPAD]);
  2003. bytebuf_puts(&global.out, TB_HARDCAP_EXIT_MOUSE);
  2004. bytebuf_flush(&global.out, global.wfd);
  2005. }
  2006. if (global.ttyfd >= 0) {
  2007. if (global.has_orig_tios) {
  2008. tcsetattr(global.ttyfd, TCSAFLUSH, &global.orig_tios);
  2009. }
  2010. if (global.ttyfd_open) {
  2011. close(global.ttyfd);
  2012. global.ttyfd_open = 0;
  2013. }
  2014. }
  2015. sigaction(SIGWINCH, &(struct sigaction){.sa_handler = SIG_DFL}, NULL);
  2016. if (global.resize_pipefd[0] >= 0) close(global.resize_pipefd[0]);
  2017. if (global.resize_pipefd[1] >= 0) close(global.resize_pipefd[1]);
  2018. cellbuf_free(&global.back);
  2019. cellbuf_free(&global.front);
  2020. bytebuf_free(&global.in);
  2021. bytebuf_free(&global.out);
  2022. if (global.terminfo) tb_free(global.terminfo);
  2023. cap_trie_deinit(&global.cap_trie);
  2024. tb_reset();
  2025. return TB_OK;
  2026. }
  2027. static int load_terminfo(void) {
  2028. int rv;
  2029. char tmp[TB_PATH_MAX];
  2030. // See terminfo(5) "Fetching Compiled Descriptions" for a description of
  2031. // this behavior. Some of these paths are compile-time ncurses options, so
  2032. // best guesses are used here.
  2033. const char *term = getenv("TERM");
  2034. if (!term) {
  2035. return TB_ERR;
  2036. }
  2037. // If TERMINFO is set, try that directory and stop
  2038. const char *terminfo = getenv("TERMINFO");
  2039. if (terminfo) {
  2040. return load_terminfo_from_path(terminfo, term);
  2041. }
  2042. // Next try ~/.terminfo
  2043. const char *home = getenv("HOME");
  2044. if (home) {
  2045. snprintf_or_return(rv, tmp, sizeof(tmp), "%s/.terminfo", home);
  2046. if_ok_return(rv, load_terminfo_from_path(tmp, term));
  2047. }
  2048. // Next try TERMINFO_DIRS
  2049. //
  2050. // Note, empty entries are supposed to be interpretted as the "compiled-in
  2051. // default", which is of course system-dependent. Previously /etc/terminfo
  2052. // was used here. Let's skip empty entries altogether rather than give
  2053. // precedence to a guess, and check common paths after this loop.
  2054. const char *dirs = getenv("TERMINFO_DIRS");
  2055. if (dirs) {
  2056. snprintf_or_return(rv, tmp, sizeof(tmp), "%s", dirs);
  2057. char *dir = strtok(tmp, ":");
  2058. while (dir) {
  2059. const char *cdir = dir;
  2060. if (*cdir != '\0') {
  2061. if_ok_return(rv, load_terminfo_from_path(cdir, term));
  2062. }
  2063. dir = strtok(NULL, ":");
  2064. }
  2065. }
  2066. #ifdef TB_TERMINFO_DIR
  2067. if_ok_return(rv, load_terminfo_from_path(TB_TERMINFO_DIR, term));
  2068. #endif
  2069. if_ok_return(rv, load_terminfo_from_path("/usr/local/etc/terminfo", term));
  2070. if_ok_return(rv,
  2071. load_terminfo_from_path("/usr/local/share/terminfo", term));
  2072. if_ok_return(rv, load_terminfo_from_path("/usr/local/lib/terminfo", term));
  2073. if_ok_return(rv, load_terminfo_from_path("/etc/terminfo", term));
  2074. if_ok_return(rv, load_terminfo_from_path("/usr/share/terminfo", term));
  2075. if_ok_return(rv, load_terminfo_from_path("/usr/lib/terminfo", term));
  2076. if_ok_return(rv, load_terminfo_from_path("/usr/share/lib/terminfo", term));
  2077. if_ok_return(rv, load_terminfo_from_path("/lib/terminfo", term));
  2078. return TB_ERR;
  2079. }
  2080. static int load_terminfo_from_path(const char *path, const char *term) {
  2081. int rv;
  2082. char tmp[TB_PATH_MAX];
  2083. // Look for term at this terminfo location, e.g., <terminfo>/x/xterm
  2084. snprintf_or_return(rv, tmp, sizeof(tmp), "%s/%c/%s", path, term[0], term);
  2085. if_ok_return(rv, read_terminfo_path(tmp));
  2086. #ifdef __APPLE__
  2087. // Try the Darwin equivalent path, e.g., <terminfo>/78/xterm
  2088. snprintf_or_return(rv, tmp, sizeof(tmp), "%s/%x/%s", path, term[0], term);
  2089. return read_terminfo_path(tmp);
  2090. #endif
  2091. return TB_ERR;
  2092. }
  2093. static int read_terminfo_path(const char *path) {
  2094. FILE *fp = fopen(path, "rb");
  2095. if (!fp) {
  2096. return TB_ERR;
  2097. }
  2098. struct stat st;
  2099. if (fstat(fileno(fp), &st) != 0) {
  2100. fclose(fp);
  2101. return TB_ERR;
  2102. }
  2103. size_t fsize = st.st_size;
  2104. char *data = tb_malloc(fsize);
  2105. if (!data) {
  2106. fclose(fp);
  2107. return TB_ERR;
  2108. }
  2109. if (fread(data, 1, fsize, fp) != fsize) {
  2110. fclose(fp);
  2111. tb_free(data);
  2112. return TB_ERR;
  2113. }
  2114. global.terminfo = data;
  2115. global.nterminfo = fsize;
  2116. fclose(fp);
  2117. return TB_OK;
  2118. }
  2119. static int parse_terminfo_caps(void) {
  2120. // See term(5) "LEGACY STORAGE FORMAT" and "EXTENDED STORAGE FORMAT" for a
  2121. // description of this behavior.
  2122. // Ensure there's at least a header's worth of data
  2123. if (global.nterminfo < 6) {
  2124. return TB_ERR;
  2125. }
  2126. int16_t *header = (int16_t *)global.terminfo;
  2127. // header[0] the magic number (octal 0432 or 01036)
  2128. // header[1] the size, in bytes, of the names section
  2129. // header[2] the number of bytes in the boolean section
  2130. // header[3] the number of short integers in the numbers section
  2131. // header[4] the number of offsets (short integers) in the strings section
  2132. // header[5] the size, in bytes, of the string table
  2133. // Legacy ints are 16-bit, extended ints are 32-bit
  2134. const int bytes_per_int = header[0] == 01036 ? 4 // 32-bit
  2135. : 2; // 16-bit
  2136. // > Between the boolean section and the number section, a null byte will be
  2137. // > inserted, if necessary, to ensure that the number section begins on an
  2138. // > even byte
  2139. const int align_offset = (header[1] + header[2]) % 2 != 0 ? 1 : 0;
  2140. const int pos_str_offsets =
  2141. (6 * sizeof(int16_t)) // header (12 bytes)
  2142. + header[1] // length of names section
  2143. + header[2] // length of boolean section
  2144. + align_offset +
  2145. (header[3] * bytes_per_int); // length of numbers section
  2146. const int pos_str_table =
  2147. pos_str_offsets +
  2148. (header[4] * sizeof(int16_t)); // length of string offsets table
  2149. // Load caps
  2150. int i;
  2151. for (i = 0; i < TB_CAP__COUNT; i++) {
  2152. const char *cap = get_terminfo_string(pos_str_offsets, pos_str_table,
  2153. header[5], terminfo_cap_indexes[i]);
  2154. if (!cap) {
  2155. // Something is not right
  2156. return TB_ERR;
  2157. }
  2158. global.caps[i] = cap;
  2159. }
  2160. return TB_OK;
  2161. }
  2162. static int load_builtin_caps(void) {
  2163. int i, j;
  2164. const char *term = getenv("TERM");
  2165. if (!term) {
  2166. return TB_ERR_NO_TERM;
  2167. }
  2168. // Check for exact TERM match
  2169. for (i = 0; builtin_terms[i].name != NULL; i++) {
  2170. if (strcmp(term, builtin_terms[i].name) == 0) {
  2171. for (j = 0; j < TB_CAP__COUNT; j++) {
  2172. global.caps[j] = builtin_terms[i].caps[j];
  2173. }
  2174. return TB_OK;
  2175. }
  2176. }
  2177. // Check for partial TERM or alias match
  2178. for (i = 0; builtin_terms[i].name != NULL; i++) {
  2179. if (strstr(term, builtin_terms[i].name) != NULL ||
  2180. (*(builtin_terms[i].alias) != '\0' &&
  2181. strstr(term, builtin_terms[i].alias) != NULL))
  2182. {
  2183. for (j = 0; j < TB_CAP__COUNT; j++) {
  2184. global.caps[j] = builtin_terms[i].caps[j];
  2185. }
  2186. return TB_OK;
  2187. }
  2188. }
  2189. return TB_ERR_UNSUPPORTED_TERM;
  2190. }
  2191. static const char *get_terminfo_string(int16_t str_offsets_pos,
  2192. int16_t str_table_pos, int16_t str_table_len, int16_t str_index) {
  2193. const int16_t *str_offset =
  2194. (int16_t *)(global.terminfo + (int)str_offsets_pos +
  2195. ((int)str_index * (int)sizeof(int16_t)));
  2196. if (*str_offset < 0) {
  2197. // A negative indicates the cap is absent from this terminal
  2198. return "";
  2199. }
  2200. if (*str_offset >= str_table_len) {
  2201. // Invalid string offset
  2202. return NULL;
  2203. }
  2204. if (((size_t)((int)str_table_pos + (int)*str_offset)) >= global.nterminfo) {
  2205. // Truncated/corrupt terminfo?
  2206. return NULL;
  2207. }
  2208. return (
  2209. const char *)(global.terminfo + (int)str_table_pos + (int)*str_offset);
  2210. }
  2211. static int wait_event(struct tb_event *event, int timeout) {
  2212. int rv;
  2213. char buf[TB_OPT_READ_BUF];
  2214. memset(event, 0, sizeof(*event));
  2215. if_ok_return(rv, extract_event(event));
  2216. fd_set fds;
  2217. struct timeval tv;
  2218. tv.tv_sec = timeout / 1000;
  2219. tv.tv_usec = (timeout - (tv.tv_sec * 1000)) * 1000;
  2220. do {
  2221. FD_ZERO(&fds);
  2222. FD_SET(global.rfd, &fds);
  2223. FD_SET(global.resize_pipefd[0], &fds);
  2224. int maxfd = global.resize_pipefd[0] > global.rfd
  2225. ? global.resize_pipefd[0]
  2226. : global.rfd;
  2227. int select_rv =
  2228. select(maxfd + 1, &fds, NULL, NULL, (timeout < 0) ? NULL : &tv);
  2229. if (select_rv < 0) {
  2230. // Let EINTR/EAGAIN bubble up
  2231. global.last_errno = errno;
  2232. return TB_ERR_POLL;
  2233. } else if (select_rv == 0) {
  2234. return TB_ERR_NO_EVENT;
  2235. }
  2236. int tty_has_events = (FD_ISSET(global.rfd, &fds));
  2237. int resize_has_events = (FD_ISSET(global.resize_pipefd[0], &fds));
  2238. if (tty_has_events) {
  2239. ssize_t read_rv = read(global.rfd, buf, sizeof(buf));
  2240. if (read_rv < 0) {
  2241. global.last_errno = errno;
  2242. return TB_ERR_READ;
  2243. } else if (read_rv > 0) {
  2244. bytebuf_nputs(&global.in, buf, read_rv);
  2245. }
  2246. }
  2247. if (resize_has_events) {
  2248. int ignore = 0;
  2249. read(global.resize_pipefd[0], &ignore, sizeof(ignore));
  2250. // TODO Harden against errors encountered mid-resize
  2251. if_err_return(rv, update_term_size());
  2252. if_err_return(rv, resize_cellbufs());
  2253. event->type = TB_EVENT_RESIZE;
  2254. event->w = global.width;
  2255. event->h = global.height;
  2256. return TB_OK;
  2257. }
  2258. memset(event, 0, sizeof(*event));
  2259. if_ok_return(rv, extract_event(event));
  2260. } while (timeout == -1);
  2261. return rv;
  2262. }
  2263. static int extract_event(struct tb_event *event) {
  2264. int rv;
  2265. struct bytebuf_t *in = &global.in;
  2266. if (in->len == 0) {
  2267. return TB_ERR;
  2268. }
  2269. if (in->buf[0] == '\x1b') {
  2270. // Escape sequence?
  2271. // In TB_INPUT_ESC, skip if the buffer is a single escape char
  2272. if (!((global.input_mode & TB_INPUT_ESC) && in->len == 1)) {
  2273. if_ok_or_need_more_return(rv, extract_esc(event));
  2274. }
  2275. // Escape key?
  2276. if (global.input_mode & TB_INPUT_ESC) {
  2277. event->type = TB_EVENT_KEY;
  2278. event->ch = 0;
  2279. event->key = TB_KEY_ESC;
  2280. event->mod = 0;
  2281. bytebuf_shift(in, 1);
  2282. return TB_OK;
  2283. }
  2284. // Recurse for alt key
  2285. event->mod |= TB_MOD_ALT;
  2286. bytebuf_shift(in, 1);
  2287. return extract_event(event);
  2288. }
  2289. // ASCII control key?
  2290. if ((uint16_t)in->buf[0] < TB_KEY_SPACE || in->buf[0] == TB_KEY_BACKSPACE2)
  2291. {
  2292. event->type = TB_EVENT_KEY;
  2293. event->ch = 0;
  2294. event->key = (uint16_t)in->buf[0];
  2295. event->mod |= TB_MOD_CTRL;
  2296. bytebuf_shift(in, 1);
  2297. return TB_OK;
  2298. }
  2299. // UTF-8?
  2300. if (in->len >= (size_t)tb_utf8_char_length(in->buf[0])) {
  2301. event->type = TB_EVENT_KEY;
  2302. tb_utf8_char_to_unicode(&event->ch, in->buf);
  2303. event->key = 0;
  2304. bytebuf_shift(in, tb_utf8_char_length(in->buf[0]));
  2305. return TB_OK;
  2306. }
  2307. // Need more input
  2308. return TB_ERR;
  2309. }
  2310. static int extract_esc(struct tb_event *event) {
  2311. int rv;
  2312. if_ok_or_need_more_return(rv, extract_esc_user(event, 0));
  2313. if_ok_or_need_more_return(rv, extract_esc_cap(event));
  2314. if_ok_or_need_more_return(rv, extract_esc_mouse(event));
  2315. if_ok_or_need_more_return(rv, extract_esc_user(event, 1));
  2316. return TB_ERR;
  2317. }
  2318. static int extract_esc_user(struct tb_event *event, int is_post) {
  2319. int rv;
  2320. size_t consumed = 0;
  2321. struct bytebuf_t *in = &global.in;
  2322. int (*fn)(struct tb_event *, size_t *);
  2323. fn = is_post ? global.fn_extract_esc_post : global.fn_extract_esc_pre;
  2324. if (!fn) {
  2325. return TB_ERR;
  2326. }
  2327. rv = fn(event, &consumed);
  2328. if (rv == TB_OK) {
  2329. bytebuf_shift(in, consumed);
  2330. }
  2331. if_ok_or_need_more_return(rv, rv);
  2332. return TB_ERR;
  2333. }
  2334. static int extract_esc_cap(struct tb_event *event) {
  2335. int rv;
  2336. struct bytebuf_t *in = &global.in;
  2337. struct cap_trie_t *node;
  2338. size_t depth;
  2339. if_err_return(rv, cap_trie_find(in->buf, in->len, &node, &depth));
  2340. if (node->is_leaf) {
  2341. // Found a leaf node
  2342. event->type = TB_EVENT_KEY;
  2343. event->ch = 0;
  2344. event->key = node->key;
  2345. event->mod = node->mod;
  2346. bytebuf_shift(in, depth);
  2347. return TB_OK;
  2348. } else if (node->nchildren > 0 && in->len <= depth) {
  2349. // Found a branch node (not enough input)
  2350. return TB_ERR_NEED_MORE;
  2351. }
  2352. return TB_ERR;
  2353. }
  2354. static int extract_esc_mouse(struct tb_event *event) {
  2355. struct bytebuf_t *in = &global.in;
  2356. enum type { TYPE_VT200 = 0, TYPE_1006, TYPE_1015, TYPE_MAX };
  2357. char *cmp[TYPE_MAX] = {//
  2358. // X10 mouse encoding, the simplest one
  2359. // \x1b [ M Cb Cx Cy
  2360. [TYPE_VT200] = "\x1b[M",
  2361. // xterm 1006 extended mode or urxvt 1015 extended mode
  2362. // xterm: \x1b [ < Cb ; Cx ; Cy (M or m)
  2363. [TYPE_1006] = "\x1b[<",
  2364. // urxvt: \x1b [ Cb ; Cx ; Cy M
  2365. [TYPE_1015] = "\x1b["};
  2366. enum type type = 0;
  2367. int ret = TB_ERR;
  2368. // Unrolled at compile-time (probably)
  2369. for (; type < TYPE_MAX; type++) {
  2370. size_t size = strlen(cmp[type]);
  2371. if (in->len >= size && (strncmp(cmp[type], in->buf, size)) == 0) {
  2372. break;
  2373. }
  2374. }
  2375. if (type == TYPE_MAX) {
  2376. ret = TB_ERR; // No match
  2377. return ret;
  2378. }
  2379. size_t buf_shift = 0;
  2380. switch (type) {
  2381. case TYPE_VT200:
  2382. if (in->len >= 6) {
  2383. int b = in->buf[3] - 0x20;
  2384. int fail = 0;
  2385. switch (b & 3) {
  2386. case 0:
  2387. event->key = ((b & 64) != 0) ? TB_KEY_MOUSE_WHEEL_UP
  2388. : TB_KEY_MOUSE_LEFT;
  2389. break;
  2390. case 1:
  2391. event->key = ((b & 64) != 0) ? TB_KEY_MOUSE_WHEEL_DOWN
  2392. : TB_KEY_MOUSE_MIDDLE;
  2393. break;
  2394. case 2:
  2395. event->key = TB_KEY_MOUSE_RIGHT;
  2396. break;
  2397. case 3:
  2398. event->key = TB_KEY_MOUSE_RELEASE;
  2399. break;
  2400. default:
  2401. ret = TB_ERR;
  2402. fail = 1;
  2403. break;
  2404. }
  2405. if (!fail) {
  2406. if ((b & 32) != 0) {
  2407. event->mod |= TB_MOD_MOTION;
  2408. }
  2409. // the coord is 1,1 for upper left
  2410. event->x = ((uint8_t)in->buf[4]) - 0x21;
  2411. event->y = ((uint8_t)in->buf[5]) - 0x21;
  2412. ret = TB_OK;
  2413. }
  2414. buf_shift = 6;
  2415. }
  2416. break;
  2417. case TYPE_1006:
  2418. // fallthrough
  2419. case TYPE_1015: {
  2420. size_t index_fail = (size_t)-1;
  2421. enum {
  2422. FIRST_M = 0,
  2423. FIRST_SEMICOLON,
  2424. LAST_SEMICOLON,
  2425. FIRST_LAST_MAX
  2426. };
  2427. size_t indices[FIRST_LAST_MAX] = {index_fail, index_fail,
  2428. index_fail};
  2429. int m_is_capital = 0;
  2430. for (size_t i = 0; i < in->len; i++) {
  2431. if (in->buf[i] == ';') {
  2432. if (indices[FIRST_SEMICOLON] == index_fail) {
  2433. indices[FIRST_SEMICOLON] = i;
  2434. } else {
  2435. indices[LAST_SEMICOLON] = i;
  2436. }
  2437. } else if (indices[FIRST_M] == index_fail) {
  2438. if (in->buf[i] == 'm' || in->buf[i] == 'M') {
  2439. m_is_capital = (in->buf[i] == 'M');
  2440. indices[FIRST_M] = i;
  2441. }
  2442. }
  2443. }
  2444. if (indices[FIRST_M] == index_fail ||
  2445. indices[FIRST_SEMICOLON] == index_fail ||
  2446. indices[LAST_SEMICOLON] == index_fail)
  2447. {
  2448. ret = TB_ERR;
  2449. } else {
  2450. int start = (type == TYPE_1015 ? 2 : 3);
  2451. unsigned n1 = strtoul(&in->buf[start], NULL, 10);
  2452. unsigned n2 =
  2453. strtoul(&in->buf[indices[FIRST_SEMICOLON] + 1], NULL, 10);
  2454. unsigned n3 =
  2455. strtoul(&in->buf[indices[LAST_SEMICOLON] + 1], NULL, 10);
  2456. if (type == TYPE_1015) {
  2457. n1 -= 0x20;
  2458. }
  2459. int fail = 0;
  2460. switch (n1 & 3) {
  2461. case 0:
  2462. event->key = ((n1 & 64) != 0) ? TB_KEY_MOUSE_WHEEL_UP
  2463. : TB_KEY_MOUSE_LEFT;
  2464. break;
  2465. case 1:
  2466. event->key = ((n1 & 64) != 0) ? TB_KEY_MOUSE_WHEEL_DOWN
  2467. : TB_KEY_MOUSE_MIDDLE;
  2468. break;
  2469. case 2:
  2470. event->key = TB_KEY_MOUSE_RIGHT;
  2471. break;
  2472. case 3:
  2473. event->key = TB_KEY_MOUSE_RELEASE;
  2474. break;
  2475. default:
  2476. ret = TB_ERR;
  2477. fail = 1;
  2478. break;
  2479. }
  2480. buf_shift = in->len;
  2481. if (!fail) {
  2482. if (!m_is_capital) {
  2483. // on xterm mouse release is signaled by lowercase m
  2484. event->key = TB_KEY_MOUSE_RELEASE;
  2485. }
  2486. if ((n1 & 32) != 0) {
  2487. event->mod |= TB_MOD_MOTION;
  2488. }
  2489. event->x = ((uint8_t)n2) - 1;
  2490. event->y = ((uint8_t)n3) - 1;
  2491. ret = TB_OK;
  2492. }
  2493. }
  2494. } break;
  2495. case TYPE_MAX:
  2496. ret = TB_ERR;
  2497. }
  2498. if (buf_shift > 0) {
  2499. bytebuf_shift(in, buf_shift);
  2500. }
  2501. if (ret == TB_OK) {
  2502. event->type = TB_EVENT_MOUSE;
  2503. }
  2504. return ret;
  2505. }
  2506. static int resize_cellbufs(void) {
  2507. int rv;
  2508. if_err_return(rv,
  2509. cellbuf_resize(&global.back, global.width, global.height));
  2510. if_err_return(rv,
  2511. cellbuf_resize(&global.front, global.width, global.height));
  2512. if_err_return(rv, cellbuf_clear(&global.front));
  2513. if_err_return(rv, send_clear());
  2514. return TB_OK;
  2515. }
  2516. static void handle_resize(int sig) {
  2517. int errno_copy = errno;
  2518. write(global.resize_pipefd[1], &sig, sizeof(sig));
  2519. errno = errno_copy;
  2520. }
  2521. static int send_attr(uintattr_t fg, uintattr_t bg) {
  2522. int rv;
  2523. if (fg == global.last_fg && bg == global.last_bg) {
  2524. return TB_OK;
  2525. }
  2526. if_err_return(rv, bytebuf_puts(&global.out, global.caps[TB_CAP_SGR0]));
  2527. uintattr_t cfg, cbg;
  2528. switch (global.output_mode) {
  2529. default:
  2530. case TB_OUTPUT_NORMAL:
  2531. cfg = fg & 0x0f;
  2532. cbg = bg & 0x0f;
  2533. break;
  2534. case TB_OUTPUT_256:
  2535. cfg = fg & 0xff;
  2536. cbg = bg & 0xff;
  2537. if (fg & TB_256_BLACK) cfg = 0;
  2538. if (bg & TB_256_BLACK) cbg = 0;
  2539. break;
  2540. case TB_OUTPUT_216:
  2541. cfg = fg & 0xff;
  2542. cbg = bg & 0xff;
  2543. if (cfg > 216) cfg = 216;
  2544. if (cbg > 216) cbg = 216;
  2545. cfg += 0x0f;
  2546. cbg += 0x0f;
  2547. break;
  2548. case TB_OUTPUT_GRAYSCALE:
  2549. cfg = fg & 0xff;
  2550. cbg = bg & 0xff;
  2551. if (cfg > 24) cfg = 24;
  2552. if (cbg > 24) cbg = 24;
  2553. cfg += 0xe7;
  2554. cbg += 0xe7;
  2555. break;
  2556. #ifdef TB_OPT_TRUECOLOR
  2557. case TB_OUTPUT_TRUECOLOR:
  2558. cfg = fg & 0xffffff;
  2559. cbg = bg & 0xffffff;
  2560. if (fg & TB_TRUECOLOR_BLACK) cfg = 0;
  2561. if (bg & TB_TRUECOLOR_BLACK) cbg = 0;
  2562. break;
  2563. #endif
  2564. }
  2565. uintattr_t attr_bold, attr_blink, attr_italic, attr_underline, attr_reverse;
  2566. #ifdef TB_OPT_TRUECOLOR
  2567. if (global.output_mode == TB_OUTPUT_TRUECOLOR) {
  2568. attr_bold = TB_TRUECOLOR_BOLD;
  2569. attr_blink = TB_TRUECOLOR_BLINK;
  2570. attr_italic = TB_TRUECOLOR_ITALIC;
  2571. attr_underline = TB_TRUECOLOR_UNDERLINE;
  2572. attr_reverse = TB_TRUECOLOR_REVERSE;
  2573. } else
  2574. #endif
  2575. {
  2576. attr_bold = TB_BOLD;
  2577. attr_blink = TB_BLINK;
  2578. attr_italic = TB_ITALIC;
  2579. attr_underline = TB_UNDERLINE;
  2580. attr_reverse = TB_REVERSE;
  2581. }
  2582. if (fg & attr_bold)
  2583. if_err_return(rv, bytebuf_puts(&global.out, global.caps[TB_CAP_BOLD]));
  2584. if (fg & attr_blink)
  2585. if_err_return(rv, bytebuf_puts(&global.out, global.caps[TB_CAP_BLINK]));
  2586. if (fg & attr_underline)
  2587. if_err_return(rv,
  2588. bytebuf_puts(&global.out, global.caps[TB_CAP_UNDERLINE]));
  2589. if (fg & attr_italic)
  2590. if_err_return(rv,
  2591. bytebuf_puts(&global.out, global.caps[TB_CAP_ITALIC]));
  2592. if ((fg & attr_reverse) || (bg & attr_reverse))
  2593. if_err_return(rv,
  2594. bytebuf_puts(&global.out, global.caps[TB_CAP_REVERSE]));
  2595. int fg_is_default = (fg & 0xff) == 0;
  2596. int bg_is_default = (bg & 0xff) == 0;
  2597. if (global.output_mode == TB_OUTPUT_256) {
  2598. if (fg & TB_256_BLACK) fg_is_default = 0;
  2599. if (bg & TB_256_BLACK) bg_is_default = 0;
  2600. }
  2601. #ifdef TB_OPT_TRUECOLOR
  2602. if (global.output_mode == TB_OUTPUT_TRUECOLOR) {
  2603. fg_is_default =
  2604. ((fg & 0xffffff) == 0) && ((fg & TB_TRUECOLOR_BLACK) == 0);
  2605. bg_is_default =
  2606. ((bg & 0xffffff) == 0) && ((bg & TB_TRUECOLOR_BLACK) == 0);
  2607. }
  2608. #endif
  2609. if_err_return(rv, send_sgr(cfg, cbg, fg_is_default, bg_is_default));
  2610. global.last_fg = fg;
  2611. global.last_bg = bg;
  2612. return TB_OK;
  2613. }
  2614. static int send_sgr(uintattr_t cfg, uintattr_t cbg, int fg_is_default,
  2615. int bg_is_default) {
  2616. int rv;
  2617. char nbuf[32];
  2618. if (fg_is_default && bg_is_default) {
  2619. return TB_OK;
  2620. }
  2621. switch (global.output_mode) {
  2622. default:
  2623. case TB_OUTPUT_NORMAL:
  2624. send_literal(rv, "\x1b[");
  2625. if (!fg_is_default) {
  2626. send_literal(rv, "3");
  2627. send_num(rv, nbuf, cfg - 1);
  2628. if (!bg_is_default) {
  2629. send_literal(rv, ";");
  2630. }
  2631. }
  2632. if (!bg_is_default) {
  2633. send_literal(rv, "4");
  2634. send_num(rv, nbuf, cbg - 1);
  2635. }
  2636. send_literal(rv, "m");
  2637. break;
  2638. case TB_OUTPUT_256:
  2639. case TB_OUTPUT_216:
  2640. case TB_OUTPUT_GRAYSCALE:
  2641. send_literal(rv, "\x1b[");
  2642. if (!fg_is_default) {
  2643. send_literal(rv, "38;5;");
  2644. send_num(rv, nbuf, cfg);
  2645. if (!bg_is_default) {
  2646. send_literal(rv, ";");
  2647. }
  2648. }
  2649. if (!bg_is_default) {
  2650. send_literal(rv, "48;5;");
  2651. send_num(rv, nbuf, cbg);
  2652. }
  2653. send_literal(rv, "m");
  2654. break;
  2655. #ifdef TB_OPT_TRUECOLOR
  2656. case TB_OUTPUT_TRUECOLOR:
  2657. send_literal(rv, "\x1b[");
  2658. if (!fg_is_default) {
  2659. send_literal(rv, "38;2;");
  2660. send_num(rv, nbuf, (cfg >> 16) & 0xff);
  2661. send_literal(rv, ";");
  2662. send_num(rv, nbuf, (cfg >> 8) & 0xff);
  2663. send_literal(rv, ";");
  2664. send_num(rv, nbuf, cfg & 0xff);
  2665. if (!bg_is_default) {
  2666. send_literal(rv, ";");
  2667. }
  2668. }
  2669. if (!bg_is_default) {
  2670. send_literal(rv, "48;2;");
  2671. send_num(rv, nbuf, (cbg >> 16) & 0xff);
  2672. send_literal(rv, ";");
  2673. send_num(rv, nbuf, (cbg >> 8) & 0xff);
  2674. send_literal(rv, ";");
  2675. send_num(rv, nbuf, cbg & 0xff);
  2676. }
  2677. send_literal(rv, "m");
  2678. break;
  2679. #endif
  2680. }
  2681. return TB_OK;
  2682. }
  2683. static int send_cursor_if(int x, int y) {
  2684. int rv;
  2685. char nbuf[32];
  2686. if (x < 0 || y < 0) {
  2687. return TB_OK;
  2688. }
  2689. send_literal(rv, "\x1b[");
  2690. send_num(rv, nbuf, y + 1);
  2691. send_literal(rv, ";");
  2692. send_num(rv, nbuf, x + 1);
  2693. send_literal(rv, "H");
  2694. return TB_OK;
  2695. }
  2696. static int send_char(int x, int y, uint32_t ch) {
  2697. return send_cluster(x, y, &ch, 1);
  2698. }
  2699. static int send_cluster(int x, int y, uint32_t *ch, size_t nch) {
  2700. int rv;
  2701. char abuf[8];
  2702. if (global.last_x != x - 1 || global.last_y != y) {
  2703. if_err_return(rv, send_cursor_if(x, y));
  2704. }
  2705. global.last_x = x;
  2706. global.last_y = y;
  2707. int i;
  2708. for (i = 0; i < (int)nch; i++) {
  2709. uint32_t ach = *(ch + i);
  2710. int aw = tb_utf8_unicode_to_char(abuf, ach);
  2711. if (!ach) {
  2712. abuf[0] = ' ';
  2713. }
  2714. if_err_return(rv, bytebuf_nputs(&global.out, abuf, (size_t)aw));
  2715. }
  2716. return TB_OK;
  2717. }
  2718. static int convert_num(uint32_t num, char *buf) {
  2719. int i, l = 0;
  2720. char ch;
  2721. do {
  2722. /* '0' = 48; 48 + num%10 < 58 < MAX_8bitCHAR */
  2723. buf[l++] = (char)('0' + (num % 10));
  2724. num /= 10;
  2725. } while (num);
  2726. for (i = 0; i < l / 2; i++) {
  2727. ch = buf[i];
  2728. buf[i] = buf[l - 1 - i];
  2729. buf[l - 1 - i] = ch;
  2730. }
  2731. return l;
  2732. }
  2733. static int cell_cmp(struct tb_cell *a, struct tb_cell *b) {
  2734. if (a->ch != b->ch || a->fg != b->fg || a->bg != b->bg) {
  2735. return 1;
  2736. }
  2737. #ifdef TB_OPT_EGC
  2738. if (a->nech != b->nech) {
  2739. return 1;
  2740. } else if (a->nech > 0) { // a->nech == b->nech
  2741. return memcmp(a->ech, b->ech, a->nech);
  2742. }
  2743. #endif
  2744. return 0;
  2745. }
  2746. static int cell_copy(struct tb_cell *dst, struct tb_cell *src) {
  2747. #ifdef TB_OPT_EGC
  2748. if (src->nech > 0) {
  2749. return cell_set(dst, src->ech, src->nech, src->fg, src->bg);
  2750. }
  2751. #endif
  2752. return cell_set(dst, &src->ch, 1, src->fg, src->bg);
  2753. }
  2754. static int cell_set(struct tb_cell *cell, uint32_t *ch, size_t nch,
  2755. uintattr_t fg, uintattr_t bg) {
  2756. cell->ch = ch ? *ch : 0;
  2757. cell->fg = fg;
  2758. cell->bg = bg;
  2759. #ifdef TB_OPT_EGC
  2760. if (nch <= 1) {
  2761. cell->nech = 0;
  2762. } else {
  2763. int rv;
  2764. if_err_return(rv, cell_reserve_ech(cell, nch + 1));
  2765. memcpy(cell->ech, ch, nch);
  2766. cell->ech[nch] = '\0';
  2767. cell->nech = nch;
  2768. }
  2769. #else
  2770. (void)nch;
  2771. (void)cell_reserve_ech;
  2772. #endif
  2773. return TB_OK;
  2774. }
  2775. static int cell_reserve_ech(struct tb_cell *cell, size_t n) {
  2776. #ifdef TB_OPT_EGC
  2777. if (cell->cech >= n) {
  2778. return TB_OK;
  2779. }
  2780. if (!(cell->ech = tb_realloc(cell->ech, n * sizeof(cell->ch)))) {
  2781. return TB_ERR_MEM;
  2782. }
  2783. cell->cech = n;
  2784. return TB_OK;
  2785. #else
  2786. (void)cell;
  2787. (void)n;
  2788. return TB_ERR;
  2789. #endif
  2790. }
  2791. static int cell_free(struct tb_cell *cell) {
  2792. #ifdef TB_OPT_EGC
  2793. if (cell->ech) {
  2794. tb_free(cell->ech);
  2795. }
  2796. #endif
  2797. memset(cell, 0, sizeof(*cell));
  2798. return TB_OK;
  2799. }
  2800. static int cellbuf_init(struct cellbuf_t *c, int w, int h) {
  2801. c->cells = tb_malloc(sizeof(struct tb_cell) * w * h);
  2802. if (!c->cells) {
  2803. return TB_ERR_MEM;
  2804. }
  2805. memset(c->cells, 0, sizeof(struct tb_cell) * w * h);
  2806. c->width = w;
  2807. c->height = h;
  2808. return TB_OK;
  2809. }
  2810. static int cellbuf_free(struct cellbuf_t *c) {
  2811. if (c->cells) {
  2812. int i;
  2813. for (i = 0; i < c->width * c->height; i++) {
  2814. cell_free(&c->cells[i]);
  2815. }
  2816. tb_free(c->cells);
  2817. }
  2818. memset(c, 0, sizeof(*c));
  2819. return TB_OK;
  2820. }
  2821. static int cellbuf_clear(struct cellbuf_t *c) {
  2822. int rv, i;
  2823. uint32_t space = (uint32_t)' ';
  2824. for (i = 0; i < c->width * c->height; i++) {
  2825. if_err_return(rv,
  2826. cell_set(&c->cells[i], &space, 1, global.fg, global.bg));
  2827. }
  2828. return TB_OK;
  2829. }
  2830. static int cellbuf_get(struct cellbuf_t *c, int x, int y,
  2831. struct tb_cell **out) {
  2832. if (x < 0 || x >= c->width || y < 0 || y >= c->height) {
  2833. *out = NULL;
  2834. return TB_ERR_OUT_OF_BOUNDS;
  2835. }
  2836. *out = &c->cells[(y * c->width) + x];
  2837. return TB_OK;
  2838. }
  2839. static int cellbuf_resize(struct cellbuf_t *c, int w, int h) {
  2840. int rv;
  2841. int ow = c->width;
  2842. int oh = c->height;
  2843. if (ow == w && oh == h) {
  2844. return TB_OK;
  2845. }
  2846. w = w < 1 ? 1 : w;
  2847. h = h < 1 ? 1 : h;
  2848. int minw = (w < ow) ? w : ow;
  2849. int minh = (h < oh) ? h : oh;
  2850. struct tb_cell *prev = c->cells;
  2851. if_err_return(rv, cellbuf_init(c, w, h));
  2852. if_err_return(rv, cellbuf_clear(c));
  2853. int x, y;
  2854. for (x = 0; x < minw; x++) {
  2855. for (y = 0; y < minh; y++) {
  2856. struct tb_cell *src, *dst;
  2857. src = &prev[(y * ow) + x];
  2858. if_err_return(rv, cellbuf_get(c, x, y, &dst));
  2859. if_err_return(rv, cell_copy(dst, src));
  2860. }
  2861. }
  2862. tb_free(prev);
  2863. return TB_OK;
  2864. }
  2865. static int bytebuf_puts(struct bytebuf_t *b, const char *str) {
  2866. return bytebuf_nputs(b, str, (size_t)strlen(str));
  2867. }
  2868. static int bytebuf_nputs(struct bytebuf_t *b, const char *str, size_t nstr) {
  2869. int rv;
  2870. if_err_return(rv, bytebuf_reserve(b, b->len + nstr + 1));
  2871. memcpy(b->buf + b->len, str, nstr);
  2872. b->len += nstr;
  2873. b->buf[b->len] = '\0';
  2874. return TB_OK;
  2875. }
  2876. static int bytebuf_shift(struct bytebuf_t *b, size_t n) {
  2877. if (n > b->len) {
  2878. n = b->len;
  2879. }
  2880. size_t nmove = b->len - n;
  2881. memmove(b->buf, b->buf + n, nmove);
  2882. b->len -= n;
  2883. return TB_OK;
  2884. }
  2885. static int bytebuf_flush(struct bytebuf_t *b, int fd) {
  2886. if (b->len <= 0) {
  2887. return TB_OK;
  2888. }
  2889. ssize_t write_rv = write(fd, b->buf, b->len);
  2890. if (write_rv < 0 || (size_t)write_rv != b->len) {
  2891. // Note, errno will be 0 on partial write
  2892. global.last_errno = errno;
  2893. return TB_ERR;
  2894. }
  2895. b->len = 0;
  2896. return TB_OK;
  2897. }
  2898. static int bytebuf_reserve(struct bytebuf_t *b, size_t sz) {
  2899. if (b->cap >= sz) {
  2900. return TB_OK;
  2901. }
  2902. size_t newcap = b->cap > 0 ? b->cap : 1;
  2903. while (newcap < sz) {
  2904. newcap *= 2;
  2905. }
  2906. char *newbuf;
  2907. if (b->buf) {
  2908. newbuf = tb_realloc(b->buf, newcap);
  2909. } else {
  2910. newbuf = tb_malloc(newcap);
  2911. }
  2912. if (!newbuf) {
  2913. return TB_ERR_MEM;
  2914. }
  2915. b->buf = newbuf;
  2916. b->cap = newcap;
  2917. return TB_OK;
  2918. }
  2919. static int bytebuf_free(struct bytebuf_t *b) {
  2920. if (b->buf) {
  2921. tb_free(b->buf);
  2922. }
  2923. memset(b, 0, sizeof(*b));
  2924. return TB_OK;
  2925. }
  2926. #endif /* TB_IMPL */