From 8cd8d6d125ce01f19d4059a43c968700f3a69e8a Mon Sep 17 00:00:00 2001
From: faiface <faiface@ksp.sk>
Date: Fri, 16 Dec 2016 01:11:00 +0100
Subject: [PATCH] execute monitor stuff on main thread

---
 monitor.go | 45 ++++++++++++++++++++++++++++++++++-----------
 1 file changed, 34 insertions(+), 11 deletions(-)

diff --git a/monitor.go b/monitor.go
index 9a941ef..75f2fd2 100644
--- a/monitor.go
+++ b/monitor.go
@@ -1,6 +1,9 @@
 package pixel
 
-import "github.com/go-gl/glfw/v3.2/glfw"
+import (
+	"github.com/faiface/pixel/pixelgl"
+	"github.com/go-gl/glfw/v3.2/glfw"
+)
 
 // Monitor represents a physical display attached to your computer.
 type Monitor struct {
@@ -9,28 +12,39 @@ type Monitor struct {
 
 // PrimaryMonitor returns the main monitor (usually the one with the taskbar and stuff).
 func PrimaryMonitor() *Monitor {
+	monitor := pixelgl.DoVal(func() interface{} {
+		return glfw.GetPrimaryMonitor()
+	}).(*glfw.Monitor)
 	return &Monitor{
-		monitor: glfw.GetPrimaryMonitor(),
+		monitor: monitor,
 	}
 }
 
 // Monitors returns a slice of all currently available monitors.
 func Monitors() []*Monitor {
 	var monitors []*Monitor
-	for _, monitor := range glfw.GetMonitors() {
-		monitors = append(monitors, &Monitor{monitor: monitor})
-	}
+	pixelgl.Do(func() {
+		for _, monitor := range glfw.GetMonitors() {
+			monitors = append(monitors, &Monitor{monitor: monitor})
+		}
+	})
 	return monitors
 }
 
 // Name returns a human-readable name of a monitor.
 func (m *Monitor) Name() string {
-	return m.monitor.GetName()
+	name := pixelgl.DoVal(func() interface{} {
+		return m.monitor.GetName()
+	}).(string)
+	return name
 }
 
 // PhysicalSize returns the size of the display area of a monitor in millimeters.
 func (m *Monitor) PhysicalSize() (width, height float64) {
-	wi, hi := m.monitor.GetPhysicalSize()
+	var wi, hi int
+	pixelgl.Do(func() {
+		wi, hi = m.monitor.GetPhysicalSize()
+	})
 	width = float64(wi)
 	height = float64(hi)
 	return
@@ -38,7 +52,10 @@ func (m *Monitor) PhysicalSize() (width, height float64) {
 
 // Position returns the position of the upper-left corner of a monitor in screen coordinates.
 func (m *Monitor) Position() (x, y float64) {
-	xi, yi := m.monitor.GetPos()
+	var xi, yi int
+	pixelgl.Do(func() {
+		xi, yi = m.monitor.GetPos()
+	})
 	x = float64(xi)
 	y = float64(yi)
 	return
@@ -46,7 +63,9 @@ func (m *Monitor) Position() (x, y float64) {
 
 // Size returns the resolution of a monitor in pixels.
 func (m *Monitor) Size() (width, height float64) {
-	mode := m.monitor.GetVideoMode()
+	mode := pixelgl.DoVal(func() interface{} {
+		return m.monitor.GetVideoMode()
+	}).(*glfw.VidMode)
 	width = float64(mode.Width)
 	height = float64(mode.Height)
 	return
@@ -54,7 +73,9 @@ func (m *Monitor) Size() (width, height float64) {
 
 // BitDepth returns the number of bits per color of a monitor.
 func (m *Monitor) BitDepth() (red, green, blue int) {
-	mode := m.monitor.GetVideoMode()
+	mode := pixelgl.DoVal(func() interface{} {
+		return m.monitor.GetVideoMode()
+	}).(*glfw.VidMode)
 	red = mode.RedBits
 	green = mode.GreenBits
 	blue = mode.BlueBits
@@ -63,7 +84,9 @@ func (m *Monitor) BitDepth() (red, green, blue int) {
 
 // RefreshRate returns the refresh frequency of a monitor in Hz (refreshes/second).
 func (m *Monitor) RefreshRate() (rate float64) {
-	mode := m.monitor.GetVideoMode()
+	mode := pixelgl.DoVal(func() interface{} {
+		return m.monitor.GetVideoMode()
+	}).(*glfw.VidMode)
 	rate = float64(mode.RefreshRate)
 	return
 }
-- 
GitLab