source: libcaca/trunk/examples/input.c @ 4333

Last change on this file since 4333 was 4333, checked in by Sam Hocevar, 10 years ago

Large source code cleanup, getting rid of spaces, tabs, and svn keywords.

  • Property svn:keywords set to Id
File size: 5.2 KB
RevLine 
[967]1/*
2 *  input         libcaca text input test program
[4148]3 *  Copyright (c) 2006 Sam Hocevar <sam@hocevar.net>
[967]4 *                All Rights Reserved
5 *
[1462]6 *  This program is free software. It comes without any warranty, to
[1452]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
[967]10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13#include "config.h"
14
[1048]15#if !defined(__KERNEL__)
16#   include <string.h>
[1806]17#   include <stdio.h>
[967]18#endif
19
20#include "caca.h"
21
22#define BUFFER_SIZE 75
23#define TEXT_ENTRIES 5
24
25typedef struct textentry
26{
27    uint32_t buffer[BUFFER_SIZE + 1];
[3485]28    unsigned int size, cursor, changed;
[967]29} textentry;
30
[1048]31int main(int argc, char *argv[])
[967]32{
33    textentry entries[TEXT_ENTRIES];
[2821]34    caca_canvas_t *cv;
[967]35    caca_display_t *dp;
36    unsigned int i, e = 0, running = 1;
37
[2821]38    cv = caca_create_canvas(0, 0);
[1753]39    if(cv == NULL)
40    {
[1806]41        printf("Can't create canvas\n");
[1753]42        return -1;
43    }
[967]44    dp = caca_create_display(cv);
[1753]45    if(dp == NULL)
46    {
47        printf("Can't create display\n");
48        return -1;
49    }
[1837]50    caca_set_cursor(dp, 1);
[967]51
[2821]52    caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
53    caca_put_str(cv, 1, 1, "Text entries - press tab to cycle");
[967]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;
[3485]60        entries[i].changed = 1;
[3481]61        caca_printf(cv, 3, 3 * i + 4, "[entry %i]", i + 1);
[967]62    }
63
[4147]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
[967]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
[3485]82            if(!entries[i].changed)
83                continue;
84
[2821]85            caca_set_color_ansi(cv, CACA_BLACK, CACA_LIGHTGRAY);
[3481]86            caca_fill_box(cv, 2, 3 * i + 5, BUFFER_SIZE + 1, 1, ' ');
[967]87
88            start = 0;
89            size = entries[i].size;
90
91            for(j = 0; j < size; j++)
92            {
[3481]93                caca_put_char(cv, 2 + j, 3 * i + 5,
[967]94                              entries[i].buffer[start + j]);
95            }
[3485]96
97            entries[i].changed = 0;
[967]98        }
99
100        /* Put the cursor on the active textentry */
[3481]101        caca_gotoxy(cv, 2 + entries[e].cursor, 3 * e + 5);
[967]102
103        caca_refresh_display(dp);
104
105        if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1) == 0)
106            continue;
107
[2049]108        switch(caca_get_event_key_ch(&ev))
[967]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--;
[3485]138                    entries[e].changed = 1;
[967]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--;
[3485]149                    entries[e].changed = 1;
[967]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);
[2049]158                    entries[e].buffer[entries[e].cursor] =
159                                              caca_get_event_key_utf32(&ev);
[967]160                    entries[e].size++;
161                    entries[e].cursor++;
[3485]162                    entries[e].changed = 1;
[967]163                }
164                break;
165        }
166    }
167
168    caca_free_display(dp);
[2821]169    caca_free_canvas(cv);
[967]170
171    return 0;
172}
173
Note: See TracBrowser for help on using the repository browser.