key.c 58 KB

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