-
Notifications
You must be signed in to change notification settings - Fork 1
/
purego.go
605 lines (569 loc) · 25.9 KB
/
purego.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
package webkitgtk
import (
"errors"
"fmt"
"github.com/ebitengine/purego"
"os"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strings"
"time"
)
type (
ptr uintptr
gsListPtr *gsList
webkitSettingsPtr uintptr
webviewPtr uintptr
windowPtr uintptr
userContentManagerPtr uintptr
gsList struct {
data ptr
next *gsList
}
gList struct {
data ptr
next *gList
prev *gList
}
gdkGeometry struct {
minWidth int32
minHeight int32
maxWidth int32
maxHeight int32
baseWidth int32
baseHeight int32
widthInc int32
heightInc int32
padding int32
minAspect float64
maxAspect float64
GdkGravity int32
}
)
var (
nilRadioGroup gsListPtr = nil
)
type gError struct {
domain uint32
code int
message string
}
func (gErr *gError) toError(msg string) error {
if gErr == nil && gErr.message == "" {
return nil
}
msg += ": " + gErr.message
lib.g.ErrorFree(gErr)
return errors.New(msg)
}
const (
gSourceRemove int = 0
gdkHintMinSize = 1 << 1
gdkHintMaxSize = 1 << 2
gdkWindowStateIconified = 1 << 1
gdkWindowStateMaximized = 1 << 2
gdkWindowStateFullscreen = 1 << 4
gtkOrientationVertical = 1
)
var libs = [][]string{
{"gtk-3", "webkit2gtk-4.1"},
{"gtk-4", "webkitgtk-6.0"},
}
var lib struct {
Target string
Version int
GTK uintptr
Webkit uintptr
g struct {
ApplicationHold func(ptr)
ApplicationQuit func(ptr)
ApplicationRegister func(ptr, ptr, ptr)
ApplicationActivate func(ptr)
GetApplicationName func() string
ApplicationIdIsValid func(string) bool
ApplicationRelease func(ptr)
ApplicationRun func(ptr, int, []string) int
BytesNewStatic func(uintptr, int) uintptr
BytesUnref func(uintptr)
Free func(ptr)
IdleAdd func(uintptr)
ObjectRef func(ptr)
ObjectRefSink func(ptr)
ObjectUnref func(ptr)
ObjectSet func(ptr, string, ptr)
ObjectGet func(ptr, string, ptr)
SignalConnectData func(ptr, string, uintptr, ptr, bool, int) int
SignalConnectObject func(ptr, string, ptr, ptr, int) uint
SignalHandlerBlock func(ptr, uint)
SignalHandlerUnblock func(ptr, uint)
ThreadSelf func() uint64
InputStreamClose func(ptr, ptr, *gError) bool
InputStreamReadAll func(ptr, ptr, int, *int, ptr, *gError) bool
ErrorFree func(*gError)
UnixInputStreamNew func(int, bool) ptr
ErrorNewLiteral func(uint32, string, int, string) *gError
QuarkFromString func(string) ptr
BuildFilename func(...string) ptr
GetHomeDir func() string
RefStringNew func(string) ptr
RefStringRelease func(ptr)
CancellableNew func() ptr
CancellableCancel func(ptr)
CancellableIsCancelled func(ptr) bool
}
gdk struct {
DisplayGetMonitor func(ptr, int) ptr
DisplayGetMonitorAtWindow func(ptr, windowPtr) ptr
DisplayGetNMonitors func(ptr) int
MonitorGetGeometry func(ptr, ptr) ptr
MonitorGetScaleFactor func(ptr) int
MonitorIsPrimary func(ptr) int
PixbufNewFromBytes func(uintptr, int, int, int, int, int, int) ptr
RgbaParse func(ptr, string) bool
ScreenGetRgbaVisual func(ptr) ptr
ScreenIsComposited func(ptr) int
WindowGetState func(windowPtr) int
WindowGetDisplay func(ptr) ptr
}
gtk struct {
ApplicationNew func(string, uint) ptr
ApplicationGetActiveWindow func(ptr) ptr
ApplicationGetWindows func(ptr) *gList
ApplicationWindowNew func(ptr) windowPtr
BoxNew func(int, int) ptr
BoxPackStart func(ptr, ptr, int, int, int)
CheckMenuItemGetActive func(ptr) int
CheckMenuItemNewWithLabel func(string) ptr
CheckMenuItemSetActive func(ptr, int)
ContainerAdd func(windowPtr, ptr)
CssProviderLoadFromData func(ptr, string, int, ptr)
CssProviderNew func() ptr
DialogAddButton func(windowPtr, string, int)
DialogGetContentArea func(windowPtr) windowPtr
DialogRun func(windowPtr) int
DialogSetDefaultResponse func(windowPtr, int)
DragDestSet func(webviewPtr, uint, ptr, uint, uint)
FileChooserAddFilter func(windowPtr, ptr)
FileChooserDialogNew func(string, windowPtr, int, string, int, string, int, ptr) windowPtr
FileChooserGetFilenames func(windowPtr) *gsList
FileChooserSetAction func(windowPtr, int)
FileChooserSetCreateFolders func(windowPtr, bool)
FileChooserSetCurrentFolder func(windowPtr, string)
FileChooserSetSelectMultiple func(windowPtr, bool)
FileChooserSetShowHidden func(windowPtr, bool)
FileFilterAddPattern func(ptr, string)
FileFilterNew func() ptr
FileFilterSetName func(ptr, string)
ImageNewFromPixbuf func(ptr) ptr
MenuBarNew func() ptr
MenuItemNewWithLabel func(string) ptr
MenuItemSetLabel func(ptr, string)
MenuItemSetSubmenu func(ptr, ptr)
MenuNew func() ptr
MenuShellAppend func(ptr, ptr)
MessageDialogNew func(windowPtr, int, int, int, string) windowPtr
RadioMenuItemGetGroup func(ptr) gsListPtr
RadioMenuItemNewWithLabel func(gsListPtr, string) ptr
SeparatorMenuItemNew func() ptr
StyleContextAddProvider func(ptr, ptr, int)
TargetEntryFree func(ptr)
TargetEntryNew func(string, int, uint) ptr
WidgetDestroy func(windowPtr)
WidgetGetDisplay func(windowPtr) ptr
WidgetGetScreen func(windowPtr) ptr
WidgetGetStyleContext func(windowPtr) ptr
WidgetGetWindow func(windowPtr) windowPtr
WidgetHide func(ptr)
WidgetIsVisible func(ptr) bool
WidgetShow func(ptr)
WidgetShowAll func(windowPtr)
WidgetSetAppPaintable func(windowPtr, int)
WidgetSetName func(ptr, string)
WidgetSetSensitive func(ptr, int)
WidgetSetTooltipText func(windowPtr, string)
WidgetSetVisual func(windowPtr, ptr)
WindowClose func(windowPtr)
WindowFullscreen func(windowPtr)
WindowGetPosition func(windowPtr, *int, *int) bool
WindowGetSize func(windowPtr, *int, *int)
WindowHasToplevelFocus func(windowPtr) int
//WindowKeepAbove func(pointer, bool)
WindowMaximize func(windowPtr)
WindowIconify func(windowPtr)
WindowMove func(windowPtr, int, int)
WindowPresent func(windowPtr)
WindowResize func(windowPtr, int, int)
WindowSetDecorated func(windowPtr, int)
WindowSetGeometryHints func(windowPtr, ptr, ptr, int)
WindowSetKeepAbove func(windowPtr, bool)
WindowSetResizable func(windowPtr, bool)
WindowSetTitle func(windowPtr, string)
WindowUnfullscreen func(windowPtr)
WindowUnmaximize func(windowPtr)
}
soup struct {
MessageHeadersNew func(int) ptr
MessageHeadersAppend func(ptr, string, string)
}
webkitSettings struct {
GetEnableJavascript func(webkitSettingsPtr) bool
SetEnableJavascript func(webkitSettingsPtr, bool)
GetAutoLoadImages func(webkitSettingsPtr) bool
SetAutoLoadImages func(webkitSettingsPtr, bool)
GetLoadIconsIgnoringImageLoadSetting func(webkitSettingsPtr) bool
SetLoadIconsIgnoringImageLoadSetting func(webkitSettingsPtr, bool)
GetEnableOfflineWebApplicationCache func(webkitSettingsPtr) bool
SetEnableOfflineWebApplicationCache func(webkitSettingsPtr, bool)
GetEnableHtml5LocalStorage func(webkitSettingsPtr) bool
SetEnableHtml5LocalStorage func(webkitSettingsPtr, bool)
GetEnableHtml5Database func(webkitSettingsPtr) bool
SetEnableHtml5Database func(webkitSettingsPtr, bool)
GetEnableXssAuditor func(webkitSettingsPtr) bool
SetEnableXssAuditor func(webkitSettingsPtr, bool)
GetEnableFrameFlattening func(webkitSettingsPtr) bool
SetEnableFrameFlattening func(webkitSettingsPtr, bool)
GetEnablePlugins func(webkitSettingsPtr) bool
SetEnablePlugins func(webkitSettingsPtr, bool)
GetEnableJava func(webkitSettingsPtr) bool
SetEnableJava func(webkitSettingsPtr, bool)
GetJavascriptCanOpenWindowsAutomatically func(webkitSettingsPtr) bool
SetJavascriptCanOpenWindowsAutomatically func(webkitSettingsPtr, bool)
GetEnableHyperlinkAuditing func(webkitSettingsPtr) bool
SetEnableHyperlinkAuditing func(webkitSettingsPtr, bool)
GetDefaultFontFamily func(webkitSettingsPtr) string
SetDefaultFontFamily func(webkitSettingsPtr, string)
GetMonospaceFontFamily func(webkitSettingsPtr) string
SetMonospaceFontFamily func(webkitSettingsPtr, string)
GetSerifFontFamily func(webkitSettingsPtr) string
SetSerifFontFamily func(webkitSettingsPtr, string)
GetSansSerifFontFamily func(webkitSettingsPtr) string
SetSansSerifFontFamily func(webkitSettingsPtr, string)
GetCursiveFontFamily func(webkitSettingsPtr) string
SetCursiveFontFamily func(webkitSettingsPtr, string)
GetFantasyFontFamily func(webkitSettingsPtr) string
SetFantasyFontFamily func(webkitSettingsPtr, string)
GetPictographFontFamily func(webkitSettingsPtr) string
SetPictographFontFamily func(webkitSettingsPtr, string)
GetDefaultFontSize func(webkitSettingsPtr) uint32
SetDefaultFontSize func(webkitSettingsPtr, uint32)
GetDefaultMonospaceFontSize func(webkitSettingsPtr) uint32
SetDefaultMonospaceFontSize func(webkitSettingsPtr, uint32)
GetMinimumFontSize func(webkitSettingsPtr) uint32
SetMinimumFontSize func(webkitSettingsPtr, uint32)
GetDefaultCharset func(webkitSettingsPtr) string
SetDefaultCharset func(webkitSettingsPtr, string)
GetEnableDeveloperExtras func(webkitSettingsPtr) bool
SetEnableDeveloperExtras func(webkitSettingsPtr, bool)
GetEnableResizableTextAreas func(webkitSettingsPtr) bool
SetEnableResizableTextAreas func(webkitSettingsPtr, bool)
GetEnableTabsToLinks func(webkitSettingsPtr) bool
SetEnableTabsToLinks func(webkitSettingsPtr, bool)
GetEnableDnsPrefetching func(webkitSettingsPtr) bool
SetEnableDnsPrefetching func(webkitSettingsPtr, bool)
GetEnableCaretBrowsing func(webkitSettingsPtr) bool
SetEnableCaretBrowsing func(webkitSettingsPtr, bool)
GetEnableFullscreen func(webkitSettingsPtr) bool
SetEnableFullscreen func(webkitSettingsPtr, bool)
GetPrintBackgrounds func(webkitSettingsPtr) bool
SetPrintBackgrounds func(webkitSettingsPtr, bool)
GetEnableWebaudio func(webkitSettingsPtr) bool
SetEnableWebaudio func(webkitSettingsPtr, bool)
GetEnableWebgl func(webkitSettingsPtr) bool
SetEnableWebgl func(webkitSettingsPtr, bool)
SetAllowModalDialogs func(webkitSettingsPtr, bool)
GetAllowModalDialogs func(webkitSettingsPtr) bool
SetZoomTextOnly func(webkitSettingsPtr, bool)
GetZoomTextOnly func(webkitSettingsPtr) bool
GetJavascriptCanAccessClipboard func(webkitSettingsPtr) bool
SetJavascriptCanAccessClipboard func(webkitSettingsPtr, bool)
GetMediaPlaybackRequiresUserGesture func(webkitSettingsPtr) bool
SetMediaPlaybackRequiresUserGesture func(webkitSettingsPtr, bool)
GetMediaPlaybackAllowsInline func(webkitSettingsPtr) bool
SetMediaPlaybackAllowsInline func(webkitSettingsPtr, bool)
GetDrawCompositingIndicators func(webkitSettingsPtr) bool
SetDrawCompositingIndicators func(webkitSettingsPtr, bool)
GetEnableSiteSpecificQuirks func(webkitSettingsPtr) bool
SetEnableSiteSpecificQuirks func(webkitSettingsPtr, bool)
GetEnablePageCache func(webkitSettingsPtr) bool
SetEnablePageCache func(webkitSettingsPtr, bool)
GetUserAgent func(webkitSettingsPtr) string
SetUserAgent func(webkitSettingsPtr, string)
SetUserAgentWithApplicationDetails func(webkitSettingsPtr, string, string)
GetEnableSmoothScrolling func(webkitSettingsPtr) bool
SetEnableSmoothScrolling func(webkitSettingsPtr, bool)
GetEnableAccelerated2DCanvas func(webkitSettingsPtr) bool `name:"webkit_settings_get_enable_accelerated_2d_canvas"`
SetEnableAccelerated2DCanvas func(webkitSettingsPtr, bool) `name:"webkit_settings_set_enable_accelerated_2d_canvas"`
GetEnableWriteConsoleMessagesToStdout func(webkitSettingsPtr) bool
SetEnableWriteConsoleMessagesToStdout func(webkitSettingsPtr, bool)
GetEnableMediaStream func(webkitSettingsPtr) bool
SetEnableMediaStream func(webkitSettingsPtr, bool)
GetEnableMockCaptureDevices func(webkitSettingsPtr) bool
SetEnableMockCaptureDevices func(webkitSettingsPtr, bool)
GetEnableSpatialNavigation func(webkitSettingsPtr) bool
SetEnableSpatialNavigation func(webkitSettingsPtr, bool)
GetEnableMediasource func(webkitSettingsPtr) bool
SetEnableMediasource func(webkitSettingsPtr, bool)
GetEnableEncryptedMedia func(webkitSettingsPtr) bool
SetEnableEncryptedMedia func(webkitSettingsPtr, bool)
GetEnableMediaCapabilities func(webkitSettingsPtr) bool
SetEnableMediaCapabilities func(webkitSettingsPtr, bool)
GetAllowFileAccessFromFileUrls func(webkitSettingsPtr) bool
SetAllowFileAccessFromFileUrls func(webkitSettingsPtr, bool)
GetAllowUniversalAccessFromFileUrls func(webkitSettingsPtr) bool
SetAllowUniversalAccessFromFileUrls func(webkitSettingsPtr, bool)
GetAllowTopNavigationToDataUrls func(webkitSettingsPtr) bool
SetAllowTopNavigationToDataUrls func(webkitSettingsPtr, bool)
GetHardwareAccelerationPolicy func(webkitSettingsPtr) int
SetHardwareAccelerationPolicy func(webkitSettingsPtr, int)
GetEnableBackForwardNavigationGestures func(webkitSettingsPtr) bool
SetEnableBackForwardNavigationGestures func(webkitSettingsPtr, bool)
FontSizeToPoints func(uint32) uint32
FontSizeToPixels func(uint32) uint32
GetEnableJavascriptMarkup func(webkitSettingsPtr) bool
SetEnableJavascriptMarkup func(webkitSettingsPtr, bool)
GetEnableMedia func(webkitSettingsPtr) bool
SetEnableMedia func(webkitSettingsPtr, bool)
GetMediaContentTypesRequiringHardwareSupport func(webkitSettingsPtr) string
SetMediaContentTypesRequiringHardwareSupport func(webkitSettingsPtr, string)
GetEnableWebrtc func(webkitSettingsPtr) bool
SetEnableWebrtc func(webkitSettingsPtr, bool)
GetDisableWebSecurity func(webkitSettingsPtr) bool
SetDisableWebSecurity func(webkitSettingsPtr, bool)
}
jsc struct {
ValueNewString func(string) ptr
ValueToString func(ptr) string
ValueToStringAsBytes func(ptr) string
ValueIsString func(ptr) bool
ValueToDouble func(ptr) float64
ValueIsNumber func(ptr) bool
ValueToBoolean func(ptr) bool
ValueIsBoolean func(ptr) bool
ValueIsObject func(ptr) bool
ValueObjectInvokeMethodv func(ptr, string, int, ...ptr) ptr
ValueObjectGetProperty func(ptr, string) ptr
ValueObjectSetProperty func(ptr, string, ptr)
ValueIsArray func(ptr) bool
ContextGetCurrent func() ptr
ValueIsUndefined func(ptr) bool
ValueIsNull func(ptr) bool
ContextGetValue func(ptr, string) ptr
ContextGetException func(ptr) ptr
ContextGetGlobalObject func(ptr) ptr
ExceptionGetMessage func(ptr) string
ValueGetContext func(ptr) ptr
}
webkit struct {
WebViewNewWithContext func(ptr) webviewPtr
WebViewNewWithUserContentManager func(userContentManagerPtr) webviewPtr
WebContextRegisterUriScheme func(ptr, string, ptr, int, int)
//webkitSettingsGetEnableDeveloperExtras func(pointer) bool
//webkitSettingsSetHardwareAccelerationPolicy func(pointer, int)
//webkitSettingsSetEnableDeveloperExtras func(pointer, bool)
//webkitSettingsSetUserAgentWithApplicationDetails func(pointer, string, string)
UserContentManagerNew func() userContentManagerPtr
UserContentManagerRegisterScriptMessageHandler func(userContentManagerPtr, string)
WebContextGetWebsiteDataManager func(ptr) ptr
CookieManagerSetPersistentStorage func(ptr, string, int)
CookieManagerSetAcceptPolicy func(ptr, int)
WebContextGetCookieManager func(ptr) ptr
WebViewGetUserContentManager func(webviewPtr) userContentManagerPtr
WebsiteDataManagerNew func(string, string, string, string, string, bool, ptr) ptr
WebContextSetFaviconDatabaseDirectory func(ptr, string)
WebContextSetWebExtensionsDirectory func(ptr, string)
WebsiteDataManagerGetBaseCacheDirectory func(ptr) string
WebsiteDataManagerGetLocalStorageDirectory func(ptr) string
WebsiteDataManagerSetPersistentCredentialStorageEnabled func(ptr, bool)
WebContextNewWithWebsiteDataManager func(ptr) ptr
WebContextSetCacheModel func(ptr, int)
WebContextGetSandboxEnabled func(ptr) bool
WebContextSetSandboxEnabled func(ptr, bool)
WebContextAddPathToSandbox func(ptr, string)
WebContextGetSpellCheckingEnabled func(ptr) bool
WebContextSetSpellCheckingEnabled func(ptr, bool)
JavascriptResultGetJsValue func(ptr) ptr
JavascriptResultUnref func(ptr)
WebContextGetDefault func() ptr
WebContextGetSecurityManager func(ptr) ptr
SecurityManagerRegisterUriSchemeAsSecure func(ptr, string)
SecurityManagerRegisterUriSchemeAsNoAccess func(ptr, string)
SecurityManagerRegisterUriSchemeAsDisplayIsolated func(ptr, string)
SecurityManagerRegisterUriSchemeAsCorsEnabled func(ptr, string)
SecurityManagerRegisterUriSchemeAsLocal func(ptr, string)
WebViewEvaluateJavascript func(webviewPtr, string, int, ptr, ptr, ptr, ptr, ptr)
WebViewEvaluateJavascriptFinish func(webviewPtr, ptr, *gError) ptr
WebViewCallAsyncJavascriptFunction func(webviewPtr, string, int, ptr, ptr, ptr, ptr, ptr, ptr)
WebViewCallAsyncJavascriptFunctionFinish func(webviewPtr, ptr, *gError) ptr
WebViewGetSettings func(webviewPtr) webkitSettingsPtr
WebViewGetZoomLevel func(webviewPtr) float64
//WebViewLoadAlternateHtml func(webviewPtr, string, string, *string)
WebViewLoadUri func(webviewPtr, string)
WebViewLoadHtml func(webviewPtr, string, string)
WebViewSetBackgroundColor func(webviewPtr, ptr)
WebViewSetSettings func(webviewPtr, webkitSettingsPtr)
WebViewSetZoomLevel func(webviewPtr, float64)
WebViewLoadBytes func(webviewPtr, []byte, string, string, string)
WebViewSetCorsAllowlist func(webviewPtr, ...ptr)
UriSchemeRequestGetUri func(ptr) string
UriSchemeRequestGetHttpMethod func(ptr) string
UriSchemeRequestGetHttpHeaders func(ptr) ptr
UriSchemeRequestGetHttpBody func(ptr) ptr
UriSchemeRequestFinish func(ptr, ptr, int, string)
UriSchemeRequestFinishError func(ptr, *gError)
UriSchemeRequestFinishWithResponse func(ptr, ptr)
UriSchemeResponseNew func(ptr, int64) ptr
UriSchemeResponseSetStatus func(ptr, int, string)
UriSchemeResponseSetContentType func(ptr, string)
UriSchemeResponseSetHttpHeaders func(ptr, ptr)
}
}
func registerFunctions(lib uintptr, prefix string, v interface{}) error {
if reflect.TypeOf(v).Kind() != reflect.Pointer {
return fmt.Errorf("v must be a struct pointer")
}
vElem := reflect.ValueOf(v).Elem()
if vElem.Kind() != reflect.Struct {
return fmt.Errorf("v must be a struct pointer")
}
vType := vElem.Type()
re := regexp.MustCompile("(\\p{Lu}\\P{Lu}*)")
for i := 0; i < vElem.NumField(); i++ {
field := vElem.Field(i)
if field.Kind() == reflect.Func {
name := vType.Field(i).Tag.Get("name")
if name == "" {
name = prefix + strings.ToLower(re.ReplaceAllString(vType.Field(i).Name, "_${1}"))
}
sym, err := purego.Dlsym(lib, name)
if err != nil {
return err
}
purego.RegisterFunc(field.Addr().Interface(), sym)
}
}
return nil
}
func getLibTarget() string {
switch runtime.GOOS + "/" + runtime.GOARCH {
case "linux/amd64":
return "x86_64-linux-gnu"
case "linux/arm64":
return "aarch64-linux-gnu"
case "freebsd/amd64":
return "x86_64-unknown-freebsd"
case "freebsd/arm64":
return "aarch64-unknown-freebsd"
case "darwin/amd64":
return "x86_64-apple-darwin"
case "darwin/arm64":
return "aarch64-apple-darwin"
case "ios/amd64":
return "x86_64-apple-ios"
case "ios/arm64":
return "aarch64-apple-ios"
default:
panic("unsupported architecture: " + runtime.GOARCH)
}
}
func findSharedLib(target string, names []string) []string {
var paths []string
for _, name := range names {
// 2.1.2 build library paths
libDirs := []string{
"/lib/" + target + "/",
"/lib64/" + target + "/",
"/usr/lib/" + target + "/",
"/usr/lib64/" + target + "/",
"/usr/local/lib64/" + target + "/",
"/usr/local/lib/" + target + "/",
}
// 2.1.3 determine library path
for _, libDir := range libDirs {
if info, err := os.Stat(libDir); os.IsNotExist(err) || !info.IsDir() {
continue
}
libPath := filepath.Join(libDir, "lib"+name+".so")
if info, err := os.Stat(libPath); !os.IsNotExist(err) && !info.IsDir() {
paths = append(paths, libPath)
break
}
matches, err := filepath.Glob(filepath.Join(libDir, "lib"+name+".so") + ".*")
if err != nil {
continue
}
if len(matches) == 0 {
continue
}
paths = append(paths, matches[0])
break
}
}
if len(paths) != len(names) {
return nil
}
return paths
}
func (a *App) loadSharedLibs() error {
a.log("loading shared libraries", "GOOS", runtime.GOOS, "GOARCH", runtime.GOARCH)
loadTime := time.Now()
// 1. Locate shared libraries
target := getLibTarget()
var libPaths []string
for i, names := range libs {
a.log("locating shared libraries", "target", target, "libs", names)
paths := findSharedLib(target, names)
if paths != nil {
libPaths = paths
lib.Version = i
break
}
}
if libPaths == nil {
return fmt.Errorf("unable to locate shared libraries for %s", target)
}
// 2. Load shared libraries
var err error
a.log("loading gtk library", "path", libPaths[0])
lib.GTK, err = purego.Dlopen(libPaths[0], purego.RTLD_NOW|purego.RTLD_GLOBAL)
if err != nil {
return fmt.Errorf("unable to load gtk library: %w", err)
}
a.log("loading webkit library", "path", libPaths[1])
lib.Webkit, err = purego.Dlopen(libPaths[1], purego.RTLD_NOW|purego.RTLD_GLOBAL)
if err != nil {
return fmt.Errorf("unable to load webkit library: %w", err)
}
// 3. Register functions
err = registerFunctions(lib.GTK, "g", &lib.g)
if err != nil {
return fmt.Errorf("unable to register g functions: %w", err)
}
err = registerFunctions(lib.GTK, "gdk", &lib.gdk)
if err != nil {
return fmt.Errorf("unable to register gdk functions: %w", err)
}
err = registerFunctions(lib.GTK, "gtk", &lib.gtk)
if err != nil {
return fmt.Errorf("unable to register gtk functions: %w", err)
}
err = registerFunctions(lib.Webkit, "soup", &lib.soup)
if err != nil {
return fmt.Errorf("unable to register soup functions: %w", err)
}
err = registerFunctions(lib.Webkit, "jsc", &lib.jsc)
if err != nil {
return fmt.Errorf("unable to register jsc functions: %w", err)
}
err = registerFunctions(lib.Webkit, "webkit", &lib.webkit)
if err != nil {
a.log("unable to register webkit functions", "error", err)
}
err = registerFunctions(lib.Webkit, "webkit_settings", &lib.webkitSettings)
if err != nil {
a.log("unable to register webkit_settings functions", "error", err)
}
a.log("shared libraries loaded", "in", time.Since(loadTime), "paths", libPaths)
return nil
}