ncurses.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 30 Apr 2009 23:38:48 +0200
changeset 88 91fbc3ea240b
parent 77 49e0babccb23
child 114 6f6850407ccf
permissions -rw-r--r--
how to use templates We read 10 numbers from stdin and print the nearest values of a query. 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 <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 func (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 ();
    keypad (win, 1);
    noecho ();

    x = func (win);

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

    return EXIT_SUCCESS;
}