x11.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build (linux && !android) || freebsd
  5. // +build linux,!android freebsd
  6. package app
  7. /*
  8. Simple on-screen app debugging for X11. Not an officially supported
  9. development target for apps, as screens with mice are very different
  10. than screens with touch panels.
  11. */
  12. /*
  13. #cgo LDFLAGS: -lEGL -lGLESv2 -lX11
  14. #cgo freebsd CFLAGS: -I/usr/local/include/
  15. void createWindow(void);
  16. void processEvents(void);
  17. void swapBuffers(void);
  18. */
  19. import "C"
  20. import (
  21. "runtime"
  22. "time"
  23. "fyne.io/fyne/v2/internal/driver/mobile/event/lifecycle"
  24. "fyne.io/fyne/v2/internal/driver/mobile/event/paint"
  25. "fyne.io/fyne/v2/internal/driver/mobile/event/size"
  26. "fyne.io/fyne/v2/internal/driver/mobile/event/touch"
  27. )
  28. func init() {
  29. theApp.registerGLViewportFilter()
  30. }
  31. func main(f func(App)) {
  32. runtime.LockOSThread()
  33. workAvailable := theApp.worker.WorkAvailable()
  34. heartbeat := time.NewTicker(time.Second / 60)
  35. C.createWindow()
  36. // TODO: send lifecycle events when e.g. the X11 window is iconified or moved off-screen.
  37. theApp.sendLifecycle(lifecycle.StageFocused)
  38. // TODO: translate X11 expose events to shiny paint events, instead of
  39. // sending this synthetic paint event as a hack.
  40. theApp.events.In() <- paint.Event{}
  41. donec := make(chan struct{})
  42. go func() {
  43. f(theApp)
  44. close(donec)
  45. }()
  46. // TODO: can we get the actual vsync signal?
  47. ticker := time.NewTicker(time.Second / 60)
  48. defer ticker.Stop()
  49. var tc <-chan time.Time
  50. for {
  51. select {
  52. case <-donec:
  53. return
  54. case <-heartbeat.C:
  55. C.processEvents()
  56. case <-workAvailable:
  57. theApp.worker.DoWork()
  58. case <-theApp.publish:
  59. C.swapBuffers()
  60. tc = ticker.C
  61. case <-tc:
  62. tc = nil
  63. theApp.publishResult <- PublishResult{}
  64. }
  65. }
  66. }
  67. func GoBack() {
  68. // When simulating mobile there are no other activities open (and we can't just force background)
  69. }
  70. //export onResize
  71. func onResize(w, h int) {
  72. // TODO(nigeltao): don't assume 72 DPI. DisplayWidth and DisplayWidthMM
  73. // is probably the best place to start looking.
  74. pixelsPerPt := float32(1)
  75. theApp.events.In() <- size.Event{
  76. WidthPx: w,
  77. HeightPx: h,
  78. WidthPt: float32(w),
  79. HeightPt: float32(h),
  80. PixelsPerPt: pixelsPerPt,
  81. Orientation: screenOrientation(w, h),
  82. }
  83. }
  84. func sendTouch(t touch.Type, x, y float32) {
  85. theApp.events.In() <- touch.Event{
  86. X: x,
  87. Y: y,
  88. Sequence: 0, // TODO: button??
  89. Type: t,
  90. }
  91. }
  92. //export onTouchBegin
  93. func onTouchBegin(x, y float32) { sendTouch(touch.TypeBegin, x, y) }
  94. //export onTouchMove
  95. func onTouchMove(x, y float32) { sendTouch(touch.TypeMove, x, y) }
  96. //export onTouchEnd
  97. func onTouchEnd(x, y float32) { sendTouch(touch.TypeEnd, x, y) }
  98. var stopped bool
  99. //export onStop
  100. func onStop() {
  101. if stopped {
  102. return
  103. }
  104. stopped = true
  105. theApp.sendLifecycle(lifecycle.StageDead)
  106. theApp.events.Close()
  107. }
  108. // driverShowVirtualKeyboard does nothing on desktop
  109. func driverShowVirtualKeyboard(KeyboardType) {
  110. }
  111. // driverHideVirtualKeyboard does nothing on desktop
  112. func driverHideVirtualKeyboard() {
  113. }
  114. // driverShowFileOpenPicker does nothing on desktop
  115. func driverShowFileOpenPicker(func(string, func()), *FileFilter) {
  116. }
  117. // driverShowFileSavePicker does nothing on desktop
  118. func driverShowFileSavePicker(func(string, func()), *FileFilter, string) {
  119. }