forked from adamk33n3r/GoBorderless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
230 lines (201 loc) · 5.2 KB
/
main.go
File metadata and controls
230 lines (201 loc) · 5.2 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"fmt"
"path/filepath"
"strings"
"time"
"unsafe"
"slices"
"github.com/lxn/win"
"golang.org/x/sys/windows"
)
const (
APP_NAME = "GoBorderless"
)
type Window struct {
hwnd win.HWND
title string
exePath string
}
func (w Window) String() string {
return fmt.Sprintf("%s | %s", w.title, w.exePath)
}
var monitors []Monitor
var chWindowList = make(chan []Window) // Channel to send window list updates
var ALWAYS_HIDDEN_PROCESSESS = []string{
// Skip self
strings.ToLower(APP_NAME),
// Skip other borderless apps
"borderlessgaming",
"nomoreborder",
// Inspired by BorderlessGaming
// Skip Windows core system processes
"csrss",
"smss",
"lsass",
"wininit",
"svchost",
"services",
"winlogon",
"dwm",
"explorer",
"taskmgr",
"mmc",
"rundll32",
"vcredist_x86",
"vcredist_x64",
"msiexec",
// Skip common video streaming software
"xsplit",
"obs64",
// Skip common web browsers
"iexplore",
"firefox",
"chrome",
"safari",
"msedge",
// Skip launchers/misc.
"iw4 console",
"steam",
"steamwebhelper",
"origin",
"uplay",
}
var MUTEX windows.Handle
func getWindowData(hwnds []win.HWND) []Window {
windows := make([]Window, 0)
for _, hwnd := range hwnds {
// Check if the window is visible; skip if not
if !isVisible(uintptr(hwnd)) {
continue
}
title := getWindowTitle(uintptr(hwnd))
if title == "" {
continue
}
// Filter out windows with no size
rect := getWindowRect(hwnd)
if rect.Left == rect.Right && rect.Top == rect.Bottom {
continue
}
var pid uint32
win.GetWindowThreadProcessId(win.HWND(hwnd), &pid)
processPath, err := getProcessPath(pid)
if err != nil {
continue
}
processName := filepath.Base(processPath)
// Filter out some stuff you probably never want to see
if slices.Contains(ALWAYS_HIDDEN_PROCESSESS, strings.TrimSuffix(strings.ToLower(processName), filepath.Ext(processName))) {
continue
}
if slices.Contains(ALWAYS_HIDDEN_PROCESSESS, strings.ToLower(title)) {
continue
}
windows = append(windows, Window{hwnd: hwnd, title: title, exePath: processPath})
}
return windows
}
func enumWindowsCallback(hwnd uintptr, lparam uintptr) uintptr {
restoredPtr := (*[]win.HWND)(unsafe.Pointer(lparam)) //nolint:govet
*restoredPtr = append(*restoredPtr, win.HWND(hwnd))
return 1 // continue enumeration
}
func matchWindow(win Window, appSetting AppSetting) bool {
switch appSetting.MatchType {
case MatchWindowTitle:
return win.title == appSetting.WindowName
case MatchExePath:
return win.exePath == appSetting.ExePath
case MatchBoth:
return win.title == appSetting.WindowName && win.exePath == appSetting.ExePath
case MatchEither:
return win.title == appSetting.WindowName || win.exePath == appSetting.ExePath
default:
return false
}
}
func getPrimaryMonitor() Monitor {
for _, monitor := range monitors {
if monitor.isPrimary {
return monitor
}
}
return monitors[0]
}
type EnumWindowsWrapper struct {
windows []win.HWND
}
func EnumWindows() []win.HWND {
enumWindowsWrapper := &EnumWindowsWrapper{
windows: make([]win.HWND, 0),
}
enumWindows(enumWindowsCallback, unsafe.Pointer(enumWindowsWrapper))
return enumWindowsWrapper.windows
}
func scanWindows(settings *Settings) {
for {
allWindows := EnumWindows()
windowData := getWindowData(allWindows)
chWindowList <- windowData // Update global window list
for appSettingIdx, appSetting := range settings.Apps {
if !appSetting.AutoApply {
continue
}
for _, win := range windowData {
if matchWindow(win, appSetting) {
if !isBorderless(win) {
originalRect := getWindowRect(win.hwnd)
appSetting.PreWidth = int32(originalRect.Right - originalRect.Left)
appSetting.PreHeight = int32(originalRect.Bottom - originalRect.Top)
appSetting.PreOffsetX = int32(originalRect.Left)
appSetting.PreOffsetY = int32(originalRect.Top)
settings.Apps[appSettingIdx] = appSetting
settings.Save()
}
makeBorderless(win, appSetting)
break
}
}
}
time.Sleep(time.Second * 1) // Sleep for 1 second before next scan
}
}
func main() {
settings, err := loadSettings()
if err != nil {
fmt.Println("Error loading settings:", err)
backUpSettingsFile()
}
MUTEX, err = createMutex(APP_NAME + "_InstanceMutex")
if err == windows.ERROR_ALREADY_EXISTS {
fmt.Println("Another instance of the app is already running, bringing other instance to foreground.")
if appNamePtr, err := windows.UTF16PtrFromString(APP_NAME); err == nil {
existingWin := win.FindWindow(nil, appNamePtr)
win.SetForegroundWindow(existingWin)
}
return
}
monitors = getMonitors()
primaryMonitor := getPrimaryMonitor()
if settings.Defaults.Monitor == 0 {
settings.Defaults.Monitor = primaryMonitor.number
}
if settings.Defaults.Width == 0 {
settings.Defaults.Width = primaryMonitor.width
}
if settings.Defaults.Height == 0 {
settings.Defaults.Height = primaryMonitor.height
}
settings.Save()
fmt.Println(settings)
for _, mon := range monitors {
fmt.Printf("Monitor %d\n", mon.number)
fmt.Printf(" Resolution: %dx%d\n", mon.width, mon.height)
fmt.Printf(" Position: (%d, %d)\n", mon.left, mon.top)
}
go scanWindows(settings)
fmt.Println("Building app")
fyneApp := buildApp(settings)
fyneApp.Run()
}