key.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244
  1. /*
  2. Keyboard support routines.
  3. Copyright (C) 1994-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Miguel de Icaza, 1994, 1995
  7. Janne Kukonlehto, 1994, 1995
  8. Jakub Jelinek, 1995
  9. Norbert Warmuth, 1997
  10. Denys Vlasenko <vda.linux@googlemail.com>, 2013
  11. Slava Zanko <slavazanko@gmail.com>, 2013
  12. Egmont Koblinger <egmont@gmail.com>, 2013
  13. This file is part of the Midnight Commander.
  14. The Midnight Commander is free software: you can redistribute it
  15. and/or modify it under the terms of the GNU General Public License as
  16. published by the Free Software Foundation, either version 3 of the License,
  17. or (at your option) any later version.
  18. The Midnight Commander is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. /** \file key.c
  26. * \brief Source: keyboard support routines
  27. */
  28. #include <config.h>
  29. #include <ctype.h>
  30. #include <errno.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #ifdef HAVE_SYS_SELECT_H
  35. #include <sys/select.h>
  36. #else
  37. #include <sys/time.h>
  38. #include <sys/types.h>
  39. #include <unistd.h>
  40. #endif
  41. #include "lib/global.h"
  42. #include "lib/vfs/vfs.h"
  43. #include "tty.h"
  44. #include "tty-internal.h" /* mouse_enabled */
  45. #include "mouse.h"
  46. #include "key.h"
  47. #include "lib/widget.h" /* mc_refresh() */
  48. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  49. #include "x11conn.h"
  50. #endif
  51. #ifdef __linux__
  52. #if defined(__GLIBC__) && (__GLIBC__ < 2)
  53. #include <linux/termios.h> /* TIOCLINUX */
  54. #else
  55. #include <termios.h>
  56. #endif
  57. #ifdef HAVE_SYS_IOCTL_H
  58. #include <sys/ioctl.h>
  59. #endif
  60. #endif /* __linux__ */
  61. #ifdef __CYGWIN__
  62. #include <termios.h>
  63. #ifdef HAVE_SYS_IOCTL_H
  64. #include <sys/ioctl.h>
  65. #endif
  66. #endif /* __CYGWIN__ */
  67. #ifdef __QNXNTO__
  68. #include <dlfcn.h>
  69. #include <Ph.h>
  70. #include <sys/dcmd_chr.h>
  71. #endif /* __QNXNTO__ */
  72. /*** global variables ****************************************************************************/
  73. int mou_auto_repeat = 100; /* ms */
  74. int double_click_speed = 250; /* ms */
  75. gboolean old_esc_mode = TRUE;
  76. /* timeout for old_esc_mode in usec */
  77. int old_esc_mode_timeout = G_USEC_PER_SEC; /* us, settable via env */
  78. gboolean use_8th_bit_as_meta = FALSE;
  79. gboolean bracketed_pasting_in_progress = FALSE;
  80. /* This table is a mapping between names and the constants we use
  81. * We use this to allow users to define alternate definitions for
  82. * certain keys that may be missing from the terminal database
  83. */
  84. const key_code_name_t key_name_conv_tab[] = {
  85. {ESC_CHAR, "escape", N_("Escape"), "Esc"},
  86. /* KEY_F(0) is not here, since we are mapping it to f10, so there is no reason
  87. to define f0 as well. Also, it makes Learn keys a bunch of problems :( */
  88. {KEY_F (1), "f1", N_("Function key 1"), "F1"},
  89. {KEY_F (2), "f2", N_("Function key 2"), "F2"},
  90. {KEY_F (3), "f3", N_("Function key 3"), "F3"},
  91. {KEY_F (4), "f4", N_("Function key 4"), "F4"},
  92. {KEY_F (5), "f5", N_("Function key 5"), "F5"},
  93. {KEY_F (6), "f6", N_("Function key 6"), "F6"},
  94. {KEY_F (7), "f7", N_("Function key 7"), "F7"},
  95. {KEY_F (8), "f8", N_("Function key 8"), "F8"},
  96. {KEY_F (9), "f9", N_("Function key 9"), "F9"},
  97. {KEY_F (10), "f10", N_("Function key 10"), "F10"},
  98. {KEY_F (11), "f11", N_("Function key 11"), "F11"},
  99. {KEY_F (12), "f12", N_("Function key 12"), "F12"},
  100. {KEY_F (13), "f13", N_("Function key 13"), "F13"},
  101. {KEY_F (14), "f14", N_("Function key 14"), "F14"},
  102. {KEY_F (15), "f15", N_("Function key 15"), "F15"},
  103. {KEY_F (16), "f16", N_("Function key 16"), "F16"},
  104. {KEY_F (17), "f17", N_("Function key 17"), "F17"},
  105. {KEY_F (18), "f18", N_("Function key 18"), "F18"},
  106. {KEY_F (19), "f19", N_("Function key 19"), "F19"},
  107. {KEY_F (20), "f20", N_("Function key 20"), "F20"},
  108. {ALT ('\t'), "complete", N_("Completion/M-tab"), "Meta-Tab"},
  109. {KEY_BTAB, "backtab", N_("BackTab/S-tab"), "Shift-Tab"},
  110. {KEY_BACKSPACE, "backspace", N_("Backspace"), "Backspace"},
  111. {KEY_UP, "up", N_("Up arrow"), "Up"},
  112. {KEY_DOWN, "down", N_("Down arrow"), "Down"},
  113. {KEY_LEFT, "left", N_("Left arrow"), "Left"},
  114. {KEY_RIGHT, "right", N_("Right arrow"), "Right"},
  115. {KEY_IC, "insert", N_("Insert"), "Ins"},
  116. {KEY_DC, "delete", N_("Delete"), "Del"},
  117. {KEY_HOME, "home", N_("Home"), "Home"},
  118. {KEY_END, "end", N_("End key"), "End"},
  119. {KEY_PPAGE, "pgup", N_("Page Up"), "PgUp"},
  120. {KEY_NPAGE, "pgdn", N_("Page Down"), "PgDn"},
  121. {(int) '/', "kpslash", N_("/ on keypad"), "/"},
  122. {KEY_KP_MULTIPLY, "kpasterisk", N_("* on keypad"), "*"},
  123. {KEY_KP_SUBTRACT, "kpminus", N_("- on keypad"), "-"},
  124. {KEY_KP_ADD, "kpplus", N_("+ on keypad"), "+"},
  125. /* From here on, these won't be shown in Learn keys (no space) */
  126. {KEY_LEFT, "kpleft", N_("Left arrow keypad"), "Left"},
  127. {KEY_RIGHT, "kpright", N_("Right arrow keypad"), "Right"},
  128. {KEY_UP, "kpup", N_("Up arrow keypad"), "Up"},
  129. {KEY_DOWN, "kpdown", N_("Down arrow keypad"), "Down"},
  130. {KEY_HOME, "kphome", N_("Home on keypad"), "Home"},
  131. {KEY_END, "kpend", N_("End on keypad"), "End"},
  132. {KEY_NPAGE, "kpnpage", N_("Page Down keypad"), "PgDn"},
  133. {KEY_PPAGE, "kpppage", N_("Page Up keypad"), "PgUp"},
  134. {KEY_IC, "kpinsert", N_("Insert on keypad"), "Ins"},
  135. {KEY_DC, "kpdelete", N_("Delete on keypad"), "Del"},
  136. {(int) '\n', "kpenter", N_("Enter on keypad"), "Enter"},
  137. {KEY_F (21), "f21", N_("Function key 21"), "F21"},
  138. {KEY_F (22), "f22", N_("Function key 22"), "F22"},
  139. {KEY_F (23), "f23", N_("Function key 23"), "F23"},
  140. {KEY_F (24), "f24", N_("Function key 24"), "F24"},
  141. {KEY_A1, "a1", N_("A1 key"), "A1"},
  142. {KEY_C1, "c1", N_("C1 key"), "C1"},
  143. /* Alternative label */
  144. {ESC_CHAR, "esc", N_("Escape"), "Esc"},
  145. {KEY_BACKSPACE, "bs", N_("Backspace"), "Bakspace"},
  146. {KEY_IC, "ins", N_("Insert"), "Ins"},
  147. {KEY_DC, "del", N_("Delete"), "Del"},
  148. {(int) '*', "asterisk", N_("Asterisk"), "*"},
  149. {(int) '-', "minus", N_("Minus"), "-"},
  150. {(int) '+', "plus", N_("Plus"), "+"},
  151. {(int) '.', "dot", N_("Dot"), "."},
  152. {(int) '<', "lt", N_("Less than"), "<"},
  153. {(int) '>', "gt", N_("Great than"), ">"},
  154. {(int) '=', "equal", N_("Equal"), "="},
  155. {(int) ',', "comma", N_("Comma"), ","},
  156. {(int) '\'', "apostrophe", N_("Apostrophe"), "\'"},
  157. {(int) ':', "colon", N_("Colon"), ":"},
  158. {(int) ';', "semicolon", N_("Semicolon"), ";"},
  159. {(int) '!', "exclamation", N_("Exclamation mark"), "!"},
  160. {(int) '?', "question", N_("Question mark"), "?"},
  161. {(int) '&', "ampersand", N_("Ampersand"), "&"},
  162. {(int) '$', "dollar", N_("Dollar sign"), "$"},
  163. {(int) '"', "quota", N_("Quotation mark"), "\""},
  164. {(int) '%', "percent", N_("Percent sign"), "%"},
  165. {(int) '^', "caret", N_("Caret"), "^"},
  166. {(int) '~', "tilda", N_("Tilda"), "~"},
  167. {(int) '`', "prime", N_("Prime"), "`"},
  168. {(int) '_', "underline", N_("Underline"), "_"},
  169. {(int) '_', "understrike", N_("Understrike"), "_"},
  170. {(int) '|', "pipe", N_("Pipe"), "|"},
  171. {(int) '(', "lparenthesis", N_("Left parenthesis"), "("},
  172. {(int) ')', "rparenthesis", N_("Right parenthesis"), ")"},
  173. {(int) '[', "lbracket", N_("Left bracket"), "["},
  174. {(int) ']', "rbracket", N_("Right bracket"), "]"},
  175. {(int) '{', "lbrace", N_("Left brace"), "{"},
  176. {(int) '}', "rbrace", N_("Right brace"), "}"},
  177. {(int) '\n', "enter", N_("Enter"), "Enter"},
  178. {(int) '\t', "tab", N_("Tab key"), "Tab"},
  179. {(int) ' ', "space", N_("Space key"), "Space"},
  180. {(int) '/', "slash", N_("Slash key"), "/"},
  181. {(int) '\\', "backslash", N_("Backslash key"), "\\"},
  182. {(int) '#', "number", N_("Number sign #"), "#"},
  183. {(int) '#', "hash", N_("Number sign #"), "#"},
  184. /* TRANSLATORS: Please translate as in "at sign" (@). */
  185. {(int) '@', "at", N_("At sign"), "@"},
  186. /* meta keys */
  187. {KEY_M_CTRL, "control", N_("Ctrl"), "C"},
  188. {KEY_M_CTRL, "ctrl", N_("Ctrl"), "C"},
  189. {KEY_M_ALT, "meta", N_("Alt"), "M"},
  190. {KEY_M_ALT, "alt", N_("Alt"), "M"},
  191. {KEY_M_ALT, "ralt", N_("Alt"), "M"},
  192. {KEY_M_SHIFT, "shift", N_("Shift"), "S"},
  193. {0, NULL, NULL, NULL}
  194. };
  195. /*** file scope macro definitions ****************************************************************/
  196. #define MC_USEC_PER_MSEC 1000
  197. /* The maximum sequence length (32 + null terminator) */
  198. #define SEQ_BUFFER_LEN 33
  199. /*** file scope type declarations ****************************************************************/
  200. /* Linux console keyboard modifiers */
  201. typedef enum
  202. {
  203. SHIFT_PRESSED = (1 << 0),
  204. ALTR_PRESSED = (1 << 1),
  205. CONTROL_PRESSED = (1 << 2),
  206. ALTL_PRESSED = (1 << 3)
  207. } mod_pressed_t;
  208. typedef struct key_def
  209. {
  210. char ch; /* Holds the matching char code */
  211. int code; /* The code returned, valid if child == NULL */
  212. struct key_def *next;
  213. struct key_def *child; /* sequence continuation */
  214. int action; /* optional action to be done. Now used only
  215. to mark that we are just after the first
  216. Escape */
  217. } key_def;
  218. typedef struct
  219. {
  220. int code;
  221. const char *seq;
  222. int action;
  223. } key_define_t;
  224. /* File descriptor monitoring add/remove routines */
  225. typedef struct
  226. {
  227. int fd;
  228. select_fn callback;
  229. void *info;
  230. } select_t;
  231. typedef enum KeySortType
  232. {
  233. KEY_NOSORT = 0,
  234. KEY_SORTBYNAME,
  235. KEY_SORTBYCODE
  236. } KeySortType;
  237. #ifdef __QNXNTO__
  238. typedef int (*ph_dv_f) (void *, void *);
  239. typedef int (*ph_ov_f) (void *);
  240. typedef int (*ph_pqc_f) (unsigned short, PhCursorInfo_t *);
  241. #endif
  242. /*** forward declarations (file scope functions) *************************************************/
  243. /*** file scope variables ************************************************************************/
  244. static key_define_t mc_default_keys[] = {
  245. {ESC_CHAR, ESC_STR, MCKEY_ESCAPE},
  246. {ESC_CHAR, ESC_STR ESC_STR, MCKEY_NOACTION},
  247. {MCKEY_BRACKETED_PASTING_START, ESC_STR "[200~", MCKEY_NOACTION},
  248. {MCKEY_BRACKETED_PASTING_END, ESC_STR "[201~", MCKEY_NOACTION},
  249. {0, NULL, MCKEY_NOACTION},
  250. };
  251. /* Broken terminfo and termcap databases on xterminals */
  252. static key_define_t xterm_key_defines[] = {
  253. {KEY_F (1), ESC_STR "OP", MCKEY_NOACTION},
  254. {KEY_F (2), ESC_STR "OQ", MCKEY_NOACTION},
  255. {KEY_F (3), ESC_STR "OR", MCKEY_NOACTION},
  256. {KEY_F (4), ESC_STR "OS", MCKEY_NOACTION},
  257. {KEY_F (1), ESC_STR "[11~", MCKEY_NOACTION},
  258. {KEY_F (2), ESC_STR "[12~", MCKEY_NOACTION},
  259. {KEY_F (3), ESC_STR "[13~", MCKEY_NOACTION},
  260. {KEY_F (4), ESC_STR "[14~", MCKEY_NOACTION},
  261. {KEY_F (5), ESC_STR "[15~", MCKEY_NOACTION},
  262. {KEY_F (6), ESC_STR "[17~", MCKEY_NOACTION},
  263. {KEY_F (7), ESC_STR "[18~", MCKEY_NOACTION},
  264. {KEY_F (8), ESC_STR "[19~", MCKEY_NOACTION},
  265. {KEY_F (9), ESC_STR "[20~", MCKEY_NOACTION},
  266. {KEY_F (10), ESC_STR "[21~", MCKEY_NOACTION},
  267. /* old xterm Shift-arrows */
  268. {KEY_M_SHIFT | KEY_UP, ESC_STR "O2A", MCKEY_NOACTION},
  269. {KEY_M_SHIFT | KEY_DOWN, ESC_STR "O2B", MCKEY_NOACTION},
  270. {KEY_M_SHIFT | KEY_RIGHT, ESC_STR "O2C", MCKEY_NOACTION},
  271. {KEY_M_SHIFT | KEY_LEFT, ESC_STR "O2D", MCKEY_NOACTION},
  272. /* new xterm Shift-arrows */
  273. {KEY_M_SHIFT | KEY_UP, ESC_STR "[1;2A", MCKEY_NOACTION},
  274. {KEY_M_SHIFT | KEY_DOWN, ESC_STR "[1;2B", MCKEY_NOACTION},
  275. {KEY_M_SHIFT | KEY_RIGHT, ESC_STR "[1;2C", MCKEY_NOACTION},
  276. {KEY_M_SHIFT | KEY_LEFT, ESC_STR "[1;2D", MCKEY_NOACTION},
  277. /* more xterm keys with modifiers */
  278. {KEY_M_CTRL | KEY_PPAGE, ESC_STR "[5;5~", MCKEY_NOACTION},
  279. {KEY_M_CTRL | KEY_NPAGE, ESC_STR "[6;5~", MCKEY_NOACTION},
  280. {KEY_M_CTRL | KEY_IC, ESC_STR "[2;5~", MCKEY_NOACTION},
  281. {KEY_M_CTRL | KEY_DC, ESC_STR "[3;5~", MCKEY_NOACTION},
  282. {KEY_M_CTRL | KEY_HOME, ESC_STR "[1;5H", MCKEY_NOACTION},
  283. {KEY_M_CTRL | KEY_END, ESC_STR "[1;5F", MCKEY_NOACTION},
  284. {KEY_M_SHIFT | KEY_HOME, ESC_STR "[1;2H", MCKEY_NOACTION},
  285. {KEY_M_SHIFT | KEY_END, ESC_STR "[1;2F", MCKEY_NOACTION},
  286. {KEY_M_CTRL | KEY_UP, ESC_STR "[1;5A", MCKEY_NOACTION},
  287. {KEY_M_CTRL | KEY_DOWN, ESC_STR "[1;5B", MCKEY_NOACTION},
  288. {KEY_M_CTRL | KEY_RIGHT, ESC_STR "[1;5C", MCKEY_NOACTION},
  289. {KEY_M_CTRL | KEY_LEFT, ESC_STR "[1;5D", MCKEY_NOACTION},
  290. {KEY_M_SHIFT | KEY_IC, ESC_STR "[2;2~", MCKEY_NOACTION},
  291. {KEY_M_SHIFT | KEY_DC, ESC_STR "[3;2~", MCKEY_NOACTION},
  292. {KEY_M_SHIFT | KEY_M_CTRL | KEY_UP, ESC_STR "[1;6A", MCKEY_NOACTION},
  293. {KEY_M_SHIFT | KEY_M_CTRL | KEY_DOWN, ESC_STR "[1;6B", MCKEY_NOACTION},
  294. {KEY_M_SHIFT | KEY_M_CTRL | KEY_RIGHT, ESC_STR "[1;6C", MCKEY_NOACTION},
  295. {KEY_M_SHIFT | KEY_M_CTRL | KEY_LEFT, ESC_STR "[1;6D", MCKEY_NOACTION},
  296. {KEY_M_SHIFT | '\t', ESC_STR "[Z", MCKEY_NOACTION},
  297. /* putty */
  298. {KEY_M_SHIFT | KEY_M_CTRL | KEY_UP, ESC_STR "[[1;6A", MCKEY_NOACTION},
  299. {KEY_M_SHIFT | KEY_M_CTRL | KEY_DOWN, ESC_STR "[[1;6B", MCKEY_NOACTION},
  300. {KEY_M_SHIFT | KEY_M_CTRL | KEY_RIGHT, ESC_STR "[[1;6C", MCKEY_NOACTION},
  301. {KEY_M_SHIFT | KEY_M_CTRL | KEY_LEFT, ESC_STR "[[1;6D", MCKEY_NOACTION},
  302. /* putty alt-arrow keys */
  303. /* removed as source esc esc esc trouble */
  304. /*
  305. { KEY_M_ALT | KEY_UP, ESC_STR ESC_STR "OA", MCKEY_NOACTION },
  306. { KEY_M_ALT | KEY_DOWN, ESC_STR ESC_STR "OB", MCKEY_NOACTION },
  307. { KEY_M_ALT | KEY_RIGHT, ESC_STR ESC_STR "OC", MCKEY_NOACTION },
  308. { KEY_M_ALT | KEY_LEFT, ESC_STR ESC_STR "OD", MCKEY_NOACTION },
  309. { KEY_M_ALT | KEY_PPAGE, ESC_STR ESC_STR "[5~", MCKEY_NOACTION },
  310. { KEY_M_ALT | KEY_NPAGE, ESC_STR ESC_STR "[6~", MCKEY_NOACTION },
  311. { KEY_M_ALT | KEY_HOME, ESC_STR ESC_STR "[1~", MCKEY_NOACTION },
  312. { KEY_M_ALT | KEY_END, ESC_STR ESC_STR "[4~", MCKEY_NOACTION },
  313. { KEY_M_CTRL | KEY_M_ALT | KEY_UP, ESC_STR ESC_STR "[1;2A", MCKEY_NOACTION },
  314. { KEY_M_CTRL | KEY_M_ALT | KEY_DOWN, ESC_STR ESC_STR "[1;2B", MCKEY_NOACTION },
  315. { KEY_M_CTRL | KEY_M_ALT | KEY_RIGHT, ESC_STR ESC_STR "[1;2C", MCKEY_NOACTION },
  316. { KEY_M_CTRL | KEY_M_ALT | KEY_LEFT, ESC_STR ESC_STR "[1;2D", MCKEY_NOACTION },
  317. { KEY_M_CTRL | KEY_M_ALT | KEY_PPAGE, ESC_STR ESC_STR "[[5;5~", MCKEY_NOACTION },
  318. { KEY_M_CTRL | KEY_M_ALT | KEY_NPAGE, ESC_STR ESC_STR "[[6;5~", MCKEY_NOACTION },
  319. { KEY_M_CTRL | KEY_M_ALT | KEY_HOME, ESC_STR ESC_STR "[1;5H", MCKEY_NOACTION },
  320. { KEY_M_CTRL | KEY_M_ALT | KEY_END, ESC_STR ESC_STR "[1;5F", MCKEY_NOACTION },
  321. */
  322. /* xterm alt-arrow keys */
  323. {KEY_M_ALT | KEY_UP, ESC_STR "[1;3A", MCKEY_NOACTION},
  324. {KEY_M_ALT | KEY_DOWN, ESC_STR "[1;3B", MCKEY_NOACTION},
  325. {KEY_M_ALT | KEY_RIGHT, ESC_STR "[1;3C", MCKEY_NOACTION},
  326. {KEY_M_ALT | KEY_LEFT, ESC_STR "[1;3D", MCKEY_NOACTION},
  327. {KEY_M_ALT | KEY_PPAGE, ESC_STR "[5;3~", MCKEY_NOACTION},
  328. {KEY_M_ALT | KEY_NPAGE, ESC_STR "[6;3~", MCKEY_NOACTION},
  329. {KEY_M_ALT | KEY_HOME, ESC_STR "[1~", MCKEY_NOACTION},
  330. {KEY_M_ALT | KEY_END, ESC_STR "[4~", MCKEY_NOACTION},
  331. {KEY_M_CTRL | KEY_M_ALT | KEY_UP, ESC_STR "[1;7A", MCKEY_NOACTION},
  332. {KEY_M_CTRL | KEY_M_ALT | KEY_DOWN, ESC_STR "[1;7B", MCKEY_NOACTION},
  333. {KEY_M_CTRL | KEY_M_ALT | KEY_RIGHT, ESC_STR "[1;7C", MCKEY_NOACTION},
  334. {KEY_M_CTRL | KEY_M_ALT | KEY_LEFT, ESC_STR "[1;7D", MCKEY_NOACTION},
  335. {KEY_M_CTRL | KEY_M_ALT | KEY_PPAGE, ESC_STR "[5;7~", MCKEY_NOACTION},
  336. {KEY_M_CTRL | KEY_M_ALT | KEY_NPAGE, ESC_STR "[6;7~", MCKEY_NOACTION},
  337. {KEY_M_CTRL | KEY_M_ALT | KEY_HOME, ESC_STR "OH", MCKEY_NOACTION},
  338. {KEY_M_CTRL | KEY_M_ALT | KEY_END, ESC_STR "OF", MCKEY_NOACTION},
  339. {KEY_M_SHIFT | KEY_M_ALT | KEY_UP, ESC_STR "[1;4A", MCKEY_NOACTION},
  340. {KEY_M_SHIFT | KEY_M_ALT | KEY_DOWN, ESC_STR "[1;4B", MCKEY_NOACTION},
  341. {KEY_M_SHIFT | KEY_M_ALT | KEY_RIGHT, ESC_STR "[1;4C", MCKEY_NOACTION},
  342. {KEY_M_SHIFT | KEY_M_ALT | KEY_LEFT, ESC_STR "[1;4D", MCKEY_NOACTION},
  343. /* rxvt keys with modifiers */
  344. {KEY_M_SHIFT | KEY_UP, ESC_STR "[a", MCKEY_NOACTION},
  345. {KEY_M_SHIFT | KEY_DOWN, ESC_STR "[b", MCKEY_NOACTION},
  346. {KEY_M_SHIFT | KEY_RIGHT, ESC_STR "[c", MCKEY_NOACTION},
  347. {KEY_M_SHIFT | KEY_LEFT, ESC_STR "[d", MCKEY_NOACTION},
  348. {KEY_M_CTRL | KEY_UP, ESC_STR "Oa", MCKEY_NOACTION},
  349. {KEY_M_CTRL | KEY_DOWN, ESC_STR "Ob", MCKEY_NOACTION},
  350. {KEY_M_CTRL | KEY_RIGHT, ESC_STR "Oc", MCKEY_NOACTION},
  351. {KEY_M_CTRL | KEY_LEFT, ESC_STR "Od", MCKEY_NOACTION},
  352. {KEY_M_CTRL | KEY_PPAGE, ESC_STR "[5^", MCKEY_NOACTION},
  353. {KEY_M_CTRL | KEY_NPAGE, ESC_STR "[6^", MCKEY_NOACTION},
  354. {KEY_M_CTRL | KEY_HOME, ESC_STR "[7^", MCKEY_NOACTION},
  355. {KEY_M_CTRL | KEY_END, ESC_STR "[8^", MCKEY_NOACTION},
  356. {KEY_M_SHIFT | KEY_HOME, ESC_STR "[7$", MCKEY_NOACTION},
  357. {KEY_M_SHIFT | KEY_END, ESC_STR "[8$", MCKEY_NOACTION},
  358. {KEY_M_CTRL | KEY_IC, ESC_STR "[2^", MCKEY_NOACTION},
  359. {KEY_M_CTRL | KEY_DC, ESC_STR "[3^", MCKEY_NOACTION},
  360. {KEY_M_SHIFT | KEY_DC, ESC_STR "[3$", MCKEY_NOACTION},
  361. /* konsole keys with modifiers */
  362. {KEY_M_SHIFT | KEY_HOME, ESC_STR "O2H", MCKEY_NOACTION},
  363. {KEY_M_SHIFT | KEY_END, ESC_STR "O2F", MCKEY_NOACTION},
  364. /* gnome-terminal */
  365. {KEY_M_SHIFT | KEY_UP, ESC_STR "[2A", MCKEY_NOACTION},
  366. {KEY_M_SHIFT | KEY_DOWN, ESC_STR "[2B", MCKEY_NOACTION},
  367. {KEY_M_SHIFT | KEY_RIGHT, ESC_STR "[2C", MCKEY_NOACTION},
  368. {KEY_M_SHIFT | KEY_LEFT, ESC_STR "[2D", MCKEY_NOACTION},
  369. {KEY_M_CTRL | KEY_UP, ESC_STR "[5A", MCKEY_NOACTION},
  370. {KEY_M_CTRL | KEY_DOWN, ESC_STR "[5B", MCKEY_NOACTION},
  371. {KEY_M_CTRL | KEY_RIGHT, ESC_STR "[5C", MCKEY_NOACTION},
  372. {KEY_M_CTRL | KEY_LEFT, ESC_STR "[5D", MCKEY_NOACTION},
  373. {KEY_M_SHIFT | KEY_M_CTRL | KEY_UP, ESC_STR "[6A", MCKEY_NOACTION},
  374. {KEY_M_SHIFT | KEY_M_CTRL | KEY_DOWN, ESC_STR "[6B", MCKEY_NOACTION},
  375. {KEY_M_SHIFT | KEY_M_CTRL | KEY_RIGHT, ESC_STR "[6C", MCKEY_NOACTION},
  376. {KEY_M_SHIFT | KEY_M_CTRL | KEY_LEFT, ESC_STR "[6D", MCKEY_NOACTION},
  377. /* gnome-terminal - application mode */
  378. {KEY_M_CTRL | KEY_UP, ESC_STR "O5A", MCKEY_NOACTION},
  379. {KEY_M_CTRL | KEY_DOWN, ESC_STR "O5B", MCKEY_NOACTION},
  380. {KEY_M_CTRL | KEY_RIGHT, ESC_STR "O5C", MCKEY_NOACTION},
  381. {KEY_M_CTRL | KEY_LEFT, ESC_STR "O5D", MCKEY_NOACTION},
  382. {KEY_M_SHIFT | KEY_M_CTRL | KEY_UP, ESC_STR "O6A", MCKEY_NOACTION},
  383. {KEY_M_SHIFT | KEY_M_CTRL | KEY_DOWN, ESC_STR "O6B", MCKEY_NOACTION},
  384. {KEY_M_SHIFT | KEY_M_CTRL | KEY_RIGHT, ESC_STR "O6C", MCKEY_NOACTION},
  385. {KEY_M_SHIFT | KEY_M_CTRL | KEY_LEFT, ESC_STR "O6D", MCKEY_NOACTION},
  386. /* iTerm */
  387. {KEY_M_SHIFT | KEY_PPAGE, ESC_STR "[5;2~", MCKEY_NOACTION},
  388. {KEY_M_SHIFT | KEY_NPAGE, ESC_STR "[6;2~", MCKEY_NOACTION},
  389. /* putty */
  390. {KEY_M_SHIFT | KEY_PPAGE, ESC_STR "[[5;53~", MCKEY_NOACTION},
  391. {KEY_M_SHIFT | KEY_NPAGE, ESC_STR "[[6;53~", MCKEY_NOACTION},
  392. /* keypad keys */
  393. {KEY_IC, ESC_STR "Op", MCKEY_NOACTION},
  394. {KEY_DC, ESC_STR "On", MCKEY_NOACTION},
  395. {'/', ESC_STR "Oo", MCKEY_NOACTION},
  396. {'\n', ESC_STR "OM", MCKEY_NOACTION},
  397. {0, NULL, MCKEY_NOACTION},
  398. };
  399. /* qansi-m terminals have a much more key combinations,
  400. which are undefined in termcap/terminfo */
  401. static key_define_t qansi_key_defines[] = {
  402. /* qansi-m terminal */
  403. {KEY_M_CTRL | KEY_NPAGE, ESC_STR "[u", MCKEY_NOACTION}, /* Ctrl-PgDown */
  404. {KEY_M_CTRL | KEY_PPAGE, ESC_STR "[v", MCKEY_NOACTION}, /* Ctrl-PgUp */
  405. {KEY_M_CTRL | KEY_HOME, ESC_STR "[h", MCKEY_NOACTION}, /* Ctrl-Home */
  406. {KEY_M_CTRL | KEY_END, ESC_STR "[y", MCKEY_NOACTION}, /* Ctrl-End */
  407. {KEY_M_CTRL | KEY_IC, ESC_STR "[`", MCKEY_NOACTION}, /* Ctrl-Insert */
  408. {KEY_M_CTRL | KEY_DC, ESC_STR "[p", MCKEY_NOACTION}, /* Ctrl-Delete */
  409. {KEY_M_CTRL | KEY_LEFT, ESC_STR "[d", MCKEY_NOACTION}, /* Ctrl-Left */
  410. {KEY_M_CTRL | KEY_RIGHT, ESC_STR "[c", MCKEY_NOACTION}, /* Ctrl-Right */
  411. {KEY_M_CTRL | KEY_DOWN, ESC_STR "[b", MCKEY_NOACTION}, /* Ctrl-Down */
  412. {KEY_M_CTRL | KEY_UP, ESC_STR "[a", MCKEY_NOACTION}, /* Ctrl-Up */
  413. {KEY_M_CTRL | KEY_KP_ADD, ESC_STR "[s", MCKEY_NOACTION}, /* Ctrl-Gr-Plus */
  414. {KEY_M_CTRL | KEY_KP_SUBTRACT, ESC_STR "[t", MCKEY_NOACTION}, /* Ctrl-Gr-Minus */
  415. {KEY_M_CTRL | '\t', ESC_STR "[z", MCKEY_NOACTION}, /* Ctrl-Tab */
  416. {KEY_M_SHIFT | '\t', ESC_STR "[Z", MCKEY_NOACTION}, /* Shift-Tab */
  417. {KEY_M_CTRL | KEY_F (1), ESC_STR "[1~", MCKEY_NOACTION}, /* Ctrl-F1 */
  418. {KEY_M_CTRL | KEY_F (2), ESC_STR "[2~", MCKEY_NOACTION}, /* Ctrl-F2 */
  419. {KEY_M_CTRL | KEY_F (3), ESC_STR "[3~", MCKEY_NOACTION}, /* Ctrl-F3 */
  420. {KEY_M_CTRL | KEY_F (4), ESC_STR "[4~", MCKEY_NOACTION}, /* Ctrl-F4 */
  421. {KEY_M_CTRL | KEY_F (5), ESC_STR "[5~", MCKEY_NOACTION}, /* Ctrl-F5 */
  422. {KEY_M_CTRL | KEY_F (6), ESC_STR "[6~", MCKEY_NOACTION}, /* Ctrl-F6 */
  423. {KEY_M_CTRL | KEY_F (7), ESC_STR "[7~", MCKEY_NOACTION}, /* Ctrl-F7 */
  424. {KEY_M_CTRL | KEY_F (8), ESC_STR "[8~", MCKEY_NOACTION}, /* Ctrl-F8 */
  425. {KEY_M_CTRL | KEY_F (9), ESC_STR "[9~", MCKEY_NOACTION}, /* Ctrl-F9 */
  426. {KEY_M_CTRL | KEY_F (10), ESC_STR "[10~", MCKEY_NOACTION}, /* Ctrl-F10 */
  427. {KEY_M_CTRL | KEY_F (11), ESC_STR "[11~", MCKEY_NOACTION}, /* Ctrl-F11 */
  428. {KEY_M_CTRL | KEY_F (12), ESC_STR "[12~", MCKEY_NOACTION}, /* Ctrl-F12 */
  429. {KEY_M_ALT | KEY_F (1), ESC_STR "[17~", MCKEY_NOACTION}, /* Alt-F1 */
  430. {KEY_M_ALT | KEY_F (2), ESC_STR "[18~", MCKEY_NOACTION}, /* Alt-F2 */
  431. {KEY_M_ALT | KEY_F (3), ESC_STR "[19~", MCKEY_NOACTION}, /* Alt-F3 */
  432. {KEY_M_ALT | KEY_F (4), ESC_STR "[20~", MCKEY_NOACTION}, /* Alt-F4 */
  433. {KEY_M_ALT | KEY_F (5), ESC_STR "[21~", MCKEY_NOACTION}, /* Alt-F5 */
  434. {KEY_M_ALT | KEY_F (6), ESC_STR "[22~", MCKEY_NOACTION}, /* Alt-F6 */
  435. {KEY_M_ALT | KEY_F (7), ESC_STR "[23~", MCKEY_NOACTION}, /* Alt-F7 */
  436. {KEY_M_ALT | KEY_F (8), ESC_STR "[24~", MCKEY_NOACTION}, /* Alt-F8 */
  437. {KEY_M_ALT | KEY_F (9), ESC_STR "[25~", MCKEY_NOACTION}, /* Alt-F9 */
  438. {KEY_M_ALT | KEY_F (10), ESC_STR "[26~", MCKEY_NOACTION}, /* Alt-F10 */
  439. {KEY_M_ALT | KEY_F (11), ESC_STR "[27~", MCKEY_NOACTION}, /* Alt-F11 */
  440. {KEY_M_ALT | KEY_F (12), ESC_STR "[28~", MCKEY_NOACTION}, /* Alt-F12 */
  441. {KEY_M_ALT | 'a', ESC_STR "Na", MCKEY_NOACTION}, /* Alt-a */
  442. {KEY_M_ALT | 'b', ESC_STR "Nb", MCKEY_NOACTION}, /* Alt-b */
  443. {KEY_M_ALT | 'c', ESC_STR "Nc", MCKEY_NOACTION}, /* Alt-c */
  444. {KEY_M_ALT | 'd', ESC_STR "Nd", MCKEY_NOACTION}, /* Alt-d */
  445. {KEY_M_ALT | 'e', ESC_STR "Ne", MCKEY_NOACTION}, /* Alt-e */
  446. {KEY_M_ALT | 'f', ESC_STR "Nf", MCKEY_NOACTION}, /* Alt-f */
  447. {KEY_M_ALT | 'g', ESC_STR "Ng", MCKEY_NOACTION}, /* Alt-g */
  448. {KEY_M_ALT | 'h', ESC_STR "Nh", MCKEY_NOACTION}, /* Alt-h */
  449. {KEY_M_ALT | 'i', ESC_STR "Ni", MCKEY_NOACTION}, /* Alt-i */
  450. {KEY_M_ALT | 'j', ESC_STR "Nj", MCKEY_NOACTION}, /* Alt-j */
  451. {KEY_M_ALT | 'k', ESC_STR "Nk", MCKEY_NOACTION}, /* Alt-k */
  452. {KEY_M_ALT | 'l', ESC_STR "Nl", MCKEY_NOACTION}, /* Alt-l */
  453. {KEY_M_ALT | 'm', ESC_STR "Nm", MCKEY_NOACTION}, /* Alt-m */
  454. {KEY_M_ALT | 'n', ESC_STR "Nn", MCKEY_NOACTION}, /* Alt-n */
  455. {KEY_M_ALT | 'o', ESC_STR "No", MCKEY_NOACTION}, /* Alt-o */
  456. {KEY_M_ALT | 'p', ESC_STR "Np", MCKEY_NOACTION}, /* Alt-p */
  457. {KEY_M_ALT | 'q', ESC_STR "Nq", MCKEY_NOACTION}, /* Alt-q */
  458. {KEY_M_ALT | 'r', ESC_STR "Nr", MCKEY_NOACTION}, /* Alt-r */
  459. {KEY_M_ALT | 's', ESC_STR "Ns", MCKEY_NOACTION}, /* Alt-s */
  460. {KEY_M_ALT | 't', ESC_STR "Nt", MCKEY_NOACTION}, /* Alt-t */
  461. {KEY_M_ALT | 'u', ESC_STR "Nu", MCKEY_NOACTION}, /* Alt-u */
  462. {KEY_M_ALT | 'v', ESC_STR "Nv", MCKEY_NOACTION}, /* Alt-v */
  463. {KEY_M_ALT | 'w', ESC_STR "Nw", MCKEY_NOACTION}, /* Alt-w */
  464. {KEY_M_ALT | 'x', ESC_STR "Nx", MCKEY_NOACTION}, /* Alt-x */
  465. {KEY_M_ALT | 'y', ESC_STR "Ny", MCKEY_NOACTION}, /* Alt-y */
  466. {KEY_M_ALT | 'z', ESC_STR "Nz", MCKEY_NOACTION}, /* Alt-z */
  467. {KEY_KP_SUBTRACT, ESC_STR "[S", MCKEY_NOACTION}, /* Gr-Minus */
  468. {KEY_KP_ADD, ESC_STR "[T", MCKEY_NOACTION}, /* Gr-Plus */
  469. {0, NULL, MCKEY_NOACTION},
  470. };
  471. /* This holds all the key definitions */
  472. static key_def *keys = NULL;
  473. static int input_fd;
  474. static int disabled_channels = 0; /* Disable channels checking */
  475. static GSList *select_list = NULL;
  476. static int seq_buffer[SEQ_BUFFER_LEN];
  477. static int *seq_append = NULL;
  478. static int *pending_keys = NULL;
  479. #ifdef __QNXNTO__
  480. ph_dv_f ph_attach;
  481. ph_ov_f ph_input_group;
  482. ph_pqc_f ph_query_cursor;
  483. #endif
  484. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  485. static Display *x11_display;
  486. static Window x11_window;
  487. #endif /* HAVE_TEXTMODE_X11_SUPPORT */
  488. static KeySortType has_been_sorted = KEY_NOSORT;
  489. /* *INDENT-OFF* */
  490. static const size_t key_conv_tab_size = G_N_ELEMENTS (key_name_conv_tab) - 1;
  491. /* *INDENT-ON* */
  492. static const key_code_name_t *key_conv_tab_sorted[G_N_ELEMENTS (key_name_conv_tab) - 1];
  493. /* --------------------------------------------------------------------------------------------- */
  494. /*** file scope functions ************************************************************************/
  495. /* --------------------------------------------------------------------------------------------- */
  496. static int
  497. select_cmp_by_fd_set (gconstpointer a, gconstpointer b)
  498. {
  499. const select_t *s = (const select_t *) a;
  500. const fd_set *f = (const fd_set *) b;
  501. return (FD_ISSET (s->fd, f) ? 0 : 1);
  502. }
  503. /* --------------------------------------------------------------------------------------------- */
  504. static int
  505. select_cmp_by_fd (gconstpointer a, gconstpointer b)
  506. {
  507. const select_t *s = (const select_t *) a;
  508. const int fd = GPOINTER_TO_INT (b);
  509. return (s->fd == fd ? 0 : 1);
  510. }
  511. /* --------------------------------------------------------------------------------------------- */
  512. static int
  513. add_selects (fd_set *select_set)
  514. {
  515. int top_fd = 0;
  516. if (disabled_channels == 0)
  517. {
  518. GSList *s;
  519. for (s = select_list; s != NULL; s = g_slist_next (s))
  520. {
  521. select_t *p = (select_t *) s->data;
  522. FD_SET (p->fd, select_set);
  523. if (p->fd > top_fd)
  524. top_fd = p->fd;
  525. }
  526. }
  527. return top_fd;
  528. }
  529. /* --------------------------------------------------------------------------------------------- */
  530. static void
  531. check_selects (fd_set *select_set)
  532. {
  533. while (disabled_channels == 0)
  534. {
  535. GSList *s;
  536. select_t *p;
  537. s = g_slist_find_custom (select_list, select_set, select_cmp_by_fd_set);
  538. if (s == NULL)
  539. break;
  540. p = (select_t *) s->data;
  541. FD_CLR (p->fd, select_set);
  542. p->callback (p->fd, p->info);
  543. }
  544. }
  545. /* --------------------------------------------------------------------------------------------- */
  546. /* If set timeout is set, then we wait 0.1 seconds, else, we block */
  547. static void
  548. try_channels (gboolean set_timeout)
  549. {
  550. struct timeval time_out;
  551. static fd_set select_set;
  552. while (TRUE)
  553. {
  554. struct timeval *timeptr = NULL;
  555. int maxfdp, v;
  556. FD_ZERO (&select_set);
  557. FD_SET (input_fd, &select_set); /* Add stdin */
  558. maxfdp = MAX (add_selects (&select_set), input_fd);
  559. if (set_timeout)
  560. {
  561. time_out.tv_sec = 0;
  562. time_out.tv_usec = 100 * MC_USEC_PER_MSEC;
  563. timeptr = &time_out;
  564. }
  565. v = select (maxfdp + 1, &select_set, NULL, NULL, timeptr);
  566. if (v > 0)
  567. {
  568. check_selects (&select_set);
  569. if (FD_ISSET (input_fd, &select_set))
  570. break;
  571. }
  572. }
  573. }
  574. /* --------------------------------------------------------------------------------------------- */
  575. static key_def *
  576. create_sequence (const char *seq, int code, int action)
  577. {
  578. key_def *base, *p, *attach;
  579. for (base = attach = NULL; *seq != '\0'; seq++)
  580. {
  581. p = g_new (key_def, 1);
  582. if (base == NULL)
  583. base = p;
  584. if (attach != NULL)
  585. attach->child = p;
  586. p->ch = *seq;
  587. p->code = code;
  588. p->child = p->next = NULL;
  589. if (seq[1] == '\0')
  590. p->action = action;
  591. else
  592. p->action = MCKEY_NOACTION;
  593. attach = p;
  594. }
  595. return base;
  596. }
  597. /* --------------------------------------------------------------------------------------------- */
  598. static void
  599. define_sequences (const key_define_t *kd)
  600. {
  601. int i;
  602. for (i = 0; kd[i].code != 0; i++)
  603. define_sequence (kd[i].code, kd[i].seq, kd[i].action);
  604. }
  605. /* --------------------------------------------------------------------------------------------- */
  606. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  607. static void
  608. init_key_x11 (void)
  609. {
  610. if (getenv ("DISPLAY") != NULL && !mc_global.tty.disable_x11)
  611. {
  612. x11_display = mc_XOpenDisplay (0);
  613. if (x11_display != NULL)
  614. x11_window = DefaultRootWindow (x11_display);
  615. }
  616. }
  617. #endif /* HAVE_TEXTMODE_X11_SUPPORT */
  618. /* --------------------------------------------------------------------------------------------- */
  619. /* Workaround for System V Curses vt100 bug */
  620. static int
  621. getch_with_delay (void)
  622. {
  623. int c;
  624. /* This routine could be used on systems without mouse support,
  625. so we need to do the select check :-( */
  626. while (TRUE)
  627. {
  628. if (pending_keys == NULL)
  629. try_channels (FALSE);
  630. /* Try to get a character */
  631. c = get_key_code (0);
  632. if (c != -1)
  633. break;
  634. /* Failed -> wait 0.1 secs and try again */
  635. try_channels (TRUE);
  636. }
  637. /* Success -> return the character */
  638. return c;
  639. }
  640. /* --------------------------------------------------------------------------------------------- */
  641. static void
  642. xmouse_get_event (Gpm_Event *ev, gboolean extended)
  643. {
  644. static gint64 tv1 = 0; /* Force first click as single */
  645. static int clicks = 0;
  646. static int last_btn = 0;
  647. int btn;
  648. /* Decode Xterm mouse information to a GPM style event */
  649. if (!extended)
  650. {
  651. /* Variable btn has following meaning: */
  652. /* 0 = btn1 dn, 1 = btn2 dn, 2 = btn3 dn, 3 = btn up */
  653. btn = tty_lowlevel_getch () - 32;
  654. /* Coordinates are 33-based */
  655. /* Transform them to 1-based */
  656. ev->x = tty_lowlevel_getch () - 32;
  657. ev->y = tty_lowlevel_getch () - 32;
  658. }
  659. else
  660. {
  661. /* SGR 1006 extension (e.g. "\e[<0;12;300M"):
  662. - Numbers are encoded in decimal to make it ASCII-safe
  663. and to overcome the limit of 223 columns/rows.
  664. - Mouse release is encoded by trailing 'm' rather than 'M'
  665. so that the released button can be reported.
  666. - Numbers are no longer offset by 32. */
  667. char c;
  668. btn = ev->x = ev->y = 0;
  669. ev->type = 0; /* In case we return on an invalid sequence */
  670. while ((c = tty_lowlevel_getch ()) != ';')
  671. {
  672. if (c < '0' || c > '9')
  673. return;
  674. btn = 10 * btn + (c - '0');
  675. }
  676. while ((c = tty_lowlevel_getch ()) != ';')
  677. {
  678. if (c < '0' || c > '9')
  679. return;
  680. ev->x = 10 * ev->x + (c - '0');
  681. }
  682. while ((c = tty_lowlevel_getch ()) != 'M' && c != 'm')
  683. {
  684. if (c < '0' || c > '9')
  685. return;
  686. ev->y = 10 * ev->y + (c - '0');
  687. }
  688. /* Legacy mouse protocol doesn't tell which button was released,
  689. conveniently all of mc's widgets are written not to rely on this
  690. information. With the SGR extension the released button becomes
  691. known, but for the sake of simplicity we just ignore it. */
  692. if (c == 'm')
  693. btn = 3;
  694. }
  695. /* There seems to be no way of knowing which button was released */
  696. /* So we assume all the buttons were released */
  697. if (btn == 3)
  698. {
  699. if (last_btn != 0)
  700. {
  701. if ((last_btn & (GPM_B_UP | GPM_B_DOWN)) != 0)
  702. {
  703. /* FIXME: DIRTY HACK */
  704. /* don't generate GPM_UP after mouse wheel */
  705. /* need for menu event handling */
  706. ev->type = 0;
  707. tv1 = 0;
  708. }
  709. else
  710. {
  711. ev->type = GPM_UP | (GPM_SINGLE << clicks);
  712. tv1 = g_get_monotonic_time ();
  713. }
  714. ev->buttons = 0;
  715. last_btn = 0;
  716. clicks = 0;
  717. }
  718. else
  719. {
  720. /* Bogus event, maybe mouse wheel */
  721. ev->type = 0;
  722. }
  723. }
  724. else
  725. {
  726. gint64 tv2;
  727. if (btn >= 32 && btn <= 34)
  728. {
  729. btn -= 32;
  730. ev->type = GPM_DRAG;
  731. }
  732. else
  733. ev->type = GPM_DOWN;
  734. tv2 = g_get_monotonic_time ();
  735. if (tv1 != 0 && tv2 - tv1 < (gint64) double_click_speed * MC_USEC_PER_MSEC)
  736. {
  737. clicks++;
  738. clicks %= 3;
  739. }
  740. else
  741. clicks = 0;
  742. switch (btn)
  743. {
  744. case 0:
  745. ev->buttons = GPM_B_LEFT;
  746. break;
  747. case 1:
  748. ev->buttons = GPM_B_MIDDLE;
  749. break;
  750. case 2:
  751. ev->buttons = GPM_B_RIGHT;
  752. break;
  753. case 64:
  754. ev->buttons = GPM_B_UP;
  755. clicks = 0;
  756. break;
  757. case 65:
  758. ev->buttons = GPM_B_DOWN;
  759. clicks = 0;
  760. break;
  761. default:
  762. /* Nothing */
  763. ev->type = 0;
  764. ev->buttons = 0;
  765. break;
  766. }
  767. last_btn = ev->buttons;
  768. }
  769. }
  770. /* --------------------------------------------------------------------------------------------- */
  771. /**
  772. * Get modifier state (shift, alt, ctrl) for the last key pressed.
  773. * We are assuming that the state didn't change since the key press.
  774. * This is only correct if get_modifier() is called very fast after
  775. * the input was received, so that the user didn't release the
  776. * modifier keys yet.
  777. */
  778. static int
  779. get_modifier (void)
  780. {
  781. int result = 0;
  782. #ifdef __QNXNTO__
  783. static int in_photon = 0;
  784. static int ph_ig = 0;
  785. #endif /* __QNXNTO__ */
  786. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  787. if (x11_window != 0)
  788. {
  789. Window root, child;
  790. int root_x, root_y;
  791. int win_x, win_y;
  792. unsigned int mask;
  793. mc_XQueryPointer (x11_display, x11_window, &root, &child, &root_x,
  794. &root_y, &win_x, &win_y, &mask);
  795. if ((mask & ShiftMask) != 0)
  796. result |= KEY_M_SHIFT;
  797. if ((mask & ControlMask) != 0)
  798. result |= KEY_M_CTRL;
  799. return result;
  800. }
  801. #endif /* HAVE_TEXTMODE_X11_SUPPORT */
  802. #ifdef __QNXNTO__
  803. if (in_photon == 0)
  804. {
  805. /* First time here, let's load Photon library and attach to Photon */
  806. in_photon = -1;
  807. if (getenv ("PHOTON2_PATH") != NULL)
  808. {
  809. /* QNX 6.x has no support for RTLD_LAZY */
  810. void *ph_handle;
  811. ph_handle = dlopen ("/usr/lib/libph.so", RTLD_NOW);
  812. if (ph_handle != NULL)
  813. {
  814. ph_attach = (ph_dv_f) dlsym (ph_handle, "PhAttach");
  815. ph_input_group = (ph_ov_f) dlsym (ph_handle, "PhInputGroup");
  816. ph_query_cursor = (ph_pqc_f) dlsym (ph_handle, "PhQueryCursor");
  817. if ((ph_attach != NULL) && (ph_input_group != NULL) && (ph_query_cursor != NULL)
  818. && (*ph_attach) (0, 0) != NULL)
  819. {
  820. /* Attached */
  821. ph_ig = (*ph_input_group) (0);
  822. in_photon = 1;
  823. }
  824. }
  825. }
  826. }
  827. /* We do not have Photon running. Assume we are in text console or xterm */
  828. if (in_photon == -1)
  829. {
  830. int mod_status;
  831. int shift_ext_status;
  832. if (devctl (fileno (stdin), DCMD_CHR_LINESTATUS, &mod_status, sizeof (mod_status), NULL) ==
  833. -1)
  834. return 0;
  835. shift_ext_status = mod_status & 0xffffff00UL;
  836. mod_status &= 0x7f;
  837. if ((mod_status & _LINESTATUS_CON_ALT) != 0)
  838. result |= KEY_M_ALT;
  839. if ((mod_status & _LINESTATUS_CON_CTRL) != 0)
  840. result |= KEY_M_CTRL;
  841. if ((mod_status & _LINESTATUS_CON_SHIFT) != 0 || (shift_ext_status & 0x00000800UL) != 0)
  842. result |= KEY_M_SHIFT;
  843. }
  844. else
  845. {
  846. PhCursorInfo_t cursor_info;
  847. (*ph_query_cursor) (ph_ig, &cursor_info);
  848. if ((cursor_info.key_mods & 0x04) != 0)
  849. result |= KEY_M_ALT;
  850. if ((cursor_info.key_mods & 0x02) != 0)
  851. result |= KEY_M_CTRL;
  852. if ((cursor_info.key_mods & 0x01) != 0)
  853. result |= KEY_M_SHIFT;
  854. }
  855. #endif /* __QNXNTO__ */
  856. #if defined __linux__ || (defined __CYGWIN__ && defined TIOCLINUX)
  857. {
  858. unsigned char modifiers = 6;
  859. if (ioctl (0, TIOCLINUX, &modifiers) < 0)
  860. return 0;
  861. /* Translate Linux modifiers into mc modifiers */
  862. if ((modifiers & SHIFT_PRESSED) != 0)
  863. result |= KEY_M_SHIFT;
  864. if ((modifiers & (ALTL_PRESSED | ALTR_PRESSED)) != 0)
  865. result |= KEY_M_ALT;
  866. if ((modifiers & CONTROL_PRESSED) != 0)
  867. result |= KEY_M_CTRL;
  868. }
  869. #endif /* !__linux__ */
  870. return result;
  871. }
  872. /* --------------------------------------------------------------------------------------------- */
  873. static gboolean
  874. push_char (int c)
  875. {
  876. gboolean ret = FALSE;
  877. if (seq_append == NULL)
  878. seq_append = seq_buffer;
  879. if (seq_append != &(seq_buffer[SEQ_BUFFER_LEN - 2]))
  880. {
  881. *(seq_append++) = c;
  882. *seq_append = '\0';
  883. ret = TRUE;
  884. }
  885. return ret;
  886. }
  887. /* --------------------------------------------------------------------------------------------- */
  888. /* Apply corrections for the keycode generated in get_key_code() */
  889. static int
  890. correct_key_code (int code)
  891. {
  892. unsigned int c = code & ~KEY_M_MASK; /* code without modifier */
  893. unsigned int mod = code & KEY_M_MASK; /* modifier */
  894. #ifdef __QNXNTO__
  895. unsigned int qmod; /* bunch of the QNX console
  896. modifiers needs unchanged */
  897. #endif /* __QNXNTO__ */
  898. /*
  899. * Add key modifiers directly from X11 or OS.
  900. * Ordinary characters only get modifiers from sequences.
  901. */
  902. if (c < 32 || c >= 256)
  903. mod |= get_modifier ();
  904. /* This is needed if the newline is reported as carriage return */
  905. if (c == '\r')
  906. c = '\n';
  907. /* This is reported to be useful on AIX */
  908. if (c == KEY_SCANCEL)
  909. c = '\t';
  910. /* Convert Back Tab to Shift+Tab */
  911. if (c == KEY_BTAB)
  912. {
  913. c = '\t';
  914. mod = KEY_M_SHIFT;
  915. }
  916. /* F0 is the same as F10 for out purposes */
  917. if (c == KEY_F (0))
  918. c = KEY_F (10);
  919. /*
  920. * We are not interested if Ctrl was pressed when entering control
  921. * characters, so assume that it was. When checking for such keys,
  922. * XCTRL macro should be used. In some cases, we are interested,
  923. * e.g. to distinguish Ctrl-Enter from Enter.
  924. */
  925. if (c == '\b')
  926. {
  927. /* Special case for backspase ('\b' < 32) */
  928. c = KEY_BACKSPACE;
  929. mod &= ~KEY_M_CTRL;
  930. }
  931. else if (c < 32 && c != ESC_CHAR && c != '\t' && c != '\n')
  932. mod |= KEY_M_CTRL;
  933. #ifdef __QNXNTO__
  934. qmod = get_modifier ();
  935. if (c == 127 && mod == 0)
  936. {
  937. /* Add Ctrl/Alt/Shift-BackSpace */
  938. mod |= get_modifier ();
  939. c = KEY_BACKSPACE;
  940. }
  941. if (c == '0' && mod == 0 && (qmod & KEY_M_SHIFT) == KEY_M_SHIFT)
  942. {
  943. /* Add Shift-Insert on key pad */
  944. mod = KEY_M_SHIFT;
  945. c = KEY_IC;
  946. }
  947. if (c == '.' && mod == 0 && (qmod & KEY_M_SHIFT) == KEY_M_SHIFT)
  948. {
  949. /* Add Shift-Del on key pad */
  950. mod = KEY_M_SHIFT;
  951. c = KEY_DC;
  952. }
  953. #endif /* __QNXNTO__ */
  954. /* Unrecognized 0177 is delete (preserve Ctrl) */
  955. if (c == 0177)
  956. c = KEY_BACKSPACE;
  957. #if 0
  958. /* Unrecognized Ctrl-d is delete */
  959. if (c == 'd' & 31)
  960. {
  961. c = KEY_DC;
  962. mod &= ~KEY_M_CTRL;
  963. }
  964. /* Unrecognized Ctrl-h is backspace */
  965. if (c == 'h' & 31)
  966. {
  967. c = KEY_BACKSPACE;
  968. mod &= ~KEY_M_CTRL;
  969. }
  970. #endif
  971. /* Shift+BackSpace is backspace */
  972. if (c == KEY_BACKSPACE && (mod & KEY_M_SHIFT) != 0)
  973. mod &= ~KEY_M_SHIFT;
  974. /* Convert Shift+Fn to F(n+10) */
  975. if (c >= KEY_F (1) && c <= KEY_F (10) && (mod & KEY_M_SHIFT) != 0)
  976. c += 10;
  977. /* Remove Shift information from function keys */
  978. if (c >= KEY_F (1) && c <= KEY_F (20))
  979. mod &= ~KEY_M_SHIFT;
  980. if (!mc_global.tty.alternate_plus_minus)
  981. switch (c)
  982. {
  983. case KEY_KP_ADD:
  984. c = '+';
  985. break;
  986. case KEY_KP_SUBTRACT:
  987. c = '-';
  988. break;
  989. case KEY_KP_MULTIPLY:
  990. c = '*';
  991. break;
  992. default:
  993. break;
  994. }
  995. return (mod | c);
  996. }
  997. /* --------------------------------------------------------------------------------------------- */
  998. static int
  999. getch_with_timeout (unsigned int delay_us)
  1000. {
  1001. fd_set Read_FD_Set;
  1002. int c;
  1003. struct timeval time_out;
  1004. time_out.tv_sec = delay_us / G_USEC_PER_SEC;
  1005. time_out.tv_usec = delay_us % G_USEC_PER_SEC;
  1006. tty_nodelay (TRUE);
  1007. FD_ZERO (&Read_FD_Set);
  1008. FD_SET (input_fd, &Read_FD_Set);
  1009. select (input_fd + 1, &Read_FD_Set, NULL, NULL, &time_out);
  1010. c = tty_lowlevel_getch ();
  1011. tty_nodelay (FALSE);
  1012. return c;
  1013. }
  1014. /* --------------------------------------------------------------------------------------------- */
  1015. static void
  1016. learn_store_key (GString *buffer, int c)
  1017. {
  1018. if (c == ESC_CHAR)
  1019. g_string_append (buffer, "\\e");
  1020. else if (c < ' ')
  1021. {
  1022. g_string_append_c (buffer, '^');
  1023. g_string_append_c (buffer, c + 'a' - 1);
  1024. }
  1025. else if (c == '^')
  1026. g_string_append (buffer, "^^");
  1027. else
  1028. g_string_append_c (buffer, (char) c);
  1029. }
  1030. /* --------------------------------------------------------------------------------------------- */
  1031. static void
  1032. k_dispose (key_def *k)
  1033. {
  1034. if (k != NULL)
  1035. {
  1036. k_dispose (k->child);
  1037. k_dispose (k->next);
  1038. g_free (k);
  1039. }
  1040. }
  1041. /* --------------------------------------------------------------------------------------------- */
  1042. static int
  1043. key_code_comparator_by_name (const void *p1, const void *p2)
  1044. {
  1045. const key_code_name_t *n1 = *(const key_code_name_t * const *) p1;
  1046. const key_code_name_t *n2 = *(const key_code_name_t * const *) p2;
  1047. return g_ascii_strcasecmp (n1->name, n2->name);
  1048. }
  1049. /* --------------------------------------------------------------------------------------------- */
  1050. static int
  1051. key_code_comparator_by_code (const void *p1, const void *p2)
  1052. {
  1053. const key_code_name_t *n1 = *(const key_code_name_t * const *) p1;
  1054. const key_code_name_t *n2 = *(const key_code_name_t * const *) p2;
  1055. return n1->code - n2->code;
  1056. }
  1057. /* --------------------------------------------------------------------------------------------- */
  1058. static inline void
  1059. sort_key_conv_tab (enum KeySortType type_sort)
  1060. {
  1061. if (has_been_sorted != type_sort)
  1062. {
  1063. size_t i;
  1064. for (i = 0; i < key_conv_tab_size; i++)
  1065. key_conv_tab_sorted[i] = &key_name_conv_tab[i];
  1066. if (type_sort == KEY_SORTBYNAME)
  1067. qsort (key_conv_tab_sorted, key_conv_tab_size, sizeof (key_conv_tab_sorted[0]),
  1068. &key_code_comparator_by_name);
  1069. else if (type_sort == KEY_SORTBYCODE)
  1070. qsort (key_conv_tab_sorted, key_conv_tab_size, sizeof (key_conv_tab_sorted[0]),
  1071. &key_code_comparator_by_code);
  1072. has_been_sorted = type_sort;
  1073. }
  1074. }
  1075. /* --------------------------------------------------------------------------------------------- */
  1076. static int
  1077. lookup_keyname (const char *name, int *idx)
  1078. {
  1079. if (name[0] != '\0')
  1080. {
  1081. const key_code_name_t key = { 0, name, NULL, NULL };
  1082. const key_code_name_t *keyp = &key;
  1083. const key_code_name_t **res;
  1084. if (name[1] == '\0')
  1085. {
  1086. *idx = -1;
  1087. return (int) name[0];
  1088. }
  1089. sort_key_conv_tab (KEY_SORTBYNAME);
  1090. res = bsearch (&keyp, key_conv_tab_sorted, key_conv_tab_size,
  1091. sizeof (key_conv_tab_sorted[0]), key_code_comparator_by_name);
  1092. if (res != NULL)
  1093. {
  1094. *idx = (int) (res - key_conv_tab_sorted);
  1095. return (*res)->code;
  1096. }
  1097. }
  1098. *idx = -1;
  1099. return 0;
  1100. }
  1101. /* --------------------------------------------------------------------------------------------- */
  1102. static gboolean
  1103. lookup_keycode (const long code, int *idx)
  1104. {
  1105. if (code != 0)
  1106. {
  1107. const key_code_name_t key = { code, NULL, NULL, NULL };
  1108. const key_code_name_t *keyp = &key;
  1109. const key_code_name_t **res;
  1110. sort_key_conv_tab (KEY_SORTBYCODE);
  1111. res = bsearch (&keyp, key_conv_tab_sorted, key_conv_tab_size,
  1112. sizeof (key_conv_tab_sorted[0]), key_code_comparator_by_code);
  1113. if (res != NULL)
  1114. {
  1115. *idx = (int) (res - key_conv_tab_sorted);
  1116. return TRUE;
  1117. }
  1118. }
  1119. *idx = -1;
  1120. return FALSE;
  1121. }
  1122. /* --------------------------------------------------------------------------------------------- */
  1123. /*** public functions ****************************************************************************/
  1124. /* --------------------------------------------------------------------------------------------- */
  1125. /* This has to be called before init_slang or whatever routine
  1126. calls any define_sequence */
  1127. void
  1128. init_key (void)
  1129. {
  1130. const char *term;
  1131. term = getenv ("TERM");
  1132. /* This has to be the first define_sequence */
  1133. /* So, we can assume that the first keys member has ESC */
  1134. define_sequences (mc_default_keys);
  1135. /* Terminfo on irix does not have some keys */
  1136. if (mc_global.tty.xterm_flag
  1137. || (term != NULL
  1138. && (strncmp (term, "iris-ansi", 9) == 0
  1139. || strncmp (term, "xterm", 5) == 0
  1140. || strncmp (term, "rxvt", 4) == 0 || strncmp (term, "screen", 6) == 0)))
  1141. define_sequences (xterm_key_defines);
  1142. /* load some additional keys (e.g. direct Alt-? support) */
  1143. load_xtra_key_defines ();
  1144. #ifdef __QNX__
  1145. if ((term != NULL) && (strncmp (term, "qnx", 3) == 0))
  1146. {
  1147. /* Modify the default value of use_8th_bit_as_meta: we would
  1148. * like to provide a working mc for a newbie who knows nothing
  1149. * about [Options|Display bits|Full 8 bits input]...
  1150. *
  1151. * Don't use 'meta'-bit, when we are dealing with a
  1152. * 'qnx*'-type terminal: clear the default value!
  1153. * These terminal types use 0xFF as an escape character,
  1154. * so use_8th_bit_as_meta==1 must not be enabled!
  1155. *
  1156. * [mc-4.1.21+,slint.c/getch(): the DEC_8BIT_HACK stuff
  1157. * is not used now (doesn't even depend on use_8th_bit_as_meta
  1158. * as in mc-3.1.2)...GREAT!...no additional code is required!]
  1159. */
  1160. use_8th_bit_as_meta = FALSE;
  1161. }
  1162. #endif /* __QNX__ */
  1163. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  1164. init_key_x11 ();
  1165. #endif
  1166. /* Load the qansi-m key definitions
  1167. if we are running under the qansi-m terminal */
  1168. if (term != NULL && (strncmp (term, "qansi-m", 7) == 0))
  1169. define_sequences (qansi_key_defines);
  1170. }
  1171. /* --------------------------------------------------------------------------------------------- */
  1172. /**
  1173. * This has to be called after SLang_init_tty/slint_init
  1174. */
  1175. void
  1176. init_key_input_fd (void)
  1177. {
  1178. #ifdef HAVE_SLANG
  1179. input_fd = SLang_TT_Read_FD;
  1180. #endif
  1181. }
  1182. /* --------------------------------------------------------------------------------------------- */
  1183. void
  1184. done_key (void)
  1185. {
  1186. k_dispose (keys);
  1187. g_slist_free_full (select_list, g_free);
  1188. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  1189. if (x11_display)
  1190. mc_XCloseDisplay (x11_display);
  1191. #endif
  1192. }
  1193. /* --------------------------------------------------------------------------------------------- */
  1194. void
  1195. add_select_channel (int fd, select_fn callback, void *info)
  1196. {
  1197. select_t *new;
  1198. new = g_new (select_t, 1);
  1199. new->fd = fd;
  1200. new->callback = callback;
  1201. new->info = info;
  1202. select_list = g_slist_prepend (select_list, new);
  1203. }
  1204. /* --------------------------------------------------------------------------------------------- */
  1205. void
  1206. delete_select_channel (int fd)
  1207. {
  1208. GSList *p;
  1209. p = g_slist_find_custom (select_list, GINT_TO_POINTER (fd), select_cmp_by_fd);
  1210. if (p != NULL)
  1211. select_list = g_slist_delete_link (select_list, p);
  1212. }
  1213. /* --------------------------------------------------------------------------------------------- */
  1214. void
  1215. channels_up (void)
  1216. {
  1217. if (disabled_channels == 0)
  1218. fputs ("Error: channels_up called with disabled_channels = 0\n", stderr);
  1219. disabled_channels--;
  1220. }
  1221. /* --------------------------------------------------------------------------------------------- */
  1222. void
  1223. channels_down (void)
  1224. {
  1225. disabled_channels++;
  1226. }
  1227. /* --------------------------------------------------------------------------------------------- */
  1228. /**
  1229. * Return the code associated with the symbolic name keyname
  1230. */
  1231. long
  1232. tty_keyname_to_keycode (const char *name, char **label)
  1233. {
  1234. char **lc_keys, **p;
  1235. char *cname;
  1236. int k = -1;
  1237. int key = 0;
  1238. int lc_index = -1;
  1239. int use_meta = -1;
  1240. int use_ctrl = -1;
  1241. int use_shift = -1;
  1242. if (name == NULL)
  1243. return 0;
  1244. cname = g_strstrip (g_strdup (name));
  1245. lc_keys = g_strsplit_set (cname, "-+ ", -1);
  1246. g_free (cname);
  1247. for (p = lc_keys; p != NULL && *p != NULL; p++)
  1248. {
  1249. if ((*p)[0] != '\0')
  1250. {
  1251. int idx;
  1252. key = lookup_keyname (g_strstrip (*p), &idx);
  1253. if (key == KEY_M_ALT)
  1254. use_meta = idx;
  1255. else if (key == KEY_M_CTRL)
  1256. use_ctrl = idx;
  1257. else if (key == KEY_M_SHIFT)
  1258. use_shift = idx;
  1259. else
  1260. {
  1261. k = key;
  1262. lc_index = idx;
  1263. break;
  1264. }
  1265. }
  1266. }
  1267. g_strfreev (lc_keys);
  1268. /* output */
  1269. if (k <= 0)
  1270. return 0;
  1271. if (label != NULL)
  1272. {
  1273. GString *s;
  1274. s = g_string_new ("");
  1275. if (use_meta != -1)
  1276. {
  1277. g_string_append (s, key_conv_tab_sorted[use_meta]->shortcut);
  1278. g_string_append_c (s, '-');
  1279. }
  1280. if (use_ctrl != -1)
  1281. {
  1282. g_string_append (s, key_conv_tab_sorted[use_ctrl]->shortcut);
  1283. g_string_append_c (s, '-');
  1284. }
  1285. if (use_shift != -1)
  1286. {
  1287. if (k < 127)
  1288. g_string_append_c (s, (gchar) g_ascii_toupper ((gchar) k));
  1289. else
  1290. {
  1291. g_string_append (s, key_conv_tab_sorted[use_shift]->shortcut);
  1292. g_string_append_c (s, '-');
  1293. g_string_append (s, key_conv_tab_sorted[lc_index]->shortcut);
  1294. }
  1295. }
  1296. else if (k < 128)
  1297. {
  1298. if ((k >= 'A') || (lc_index < 0) || (key_conv_tab_sorted[lc_index]->shortcut == NULL))
  1299. g_string_append_c (s, (gchar) g_ascii_tolower ((gchar) k));
  1300. else
  1301. g_string_append (s, key_conv_tab_sorted[lc_index]->shortcut);
  1302. }
  1303. else if ((lc_index != -1) && (key_conv_tab_sorted[lc_index]->shortcut != NULL))
  1304. g_string_append (s, key_conv_tab_sorted[lc_index]->shortcut);
  1305. else
  1306. g_string_append_c (s, (gchar) g_ascii_tolower ((gchar) key));
  1307. *label = g_string_free (s, FALSE);
  1308. }
  1309. if (use_shift != -1)
  1310. {
  1311. if (k < 127 && k > 31)
  1312. k = g_ascii_toupper ((gchar) k);
  1313. else
  1314. k |= KEY_M_SHIFT;
  1315. }
  1316. if (use_ctrl != -1)
  1317. {
  1318. if (k < 256)
  1319. k = XCTRL (k);
  1320. else
  1321. k |= KEY_M_CTRL;
  1322. }
  1323. if (use_meta != -1)
  1324. k = ALT (k);
  1325. return (long) k;
  1326. }
  1327. /* --------------------------------------------------------------------------------------------- */
  1328. char *
  1329. tty_keycode_to_keyname (const int keycode)
  1330. {
  1331. /* code without modifier */
  1332. unsigned int k = keycode & ~KEY_M_MASK;
  1333. /* modifier */
  1334. unsigned int mod = keycode & KEY_M_MASK;
  1335. int key_idx = -1;
  1336. GString *s;
  1337. int idx;
  1338. s = g_string_sized_new (8);
  1339. if (lookup_keycode (k, &key_idx) || (k > 0 && k < 256))
  1340. {
  1341. if ((mod & KEY_M_ALT) != 0 && lookup_keycode (KEY_M_ALT, &idx))
  1342. {
  1343. g_string_append (s, key_conv_tab_sorted[idx]->name);
  1344. g_string_append_c (s, '-');
  1345. }
  1346. if ((mod & KEY_M_CTRL) != 0)
  1347. {
  1348. /* non printeble chars like a CTRL-[A..Z] */
  1349. if (k < 32)
  1350. k += 64;
  1351. if (lookup_keycode (KEY_M_CTRL, &idx))
  1352. {
  1353. g_string_append (s, key_conv_tab_sorted[idx]->name);
  1354. g_string_append_c (s, '-');
  1355. }
  1356. }
  1357. if ((mod & KEY_M_SHIFT) != 0)
  1358. {
  1359. if (lookup_keycode (KEY_M_ALT, &idx))
  1360. {
  1361. if (k < 127)
  1362. g_string_append_c (s, (gchar) g_ascii_toupper ((gchar) k));
  1363. else
  1364. {
  1365. g_string_append (s, key_conv_tab_sorted[idx]->name);
  1366. g_string_append_c (s, '-');
  1367. g_string_append (s, key_conv_tab_sorted[key_idx]->name);
  1368. }
  1369. }
  1370. }
  1371. else if (k < 128)
  1372. {
  1373. if ((k >= 'A') || (key_idx < 0) || (key_conv_tab_sorted[key_idx]->name == NULL))
  1374. g_string_append_c (s, (gchar) k);
  1375. else
  1376. g_string_append (s, key_conv_tab_sorted[key_idx]->name);
  1377. }
  1378. else if ((key_idx != -1) && (key_conv_tab_sorted[key_idx]->name != NULL))
  1379. g_string_append (s, key_conv_tab_sorted[key_idx]->name);
  1380. else
  1381. g_string_append_c (s, (gchar) keycode);
  1382. }
  1383. return g_string_free (s, s->len == 0);
  1384. }
  1385. /* --------------------------------------------------------------------------------------------- */
  1386. /**
  1387. * Return TRUE on success, FALSE on error.
  1388. * An error happens if SEQ is a beginning of an existing longer sequence.
  1389. */
  1390. gboolean
  1391. define_sequence (int code, const char *seq, int action)
  1392. {
  1393. key_def *base;
  1394. if (strlen (seq) > SEQ_BUFFER_LEN - 1)
  1395. return FALSE;
  1396. for (base = keys; (base != NULL) && (*seq != '\0');)
  1397. if (*seq == base->ch)
  1398. {
  1399. if (base->child == NULL)
  1400. {
  1401. if (*(seq + 1) != '\0')
  1402. base->child = create_sequence (seq + 1, code, action);
  1403. else
  1404. {
  1405. /* The sequence matches an existing one. */
  1406. base->code = code;
  1407. base->action = action;
  1408. }
  1409. return TRUE;
  1410. }
  1411. base = base->child;
  1412. seq++;
  1413. }
  1414. else
  1415. {
  1416. if (base->next != NULL)
  1417. base = base->next;
  1418. else
  1419. {
  1420. base->next = create_sequence (seq, code, action);
  1421. return TRUE;
  1422. }
  1423. }
  1424. if (*seq == '\0')
  1425. {
  1426. /* Attempt to redefine a sequence with a shorter sequence. */
  1427. return FALSE;
  1428. }
  1429. keys = create_sequence (seq, code, action);
  1430. return TRUE;
  1431. }
  1432. /* --------------------------------------------------------------------------------------------- */
  1433. /**
  1434. * Check if we are idle, i.e. there are no pending keyboard or mouse
  1435. * events. Return 1 is idle, 0 is there are pending events.
  1436. */
  1437. gboolean
  1438. is_idle (void)
  1439. {
  1440. int nfd;
  1441. fd_set select_set;
  1442. struct timeval time_out;
  1443. FD_ZERO (&select_set);
  1444. FD_SET (input_fd, &select_set);
  1445. nfd = MAX (0, input_fd) + 1;
  1446. time_out.tv_sec = 0;
  1447. time_out.tv_usec = 0;
  1448. #ifdef HAVE_LIBGPM
  1449. if (mouse_enabled && use_mouse_p == MOUSE_GPM)
  1450. {
  1451. if (gpm_fd >= 0)
  1452. {
  1453. FD_SET (gpm_fd, &select_set);
  1454. nfd = MAX (nfd, gpm_fd + 1);
  1455. }
  1456. else
  1457. {
  1458. if (mouse_fd >= 0) /* error indicative */
  1459. {
  1460. if (FD_ISSET (mouse_fd, &select_set))
  1461. FD_CLR (mouse_fd, &select_set);
  1462. mouse_fd = gpm_fd;
  1463. }
  1464. /* gpm_fd == -2 means under some X terminal */
  1465. if (gpm_fd == -1)
  1466. {
  1467. mouse_enabled = FALSE;
  1468. use_mouse_p = MOUSE_NONE;
  1469. }
  1470. }
  1471. }
  1472. #endif
  1473. return (select (nfd, &select_set, 0, 0, &time_out) <= 0);
  1474. }
  1475. /* --------------------------------------------------------------------------------------------- */
  1476. int
  1477. get_key_code (int no_delay)
  1478. {
  1479. int c;
  1480. static key_def *this = NULL, *parent;
  1481. static gint64 esc_time = -1;
  1482. static int lastnodelay = -1;
  1483. if (no_delay != lastnodelay)
  1484. {
  1485. this = NULL;
  1486. lastnodelay = no_delay;
  1487. }
  1488. pend_send:
  1489. if (pending_keys != NULL)
  1490. {
  1491. gboolean bad_seq;
  1492. c = *pending_keys++;
  1493. while (c == ESC_CHAR)
  1494. c = ALT (*pending_keys++);
  1495. bad_seq = (*pending_keys != ESC_CHAR && *pending_keys != '\0');
  1496. if (*pending_keys == '\0' || bad_seq)
  1497. pending_keys = seq_append = NULL;
  1498. if (bad_seq)
  1499. {
  1500. /* This is an unknown ESC sequence.
  1501. * To prevent interpreting its tail as a random garbage,
  1502. * eat and discard all buffered and quickly following chars.
  1503. * Small, but non-zero timeout is needed to reconnect
  1504. * escape sequence split up by e.g. a serial line.
  1505. */
  1506. int paranoia = 20;
  1507. while (getch_with_timeout (old_esc_mode_timeout) >= 0 && --paranoia != 0)
  1508. ;
  1509. }
  1510. else
  1511. {
  1512. if (c > 127 && c < 256 && use_8th_bit_as_meta)
  1513. c = ALT (c & 0x7f);
  1514. goto done;
  1515. }
  1516. }
  1517. nodelay_try_again:
  1518. if (no_delay != 0)
  1519. tty_nodelay (TRUE);
  1520. c = tty_lowlevel_getch ();
  1521. #if (defined(USE_NCURSES) || defined(USE_NCURSESW)) && defined(KEY_RESIZE)
  1522. if (c == KEY_RESIZE)
  1523. goto nodelay_try_again;
  1524. #endif
  1525. if (no_delay != 0)
  1526. {
  1527. tty_nodelay (FALSE);
  1528. if (c == -1)
  1529. {
  1530. if (this == NULL || parent == NULL || parent->action != MCKEY_ESCAPE || !old_esc_mode ||
  1531. esc_time == -1 || g_get_monotonic_time () < esc_time + old_esc_mode_timeout)
  1532. return -1;
  1533. this = NULL;
  1534. pending_keys = seq_append = NULL;
  1535. return ESC_CHAR;
  1536. }
  1537. }
  1538. else if (c == -1)
  1539. {
  1540. /* Maybe we got an incomplete match.
  1541. This we do only in delay mode, since otherwise
  1542. tty_lowlevel_getch can return -1 at any time. */
  1543. if (seq_append == NULL)
  1544. {
  1545. this = NULL;
  1546. return -1;
  1547. }
  1548. pending_keys = seq_buffer;
  1549. goto pend_send;
  1550. }
  1551. /* Search the key on the root */
  1552. if (no_delay == 0 || this == NULL)
  1553. {
  1554. this = keys;
  1555. parent = NULL;
  1556. if (c > 127 && c < 256 && use_8th_bit_as_meta)
  1557. {
  1558. c &= 0x7f;
  1559. /* The first sequence defined starts with esc */
  1560. parent = keys;
  1561. this = keys->child;
  1562. }
  1563. }
  1564. while (this != NULL)
  1565. {
  1566. if (c == this->ch)
  1567. {
  1568. if (this->child == NULL)
  1569. {
  1570. /* We got a complete match, return and reset search */
  1571. pending_keys = seq_append = NULL;
  1572. c = this->code;
  1573. goto done;
  1574. }
  1575. /* No match yet, but it may be a prefix for a valid seq */
  1576. if (!push_char (c))
  1577. {
  1578. pending_keys = seq_buffer;
  1579. goto pend_send;
  1580. }
  1581. parent = this;
  1582. this = this->child;
  1583. if (parent->action == MCKEY_ESCAPE && old_esc_mode)
  1584. {
  1585. if (no_delay != 0)
  1586. {
  1587. esc_time = g_get_monotonic_time ();
  1588. goto nodelay_try_again;
  1589. }
  1590. esc_time = -1;
  1591. c = getch_with_timeout (old_esc_mode_timeout);
  1592. if (c != -1)
  1593. continue;
  1594. pending_keys = seq_append = NULL;
  1595. this = NULL;
  1596. return ESC_CHAR;
  1597. }
  1598. if (no_delay != 0)
  1599. goto nodelay_try_again;
  1600. c = tty_lowlevel_getch ();
  1601. continue;
  1602. }
  1603. /* c != this->ch. Try other keys with this prefix */
  1604. if (this->next != NULL)
  1605. {
  1606. this = this->next;
  1607. continue;
  1608. }
  1609. /* No match found. Is it one of our ESC <key> specials? */
  1610. if ((parent != NULL) && (parent->action == MCKEY_ESCAPE))
  1611. {
  1612. /* Convert escape-digits to F-keys */
  1613. if (g_ascii_isdigit (c))
  1614. c = KEY_F (c - '0');
  1615. else if (c == ' ')
  1616. c = ESC_CHAR;
  1617. else
  1618. c = ALT (c);
  1619. pending_keys = seq_append = NULL;
  1620. goto done;
  1621. }
  1622. /* Unknown sequence. Maybe a prefix of a longer one. Save it. */
  1623. push_char (c);
  1624. pending_keys = seq_buffer;
  1625. goto pend_send;
  1626. } /* while (this != NULL) */
  1627. done:
  1628. this = NULL;
  1629. return correct_key_code (c);
  1630. }
  1631. /* --------------------------------------------------------------------------------------------- */
  1632. /* Returns a character read from stdin with appropriate interpretation */
  1633. /* Also takes care of generated mouse events */
  1634. /* Returns EV_MOUSE if it is a mouse event */
  1635. /* Returns EV_NONE if non-blocking or interrupt set and nothing was done */
  1636. int
  1637. tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block)
  1638. {
  1639. int c;
  1640. int flag = 0; /* Return value from select */
  1641. #ifdef HAVE_LIBGPM
  1642. static struct Gpm_Event ev; /* Mouse event */
  1643. #endif
  1644. struct timeval time_out;
  1645. struct timeval *time_addr = NULL;
  1646. static int dirty = 3;
  1647. if ((dirty == 3) || is_idle ())
  1648. {
  1649. mc_refresh ();
  1650. dirty = 1;
  1651. }
  1652. else
  1653. dirty++;
  1654. vfs_timeout_handler ();
  1655. /* Ok, we use (event->x < 0) to signal that the event does not contain
  1656. a suitable position for the mouse, so we can't use show_mouse_pointer
  1657. on it.
  1658. */
  1659. if (event->x > 0)
  1660. {
  1661. show_mouse_pointer (event->x, event->y);
  1662. if (!redo_event)
  1663. event->x = -1;
  1664. }
  1665. /* Repeat if using mouse */
  1666. while (pending_keys == NULL)
  1667. {
  1668. int nfd;
  1669. fd_set select_set;
  1670. FD_ZERO (&select_set);
  1671. FD_SET (input_fd, &select_set);
  1672. nfd = MAX (add_selects (&select_set), MAX (0, input_fd)) + 1;
  1673. #ifdef HAVE_LIBGPM
  1674. if (mouse_enabled && (use_mouse_p == MOUSE_GPM))
  1675. {
  1676. if (gpm_fd >= 0)
  1677. {
  1678. FD_SET (gpm_fd, &select_set);
  1679. nfd = MAX (nfd, gpm_fd + 1);
  1680. }
  1681. else
  1682. {
  1683. if (mouse_fd >= 0) /* error indicative */
  1684. {
  1685. if (FD_ISSET (mouse_fd, &select_set))
  1686. FD_CLR (mouse_fd, &select_set);
  1687. mouse_fd = gpm_fd;
  1688. }
  1689. /* gpm_fd == -2 means under some X terminal */
  1690. if (gpm_fd == -1)
  1691. {
  1692. mouse_enabled = FALSE;
  1693. use_mouse_p = MOUSE_NONE;
  1694. }
  1695. break;
  1696. }
  1697. }
  1698. #endif
  1699. if (redo_event)
  1700. {
  1701. time_out.tv_usec = mou_auto_repeat * MC_USEC_PER_MSEC;
  1702. time_out.tv_sec = 0;
  1703. time_addr = &time_out;
  1704. }
  1705. else
  1706. {
  1707. int seconds;
  1708. seconds = vfs_timeouts ();
  1709. time_addr = NULL;
  1710. if (seconds != 0)
  1711. {
  1712. /* the timeout could be improved and actually be
  1713. * the number of seconds until the next vfs entry
  1714. * timeouts in the stamp list.
  1715. */
  1716. time_out.tv_sec = seconds;
  1717. time_out.tv_usec = 0;
  1718. time_addr = &time_out;
  1719. }
  1720. }
  1721. if (!block || tty_got_winch ())
  1722. {
  1723. time_addr = &time_out;
  1724. time_out.tv_sec = 0;
  1725. time_out.tv_usec = 0;
  1726. }
  1727. tty_enable_interrupt_key ();
  1728. flag = select (nfd, &select_set, NULL, NULL, time_addr);
  1729. tty_disable_interrupt_key ();
  1730. /* select timed out: it could be for any of the following reasons:
  1731. * redo_event -> it was because of the MOU_REPEAT handler
  1732. * !block -> we did not block in the select call
  1733. * else -> 10 second timeout to check the vfs status.
  1734. */
  1735. if (flag == 0)
  1736. {
  1737. if (redo_event)
  1738. return EV_MOUSE;
  1739. if (!block || tty_got_winch ())
  1740. return EV_NONE;
  1741. vfs_timeout_handler ();
  1742. }
  1743. if (flag == -1 && errno == EINTR)
  1744. return EV_NONE;
  1745. check_selects (&select_set);
  1746. if (FD_ISSET (input_fd, &select_set))
  1747. break;
  1748. #ifdef HAVE_LIBGPM
  1749. if (mouse_enabled && use_mouse_p == MOUSE_GPM)
  1750. {
  1751. if (gpm_fd >= 0)
  1752. {
  1753. if (FD_ISSET (gpm_fd, &select_set))
  1754. {
  1755. int status;
  1756. status = Gpm_GetEvent (&ev);
  1757. if (status == 1) /* success */
  1758. {
  1759. Gpm_FitEvent (&ev);
  1760. *event = ev;
  1761. return EV_MOUSE;
  1762. }
  1763. if (status <= 0) /* connection closed; -1 == error */
  1764. {
  1765. if (mouse_fd >= 0 && FD_ISSET (mouse_fd, &select_set))
  1766. FD_CLR (mouse_fd, &select_set);
  1767. disable_mouse ();
  1768. return EV_NONE;
  1769. }
  1770. }
  1771. }
  1772. else
  1773. {
  1774. if (mouse_fd >= 0) /* error indicative */
  1775. {
  1776. if (FD_ISSET (mouse_fd, &select_set))
  1777. FD_CLR (mouse_fd, &select_set);
  1778. mouse_fd = gpm_fd;
  1779. }
  1780. /* gpm_fd == -2 means under some X terminal */
  1781. if (gpm_fd == -1)
  1782. {
  1783. mouse_enabled = FALSE;
  1784. use_mouse_p = MOUSE_NONE;
  1785. }
  1786. break;
  1787. }
  1788. }
  1789. #endif /* !HAVE_LIBGPM */
  1790. }
  1791. #ifndef HAVE_SLANG
  1792. flag = is_wintouched (stdscr);
  1793. untouchwin (stdscr);
  1794. #endif /* !HAVE_SLANG */
  1795. c = block ? getch_with_delay () : get_key_code (1);
  1796. #ifndef HAVE_SLANG
  1797. if (flag > 0)
  1798. tty_touch_screen ();
  1799. #endif /* !HAVE_SLANG */
  1800. if (mouse_enabled && (c == MCKEY_MOUSE
  1801. #ifdef KEY_MOUSE
  1802. || c == KEY_MOUSE
  1803. #endif /* KEY_MOUSE */
  1804. || c == MCKEY_EXTENDED_MOUSE))
  1805. {
  1806. /* Mouse event. See tickets 2956 and 3954 for extended mode detection. */
  1807. gboolean extended = c == MCKEY_EXTENDED_MOUSE;
  1808. #ifdef KEY_MOUSE
  1809. extended = extended || (c == KEY_MOUSE && xmouse_seq == NULL
  1810. && xmouse_extended_seq != NULL);
  1811. #endif /* KEY_MOUSE */
  1812. xmouse_get_event (event, extended);
  1813. c = (event->type != 0) ? EV_MOUSE : EV_NONE;
  1814. }
  1815. else if (c == MCKEY_BRACKETED_PASTING_START)
  1816. {
  1817. bracketed_pasting_in_progress = TRUE;
  1818. c = EV_NONE;
  1819. }
  1820. else if (c == MCKEY_BRACKETED_PASTING_END)
  1821. {
  1822. bracketed_pasting_in_progress = FALSE;
  1823. c = EV_NONE;
  1824. }
  1825. return c;
  1826. }
  1827. /* --------------------------------------------------------------------------------------------- */
  1828. /* Returns a key press, mouse events are discarded */
  1829. int
  1830. tty_getch (void)
  1831. {
  1832. Gpm_Event ev;
  1833. int key;
  1834. ev.x = -1;
  1835. while ((key = tty_get_event (&ev, FALSE, TRUE)) == EV_NONE)
  1836. ;
  1837. return key;
  1838. }
  1839. /* --------------------------------------------------------------------------------------------- */
  1840. char *
  1841. learn_key (void)
  1842. {
  1843. /* LEARN_TIMEOUT in ms */
  1844. #define LEARN_TIMEOUT 200
  1845. fd_set Read_FD_Set;
  1846. gint64 end_time;
  1847. int c;
  1848. GString *buffer;
  1849. buffer = g_string_sized_new (16);
  1850. tty_keypad (FALSE); /* disable interpreting keys by ncurses */
  1851. c = tty_lowlevel_getch ();
  1852. while (c == -1)
  1853. c = tty_lowlevel_getch (); /* Sanity check, should be unnecessary */
  1854. learn_store_key (buffer, c);
  1855. end_time = g_get_monotonic_time () + LEARN_TIMEOUT * MC_USEC_PER_MSEC;
  1856. tty_nodelay (TRUE);
  1857. while (TRUE)
  1858. {
  1859. while ((c = tty_lowlevel_getch ()) == -1)
  1860. {
  1861. gint64 time_out;
  1862. struct timeval tv;
  1863. time_out = end_time - g_get_monotonic_time ();
  1864. if (time_out <= 0)
  1865. break;
  1866. tv.tv_sec = time_out / G_USEC_PER_SEC;
  1867. tv.tv_usec = time_out % G_USEC_PER_SEC;
  1868. FD_ZERO (&Read_FD_Set);
  1869. FD_SET (input_fd, &Read_FD_Set);
  1870. select (input_fd + 1, &Read_FD_Set, NULL, NULL, &tv);
  1871. }
  1872. if (c == -1)
  1873. break;
  1874. learn_store_key (buffer, c);
  1875. }
  1876. tty_keypad (TRUE);
  1877. tty_nodelay (FALSE);
  1878. return g_string_free (buffer, buffer->len == 0);
  1879. #undef LEARN_TIMEOUT
  1880. }
  1881. /* --------------------------------------------------------------------------------------------- */
  1882. /* xterm and linux console only: set keypad to numeric or application
  1883. mode. Only in application keypad mode it's possible to distinguish
  1884. the '+' key and the '+' on the keypad ('*' and '-' ditto) */
  1885. void
  1886. numeric_keypad_mode (void)
  1887. {
  1888. if (mc_global.tty.console_flag != '\0' || mc_global.tty.xterm_flag)
  1889. {
  1890. fputs (ESC_STR ">", stdout);
  1891. fflush (stdout);
  1892. }
  1893. }
  1894. /* --------------------------------------------------------------------------------------------- */
  1895. void
  1896. application_keypad_mode (void)
  1897. {
  1898. if (mc_global.tty.console_flag != '\0' || mc_global.tty.xterm_flag)
  1899. {
  1900. fputs (ESC_STR "=", stdout);
  1901. fflush (stdout);
  1902. }
  1903. }
  1904. /* --------------------------------------------------------------------------------------------- */
  1905. void
  1906. enable_bracketed_paste (void)
  1907. {
  1908. printf (ESC_STR "[?2004h");
  1909. fflush (stdout);
  1910. }
  1911. /* --------------------------------------------------------------------------------------------- */
  1912. void
  1913. disable_bracketed_paste (void)
  1914. {
  1915. printf (ESC_STR "[?2004l");
  1916. fflush (stdout);
  1917. bracketed_pasting_in_progress = FALSE;
  1918. }
  1919. /* --------------------------------------------------------------------------------------------- */