ncurses.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:57:58 +0100
changeset 6 c3dc3eb3b541
parent 0 af501b0c1716
child 8 96d16dfe787a
permissions -rw-r--r--
a small libtool demo added to the demo distribution committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 *     $Id: ncurses.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $
 * $Source: /development/c/demos/ncurses.c,v $
 *
 */

/*
 * Unicode != UTF-8
 * unicode with ncursesw ( wide char)
 * gcc $< -lncursesw -o $@
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <ncursesw/curses.h>
#include <wchar.h>
#include <locale.h>

#ifndef u_char
typedef unsigned char u_char;
#endif

#define ESCAPE 0x1B

#ifndef get_wch
extern int get_wch ();
#endif

int check_locale ()
{
    if (!setlocale (LC_CTYPE, "")) {
        perror ("SETLOCALE");
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

wchar_t fucnt (WINDOW * win)
{
    wchar_t guess;
    char dest[4];
    int maxx;
    int maxy;

    while (guess != ESCAPE) {
        while ((get_wch (&guess)) != OK)
            /*
             * waiting for multibyte character
             */
            ;

        maxx = getmaxx (win);
        maxy = getmaxy (win);

        dest[0] = 0;
        dest[1] = 0;
        dest[2] = 0;
        dest[3] = 0;
        if (wctomb (dest, guess) == -1) {
            perror ("WCTOMB");
            return -1;
        }

        mvprintw (maxy / 2, maxx / 2 - 12,
                  "UTF-8: (%2X:%2X:%2X:%2X)\t\t", (u_char) dest[0],
                  (u_char) dest[1], (u_char) dest[2], (u_char) dest[3]);

        mvprintw (maxy - 1, 0, "KEY: %2lc\t", guess);
        mvprintw (maxy - 1, maxx - 14, "UNICODE: %4X", guess);
        mvprintw (maxy - 1, maxx / 2 - 10, "Press ESC to quit");
    }
    return guess;
}

int main ()
{
    wchar_t x;
    WINDOW *win;

    check_locale ();
    win = initscr ();
    noecho ();
    x = fucnt (win);
    endwin ();

    printf ("\n");
    return 0;
}