-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasker.go
executable file
·142 lines (115 loc) · 2.79 KB
/
tasker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Package main provides ...
package main
import (
"fmt"
"syscall"
"unsafe"
//"io/ioutil"
//"github.com/fvbock/endless"
"github.com/gin-gonic/gin"
//"log"
"net/http"
// "os"
// "os/exec"
)
const (
MOUSE_X = 1000
MOUSE_Y = 1000
M_POINT = ( MOUSE_X & 0xFFFFFFFF ) | ( MOUSE_Y << 32 )
VPC_SOURCE_SELECT = '\x60'
WINDOWS_M = 15
LINUX_M = 17
WINDOWS_PARAM = "windows"
LINUX_PARAM = "linux"
)
var (
user32, _ = syscall.LoadLibrary("User32.dll")
dxva2, _ = syscall.LoadLibrary("dxva2.dll")
monitorFromPoint, _ = syscall.GetProcAddress(user32, "MonitorFromPoint")
GetPhysicalMonitorsFromHMONITOR, _ = syscall.GetProcAddress(dxva2, "GetPhysicalMonitorsFromHMONITOR")
SetVCPFeature, _ = syscall.GetProcAddress(dxva2, "SetVCPFeature")
DestroyPhysicalMonitor, _ = syscall.GetProcAddress(dxva2, "DestroyPhysicalMonitor")
)
func switchMonitorHandler(c *gin.Context) {
os := c.Param("os")
switch os {
case WINDOWS_PARAM:
go setMonitorInputSource(WINDOWS_M)
case LINUX_PARAM:
go setMonitorInputSource(LINUX_M)
}
//fmt.Printf("Received command %s", command)
c.String(http.StatusOK, fmt.Sprintf("monitor switched to %s!", os))
}
func getMonitorHandle() (result uintptr) {
var nargs uintptr = 2
ret, _, callErr := syscall.Syscall(
uintptr(monitorFromPoint),
nargs,
uintptr(M_POINT),
uintptr(1),
0)
if callErr != 0 {
abort("Call getMonitorHandle", callErr)
}
res := uintptr(ret)
result = getPhysicalMonitor(res)
return
}
func getPhysicalMonitor(handle uintptr) (result uintptr) {
b := make([]byte, 256)
var nargs uintptr = 3
_, _, callErr := syscall.Syscall(
uintptr(GetPhysicalMonitorsFromHMONITOR),
nargs,
handle,
uintptr(1),
uintptr(unsafe.Pointer(&b[0])))
if callErr != 0 {
abort("Call getPhysicalMonitor", callErr)
}
result = uintptr(b[0])
return
}
func setMonitorInputSource(source int) {
mHandle := getMonitorHandle()
var nargs uintptr = 3
_, _, callErr := syscall.Syscall(
uintptr(SetVCPFeature),
nargs,
uintptr(mHandle),
VPC_SOURCE_SELECT,
uintptr(source))
if callErr != 0 {
abort("Call SetVCPFeature", callErr)
}
destroyPhysicalMonitor(mHandle)
}
func destroyPhysicalMonitor(monitor uintptr) {
var nargs uintptr = 1
_, _, callErr := syscall.Syscall(
uintptr(DestroyPhysicalMonitor),
nargs,
monitor,
0,
0)
if callErr != 0 {
abort("Call destroyPhysicalMonitor", callErr)
}
}
func abort(funcname string, err error) {
panic(fmt.Sprintf("%s failed: %v", funcname, err))
}
func main() {
defer syscall.FreeLibrary(user32)
defer syscall.FreeLibrary(dxva2)
router := gin.Default()
router.Use(gin.Logger())
router.Use(gin.Recovery())
monitor := router.Group("/monitor")
{
monitor.GET("/switch/:os", switchMonitorHandler)
}
router.Run(":7500")
//endless.ListenAndServe(":7500", router)
}