Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

input.c 4.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * input libcaca text input test program
  3. * Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
  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. #include "common.h"
  16. #if !defined(__KERNEL__)
  17. # if defined(HAVE_INTTYPES_H)
  18. # include <inttypes.h>
  19. # endif
  20. # include <string.h>
  21. # include <stdio.h>
  22. #endif
  23. #include "cucul.h"
  24. #include "caca.h"
  25. #define BUFFER_SIZE 75
  26. #define TEXT_ENTRIES 5
  27. typedef struct textentry
  28. {
  29. uint32_t buffer[BUFFER_SIZE + 1];
  30. unsigned int size, cursor;
  31. } textentry;
  32. int main(int argc, char *argv[])
  33. {
  34. textentry entries[TEXT_ENTRIES];
  35. cucul_canvas_t *cv;
  36. caca_display_t *dp;
  37. unsigned int i, e = 0, running = 1;
  38. cv = cucul_create_canvas(0, 0);
  39. if(cv == NULL)
  40. {
  41. printf("Can't create canvas\n");
  42. return -1;
  43. }
  44. dp = caca_create_display(cv);
  45. if(dp == NULL)
  46. {
  47. printf("Can't create display\n");
  48. return -1;
  49. }
  50. caca_set_cursor(dp, 1);
  51. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
  52. cucul_put_str(cv, 1, 1, "Text entries - press tab to cycle");
  53. for(i = 0; i < TEXT_ENTRIES; i++)
  54. {
  55. entries[i].buffer[0] = 0;
  56. entries[i].size = 0;
  57. entries[i].cursor = 0;
  58. }
  59. while(running)
  60. {
  61. caca_event_t ev;
  62. for(i = 0; i < TEXT_ENTRIES; i++)
  63. {
  64. unsigned int j, start, size;
  65. cucul_set_color_ansi(cv, CUCUL_BLACK, CUCUL_LIGHTGRAY);
  66. cucul_fill_box(cv, 2, 3 * i + 4, 2 + BUFFER_SIZE, 3 * i + 4, ' ');
  67. start = 0;
  68. size = entries[i].size;
  69. for(j = 0; j < size; j++)
  70. {
  71. cucul_put_char(cv, 2 + j, 3 * i + 4,
  72. entries[i].buffer[start + j]);
  73. }
  74. }
  75. /* Put the cursor on the active textentry */
  76. cucul_gotoxy(cv, 2 + entries[e].cursor, 3 * e + 4);
  77. caca_refresh_display(dp);
  78. if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1) == 0)
  79. continue;
  80. switch(ev.data.key.ch)
  81. {
  82. case CACA_KEY_ESCAPE:
  83. running = 0;
  84. break;
  85. case CACA_KEY_TAB:
  86. case CACA_KEY_RETURN:
  87. e = (e + 1) % TEXT_ENTRIES;
  88. break;
  89. case CACA_KEY_HOME:
  90. entries[e].cursor = 0;
  91. break;
  92. case CACA_KEY_END:
  93. entries[e].cursor = entries[e].size;
  94. break;
  95. case CACA_KEY_LEFT:
  96. if(entries[e].cursor)
  97. entries[e].cursor--;
  98. break;
  99. case CACA_KEY_RIGHT:
  100. if(entries[e].cursor < entries[e].size)
  101. entries[e].cursor++;
  102. break;
  103. case CACA_KEY_DELETE:
  104. if(entries[e].cursor < entries[e].size)
  105. {
  106. memmove(entries[e].buffer + entries[e].cursor,
  107. entries[e].buffer + entries[e].cursor + 1,
  108. (entries[e].size - entries[e].cursor + 1) * 4);
  109. entries[e].size--;
  110. }
  111. break;
  112. case CACA_KEY_BACKSPACE:
  113. if(entries[e].cursor)
  114. {
  115. memmove(entries[e].buffer + entries[e].cursor - 1,
  116. entries[e].buffer + entries[e].cursor,
  117. (entries[e].size - entries[e].cursor) * 4);
  118. entries[e].size--;
  119. entries[e].cursor--;
  120. }
  121. break;
  122. default:
  123. if(entries[e].size < BUFFER_SIZE)
  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].buffer[entries[e].cursor] = ev.data.key.utf32;
  129. entries[e].size++;
  130. entries[e].cursor++;
  131. }
  132. break;
  133. }
  134. }
  135. caca_free_display(dp);
  136. cucul_free_canvas(cv);
  137. return 0;
  138. }