Skip to content

Commit ef02c14

Browse files
committed
Enable termination of the game
Previous version of implementation didn't have the ability to stop the whole main loop even we press Ctrl+Q, we enable this feature by adding a boolean variable to determine wheter the game's over or not, if yes then we'll jump out of the main loop as well.
1 parent 2b52d81 commit ef02c14

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

kmldrv-user.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ static void enableRawMode(void)
5353
}
5454

5555
static bool read_attr;
56+
static bool end_attr;
5657

5758
static void listen_keyboard_handler(void)
5859
{
@@ -67,12 +68,14 @@ static void listen_keyboard_handler(void)
6768
buf[0] = (buf[0] - '0') ? '0' : '1';
6869
read_attr ^= 1;
6970
write(attr_fd, buf, 6);
70-
printf("Stopping to display the chess board...\n");
71+
if (!read_attr)
72+
printf("Stopping to display the chess board...\n");
7173
break;
7274
case 17:
7375
read(attr_fd, buf, 6);
7476
buf[4] = '1';
7577
read_attr = false;
78+
end_attr = true;
7679
write(attr_fd, buf, 6);
7780
printf("Stopping the kernel space tic-tac-toe game...\n");
7881
break;
@@ -119,8 +122,9 @@ int main(int argc, char *argv[])
119122
int device_fd = open(KMLDRV_DEVICE_FILE, O_RDONLY);
120123
int max_fd = device_fd > STDIN_FILENO ? device_fd : STDIN_FILENO;
121124
read_attr = true;
125+
end_attr = false;
122126

123-
while (1) {
127+
while (!end_attr) {
124128
FD_ZERO(&readset);
125129
FD_SET(STDIN_FILENO, &readset);
126130
FD_SET(device_fd, &readset);

0 commit comments

Comments
 (0)