diff --git a/main.c b/main.c index 731cefa..672c608 100644 --- a/main.c +++ b/main.c @@ -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; } diff --git a/xo-user.c b/xo-user.c index 4e567de..aabb92c 100644 --- a/xo-user.c +++ b/xo-user.c @@ -67,7 +67,7 @@ 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); @@ -75,7 +75,7 @@ static void listen_keyboard_handler(void) 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; } } @@ -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); } }