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, *attach;
  579. for (base = attach = NULL; *seq != '\0'; seq++)
  580. {
  581. key_def *p;
  582. p = g_new (key_def, 1);
  583. if (base == NULL)
  584. base = p;
  585. if (attach != NULL)
  586. attach->child = p;
  587. p->ch = *seq;
  588. p->code = code;
  589. p->child = NULL;
  590. p->next = NULL;
  591. p->action = seq[1] == '\0' ? action : MCKEY_NOACTION;
  592. attach = p;
  593. }
  594. return base;
  595. }
  596. /* --------------------------------------------------------------------------------------------- */
  597. static void
  598. define_sequences (const key_define_t *kd)
  599. {
  600. int i;
  601. for (i = 0; kd[i].code != 0; i++)
  602. define_sequence (kd[i].code, kd[i].seq, kd[i].action);
  603. }
  604. /* --------------------------------------------------------------------------------------------- */
  605. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  606. static void
  607. init_key_x11 (void)
  608. {
  609. if (getenv ("DISPLAY") != NULL && !mc_global.tty.disable_x11)
  610. {
  611. x11_display = mc_XOpenDisplay (0);
  612. if (x11_display != NULL)
  613. x11_window = DefaultRootWindow (x11_display);
  614. }
  615. }
  616. #endif /* HAVE_TEXTMODE_X11_SUPPORT */
  617. /* --------------------------------------------------------------------------------------------- */
  618. /* Workaround for System V Curses vt100 bug */
  619. static int
  620. getch_with_delay (void)
  621. {
  622. int c;
  623. /* This routine could be used on systems without mouse support,
  624. so we need to do the select check :-( */
  625. while (TRUE)
  626. {
  627. if (pending_keys == NULL)
  628. try_channels (FALSE);
  629. /* Try to get a character */
  630. c = get_key_code (0);
  631. if (c != -1)
  632. break;
  633. /* Failed -> wait 0.1 secs and try again */
  634. try_channels (TRUE);
  635. }
  636. /* Success -> return the character */
  637. return c;
  638. }
  639. /* --------------------------------------------------------------------------------------------- */
  640. static void
  641. xmouse_get_event (Gpm_Event *ev, gboolean extended)
  642. {
  643. static gint64 tv1 = 0; /* Force first click as single */
  644. static int clicks = 0;
  645. static int last_btn = 0;
  646. int btn;
  647. /* Decode Xterm mouse information to a GPM style event */
  648. if (!extended)
  649. {
  650. /* Variable btn has following meaning: */
  651. /* 0 = btn1 dn, 1 = btn2 dn, 2 = btn3 dn, 3 = btn up */
  652. btn = tty_lowlevel_getch () - 32;
  653. /* Coordinates are 33-based */
  654. /* Transform them to 1-based */
  655. ev->x = tty_lowlevel_getch () - 32;
  656. ev->y = tty_lowlevel_getch () - 32;
  657. }
  658. else
  659. {
  660. /* SGR 1006 extension (e.g. "\e[<0;12;300M"):
  661. - Numbers are encoded in decimal to make it ASCII-safe
  662. and to overcome the limit of 223 columns/rows.
  663. - Mouse release is encoded by trailing 'm' rather than 'M'
  664. so that the released button can be reported.
  665. - Numbers are no longer offset by 32. */
  666. char c;
  667. btn = ev->x = ev->y = 0;
  668. ev->type = 0; /* In case we return on an invalid sequence */
  669. while ((c = tty_lowlevel_getch ()) != ';')
  670. {
  671. if (c < '0' || c > '9')
  672. return;
  673. btn = 10 * btn + (c - '0');
  674. }
  675. while ((c = tty_lowlevel_getch ()) != ';')
  676. {
  677. if (c < '0' || c > '9')
  678. return;
  679. ev->x = 10 * ev->x + (c - '0');
  680. }
  681. while ((c = tty_lowlevel_getch ()) != 'M' && c != 'm')
  682. {
  683. if (c < '0' || c > '9')
  684. return;
  685. ev->y = 10 * ev->y + (c - '0');
  686. }
  687. /* Legacy mouse protocol doesn't tell which button was released,
  688. conveniently all of mc's widgets are written not to rely on this
  689. information. With the SGR extension the released button becomes
  690. known, but for the sake of simplicity we just ignore it. */
  691. if (c == 'm')
  692. btn = 3;
  693. }
  694. /* There seems to be no way of knowing which button was released */
  695. /* So we assume all the buttons were released */
  696. if (btn == 3)
  697. {
  698. if (last_btn != 0)
  699. {
  700. if ((last_btn & (GPM_B_UP | GPM_B_DOWN)) != 0)
  701. {
  702. /* FIXME: DIRTY HACK */
  703. /* don't generate GPM_UP after mouse wheel */
  704. /* need for menu event handling */
  705. ev->type = 0;
  706. tv1 = 0;
  707. }
  708. else
  709. {
  710. ev->type = GPM_UP | (GPM_SINGLE << clicks);
  711. tv1 = g_get_monotonic_time ();
  712. }
  713. ev->buttons = 0;
  714. last_btn = 0;
  715. clicks = 0;
  716. }
  717. else
  718. {
  719. /* Bogus event, maybe mouse wheel */
  720. ev->type = 0;
  721. }
  722. }
  723. else
  724. {
  725. gint64 tv2;
  726. if (btn >= 32 && btn <= 34)
  727. {
  728. btn -= 32;
  729. ev->type = GPM_DRAG;
  730. }
  731. else
  732. ev->type = GPM_DOWN;
  733. tv2 = g_get_monotonic_time ();
  734. if (tv1 != 0 && tv2 - tv1 < (gint64) double_click_speed * MC_USEC_PER_MSEC)
  735. {
  736. clicks++;
  737. clicks %= 3;
  738. }
  739. else
  740. clicks = 0;
  741. switch (btn)
  742. {
  743. case 0:
  744. ev->buttons = GPM_B_LEFT;
  745. break;
  746. case 1:
  747. ev->buttons = GPM_B_MIDDLE;
  748. break;
  749. case 2:
  750. ev->buttons = GPM_B_RIGHT;
  751. break;
  752. case 64:
  753. ev->buttons = GPM_B_UP;
  754. clicks = 0;
  755. break;
  756. case 65:
  757. ev->buttons = GPM_B_DOWN;
  758. clicks = 0;
  759. break;
  760. default:
  761. /* Nothing */
  762. ev->type = 0;
  763. ev->buttons = 0;
  764. break;
  765. }
  766. last_btn = ev->buttons;
  767. }
  768. }
  769. /* --------------------------------------------------------------------------------------------- */
  770. /**
  771. * Get modifier state (shift, alt, ctrl) for the last key pressed.
  772. * We are assuming that the state didn't change since the key press.
  773. * This is only correct if get_modifier() is called very fast after
  774. * the input was received, so that the user didn't release the
  775. * modifier keys yet.
  776. */
  777. static int
  778. get_modifier (void)
  779. {
  780. int result = 0;
  781. #ifdef __QNXNTO__
  782. static int in_photon = 0;
  783. static int ph_ig = 0;
  784. #endif /* __QNXNTO__ */
  785. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  786. if (x11_window != 0)
  787. {
  788. Window root, child;
  789. int root_x, root_y;
  790. int win_x, win_y;
  791. unsigned int mask;
  792. mc_XQueryPointer (x11_display, x11_window, &root, &child, &root_x,
  793. &root_y, &win_x, &win_y, &mask);
  794. if ((mask & ShiftMask) != 0)
  795. result |= KEY_M_SHIFT;
  796. if ((mask & ControlMask) != 0)
  797. result |= KEY_M_CTRL;
  798. return result;
  799. }
  800. #endif /* HAVE_TEXTMODE_X11_SUPPORT */
  801. #ifdef __QNXNTO__
  802. if (in_photon == 0)
  803. {
  804. /* First time here, let's load Photon library and attach to Photon */
  805. in_photon = -1;
  806. if (getenv ("PHOTON2_PATH") != NULL)
  807. {
  808. /* QNX 6.x has no support for RTLD_LAZY */
  809. void *ph_handle;
  810. ph_handle = dlopen ("/usr/lib/libph.so", RTLD_NOW);
  811. if (ph_handle != NULL)
  812. {
  813. ph_attach = (ph_dv_f) dlsym (ph_handle, "PhAttach");
  814. ph_input_group = (ph_ov_f) dlsym (ph_handle, "PhInputGroup");
  815. ph_query_cursor = (ph_pqc_f) dlsym (ph_handle, "PhQueryCursor");
  816. if ((ph_attach != NULL) && (ph_input_group != NULL) && (ph_query_cursor != NULL)
  817. && (*ph_attach) (0, 0) != NULL)
  818. {
  819. /* Attached */
  820. ph_ig = (*ph_input_group) (0);
  821. in_photon = 1;
  822. }
  823. }
  824. }
  825. }
  826. /* We do not have Photon running. Assume we are in text console or xterm */
  827. if (in_photon == -1)
  828. {
  829. int mod_status;
  830. int shift_ext_status;
  831. if (devctl (fileno (stdin), DCMD_CHR_LINESTATUS, &mod_status, sizeof (mod_status), NULL) ==
  832. -1)
  833. return 0;
  834. shift_ext_status = mod_status & 0xffffff00UL;
  835. mod_status &= 0x7f;
  836. if ((mod_status & _LINESTATUS_CON_ALT) != 0)
  837. result |= KEY_M_ALT;
  838. if ((mod_status & _LINESTATUS_CON_CTRL) != 0)
  839. result |= KEY_M_CTRL;
  840. if ((mod_status & _LINESTATUS_CON_SHIFT) != 0 || (shift_ext_status & 0x00000800UL) != 0)
  841. result |= KEY_M_SHIFT;
  842. }
  843. else
  844. {
  845. PhCursorInfo_t cursor_info;
  846. (*ph_query_cursor) (ph_ig, &cursor_info);
  847. if ((cursor_info.key_mods & 0x04) != 0)
  848. result |= KEY_M_ALT;
  849. if ((cursor_info.key_mods & 0x02) != 0)
  850. result |= KEY_M_CTRL;
  851. if ((cursor_info.key_mods & 0x01) != 0)
  852. result |= KEY_M_SHIFT;
  853. }
  854. #endif /* __QNXNTO__ */
  855. #if defined __linux__ || (defined __CYGWIN__ && defined TIOCLINUX)
  856. {
  857. unsigned char modifiers = 6;
  858. if (ioctl (0, TIOCLINUX, &modifiers) < 0)
  859. return 0;
  860. /* Translate Linux modifiers into mc modifiers */
  861. if ((modifiers & SHIFT_PRESSED) != 0)
  862. result |= KEY_M_SHIFT;
  863. if ((modifiers & (ALTL_PRESSED | ALTR_PRESSED)) != 0)
  864. result |= KEY_M_ALT;
  865. if ((modifiers & CONTROL_PRESSED) != 0)
  866. result |= KEY_M_CTRL;
  867. }
  868. #endif /* !__linux__ */
  869. return result;
  870. }
  871. /* --------------------------------------------------------------------------------------------- */
  872. static gboolean
  873. push_char (int c)
  874. {
  875. gboolean ret = FALSE;
  876. if (seq_append == NULL)
  877. seq_append = seq_buffer;
  878. if (seq_append != &(seq_buffer[SEQ_BUFFER_LEN - 2]))
  879. {
  880. *(seq_append++) = c;
  881. *seq_append = '\0';
  882. ret = TRUE;
  883. }
  884. return ret;
  885. }
  886. /* --------------------------------------------------------------------------------------------- */
  887. /* Apply corrections for the keycode generated in get_key_code() */
  888. static int
  889. correct_key_code (int code)
  890. {
  891. unsigned int c = code & ~KEY_M_MASK; /* code without modifier */
  892. unsigned int mod = code & KEY_M_MASK; /* modifier */
  893. #ifdef __QNXNTO__
  894. unsigned int qmod; /* bunch of the QNX console
  895. modifiers needs unchanged */
  896. #endif /* __QNXNTO__ */
  897. /*
  898. * Add key modifiers directly from X11 or OS.
  899. * Ordinary characters only get modifiers from sequences.
  900. */
  901. if (c < 32 || c >= 256)
  902. mod |= get_modifier ();
  903. /* This is needed if the newline is reported as carriage return */
  904. if (c == '\r')
  905. c = '\n';
  906. /* This is reported to be useful on AIX */
  907. if (c == KEY_SCANCEL)
  908. c = '\t';
  909. /* Convert Back Tab to Shift+Tab */
  910. if (c == KEY_BTAB)
  911. {
  912. c = '\t';
  913. mod = KEY_M_SHIFT;
  914. }
  915. /* F0 is the same as F10 for out purposes */
  916. if (c == KEY_F (0))
  917. c = KEY_F (10);
  918. /*
  919. * We are not interested if Ctrl was pressed when entering control
  920. * characters, so assume that it was. When checking for such keys,
  921. * XCTRL macro should be used. In some cases, we are interested,
  922. * e.g. to distinguish Ctrl-Enter from Enter.
  923. */
  924. if (c == '\b')
  925. {
  926. /* Special case for backspase ('\b' < 32) */
  927. c = KEY_BACKSPACE;
  928. mod &= ~KEY_M_CTRL;
  929. }
  930. else if (c < 32 && c != ESC_CHAR && c != '\t' && c != '\n')
  931. mod |= KEY_M_CTRL;
  932. #ifdef __QNXNTO__
  933. qmod = get_modifier ();
  934. if (c == 127 && mod == 0)
  935. {
  936. /* Add Ctrl/Alt/Shift-BackSpace */
  937. mod |= get_modifier ();
  938. c = KEY_BACKSPACE;
  939. }
  940. if (c == '0' && mod == 0 && (qmod & KEY_M_SHIFT) == KEY_M_SHIFT)
  941. {
  942. /* Add Shift-Insert on key pad */
  943. mod = KEY_M_SHIFT;
  944. c = KEY_IC;
  945. }
  946. if (c == '.' && mod == 0 && (qmod & KEY_M_SHIFT) == KEY_M_SHIFT)
  947. {
  948. /* Add Shift-Del on key pad */
  949. mod = KEY_M_SHIFT;
  950. c = KEY_DC;
  951. }
  952. #endif /* __QNXNTO__ */
  953. /* Unrecognized 0177 is delete (preserve Ctrl) */
  954. if (c == 0177)
  955. c = KEY_BACKSPACE;
  956. #if 0
  957. /* Unrecognized Ctrl-d is delete */
  958. if (c == 'd' & 31)
  959. {
  960. c = KEY_DC;
  961. mod &= ~KEY_M_CTRL;
  962. }
  963. /* Unrecognized Ctrl-h is backspace */
  964. if (c == 'h' & 31)
  965. {
  966. c = KEY_BACKSPACE;
  967. mod &= ~KEY_M_CTRL;
  968. }
  969. #endif
  970. /* Shift+BackSpace is backspace */
  971. if (c == KEY_BACKSPACE && (mod & KEY_M_SHIFT) != 0)
  972. mod &= ~KEY_M_SHIFT;
  973. /* Convert Shift+Fn to F(n+10) */
  974. if (c >= KEY_F (1) && c <= KEY_F (10) && (mod & KEY_M_SHIFT) != 0)
  975. c += 10;
  976. /* Remove Shift information from function keys */
  977. if (c >= KEY_F (1) && c <= KEY_F (20))
  978. mod &= ~KEY_M_SHIFT;
  979. if (!mc_global.tty.alternate_plus_minus)
  980. switch (c)
  981. {
  982. case KEY_KP_ADD:
  983. c = '+';
  984. break;
  985. case KEY_KP_SUBTRACT:
  986. c = '-';
  987. break;
  988. case KEY_KP_MULTIPLY:
  989. c = '*';
  990. break;
  991. default:
  992. break;
  993. }
  994. return (mod | c);
  995. }
  996. /* --------------------------------------------------------------------------------------------- */
  997. static int
  998. getch_with_timeout (unsigned int delay_us)
  999. {
  1000. fd_set Read_FD_Set;
  1001. int c;
  1002. struct timeval time_out;
  1003. time_out.tv_sec = delay_us / G_USEC_PER_SEC;
  1004. time_out.tv_usec = delay_us % G_USEC_PER_SEC;
  1005. tty_nodelay (TRUE);
  1006. FD_ZERO (&Read_FD_Set);
  1007. FD_SET (input_fd, &Read_FD_Set);
  1008. select (input_fd + 1, &Read_FD_Set, NULL, NULL, &time_out);
  1009. c = tty_lowlevel_getch ();
  1010. tty_nodelay (FALSE);
  1011. return c;
  1012. }
  1013. /* --------------------------------------------------------------------------------------------- */
  1014. static void
  1015. learn_store_key (GString *buffer, int c)
  1016. {
  1017. if (c == ESC_CHAR)
  1018. g_string_append (buffer, "\\e");
  1019. else if (c < ' ')
  1020. {
  1021. g_string_append_c (buffer, '^');
  1022. g_string_append_c (buffer, c + 'a' - 1);
  1023. }
  1024. else if (c == '^')
  1025. g_string_append (buffer, "^^");
  1026. else
  1027. g_string_append_c (buffer, (char) c);
  1028. }
  1029. /* --------------------------------------------------------------------------------------------- */
  1030. static void
  1031. k_dispose (key_def *k)
  1032. {
  1033. if (k != NULL)
  1034. {
  1035. k_dispose (k->child);
  1036. k_dispose (k->next);
  1037. g_free (k);
  1038. }
  1039. }
  1040. /* --------------------------------------------------------------------------------------------- */
  1041. static int
  1042. key_code_comparator_by_name (const void *p1, const void *p2)
  1043. {
  1044. const key_code_name_t *n1 = *(const key_code_name_t * const *) p1;
  1045. const key_code_name_t *n2 = *(const key_code_name_t * const *) p2;
  1046. return g_ascii_strcasecmp (n1->name, n2->name);
  1047. }
  1048. /* --------------------------------------------------------------------------------------------- */
  1049. static int
  1050. key_code_comparator_by_code (const void *p1, const void *p2)
  1051. {
  1052. const key_code_name_t *n1 = *(const key_code_name_t * const *) p1;
  1053. const key_code_name_t *n2 = *(const key_code_name_t * const *) p2;
  1054. return n1->code - n2->code;
  1055. }
  1056. /* --------------------------------------------------------------------------------------------- */
  1057. static inline void
  1058. sort_key_conv_tab (enum KeySortType type_sort)
  1059. {
  1060. if (has_been_sorted != type_sort)
  1061. {
  1062. size_t i;
  1063. for (i = 0; i < key_conv_tab_size; i++)
  1064. key_conv_tab_sorted[i] = &key_name_conv_tab[i];
  1065. if (type_sort == KEY_SORTBYNAME)
  1066. qsort (key_conv_tab_sorted, key_conv_tab_size, sizeof (key_conv_tab_sorted[0]),
  1067. &key_code_comparator_by_name);
  1068. else if (type_sort == KEY_SORTBYCODE)
  1069. qsort (key_conv_tab_sorted, key_conv_tab_size, sizeof (key_conv_tab_sorted[0]),
  1070. &key_code_comparator_by_code);
  1071. has_been_sorted = type_sort;
  1072. }
  1073. }
  1074. /* --------------------------------------------------------------------------------------------- */
  1075. static int
  1076. lookup_keyname (const char *name, int *idx)
  1077. {
  1078. if (name[0] != '\0')
  1079. {
  1080. const key_code_name_t key = { 0, name, NULL, NULL };
  1081. const key_code_name_t *keyp = &key;
  1082. const key_code_name_t **res;
  1083. if (name[1] == '\0')
  1084. {
  1085. *idx = -1;
  1086. return (int) name[0];
  1087. }
  1088. sort_key_conv_tab (KEY_SORTBYNAME);
  1089. res = bsearch (&keyp, key_conv_tab_sorted, key_conv_tab_size,
  1090. sizeof (key_conv_tab_sorted[0]), key_code_comparator_by_name);
  1091. if (res != NULL)
  1092. {
  1093. *idx = (int) (res - key_conv_tab_sorted);
  1094. return (*res)->code;
  1095. }
  1096. }
  1097. *idx = -1;
  1098. return 0;
  1099. }
  1100. /* --------------------------------------------------------------------------------------------- */
  1101. static gboolean
  1102. lookup_keycode (const long code, int *idx)
  1103. {
  1104. if (code != 0)
  1105. {
  1106. const key_code_name_t key = { code, NULL, NULL, NULL };
  1107. const key_code_name_t *keyp = &key;
  1108. const key_code_name_t **res;
  1109. sort_key_conv_tab (KEY_SORTBYCODE);
  1110. res = bsearch (&keyp, key_conv_tab_sorted, key_conv_tab_size,
  1111. sizeof (key_conv_tab_sorted[0]), key_code_comparator_by_code);
  1112. if (res != NULL)
  1113. {
  1114. *idx = (int) (res - key_conv_tab_sorted);
  1115. return TRUE;
  1116. }
  1117. }
  1118. *idx = -1;
  1119. return FALSE;
  1120. }
  1121. /* --------------------------------------------------------------------------------------------- */
  1122. /*** public functions ****************************************************************************/
  1123. /* --------------------------------------------------------------------------------------------- */
  1124. /* This has to be called before init_slang or whatever routine
  1125. calls any define_sequence */
  1126. void
  1127. init_key (void)
  1128. {
  1129. const char *term;
  1130. term = getenv ("TERM");
  1131. /* This has to be the first define_sequence */
  1132. /* So, we can assume that the first keys member has ESC */
  1133. define_sequences (mc_default_keys);
  1134. /* Terminfo on irix does not have some keys */
  1135. if (mc_global.tty.xterm_flag
  1136. || (term != NULL
  1137. && (strncmp (term, "iris-ansi", 9) == 0
  1138. || strncmp (term, "xterm", 5) == 0
  1139. || strncmp (term, "rxvt", 4) == 0 || strncmp (term, "screen", 6) == 0)))
  1140. define_sequences (xterm_key_defines);
  1141. /* load some additional keys (e.g. direct Alt-? support) */
  1142. load_xtra_key_defines ();
  1143. #ifdef __QNX__
  1144. if ((term != NULL) && (strncmp (term, "qnx", 3) == 0))
  1145. {
  1146. /* Modify the default value of use_8th_bit_as_meta: we would
  1147. * like to provide a working mc for a newbie who knows nothing
  1148. * about [Options|Display bits|Full 8 bits input]...
  1149. *
  1150. * Don't use 'meta'-bit, when we are dealing with a
  1151. * 'qnx*'-type terminal: clear the default value!
  1152. * These terminal types use 0xFF as an escape character,
  1153. * so use_8th_bit_as_meta==1 must not be enabled!
  1154. *
  1155. * [mc-4.1.21+,slint.c/getch(): the DEC_8BIT_HACK stuff
  1156. * is not used now (doesn't even depend on use_8th_bit_as_meta
  1157. * as in mc-3.1.2)...GREAT!...no additional code is required!]
  1158. */
  1159. use_8th_bit_as_meta = FALSE;
  1160. }
  1161. #endif /* __QNX__ */
  1162. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  1163. init_key_x11 ();
  1164. #endif
  1165. /* Load the qansi-m key definitions
  1166. if we are running under the qansi-m terminal */
  1167. if (term != NULL && (strncmp (term, "qansi-m", 7) == 0))
  1168. define_sequences (qansi_key_defines);
  1169. }
  1170. /* --------------------------------------------------------------------------------------------- */
  1171. /**
  1172. * This has to be called after SLang_init_tty/slint_init
  1173. */
  1174. void
  1175. init_key_input_fd (void)
  1176. {
  1177. #ifdef HAVE_SLANG
  1178. input_fd = SLang_TT_Read_FD;
  1179. #endif
  1180. }
  1181. /* --------------------------------------------------------------------------------------------- */
  1182. void
  1183. done_key (void)
  1184. {
  1185. k_dispose (keys);
  1186. g_slist_free_full (select_list, g_free);
  1187. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  1188. if (x11_display)
  1189. mc_XCloseDisplay (x11_display);
  1190. #endif
  1191. }
  1192. /* --------------------------------------------------------------------------------------------- */
  1193. void
  1194. add_select_channel (int fd, select_fn callback, void *info)
  1195. {
  1196. select_t *new;
  1197. new = g_new (select_t, 1);
  1198. new->fd = fd;
  1199. new->callback = callback;
  1200. new->info = info;
  1201. select_list = g_slist_prepend (select_list, new);
  1202. }
  1203. /* --------------------------------------------------------------------------------------------- */
  1204. void
  1205. delete_select_channel (int fd)
  1206. {
  1207. GSList *p;
  1208. p = g_slist_find_custom (select_list, GINT_TO_POINTER (fd), select_cmp_by_fd);
  1209. if (p != NULL)
  1210. select_list = g_slist_delete_link (select_list, p);
  1211. }
  1212. /* --------------------------------------------------------------------------------------------- */
  1213. void
  1214. channels_up (void)
  1215. {
  1216. if (disabled_channels == 0)
  1217. fputs ("Error: channels_up called with disabled_channels = 0\n", stderr);
  1218. disabled_channels--;
  1219. }
  1220. /* --------------------------------------------------------------------------------------------- */
  1221. void
  1222. channels_down (void)
  1223. {
  1224. disabled_channels++;
  1225. }
  1226. /* --------------------------------------------------------------------------------------------- */
  1227. /**
  1228. * Return the code associated with the symbolic name keyname
  1229. */
  1230. long
  1231. tty_keyname_to_keycode (const char *name, char **label)
  1232. {
  1233. char **lc_keys, **p;
  1234. char *cname;
  1235. int k = -1;
  1236. int key = 0;
  1237. int lc_index = -1;
  1238. int use_meta = -1;
  1239. int use_ctrl = -1;
  1240. int use_shift = -1;
  1241. if (name == NULL)
  1242. return 0;
  1243. cname = g_strstrip (g_strdup (name));
  1244. lc_keys = g_strsplit_set (cname, "-+ ", -1);
  1245. g_free (cname);
  1246. for (p = lc_keys; p != NULL && *p != NULL; p++)
  1247. {
  1248. if ((*p)[0] != '\0')
  1249. {
  1250. int idx;
  1251. key = lookup_keyname (g_strstrip (*p), &idx);
  1252. if (key == KEY_M_ALT)
  1253. use_meta = idx;
  1254. else if (key == KEY_M_CTRL)
  1255. use_ctrl = idx;
  1256. else if (key == KEY_M_SHIFT)
  1257. use_shift = idx;
  1258. else
  1259. {
  1260. k = key;
  1261. lc_index = idx;
  1262. break;
  1263. }
  1264. }
  1265. }
  1266. g_strfreev (lc_keys);
  1267. /* output */
  1268. if (k <= 0)
  1269. return 0;
  1270. if (label != NULL)
  1271. {
  1272. GString *s;
  1273. s = g_string_new ("");
  1274. if (use_meta != -1)
  1275. {
  1276. g_string_append (s, key_conv_tab_sorted[use_meta]->shortcut);
  1277. g_string_append_c (s, '-');
  1278. }
  1279. if (use_ctrl != -1)
  1280. {
  1281. g_string_append (s, key_conv_tab_sorted[use_ctrl]->shortcut);
  1282. g_string_append_c (s, '-');
  1283. }
  1284. if (use_shift != -1)
  1285. {
  1286. if (k < 127)
  1287. g_string_append_c (s, (gchar) g_ascii_toupper ((gchar) k));
  1288. else
  1289. {
  1290. g_string_append (s, key_conv_tab_sorted[use_shift]->shortcut);
  1291. g_string_append_c (s, '-');
  1292. g_string_append (s, key_conv_tab_sorted[lc_index]->shortcut);
  1293. }
  1294. }
  1295. else if (k < 128)
  1296. {
  1297. if ((k >= 'A') || (lc_index < 0) || (key_conv_tab_sorted[lc_index]->shortcut == NULL))
  1298. g_string_append_c (s, (gchar) g_ascii_tolower ((gchar) k));
  1299. else
  1300. g_string_append (s, key_conv_tab_sorted[lc_index]->shortcut);
  1301. }
  1302. else if ((lc_index != -1) && (key_conv_tab_sorted[lc_index]->shortcut != NULL))
  1303. g_string_append (s, key_conv_tab_sorted[lc_index]->shortcut);
  1304. else
  1305. g_string_append_c (s, (gchar) g_ascii_tolower ((gchar) key));
  1306. *label = g_string_free (s, FALSE);
  1307. }
  1308. if (use_shift != -1)
  1309. {
  1310. if (k < 127 && k > 31)
  1311. k = g_ascii_toupper ((gchar) k);
  1312. else
  1313. k |= KEY_M_SHIFT;
  1314. }
  1315. if (use_ctrl != -1)
  1316. {
  1317. if (k < 256)
  1318. k = XCTRL (k);
  1319. else
  1320. k |= KEY_M_CTRL;
  1321. }
  1322. if (use_meta != -1)
  1323. k = ALT (k);
  1324. return (long) k;
  1325. }
  1326. /* --------------------------------------------------------------------------------------------- */
  1327. char *
  1328. tty_keycode_to_keyname (const int keycode)
  1329. {
  1330. /* code without modifier */
  1331. unsigned int k = keycode & ~KEY_M_MASK;
  1332. /* modifier */
  1333. unsigned int mod = keycode & KEY_M_MASK;
  1334. int key_idx = -1;
  1335. GString *s;
  1336. int idx;
  1337. s = g_string_sized_new (8);
  1338. if (lookup_keycode (k, &key_idx) || (k > 0 && k < 256))
  1339. {
  1340. if ((mod & KEY_M_ALT) != 0 && lookup_keycode (KEY_M_ALT, &idx))
  1341. {
  1342. g_string_append (s, key_conv_tab_sorted[idx]->name);
  1343. g_string_append_c (s, '-');
  1344. }
  1345. if ((mod & KEY_M_CTRL) != 0)
  1346. {
  1347. /* non printeble chars like a CTRL-[A..Z] */
  1348. if (k < 32)
  1349. k += 64;
  1350. if (lookup_keycode (KEY_M_CTRL, &idx))
  1351. {
  1352. g_string_append (s, key_conv_tab_sorted[idx]->name);
  1353. g_string_append_c (s, '-');
  1354. }
  1355. }
  1356. if ((mod & KEY_M_SHIFT) != 0)
  1357. {
  1358. if (lookup_keycode (KEY_M_ALT, &idx))
  1359. {
  1360. if (k < 127)
  1361. g_string_append_c (s, (gchar) g_ascii_toupper ((gchar) k));
  1362. else
  1363. {
  1364. g_string_append (s, key_conv_tab_sorted[idx]->name);
  1365. g_string_append_c (s, '-');
  1366. g_string_append (s, key_conv_tab_sorted[key_idx]->name);
  1367. }
  1368. }
  1369. }
  1370. else if (k < 128)
  1371. {
  1372. if ((k >= 'A') || (key_idx < 0) || (key_conv_tab_sorted[key_idx]->name == NULL))
  1373. g_string_append_c (s, (gchar) k);
  1374. else
  1375. g_string_append (s, key_conv_tab_sorted[key_idx]->name);
  1376. }
  1377. else if ((key_idx != -1) && (key_conv_tab_sorted[key_idx]->name != NULL))
  1378. g_string_append (s, key_conv_tab_sorted[key_idx]->name);
  1379. else
  1380. g_string_append_c (s, (gchar) keycode);
  1381. }
  1382. return g_string_free (s, s->len == 0);
  1383. }
  1384. /* --------------------------------------------------------------------------------------------- */
  1385. /**
  1386. * Return TRUE on success, FALSE on error.
  1387. * An error happens if SEQ is a beginning of an existing longer sequence.
  1388. */
  1389. gboolean
  1390. define_sequence (int code, const char *seq, int action)
  1391. {
  1392. key_def *base;
  1393. if (strlen (seq) > SEQ_BUFFER_LEN - 1)
  1394. return FALSE;
  1395. for (base = keys; (base != NULL) && (*seq != '\0');)
  1396. if (*seq == base->ch)
  1397. {
  1398. if (base->child == NULL)
  1399. {
  1400. if (*(seq + 1) != '\0')
  1401. base->child = create_sequence (seq + 1, code, action);
  1402. else
  1403. {
  1404. /* The sequence matches an existing one. */
  1405. base->code = code;
  1406. base->action = action;
  1407. }
  1408. return TRUE;
  1409. }
  1410. base = base->child;
  1411. seq++;
  1412. }
  1413. else
  1414. {
  1415. if (base->next != NULL)
  1416. base = base->next;
  1417. else
  1418. {
  1419. base->next = create_sequence (seq, code, action);
  1420. return TRUE;
  1421. }
  1422. }
  1423. if (*seq == '\0')
  1424. {
  1425. /* Attempt to redefine a sequence with a shorter sequence. */
  1426. return FALSE;
  1427. }
  1428. keys = create_sequence (seq, code, action);
  1429. return TRUE;
  1430. }
  1431. /* --------------------------------------------------------------------------------------------- */
  1432. /**
  1433. * Check if we are idle, i.e. there are no pending keyboard or mouse
  1434. * events. Return 1 is idle, 0 is there are pending events.
  1435. */
  1436. gboolean
  1437. is_idle (void)
  1438. {
  1439. int nfd;
  1440. fd_set select_set;
  1441. struct timeval time_out;
  1442. FD_ZERO (&select_set);
  1443. FD_SET (input_fd, &select_set);
  1444. nfd = MAX (0, input_fd) + 1;
  1445. time_out.tv_sec = 0;
  1446. time_out.tv_usec = 0;
  1447. #ifdef HAVE_LIBGPM
  1448. if (mouse_enabled && use_mouse_p == MOUSE_GPM)
  1449. {
  1450. if (gpm_fd >= 0)
  1451. {
  1452. FD_SET (gpm_fd, &select_set);
  1453. nfd = MAX (nfd, gpm_fd + 1);
  1454. }
  1455. else
  1456. {
  1457. if (mouse_fd >= 0) /* error indicative */
  1458. {
  1459. if (FD_ISSET (mouse_fd, &select_set))
  1460. FD_CLR (mouse_fd, &select_set);
  1461. mouse_fd = gpm_fd;
  1462. }
  1463. /* gpm_fd == -2 means under some X terminal */
  1464. if (gpm_fd == -1)
  1465. {
  1466. mouse_enabled = FALSE;
  1467. use_mouse_p = MOUSE_NONE;
  1468. }
  1469. }
  1470. }
  1471. #endif
  1472. return (select (nfd, &select_set, 0, 0, &time_out) <= 0);
  1473. }
  1474. /* --------------------------------------------------------------------------------------------- */
  1475. int
  1476. get_key_code (int no_delay)
  1477. {
  1478. int c;
  1479. static key_def *this = NULL, *parent;
  1480. static gint64 esc_time = -1;
  1481. static int lastnodelay = -1;
  1482. if (no_delay != lastnodelay)
  1483. {
  1484. this = NULL;
  1485. lastnodelay = no_delay;
  1486. }
  1487. pend_send:
  1488. if (pending_keys != NULL)
  1489. {
  1490. gboolean bad_seq;
  1491. c = *pending_keys++;
  1492. while (c == ESC_CHAR)
  1493. c = ALT (*pending_keys++);
  1494. bad_seq = (*pending_keys != ESC_CHAR && *pending_keys != '\0');
  1495. if (*pending_keys == '\0' || bad_seq)
  1496. pending_keys = seq_append = NULL;
  1497. if (bad_seq)
  1498. {
  1499. /* This is an unknown ESC sequence.
  1500. * To prevent interpreting its tail as a random garbage,
  1501. * eat and discard all buffered and quickly following chars.
  1502. * Small, but non-zero timeout is needed to reconnect
  1503. * escape sequence split up by e.g. a serial line.
  1504. */
  1505. int paranoia = 20;
  1506. while (getch_with_timeout (old_esc_mode_timeout) >= 0 && --paranoia != 0)
  1507. ;
  1508. }
  1509. else
  1510. {
  1511. if (c > 127 && c < 256 && use_8th_bit_as_meta)
  1512. c = ALT (c & 0x7f);
  1513. goto done;
  1514. }
  1515. }
  1516. nodelay_try_again:
  1517. if (no_delay != 0)
  1518. tty_nodelay (TRUE);
  1519. c = tty_lowlevel_getch ();
  1520. #if (defined(USE_NCURSES) || defined(USE_NCURSESW)) && defined(KEY_RESIZE)
  1521. if (c == KEY_RESIZE)
  1522. goto nodelay_try_again;
  1523. #endif
  1524. if (no_delay != 0)
  1525. {
  1526. tty_nodelay (FALSE);
  1527. if (c == -1)
  1528. {
  1529. if (this == NULL || parent == NULL || parent->action != MCKEY_ESCAPE || !old_esc_mode ||
  1530. esc_time == -1 || g_get_monotonic_time () < esc_time + old_esc_mode_timeout)
  1531. return -1;
  1532. this = NULL;
  1533. pending_keys = seq_append = NULL;
  1534. return ESC_CHAR;
  1535. }
  1536. }
  1537. else if (c == -1)
  1538. {
  1539. /* Maybe we got an incomplete match.
  1540. This we do only in delay mode, since otherwise
  1541. tty_lowlevel_getch can return -1 at any time. */
  1542. if (seq_append == NULL)
  1543. {
  1544. this = NULL;
  1545. return -1;
  1546. }
  1547. pending_keys = seq_buffer;
  1548. goto pend_send;
  1549. }
  1550. /* Search the key on the root */
  1551. if (no_delay == 0 || this == NULL)
  1552. {
  1553. this = keys;
  1554. parent = NULL;
  1555. if (c > 127 && c < 256 && use_8th_bit_as_meta)
  1556. {
  1557. c &= 0x7f;
  1558. /* The first sequence defined starts with esc */
  1559. parent = keys;
  1560. this = keys->child;
  1561. }
  1562. }
  1563. while (this != NULL)
  1564. {
  1565. if (c == this->ch)
  1566. {
  1567. if (this->child == NULL)
  1568. {
  1569. /* We got a complete match, return and reset search */
  1570. pending_keys = seq_append = NULL;
  1571. c = this->code;
  1572. goto done;
  1573. }
  1574. /* No match yet, but it may be a prefix for a valid seq */
  1575. if (!push_char (c))
  1576. {
  1577. pending_keys = seq_buffer;
  1578. goto pend_send;
  1579. }
  1580. parent = this;
  1581. this = this->child;
  1582. if (parent->action == MCKEY_ESCAPE && old_esc_mode)
  1583. {
  1584. if (no_delay != 0)
  1585. {
  1586. esc_time = g_get_monotonic_time ();
  1587. goto nodelay_try_again;
  1588. }
  1589. esc_time = -1;
  1590. c = getch_with_timeout (old_esc_mode_timeout);
  1591. if (c != -1)
  1592. continue;
  1593. pending_keys = seq_append = NULL;
  1594. this = NULL;
  1595. return ESC_CHAR;
  1596. }
  1597. if (no_delay != 0)
  1598. goto nodelay_try_again;
  1599. c = tty_lowlevel_getch ();
  1600. continue;
  1601. }
  1602. /* c != this->ch. Try other keys with this prefix */
  1603. if (this->next != NULL)
  1604. {
  1605. this = this->next;
  1606. continue;
  1607. }
  1608. /* No match found. Is it one of our ESC <key> specials? */
  1609. if ((parent != NULL) && (parent->action == MCKEY_ESCAPE))
  1610. {
  1611. /* Convert escape-digits to F-keys */
  1612. if (g_ascii_isdigit (c))
  1613. c = KEY_F (c - '0');
  1614. else if (c == ' ')
  1615. c = ESC_CHAR;
  1616. else
  1617. c = ALT (c);
  1618. pending_keys = seq_append = NULL;
  1619. goto done;
  1620. }
  1621. /* Unknown sequence. Maybe a prefix of a longer one. Save it. */
  1622. push_char (c);
  1623. pending_keys = seq_buffer;
  1624. goto pend_send;
  1625. } /* while (this != NULL) */
  1626. done:
  1627. this = NULL;
  1628. return correct_key_code (c);
  1629. }
  1630. /* --------------------------------------------------------------------------------------------- */
  1631. /* Returns a character read from stdin with appropriate interpretation */
  1632. /* Also takes care of generated mouse events */
  1633. /* Returns EV_MOUSE if it is a mouse event */
  1634. /* Returns EV_NONE if non-blocking or interrupt set and nothing was done */
  1635. int
  1636. tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block)
  1637. {
  1638. int c;
  1639. int flag = 0; /* Return value from select */
  1640. #ifdef HAVE_LIBGPM
  1641. static struct Gpm_Event ev; /* Mouse event */
  1642. #endif
  1643. struct timeval time_out;
  1644. struct timeval *time_addr = NULL;
  1645. static int dirty = 3;
  1646. if ((dirty == 3) || is_idle ())
  1647. {
  1648. mc_refresh ();
  1649. dirty = 1;
  1650. }
  1651. else
  1652. dirty++;
  1653. vfs_timeout_handler ();
  1654. /* Ok, we use (event->x < 0) to signal that the event does not contain
  1655. a suitable position for the mouse, so we can't use show_mouse_pointer
  1656. on it.
  1657. */
  1658. if (event->x > 0)
  1659. {
  1660. show_mouse_pointer (event->x, event->y);
  1661. if (!redo_event)
  1662. event->x = -1;
  1663. }
  1664. /* Repeat if using mouse */
  1665. while (pending_keys == NULL)
  1666. {
  1667. int nfd;
  1668. fd_set select_set;
  1669. FD_ZERO (&select_set);
  1670. FD_SET (input_fd, &select_set);
  1671. nfd = MAX (add_selects (&select_set), MAX (0, input_fd)) + 1;
  1672. #ifdef HAVE_LIBGPM
  1673. if (mouse_enabled && (use_mouse_p == MOUSE_GPM))
  1674. {
  1675. if (gpm_fd >= 0)
  1676. {
  1677. FD_SET (gpm_fd, &select_set);
  1678. nfd = MAX (nfd, gpm_fd + 1);
  1679. }
  1680. else
  1681. {
  1682. if (mouse_fd >= 0) /* error indicative */
  1683. {
  1684. if (FD_ISSET (mouse_fd, &select_set))
  1685. FD_CLR (mouse_fd, &select_set);
  1686. mouse_fd = gpm_fd;
  1687. }
  1688. /* gpm_fd == -2 means under some X terminal */
  1689. if (gpm_fd == -1)
  1690. {
  1691. mouse_enabled = FALSE;
  1692. use_mouse_p = MOUSE_NONE;
  1693. }
  1694. break;
  1695. }
  1696. }
  1697. #endif
  1698. if (redo_event)
  1699. {
  1700. time_out.tv_usec = mou_auto_repeat * MC_USEC_PER_MSEC;
  1701. time_out.tv_sec = 0;
  1702. time_addr = &time_out;
  1703. }
  1704. else
  1705. {
  1706. int seconds;
  1707. seconds = vfs_timeouts ();
  1708. time_addr = NULL;
  1709. if (seconds != 0)
  1710. {
  1711. /* the timeout could be improved and actually be
  1712. * the number of seconds until the next vfs entry
  1713. * timeouts in the stamp list.
  1714. */
  1715. time_out.tv_sec = seconds;
  1716. time_out.tv_usec = 0;
  1717. time_addr = &time_out;
  1718. }
  1719. }
  1720. if (!block || tty_got_winch ())
  1721. {
  1722. time_addr = &time_out;
  1723. time_out.tv_sec = 0;
  1724. time_out.tv_usec = 0;
  1725. }
  1726. tty_enable_interrupt_key ();
  1727. flag = select (nfd, &select_set, NULL, NULL, time_addr);
  1728. tty_disable_interrupt_key ();
  1729. /* select timed out: it could be for any of the following reasons:
  1730. * redo_event -> it was because of the MOU_REPEAT handler
  1731. * !block -> we did not block in the select call
  1732. * else -> 10 second timeout to check the vfs status.
  1733. */
  1734. if (flag == 0)
  1735. {
  1736. if (redo_event)
  1737. return EV_MOUSE;
  1738. if (!block || tty_got_winch ())
  1739. return EV_NONE;
  1740. vfs_timeout_handler ();
  1741. }
  1742. if (flag == -1 && errno == EINTR)
  1743. return EV_NONE;
  1744. check_selects (&select_set);
  1745. if (FD_ISSET (input_fd, &select_set))
  1746. break;
  1747. #ifdef HAVE_LIBGPM
  1748. if (mouse_enabled && use_mouse_p == MOUSE_GPM)
  1749. {
  1750. if (gpm_fd >= 0)
  1751. {
  1752. if (FD_ISSET (gpm_fd, &select_set))
  1753. {
  1754. int status;
  1755. status = Gpm_GetEvent (&ev);
  1756. if (status == 1) /* success */
  1757. {
  1758. Gpm_FitEvent (&ev);
  1759. *event = ev;
  1760. return EV_MOUSE;
  1761. }
  1762. if (status <= 0) /* connection closed; -1 == error */
  1763. {
  1764. if (mouse_fd >= 0 && FD_ISSET (mouse_fd, &select_set))
  1765. FD_CLR (mouse_fd, &select_set);
  1766. disable_mouse ();
  1767. return EV_NONE;
  1768. }
  1769. }
  1770. }
  1771. else
  1772. {
  1773. if (mouse_fd >= 0) /* error indicative */
  1774. {
  1775. if (FD_ISSET (mouse_fd, &select_set))
  1776. FD_CLR (mouse_fd, &select_set);
  1777. mouse_fd = gpm_fd;
  1778. }
  1779. /* gpm_fd == -2 means under some X terminal */
  1780. if (gpm_fd == -1)
  1781. {
  1782. mouse_enabled = FALSE;
  1783. use_mouse_p = MOUSE_NONE;
  1784. }
  1785. break;
  1786. }
  1787. }
  1788. #endif /* !HAVE_LIBGPM */
  1789. }
  1790. #ifndef HAVE_SLANG
  1791. flag = is_wintouched (stdscr);
  1792. untouchwin (stdscr);
  1793. #endif /* !HAVE_SLANG */
  1794. c = block ? getch_with_delay () : get_key_code (1);
  1795. #ifndef HAVE_SLANG
  1796. if (flag > 0)
  1797. tty_touch_screen ();
  1798. #endif /* !HAVE_SLANG */
  1799. if (mouse_enabled && (c == MCKEY_MOUSE
  1800. #ifdef KEY_MOUSE
  1801. || c == KEY_MOUSE
  1802. #endif /* KEY_MOUSE */
  1803. || c == MCKEY_EXTENDED_MOUSE))
  1804. {
  1805. /* Mouse event. See tickets 2956 and 3954 for extended mode detection. */
  1806. gboolean extended = c == MCKEY_EXTENDED_MOUSE;
  1807. #ifdef KEY_MOUSE
  1808. extended = extended || (c == KEY_MOUSE && xmouse_seq == NULL
  1809. && xmouse_extended_seq != NULL);
  1810. #endif /* KEY_MOUSE */
  1811. xmouse_get_event (event, extended);
  1812. c = (event->type != 0) ? EV_MOUSE : EV_NONE;
  1813. }
  1814. else if (c == MCKEY_BRACKETED_PASTING_START)
  1815. {
  1816. bracketed_pasting_in_progress = TRUE;
  1817. c = EV_NONE;
  1818. }
  1819. else if (c == MCKEY_BRACKETED_PASTING_END)
  1820. {
  1821. bracketed_pasting_in_progress = FALSE;
  1822. c = EV_NONE;
  1823. }
  1824. return c;
  1825. }
  1826. /* --------------------------------------------------------------------------------------------- */
  1827. /* Returns a key press, mouse events are discarded */
  1828. int
  1829. tty_getch (void)
  1830. {
  1831. Gpm_Event ev;
  1832. int key;
  1833. ev.x = -1;
  1834. while ((key = tty_get_event (&ev, FALSE, TRUE)) == EV_NONE)
  1835. ;
  1836. return key;
  1837. }
  1838. /* --------------------------------------------------------------------------------------------- */
  1839. char *
  1840. learn_key (void)
  1841. {
  1842. /* LEARN_TIMEOUT in ms */
  1843. #define LEARN_TIMEOUT 200
  1844. fd_set Read_FD_Set;
  1845. gint64 end_time;
  1846. int c;
  1847. GString *buffer;
  1848. buffer = g_string_sized_new (16);
  1849. tty_keypad (FALSE); /* disable interpreting keys by ncurses */
  1850. c = tty_lowlevel_getch ();
  1851. while (c == -1)
  1852. c = tty_lowlevel_getch (); /* Sanity check, should be unnecessary */
  1853. learn_store_key (buffer, c);
  1854. end_time = g_get_monotonic_time () + LEARN_TIMEOUT * MC_USEC_PER_MSEC;
  1855. tty_nodelay (TRUE);
  1856. while (TRUE)
  1857. {
  1858. while ((c = tty_lowlevel_getch ()) == -1)
  1859. {
  1860. gint64 time_out;
  1861. struct timeval tv;
  1862. time_out = end_time - g_get_monotonic_time ();
  1863. if (time_out <= 0)
  1864. break;
  1865. tv.tv_sec = time_out / G_USEC_PER_SEC;
  1866. tv.tv_usec = time_out % G_USEC_PER_SEC;
  1867. FD_ZERO (&Read_FD_Set);
  1868. FD_SET (input_fd, &Read_FD_Set);
  1869. select (input_fd + 1, &Read_FD_Set, NULL, NULL, &tv);
  1870. }
  1871. if (c == -1)
  1872. break;
  1873. learn_store_key (buffer, c);
  1874. }
  1875. tty_keypad (TRUE);
  1876. tty_nodelay (FALSE);
  1877. return g_string_free (buffer, buffer->len == 0);
  1878. #undef LEARN_TIMEOUT
  1879. }
  1880. /* --------------------------------------------------------------------------------------------- */
  1881. /* xterm and linux console only: set keypad to numeric or application
  1882. mode. Only in application keypad mode it's possible to distinguish
  1883. the '+' key and the '+' on the keypad ('*' and '-' ditto) */
  1884. void
  1885. numeric_keypad_mode (void)
  1886. {
  1887. if (mc_global.tty.console_flag != '\0' || mc_global.tty.xterm_flag)
  1888. {
  1889. fputs (ESC_STR ">", stdout);
  1890. fflush (stdout);
  1891. }
  1892. }
  1893. /* --------------------------------------------------------------------------------------------- */
  1894. void
  1895. application_keypad_mode (void)
  1896. {
  1897. if (mc_global.tty.console_flag != '\0' || mc_global.tty.xterm_flag)
  1898. {
  1899. fputs (ESC_STR "=", stdout);
  1900. fflush (stdout);
  1901. }
  1902. }
  1903. /* --------------------------------------------------------------------------------------------- */
  1904. void
  1905. enable_bracketed_paste (void)
  1906. {
  1907. printf (ESC_STR "[?2004h");
  1908. fflush (stdout);
  1909. }
  1910. /* --------------------------------------------------------------------------------------------- */
  1911. void
  1912. disable_bracketed_paste (void)
  1913. {
  1914. printf (ESC_STR "[?2004l");
  1915. fflush (stdout);
  1916. bracketed_pasting_in_progress = FALSE;
  1917. }
  1918. /* --------------------------------------------------------------------------------------------- */