key.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277
  1. /*
  2. Keyboard support routines.
  3. Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  4. 2005, 2006, 2007, 2009, 2010, 2011, 2013
  5. The Free Software Foundation, Inc.
  6. Written by:
  7. Miguel de Icaza, 1994, 1995
  8. Janne Kukonlehto, 1994, 1995
  9. Jakub Jelinek, 1995
  10. Norbert Warmuth, 1997
  11. Denys Vlasenko <vda.linux@googlemail.com>, 2013
  12. Slava Zanko <slavazanko@gmail.com>, 2013
  13. Egmont Koblinger <egmont@gmail.com>, 2013
  14. This file is part of the Midnight Commander.
  15. The Midnight Commander is free software: you can redistribute it
  16. and/or modify it under the terms of the GNU General Public License as
  17. published by the Free Software Foundation, either version 3 of the License,
  18. or (at your option) any later version.
  19. The Midnight Commander is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. GNU General Public License for more details.
  23. You should have received a copy of the GNU General Public License
  24. along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. /** \file key.c
  27. * \brief Source: keyboard support routines
  28. */
  29. #include <config.h>
  30. #include <ctype.h>
  31. #include <errno.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <sys/time.h>
  36. #include <sys/types.h>
  37. #include <unistd.h>
  38. #include "lib/global.h"
  39. #include "lib/vfs/vfs.h"
  40. #include "tty.h"
  41. #include "tty-internal.h" /* mouse_enabled */
  42. #include "mouse.h"
  43. #include "key.h"
  44. #include "lib/widget.h" /* mc_refresh() */
  45. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  46. #include "x11conn.h"
  47. #endif
  48. #ifdef __linux__
  49. #if defined(__GLIBC__) && (__GLIBC__ < 2)
  50. #include <linux/termios.h> /* TIOCLINUX */
  51. #else
  52. #include <termios.h>
  53. #endif
  54. #ifdef HAVE_SYS_IOCTL_H
  55. #include <sys/ioctl.h>
  56. #endif
  57. #endif /* __linux__ */
  58. #ifdef __CYGWIN__
  59. #include <termios.h>
  60. #ifdef HAVE_SYS_IOCTL_H
  61. #include <sys/ioctl.h>
  62. #endif
  63. #endif /* __CYGWIN__ */
  64. #ifdef __QNXNTO__
  65. #include <dlfcn.h>
  66. #include <Ph.h>
  67. #include <sys/dcmd_chr.h>
  68. #endif /* __QNXNTO__ */
  69. /*** global variables ****************************************************************************/
  70. int mou_auto_repeat = 100;
  71. int double_click_speed = 250;
  72. int old_esc_mode = 0;
  73. /* timeout for old_esc_mode in usec */
  74. int old_esc_mode_timeout = 1000000; /* settable via env */
  75. int use_8th_bit_as_meta = 0;
  76. gboolean bracketed_pasting_in_progress = FALSE;
  77. /* This table is a mapping between names and the constants we use
  78. * We use this to allow users to define alternate definitions for
  79. * certain keys that may be missing from the terminal database
  80. */
  81. const key_code_name_t key_name_conv_tab[] = {
  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. {KEY_BACKSPACE, "backspace", N_("Backspace key"), "Backspace"},
  105. {KEY_END, "end", N_("End key"), "End"},
  106. {KEY_UP, "up", N_("Up arrow key"), "Up"},
  107. {KEY_DOWN, "down", N_("Down arrow key"), "Down"},
  108. {KEY_LEFT, "left", N_("Left arrow key"), "Left"},
  109. {KEY_RIGHT, "right", N_("Right arrow key"), "Right"},
  110. {KEY_HOME, "home", N_("Home key"), "Home"},
  111. {KEY_NPAGE, "pgdn", N_("Page Down key"), "PgDn"},
  112. {KEY_PPAGE, "pgup", N_("Page Up key"), "PgUp"},
  113. {KEY_IC, "insert", N_("Insert key"), "Ins"},
  114. {KEY_DC, "delete", N_("Delete key"), "Del"},
  115. {ALT ('\t'), "complete", N_("Completion/M-tab"), "Meta-Tab"},
  116. {KEY_BTAB, "backtab", N_("Back Tabulation S-tab"), "Shift-Tab"},
  117. {KEY_KP_ADD, "kpplus", N_("+ on keypad"), "+"},
  118. {KEY_KP_SUBTRACT, "kpminus", N_("- on keypad"), "-"},
  119. {(int) '/', "kpslash", N_("Slash on keypad"), "/"},
  120. {KEY_KP_MULTIPLY, "kpasterisk", N_("* on keypad"), "*"},
  121. /* From here on, these won't be shown in Learn keys (no space) */
  122. {ESC_CHAR, "escape", N_("Escape key"), "Esc"},
  123. {KEY_LEFT, "kpleft", N_("Left arrow keypad"), "Left"},
  124. {KEY_RIGHT, "kpright", N_("Right arrow keypad"), "Right"},
  125. {KEY_UP, "kpup", N_("Up arrow keypad"), "Up"},
  126. {KEY_DOWN, "kpdown", N_("Down arrow keypad"), "Down"},
  127. {KEY_HOME, "kphome", N_("Home on keypad"), "Home"},
  128. {KEY_END, "kpend", N_("End on keypad"), "End"},
  129. {KEY_NPAGE, "kpnpage", N_("Page Down keypad"), "PgDn"},
  130. {KEY_PPAGE, "kpppage", N_("Page Up keypad"), "PgUp"},
  131. {KEY_IC, "kpinsert", N_("Insert on keypad"), "Ins"},
  132. {KEY_DC, "kpdelete", N_("Delete on keypad"), "Del"},
  133. {(int) '\n', "kpenter", N_("Enter on keypad"), "Enter"},
  134. {KEY_F (21), "f21", N_("Function key 21"), "F21"},
  135. {KEY_F (22), "f22", N_("Function key 22"), "F22"},
  136. {KEY_F (23), "f23", N_("Function key 23"), "F23"},
  137. {KEY_F (24), "f24", N_("Function key 24"), "F24"},
  138. {KEY_A1, "a1", N_("A1 key"), "A1"},
  139. {KEY_C1, "c1", N_("C1 key"), "C1"},
  140. /* Alternative label */
  141. {ESC_CHAR, "esc", N_("Escape key"), "Esc"},
  142. {KEY_BACKSPACE, "bs", N_("Backspace key"), "Bakspace"},
  143. {KEY_IC, "ins", N_("Insert key"), "Ins"},
  144. {KEY_DC, "del", N_("Delete key"), "Del"},
  145. {(int) '+', "plus", N_("Plus"), "+"},
  146. {(int) '-', "minus", N_("Minus"), "-"},
  147. {(int) '*', "asterisk", N_("Asterisk"), "*"},
  148. {(int) '.', "dot", N_("Dot"), "."},
  149. {(int) '<', "lt", N_("Less than"), "<"},
  150. {(int) '>', "gt", N_("Great than"), ">"},
  151. {(int) '=', "equal", N_("Equal"), "="},
  152. {(int) ',', "comma", N_("Comma"), ","},
  153. {(int) '\'', "apostrophe", N_("Apostrophe"), "\'"},
  154. {(int) ':', "colon", N_("Colon"), ":"},
  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. struct timeval *timeptr;
  535. int v;
  536. int maxfdp;
  537. while (1)
  538. {
  539. FD_ZERO (&select_set);
  540. FD_SET (input_fd, &select_set); /* Add stdin */
  541. maxfdp = max (add_selects (&select_set), input_fd);
  542. timeptr = NULL;
  543. if (set_timeout)
  544. {
  545. time_out.tv_sec = 0;
  546. time_out.tv_usec = 100000;
  547. timeptr = &time_out;
  548. }
  549. v = select (maxfdp + 1, &select_set, NULL, NULL, timeptr);
  550. if (v > 0)
  551. {
  552. check_selects (&select_set);
  553. if (FD_ISSET (input_fd, &select_set))
  554. break;
  555. }
  556. }
  557. }
  558. /* --------------------------------------------------------------------------------------------- */
  559. static key_def *
  560. create_sequence (const char *seq, int code, int action)
  561. {
  562. key_def *base, *p, *attach;
  563. for (base = attach = NULL; *seq; seq++)
  564. {
  565. p = g_new (key_def, 1);
  566. if (base == NULL)
  567. base = p;
  568. if (attach != NULL)
  569. attach->child = p;
  570. p->ch = *seq;
  571. p->code = code;
  572. p->child = p->next = NULL;
  573. if (seq[1] == '\0')
  574. p->action = action;
  575. else
  576. p->action = MCKEY_NOACTION;
  577. attach = p;
  578. }
  579. return base;
  580. }
  581. /* --------------------------------------------------------------------------------------------- */
  582. static void
  583. define_sequences (const key_define_t * kd)
  584. {
  585. int i;
  586. for (i = 0; kd[i].code != 0; i++)
  587. define_sequence (kd[i].code, kd[i].seq, kd[i].action);
  588. }
  589. /* --------------------------------------------------------------------------------------------- */
  590. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  591. static void
  592. init_key_x11 (void)
  593. {
  594. if (getenv ("DISPLAY") != NULL && !mc_global.tty.disable_x11)
  595. {
  596. x11_display = mc_XOpenDisplay (0);
  597. if (x11_display != NULL)
  598. x11_window = DefaultRootWindow (x11_display);
  599. }
  600. }
  601. #endif /* HAVE_TEXTMODE_X11_SUPPORT */
  602. /* --------------------------------------------------------------------------------------------- */
  603. /* Workaround for System V Curses vt100 bug */
  604. static int
  605. getch_with_delay (void)
  606. {
  607. int c;
  608. /* This routine could be used on systems without mouse support,
  609. so we need to do the select check :-( */
  610. while (1)
  611. {
  612. if (pending_keys == NULL)
  613. try_channels (0);
  614. /* Try to get a character */
  615. c = get_key_code (0);
  616. if (c != -1)
  617. break;
  618. /* Failed -> wait 0.1 secs and try again */
  619. try_channels (1);
  620. }
  621. /* Success -> return the character */
  622. return c;
  623. }
  624. /* --------------------------------------------------------------------------------------------- */
  625. static void
  626. xmouse_get_event (Gpm_Event * ev, gboolean extended)
  627. {
  628. static struct timeval tv1 = { 0, 0 }; /* Force first click as single */
  629. static struct timeval tv2;
  630. static int clicks = 0;
  631. static int last_btn = 0;
  632. int btn;
  633. /* Decode Xterm mouse information to a GPM style event */
  634. if (!extended)
  635. {
  636. /* Variable btn has following meaning: */
  637. /* 0 = btn1 dn, 1 = btn2 dn, 2 = btn3 dn, 3 = btn up */
  638. btn = tty_lowlevel_getch () - 32;
  639. /* Coordinates are 33-based */
  640. /* Transform them to 1-based */
  641. ev->x = tty_lowlevel_getch () - 32;
  642. ev->y = tty_lowlevel_getch () - 32;
  643. }
  644. else
  645. {
  646. /* SGR 1006 extension (e.g. "\e[<0;12;300M"):
  647. - Numbers are encoded in decimal to make it ASCII-safe
  648. and to overcome the limit of 223 columns/rows.
  649. - Mouse release is encoded by trailing 'm' rather than 'M'
  650. so that the released button can be reported.
  651. - Numbers are no longer offset by 32. */
  652. char c;
  653. btn = ev->x = ev->y = 0;
  654. ev->type = 0; /* In case we return on an invalid sequence */
  655. while ((c = tty_lowlevel_getch ()) != ';')
  656. {
  657. if (c < '0' || c > '9')
  658. return;
  659. btn = 10 * btn + (c - '0');
  660. }
  661. while ((c = tty_lowlevel_getch ()) != ';')
  662. {
  663. if (c < '0' || c > '9')
  664. return;
  665. ev->x = 10 * ev->x + (c - '0');
  666. }
  667. while ((c = tty_lowlevel_getch ()) != 'M' && c != 'm')
  668. {
  669. if (c < '0' || c > '9')
  670. return;
  671. ev->y = 10 * ev->y + (c - '0');
  672. }
  673. /* Legacy mouse protocol doesn't tell which button was released,
  674. conveniently all of mc's widgets are written not to rely on this
  675. information. With the SGR extension the released button becomes
  676. known, but for the sake of simplicity we just ignore it. */
  677. if (c == 'm')
  678. btn = 3;
  679. }
  680. /* There seems to be no way of knowing which button was released */
  681. /* So we assume all the buttons were released */
  682. if (btn == 3)
  683. {
  684. if (last_btn != 0)
  685. {
  686. if ((last_btn & (GPM_B_UP | GPM_B_DOWN)) != 0)
  687. {
  688. /* FIXME: DIRTY HACK */
  689. /* don't generate GPM_UP after mouse wheel */
  690. /* need for menu event handling */
  691. ev->type = 0;
  692. tv1.tv_sec = 0;
  693. tv1.tv_usec = 0;
  694. }
  695. else
  696. {
  697. ev->type = GPM_UP | (GPM_SINGLE << clicks);
  698. GET_TIME (tv1);
  699. }
  700. ev->buttons = 0;
  701. last_btn = 0;
  702. clicks = 0;
  703. }
  704. else
  705. {
  706. /* Bogus event, maybe mouse wheel */
  707. ev->type = 0;
  708. }
  709. }
  710. else
  711. {
  712. if (btn >= 32 && btn <= 34)
  713. {
  714. btn -= 32;
  715. ev->type = GPM_DRAG;
  716. }
  717. else
  718. ev->type = GPM_DOWN;
  719. GET_TIME (tv2);
  720. if (tv1.tv_sec && (DIF_TIME (tv1, tv2) < double_click_speed))
  721. {
  722. clicks++;
  723. clicks %= 3;
  724. }
  725. else
  726. clicks = 0;
  727. switch (btn)
  728. {
  729. case 0:
  730. ev->buttons = GPM_B_LEFT;
  731. break;
  732. case 1:
  733. ev->buttons = GPM_B_MIDDLE;
  734. break;
  735. case 2:
  736. ev->buttons = GPM_B_RIGHT;
  737. break;
  738. case 64:
  739. ev->buttons = GPM_B_UP;
  740. clicks = 0;
  741. break;
  742. case 65:
  743. ev->buttons = GPM_B_DOWN;
  744. clicks = 0;
  745. break;
  746. default:
  747. /* Nothing */
  748. ev->type = 0;
  749. ev->buttons = 0;
  750. break;
  751. }
  752. last_btn = ev->buttons;
  753. }
  754. }
  755. /* --------------------------------------------------------------------------------------------- */
  756. /**
  757. * Get modifier state (shift, alt, ctrl) for the last key pressed.
  758. * We are assuming that the state didn't change since the key press.
  759. * This is only correct if get_modifier() is called very fast after
  760. * the input was received, so that the user didn't release the
  761. * modifier keys yet.
  762. */
  763. static int
  764. get_modifier (void)
  765. {
  766. int result = 0;
  767. #ifdef __QNXNTO__
  768. int mod_status, shift_ext_status;
  769. static int in_photon = 0;
  770. static int ph_ig = 0;
  771. PhCursorInfo_t cursor_info;
  772. #endif /* __QNXNTO__ */
  773. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  774. if (x11_window != 0)
  775. {
  776. Window root, child;
  777. int root_x, root_y;
  778. int win_x, win_y;
  779. unsigned int mask;
  780. mc_XQueryPointer (x11_display, x11_window, &root, &child, &root_x,
  781. &root_y, &win_x, &win_y, &mask);
  782. if (mask & ShiftMask)
  783. result |= KEY_M_SHIFT;
  784. if (mask & ControlMask)
  785. result |= KEY_M_CTRL;
  786. return result;
  787. }
  788. #endif /* HAVE_TEXTMODE_X11_SUPPORT */
  789. #ifdef __QNXNTO__
  790. if (in_photon == 0)
  791. {
  792. /* First time here, let's load Photon library and attach
  793. to Photon */
  794. in_photon = -1;
  795. if (getenv ("PHOTON2_PATH") != NULL)
  796. {
  797. /* QNX 6.x has no support for RTLD_LAZY */
  798. void *ph_handle = dlopen ("/usr/lib/libph.so", RTLD_NOW);
  799. if (ph_handle != NULL)
  800. {
  801. ph_attach = (ph_dv_f) dlsym (ph_handle, "PhAttach");
  802. ph_input_group = (ph_ov_f) dlsym (ph_handle, "PhInputGroup");
  803. ph_query_cursor = (ph_pqc_f) dlsym (ph_handle, "PhQueryCursor");
  804. if ((ph_attach != NULL) && (ph_input_group != NULL) && (ph_query_cursor != NULL))
  805. {
  806. if ((*ph_attach) (0, 0))
  807. { /* Attached */
  808. ph_ig = (*ph_input_group) (0);
  809. in_photon = 1;
  810. }
  811. }
  812. }
  813. }
  814. }
  815. /* We do not have Photon running. Assume we are in text
  816. console or xterm */
  817. if (in_photon == -1)
  818. {
  819. if (devctl (fileno (stdin), DCMD_CHR_LINESTATUS, &mod_status, sizeof (int), NULL) == -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. }
  993. return (mod | c);
  994. }
  995. /* --------------------------------------------------------------------------------------------- */
  996. static int
  997. getch_with_timeout (unsigned int delay_us)
  998. {
  999. fd_set Read_FD_Set;
  1000. int c;
  1001. struct timeval time_out;
  1002. time_out.tv_sec = delay_us / 1000000u;
  1003. time_out.tv_usec = delay_us % 1000000u;
  1004. tty_nodelay (TRUE);
  1005. FD_ZERO (&Read_FD_Set);
  1006. FD_SET (input_fd, &Read_FD_Set);
  1007. select (input_fd + 1, &Read_FD_Set, NULL, NULL, &time_out);
  1008. c = tty_lowlevel_getch ();
  1009. tty_nodelay (FALSE);
  1010. return c;
  1011. }
  1012. /* --------------------------------------------------------------------------------------------- */
  1013. static void
  1014. learn_store_key (char *buffer, char **p, int c)
  1015. {
  1016. if (*p - buffer > 253)
  1017. return;
  1018. if (c == ESC_CHAR)
  1019. {
  1020. *(*p)++ = '\\';
  1021. *(*p)++ = 'e';
  1022. }
  1023. else if (c < ' ')
  1024. {
  1025. *(*p)++ = '^';
  1026. *(*p)++ = c + 'a' - 1;
  1027. }
  1028. else if (c == '^')
  1029. {
  1030. *(*p)++ = '^';
  1031. *(*p)++ = '^';
  1032. }
  1033. else
  1034. *(*p)++ = (char) c;
  1035. }
  1036. /* --------------------------------------------------------------------------------------------- */
  1037. static void
  1038. k_dispose (key_def * k)
  1039. {
  1040. if (k != NULL)
  1041. {
  1042. k_dispose (k->child);
  1043. k_dispose (k->next);
  1044. g_free (k);
  1045. }
  1046. }
  1047. /* --------------------------------------------------------------------------------------------- */
  1048. static void
  1049. s_dispose (SelectList * sel)
  1050. {
  1051. if (sel != NULL)
  1052. {
  1053. s_dispose (sel->next);
  1054. g_free (sel);
  1055. }
  1056. }
  1057. /* --------------------------------------------------------------------------------------------- */
  1058. static int
  1059. key_code_comparator_by_name (const void *p1, const void *p2)
  1060. {
  1061. const key_code_name_t *n1 = *(const key_code_name_t **) p1;
  1062. const key_code_name_t *n2 = *(const key_code_name_t **) p2;
  1063. return g_ascii_strcasecmp (n1->name, n2->name);
  1064. }
  1065. /* --------------------------------------------------------------------------------------------- */
  1066. static int
  1067. key_code_comparator_by_code (const void *p1, const void *p2)
  1068. {
  1069. const key_code_name_t *n1 = *(const key_code_name_t **) p1;
  1070. const key_code_name_t *n2 = *(const key_code_name_t **) p2;
  1071. return n1->code - n2->code;
  1072. }
  1073. /* --------------------------------------------------------------------------------------------- */
  1074. static inline void
  1075. sort_key_conv_tab (enum KeySortType type_sort)
  1076. {
  1077. if (has_been_sorted != type_sort)
  1078. {
  1079. size_t i;
  1080. for (i = 0; i < key_conv_tab_size; i++)
  1081. key_conv_tab_sorted[i] = &key_name_conv_tab[i];
  1082. if (type_sort == KEY_SORTBYNAME)
  1083. {
  1084. qsort (key_conv_tab_sorted, key_conv_tab_size, sizeof (key_conv_tab_sorted[0]),
  1085. &key_code_comparator_by_name);
  1086. }
  1087. else if (type_sort == KEY_SORTBYCODE)
  1088. {
  1089. qsort (key_conv_tab_sorted, key_conv_tab_size, sizeof (key_conv_tab_sorted[0]),
  1090. &key_code_comparator_by_code);
  1091. }
  1092. has_been_sorted = type_sort;
  1093. }
  1094. }
  1095. /* --------------------------------------------------------------------------------------------- */
  1096. static int
  1097. lookup_keyname (const char *name, int *idx)
  1098. {
  1099. if (name[0] != '\0')
  1100. {
  1101. const key_code_name_t key = { 0, name, NULL, NULL };
  1102. const key_code_name_t *keyp = &key;
  1103. key_code_name_t **res;
  1104. if (name[1] == '\0')
  1105. {
  1106. *idx = -1;
  1107. return (int) name[0];
  1108. }
  1109. sort_key_conv_tab (KEY_SORTBYNAME);
  1110. res = bsearch (&keyp, key_conv_tab_sorted, key_conv_tab_size,
  1111. sizeof (key_conv_tab_sorted[0]), key_code_comparator_by_name);
  1112. if (res != NULL)
  1113. {
  1114. *idx = (int) (res - (key_code_name_t **) key_conv_tab_sorted);
  1115. return (*res)->code;
  1116. }
  1117. }
  1118. *idx = -1;
  1119. return 0;
  1120. }
  1121. /* --------------------------------------------------------------------------------------------- */
  1122. static gboolean
  1123. lookup_keycode (const long code, int *idx)
  1124. {
  1125. if (code != 0)
  1126. {
  1127. const key_code_name_t key = { code, NULL, NULL, NULL };
  1128. const key_code_name_t *keyp = &key;
  1129. key_code_name_t **res;
  1130. sort_key_conv_tab (KEY_SORTBYCODE);
  1131. res = bsearch (&keyp, key_conv_tab_sorted, key_conv_tab_size,
  1132. sizeof (key_conv_tab_sorted[0]), key_code_comparator_by_code);
  1133. if (res != NULL)
  1134. {
  1135. *idx = (int) (res - (key_code_name_t **) key_conv_tab_sorted);
  1136. return TRUE;
  1137. }
  1138. }
  1139. *idx = -1;
  1140. return FALSE;
  1141. }
  1142. /* --------------------------------------------------------------------------------------------- */
  1143. /*** public functions ****************************************************************************/
  1144. /* --------------------------------------------------------------------------------------------- */
  1145. /* This has to be called before init_slang or whatever routine
  1146. calls any define_sequence */
  1147. void
  1148. init_key (void)
  1149. {
  1150. const char *term = getenv ("TERM");
  1151. /* This has to be the first define_sequence */
  1152. /* So, we can assume that the first keys member has ESC */
  1153. define_sequences (mc_default_keys);
  1154. /* Terminfo on irix does not have some keys */
  1155. if (mc_global.tty.xterm_flag
  1156. || (term != NULL
  1157. && (strncmp (term, "iris-ansi", 9) == 0
  1158. || strncmp (term, "xterm", 5) == 0
  1159. || strncmp (term, "rxvt", 4) == 0 || strncmp (term, "screen", 6) == 0)))
  1160. define_sequences (xterm_key_defines);
  1161. /* load some additional keys (e.g. direct Alt-? support) */
  1162. load_xtra_key_defines ();
  1163. #ifdef __QNX__
  1164. if ((term != NULL) && (strncmp (term, "qnx", 3) == 0))
  1165. {
  1166. /* Modify the default value of use_8th_bit_as_meta: we would
  1167. * like to provide a working mc for a newbie who knows nothing
  1168. * about [Options|Display bits|Full 8 bits input]...
  1169. *
  1170. * Don't use 'meta'-bit, when we are dealing with a
  1171. * 'qnx*'-type terminal: clear the default value!
  1172. * These terminal types use 0xFF as an escape character,
  1173. * so use_8th_bit_as_meta==1 must not be enabled!
  1174. *
  1175. * [mc-4.1.21+,slint.c/getch(): the DEC_8BIT_HACK stuff
  1176. * is not used now (doesn't even depend on use_8th_bit_as_meta
  1177. * as in mc-3.1.2)...GREAT!...no additional code is required!]
  1178. */
  1179. use_8th_bit_as_meta = 0;
  1180. }
  1181. #endif /* __QNX__ */
  1182. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  1183. init_key_x11 ();
  1184. #endif
  1185. /* Load the qansi-m key definitions
  1186. if we are running under the qansi-m terminal */
  1187. if (term != NULL && (strncmp (term, "qansi-m", 7) == 0))
  1188. define_sequences (qansi_key_defines);
  1189. }
  1190. /* --------------------------------------------------------------------------------------------- */
  1191. /**
  1192. * This has to be called after SLang_init_tty/slint_init
  1193. */
  1194. void
  1195. init_key_input_fd (void)
  1196. {
  1197. #ifdef HAVE_SLANG
  1198. input_fd = SLang_TT_Read_FD;
  1199. #endif
  1200. }
  1201. /* --------------------------------------------------------------------------------------------- */
  1202. void
  1203. done_key (void)
  1204. {
  1205. k_dispose (keys);
  1206. s_dispose (select_list);
  1207. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  1208. if (x11_display)
  1209. mc_XCloseDisplay (x11_display);
  1210. #endif
  1211. }
  1212. /* --------------------------------------------------------------------------------------------- */
  1213. void
  1214. add_select_channel (int fd, select_fn callback, void *info)
  1215. {
  1216. SelectList *new;
  1217. new = g_new (SelectList, 1);
  1218. new->fd = fd;
  1219. new->callback = callback;
  1220. new->info = info;
  1221. new->next = select_list;
  1222. select_list = new;
  1223. }
  1224. /* --------------------------------------------------------------------------------------------- */
  1225. void
  1226. delete_select_channel (int fd)
  1227. {
  1228. SelectList *p = select_list;
  1229. SelectList *p_prev = NULL;
  1230. SelectList *p_next;
  1231. while (p != NULL)
  1232. if (p->fd == fd)
  1233. {
  1234. p_next = p->next;
  1235. if (p_prev != NULL)
  1236. p_prev->next = p_next;
  1237. else
  1238. select_list = p_next;
  1239. g_free (p);
  1240. p = p_next;
  1241. }
  1242. else
  1243. {
  1244. p_prev = p;
  1245. p = p->next;
  1246. }
  1247. }
  1248. /* --------------------------------------------------------------------------------------------- */
  1249. void
  1250. channels_up (void)
  1251. {
  1252. if (disabled_channels == 0)
  1253. fputs ("Error: channels_up called with disabled_channels = 0\n", stderr);
  1254. disabled_channels--;
  1255. }
  1256. /* --------------------------------------------------------------------------------------------- */
  1257. void
  1258. channels_down (void)
  1259. {
  1260. disabled_channels++;
  1261. }
  1262. /* --------------------------------------------------------------------------------------------- */
  1263. /**
  1264. * Return the code associated with the symbolic name keyname
  1265. */
  1266. long
  1267. lookup_key (const char *name, char **label)
  1268. {
  1269. char **lc_keys, **p;
  1270. int k = -1;
  1271. int key = 0;
  1272. int lc_index = -1;
  1273. int use_meta = -1;
  1274. int use_ctrl = -1;
  1275. int use_shift = -1;
  1276. if (name == NULL)
  1277. return 0;
  1278. name = g_strstrip (g_strdup (name));
  1279. lc_keys = g_strsplit_set (name, "-+ ", -1);
  1280. g_free ((char *) name);
  1281. for (p = lc_keys; p != NULL && *p != NULL; p++)
  1282. {
  1283. if ((*p)[0] != '\0')
  1284. {
  1285. int idx;
  1286. key = lookup_keyname (g_strstrip (*p), &idx);
  1287. if (key == KEY_M_ALT)
  1288. use_meta = idx;
  1289. else if (key == KEY_M_CTRL)
  1290. use_ctrl = idx;
  1291. else if (key == KEY_M_SHIFT)
  1292. use_shift = idx;
  1293. else
  1294. {
  1295. k = key;
  1296. lc_index = idx;
  1297. break;
  1298. }
  1299. }
  1300. }
  1301. g_strfreev (lc_keys);
  1302. /* output */
  1303. if (k <= 0)
  1304. return 0;
  1305. if (label != NULL)
  1306. {
  1307. GString *s;
  1308. s = g_string_new ("");
  1309. if (use_meta != -1)
  1310. {
  1311. g_string_append (s, key_conv_tab_sorted[use_meta]->shortcut);
  1312. g_string_append_c (s, '-');
  1313. }
  1314. if (use_ctrl != -1)
  1315. {
  1316. g_string_append (s, key_conv_tab_sorted[use_ctrl]->shortcut);
  1317. g_string_append_c (s, '-');
  1318. }
  1319. if (use_shift != -1)
  1320. {
  1321. if (k < 127)
  1322. g_string_append_c (s, (gchar) g_ascii_toupper ((gchar) k));
  1323. else
  1324. {
  1325. g_string_append (s, key_conv_tab_sorted[use_shift]->shortcut);
  1326. g_string_append_c (s, '-');
  1327. g_string_append (s, key_conv_tab_sorted[lc_index]->shortcut);
  1328. }
  1329. }
  1330. else if (k < 128)
  1331. {
  1332. if ((k >= 'A') || (lc_index < 0) || (key_conv_tab_sorted[lc_index]->shortcut == NULL))
  1333. g_string_append_c (s, (gchar) g_ascii_tolower ((gchar) k));
  1334. else
  1335. g_string_append (s, key_conv_tab_sorted[lc_index]->shortcut);
  1336. }
  1337. else if ((lc_index != -1) && (key_conv_tab_sorted[lc_index]->shortcut != NULL))
  1338. g_string_append (s, key_conv_tab_sorted[lc_index]->shortcut);
  1339. else
  1340. g_string_append_c (s, (gchar) g_ascii_tolower ((gchar) key));
  1341. *label = g_string_free (s, FALSE);
  1342. }
  1343. if (use_shift != -1)
  1344. {
  1345. if (k < 127 && k > 31)
  1346. k = g_ascii_toupper ((gchar) k);
  1347. else
  1348. k |= KEY_M_SHIFT;
  1349. }
  1350. if (use_ctrl != -1)
  1351. {
  1352. if (k < 256)
  1353. k = XCTRL (k);
  1354. else
  1355. k |= KEY_M_CTRL;
  1356. }
  1357. if (use_meta != -1)
  1358. k = ALT (k);
  1359. return (long) k;
  1360. }
  1361. /* --------------------------------------------------------------------------------------------- */
  1362. char *
  1363. lookup_key_by_code (const int keycode)
  1364. {
  1365. /* code without modifier */
  1366. unsigned int k = keycode & ~KEY_M_MASK;
  1367. /* modifier */
  1368. unsigned int mod = keycode & KEY_M_MASK;
  1369. int use_meta = -1;
  1370. int use_ctrl = -1;
  1371. int use_shift = -1;
  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. use_meta = idx;
  1383. g_string_append (s, key_conv_tab_sorted[use_meta]->name);
  1384. g_string_append_c (s, '-');
  1385. }
  1386. }
  1387. if (mod & KEY_M_CTRL)
  1388. {
  1389. /* non printeble chars like a CTRL-[A..Z] */
  1390. if (k < 32)
  1391. k += 64;
  1392. if (lookup_keycode (KEY_M_CTRL, &idx))
  1393. {
  1394. use_ctrl = idx;
  1395. g_string_append (s, key_conv_tab_sorted[use_ctrl]->name);
  1396. g_string_append_c (s, '-');
  1397. }
  1398. }
  1399. if (mod & KEY_M_SHIFT)
  1400. {
  1401. if (lookup_keycode (KEY_M_ALT, &idx))
  1402. {
  1403. use_shift = idx;
  1404. if (k < 127)
  1405. g_string_append_c (s, (gchar) g_ascii_toupper ((gchar) k));
  1406. else
  1407. {
  1408. g_string_append (s, key_conv_tab_sorted[use_shift]->name);
  1409. g_string_append_c (s, '-');
  1410. g_string_append (s, key_conv_tab_sorted[key_idx]->name);
  1411. }
  1412. }
  1413. }
  1414. else if (k < 128)
  1415. {
  1416. if ((k >= 'A') || (key_idx < 0) || (key_conv_tab_sorted[key_idx]->name == NULL))
  1417. g_string_append_c (s, (gchar) k);
  1418. else
  1419. g_string_append (s, key_conv_tab_sorted[key_idx]->name);
  1420. }
  1421. else if ((key_idx != -1) && (key_conv_tab_sorted[key_idx]->name != NULL))
  1422. g_string_append (s, key_conv_tab_sorted[key_idx]->name);
  1423. else
  1424. g_string_append_c (s, (gchar) keycode);
  1425. }
  1426. return g_string_free (s, s->len == 0);
  1427. }
  1428. /* --------------------------------------------------------------------------------------------- */
  1429. /**
  1430. * Return TRUE on success, FALSE on error.
  1431. * An error happens if SEQ is a beginning of an existing longer sequence.
  1432. */
  1433. gboolean
  1434. define_sequence (int code, const char *seq, int action)
  1435. {
  1436. key_def *base;
  1437. if (strlen (seq) > SEQ_BUFFER_LEN - 1)
  1438. return FALSE;
  1439. for (base = keys; (base != NULL) && (*seq != '\0');)
  1440. if (*seq == base->ch)
  1441. {
  1442. if (base->child == 0)
  1443. {
  1444. if (*(seq + 1) != '\0')
  1445. base->child = create_sequence (seq + 1, code, action);
  1446. else
  1447. {
  1448. /* The sequence matches an existing one. */
  1449. base->code = code;
  1450. base->action = action;
  1451. }
  1452. return TRUE;
  1453. }
  1454. base = base->child;
  1455. seq++;
  1456. }
  1457. else
  1458. {
  1459. if (base->next)
  1460. base = base->next;
  1461. else
  1462. {
  1463. base->next = create_sequence (seq, code, action);
  1464. return TRUE;
  1465. }
  1466. }
  1467. if (*seq == '\0')
  1468. {
  1469. /* Attempt to redefine a sequence with a shorter sequence. */
  1470. return FALSE;
  1471. }
  1472. keys = create_sequence (seq, code, action);
  1473. return TRUE;
  1474. }
  1475. /* --------------------------------------------------------------------------------------------- */
  1476. /**
  1477. * Check if we are idle, i.e. there are no pending keyboard or mouse
  1478. * events. Return 1 is idle, 0 is there are pending events.
  1479. */
  1480. gboolean
  1481. is_idle (void)
  1482. {
  1483. int nfd;
  1484. fd_set select_set;
  1485. struct timeval time_out;
  1486. FD_ZERO (&select_set);
  1487. FD_SET (input_fd, &select_set);
  1488. nfd = max (0, input_fd) + 1;
  1489. time_out.tv_sec = 0;
  1490. time_out.tv_usec = 0;
  1491. #ifdef HAVE_LIBGPM
  1492. if (mouse_enabled && use_mouse_p == MOUSE_GPM)
  1493. {
  1494. if (gpm_fd >= 0)
  1495. {
  1496. FD_SET (gpm_fd, &select_set);
  1497. nfd = max (nfd, gpm_fd + 1);
  1498. }
  1499. else
  1500. {
  1501. if (mouse_fd >= 0) /* error indicative */
  1502. {
  1503. if (FD_ISSET (mouse_fd, &select_set))
  1504. FD_CLR (mouse_fd, &select_set);
  1505. mouse_fd = gpm_fd;
  1506. }
  1507. /* gpm_fd == -2 means under some X terminal */
  1508. if (gpm_fd == -1)
  1509. {
  1510. mouse_enabled = FALSE;
  1511. use_mouse_p = MOUSE_NONE;
  1512. }
  1513. }
  1514. }
  1515. #endif
  1516. return (select (nfd, &select_set, 0, 0, &time_out) <= 0);
  1517. }
  1518. /* --------------------------------------------------------------------------------------------- */
  1519. int
  1520. get_key_code (int no_delay)
  1521. {
  1522. int c;
  1523. static key_def *this = NULL, *parent;
  1524. static struct timeval esctime = { -1, -1 };
  1525. static int lastnodelay = -1;
  1526. if (no_delay != lastnodelay)
  1527. {
  1528. this = NULL;
  1529. lastnodelay = no_delay;
  1530. }
  1531. pend_send:
  1532. if (pending_keys != NULL)
  1533. {
  1534. int d;
  1535. gboolean bad_seq;
  1536. d = *pending_keys++;
  1537. while (d == ESC_CHAR)
  1538. d = ALT (*pending_keys++);
  1539. bad_seq = (*pending_keys != ESC_CHAR && *pending_keys != 0);
  1540. if (*pending_keys == '\0' || bad_seq)
  1541. pending_keys = seq_append = NULL;
  1542. if (bad_seq)
  1543. {
  1544. /* This is an unknown ESC sequence.
  1545. * To prevent interpreting its tail as a random garbage,
  1546. * eat and discard all buffered and quickly following chars.
  1547. * Small, but non-zero timeout is needed to reconnect
  1548. * escape sequence split up by e.g. a serial line.
  1549. */
  1550. int paranoia = 20;
  1551. while (getch_with_timeout (old_esc_mode_timeout) >= 0 && --paranoia != 0)
  1552. ;
  1553. goto nodelay_try_again;
  1554. }
  1555. if (d > 127 && d < 256 && use_8th_bit_as_meta)
  1556. d = ALT (d & 0x7f);
  1557. this = NULL;
  1558. return correct_key_code (d);
  1559. }
  1560. nodelay_try_again:
  1561. if (no_delay)
  1562. tty_nodelay (TRUE);
  1563. c = tty_lowlevel_getch ();
  1564. #if (defined(USE_NCURSES) || defined(USE_NCURSESW)) && defined(KEY_RESIZE)
  1565. if (c == KEY_RESIZE)
  1566. goto nodelay_try_again;
  1567. #endif
  1568. if (no_delay)
  1569. {
  1570. tty_nodelay (FALSE);
  1571. if (c == -1)
  1572. {
  1573. if (this != NULL && parent != NULL && parent->action == MCKEY_ESCAPE && old_esc_mode)
  1574. {
  1575. struct timeval current, time_out;
  1576. if (esctime.tv_sec == -1)
  1577. return -1;
  1578. GET_TIME (current);
  1579. time_out.tv_sec = old_esc_mode_timeout / 1000000 + esctime.tv_sec;
  1580. time_out.tv_usec = old_esc_mode_timeout % 1000000 + esctime.tv_usec;
  1581. if (time_out.tv_usec > 1000000)
  1582. {
  1583. time_out.tv_usec -= 1000000;
  1584. time_out.tv_sec++;
  1585. }
  1586. if (current.tv_sec < time_out.tv_sec)
  1587. return -1;
  1588. if (current.tv_sec == time_out.tv_sec && current.tv_usec < time_out.tv_usec)
  1589. return -1;
  1590. this = NULL;
  1591. pending_keys = seq_append = NULL;
  1592. return ESC_CHAR;
  1593. }
  1594. return -1;
  1595. }
  1596. }
  1597. else if (c == -1)
  1598. {
  1599. /* Maybe we got an incomplete match.
  1600. This we do only in delay mode, since otherwise
  1601. tty_lowlevel_getch can return -1 at any time. */
  1602. if (seq_append != NULL)
  1603. {
  1604. pending_keys = seq_buffer;
  1605. goto pend_send;
  1606. }
  1607. this = NULL;
  1608. return -1;
  1609. }
  1610. /* Search the key on the root */
  1611. if (!no_delay || this == NULL)
  1612. {
  1613. this = keys;
  1614. parent = NULL;
  1615. if ((c > 127 && c < 256) && use_8th_bit_as_meta)
  1616. {
  1617. c &= 0x7f;
  1618. /* The first sequence defined starts with esc */
  1619. parent = keys;
  1620. this = keys->child;
  1621. }
  1622. }
  1623. while (this != NULL)
  1624. {
  1625. if (c == this->ch)
  1626. {
  1627. if (this->child == NULL)
  1628. {
  1629. /* We got a complete match, return and reset search */
  1630. int code;
  1631. pending_keys = seq_append = NULL;
  1632. code = this->code;
  1633. this = NULL;
  1634. return correct_key_code (code);
  1635. }
  1636. /* No match yet, but it may be a prefix for a valid seq */
  1637. if (!push_char (c))
  1638. {
  1639. pending_keys = seq_buffer;
  1640. goto pend_send;
  1641. }
  1642. parent = this;
  1643. this = this->child;
  1644. if (parent->action == MCKEY_ESCAPE && old_esc_mode)
  1645. {
  1646. if (no_delay)
  1647. {
  1648. GET_TIME (esctime);
  1649. goto nodelay_try_again;
  1650. }
  1651. esctime.tv_sec = -1;
  1652. c = getch_with_timeout (old_esc_mode_timeout);
  1653. if (c == -1)
  1654. {
  1655. pending_keys = seq_append = NULL;
  1656. this = NULL;
  1657. return ESC_CHAR;
  1658. }
  1659. continue;
  1660. }
  1661. if (no_delay)
  1662. goto nodelay_try_again;
  1663. c = tty_lowlevel_getch ();
  1664. continue;
  1665. }
  1666. /* c != this->ch. Try other keys with this prefix */
  1667. if (this->next != NULL)
  1668. {
  1669. this = this->next;
  1670. continue;
  1671. }
  1672. /* No match found. Is it one of our ESC <key> specials? */
  1673. if ((parent != NULL) && (parent->action == MCKEY_ESCAPE))
  1674. {
  1675. /* Convert escape-digits to F-keys */
  1676. if (g_ascii_isdigit (c))
  1677. c = KEY_F (c - '0');
  1678. else if (c == ' ')
  1679. c = ESC_CHAR;
  1680. else
  1681. c = ALT (c);
  1682. pending_keys = seq_append = NULL;
  1683. this = NULL;
  1684. return correct_key_code (c);
  1685. }
  1686. /* Unknown sequence. Maybe a prefix of a longer one. Save it. */
  1687. push_char (c);
  1688. pending_keys = seq_buffer;
  1689. goto pend_send;
  1690. } /* while (this != NULL) */
  1691. this = NULL;
  1692. return correct_key_code (c);
  1693. }
  1694. /* --------------------------------------------------------------------------------------------- */
  1695. /* Returns a character read from stdin with appropriate interpretation */
  1696. /* Also takes care of generated mouse events */
  1697. /* Returns EV_MOUSE if it is a mouse event */
  1698. /* Returns EV_NONE if non-blocking or interrupt set and nothing was done */
  1699. int
  1700. tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block)
  1701. {
  1702. int c;
  1703. static int flag = 0; /* Return value from select */
  1704. #ifdef HAVE_LIBGPM
  1705. static struct Gpm_Event ev; /* Mouse event */
  1706. #endif
  1707. struct timeval time_out;
  1708. struct timeval *time_addr = NULL;
  1709. static int dirty = 3;
  1710. if ((dirty == 3) || is_idle ())
  1711. {
  1712. mc_refresh ();
  1713. dirty = 1;
  1714. }
  1715. else
  1716. dirty++;
  1717. vfs_timeout_handler ();
  1718. /* Ok, we use (event->x < 0) to signal that the event does not contain
  1719. a suitable position for the mouse, so we can't use show_mouse_pointer
  1720. on it.
  1721. */
  1722. if (event->x > 0)
  1723. {
  1724. show_mouse_pointer (event->x, event->y);
  1725. if (!redo_event)
  1726. event->x = -1;
  1727. }
  1728. /* Repeat if using mouse */
  1729. while (pending_keys == NULL)
  1730. {
  1731. int nfd;
  1732. fd_set select_set;
  1733. FD_ZERO (&select_set);
  1734. FD_SET (input_fd, &select_set);
  1735. nfd = max (add_selects (&select_set), max (0, input_fd)) + 1;
  1736. #ifdef HAVE_LIBGPM
  1737. if (mouse_enabled && (use_mouse_p == MOUSE_GPM))
  1738. {
  1739. if (gpm_fd >= 0)
  1740. {
  1741. FD_SET (gpm_fd, &select_set);
  1742. nfd = max (nfd, gpm_fd + 1);
  1743. }
  1744. else
  1745. {
  1746. if (mouse_fd >= 0) /* error indicative */
  1747. {
  1748. if (FD_ISSET (mouse_fd, &select_set))
  1749. FD_CLR (mouse_fd, &select_set);
  1750. mouse_fd = gpm_fd;
  1751. }
  1752. /* gpm_fd == -2 means under some X terminal */
  1753. if (gpm_fd == -1)
  1754. {
  1755. mouse_enabled = FALSE;
  1756. use_mouse_p = MOUSE_NONE;
  1757. }
  1758. break;
  1759. }
  1760. }
  1761. #endif
  1762. if (redo_event)
  1763. {
  1764. time_out.tv_usec = mou_auto_repeat * 1000;
  1765. time_out.tv_sec = 0;
  1766. time_addr = &time_out;
  1767. }
  1768. else
  1769. {
  1770. int seconds;
  1771. seconds = vfs_timeouts ();
  1772. time_addr = NULL;
  1773. if (seconds != 0)
  1774. {
  1775. /* the timeout could be improved and actually be
  1776. * the number of seconds until the next vfs entry
  1777. * timeouts in the stamp list.
  1778. */
  1779. time_out.tv_sec = seconds;
  1780. time_out.tv_usec = 0;
  1781. time_addr = &time_out;
  1782. }
  1783. }
  1784. if (!block || mc_global.tty.winch_flag != 0)
  1785. {
  1786. time_addr = &time_out;
  1787. time_out.tv_sec = 0;
  1788. time_out.tv_usec = 0;
  1789. }
  1790. tty_enable_interrupt_key ();
  1791. flag = select (nfd, &select_set, NULL, NULL, time_addr);
  1792. tty_disable_interrupt_key ();
  1793. /* select timed out: it could be for any of the following reasons:
  1794. * redo_event -> it was because of the MOU_REPEAT handler
  1795. * !block -> we did not block in the select call
  1796. * else -> 10 second timeout to check the vfs status.
  1797. */
  1798. if (flag == 0)
  1799. {
  1800. if (redo_event)
  1801. return EV_MOUSE;
  1802. if (!block || mc_global.tty.winch_flag != 0)
  1803. return EV_NONE;
  1804. vfs_timeout_handler ();
  1805. }
  1806. if (flag == -1 && errno == EINTR)
  1807. return EV_NONE;
  1808. check_selects (&select_set);
  1809. if (FD_ISSET (input_fd, &select_set))
  1810. break;
  1811. #ifdef HAVE_LIBGPM
  1812. if (mouse_enabled && use_mouse_p == MOUSE_GPM)
  1813. {
  1814. if (gpm_fd >= 0)
  1815. {
  1816. if (FD_ISSET (gpm_fd, &select_set))
  1817. {
  1818. int status;
  1819. status = Gpm_GetEvent (&ev);
  1820. if (status == 1) /* success */
  1821. {
  1822. Gpm_FitEvent (&ev);
  1823. *event = ev;
  1824. return EV_MOUSE;
  1825. }
  1826. else if (status == 0) /* connection closed; -1 == error */
  1827. {
  1828. if (mouse_fd >= 0 && FD_ISSET (mouse_fd, &select_set))
  1829. FD_CLR (mouse_fd, &select_set);
  1830. /* Try to reopen gpm_mouse connection */
  1831. disable_mouse ();
  1832. enable_mouse ();
  1833. }
  1834. }
  1835. }
  1836. else
  1837. {
  1838. if (mouse_fd >= 0) /* error indicative */
  1839. {
  1840. if (FD_ISSET (mouse_fd, &select_set))
  1841. FD_CLR (mouse_fd, &select_set);
  1842. mouse_fd = gpm_fd;
  1843. }
  1844. /* gpm_fd == -2 means under some X terminal */
  1845. if (gpm_fd == -1)
  1846. {
  1847. mouse_enabled = FALSE;
  1848. use_mouse_p = MOUSE_NONE;
  1849. }
  1850. break;
  1851. }
  1852. }
  1853. #endif /* !HAVE_LIBGPM */
  1854. }
  1855. #ifndef HAVE_SLANG
  1856. flag = is_wintouched (stdscr);
  1857. untouchwin (stdscr);
  1858. #endif /* !HAVE_SLANG */
  1859. c = block ? getch_with_delay () : get_key_code (1);
  1860. #ifndef HAVE_SLANG
  1861. if (flag > 0)
  1862. tty_touch_screen ();
  1863. #endif /* !HAVE_SLANG */
  1864. if (mouse_enabled && (c == MCKEY_MOUSE
  1865. #ifdef KEY_MOUSE
  1866. || c == KEY_MOUSE
  1867. #endif /* KEY_MOUSE */
  1868. || c == MCKEY_EXTENDED_MOUSE))
  1869. {
  1870. /* Mouse event */
  1871. xmouse_get_event (event, c == MCKEY_EXTENDED_MOUSE);
  1872. c = (event->type != 0) ? EV_MOUSE : EV_NONE;
  1873. }
  1874. else if (c == MCKEY_BRACKETED_PASTING_START)
  1875. {
  1876. bracketed_pasting_in_progress = TRUE;
  1877. c = EV_NONE;
  1878. }
  1879. else if (c == MCKEY_BRACKETED_PASTING_END)
  1880. {
  1881. bracketed_pasting_in_progress = FALSE;
  1882. c = EV_NONE;
  1883. }
  1884. return c;
  1885. }
  1886. /* --------------------------------------------------------------------------------------------- */
  1887. /* Returns a key press, mouse events are discarded */
  1888. int
  1889. tty_getch (void)
  1890. {
  1891. Gpm_Event ev;
  1892. int key;
  1893. ev.x = -1;
  1894. while ((key = tty_get_event (&ev, FALSE, TRUE)) == EV_NONE);
  1895. return key;
  1896. }
  1897. /* --------------------------------------------------------------------------------------------- */
  1898. char *
  1899. learn_key (void)
  1900. {
  1901. /* LEARN_TIMEOUT in usec */
  1902. #define LEARN_TIMEOUT 200000
  1903. fd_set Read_FD_Set;
  1904. struct timeval endtime;
  1905. struct timeval time_out;
  1906. int c;
  1907. char buffer[256];
  1908. char *p = buffer;
  1909. tty_keypad (FALSE); /* disable intepreting keys by ncurses */
  1910. c = tty_lowlevel_getch ();
  1911. while (c == -1)
  1912. c = tty_lowlevel_getch (); /* Sanity check, should be unnecessary */
  1913. learn_store_key (buffer, &p, c);
  1914. GET_TIME (endtime);
  1915. endtime.tv_usec += LEARN_TIMEOUT;
  1916. if (endtime.tv_usec > 1000000)
  1917. {
  1918. endtime.tv_usec -= 1000000;
  1919. endtime.tv_sec++;
  1920. }
  1921. tty_nodelay (TRUE);
  1922. while (TRUE)
  1923. {
  1924. while ((c = tty_lowlevel_getch ()) == -1)
  1925. {
  1926. GET_TIME (time_out);
  1927. time_out.tv_usec = endtime.tv_usec - time_out.tv_usec;
  1928. if (time_out.tv_usec < 0)
  1929. time_out.tv_sec++;
  1930. time_out.tv_sec = endtime.tv_sec - time_out.tv_sec;
  1931. if (time_out.tv_sec >= 0 && time_out.tv_usec > 0)
  1932. {
  1933. FD_ZERO (&Read_FD_Set);
  1934. FD_SET (input_fd, &Read_FD_Set);
  1935. select (input_fd + 1, &Read_FD_Set, NULL, NULL, &time_out);
  1936. }
  1937. else
  1938. break;
  1939. }
  1940. if (c == -1)
  1941. break;
  1942. learn_store_key (buffer, &p, c);
  1943. }
  1944. tty_keypad (TRUE);
  1945. tty_nodelay (FALSE);
  1946. *p = '\0';
  1947. return g_strdup (buffer);
  1948. #undef LEARN_TIMEOUT
  1949. }
  1950. /* --------------------------------------------------------------------------------------------- */
  1951. /* xterm and linux console only: set keypad to numeric or application
  1952. mode. Only in application keypad mode it's possible to distinguish
  1953. the '+' key and the '+' on the keypad ('*' and '-' ditto) */
  1954. void
  1955. numeric_keypad_mode (void)
  1956. {
  1957. if (mc_global.tty.console_flag != '\0' || mc_global.tty.xterm_flag)
  1958. {
  1959. fputs (ESC_STR ">", stdout);
  1960. fflush (stdout);
  1961. }
  1962. }
  1963. /* --------------------------------------------------------------------------------------------- */
  1964. void
  1965. application_keypad_mode (void)
  1966. {
  1967. if (mc_global.tty.console_flag != '\0' || mc_global.tty.xterm_flag)
  1968. {
  1969. fputs (ESC_STR "=", stdout);
  1970. fflush (stdout);
  1971. }
  1972. }
  1973. /* --------------------------------------------------------------------------------------------- */
  1974. void
  1975. enable_bracketed_paste (void)
  1976. {
  1977. printf (ESC_STR "[?2004h");
  1978. fflush (stdout);
  1979. }
  1980. /* --------------------------------------------------------------------------------------------- */
  1981. void
  1982. disable_bracketed_paste (void)
  1983. {
  1984. printf (ESC_STR "[?2004l");
  1985. fflush (stdout);
  1986. bracketed_pasting_in_progress = FALSE;
  1987. }
  1988. /* --------------------------------------------------------------------------------------------- */