key.c 70 KB

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