key.c 70 KB

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