key.c 71 KB

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