ncurses.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Wed, 02 May 2012 20:51:14 +0200
changeset 165 f551b78c3eee
parent 118 17dff2c6fc88
permissions -rw-r--r--
a bluetooth and a c++ demo committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 * ncurses.c
 * Copyright (C) 2008 Markus Broeker
 *
 * Unicode != UTF-8
 * unicode with ncursesw ( wide char)
 * gcc $< -lncursesw -o $@
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <ncurses.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");
        exit (EXIT_FAILURE);
    }
    return EXIT_SUCCESS;
}

wchar_t func (WINDOW * win)
{
    wchar_t guess = '\0';

    char dest[6];

    int maxx;
    int maxy;

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

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

        memset (&dest, 0, sizeof (dest));
        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 ();
    keypad (win, 1);
    noecho ();

    x = func (win);

    endwin ();
    printf ("\n");

    return EXIT_SUCCESS;
}