key.c 70 KB

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