key.c 66 KB

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