x11.go 3.4 KB

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