ncurses window 中的光标不在所需位置
cursor in ncurses window not in the required position
我正在使用 ncurses 构建我自己的终端。我的主要 window 包含其他 5 个 windows。我在下面放置的 window 是提示面板。我希望在那里输入文本,并且我希望能够在那里输入文本。我已使用 wmove 定位光标,但光标仍位于 window 的左上角。同样,当我输入文本时,一切都会发生变化。
这是我的代码的一部分;
WINDOW *mainwin, *date_time, *alarm, *colour, *output, *prompt_win;
/* Initialize ncurses */
mainwin = initscr();
if (!mainwin) {
fprintf(stderr, "Error initialising ncurses.\n");
exit(EXIT_FAILURE);
}
/* Make our child window, and add a border and some text to it. */
date_time = subwin(mainwin, 5, 35, 0, 0);
box(date_time, 0, 0);
alarm = subwin(mainwin, 5, 35, 0, 35);
box(alarm, 0, 0);
colour = subwin(mainwin, 5, 5, 0, 70);
box(colour, 0, 0);
output = subwin(mainwin, 10, 75, 5, 0);
box(output, 0, 0);
prompt_win = subwin(mainwin, 7, 75, 15, 0);
box(prompt_win, 0, 0);
pid_t pid = fork();
if (pid < 0) {
perror("Error: Fork Failed");
} else if (pid == 0) {
/* Alarm & date */
pid_t child_pid = getpid();
pid_t pid2 = fork();
if (pid2 < 0) {
perror("Error: Fork Failed");
} else if (pid2 == 0) {
/* Date */
pid_t child_pid2 = getpid();
while (1) {
wnoutrefresh(date_time);
getTime(date_time);
/* doupdate() */
}
} else {
/* Alarm */
wnoutrefresh(alarm);
wnoutrefresh(colour);
/* doupdate() */
}
/* _exit(0); */
} else {
/* Output & prompt */
/* Display and enter text */
/* prints "OK>" */
mvwprintw(prompt_win,curse_loc,1,prompt_arr[0]);
wnoutrefresh(prompt_win);
/* gets input */
getstr(buffer[curse_loc]);
strcpy(current_cmd,buffer[curse_loc]);
refresh();
while (strcmp(current_cmd,"exit")) {
wmove(prompt_win, curse_loc, sizeof(prompt_arr));
refresh();
mvwin(prompt_win, curse_loc, curse_loc);
wnoutrefresh(prompt_win);
curse_loc++;
/* again displays "OK>" */
mvwprintw(prompt_win, curse_loc, 1, prompt_arr[0]);
/* gets input */
getstr(buffer[curse_loc]);
strcpy(current_cmd, buffer[curse_loc]);
doupdate();
}
endwin();
delwin(prompt_win);
delwin(output);
delwin(colour);
delwin(alarm);
delwin(date_time);
delwin(mainwin);
refresh();
}
有几个问题:
- 对 read 个字符(从键盘)的唯一调用是
getstr
,它(使用 stdscr
作为其 window)停止,光标位于 stdscr
中的当前(例如,wmove
)位置。如果您希望光标停在另一个 window 中,您应该使用 wgetstr
,将那个 window 指针作为参数传递。
- 程序正试图从两个不同的进程写入屏幕。由于这两个进程不知道对方做了什么更改(也不知道对方向屏幕发送了什么),因此结果将不可预测。
我正在使用 ncurses 构建我自己的终端。我的主要 window 包含其他 5 个 windows。我在下面放置的 window 是提示面板。我希望在那里输入文本,并且我希望能够在那里输入文本。我已使用 wmove 定位光标,但光标仍位于 window 的左上角。同样,当我输入文本时,一切都会发生变化。
这是我的代码的一部分;
WINDOW *mainwin, *date_time, *alarm, *colour, *output, *prompt_win;
/* Initialize ncurses */
mainwin = initscr();
if (!mainwin) {
fprintf(stderr, "Error initialising ncurses.\n");
exit(EXIT_FAILURE);
}
/* Make our child window, and add a border and some text to it. */
date_time = subwin(mainwin, 5, 35, 0, 0);
box(date_time, 0, 0);
alarm = subwin(mainwin, 5, 35, 0, 35);
box(alarm, 0, 0);
colour = subwin(mainwin, 5, 5, 0, 70);
box(colour, 0, 0);
output = subwin(mainwin, 10, 75, 5, 0);
box(output, 0, 0);
prompt_win = subwin(mainwin, 7, 75, 15, 0);
box(prompt_win, 0, 0);
pid_t pid = fork();
if (pid < 0) {
perror("Error: Fork Failed");
} else if (pid == 0) {
/* Alarm & date */
pid_t child_pid = getpid();
pid_t pid2 = fork();
if (pid2 < 0) {
perror("Error: Fork Failed");
} else if (pid2 == 0) {
/* Date */
pid_t child_pid2 = getpid();
while (1) {
wnoutrefresh(date_time);
getTime(date_time);
/* doupdate() */
}
} else {
/* Alarm */
wnoutrefresh(alarm);
wnoutrefresh(colour);
/* doupdate() */
}
/* _exit(0); */
} else {
/* Output & prompt */
/* Display and enter text */
/* prints "OK>" */
mvwprintw(prompt_win,curse_loc,1,prompt_arr[0]);
wnoutrefresh(prompt_win);
/* gets input */
getstr(buffer[curse_loc]);
strcpy(current_cmd,buffer[curse_loc]);
refresh();
while (strcmp(current_cmd,"exit")) {
wmove(prompt_win, curse_loc, sizeof(prompt_arr));
refresh();
mvwin(prompt_win, curse_loc, curse_loc);
wnoutrefresh(prompt_win);
curse_loc++;
/* again displays "OK>" */
mvwprintw(prompt_win, curse_loc, 1, prompt_arr[0]);
/* gets input */
getstr(buffer[curse_loc]);
strcpy(current_cmd, buffer[curse_loc]);
doupdate();
}
endwin();
delwin(prompt_win);
delwin(output);
delwin(colour);
delwin(alarm);
delwin(date_time);
delwin(mainwin);
refresh();
}
有几个问题:
- 对 read 个字符(从键盘)的唯一调用是
getstr
,它(使用stdscr
作为其 window)停止,光标位于stdscr
中的当前(例如,wmove
)位置。如果您希望光标停在另一个 window 中,您应该使用wgetstr
,将那个 window 指针作为参数传递。 - 程序正试图从两个不同的进程写入屏幕。由于这两个进程不知道对方做了什么更改(也不知道对方向屏幕发送了什么),因此结果将不可预测。