On Sat, Mar 30, 2019 at 10:54:21AM -0000, Stuart Henderson wrote:
> On 2019-03-30, Leonid Bobrov <mazoc...@disroot.org> wrote:
> > On Tue, Mar 26, 2019 at 10:51:35AM +0200, Leonid Bobrov wrote:
> >> Hi, dear NetBSD and OpenBSD communities.
> >> 
> >> I need to work with wscons, but I don't want to guess by examples
> >> how to work with it, can you please provide documentation for its
> >> API?
> >
> > Theo, Ulf, Anton, Todd, Martin, Frederic, Jasper and Jonathan,
> > you are the most recent committers to wscons, you know wscons
> > more than anyone else, so why won't you respond?
> >
> > I had to ask letoram from Arcan for help, but he only knows 3/10
> > of wscons and he had to read kernel source to understand how to
> > work with wscons.
> >
> >
> 
> Docs are often better written by a smart person learning to use
> something (so they identify what they need to learn), reading the
> existing source and asking specific questions where they get stuck, than
> by someone who already knows the area.
> 
> Also: figuring out *what* question to ask will often lead you to the
> answer yourself.
> 

Ok, it makes sense, thank you for an adequate answer.

Right now I'm testing raw keyboard input, but I'd like to get to more
abstract interface (KS_* constants in <dev/wscons/wsksymdef.h>) so that
I can conveniently execute code on particular key presses.

I've written a small program and it appears to work:
/* Public domain. */

#include <time.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wskbdraw.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>

int
main(void)
{
        int kbdfd = open("/dev/wskbd", O_RDONLY | O_NONBLOCK | O_EXCL);

        if (kbdfd == -1) {
                printf("Couldn't open /dev/wskbd\n");
                return 1;
        }

        struct wscons_event events[32];
        int n;
        bool quit = false;

        while ((n = read(kbdfd, events, sizeof(events))) != 0) {
                if (n == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
                        printf("Error while reading.\n");
                        return 1;
                }

                n /= sizeof(struct wscons_event);

                for (int i = 0; i < n; ++i)
                        if (events[i].type == WSCONS_EVENT_KEY_DOWN) {
                                int value = events[i].value;

                                if (value == RAWKEY_q) {
                                        printf("Quit.\n");
                                        quit = true;
                                        break;
                                } else if (value >= RAWKEY_1
                                                && value <= RAWKEY_9) {
                                        printf("You pressed number: %c!\n",
                                                value - RAWKEY_1 + '1');
                                } else if (value == RAWKEY_0) {
                                        printf("You pressed number: 0!\n");
                                } else {
                                        /* Nonsense. */
                                        printf("You pressed: %c\n", value);
                                }
                        }

                if (quit)
                        break;
        }

        return 0;
}

Reply via email to