ncurses child window not keeping fixed width - c

I have an ncurses program, with multiple child windows acting as columns. Each child window has a fixed width and the height of the parent terminal window.
However I've found that if the width of the terminal is reduced then one of the windows loses their fixed width and seems to "overflow" their previously set bounds, using the entire remaining width of the terminal.
This is hard to explain so I made a quick gif showing the problem:
This is the code I've used for the above:
#include <ncurses.h>
int main() {
WINDOW * winA, * winB;
int i = 0, width = 30;
initscr();
// Suppress stdout
noecho();
// Enable keypad
keypad(stdscr, true);
// interrupt, quit, suspend, and flow control characters are all passed through uninterpreted
raw();
winA = newwin(0, width, 0, 0);
winB = newwin(0, width, 0, width);
timeout(50);
while(getch() != 'q') {
i = width * getmaxy(stdscr);
werase(winA);
werase(winB);
while (i--) {
waddstr(winA, "0");
waddstr(winB, "1");
}
wnoutrefresh(stdscr);
wnoutrefresh(winA);
wnoutrefresh(winB);
doupdate();
}
endwin();
return 0;
}
Here is another screenshot showing the issue in my actual program. The terminal on the left is correct, the one on the right shows the result after resizing the window and triggering this issue:
How can I prevent windows from losing their fixed-width-ness when the terminal is resized to a small width?

Rather than using windows for everything, you could use pads. Windows are limited to the screen-size, while pads are not.
When ncurses gets a SIGWINCH, it resizes stdscr and everything contained within stdscr, shrinking those windows as needed to fit in the new screen-size, or (if their margins match the old screen-size), increasing their size. That's automatic. Your program could check for KEY_RESIZE returned by getch and call wresize to change window sizes (and redraw their contents).
If you used pads, those do not get resized (pads are shown through a viewport that the caller can adjust).

Related

Why XServer sends FocusOut notify twice

I write dummy window for software KVM. Idea is when user switches to another machine, previous machine opens dummy window beyond the screen bounds, so emulates focus lost. Windows without focus are transparent on my system (awesome wm). If I just use XSetInputFocus(dpy, None, RevertToNone, CurrentTime) current window loses focus but doesn't become transparent. That is why I use dummy window.
Come to the point. My program saves focused window before opens dummy window. When dummy window loses focus program restores saved. So It needs to catch FocusOut event and then call XSetInputFocus function to restore focus. Problem is that xserver sends FocusOut notify twice. If I handle it only once program works incorrect.
Code is below.
#include <X11/Xlib.h>
#include <X11/Xatom.h>
int x = 100, y = 100, height = 200, width = 200;
typedef struct {
Window win;
int notify;
} wait_arg;
Bool xevent_handler(Display *dpy, XEvent *ev, XPointer arg) {
wait_arg *a = (wait_arg *) arg;
return (ev->type == a->notify) && (ev->xvisibility.window == a->win);
}
int main(int argc, char *argv[]) {
Display *dpy;
Window focused;
int revert_to;
Window win;
wait_arg arg;
XEvent ev;
dpy = XOpenDisplay(NULL);
// Save window which has focus now
XGetInputFocus(dpy, &focused, &revert_to);
int s = DefaultScreen(dpy);
win = XCreateSimpleWindow(dpy, RootWindow(dpy, s), 0, 0, height, width, 0,
CopyFromParent, CopyFromParent);
// Set DIALOG type for window
Atom type = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
long value = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
XChangeProperty(dpy, win, type, XA_ATOM, 32, PropModeReplace,
(unsigned char *) &value, 1);
// Set events to handle
XSelectInput(dpy, win, VisibilityChangeMask | FocusChangeMask);
// Draw window
XMapWindow(dpy, win);
// Wait until window stands visible, otherwise will get
// "Error of failed request: BadMatch"
arg.win = win;
arg.notify = VisibilityNotify;
XIfEvent(dpy, &ev, &xevent_handler, (XPointer) &arg);
XSetInputFocus(dpy, win, RevertToNone, CurrentTime);
XSync(dpy, False);
XMoveWindow(dpy, win, x, y);
// Wait until focus lost
arg.win = win;
arg.notify = FocusOut;
XIfEvent(dpy, &ev, &xevent_handler, (XPointer) &arg);
// Why I should handle this event twice?
XIfEvent(dpy, &ev, &xevent_handler, (XPointer) &arg);
// Restore focus
XSetInputFocus(dpy, focused, revert_to, CurrentTime);
XSync(dpy, False);
}
How to check incorrect behavior:
I comment second FocusOut handling. I use two monitors: left and right. I start program in terminal on right monitor. Program opens dummy window on left monitor. When dummy window loses focus another window on left monitor obtains it. Not terminal window on right monitor!
Why xserver sends FocusOut twice? Or it is bug/feature of my window manager?
Without your implementation it is really impossible to know exactly what the driver is for the behavior you are seeing, but the man pages do provide some insight into what may be happening.
This particular behavior may be the result of the last-focus-change time. From this man page:
...The XSetInputFocus() function changes the input focus and the last-focus-change time. It has no effect if the specified time is earlier than the current
last-focus-change time or is later than the current X server time.

How box() is adopting max width after resizing terminal

I don't know how reexecuting the function box() is adopting max width automatically and here is test code :
getmaxyx(stdscr,y,x);
WINDOW *titleWin = update_title_win(NULL,2,x,0,0);
while((ch = getch()) != 27) {
if(ch == KEY_RESIZE) {
update_title_win(titleWin);
}
}
update_title_win() code :
WINDOW* update_title_win(WINDOW *win, int height, int width, int y, int x)
{
if(!win)
win = newwin(height,width,y,x);
box(win,0,0);
refresh();
wrefresh(win);
return win;
}
The ncurses library uses resizeterm to do the adjustments when getch returns KEY_RESIZE. That function will reduce the size of windows if the (terminal/screen) shrinks, but the behavior when the screen increases in size is less apparent.
The manual page for resizeterm does not mention this, but a comment in the source code explains the behavior:
/*
* If we're increasing size, recursively search for windows that have no
* parent, increase those to fit, then increase the contained window, etc.
*/
A window created with newwin has no parent (unlike something made with subwin). Since the titleWin started off with the full width of the screen, as the screen width changes, ncurses will attempt to keep the title window full-width.

cairo / xlib not updating window content

I am trying to learn how to use Cairo 2D drawing library with xlib surfaces.
I wrote a little test program that allows creating multiple windows. Each function may have a custom paint() function that is called regularly to add some graphics content to the window, or redraw it completely if desired. There is also an option to define mouse and key listener. The main routine checks for X events (to delegate them to mouse and key listener) and for timeout for periodic call of those paint() functions.
I tried with the 1.14.6 version of Cairo (that is currently available as package in Ubuntu 16.04), and the latest 1.15.12, but the results are the same.
The expected behavior of this demo is to open 3 windows. One will have random rectangles being added, another one random texts, and the third random circles.
In addition, clicking into windows should produce lines (connecting to mouse click, or randomly), and using arrow keys should draw a red line in the window with circles.
The circles and text seem to show up regularly as expected. All three windows should have white background, but two of them are black. And the worst, the window with rectangles does not get updated much (and it does not matter if it is the first window created or not, it is always the rectangles that do not show up properly).
They are only shown when the focus changes to or from that window - then the remaining rectangles that should have been drawn meanwhile suddenly appear.
I am calling cairo_surface_flush() on the surface of each window after adding any content, but that does not help. I also tried posting XEvents to that window of various kind (such as focus), they arrive, but rectangles do not show up.
Furthermore, even though drawing lines with mouse works fine, drawing line with key arrows suffers from the same problem - it is drawn, but not shown properly.
I am obviously wrong in some of my assumptions about what this library can do, but I am not sure where.
It seems that there are some two competing versions of drawing being shown, since it happens sometimes that one or two rectangles, or pieces of the red line are flashing. Some kind of strange buffering, caching?
It may just be some bug in my program, I do not know.
Another observation - the black background is because drawing white background happens before the window is shown, and thus those cairo_paint calls are somehow discarded. I do not know how to make the window appear earlier, it seems it appears only after some later changes on the screen.
I am stuck on this after a couple of desperate days, could you help me out at least in part, please?
The program is here: test_cairo.c
An example screenshot (with a broken red line drawn by keys, and rectangles not showing up properly): test_cairo.png
To compile (on Ubuntu 16.04 or similar system):
gcc -o test_cairo test_cairo.c -I/usr/include/cairo -lX11 -lcairo
X11 does not retain window content for you. When you get an Expose event, you have to repaint the area described by that event completely.
All three windows should have white background, but two of them are black.
You create your window with XCreateSimpleWindow, so their background attribute is set to black. The X11 server will fill exposed areas with black for you before sending an expose event. Since you do not tell cairo to draw a white background, the black stays.
Try this:
--- test_cairo.c.orig 2018-07-28 09:53:10.000000000 +0200
+++ test_cairo.c 2018-07-29 10:52:43.268867754 +0200
## -63,6 +63,7 ## static gui_mouse_callback mouse_callback
static cairo_t *windows[MAX_GUI_WINDOWS_COUNT];
static cairo_surface_t *surfaces[MAX_GUI_WINDOWS_COUNT];
+static cairo_surface_t *real_surfaces[MAX_GUI_WINDOWS_COUNT];
static Window x11windows[MAX_GUI_WINDOWS_COUNT];
static char *window_names[MAX_GUI_WINDOWS_COUNT];
## -79,7 +80,12 ## long long usec()
void repaint_window(int window_handle)
{
draw_callbacks[window_handle](windows[window_handle]);
- cairo_surface_flush(surfaces[window_handle]);
+
+ cairo_t *cr = cairo_create(real_surfaces[window_handle]);
+ cairo_set_source_surface(cr, surfaces[window_handle], 0, 0);
+ cairo_paint(cr);
+ cairo_destroy(cr);
+ cairo_surface_flush(real_surfaces[window_handle]);
}
int gui_cairo_check_event(int *xclick, int *yclick, int *win)
## -149,7 +155,6 ## void draw_windows_title(int window_handl
sprintf(fullname, "Mikes - %d - [%s]", window_handle, context_names[current_context]);
else
sprintf(fullname, "Mikes - %s - [%s]", window_names[window_handle], context_names[current_context]);
- cairo_surface_flush(surfaces[window_handle]);
XStoreName(dsp, x11windows[window_handle], fullname);
}
## -179,20 +184,17 ## int gui_open_window(gui_draw_callback pa
}
if (window_handle < 0) return -1;
- surfaces[window_handle] = gui_cairo_create_x11_surface(&width, &height, window_handle);
+ real_surfaces[window_handle] = gui_cairo_create_x11_surface(&width, &height, window_handle);
+ surfaces[window_handle] = cairo_surface_create_similar(real_surfaces[window_handle], CAIRO_CONTENT_COLOR, width, height);
windows[window_handle] = cairo_create(surfaces[window_handle]);
mouse_callbacks[window_handle] = 0;
draw_callbacks[window_handle] = paint;
window_update_periods[window_handle] = update_period_in_ms;
window_names[window_handle] = 0;
-
- cairo_surface_flush(surfaces[window_handle]);
cairo_set_source_rgb(windows[window_handle], 1, 1, 1);
cairo_paint(windows[window_handle]);
-
- cairo_surface_flush(surfaces[window_handle]);
draw_callbacks[window_handle](windows[window_handle]);
## -201,7 +203,6 ## int gui_open_window(gui_draw_callback pa
else next_window_update[window_handle] = 0;
draw_windows_title(window_handle);
- cairo_surface_flush(surfaces[window_handle]);
window_in_use[window_handle] = 1;
return window_handle;
## -213,6 +214,7 ## void gui_close_window(int window_handle)
cairo_destroy(windows[window_handle]);
cairo_surface_destroy(surfaces[window_handle]);
+ cairo_surface_destroy(real_surfaces[window_handle]);
window_in_use[window_handle] = 0;
int no_more_windows = 1;
for (int i = 0; i < MAX_GUI_WINDOWS_COUNT; i++)

How do I get a bright white background color with ncurses?

How do I get a bright white background with black text on it in ncurses, similar to the title-bar in nano? All I can seem to achieve despite following the advice in another question (which has to do with getting bright white text on a black background, the opposite of what I want to achieve), is an ugly beige-colored background.
Images:
GNU nano's titlebar, what I want.
What I get with the program below. (Build with gcc -lncursesw -I/usr/include minimal_example.c)
#include <locale.h>
#include <ncurses.h>
int main() {
setlocale(LC_ALL, "");
// Initialize curses library
initscr();
// Enable colors
start_color();
// Attempt recommendation in https://stackoverflow.com/questions/1896162/how-to-get-a-brightwhite-color-in-ncurses and other places on the web
use_default_colors();
// Make the COLOR_PAIR 0xFF refer to a white foreground, black background window.
// Using -1 will not work in my case, because I want the opposite of the default (black text on white bg), not the default (white text on black bg).
init_pair(0xFF, COLOR_BLACK, COLOR_WHITE);
refresh();
// Get our term height and width.
int x;
int y;
// & not required because this is a macro
getmaxyx(stdscr, y, x);
// Create a new window.
// TODO: Resize the window when the term resizes.
WINDOW *window = newwin(y,x,0,0);
// Try some other attributes recommended online, no dice. Putting this after the call to wbkgd() just makes the text look strange, does not change the background.
wattron(window,A_BOLD|A_STANDOUT);
// Set window color.
wbkgd(window, COLOR_PAIR(0xff));
// Draw a nice box around the window.
box(window, 0, 0);
// Write some text.
mvwprintw(window, 1, 1, "背景:不白");
wrefresh(window);
// Wait for keypress to exit.
getch();
// De-initialize ncurses.
endwin();
return 0;
}
I thought that perhaps there was something wrong with my terminal configuration (termite), but I was able to reproduce the problem in xfce4-terminal and xterm, both using the default configurations. The only way to fix this is to set my color7 and color15 to the same color as foreground, which obviously I do not want to do because that is non-standard and I want to distribute the larger application this code is used in.
(xfce4-terminal with the bug)
My recommendation is to define the bright colors (9 to 15) if there are at least 16 colors and can_change_color() returns true. Otherwise fall back to non-bright colors:
#include <stdlib.h>
#include <locale.h>
#include <ncurses.h>
#define PAIR_BW 1
#define BRIGHT_WHITE 15
int main(void) {
int rows, cols;
setlocale(LC_ALL, "");
initscr();
start_color();
use_default_colors();
if (can_change_color() && COLORS >= 16)
init_color(BRIGHT_WHITE, 1000,1000,1000);
if (COLORS >= 16) {
init_pair(PAIR_BW, COLOR_BLACK, BRIGHT_WHITE);
} else {
init_pair(PAIR_BW, COLOR_BLACK, COLOR_WHITE);
}
refresh();
getmaxyx(stdscr, rows, cols);
WINDOW *window = newwin(rows,cols,0,0);
wbkgd(window, COLOR_PAIR(PAIR_BW));
box(window, 0, 0);
mvwprintw(window, 1, 1, "背景:不白");
wrefresh(window);
getch();
endwin();
return EXIT_SUCCESS;
}
This is tested to work in Gnome Terminal 3.18.3 and XTerm 322, and it should work in all color-capable terminals if using ncursesw (although on some weird ones you might still get the non-bright-white background).
Colors in terminal emulators are a bit of a mess. Your problem is that that gray background really is "white" according to your terminal! Check out this table on Wikipedia. See how gray-looking the "White" row is across all the different terminal emulators? What you really want is "bright white", which is beyond the 8 initial colors. The problem is that, according to curses:
Some terminals support more than the eight (8) “ANSI” colors. There
are no standard names for those additional colors.
So you just have to use them by number and hope that everyone's terminal conforms to the tables on Wikipedia (I think most do).
init_pair(0xFF, COLOR_BLACK, COLORS > 15 ? 15 : COLOR_WHITE);
That's all you need, so you can get rid of all that other use_default_colors and A_BOLD stuff.
Old answer:
The curs_color manual says
Note that setting an implicit background color via a color pair affects only character cells that a character write operation explicitly touches.
...
The A_BLINK attribute should in theory cause the background to go bright.
Indeed, if you just change
wbkgd(window, COLOR_PAIR(0xff) | A_BLINK);
You get a bright white background, but only in areas where text was drawn (including the window border). I'm not sure how to get the same effect over the entire window background, but hopefully this can get you started.
This change (assuming TERM=xterm-256color) does what was asked:
> diff -u foo.c foo2.c
--- foo.c 2017-10-06 15:59:56.000000000 -0400
+++ foo2.c 2017-10-06 16:10:11.893529758 -0400
## -7,10 +7,9 ##
initscr();
// Enable colors
start_color();
- // Attempt recommendation in https://stackoverflow.com/questions/1896162/how-to-get-a-brightwhite-color-in-ncurses and other places on the web
- use_default_colors();
- // Make the COLOR_PAIR 0xFF refer to a white foreground, black background window.
- // Using -1 will not work in my case, because I want the opposite of the default (black text on white bg), not the default (white text on black bg).
+ // redefine colors, using the initc capability in TERM=xterm-256color
+ init_color(COLOR_BLACK, 0, 0, 0);
+ init_color(COLOR_WHITE, 999, 999, 999);
init_pair(0xFF, COLOR_BLACK, COLOR_WHITE);
refresh();
// Get our term height and width.
But nano doesn't do that. You might find it helpful to read its source-code, to see that it solves the problem by using the terminal's default colors.

Simple C Program that creates 2 X11 windows

I want to create 2 windows in linux that I'll later draw in from a separate thread. I currently have a non-deterministic bug where the second window that I create sometimes doesn't get created (no errors though).
Here is the code.
static void create_x_window(Display *display, Window *win, int width, int height)
{
int screen_num = DefaultScreen(display);
unsigned long background = WhitePixel(display, screen_num);
unsigned long border = BlackPixel(display, screen_num);
*win = XCreateSimpleWindow(display, DefaultRootWindow(display), /* display, parent */
0,0, /* x, y */
width, height, /* width, height */
2, border, /* border width & colour */
background); /* background colour */
XSelectInput(display, *win, ButtonPressMask|StructureNotifyMask);
XMapWindow(display, *win);
}
int main(void) {
XInitThreads(); // prevent threaded XIO errors
local_display = XOpenDisplay(":0.0");
Window self_win, remote_win;
XEvent self_event, remote_event;
create_x_window(local_display, &remote_win, 640,480);
// this line flushes buffer and blocks so that the window doesn't crash for a reason i dont know yet
XNextEvent(local_display, &remote_event);
create_x_window(local_display, &self_win, 320, 240);
// this line flushes buffer and blocks so that the window doesn't crash for a reason i dont know yet
XNextEvent(local_display, &self_event);
while (1) {
}
return 0;
}
I don't really care for capturing input in the windows, but I found a tutorial that had XSelectInput and XNextEvent (in an event loop) and I was having trouble making this work without either.
It's not a bug, it's a feature. You left out the event loop.
Although you cleverly called XNextEvent twice, the X protocol is asynchronous so the server may still be setting up the actual window while you call XNextEvent, so there is nothing to do.
Tutorial here.

Resources