rthornton128 / goncurses

NCurses Library for Go
Other
383 stars 51 forks source link

KEY_RESIZE not fired #32

Closed koojunho closed 7 years ago

koojunho commented 8 years ago

This go code doesn't receive KEY_RESIZE, when I resize the terminal window.

package main

import (
    gc "github.com/rthornton128/goncurses"
)

func main() {
    stdscr, _ := gc.Init()
    defer gc.End()

    gc.Echo(false)

    for {
        y, x := stdscr.MaxYX()
        stdscr.Printf("x=%d, y=%d\n", x, y)

        ch := stdscr.GetChar()
        switch ch {
        case gc.KEY_RESIZE:
            stdscr.Println("KEY_RESIZE")
        case 'q':
            return
        }
    }
}

Here is the other version that can get KEY_RESIZE which is written in C.

#include <curses.h>
#include <string.h>

int main()
{
    WINDOW *win = initscr();
    int key;
    while (1) {
        key = wgetch(win);
        if (key == 27) {
            break;
        } else if (key == KEY_RESIZE) {
            clear();
            mvprintw(0, 0, "COLS = %d, LINES = %d", COLS, LINES);
            for (int i = 0; i < COLS; i++)
                mvaddch(1, i, '*');
            refresh();
        }
    }

    endwin();
    return 0;
}
rthornton128 commented 8 years ago

I've tried to get to the bottom of this one but it appears that it may be a cgo bug.

rthornton128 commented 8 years ago

After further research this is a result of golang signal handling. Go takes control of all signals so the underlying C code never sees the signal.

An initial work around is to use signal.Notify() for SIGWINCH. You can then manually do resizing.

Be forewarned that ncurses will not play nice with go routines so take extra care.

rthornton128 commented 8 years ago

Looks like I was correct in that using signal.Notify will trap the signal as in C. See issue #34 for the solution.

koojunho commented 7 years ago

Thank you !!