key.c 70 KB

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