github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/mobile/app/x11.go (about)

     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  
     5  // +build linux,!android
     6  
     7  package app
     8  
     9  /*
    10  Simple on-screen app debugging for X11. Not an officially supported
    11  development target for apps, as screens with mice are very different
    12  than screens with touch panels.
    13  
    14  On Ubuntu 14.04 'Trusty', you may have to install these libraries:
    15  sudo apt-get install libegl1-mesa-dev libgles2-mesa-dev libx11-dev
    16  */
    17  
    18  /*
    19  #cgo LDFLAGS: -lEGL -lGLESv2 -lX11
    20  
    21  void createWindow(void);
    22  void processEvents(void);
    23  void swapBuffers(void);
    24  */
    25  import "C"
    26  import (
    27  	"runtime"
    28  	"time"
    29  
    30  	"golang.org/x/mobile/event/lifecycle"
    31  	"golang.org/x/mobile/event/paint"
    32  	"golang.org/x/mobile/event/size"
    33  	"golang.org/x/mobile/event/touch"
    34  	"golang.org/x/mobile/geom"
    35  )
    36  
    37  func init() {
    38  	theApp.registerGLViewportFilter()
    39  }
    40  
    41  func main(f func(App)) {
    42  	runtime.LockOSThread()
    43  
    44  	workAvailable := theApp.worker.WorkAvailable()
    45  
    46  	C.createWindow()
    47  
    48  	// TODO: send lifecycle events when e.g. the X11 window is iconified or moved off-screen.
    49  	theApp.sendLifecycle(lifecycle.StageFocused)
    50  
    51  	// TODO: translate X11 expose events to shiny paint events, instead of
    52  	// sending this synthetic paint event as a hack.
    53  	theApp.eventsIn <- paint.Event{}
    54  
    55  	donec := make(chan struct{})
    56  	go func() {
    57  		f(theApp)
    58  		close(donec)
    59  	}()
    60  
    61  	// TODO: can we get the actual vsync signal?
    62  	ticker := time.NewTicker(time.Second / 60)
    63  	defer ticker.Stop()
    64  	var tc <-chan time.Time
    65  
    66  	for {
    67  		select {
    68  		case <-donec:
    69  			return
    70  		case <-workAvailable:
    71  			theApp.worker.DoWork()
    72  		case <-theApp.publish:
    73  			C.swapBuffers()
    74  			tc = ticker.C
    75  		case <-tc:
    76  			tc = nil
    77  			theApp.publishResult <- PublishResult{}
    78  		}
    79  		C.processEvents()
    80  	}
    81  }
    82  
    83  //export onResize
    84  func onResize(w, h int) {
    85  	// TODO(nigeltao): don't assume 72 DPI. DisplayWidth and DisplayWidthMM
    86  	// is probably the best place to start looking.
    87  	pixelsPerPt := float32(1)
    88  	theApp.eventsIn <- size.Event{
    89  		WidthPx:     w,
    90  		HeightPx:    h,
    91  		WidthPt:     geom.Pt(w),
    92  		HeightPt:    geom.Pt(h),
    93  		PixelsPerPt: pixelsPerPt,
    94  	}
    95  }
    96  
    97  func sendTouch(t touch.Type, x, y float32) {
    98  	theApp.eventsIn <- touch.Event{
    99  		X:        x,
   100  		Y:        y,
   101  		Sequence: 0, // TODO: button??
   102  		Type:     t,
   103  	}
   104  }
   105  
   106  //export onTouchBegin
   107  func onTouchBegin(x, y float32) { sendTouch(touch.TypeBegin, x, y) }
   108  
   109  //export onTouchMove
   110  func onTouchMove(x, y float32) { sendTouch(touch.TypeMove, x, y) }
   111  
   112  //export onTouchEnd
   113  func onTouchEnd(x, y float32) { sendTouch(touch.TypeEnd, x, y) }
   114  
   115  var stopped bool
   116  
   117  //export onStop
   118  func onStop() {
   119  	if stopped {
   120  		return
   121  	}
   122  	stopped = true
   123  	theApp.sendLifecycle(lifecycle.StageDead)
   124  	theApp.eventsIn <- stopPumping{}
   125  }