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