Skip to content

Fix output garbling and allow clean restart after game exit #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ static int kxo_release(struct inode *inode, struct file *filp)
fast_buf_clear();
}
pr_info("release, current cnt: %d\n", atomic_read(&open_cnt));
attr_obj.end = 48;

return 0;
}
Expand Down
35 changes: 24 additions & 11 deletions xo-user.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ static void listen_keyboard_handler(void)
read_attr ^= 1;
write(attr_fd, buf, 6);
if (!read_attr)
printf("Stopping to display the chess board...\n");
printf("\n\nStopping to display the chess board...\n");
break;
case 17: /* Ctrl-Q */
read(attr_fd, buf, 6);
buf[4] = '1';
read_attr = false;
end_attr = true;
write(attr_fd, buf, 6);
printf("Stopping the kernel space tic-tac-toe game...\n");
printf("\n\nStopping the kernel space tic-tac-toe game...\n");
break;
}
}
Expand All @@ -91,32 +91,45 @@ int main(int argc, char *argv[])
int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);

char display_buf[DRAWBUFFER_SIZE];
char display_buf[DRAWBUFFER_SIZE]; // Buffer for storing board data read
// from the device.

fd_set readset;
int device_fd = open(XO_DEVICE_FILE, O_RDONLY);
int max_fd = device_fd > STDIN_FILENO ? device_fd : STDIN_FILENO;
read_attr = true;
end_attr = false;
int device_fd =
open(XO_DEVICE_FILE, O_RDONLY); // Open the device node to transfer
// data from kernel to user space.
int max_fd = device_fd > STDIN_FILENO
? device_fd
: STDIN_FILENO; // The maximum file descriptor required.
read_attr = true; // true: Continue reading the board
end_attr = false; // true: Terminate the main loop

while (!end_attr) {
FD_ZERO(&readset);
FD_SET(STDIN_FILENO, &readset);
FD_SET(device_fd, &readset);

FD_ZERO(&readset); // Clear readset
FD_SET(
STDIN_FILENO,
&readset); // Add the file descriptor for standard input to readset
FD_SET(device_fd,
&readset); // Add the file descriptor for the device to readset.

// Wait for any event to occur
int result = select(max_fd + 1, &readset, NULL, NULL, NULL);
if (result < 0) {
printf("Error with select system call\n");
exit(1);
}

// Handle keyboard input events (Ctrl-P or Ctrl-Q)
if (FD_ISSET(STDIN_FILENO, &readset)) {
FD_CLR(STDIN_FILENO, &readset);
listen_keyboard_handler();

// Display the board
} else if (read_attr && FD_ISSET(device_fd, &readset)) {
FD_CLR(device_fd, &readset);
printf("\033[H\033[J"); /* ASCII escape code to clear the screen */
read(device_fd, display_buf, DRAWBUFFER_SIZE);
display_buf[DRAWBUFFER_SIZE - 1] = '\0';
printf("%s", display_buf);
}
}
Expand Down