選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

input.c 5.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * input libcaca text input test program
  3. * Copyright (c) 2006 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. #include "config.h"
  15. #if !defined(__KERNEL__)
  16. # include <string.h>
  17. # include <stdio.h>
  18. #endif
  19. #include "caca.h"
  20. #define BUFFER_SIZE 75
  21. #define TEXT_ENTRIES 5
  22. typedef struct textentry
  23. {
  24. uint32_t buffer[BUFFER_SIZE + 1];
  25. unsigned int size, cursor, changed;
  26. } textentry;
  27. int main(int argc, char *argv[])
  28. {
  29. textentry entries[TEXT_ENTRIES];
  30. caca_canvas_t *cv;
  31. caca_display_t *dp;
  32. unsigned int i, e = 0, running = 1;
  33. cv = caca_create_canvas(0, 0);
  34. if(cv == NULL)
  35. {
  36. printf("Can't create canvas\n");
  37. return -1;
  38. }
  39. dp = caca_create_display(cv);
  40. if(dp == NULL)
  41. {
  42. printf("Can't create display\n");
  43. return -1;
  44. }
  45. caca_set_cursor(dp, 1);
  46. caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
  47. caca_put_str(cv, 1, 1, "Text entries - press tab to cycle");
  48. for(i = 0; i < TEXT_ENTRIES; i++)
  49. {
  50. entries[i].buffer[0] = 0;
  51. entries[i].size = 0;
  52. entries[i].cursor = 0;
  53. entries[i].changed = 1;
  54. caca_printf(cv, 3, 3 * i + 4, "[entry %i]", i + 1);
  55. }
  56. /* Put Unicode crap in the last text entry */
  57. entries[TEXT_ENTRIES - 1].buffer[0] = 'A';
  58. entries[TEXT_ENTRIES - 1].buffer[1] = 'b';
  59. entries[TEXT_ENTRIES - 1].buffer[2] = caca_utf8_to_utf32("Ç", NULL);
  60. entries[TEXT_ENTRIES - 1].buffer[3] = caca_utf8_to_utf32("đ", NULL);
  61. entries[TEXT_ENTRIES - 1].buffer[4] = caca_utf8_to_utf32("ボ", NULL);
  62. entries[TEXT_ENTRIES - 1].buffer[5] = CACA_MAGIC_FULLWIDTH;
  63. entries[TEXT_ENTRIES - 1].buffer[6] = caca_utf8_to_utf32("♥", NULL);
  64. entries[TEXT_ENTRIES - 1].size = 7;
  65. while(running)
  66. {
  67. caca_event_t ev;
  68. for(i = 0; i < TEXT_ENTRIES; i++)
  69. {
  70. unsigned int j, start, size;
  71. if(!entries[i].changed)
  72. continue;
  73. caca_set_color_ansi(cv, CACA_BLACK, CACA_LIGHTGRAY);
  74. caca_fill_box(cv, 2, 3 * i + 5, BUFFER_SIZE + 1, 1, ' ');
  75. start = 0;
  76. size = entries[i].size;
  77. for(j = 0; j < size; j++)
  78. {
  79. caca_put_char(cv, 2 + j, 3 * i + 5,
  80. entries[i].buffer[start + j]);
  81. }
  82. entries[i].changed = 0;
  83. }
  84. /* Put the cursor on the active textentry */
  85. caca_gotoxy(cv, 2 + entries[e].cursor, 3 * e + 5);
  86. caca_refresh_display(dp);
  87. if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1) == 0)
  88. continue;
  89. switch(caca_get_event_key_ch(&ev))
  90. {
  91. case CACA_KEY_ESCAPE:
  92. running = 0;
  93. break;
  94. case CACA_KEY_TAB:
  95. case CACA_KEY_RETURN:
  96. e = (e + 1) % TEXT_ENTRIES;
  97. break;
  98. case CACA_KEY_HOME:
  99. entries[e].cursor = 0;
  100. break;
  101. case CACA_KEY_END:
  102. entries[e].cursor = entries[e].size;
  103. break;
  104. case CACA_KEY_LEFT:
  105. if(entries[e].cursor)
  106. entries[e].cursor--;
  107. break;
  108. case CACA_KEY_RIGHT:
  109. if(entries[e].cursor < entries[e].size)
  110. entries[e].cursor++;
  111. break;
  112. case CACA_KEY_DELETE:
  113. if(entries[e].cursor < entries[e].size)
  114. {
  115. memmove(entries[e].buffer + entries[e].cursor,
  116. entries[e].buffer + entries[e].cursor + 1,
  117. (entries[e].size - entries[e].cursor + 1) * 4);
  118. entries[e].size--;
  119. entries[e].changed = 1;
  120. }
  121. break;
  122. case CACA_KEY_BACKSPACE:
  123. if(entries[e].cursor)
  124. {
  125. memmove(entries[e].buffer + entries[e].cursor - 1,
  126. entries[e].buffer + entries[e].cursor,
  127. (entries[e].size - entries[e].cursor) * 4);
  128. entries[e].size--;
  129. entries[e].cursor--;
  130. entries[e].changed = 1;
  131. }
  132. break;
  133. default:
  134. if(entries[e].size < BUFFER_SIZE)
  135. {
  136. memmove(entries[e].buffer + entries[e].cursor + 1,
  137. entries[e].buffer + entries[e].cursor,
  138. (entries[e].size - entries[e].cursor) * 4);
  139. entries[e].buffer[entries[e].cursor] =
  140. caca_get_event_key_utf32(&ev);
  141. entries[e].size++;
  142. entries[e].cursor++;
  143. entries[e].changed = 1;
  144. }
  145. break;
  146. }
  147. }
  148. caca_free_display(dp);
  149. caca_free_canvas(cv);
  150. return 0;
  151. }