key.c 71 KB

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