key.c 70 KB

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