x11.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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
  5. // +build linux,!android
  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. void createWindow(void);
  15. void processEvents(void);
  16. void swapBuffers(void);
  17. */
  18. import "C"
  19. import (
  20. "runtime"
  21. "time"
  22. "golang.org/x/mobile/event/lifecycle"
  23. "golang.org/x/mobile/event/paint"
  24. "golang.org/x/mobile/event/size"
  25. "golang.org/x/mobile/event/touch"
  26. "golang.org/x/mobile/geom"
  27. )
  28. func init() {
  29. theApp.registerGLViewportFilter()
  30. }
  31. func main(f func(App)) {
  32. runtime.LockOSThread()
  33. workAvailable := theApp.worker.WorkAvailable()
  34. C.createWindow()
  35. // TODO: send lifecycle events when e.g. the X11 window is iconified or moved off-screen.
  36. theApp.sendLifecycle(lifecycle.StageFocused)
  37. // TODO: translate X11 expose events to shiny paint events, instead of
  38. // sending this synthetic paint event as a hack.
  39. theApp.eventsIn <- paint.Event{}
  40. donec := make(chan struct{})
  41. go func() {
  42. // close the donec channel in a defer statement
  43. // so that we could still be able to return even
  44. // if f panics.
  45. defer close(donec)
  46. f(theApp)
  47. }()
  48. // TODO: can we get the actual vsync signal?
  49. ticker := time.NewTicker(time.Second / 60)
  50. defer ticker.Stop()
  51. var tc <-chan time.Time
  52. for {
  53. select {
  54. case <-donec:
  55. return
  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. C.processEvents()
  66. }
  67. }
  68. //export onResize
  69. func onResize(w, h int) {
  70. // TODO(nigeltao): don't assume 72 DPI. DisplayWidth and DisplayWidthMM
  71. // is probably the best place to start looking.
  72. pixelsPerPt := float32(1)
  73. theApp.eventsIn <- size.Event{
  74. WidthPx: w,
  75. HeightPx: h,
  76. WidthPt: geom.Pt(w),
  77. HeightPt: geom.Pt(h),
  78. PixelsPerPt: pixelsPerPt,
  79. }
  80. }
  81. func sendTouch(t touch.Type, x, y float32) {
  82. theApp.eventsIn <- touch.Event{
  83. X: x,
  84. Y: y,
  85. Sequence: 0, // TODO: button??
  86. Type: t,
  87. }
  88. }
  89. //export onTouchBegin
  90. func onTouchBegin(x, y float32) { sendTouch(touch.TypeBegin, x, y) }
  91. //export onTouchMove
  92. func onTouchMove(x, y float32) { sendTouch(touch.TypeMove, x, y) }
  93. //export onTouchEnd
  94. func onTouchEnd(x, y float32) { sendTouch(touch.TypeEnd, x, y) }
  95. var stopped bool
  96. //export onStop
  97. func onStop() {
  98. if stopped {
  99. return
  100. }
  101. stopped = true
  102. theApp.sendLifecycle(lifecycle.StageDead)
  103. theApp.eventsIn <- stopPumping{}
  104. }