ncurses.c
author Markus Bröker<broeker.markus@googlemail.com>
Sat, 21 Oct 2017 13:45:05 +0200
changeset 171 c6e0af68825a
parent 118 17dff2c6fc88
permissions -rw-r--r--
Entrypoint and RET fixed

/**
 * 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;
}