-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This pull-request DROPS SUPPORT FOR WINDOWS, but adds the ability to detect when there is pending console input ready to be read. This closes #117. I've tested on Linux, I think the other platforms are _probably_ okay. But who knows? Feedback most welcome. I guess it will come if it affects people if this is broken then playing zork will fail. If this works we've got access to more console-based games.
- Loading branch information
Showing
12 changed files
with
229 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//go:build darwin | ||
|
||
package consolein | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
) | ||
|
||
// canSelect contains a platform-specific implementation of code that tries to use | ||
// SELECT to read from STDIN. | ||
func canSelect() bool { | ||
|
||
var readfds syscall.FdSet | ||
|
||
fd := os.Stdin.Fd() | ||
readfds.Bits[fd/64] |= 1 << (fd % 64) | ||
|
||
// See if input is pending, for a while. | ||
err := syscall.Select(1, &readfds, nil, nil, &syscall.Timeval{Usec: 200}) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//go:+build freebsd | ||
|
||
package consolein | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
) | ||
|
||
// fdget returns index and offset of fd in fds. | ||
func fdget(fd int, fds *syscall.FdSet) (index, offset int) { | ||
index = fd / (syscall.FD_SETSIZE / len(fds.X__fds_bits)) % len(fds.X__fds_bits) | ||
offset = fd % (syscall.FD_SETSIZE / len(fds.X__fds_bits)) | ||
return | ||
} | ||
|
||
// fdset implements FD_SET macro. | ||
func fdset(fd int, fds *syscall.FdSet) { | ||
idx, pos := fdget(fd, fds) | ||
fds.X__fds_bits[idx] = 1 << uint(pos) | ||
} | ||
|
||
// canSelect contains a platform-specific implementation of code that tries to use | ||
// SELECT to read from STDIN. | ||
func canSelect() bool { | ||
|
||
var readfds syscall.FdSet | ||
|
||
fdset(int(os.Stdin.Fd()), &readfds) | ||
|
||
// See if input is pending, for a while. | ||
err := syscall.Select(1, &readfds, nil, nil, &syscall.Timeval{Usec: 200}) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//go:build linux | ||
|
||
package consolein | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
) | ||
|
||
// canSelect contains a platform-specific implementation of code that tries to use | ||
// SELECT to read from STDIN. | ||
func canSelect() bool { | ||
|
||
var readfds syscall.FdSet | ||
|
||
fd := os.Stdin.Fd() | ||
readfds.Bits[fd/64] |= 1 << (fd % 64) | ||
|
||
// See if input is pending, for a while. | ||
nRead, err := syscall.Select(1, &readfds, nil, nil, &syscall.Timeval{Usec: 200}) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return (nRead > 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//go:build netbsd | ||
|
||
package consolein | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
) | ||
|
||
// canSelect contains a platform-specific implementation of code that tries to use | ||
// SELECT to read from STDIN. | ||
func canSelect() bool { | ||
|
||
var readfds syscall.FdSet | ||
|
||
fd := os.Stdin.Fd() | ||
readfds.Bits[fd/64] |= 1 << (fd % 64) | ||
|
||
// See if input is pending, for a while. | ||
err := syscall.Select(1, &readfds, nil, nil, &syscall.Timeval{Usec: 200}) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//go:build openbsd | ||
|
||
package consolein | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
) | ||
|
||
// canSelect contains a platform-specific implementation of code that tries to use | ||
// SELECT to read from STDIN. | ||
func canSelect() bool { | ||
|
||
var readfds syscall.FdSet | ||
|
||
fd := os.Stdin.Fd() | ||
readfds.Bits[fd/64] |= 1 << (fd % 64) | ||
|
||
// See if input is pending, for a while. | ||
err := syscall.Select(1, &readfds, nil, nil, &syscall.Timeval{Usec: 200}) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters