The live data also works now, but I have some refactoring to do
This commit is contained in:
+38
-87
@@ -1,108 +1,59 @@
|
||||
//go:build windows && cgo
|
||||
// +build windows,cgo
|
||||
|
||||
// I should rename winutils to something else but what this package does
|
||||
// is interface some windows stuff that we need for the:
|
||||
// - Broadcast Channel
|
||||
// - Valid Data Event windows thing
|
||||
package winutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"ibtReader/sharedMem"
|
||||
"io"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
const (
|
||||
WAIT_OBJECT_0 = 0
|
||||
WAIT_TIMEOUT = 258
|
||||
)
|
||||
type Reader interface {
|
||||
io.Reader
|
||||
io.ReaderAt
|
||||
io.ReadCloser
|
||||
}
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
user32DLL *windows.LazyDLL = nil
|
||||
)
|
||||
type IRacingWinUtils struct {
|
||||
Utils *utils
|
||||
}
|
||||
|
||||
// openEvent opens a windows.Handle for a given event
|
||||
func OpenEvent(eventName string) (windows.Handle, error) {
|
||||
name, err := windows.UTF16PtrFromString(eventName)
|
||||
func Init() (*IRacingWinUtils, error) {
|
||||
u, err := newUtils()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return nil, err
|
||||
}
|
||||
return &IRacingWinUtils{u}, nil
|
||||
}
|
||||
|
||||
handle, err := windows.OpenEvent(windows.SYNCHRONIZE, false, name)
|
||||
func (u *IRacingWinUtils) Close() {
|
||||
u.Utils.Close()
|
||||
}
|
||||
|
||||
// OpenMemMap returns a Reader interface that can be used to read the data
|
||||
// No need to encapsulate it
|
||||
func OpenMemMap(path string, size uint32) (Reader, error) {
|
||||
file, err := sharedMem.Open(path, size)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("error opening event %s: %w", eventName, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return handle, nil
|
||||
return file, nil
|
||||
}
|
||||
|
||||
// closeEvent closes a given windows.Handle
|
||||
func CloseEvent(h windows.Handle) {
|
||||
windows.CloseHandle(h)
|
||||
// OpenWinEvent will open the named windows event
|
||||
func (u *IRacingWinUtils) OpenWinEvent(name string) error {
|
||||
return u.Utils.OpenEvent(name)
|
||||
}
|
||||
|
||||
// openEvent waits for a good response for some given time
|
||||
func CheckValidDataEvent(handle windows.Handle, timeout time.Duration) bool {
|
||||
t0 := time.Now().UnixNano()
|
||||
timeoutInt := uint32(timeout / time.Millisecond)
|
||||
|
||||
result, err := windows.WaitForSingleObject(handle, timeoutInt)
|
||||
if err != nil {
|
||||
remaining := timeoutInt - uint32((time.Now().UnixNano()-t0)/1000000)
|
||||
if remaining > 0 {
|
||||
time.Sleep(time.Duration(remaining) * time.Millisecond)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Check the result of the wait
|
||||
if result == WAIT_OBJECT_0 {
|
||||
return true
|
||||
} else if result == WAIT_TIMEOUT {
|
||||
return false
|
||||
}
|
||||
|
||||
return false
|
||||
// OpenWinEvent will open the broadcast channel
|
||||
func (u *IRacingWinUtils) OpenBroadcastChannel(name string) error {
|
||||
return u.Utils.OpenBroadcastChannel(name)
|
||||
}
|
||||
|
||||
// loadUser32DLL loads the user32.dll which is used to create some processes
|
||||
func loadUser32DLL() {
|
||||
user32DLL = windows.NewLazyDLL("user32.dll")
|
||||
}
|
||||
|
||||
// OpenBroadcastChannel opens up a broadcast channel to send commands to iracing
|
||||
func OpenBroadcastChannel(name string) (uintptr, error) {
|
||||
if user32DLL == nil {
|
||||
once.Do(loadUser32DLL)
|
||||
}
|
||||
registerWindowsMessageW := user32DLL.NewProc("RegisterWindowMessageW")
|
||||
|
||||
msgPtr, err := windows.UTF16PtrFromString(name)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
ret, _, err := registerWindowsMessageW.Call(uintptr(unsafe.Pointer(msgPtr)))
|
||||
if ret == 0 {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// SendBroadcastMessage sends a message trough the broadcast channel
|
||||
func SendBroadcastMessage(id, p1, p2 uintptr) error {
|
||||
if user32DLL == nil {
|
||||
once.Do(loadUser32DLL)
|
||||
}
|
||||
|
||||
sendMsg := user32DLL.NewProc("SendNotifyMessageW")
|
||||
ret, _, err := sendMsg.Call(0xffff, id, p1, p2)
|
||||
|
||||
if ret == 1 {
|
||||
return nil
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
// CheckValidDataEvent checks if our windows even is telling us we are good to go
|
||||
func (u *IRacingWinUtils) CheckValidDataEvent(timeout time.Duration) bool {
|
||||
return u.Utils.CheckValidDataEvent(timeout)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
//go:build windows && cgo
|
||||
// +build windows,cgo
|
||||
|
||||
package winutils
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
const (
|
||||
WAIT_OBJECT_0 = 0
|
||||
WAIT_TIMEOUT = 258
|
||||
)
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
type utils struct {
|
||||
user32DLL *windows.LazyDLL
|
||||
wEvent *windows.Handle
|
||||
wBroadcastChn uintptr
|
||||
}
|
||||
|
||||
// INITIALIZATION
|
||||
func newUtils() (*utils, error) {
|
||||
return &utils{
|
||||
user32DLL: openUser32DLL(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (u *utils) Close() {
|
||||
closeEvent(u.wEvent)
|
||||
// Do we need to unload the user32DLL ???
|
||||
// Do we need to close the broadcast channel ???
|
||||
}
|
||||
|
||||
// openEvent opens a windows.Handle for a given event
|
||||
func (u *utils) OpenEvent(eventName string) error {
|
||||
name, err := windows.UTF16PtrFromString(eventName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
event, err := windows.OpenEvent(windows.SYNCHRONIZE, false, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
u.wEvent = &event
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// loadUser32DLL loads the user32.dll which is used to create some processes
|
||||
func openUser32DLL() *windows.LazyDLL {
|
||||
return windows.NewLazyDLL("user32.dll")
|
||||
}
|
||||
|
||||
// OpenBroadcastChannel opens up a broadcast channel to send commands to iracing
|
||||
func (u *utils) OpenBroadcastChannel(name string) error {
|
||||
registerWindowsMessageW := u.user32DLL.NewProc("RegisterWindowMessageW")
|
||||
|
||||
msgPtr, err := windows.UTF16PtrFromString(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ret, _, err := registerWindowsMessageW.Call(uintptr(unsafe.Pointer(msgPtr)))
|
||||
if ret == 0 {
|
||||
return err
|
||||
}
|
||||
u.wBroadcastChn = ret
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// INITIALIZATION
|
||||
|
||||
// closeEvent closes a given windows.Handle
|
||||
func closeEvent(h *windows.Handle) {
|
||||
windows.CloseHandle(*h)
|
||||
}
|
||||
|
||||
// openEvent waits for a good response for some given time
|
||||
func (u *utils) CheckValidDataEvent(timeout time.Duration) bool {
|
||||
t0 := time.Now().UnixNano()
|
||||
timeoutInt := uint32(timeout / time.Millisecond)
|
||||
|
||||
result, err := windows.WaitForSingleObject(*u.wEvent, timeoutInt)
|
||||
if err != nil {
|
||||
remaining := timeoutInt - uint32((time.Now().UnixNano()-t0)/1000000)
|
||||
if remaining > 0 {
|
||||
time.Sleep(time.Duration(remaining) * time.Millisecond)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Check the result of the wait
|
||||
if result == WAIT_OBJECT_0 {
|
||||
return true
|
||||
} else if result == WAIT_TIMEOUT {
|
||||
return false
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// SendBroadcastMessage sends a message trough the broadcast channel
|
||||
func (u *utils) SendBroadcastMessage(id, p1, p2 uintptr) error {
|
||||
sendMsg := u.user32DLL.NewProc("SendNotifyMessageW")
|
||||
ret, _, err := sendMsg.Call(0xffff, id, p1, p2)
|
||||
|
||||
if ret == 1 {
|
||||
return nil
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user