demo_defkey.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /****************************************************************************
  2. * Copyright (c) 2002-2007,2008 Free Software Foundation, Inc. *
  3. * *
  4. * Permission is hereby granted, free of charge, to any person obtaining a *
  5. * copy of this software and associated documentation files (the *
  6. * "Software"), to deal in the Software without restriction, including *
  7. * without limitation the rights to use, copy, modify, merge, publish, *
  8. * distribute, distribute with modifications, sublicense, and/or sell *
  9. * copies of the Software, and to permit persons to whom the Software is *
  10. * furnished to do so, subject to the following conditions: *
  11. * *
  12. * The above copyright notice and this permission notice shall be included *
  13. * in all copies or substantial portions of the Software. *
  14. * *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
  18. * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
  21. * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  22. * *
  23. * Except as contained in this notice, the name(s) of the above copyright *
  24. * holders shall not be used in advertising or otherwise to promote the *
  25. * sale, use or other dealings in this Software without prior written *
  26. * authorization. *
  27. ****************************************************************************/
  28. /*
  29. * $Id: demo_defkey.c,v 1.19 2008/08/04 16:24:47 tom Exp $
  30. *
  31. * Demonstrate the define_key() function.
  32. * Thomas Dickey - 2002/11/23
  33. */
  34. #include <test.priv.h>
  35. #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
  36. #define MY_LOGFILE "demo_defkey.log"
  37. /*
  38. * Log the most recently-written line to our logfile
  39. */
  40. static void
  41. log_last_line(WINDOW *win)
  42. {
  43. FILE *fp;
  44. int y, x, n;
  45. char temp[256];
  46. if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
  47. int need = sizeof(temp) - 1;
  48. if (need > COLS)
  49. need = COLS;
  50. getyx(win, y, x);
  51. wmove(win, y - 1, 0);
  52. n = winnstr(win, temp, need);
  53. while (n-- > 0) {
  54. if (isspace(UChar(temp[n])))
  55. temp[n] = '\0';
  56. else
  57. break;
  58. }
  59. wmove(win, y, x);
  60. fprintf(fp, "%s\n", temp);
  61. fclose(fp);
  62. }
  63. }
  64. /*
  65. * Convert a character to visible form.
  66. */
  67. static char *
  68. visichar(int ch)
  69. {
  70. static char temp[10];
  71. ch = UChar(ch);
  72. assert(ch >= 0 && ch < 256);
  73. if (ch == '\\') {
  74. strcpy(temp, "\\\\");
  75. } else if (ch == '\033') {
  76. strcpy(temp, "\\E");
  77. } else if (ch < ' ') {
  78. sprintf(temp, "\\%03o", ch);
  79. } else if (ch >= 127) {
  80. sprintf(temp, "\\%03o", ch);
  81. } else {
  82. sprintf(temp, "%c", ch);
  83. }
  84. return temp;
  85. }
  86. /*
  87. * Convert a string to visible form.
  88. */
  89. static char *
  90. visible(const char *string)
  91. {
  92. char *result = 0;
  93. unsigned need = 1;
  94. int pass;
  95. int n;
  96. if (string != 0 && *string != '\0') {
  97. for (pass = 0; pass < 2; ++pass) {
  98. for (n = 0; string[n] != '\0'; ++n) {
  99. char temp[80];
  100. strcpy(temp, visichar(string[n]));
  101. if (pass)
  102. strcat(result, temp);
  103. else
  104. need += strlen(temp);
  105. }
  106. if (!pass)
  107. result = typeCalloc(char, need);
  108. }
  109. } else {
  110. result = typeCalloc(char, 1);
  111. }
  112. return result;
  113. }
  114. static void
  115. really_define_key(WINDOW *win, const char *new_string, int code)
  116. {
  117. int rc;
  118. const char *code_name = keyname(code);
  119. char *old_string;
  120. char *vis_string = 0;
  121. char temp[80];
  122. if (code_name == 0) {
  123. sprintf(temp, "Keycode %d", code);
  124. code_name = temp;
  125. }
  126. if ((old_string = keybound(code, 0)) != 0) {
  127. wprintw(win, "%s is %s\n",
  128. code_name,
  129. vis_string = visible(old_string));
  130. } else {
  131. wprintw(win, "%s is not bound\n",
  132. code_name);
  133. }
  134. log_last_line(win);
  135. if (vis_string != 0) {
  136. free(vis_string);
  137. vis_string = 0;
  138. }
  139. vis_string = visible(new_string);
  140. if ((rc = key_defined(new_string)) > 0) {
  141. wprintw(win, "%s was bound to %s\n", vis_string, keyname(rc));
  142. log_last_line(win);
  143. } else if (new_string != 0 && rc < 0) {
  144. wprintw(win, "%s conflicts with longer strings\n", vis_string);
  145. log_last_line(win);
  146. }
  147. rc = define_key(new_string, code);
  148. if (rc == ERR) {
  149. wprintw(win, "%s unchanged\n", code_name);
  150. log_last_line(win);
  151. } else if (new_string != 0) {
  152. wprintw(win, "%s is now bound to %s\n",
  153. vis_string,
  154. code_name);
  155. log_last_line(win);
  156. } else if (old_string != 0) {
  157. wprintw(win, "%s deleted\n", code_name);
  158. log_last_line(win);
  159. }
  160. if (vis_string != 0)
  161. free(vis_string);
  162. if (old_string != 0)
  163. free(old_string);
  164. }
  165. static void
  166. duplicate(WINDOW *win, NCURSES_CONST char *name, int code)
  167. {
  168. char *value = tigetstr(name);
  169. if (value != 0) {
  170. const char *prefix = 0;
  171. char temp[BUFSIZ];
  172. if (!strncmp(value, "\033[", 2)) {
  173. prefix = "\033O";
  174. } else if (!strncmp(value, "\033O", 2)) {
  175. prefix = "\033[";
  176. }
  177. if (prefix != 0) {
  178. sprintf(temp, "%s%s", prefix, value + 2);
  179. really_define_key(win, temp, code);
  180. }
  181. }
  182. }
  183. static void
  184. redefine(WINDOW *win, char *string, int code)
  185. {
  186. really_define_key(win, string, code);
  187. }
  188. static void
  189. remove_definition(WINDOW *win, int code)
  190. {
  191. really_define_key(win, 0, code);
  192. }
  193. int
  194. main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
  195. {
  196. char *fkeys[12];
  197. int n;
  198. int ch;
  199. WINDOW *win;
  200. unlink(MY_LOGFILE);
  201. initscr();
  202. (void) cbreak(); /* take input chars one at a time, no wait for \n */
  203. (void) noecho(); /* don't echo input */
  204. printw("This demo is best on xterm: it reverses the definitions for f1-f12,\n");
  205. printw("adds duplicate definitions for cursor application and normal modes,\n");
  206. printw("and removes any definitions for the mini keypad. Type any of those:\n");
  207. refresh();
  208. win = newwin(LINES - 3, COLS, 3, 0);
  209. scrollok(win, TRUE);
  210. keypad(win, TRUE);
  211. wmove(win, 0, 0);
  212. /* we do the define_key() calls after keypad(), since the first call to
  213. * keypad() initializes the corresponding data.
  214. */
  215. for (n = 0; n < 12; ++n) {
  216. char name[10];
  217. sprintf(name, "kf%d", n + 1);
  218. fkeys[n] = tigetstr(name);
  219. }
  220. for (n = 0; n < 12; ++n) {
  221. redefine(win, fkeys[11 - n], KEY_F(n + 1));
  222. }
  223. duplicate(win, "kcub1", KEY_LEFT);
  224. duplicate(win, "kcuu1", KEY_UP);
  225. duplicate(win, "kcud1", KEY_DOWN);
  226. duplicate(win, "kcuf1", KEY_RIGHT);
  227. remove_definition(win, KEY_A1);
  228. remove_definition(win, KEY_A3);
  229. remove_definition(win, KEY_B2);
  230. remove_definition(win, KEY_C1);
  231. remove_definition(win, KEY_C3);
  232. really_define_key(win, "\033O", 1023);
  233. while ((ch = wgetch(win)) != ERR) {
  234. const char *name = keyname(ch);
  235. wprintw(win, "Keycode %d, name %s\n",
  236. ch,
  237. name != 0 ? name : "<null>");
  238. log_last_line(win);
  239. wclrtoeol(win);
  240. if (ch == 'q')
  241. break;
  242. }
  243. endwin();
  244. ExitProgram(EXIT_FAILURE);
  245. }
  246. #else
  247. int
  248. main(void)
  249. {
  250. printf("This program requires the ncurses library\n");
  251. ExitProgram(EXIT_FAILURE);
  252. }
  253. #endif