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