key.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  1. /* Keyboard support routines.
  2. Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  3. 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
  4. Written by: 1994, 1995 Miguel de Icaza.
  5. 1994, 1995 Janne Kukonlehto.
  6. 1995 Jakub Jelinek.
  7. 1997 Norbert Warmuth
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  19. /** \file key.c
  20. * \brief Source: keyboard support routines
  21. */
  22. #include <config.h>
  23. #include <ctype.h>
  24. #include <errno.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/time.h>
  29. #include <sys/types.h>
  30. #include <unistd.h>
  31. #include "lib/global.h"
  32. #include "lib/strutil.h" /* str_casecmp */
  33. #include "lib/vfs/mc-vfs/vfs.h"
  34. #ifdef ENABLE_VFS
  35. #include "lib/vfs/mc-vfs/gc.h"
  36. #endif /* ENABLE_VFS */
  37. #include "tty.h"
  38. #include "tty-internal.h" /* mouse_enabled */
  39. #include "mouse.h"
  40. #include "key.h"
  41. #include "win.h" /* xterm_flag */
  42. #include "src/main.h"
  43. #include "src/layout.h" /* winch_flag, mc_refresh() */
  44. #include "src/consaver/cons.saver.h"
  45. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  46. #include "x11conn.h"
  47. #endif
  48. #ifdef __linux__
  49. #if defined(__GLIBC__) && (__GLIBC__ < 2)
  50. # include <linux/termios.h> /* TIOCLINUX */
  51. #else
  52. # include <termios.h>
  53. #endif
  54. #include <sys/ioctl.h>
  55. #endif /* __linux__ */
  56. #ifdef __CYGWIN__
  57. #include <termios.h>
  58. #include <sys/ioctl.h>
  59. #endif /* __CYGWIN__ */
  60. #ifdef __QNXNTO__
  61. #include <dlfcn.h>
  62. #include <Ph.h>
  63. #include <sys/dcmd_chr.h>
  64. #endif /* __QNXNTO__ */
  65. /*** global variables **************************************************/
  66. /* If true, use + and \ keys normally and select/unselect do if M-+ / M-\.
  67. and M-- and keypad + / - */
  68. int alternate_plus_minus = 0;
  69. int mou_auto_repeat = 100;
  70. int double_click_speed = 250;
  71. int old_esc_mode = 0;
  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_KP_ADD, "kpplus", N_("+ on keypad"), "+" },
  113. { KEY_KP_SUBTRACT, "kpminus", N_("- on keypad"), "-" },
  114. { (int) '/', "kpslash", N_("Slash on keypad"), "/" },
  115. { KEY_KP_MULTIPLY, "kpasterisk", N_("* on keypad"), "*" },
  116. /* From here on, these won't be shown in Learn keys (no space) */
  117. { ESC_CHAR, "escape", N_("Escape key"), "Esc" },
  118. { KEY_LEFT, "kpleft", N_("Left arrow keypad"), "Left" },
  119. { KEY_RIGHT, "kpright", N_("Right arrow keypad"), "Right" },
  120. { KEY_UP, "kpup", N_("Up arrow keypad"), "Up" },
  121. { KEY_DOWN, "kpdown", N_("Down arrow keypad"), "Down" },
  122. { KEY_HOME, "kphome", N_("Home on keypad"), "Home" },
  123. { KEY_END, "kpend", N_("End on keypad"), "End" },
  124. { KEY_NPAGE, "kpnpage", N_("Page Down keypad"), "PgDn" },
  125. { KEY_PPAGE, "kpppage", N_("Page Up keypad"), "PgUp" },
  126. { KEY_IC, "kpinsert", N_("Insert on keypad"), "Ins" },
  127. { KEY_DC, "kpdelete", N_("Delete on keypad"), "Del" },
  128. { (int) '\n', "kpenter", N_("Enter on keypad"), "Enter" },
  129. { KEY_F (21), "f21", N_("Function key 21"), "F21" },
  130. { KEY_F (22), "f22", N_("Function key 22"), "F22" },
  131. { KEY_F (23), "f23", N_("Function key 23"), "F23" },
  132. { KEY_F (24), "f24", N_("Function key 24"), "F24" },
  133. /* Alternative label */
  134. { ESC_CHAR, "esc", N_("Escape key"), "Esc" },
  135. { KEY_BACKSPACE, "bs", N_("Backspace key"), "Bakspace" },
  136. { KEY_IC, "ins", N_("Insert key"), "Ins" },
  137. { KEY_DC, "del", N_("Delete key"), "Del" },
  138. { (int) '+', "plus", N_("Plus"), "+" },
  139. { (int) '-', "minus", N_("Minus"), "-" },
  140. { (int) '*', "asterisk", N_("Asterisk"), "*" },
  141. { (int) '.', "dot", N_("Dot"), "." },
  142. { (int) '<', "lt", N_("Less than"), "<" },
  143. { (int) '>', "gt", N_("Great than"), ">" },
  144. { (int) '=', "equal", N_("Equal"), "=" },
  145. { (int) ',', "comma", N_("Comma"), "," },
  146. { (int) '\'', "apostrophe", N_("Apostrophe"), "\'" },
  147. { (int) ':', "colon", N_("Colon"), ":" },
  148. { (int) '!', "exclamation", N_("Exclamation mark"), "!" },
  149. { (int) '?', "question", N_("Question mark"), "?" },
  150. { (int) '&', "ampersand", N_("Ampersand"), "&" },
  151. { (int) '$', "dollar", N_("Dollar sign"), "$" },
  152. { (int) '"', "quota", N_("Quotation mark"), "\"" },
  153. { (int) '^', "caret", N_("Caret"), "^" },
  154. { (int) '~', "tilda", N_("Tilda"), "~" },
  155. { (int) '`', "prime", N_("Prime"), "`" },
  156. { (int) '_', "underline", N_("Underline"), "_" },
  157. { (int) '_', "understrike", N_("Understrike"), "_" },
  158. { (int) '|', "pipe", N_("Pipe"), "|" },
  159. { (int) '\n', "enter", N_("Enter"), "Enter" },
  160. { (int) '\t', "tab", N_("Tab key"), "Tab" },
  161. { (int) ' ', "space", N_("Space key"), "Space" },
  162. { (int) '/', "slash", N_("Slash key"), "/" },
  163. { (int) '\\', "backslash", N_("Backslash key"), "\\" },
  164. { (int) '#', "number", N_("Number sign #"), "#" },
  165. { (int) '#', "hash", N_("Number sign #"), "#" },
  166. /* meta keys */
  167. { KEY_M_CTRL, "control", N_("Ctrl"), "C" },
  168. { KEY_M_CTRL, "ctrl", N_("Ctrl"), "C" },
  169. { KEY_M_ALT, "meta", N_("Alt"), "M" },
  170. { KEY_M_ALT, "alt", N_("Alt"), "M" },
  171. { KEY_M_ALT, "ralt", N_("Alt"), "M" },
  172. { KEY_M_SHIFT, "shift", N_("Shift"), "S" },
  173. { 0, NULL, NULL, NULL }
  174. };
  175. /*** file scope macro definitions **************************************/
  176. #define GET_TIME(tv) (gettimeofday(&tv, (struct timezone *) NULL))
  177. #define DIF_TIME(t1, t2) ((t2.tv_sec - t1.tv_sec) * 1000 + (t2.tv_usec - t1.tv_usec)/1000)
  178. /* The maximum sequence length (32 + null terminator) */
  179. #define SEQ_BUFFER_LEN 33
  180. /*** file scope type declarations **************************************/
  181. /* Linux console keyboard modifiers */
  182. typedef enum {
  183. SHIFT_PRESSED = (1 << 0),
  184. ALTR_PRESSED = (1 << 1),
  185. CONTROL_PRESSED = (1 << 2),
  186. ALTL_PRESSED = (1 << 3)
  187. } mod_pressed_t;
  188. typedef struct key_def {
  189. char ch; /* Holds the matching char code */
  190. int code; /* The code returned, valid if child == NULL */
  191. struct key_def *next;
  192. struct key_def *child; /* sequence continuation */
  193. int action; /* optional action to be done. Now used only
  194. to mark that we are just after the first
  195. Escape */
  196. } key_def;
  197. typedef struct {
  198. int code;
  199. const char *seq;
  200. int action;
  201. } key_define_t;
  202. /* File descriptor monitoring add/remove routines */
  203. typedef struct SelectList {
  204. int fd;
  205. select_fn callback;
  206. void *info;
  207. struct SelectList *next;
  208. } SelectList;
  209. #ifdef __QNXNTO__
  210. typedef int (*ph_dv_f) (void *, void *);
  211. typedef int (*ph_ov_f) (void *);
  212. typedef int (*ph_pqc_f) (unsigned short, PhCursorInfo_t *);
  213. #endif
  214. /*** file scope variables **********************************************/
  215. static key_define_t mc_default_keys[] = {
  216. {ESC_CHAR, ESC_STR, MCKEY_ESCAPE},
  217. {ESC_CHAR, ESC_STR ESC_STR, MCKEY_NOACTION},
  218. {0, NULL, MCKEY_NOACTION},
  219. };
  220. /* Broken terminfo and termcap databases on xterminals */
  221. static key_define_t xterm_key_defines[] = {
  222. {KEY_F (1), ESC_STR "OP", MCKEY_NOACTION},
  223. {KEY_F (2), ESC_STR "OQ", MCKEY_NOACTION},
  224. {KEY_F (3), ESC_STR "OR", MCKEY_NOACTION},
  225. {KEY_F (4), ESC_STR "OS", MCKEY_NOACTION},
  226. {KEY_F (1), ESC_STR "[11~", MCKEY_NOACTION},
  227. {KEY_F (2), ESC_STR "[12~", MCKEY_NOACTION},
  228. {KEY_F (3), ESC_STR "[13~", MCKEY_NOACTION},
  229. {KEY_F (4), ESC_STR "[14~", MCKEY_NOACTION},
  230. {KEY_F (5), ESC_STR "[15~", MCKEY_NOACTION},
  231. {KEY_F (6), ESC_STR "[17~", MCKEY_NOACTION},
  232. {KEY_F (7), ESC_STR "[18~", MCKEY_NOACTION},
  233. {KEY_F (8), ESC_STR "[19~", MCKEY_NOACTION},
  234. {KEY_F (9), ESC_STR "[20~", MCKEY_NOACTION},
  235. {KEY_F (10), ESC_STR "[21~", MCKEY_NOACTION},
  236. /* old xterm Shift-arrows */
  237. {KEY_M_SHIFT | KEY_UP, ESC_STR "O2A", MCKEY_NOACTION},
  238. {KEY_M_SHIFT | KEY_DOWN, ESC_STR "O2B", MCKEY_NOACTION},
  239. {KEY_M_SHIFT | KEY_RIGHT, ESC_STR "O2C", MCKEY_NOACTION},
  240. {KEY_M_SHIFT | KEY_LEFT, ESC_STR "O2D", MCKEY_NOACTION},
  241. /* new xterm Shift-arrows */
  242. {KEY_M_SHIFT | KEY_UP, ESC_STR "[1;2A", MCKEY_NOACTION},
  243. {KEY_M_SHIFT | KEY_DOWN, ESC_STR "[1;2B", MCKEY_NOACTION},
  244. {KEY_M_SHIFT | KEY_RIGHT, ESC_STR "[1;2C", MCKEY_NOACTION},
  245. {KEY_M_SHIFT | KEY_LEFT, ESC_STR "[1;2D", MCKEY_NOACTION},
  246. /* more xterm keys with modifiers */
  247. {KEY_M_CTRL | KEY_PPAGE, ESC_STR "[5;5~", MCKEY_NOACTION},
  248. {KEY_M_CTRL | KEY_NPAGE, ESC_STR "[6;5~", MCKEY_NOACTION},
  249. {KEY_M_CTRL | KEY_IC, ESC_STR "[2;5~", MCKEY_NOACTION},
  250. {KEY_M_CTRL | KEY_DC, ESC_STR "[3;5~", MCKEY_NOACTION},
  251. {KEY_M_CTRL | KEY_HOME, ESC_STR "[1;5H", MCKEY_NOACTION},
  252. {KEY_M_CTRL | KEY_END, ESC_STR "[1;5F", MCKEY_NOACTION},
  253. {KEY_M_SHIFT | KEY_HOME, ESC_STR "[1;2H", MCKEY_NOACTION},
  254. {KEY_M_SHIFT | KEY_END, ESC_STR "[1;2F", MCKEY_NOACTION},
  255. {KEY_M_CTRL | KEY_UP, ESC_STR "[1;5A", MCKEY_NOACTION},
  256. {KEY_M_CTRL | KEY_DOWN, ESC_STR "[1;5B", MCKEY_NOACTION},
  257. {KEY_M_CTRL | KEY_RIGHT, ESC_STR "[1;5C", MCKEY_NOACTION},
  258. {KEY_M_CTRL | KEY_LEFT, ESC_STR "[1;5D", MCKEY_NOACTION},
  259. {KEY_M_SHIFT | KEY_IC, ESC_STR "[2;2~", MCKEY_NOACTION},
  260. {KEY_M_SHIFT | KEY_DC, ESC_STR "[3;2~", MCKEY_NOACTION},
  261. {KEY_M_SHIFT | KEY_M_CTRL | KEY_UP, ESC_STR "[1;6A", MCKEY_NOACTION},
  262. {KEY_M_SHIFT | KEY_M_CTRL | KEY_DOWN, ESC_STR "[1;6B", MCKEY_NOACTION},
  263. {KEY_M_SHIFT | KEY_M_CTRL | KEY_RIGHT, ESC_STR "[1;6C", MCKEY_NOACTION},
  264. {KEY_M_SHIFT | KEY_M_CTRL | KEY_LEFT, ESC_STR "[1;6D", MCKEY_NOACTION},
  265. /* putty */
  266. {KEY_M_SHIFT | KEY_M_CTRL | KEY_UP, ESC_STR "[[1;6A", MCKEY_NOACTION},
  267. {KEY_M_SHIFT | KEY_M_CTRL | KEY_DOWN, ESC_STR "[[1;6B", MCKEY_NOACTION},
  268. {KEY_M_SHIFT | KEY_M_CTRL | KEY_RIGHT, ESC_STR "[[1;6C", MCKEY_NOACTION},
  269. {KEY_M_SHIFT | KEY_M_CTRL | KEY_LEFT, ESC_STR "[[1;6D", MCKEY_NOACTION},
  270. /* putty alt-arrow keys */
  271. /* removed as source esc esc esc trouble */
  272. /*
  273. { KEY_M_ALT | KEY_UP, ESC_STR ESC_STR "OA", MCKEY_NOACTION },
  274. { KEY_M_ALT | KEY_DOWN, ESC_STR ESC_STR "OB", MCKEY_NOACTION },
  275. { KEY_M_ALT | KEY_RIGHT, ESC_STR ESC_STR "OC", MCKEY_NOACTION },
  276. { KEY_M_ALT | KEY_LEFT, ESC_STR ESC_STR "OD", MCKEY_NOACTION },
  277. { KEY_M_ALT | KEY_PPAGE, ESC_STR ESC_STR "[5~", MCKEY_NOACTION },
  278. { KEY_M_ALT | KEY_NPAGE, ESC_STR ESC_STR "[6~", MCKEY_NOACTION },
  279. { KEY_M_ALT | KEY_HOME, ESC_STR ESC_STR "[1~", MCKEY_NOACTION },
  280. { KEY_M_ALT | KEY_END, ESC_STR ESC_STR "[4~", MCKEY_NOACTION },
  281. { KEY_M_CTRL | KEY_M_ALT | KEY_UP, ESC_STR ESC_STR "[1;2A", MCKEY_NOACTION },
  282. { KEY_M_CTRL | KEY_M_ALT | KEY_DOWN, ESC_STR ESC_STR "[1;2B", MCKEY_NOACTION },
  283. { KEY_M_CTRL | KEY_M_ALT | KEY_RIGHT, ESC_STR ESC_STR "[1;2C", MCKEY_NOACTION },
  284. { KEY_M_CTRL | KEY_M_ALT | KEY_LEFT, ESC_STR ESC_STR "[1;2D", MCKEY_NOACTION },
  285. { KEY_M_CTRL | KEY_M_ALT | KEY_PPAGE, ESC_STR ESC_STR "[[5;5~", MCKEY_NOACTION },
  286. { KEY_M_CTRL | KEY_M_ALT | KEY_NPAGE, ESC_STR ESC_STR "[[6;5~", MCKEY_NOACTION },
  287. { KEY_M_CTRL | KEY_M_ALT | KEY_HOME, ESC_STR ESC_STR "[1;5H", MCKEY_NOACTION },
  288. { KEY_M_CTRL | KEY_M_ALT | KEY_END, ESC_STR ESC_STR "[1;5F", MCKEY_NOACTION },
  289. */
  290. /* xterm alt-arrow keys */
  291. {KEY_M_ALT | KEY_UP, ESC_STR "[1;3A", MCKEY_NOACTION},
  292. {KEY_M_ALT | KEY_DOWN, ESC_STR "[1;3B", MCKEY_NOACTION},
  293. {KEY_M_ALT | KEY_RIGHT, ESC_STR "[1;3C", MCKEY_NOACTION},
  294. {KEY_M_ALT | KEY_LEFT, ESC_STR "[1;3D", MCKEY_NOACTION},
  295. {KEY_M_ALT | KEY_PPAGE, ESC_STR "[5;3~", MCKEY_NOACTION},
  296. {KEY_M_ALT | KEY_NPAGE, ESC_STR "[6;3~", MCKEY_NOACTION},
  297. {KEY_M_ALT | KEY_HOME, ESC_STR "[1~", MCKEY_NOACTION},
  298. {KEY_M_ALT | KEY_END, ESC_STR "[4~", MCKEY_NOACTION},
  299. {KEY_M_CTRL | KEY_M_ALT | KEY_UP, ESC_STR "[1;7A", MCKEY_NOACTION},
  300. {KEY_M_CTRL | KEY_M_ALT | KEY_DOWN, ESC_STR "[1;7B", MCKEY_NOACTION},
  301. {KEY_M_CTRL | KEY_M_ALT | KEY_RIGHT, ESC_STR "[1;7C", MCKEY_NOACTION},
  302. {KEY_M_CTRL | KEY_M_ALT | KEY_LEFT, ESC_STR "[1;7D", MCKEY_NOACTION},
  303. {KEY_M_CTRL | KEY_M_ALT | KEY_PPAGE, ESC_STR "[5;7~", MCKEY_NOACTION},
  304. {KEY_M_CTRL | KEY_M_ALT | KEY_NPAGE, ESC_STR "[6;7~", MCKEY_NOACTION},
  305. {KEY_M_CTRL | KEY_M_ALT | KEY_HOME, ESC_STR "OH", MCKEY_NOACTION},
  306. {KEY_M_CTRL | KEY_M_ALT | KEY_END, ESC_STR "OF", MCKEY_NOACTION},
  307. /* rxvt keys with modifiers */
  308. {KEY_M_SHIFT | KEY_UP, ESC_STR "[a", MCKEY_NOACTION},
  309. {KEY_M_SHIFT | KEY_DOWN, ESC_STR "[b", MCKEY_NOACTION},
  310. {KEY_M_SHIFT | KEY_RIGHT, ESC_STR "[c", MCKEY_NOACTION},
  311. {KEY_M_SHIFT | KEY_LEFT, ESC_STR "[d", MCKEY_NOACTION},
  312. {KEY_M_CTRL | KEY_UP, ESC_STR "Oa", MCKEY_NOACTION},
  313. {KEY_M_CTRL | KEY_DOWN, ESC_STR "Ob", MCKEY_NOACTION},
  314. {KEY_M_CTRL | KEY_RIGHT, ESC_STR "Oc", MCKEY_NOACTION},
  315. {KEY_M_CTRL | KEY_LEFT, ESC_STR "Od", MCKEY_NOACTION},
  316. {KEY_M_CTRL | KEY_PPAGE, ESC_STR "[5^", MCKEY_NOACTION},
  317. {KEY_M_CTRL | KEY_NPAGE, ESC_STR "[6^", MCKEY_NOACTION},
  318. {KEY_M_CTRL | KEY_HOME, ESC_STR "[7^", MCKEY_NOACTION},
  319. {KEY_M_CTRL | KEY_END, ESC_STR "[8^", MCKEY_NOACTION},
  320. {KEY_M_SHIFT | KEY_HOME, ESC_STR "[7$", MCKEY_NOACTION},
  321. {KEY_M_SHIFT | KEY_END, ESC_STR "[8$", MCKEY_NOACTION},
  322. {KEY_M_CTRL | KEY_IC, ESC_STR "[2^", MCKEY_NOACTION},
  323. {KEY_M_CTRL | KEY_DC, ESC_STR "[3^", MCKEY_NOACTION},
  324. {KEY_M_SHIFT | KEY_DC, ESC_STR "[3$", MCKEY_NOACTION},
  325. /* konsole keys with modifiers */
  326. {KEY_M_SHIFT | KEY_HOME, ESC_STR "O2H", MCKEY_NOACTION},
  327. {KEY_M_SHIFT | KEY_END, ESC_STR "O2F", MCKEY_NOACTION},
  328. /* gnome-terminal */
  329. {KEY_M_SHIFT | KEY_UP, ESC_STR "[2A", MCKEY_NOACTION},
  330. {KEY_M_SHIFT | KEY_DOWN, ESC_STR "[2B", MCKEY_NOACTION},
  331. {KEY_M_SHIFT | KEY_RIGHT, ESC_STR "[2C", MCKEY_NOACTION},
  332. {KEY_M_SHIFT | KEY_LEFT, ESC_STR "[2D", MCKEY_NOACTION},
  333. {KEY_M_CTRL | KEY_UP, ESC_STR "[5A", MCKEY_NOACTION},
  334. {KEY_M_CTRL | KEY_DOWN, ESC_STR "[5B", MCKEY_NOACTION},
  335. {KEY_M_CTRL | KEY_RIGHT, ESC_STR "[5C", MCKEY_NOACTION},
  336. {KEY_M_CTRL | KEY_LEFT, ESC_STR "[5D", MCKEY_NOACTION},
  337. {KEY_M_SHIFT | KEY_M_CTRL | KEY_UP, ESC_STR "[6A", MCKEY_NOACTION},
  338. {KEY_M_SHIFT | KEY_M_CTRL | KEY_DOWN, ESC_STR "[6B", MCKEY_NOACTION},
  339. {KEY_M_SHIFT | KEY_M_CTRL | KEY_RIGHT, ESC_STR "[6C", MCKEY_NOACTION},
  340. {KEY_M_SHIFT | KEY_M_CTRL | KEY_LEFT, ESC_STR "[6D", MCKEY_NOACTION},
  341. /* gnome-terminal - application mode */
  342. {KEY_M_CTRL | KEY_UP, ESC_STR "O5A", MCKEY_NOACTION},
  343. {KEY_M_CTRL | KEY_DOWN, ESC_STR "O5B", MCKEY_NOACTION},
  344. {KEY_M_CTRL | KEY_RIGHT, ESC_STR "O5C", MCKEY_NOACTION},
  345. {KEY_M_CTRL | KEY_LEFT, ESC_STR "O5D", MCKEY_NOACTION},
  346. {KEY_M_SHIFT | KEY_M_CTRL | KEY_UP, ESC_STR "O6A", MCKEY_NOACTION},
  347. {KEY_M_SHIFT | KEY_M_CTRL | KEY_DOWN, ESC_STR "O6B", MCKEY_NOACTION},
  348. {KEY_M_SHIFT | KEY_M_CTRL | KEY_RIGHT, ESC_STR "O6C", MCKEY_NOACTION},
  349. {KEY_M_SHIFT | KEY_M_CTRL | KEY_LEFT, ESC_STR "O6D", MCKEY_NOACTION},
  350. /* iTerm */
  351. {KEY_M_SHIFT | KEY_PPAGE, ESC_STR "[5;2~", MCKEY_NOACTION},
  352. {KEY_M_SHIFT | KEY_NPAGE, ESC_STR "[6;2~", MCKEY_NOACTION},
  353. /* putty */
  354. {KEY_M_SHIFT | KEY_PPAGE, ESC_STR "[[5;53~", MCKEY_NOACTION},
  355. {KEY_M_SHIFT | KEY_NPAGE, ESC_STR "[[6;53~", MCKEY_NOACTION},
  356. /* keypad keys */
  357. {KEY_IC, ESC_STR "Op", MCKEY_NOACTION},
  358. {KEY_DC, ESC_STR "On", MCKEY_NOACTION},
  359. {'/', ESC_STR "Oo", MCKEY_NOACTION},
  360. {'\n', ESC_STR "OM", MCKEY_NOACTION},
  361. {0, NULL, MCKEY_NOACTION},
  362. };
  363. /* qansi-m terminals have a much more key combinatios,
  364. which are undefined in termcap/terminfo */
  365. static key_define_t qansi_key_defines[] = {
  366. /* qansi-m terminal */
  367. {KEY_M_CTRL | KEY_NPAGE, ESC_STR "[u", MCKEY_NOACTION}, /* Ctrl-PgDown */
  368. {KEY_M_CTRL | KEY_PPAGE, ESC_STR "[v", MCKEY_NOACTION}, /* Ctrl-PgUp */
  369. {KEY_M_CTRL | KEY_HOME, ESC_STR "[h", MCKEY_NOACTION}, /* Ctrl-Home */
  370. {KEY_M_CTRL | KEY_END, ESC_STR "[y", MCKEY_NOACTION}, /* Ctrl-End */
  371. {KEY_M_CTRL | KEY_IC, ESC_STR "[`", MCKEY_NOACTION}, /* Ctrl-Insert */
  372. {KEY_M_CTRL | KEY_DC, ESC_STR "[p", MCKEY_NOACTION}, /* Ctrl-Delete */
  373. {KEY_M_CTRL | KEY_LEFT, ESC_STR "[d", MCKEY_NOACTION}, /* Ctrl-Left */
  374. {KEY_M_CTRL | KEY_RIGHT, ESC_STR "[c", MCKEY_NOACTION}, /* Ctrl-Right */
  375. {KEY_M_CTRL | KEY_DOWN, ESC_STR "[b", MCKEY_NOACTION}, /* Ctrl-Down */
  376. {KEY_M_CTRL | KEY_UP, ESC_STR "[a", MCKEY_NOACTION}, /* Ctrl-Up */
  377. {KEY_M_CTRL | KEY_KP_ADD, ESC_STR "[s", MCKEY_NOACTION}, /* Ctrl-Gr-Plus */
  378. {KEY_M_CTRL | KEY_KP_SUBTRACT, ESC_STR "[t", MCKEY_NOACTION}, /* Ctrl-Gr-Minus */
  379. {KEY_M_CTRL | '\t', ESC_STR "[z", MCKEY_NOACTION}, /* Ctrl-Tab */
  380. {KEY_M_SHIFT | '\t', ESC_STR "[Z", MCKEY_NOACTION}, /* Shift-Tab */
  381. {KEY_M_CTRL | KEY_F (1), ESC_STR "[1~", MCKEY_NOACTION}, /* Ctrl-F1 */
  382. {KEY_M_CTRL | KEY_F (2), ESC_STR "[2~", MCKEY_NOACTION}, /* Ctrl-F2 */
  383. {KEY_M_CTRL | KEY_F (3), ESC_STR "[3~", MCKEY_NOACTION}, /* Ctrl-F3 */
  384. {KEY_M_CTRL | KEY_F (4), ESC_STR "[4~", MCKEY_NOACTION}, /* Ctrl-F4 */
  385. {KEY_M_CTRL | KEY_F (5), ESC_STR "[5~", MCKEY_NOACTION}, /* Ctrl-F5 */
  386. {KEY_M_CTRL | KEY_F (6), ESC_STR "[6~", MCKEY_NOACTION}, /* Ctrl-F6 */
  387. {KEY_M_CTRL | KEY_F (7), ESC_STR "[7~", MCKEY_NOACTION}, /* Ctrl-F7 */
  388. {KEY_M_CTRL | KEY_F (8), ESC_STR "[8~", MCKEY_NOACTION}, /* Ctrl-F8 */
  389. {KEY_M_CTRL | KEY_F (9), ESC_STR "[9~", MCKEY_NOACTION}, /* Ctrl-F9 */
  390. {KEY_M_CTRL | KEY_F (10), ESC_STR "[10~", MCKEY_NOACTION}, /* Ctrl-F10 */
  391. {KEY_M_CTRL | KEY_F (11), ESC_STR "[11~", MCKEY_NOACTION}, /* Ctrl-F11 */
  392. {KEY_M_CTRL | KEY_F (12), ESC_STR "[12~", MCKEY_NOACTION}, /* Ctrl-F12 */
  393. {KEY_M_ALT | KEY_F (1), ESC_STR "[17~", MCKEY_NOACTION}, /* Alt-F1 */
  394. {KEY_M_ALT | KEY_F (2), ESC_STR "[18~", MCKEY_NOACTION}, /* Alt-F2 */
  395. {KEY_M_ALT | KEY_F (3), ESC_STR "[19~", MCKEY_NOACTION}, /* Alt-F3 */
  396. {KEY_M_ALT | KEY_F (4), ESC_STR "[20~", MCKEY_NOACTION}, /* Alt-F4 */
  397. {KEY_M_ALT | KEY_F (5), ESC_STR "[21~", MCKEY_NOACTION}, /* Alt-F5 */
  398. {KEY_M_ALT | KEY_F (6), ESC_STR "[22~", MCKEY_NOACTION}, /* Alt-F6 */
  399. {KEY_M_ALT | KEY_F (7), ESC_STR "[23~", MCKEY_NOACTION}, /* Alt-F7 */
  400. {KEY_M_ALT | KEY_F (8), ESC_STR "[24~", MCKEY_NOACTION}, /* Alt-F8 */
  401. {KEY_M_ALT | KEY_F (9), ESC_STR "[25~", MCKEY_NOACTION}, /* Alt-F9 */
  402. {KEY_M_ALT | KEY_F (10), ESC_STR "[26~", MCKEY_NOACTION}, /* Alt-F10 */
  403. {KEY_M_ALT | KEY_F (11), ESC_STR "[27~", MCKEY_NOACTION}, /* Alt-F11 */
  404. {KEY_M_ALT | KEY_F (12), ESC_STR "[28~", MCKEY_NOACTION}, /* Alt-F12 */
  405. {KEY_M_ALT | 'a', ESC_STR "Na", MCKEY_NOACTION}, /* Alt-a */
  406. {KEY_M_ALT | 'b', ESC_STR "Nb", MCKEY_NOACTION}, /* Alt-b */
  407. {KEY_M_ALT | 'c', ESC_STR "Nc", MCKEY_NOACTION}, /* Alt-c */
  408. {KEY_M_ALT | 'd', ESC_STR "Nd", MCKEY_NOACTION}, /* Alt-d */
  409. {KEY_M_ALT | 'e', ESC_STR "Ne", MCKEY_NOACTION}, /* Alt-e */
  410. {KEY_M_ALT | 'f', ESC_STR "Nf", MCKEY_NOACTION}, /* Alt-f */
  411. {KEY_M_ALT | 'g', ESC_STR "Ng", MCKEY_NOACTION}, /* Alt-g */
  412. {KEY_M_ALT | 'h', ESC_STR "Nh", MCKEY_NOACTION}, /* Alt-h */
  413. {KEY_M_ALT | 'i', ESC_STR "Ni", MCKEY_NOACTION}, /* Alt-i */
  414. {KEY_M_ALT | 'j', ESC_STR "Nj", MCKEY_NOACTION}, /* Alt-j */
  415. {KEY_M_ALT | 'k', ESC_STR "Nk", MCKEY_NOACTION}, /* Alt-k */
  416. {KEY_M_ALT | 'l', ESC_STR "Nl", MCKEY_NOACTION}, /* Alt-l */
  417. {KEY_M_ALT | 'm', ESC_STR "Nm", MCKEY_NOACTION}, /* Alt-m */
  418. {KEY_M_ALT | 'n', ESC_STR "Nn", MCKEY_NOACTION}, /* Alt-n */
  419. {KEY_M_ALT | 'o', ESC_STR "No", MCKEY_NOACTION}, /* Alt-o */
  420. {KEY_M_ALT | 'p', ESC_STR "Np", MCKEY_NOACTION}, /* Alt-p */
  421. {KEY_M_ALT | 'q', ESC_STR "Nq", MCKEY_NOACTION}, /* Alt-q */
  422. {KEY_M_ALT | 'r', ESC_STR "Nr", MCKEY_NOACTION}, /* Alt-r */
  423. {KEY_M_ALT | 's', ESC_STR "Ns", MCKEY_NOACTION}, /* Alt-s */
  424. {KEY_M_ALT | 't', ESC_STR "Nt", MCKEY_NOACTION}, /* Alt-t */
  425. {KEY_M_ALT | 'u', ESC_STR "Nu", MCKEY_NOACTION}, /* Alt-u */
  426. {KEY_M_ALT | 'v', ESC_STR "Nv", MCKEY_NOACTION}, /* Alt-v */
  427. {KEY_M_ALT | 'w', ESC_STR "Nw", MCKEY_NOACTION}, /* Alt-w */
  428. {KEY_M_ALT | 'x', ESC_STR "Nx", MCKEY_NOACTION}, /* Alt-x */
  429. {KEY_M_ALT | 'y', ESC_STR "Ny", MCKEY_NOACTION}, /* Alt-y */
  430. {KEY_M_ALT | 'z', ESC_STR "Nz", MCKEY_NOACTION}, /* Alt-z */
  431. {KEY_KP_SUBTRACT, ESC_STR "[S", MCKEY_NOACTION}, /* Gr-Minus */
  432. {KEY_KP_ADD, ESC_STR "[T", MCKEY_NOACTION}, /* Gr-Plus */
  433. {0, NULL, MCKEY_NOACTION},
  434. };
  435. /* timeout for old_esc_mode in usec */
  436. int old_esc_mode_timeout = 1000000; /* settable via env */
  437. /* This holds all the key definitions */
  438. static key_def *keys = NULL;
  439. static int input_fd;
  440. static int disabled_channels = 0; /* Disable channels checking */
  441. static SelectList *select_list = NULL;
  442. static int seq_buffer[SEQ_BUFFER_LEN];
  443. static int *seq_append = NULL;
  444. static int *pending_keys = NULL;
  445. #ifdef __QNXNTO__
  446. ph_dv_f ph_attach;
  447. ph_ov_f ph_input_group;
  448. ph_pqc_f ph_query_cursor;
  449. #endif
  450. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  451. static Display *x11_display;
  452. static Window x11_window;
  453. #endif /* HAVE_TEXTMODE_X11_SUPPORT */
  454. /*** file scope functions **********************************************/
  455. static int
  456. add_selects (fd_set * select_set)
  457. {
  458. int top_fd = 0;
  459. if (disabled_channels == 0) {
  460. SelectList *p;
  461. for (p = select_list; p != NULL; p = p->next) {
  462. FD_SET (p->fd, select_set);
  463. if (p->fd > top_fd)
  464. top_fd = p->fd;
  465. }
  466. }
  467. return top_fd;
  468. }
  469. static void
  470. check_selects (fd_set * select_set)
  471. {
  472. if (disabled_channels == 0) {
  473. gboolean retry;
  474. do {
  475. SelectList *p;
  476. retry = FALSE;
  477. for (p = select_list; p; p = p->next)
  478. if (FD_ISSET (p->fd, select_set)) {
  479. FD_CLR (p->fd, select_set);
  480. (*p->callback) (p->fd, p->info);
  481. retry = TRUE;
  482. break;
  483. }
  484. } while (retry);
  485. }
  486. }
  487. /* If set timeout is set, then we wait 0.1 seconds, else, we block */
  488. static void
  489. try_channels (int set_timeout)
  490. {
  491. struct timeval time_out;
  492. static fd_set select_set;
  493. struct timeval *timeptr;
  494. int v;
  495. int maxfdp;
  496. while (1) {
  497. FD_ZERO (&select_set);
  498. FD_SET (input_fd, &select_set); /* Add stdin */
  499. maxfdp = max (add_selects (&select_set), input_fd);
  500. timeptr = NULL;
  501. if (set_timeout) {
  502. time_out.tv_sec = 0;
  503. time_out.tv_usec = 100000;
  504. timeptr = &time_out;
  505. }
  506. v = select (maxfdp + 1, &select_set, NULL, NULL, timeptr);
  507. if (v > 0) {
  508. check_selects (&select_set);
  509. if (FD_ISSET (input_fd, &select_set))
  510. break;
  511. }
  512. }
  513. }
  514. static key_def *
  515. create_sequence (const char *seq, int code, int action)
  516. {
  517. key_def *base, *p, *attach;
  518. for (base = attach = NULL; *seq; seq++) {
  519. p = g_new (key_def, 1);
  520. if (base == NULL)
  521. base = p;
  522. if (attach != NULL)
  523. attach->child = p;
  524. p->ch = *seq;
  525. p->code = code;
  526. p->child = p->next = NULL;
  527. if (seq[1] == '\0')
  528. p->action = action;
  529. else
  530. p->action = MCKEY_NOACTION;
  531. attach = p;
  532. }
  533. return base;
  534. }
  535. static void
  536. define_sequences (const key_define_t * kd)
  537. {
  538. int i;
  539. for (i = 0; kd[i].code != 0; i++)
  540. define_sequence (kd[i].code, kd[i].seq, kd[i].action);
  541. }
  542. static void
  543. init_key_x11 (void)
  544. {
  545. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  546. if (getenv ("DISPLAY") != NULL) {
  547. x11_display = mc_XOpenDisplay (0);
  548. if (x11_display != NULL)
  549. x11_window = DefaultRootWindow (x11_display);
  550. }
  551. #endif /* HAVE_TEXTMODE_X11_SUPPORT */
  552. }
  553. /* Workaround for System V Curses vt100 bug */
  554. static int
  555. getch_with_delay (void)
  556. {
  557. int c;
  558. /* This routine could be used on systems without mouse support,
  559. so we need to do the select check :-( */
  560. while (1) {
  561. if (pending_keys == NULL)
  562. try_channels (0);
  563. /* Try to get a character */
  564. c = get_key_code (0);
  565. if (c != -1)
  566. break;
  567. /* Failed -> wait 0.1 secs and try again */
  568. try_channels (1);
  569. }
  570. /* Success -> return the character */
  571. return c;
  572. }
  573. static void
  574. xmouse_get_event (Gpm_Event * ev)
  575. {
  576. int btn;
  577. static struct timeval tv1 = { 0, 0 }; /* Force first click as single */
  578. static struct timeval tv2;
  579. static int clicks = 0;
  580. static int last_btn = 0;
  581. /* Decode Xterm mouse information to a GPM style event */
  582. /* Variable btn has following meaning: */
  583. /* 0 = btn1 dn, 1 = btn2 dn, 2 = btn3 dn, 3 = btn up */
  584. btn = tty_lowlevel_getch () - 32;
  585. /* There seems to be no way of knowing which button was released */
  586. /* So we assume all the buttons were released */
  587. if (btn == 3) {
  588. if (last_btn != 0) {
  589. if ((last_btn & (GPM_B_UP | GPM_B_DOWN)) != 0) {
  590. /* FIXME: DIRTY HACK */
  591. /* don't generate GPM_UP after mouse wheel */
  592. /* need for menu event handling */
  593. ev->type = 0;
  594. tv1.tv_sec = 0;
  595. tv1.tv_usec = 0;
  596. } else {
  597. ev->type = GPM_UP | (GPM_SINGLE << clicks);
  598. GET_TIME (tv1);
  599. }
  600. ev->buttons = 0;
  601. last_btn = 0;
  602. clicks = 0;
  603. } else {
  604. /* Bogus event, maybe mouse wheel */
  605. ev->type = 0;
  606. }
  607. } else {
  608. if (btn >= 32 && btn <= 34) {
  609. btn -= 32;
  610. ev->type = GPM_DRAG;
  611. } else
  612. ev->type = GPM_DOWN;
  613. GET_TIME (tv2);
  614. if (tv1.tv_sec && (DIF_TIME (tv1, tv2) < double_click_speed)) {
  615. clicks++;
  616. clicks %= 3;
  617. } else
  618. clicks = 0;
  619. switch (btn) {
  620. case 0:
  621. ev->buttons = GPM_B_LEFT;
  622. break;
  623. case 1:
  624. ev->buttons = GPM_B_MIDDLE;
  625. break;
  626. case 2:
  627. ev->buttons = GPM_B_RIGHT;
  628. break;
  629. case 64:
  630. ev->buttons = GPM_B_UP;
  631. clicks = 0;
  632. break;
  633. case 65:
  634. ev->buttons = GPM_B_DOWN;
  635. clicks = 0;
  636. break;
  637. default:
  638. /* Nothing */
  639. ev->type = 0;
  640. ev->buttons = 0;
  641. break;
  642. }
  643. last_btn = ev->buttons;
  644. }
  645. /* Coordinates are 33-based */
  646. /* Transform them to 1-based */
  647. ev->x = tty_lowlevel_getch () - 32;
  648. ev->y = tty_lowlevel_getch () - 32;
  649. }
  650. /*
  651. * Get modifier state (shift, alt, ctrl) for the last key pressed.
  652. * We are assuming that the state didn't change since the key press.
  653. * This is only correct if get_modifier() is called very fast after
  654. * the input was received, so that the user didn't release the
  655. * modifier keys yet.
  656. */
  657. static int
  658. get_modifier (void)
  659. {
  660. int result = 0;
  661. #ifdef __QNXNTO__
  662. int mod_status, shift_ext_status;
  663. static int in_photon = 0;
  664. static int ph_ig = 0;
  665. PhCursorInfo_t cursor_info;
  666. #endif /* __QNXNTO__ */
  667. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  668. if (x11_window != 0) {
  669. Window root, child;
  670. int root_x, root_y;
  671. int win_x, win_y;
  672. unsigned int mask;
  673. mc_XQueryPointer (x11_display, x11_window, &root, &child, &root_x,
  674. &root_y, &win_x, &win_y, &mask);
  675. if (mask & ShiftMask)
  676. result |= KEY_M_SHIFT;
  677. if (mask & ControlMask)
  678. result |= KEY_M_CTRL;
  679. return result;
  680. }
  681. #endif /* HAVE_TEXTMODE_X11_SUPPORT */
  682. #ifdef __QNXNTO__
  683. if (in_photon == 0) {
  684. /* First time here, let's load Photon library and attach
  685. to Photon */
  686. in_photon = -1;
  687. if (getenv ("PHOTON2_PATH") != NULL) {
  688. /* QNX 6.x has no support for RTLD_LAZY */
  689. void *ph_handle = dlopen ("/usr/lib/libph.so", RTLD_NOW);
  690. if (ph_handle != NULL) {
  691. ph_attach = (ph_dv_f) dlsym (ph_handle, "PhAttach");
  692. ph_input_group = (ph_ov_f) dlsym (ph_handle, "PhInputGroup");
  693. ph_query_cursor = (ph_pqc_f) dlsym (ph_handle, "PhQueryCursor");
  694. if ((ph_attach != NULL) && (ph_input_group != NULL)
  695. && (ph_query_cursor != NULL)) {
  696. if ((*ph_attach) (0, 0)) { /* Attached */
  697. ph_ig = (*ph_input_group) (0);
  698. in_photon = 1;
  699. }
  700. }
  701. }
  702. }
  703. }
  704. /* We do not have Photon running. Assume we are in text
  705. console or xterm */
  706. if (in_photon == -1) {
  707. if (devctl (fileno (stdin), DCMD_CHR_LINESTATUS, &mod_status, sizeof (int), NULL) == -1)
  708. return 0;
  709. shift_ext_status = mod_status & 0xffffff00UL;
  710. mod_status &= 0x7f;
  711. if (mod_status & _LINESTATUS_CON_ALT)
  712. result |= KEY_M_ALT;
  713. if (mod_status & _LINESTATUS_CON_CTRL)
  714. result |= KEY_M_CTRL;
  715. if ((mod_status & _LINESTATUS_CON_SHIFT)
  716. || (shift_ext_status & 0x00000800UL))
  717. result |= KEY_M_SHIFT;
  718. } else {
  719. (*ph_query_cursor) (ph_ig, &cursor_info);
  720. if (cursor_info.key_mods & 0x04)
  721. result |= KEY_M_ALT;
  722. if (cursor_info.key_mods & 0x02)
  723. result |= KEY_M_CTRL;
  724. if (cursor_info.key_mods & 0x01)
  725. result |= KEY_M_SHIFT;
  726. }
  727. #endif /* __QNXNTO__ */
  728. #if defined __linux__ || (defined __CYGWIN__ && defined TIOCLINUX)
  729. {
  730. unsigned char modifiers = 6;
  731. if (ioctl (0, TIOCLINUX, &modifiers) < 0)
  732. return 0;
  733. /* Translate Linux modifiers into mc modifiers */
  734. if (modifiers & SHIFT_PRESSED)
  735. result |= KEY_M_SHIFT;
  736. if (modifiers & (ALTL_PRESSED | ALTR_PRESSED))
  737. result |= KEY_M_ALT;
  738. if (modifiers & CONTROL_PRESSED)
  739. result |= KEY_M_CTRL;
  740. }
  741. #endif /* !__linux__ */
  742. return result;
  743. }
  744. static gboolean
  745. push_char (int c)
  746. {
  747. gboolean ret = FALSE;
  748. if (seq_append == NULL)
  749. seq_append = seq_buffer;
  750. if (seq_append != &(seq_buffer[SEQ_BUFFER_LEN - 2])) {
  751. *(seq_append++) = c;
  752. *seq_append = 0;
  753. ret = TRUE;
  754. }
  755. return ret;
  756. }
  757. /* Apply corrections for the keycode generated in get_key_code() */
  758. static int
  759. correct_key_code (int code)
  760. {
  761. unsigned int c = code & ~KEY_M_MASK; /* code without modifier */
  762. unsigned int mod = code & KEY_M_MASK; /* modifier */
  763. #ifdef __QNXNTO__
  764. unsigned int qmod; /* bunch of the QNX console
  765. modifiers needs unchanged */
  766. #endif /* __QNXNTO__ */
  767. /*
  768. * Add key modifiers directly from X11 or OS.
  769. * Ordinary characters only get modifiers from sequences.
  770. */
  771. if (c < 32 || c >= 256) {
  772. mod |= get_modifier ();
  773. }
  774. /* This is needed if the newline is reported as carriage return */
  775. if (c == '\r')
  776. c = '\n';
  777. /* This is reported to be useful on AIX */
  778. if (c == KEY_SCANCEL)
  779. c = '\t';
  780. /* Convert Shift+Tab and Ctrl+Tab to Back Tab */
  781. if ((c == '\t') && (mod & (KEY_M_SHIFT | KEY_M_CTRL))) {
  782. c = KEY_BTAB;
  783. mod = 0;
  784. }
  785. /* F0 is the same as F10 for out purposes */
  786. if (c == KEY_F (0))
  787. c = KEY_F (10);
  788. /*
  789. * We are not interested if Ctrl was pressed when entering control
  790. * characters, so assume that it was. When checking for such keys,
  791. * XCTRL macro should be used. In some cases, we are interested,
  792. * e.g. to distinguish Ctrl-Enter from Enter.
  793. */
  794. if (c < 32 && c != ESC_CHAR && c != '\t' && c != '\n') {
  795. mod |= KEY_M_CTRL;
  796. }
  797. #ifdef __QNXNTO__
  798. qmod = get_modifier ();
  799. if ((c == 127) && (mod == 0)) { /* Add Ctrl/Alt/Shift-BackSpace */
  800. mod |= get_modifier ();
  801. c = KEY_BACKSPACE;
  802. }
  803. if ((c == '0') && (mod == 0)) { /* Add Shift-Insert on key pad */
  804. if ((qmod & KEY_M_SHIFT) == KEY_M_SHIFT) {
  805. mod = KEY_M_SHIFT;
  806. c = KEY_IC;
  807. }
  808. }
  809. if ((c == '.') && (mod == 0)) { /* Add Shift-Del on key pad */
  810. if ((qmod & KEY_M_SHIFT) == KEY_M_SHIFT) {
  811. mod = KEY_M_SHIFT;
  812. c = KEY_DC;
  813. }
  814. }
  815. #endif /* __QNXNTO__ */
  816. /* Unrecognized 0177 is delete (preserve Ctrl) */
  817. if (c == 0177) {
  818. c = KEY_BACKSPACE;
  819. }
  820. /* Unrecognized Ctrl-d is delete */
  821. if (c == (31 & 'd')) {
  822. c = KEY_DC;
  823. mod &= ~KEY_M_CTRL;
  824. }
  825. /* Unrecognized Ctrl-h is backspace */
  826. if (c == (31 & 'h')) {
  827. c = KEY_BACKSPACE;
  828. mod &= ~KEY_M_CTRL;
  829. }
  830. /* Shift+BackSpace is backspace */
  831. if (c == KEY_BACKSPACE && (mod & KEY_M_SHIFT)) {
  832. mod &= ~KEY_M_SHIFT;
  833. }
  834. /* Convert Shift+Fn to F(n+10) */
  835. if (c >= KEY_F (1) && c <= KEY_F (10) && (mod & KEY_M_SHIFT)) {
  836. c += 10;
  837. }
  838. /* Remove Shift information from function keys */
  839. if (c >= KEY_F (1) && c <= KEY_F (20)) {
  840. mod &= ~KEY_M_SHIFT;
  841. }
  842. if (!alternate_plus_minus)
  843. switch (c) {
  844. case KEY_KP_ADD:
  845. c = '+';
  846. break;
  847. case KEY_KP_SUBTRACT:
  848. c = '-';
  849. break;
  850. case KEY_KP_MULTIPLY:
  851. c = '*';
  852. break;
  853. }
  854. return (mod | c);
  855. }
  856. static int
  857. xgetch_second (void)
  858. {
  859. fd_set Read_FD_Set;
  860. int c;
  861. struct timeval time_out;
  862. time_out.tv_sec = old_esc_mode_timeout / 1000000;
  863. time_out.tv_usec = old_esc_mode_timeout % 1000000;
  864. tty_nodelay (TRUE);
  865. FD_ZERO (&Read_FD_Set);
  866. FD_SET (input_fd, &Read_FD_Set);
  867. select (input_fd + 1, &Read_FD_Set, NULL, NULL, &time_out);
  868. c = tty_lowlevel_getch ();
  869. tty_nodelay (FALSE);
  870. return c;
  871. }
  872. static void
  873. learn_store_key (char *buffer, char **p, int c)
  874. {
  875. if (*p - buffer > 253)
  876. return;
  877. if (c == ESC_CHAR) {
  878. *(*p)++ = '\\';
  879. *(*p)++ = 'e';
  880. } else if (c < ' ') {
  881. *(*p)++ = '^';
  882. *(*p)++ = c + 'a' - 1;
  883. } else if (c == '^') {
  884. *(*p)++ = '^';
  885. *(*p)++ = '^';
  886. } else
  887. *(*p)++ = (char) c;
  888. }
  889. static void
  890. k_dispose (key_def * k)
  891. {
  892. if (k != NULL) {
  893. k_dispose (k->child);
  894. k_dispose (k->next);
  895. g_free (k);
  896. }
  897. }
  898. static void
  899. s_dispose (SelectList * sel)
  900. {
  901. if (sel != NULL) {
  902. s_dispose (sel->next);
  903. g_free (sel);
  904. }
  905. }
  906. /*** public functions **************************************************/
  907. /* This has to be called before init_slang or whatever routine
  908. calls any define_sequence */
  909. void
  910. init_key (void)
  911. {
  912. const char *term = getenv ("TERM");
  913. /* This has to be the first define_sequence */
  914. /* So, we can assume that the first keys member has ESC */
  915. define_sequences (mc_default_keys);
  916. /* Terminfo on irix does not have some keys */
  917. if (xterm_flag
  918. || (term != NULL
  919. && (strncmp (term, "iris-ansi", 9) == 0
  920. || strncmp (term, "xterm", 5) == 0
  921. || strncmp (term, "rxvt", 4) == 0 || strcmp (term, "screen") == 0)))
  922. define_sequences (xterm_key_defines);
  923. /* load some additional keys (e.g. direct Alt-? support) */
  924. load_xtra_key_defines ();
  925. #ifdef __QNX__
  926. if ((term != NULL) && (strncmp (term, "qnx", 3) == 0)) {
  927. /* Modify the default value of use_8th_bit_as_meta: we would
  928. * like to provide a working mc for a newbie who knows nothing
  929. * about [Options|Display bits|Full 8 bits input]...
  930. *
  931. * Don't use 'meta'-bit, when we are dealing with a
  932. * 'qnx*'-type terminal: clear the default value!
  933. * These terminal types use 0xFF as an escape character,
  934. * so use_8th_bit_as_meta==1 must not be enabled!
  935. *
  936. * [mc-4.1.21+,slint.c/getch(): the DEC_8BIT_HACK stuff
  937. * is not used now (doesn't even depend on use_8th_bit_as_meta
  938. * as in mc-3.1.2)...GREAT!...no additional code is required!]
  939. */
  940. use_8th_bit_as_meta = 0;
  941. }
  942. #endif /* __QNX__ */
  943. init_key_x11 ();
  944. /* Load the qansi-m key definitions
  945. if we are running under the qansi-m terminal */
  946. if (term != NULL && (strncmp (term, "qansi-m", 7) == 0))
  947. define_sequences (qansi_key_defines);
  948. }
  949. /* This has to be called after SLang_init_tty/slint_init */
  950. void
  951. init_key_input_fd (void)
  952. {
  953. #ifdef HAVE_SLANG
  954. input_fd = SLang_TT_Read_FD;
  955. #endif
  956. }
  957. void
  958. done_key (void)
  959. {
  960. k_dispose (keys);
  961. s_dispose (select_list);
  962. #ifdef HAVE_TEXTMODE_X11_SUPPORT
  963. if (x11_display)
  964. mc_XCloseDisplay (x11_display);
  965. #endif
  966. }
  967. void
  968. add_select_channel (int fd, select_fn callback, void *info)
  969. {
  970. SelectList *new;
  971. new = g_new (SelectList, 1);
  972. new->fd = fd;
  973. new->callback = callback;
  974. new->info = info;
  975. new->next = select_list;
  976. select_list = new;
  977. }
  978. void
  979. delete_select_channel (int fd)
  980. {
  981. SelectList *p = select_list;
  982. SelectList *p_prev = NULL;
  983. SelectList *p_next;
  984. while (p != NULL)
  985. if (p->fd == fd) {
  986. p_next = p->next;
  987. if (p_prev != NULL)
  988. p_prev->next = p_next;
  989. else
  990. select_list = p_next;
  991. g_free (p);
  992. p = p_next;
  993. } else {
  994. p_prev = p;
  995. p = p->next;
  996. }
  997. }
  998. void
  999. channels_up (void)
  1000. {
  1001. if (disabled_channels == 0)
  1002. fputs ("Error: channels_up called with disabled_channels = 0\n", stderr);
  1003. disabled_channels--;
  1004. }
  1005. void
  1006. channels_down (void)
  1007. {
  1008. disabled_channels++;
  1009. }
  1010. /*
  1011. * Common handler for standard movement keys in a text area. Provided
  1012. * functions are called with the "data" argument. backfn and forfn also
  1013. * get an argument indicating how many lines to scroll. Return MSG_HANDLED
  1014. * if the key was handled, MSG_NOT_HANDLED otherwise.
  1015. */
  1016. cb_ret_t
  1017. check_movement_keys (int key, int page_size, void *data, move_fn backfn,
  1018. move_fn forfn, move_fn topfn, move_fn bottomfn)
  1019. {
  1020. switch (key) {
  1021. case KEY_UP:
  1022. case XCTRL ('p'):
  1023. (*backfn) (data, 1);
  1024. break;
  1025. case KEY_DOWN:
  1026. case XCTRL ('n'):
  1027. (*forfn) (data, 1);
  1028. break;
  1029. case KEY_PPAGE:
  1030. case ALT ('v'):
  1031. (*backfn) (data, page_size - 1);
  1032. break;
  1033. case KEY_NPAGE:
  1034. case XCTRL ('v'):
  1035. (*forfn) (data, page_size - 1);
  1036. break;
  1037. case KEY_HOME:
  1038. case KEY_M_CTRL | KEY_HOME:
  1039. case KEY_M_CTRL | KEY_PPAGE:
  1040. case KEY_A1:
  1041. case ALT ('<'):
  1042. (*topfn) (data, 0);
  1043. break;
  1044. case KEY_END:
  1045. case KEY_M_CTRL | KEY_END:
  1046. case KEY_M_CTRL | KEY_NPAGE:
  1047. case KEY_C1:
  1048. case ALT ('>'):
  1049. (*bottomfn) (data, 0);
  1050. break;
  1051. case 'b':
  1052. case KEY_BACKSPACE:
  1053. (*backfn) (data, page_size - 1);
  1054. break;
  1055. case ' ':
  1056. (*forfn) (data, page_size - 1);
  1057. break;
  1058. case 'u':
  1059. (*backfn) (data, page_size / 2);
  1060. break;
  1061. case 'd':
  1062. (*forfn) (data, page_size / 2);
  1063. break;
  1064. case 'g':
  1065. (*topfn) (data, 0);
  1066. break;
  1067. case 'G':
  1068. (*bottomfn) (data, 0);
  1069. break;
  1070. default:
  1071. return MSG_NOT_HANDLED;
  1072. }
  1073. return MSG_HANDLED;
  1074. }
  1075. static const size_t key_name_conv_tab_size = sizeof (key_name_conv_tab) /
  1076. sizeof (key_name_conv_tab[0]) - 1;
  1077. static const key_code_name_t *key_name_conv_tab_sorted[sizeof (key_name_conv_tab) /
  1078. sizeof (key_name_conv_tab[0]) - 1];
  1079. static int
  1080. key_code_name_comparator (const void *p1, const void *p2)
  1081. {
  1082. const key_code_name_t *n1 = *(const key_code_name_t **) p1;
  1083. const key_code_name_t *n2 = *(const key_code_name_t **) p2;
  1084. return str_casecmp (n1->name, n2->name);
  1085. }
  1086. static inline void
  1087. sort_key_name_conv_tab (void)
  1088. {
  1089. static gboolean has_been_sorted = FALSE;
  1090. if (!has_been_sorted) {
  1091. size_t i;
  1092. for (i = 0; i < key_name_conv_tab_size; i++)
  1093. key_name_conv_tab_sorted[i] = &key_name_conv_tab[i];
  1094. qsort (key_name_conv_tab_sorted,
  1095. key_name_conv_tab_size, sizeof (key_name_conv_tab_sorted[0]),
  1096. &key_code_name_comparator);
  1097. has_been_sorted = TRUE;
  1098. }
  1099. }
  1100. static int
  1101. lookup_keyname (const char *name, int *idx)
  1102. {
  1103. if (name[0] != '\0') {
  1104. const key_code_name_t key = { 0, name, NULL, NULL };
  1105. const key_code_name_t *keyp = &key;
  1106. key_code_name_t **res;
  1107. if (name[1] == '\0') {
  1108. *idx = -1;
  1109. return (int) name[0];
  1110. }
  1111. sort_key_name_conv_tab ();
  1112. res = bsearch (&keyp, key_name_conv_tab_sorted,
  1113. key_name_conv_tab_size,
  1114. sizeof (key_name_conv_tab_sorted[0]),
  1115. key_code_name_comparator);
  1116. if (res != NULL) {
  1117. *idx = (int) (res - (key_code_name_t **) key_name_conv_tab_sorted);
  1118. return (*res)->code;
  1119. }
  1120. }
  1121. *idx = -1;
  1122. return 0;
  1123. }
  1124. /* Return the code associated with the symbolic name keyname */
  1125. long
  1126. lookup_key (const char *name, char **label)
  1127. {
  1128. char **lc_keys, **p;
  1129. int k = -1;
  1130. int key = 0;
  1131. int lc_index = -1;
  1132. int use_meta = -1;
  1133. int use_ctrl = -1;
  1134. int use_shift = -1;
  1135. if (name == NULL)
  1136. return 0;
  1137. name = g_strstrip (g_strdup (name));
  1138. p = lc_keys = g_strsplit_set (name, "-+ ", -1);
  1139. g_free ((char *) name);
  1140. while ((p != NULL) && (*p != NULL)) {
  1141. if ((*p)[0] != '\0') {
  1142. int idx;
  1143. key = lookup_keyname (g_strstrip (*p), &idx);
  1144. if (key == KEY_M_ALT)
  1145. use_meta = idx;
  1146. else if (key == KEY_M_CTRL)
  1147. use_ctrl = idx;
  1148. else if (key == KEY_M_SHIFT)
  1149. use_shift = idx;
  1150. else {
  1151. k = key;
  1152. lc_index = idx;
  1153. break;
  1154. }
  1155. }
  1156. p++;
  1157. }
  1158. g_strfreev (lc_keys);
  1159. /* output */
  1160. if (k <= 0)
  1161. return 0;
  1162. if (label != NULL) {
  1163. GString *s;
  1164. s = g_string_new ("");
  1165. if (use_meta != -1) {
  1166. g_string_append (s, key_name_conv_tab_sorted[use_meta]->shortcut);
  1167. g_string_append_c (s, '-');
  1168. }
  1169. if (use_ctrl != -1) {
  1170. g_string_append (s, key_name_conv_tab_sorted[use_ctrl]->shortcut);
  1171. g_string_append_c (s, '-');
  1172. }
  1173. if (use_shift != -1) {
  1174. if (k < 127)
  1175. g_string_append_c (s, (gchar) g_ascii_toupper ((gchar) k));
  1176. else {
  1177. g_string_append (s, key_name_conv_tab_sorted[use_shift]->shortcut);
  1178. g_string_append_c (s, '-');
  1179. g_string_append (s, key_name_conv_tab_sorted[lc_index]->shortcut);
  1180. }
  1181. } else if (k < 128) {
  1182. if ((k >= 'A') || (lc_index < 0)
  1183. || (key_name_conv_tab_sorted[lc_index]->shortcut == NULL))
  1184. g_string_append_c (s, (gchar) g_ascii_tolower ((gchar) k));
  1185. else
  1186. g_string_append (s, key_name_conv_tab_sorted[lc_index]->shortcut);
  1187. } else if ((lc_index != -1) && (key_name_conv_tab_sorted[lc_index]->shortcut != NULL))
  1188. g_string_append (s, key_name_conv_tab_sorted[lc_index]->shortcut);
  1189. else
  1190. g_string_append_c (s, (gchar) g_ascii_tolower ((gchar) key));
  1191. *label = g_string_free (s, FALSE);
  1192. }
  1193. if (use_shift != -1) {
  1194. if (k < 127 && k > 31 )
  1195. k = g_ascii_toupper ((gchar) k);
  1196. else
  1197. k |= KEY_M_SHIFT;
  1198. }
  1199. if (use_ctrl != -1) {
  1200. if (k < 256)
  1201. k = XCTRL (k);
  1202. else
  1203. k |= KEY_M_CTRL;
  1204. }
  1205. if (use_meta != -1)
  1206. k = ALT (k);
  1207. return (long) k;
  1208. }
  1209. /*
  1210. * Return TRUE on success, FALSE on error.
  1211. * An error happens if SEQ is a beginning of an existing longer sequence.
  1212. */
  1213. gboolean
  1214. define_sequence (int code, const char *seq, int action)
  1215. {
  1216. key_def *base;
  1217. if (strlen (seq) > SEQ_BUFFER_LEN - 1)
  1218. return FALSE;
  1219. for (base = keys; (base != NULL) && (*seq != '\0');)
  1220. if (*seq == base->ch) {
  1221. if (base->child == 0) {
  1222. if (*(seq + 1) != '\0')
  1223. base->child = create_sequence (seq + 1, code, action);
  1224. else {
  1225. /* The sequence matches an existing one. */
  1226. base->code = code;
  1227. base->action = action;
  1228. }
  1229. return TRUE;
  1230. }
  1231. base = base->child;
  1232. seq++;
  1233. } else {
  1234. if (base->next)
  1235. base = base->next;
  1236. else {
  1237. base->next = create_sequence (seq, code, action);
  1238. return TRUE;
  1239. }
  1240. }
  1241. if (*seq == '\0') {
  1242. /* Attempt to redefine a sequence with a shorter sequence. */
  1243. return FALSE;
  1244. }
  1245. keys = create_sequence (seq, code, action);
  1246. return TRUE;
  1247. }
  1248. /*
  1249. * Check if we are idle, i.e. there are no pending keyboard or mouse
  1250. * events. Return 1 is idle, 0 is there are pending events.
  1251. */
  1252. gboolean
  1253. is_idle (void)
  1254. {
  1255. int maxfdp;
  1256. fd_set select_set;
  1257. struct timeval time_out;
  1258. FD_ZERO (&select_set);
  1259. FD_SET (input_fd, &select_set);
  1260. maxfdp = input_fd;
  1261. #ifdef HAVE_LIBGPM
  1262. if (mouse_enabled && (use_mouse_p == MOUSE_GPM) && (gpm_fd > 0)) {
  1263. FD_SET (gpm_fd, &select_set);
  1264. maxfdp = max (maxfdp, gpm_fd);
  1265. }
  1266. #endif
  1267. time_out.tv_sec = 0;
  1268. time_out.tv_usec = 0;
  1269. return (select (maxfdp + 1, &select_set, 0, 0, &time_out) <= 0);
  1270. }
  1271. int
  1272. get_key_code (int no_delay)
  1273. {
  1274. int c;
  1275. static key_def *this = NULL, *parent;
  1276. static struct timeval esctime = { -1, -1 };
  1277. static int lastnodelay = -1;
  1278. if (no_delay != lastnodelay) {
  1279. this = NULL;
  1280. lastnodelay = no_delay;
  1281. }
  1282. pend_send:
  1283. if (pending_keys != NULL) {
  1284. int d = *pending_keys++;
  1285. check_pend:
  1286. if (*pending_keys == 0) {
  1287. pending_keys = NULL;
  1288. seq_append = NULL;
  1289. }
  1290. if ((d == ESC_CHAR) && (pending_keys != NULL)) {
  1291. d = ALT (*pending_keys++);
  1292. goto check_pend;
  1293. }
  1294. if ((d > 127 && d < 256) && use_8th_bit_as_meta)
  1295. d = ALT (d & 0x7f);
  1296. this = NULL;
  1297. return correct_key_code (d);
  1298. }
  1299. nodelay_try_again:
  1300. if (no_delay)
  1301. tty_nodelay (TRUE);
  1302. c = tty_lowlevel_getch ();
  1303. #if (defined(USE_NCURSES) || defined(USE_NCURSESW)) && defined(KEY_RESIZE)
  1304. if (c == KEY_RESIZE)
  1305. goto nodelay_try_again;
  1306. #endif
  1307. if (no_delay) {
  1308. tty_nodelay (FALSE);
  1309. if (c == -1) {
  1310. if (this != NULL && parent != NULL && parent->action == MCKEY_ESCAPE && old_esc_mode) {
  1311. struct timeval current, time_out;
  1312. if (esctime.tv_sec == -1)
  1313. return -1;
  1314. GET_TIME (current);
  1315. time_out.tv_sec = old_esc_mode_timeout / 1000000 + esctime.tv_sec;
  1316. time_out.tv_usec = old_esc_mode_timeout % 1000000 + esctime.tv_usec;
  1317. if (time_out.tv_usec > 1000000) {
  1318. time_out.tv_usec -= 1000000;
  1319. time_out.tv_sec++;
  1320. }
  1321. if (current.tv_sec < time_out.tv_sec)
  1322. return -1;
  1323. if (current.tv_sec == time_out.tv_sec && current.tv_usec < time_out.tv_usec)
  1324. return -1;
  1325. this = NULL;
  1326. pending_keys = seq_append = NULL;
  1327. return ESC_CHAR;
  1328. }
  1329. return -1;
  1330. }
  1331. } else if (c == -1) {
  1332. /* Maybe we got an incomplete match.
  1333. This we do only in delay mode, since otherwise
  1334. tty_lowlevel_getch can return -1 at any time. */
  1335. if (seq_append != NULL) {
  1336. pending_keys = seq_buffer;
  1337. goto pend_send;
  1338. }
  1339. this = NULL;
  1340. return -1;
  1341. }
  1342. /* Search the key on the root */
  1343. if (!no_delay || this == NULL) {
  1344. this = keys;
  1345. parent = NULL;
  1346. if ((c > 127 && c < 256) && use_8th_bit_as_meta) {
  1347. c &= 0x7f;
  1348. /* The first sequence defined starts with esc */
  1349. parent = keys;
  1350. this = keys->child;
  1351. }
  1352. }
  1353. while (this != NULL) {
  1354. if (c == this->ch) {
  1355. if (this->child) {
  1356. if (!push_char (c)) {
  1357. pending_keys = seq_buffer;
  1358. goto pend_send;
  1359. }
  1360. parent = this;
  1361. this = this->child;
  1362. if (parent->action == MCKEY_ESCAPE && old_esc_mode) {
  1363. if (no_delay) {
  1364. GET_TIME (esctime);
  1365. if (this == NULL) {
  1366. /* Shouldn't happen */
  1367. fputs ("Internal error\n", stderr);
  1368. exit (EXIT_FAILURE);
  1369. }
  1370. goto nodelay_try_again;
  1371. }
  1372. esctime.tv_sec = -1;
  1373. c = xgetch_second ();
  1374. if (c == -1) {
  1375. pending_keys = seq_append = NULL;
  1376. this = NULL;
  1377. return ESC_CHAR;
  1378. }
  1379. } else {
  1380. if (no_delay)
  1381. goto nodelay_try_again;
  1382. c = tty_lowlevel_getch ();
  1383. }
  1384. } else {
  1385. /* We got a complete match, return and reset search */
  1386. int code;
  1387. pending_keys = seq_append = NULL;
  1388. code = this->code;
  1389. this = NULL;
  1390. return correct_key_code (code);
  1391. }
  1392. } else {
  1393. if (this->next != NULL)
  1394. this = this->next;
  1395. else {
  1396. if ((parent != NULL) && (parent->action == MCKEY_ESCAPE)) {
  1397. /* Convert escape-digits to F-keys */
  1398. if (g_ascii_isdigit (c))
  1399. c = KEY_F (c - '0');
  1400. else if (c == ' ')
  1401. c = ESC_CHAR;
  1402. else
  1403. c = ALT (c);
  1404. pending_keys = seq_append = NULL;
  1405. this = NULL;
  1406. return correct_key_code (c);
  1407. }
  1408. /* Did not find a match or {c} was changed in the if above,
  1409. so we have to return everything we had skipped
  1410. */
  1411. push_char (c);
  1412. pending_keys = seq_buffer;
  1413. goto pend_send;
  1414. }
  1415. }
  1416. }
  1417. this = NULL;
  1418. return correct_key_code (c);
  1419. }
  1420. /* Returns a character read from stdin with appropriate interpretation */
  1421. /* Also takes care of generated mouse events */
  1422. /* Returns EV_MOUSE if it is a mouse event */
  1423. /* Returns EV_NONE if non-blocking or interrupt set and nothing was done */
  1424. int
  1425. tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block)
  1426. {
  1427. int c;
  1428. static int flag = 0; /* Return value from select */
  1429. #ifdef HAVE_LIBGPM
  1430. static struct Gpm_Event ev; /* Mouse event */
  1431. #endif
  1432. struct timeval time_out;
  1433. struct timeval *time_addr = NULL;
  1434. static int dirty = 3;
  1435. if ((dirty == 3) || is_idle ()) {
  1436. mc_refresh ();
  1437. dirty = 1;
  1438. } else
  1439. dirty++;
  1440. vfs_timeout_handler ();
  1441. /* Ok, we use (event->x < 0) to signal that the event does not contain
  1442. a suitable position for the mouse, so we can't use show_mouse_pointer
  1443. on it.
  1444. */
  1445. if (event->x > 0) {
  1446. show_mouse_pointer (event->x, event->y);
  1447. if (!redo_event)
  1448. event->x = -1;
  1449. }
  1450. /* Repeat if using mouse */
  1451. while (pending_keys == NULL) {
  1452. int maxfdp;
  1453. fd_set select_set;
  1454. FD_ZERO (&select_set);
  1455. FD_SET (input_fd, &select_set);
  1456. maxfdp = max (add_selects (&select_set), input_fd);
  1457. #ifdef HAVE_LIBGPM
  1458. if (mouse_enabled && (use_mouse_p == MOUSE_GPM)) {
  1459. if (gpm_fd < 0) {
  1460. /* Connection to gpm broken, possibly gpm has died */
  1461. mouse_enabled = FALSE;
  1462. use_mouse_p = MOUSE_NONE;
  1463. break;
  1464. }
  1465. FD_SET (gpm_fd, &select_set);
  1466. maxfdp = max (maxfdp, gpm_fd);
  1467. }
  1468. #endif
  1469. if (redo_event) {
  1470. time_out.tv_usec = mou_auto_repeat * 1000;
  1471. time_out.tv_sec = 0;
  1472. time_addr = &time_out;
  1473. } else {
  1474. int seconds;
  1475. seconds = vfs_timeouts ();
  1476. time_addr = NULL;
  1477. if (seconds != 0) {
  1478. /* the timeout could be improved and actually be
  1479. * the number of seconds until the next vfs entry
  1480. * timeouts in the stamp list.
  1481. */
  1482. time_out.tv_sec = seconds;
  1483. time_out.tv_usec = 0;
  1484. time_addr = &time_out;
  1485. }
  1486. }
  1487. if (!block || winch_flag) {
  1488. time_addr = &time_out;
  1489. time_out.tv_sec = 0;
  1490. time_out.tv_usec = 0;
  1491. }
  1492. tty_enable_interrupt_key ();
  1493. flag = select (maxfdp + 1, &select_set, NULL, NULL, time_addr);
  1494. tty_disable_interrupt_key ();
  1495. /* select timed out: it could be for any of the following reasons:
  1496. * redo_event -> it was because of the MOU_REPEAT handler
  1497. * !block -> we did not block in the select call
  1498. * else -> 10 second timeout to check the vfs status.
  1499. */
  1500. if (flag == 0) {
  1501. if (redo_event)
  1502. return EV_MOUSE;
  1503. if (!block || winch_flag)
  1504. return EV_NONE;
  1505. vfs_timeout_handler ();
  1506. }
  1507. if (flag == -1 && errno == EINTR)
  1508. return EV_NONE;
  1509. check_selects (&select_set);
  1510. if (FD_ISSET (input_fd, &select_set))
  1511. break;
  1512. #ifdef HAVE_LIBGPM
  1513. if (mouse_enabled && use_mouse_p == MOUSE_GPM
  1514. && gpm_fd > 0 && FD_ISSET (gpm_fd, &select_set)) {
  1515. Gpm_GetEvent (&ev);
  1516. Gpm_FitEvent (&ev);
  1517. *event = ev;
  1518. return EV_MOUSE;
  1519. }
  1520. #endif /* !HAVE_LIBGPM */
  1521. }
  1522. #ifndef HAVE_SLANG
  1523. flag = is_wintouched (stdscr);
  1524. untouchwin (stdscr);
  1525. #endif /* !HAVE_SLANG */
  1526. c = block ? getch_with_delay () : get_key_code (1);
  1527. #ifndef HAVE_SLANG
  1528. if (flag > 0)
  1529. tty_touch_screen ();
  1530. #endif /* !HAVE_SLANG */
  1531. if (mouse_enabled && (c == MCKEY_MOUSE
  1532. #ifdef KEY_MOUSE
  1533. || c == KEY_MOUSE
  1534. #endif /* KEY_MOUSE */
  1535. )) {
  1536. /* Mouse event */
  1537. xmouse_get_event (event);
  1538. return (event->type != 0) ? EV_MOUSE : EV_NONE;
  1539. }
  1540. return c;
  1541. }
  1542. /* Returns a key press, mouse events are discarded */
  1543. int
  1544. tty_getch (void)
  1545. {
  1546. Gpm_Event ev;
  1547. int key;
  1548. ev.x = -1;
  1549. while ((key = tty_get_event (&ev, FALSE, TRUE)) == EV_NONE);
  1550. return key;
  1551. }
  1552. char *
  1553. learn_key (void)
  1554. {
  1555. /* LEARN_TIMEOUT in usec */
  1556. #define LEARN_TIMEOUT 200000
  1557. fd_set Read_FD_Set;
  1558. struct timeval endtime;
  1559. struct timeval time_out;
  1560. int c;
  1561. char buffer[256];
  1562. char *p = buffer;
  1563. tty_keypad (FALSE); /* disable intepreting keys by ncurses */
  1564. c = tty_lowlevel_getch ();
  1565. while (c == -1)
  1566. c = tty_lowlevel_getch (); /* Sanity check, should be unnecessary */
  1567. learn_store_key (buffer, &p, c);
  1568. GET_TIME (endtime);
  1569. endtime.tv_usec += LEARN_TIMEOUT;
  1570. if (endtime.tv_usec > 1000000) {
  1571. endtime.tv_usec -= 1000000;
  1572. endtime.tv_sec++;
  1573. }
  1574. tty_nodelay (TRUE);
  1575. for (;;) {
  1576. while ((c = tty_lowlevel_getch ()) == -1) {
  1577. GET_TIME (time_out);
  1578. time_out.tv_usec = endtime.tv_usec - time_out.tv_usec;
  1579. if (time_out.tv_usec < 0)
  1580. time_out.tv_sec++;
  1581. time_out.tv_sec = endtime.tv_sec - time_out.tv_sec;
  1582. if (time_out.tv_sec >= 0 && time_out.tv_usec > 0) {
  1583. FD_ZERO (&Read_FD_Set);
  1584. FD_SET (input_fd, &Read_FD_Set);
  1585. select (input_fd + 1, &Read_FD_Set, NULL, NULL, &time_out);
  1586. } else
  1587. break;
  1588. }
  1589. if (c == -1)
  1590. break;
  1591. learn_store_key (buffer, &p, c);
  1592. }
  1593. tty_keypad (TRUE);
  1594. tty_nodelay (FALSE);
  1595. *p = '\0';
  1596. return g_strdup (buffer);
  1597. #undef LEARN_TIMEOUT
  1598. }
  1599. /* xterm and linux console only: set keypad to numeric or application
  1600. mode. Only in application keypad mode it's possible to distinguish
  1601. the '+' key and the '+' on the keypad ('*' and '-' ditto)*/
  1602. void
  1603. numeric_keypad_mode (void)
  1604. {
  1605. if (console_flag || xterm_flag) {
  1606. fputs ("\033>", stdout);
  1607. fflush (stdout);
  1608. }
  1609. }
  1610. void
  1611. application_keypad_mode (void)
  1612. {
  1613. if (console_flag || xterm_flag) {
  1614. fputs ("\033=", stdout);
  1615. fflush (stdout);
  1616. }
  1617. }