window.go 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. package glfw
  2. //#include <stdlib.h>
  3. //#define GLFW_INCLUDE_NONE
  4. //#include "glfw/include/GLFW/glfw3.h"
  5. //void glfwSetWindowPosCallbackCB(GLFWwindow *window);
  6. //void glfwSetWindowSizeCallbackCB(GLFWwindow *window);
  7. //void glfwSetFramebufferSizeCallbackCB(GLFWwindow *window);
  8. //void glfwSetWindowCloseCallbackCB(GLFWwindow *window);
  9. //void glfwSetWindowRefreshCallbackCB(GLFWwindow *window);
  10. //void glfwSetWindowFocusCallbackCB(GLFWwindow *window);
  11. //void glfwSetWindowIconifyCallbackCB(GLFWwindow *window);
  12. //void glfwSetWindowMaximizeCallbackCB(GLFWwindow *window);
  13. //void glfwSetWindowContentScaleCallbackCB(GLFWwindow *window);
  14. import "C"
  15. import (
  16. "image"
  17. "image/draw"
  18. "sync"
  19. "unsafe"
  20. )
  21. // Internal window list stuff
  22. type windowList struct {
  23. l sync.Mutex
  24. m map[*C.GLFWwindow]*Window
  25. }
  26. var windows = windowList{m: map[*C.GLFWwindow]*Window{}}
  27. func (w *windowList) put(wnd *Window) {
  28. w.l.Lock()
  29. defer w.l.Unlock()
  30. w.m[wnd.data] = wnd
  31. }
  32. func (w *windowList) remove(wnd *C.GLFWwindow) {
  33. w.l.Lock()
  34. defer w.l.Unlock()
  35. delete(w.m, wnd)
  36. }
  37. func (w *windowList) get(wnd *C.GLFWwindow) *Window {
  38. w.l.Lock()
  39. defer w.l.Unlock()
  40. return w.m[wnd]
  41. }
  42. // Hint corresponds to hints that can be set before creating a window.
  43. //
  44. // Hint also corresponds to the attributes of the window that can be get after
  45. // its creation.
  46. type Hint int
  47. // Init related hints. (Use with glfw.InitHint)
  48. const (
  49. JoystickHatButtons Hint = C.GLFW_JOYSTICK_HAT_BUTTONS // Specifies whether to also expose joystick hats as buttons, for compatibility with earlier versions of GLFW that did not have glfwGetJoystickHats.
  50. CocoaChdirResources Hint = C.GLFW_COCOA_CHDIR_RESOURCES // Specifies whether to set the current directory to the application to the Contents/Resources subdirectory of the application's bundle, if present.
  51. CocoaMenubar Hint = C.GLFW_COCOA_MENUBAR // Specifies whether to create a basic menu bar, either from a nib or manually, when the first window is created, which is when AppKit is initialized.
  52. )
  53. // Window related hints/attributes.
  54. const (
  55. Focused Hint = C.GLFW_FOCUSED // Specifies whether the window will be given input focus when created. This hint is ignored for full screen and initially hidden windows.
  56. Iconified Hint = C.GLFW_ICONIFIED // Specifies whether the window will be minimized.
  57. Maximized Hint = C.GLFW_MAXIMIZED // Specifies whether the window is maximized.
  58. Visible Hint = C.GLFW_VISIBLE // Specifies whether the window will be initially visible.
  59. Hovered Hint = C.GLFW_HOVERED // Specifies whether the cursor is currently directly over the content area of the window, with no other windows between. See Cursor enter/leave events for details.
  60. Resizable Hint = C.GLFW_RESIZABLE // Specifies whether the window will be resizable by the user.
  61. Decorated Hint = C.GLFW_DECORATED // Specifies whether the window will have window decorations such as a border, a close widget, etc.
  62. Floating Hint = C.GLFW_FLOATING // Specifies whether the window will be always-on-top.
  63. AutoIconify Hint = C.GLFW_AUTO_ICONIFY // Specifies whether fullscreen windows automatically iconify (and restore the previous video mode) on focus loss.
  64. CenterCursor Hint = C.GLFW_CENTER_CURSOR // Specifies whether the cursor should be centered over newly created full screen windows. This hint is ignored for windowed mode windows.
  65. TransparentFramebuffer Hint = C.GLFW_TRANSPARENT_FRAMEBUFFER // Specifies whether the framebuffer should be transparent.
  66. FocusOnShow Hint = C.GLFW_FOCUS_ON_SHOW // Specifies whether the window will be given input focus when glfwShowWindow is called.
  67. ScaleToMonitor Hint = C.GLFW_SCALE_TO_MONITOR // Specified whether the window content area should be resized based on the monitor content scale of any monitor it is placed on. This includes the initial placement when the window is created.
  68. )
  69. // Context related hints.
  70. const (
  71. ClientAPI Hint = C.GLFW_CLIENT_API // Specifies which client API to create the context for. Hard constraint.
  72. ContextVersionMajor Hint = C.GLFW_CONTEXT_VERSION_MAJOR // Specifies the client API version that the created context must be compatible with.
  73. ContextVersionMinor Hint = C.GLFW_CONTEXT_VERSION_MINOR // Specifies the client API version that the created context must be compatible with.
  74. ContextRobustness Hint = C.GLFW_CONTEXT_ROBUSTNESS // Specifies the robustness strategy to be used by the context.
  75. ContextReleaseBehavior Hint = C.GLFW_CONTEXT_RELEASE_BEHAVIOR // Specifies the release behavior to be used by the context.
  76. OpenGLForwardCompatible Hint = C.GLFW_OPENGL_FORWARD_COMPAT // Specifies whether the OpenGL context should be forward-compatible. Hard constraint.
  77. OpenGLDebugContext Hint = C.GLFW_OPENGL_DEBUG_CONTEXT // Specifies whether to create a debug OpenGL context, which may have additional error and performance issue reporting functionality. If OpenGL ES is requested, this hint is ignored.
  78. OpenGLProfile Hint = C.GLFW_OPENGL_PROFILE // Specifies which OpenGL profile to create the context for. Hard constraint.
  79. ContextCreationAPI Hint = C.GLFW_CONTEXT_CREATION_API // Specifies which context creation API to use to create the context.
  80. )
  81. // Framebuffer related hints.
  82. const (
  83. ContextRevision Hint = C.GLFW_CONTEXT_REVISION
  84. RedBits Hint = C.GLFW_RED_BITS // Specifies the desired bit depth of the default framebuffer.
  85. GreenBits Hint = C.GLFW_GREEN_BITS // Specifies the desired bit depth of the default framebuffer.
  86. BlueBits Hint = C.GLFW_BLUE_BITS // Specifies the desired bit depth of the default framebuffer.
  87. AlphaBits Hint = C.GLFW_ALPHA_BITS // Specifies the desired bit depth of the default framebuffer.
  88. DepthBits Hint = C.GLFW_DEPTH_BITS // Specifies the desired bit depth of the default framebuffer.
  89. StencilBits Hint = C.GLFW_STENCIL_BITS // Specifies the desired bit depth of the default framebuffer.
  90. AccumRedBits Hint = C.GLFW_ACCUM_RED_BITS // Specifies the desired bit depth of the accumulation buffer.
  91. AccumGreenBits Hint = C.GLFW_ACCUM_GREEN_BITS // Specifies the desired bit depth of the accumulation buffer.
  92. AccumBlueBits Hint = C.GLFW_ACCUM_BLUE_BITS // Specifies the desired bit depth of the accumulation buffer.
  93. AccumAlphaBits Hint = C.GLFW_ACCUM_ALPHA_BITS // Specifies the desired bit depth of the accumulation buffer.
  94. AuxBuffers Hint = C.GLFW_AUX_BUFFERS // Specifies the desired number of auxiliary buffers.
  95. Stereo Hint = C.GLFW_STEREO // Specifies whether to use stereoscopic rendering. Hard constraint.
  96. Samples Hint = C.GLFW_SAMPLES // Specifies the desired number of samples to use for multisampling. Zero disables multisampling.
  97. SRGBCapable Hint = C.GLFW_SRGB_CAPABLE // Specifies whether the framebuffer should be sRGB capable.
  98. RefreshRate Hint = C.GLFW_REFRESH_RATE // Specifies the desired refresh rate for full screen windows. If set to zero, the highest available refresh rate will be used. This hint is ignored for windowed mode windows.
  99. DoubleBuffer Hint = C.GLFW_DOUBLEBUFFER // Specifies whether the framebuffer should be double buffered. You nearly always want to use double buffering. This is a hard constraint.
  100. CocoaGraphicsSwitching Hint = C.GLFW_COCOA_GRAPHICS_SWITCHING // Specifies whether to in Automatic Graphics Switching, i.e. to allow the system to choose the integrated GPU for the OpenGL context and move it between GPUs if necessary or whether to force it to always run on the discrete GPU.
  101. CocoaRetinaFramebuffer Hint = C.GLFW_COCOA_RETINA_FRAMEBUFFER // Specifies whether to use full resolution framebuffers on Retina displays.
  102. )
  103. // Naming related hints. (Use with glfw.WindowHintString)
  104. const (
  105. CocoaFrameNAME Hint = C.GLFW_COCOA_FRAME_NAME // Specifies the UTF-8 encoded name to use for autosaving the window frame, or if empty disables frame autosaving for the window.
  106. X11ClassName Hint = C.GLFW_X11_CLASS_NAME // Specifies the desired ASCII encoded class parts of the ICCCM WM_CLASS window property.nd instance parts of the ICCCM WM_CLASS window property.
  107. X11InstanceName Hint = C.GLFW_X11_INSTANCE_NAME // Specifies the desired ASCII encoded instance parts of the ICCCM WM_CLASS window property.nd instance parts of the ICCCM WM_CLASS window property.
  108. )
  109. // Values for the ClientAPI hint.
  110. const (
  111. OpenGLAPI int = C.GLFW_OPENGL_API
  112. OpenGLESAPI int = C.GLFW_OPENGL_ES_API
  113. NoAPI int = C.GLFW_NO_API
  114. )
  115. // Values for ContextCreationAPI hint.
  116. const (
  117. NativeContextAPI int = C.GLFW_NATIVE_CONTEXT_API
  118. EGLContextAPI int = C.GLFW_EGL_CONTEXT_API
  119. OSMesaContextAPI int = C.GLFW_OSMESA_CONTEXT_API
  120. )
  121. // Values for the ContextRobustness hint.
  122. const (
  123. NoRobustness int = C.GLFW_NO_ROBUSTNESS
  124. NoResetNotification int = C.GLFW_NO_RESET_NOTIFICATION
  125. LoseContextOnReset int = C.GLFW_LOSE_CONTEXT_ON_RESET
  126. )
  127. // Values for ContextReleaseBehavior hint.
  128. const (
  129. AnyReleaseBehavior int = C.GLFW_ANY_RELEASE_BEHAVIOR
  130. ReleaseBehaviorFlush int = C.GLFW_RELEASE_BEHAVIOR_FLUSH
  131. ReleaseBehaviorNone int = C.GLFW_RELEASE_BEHAVIOR_NONE
  132. )
  133. // Values for the OpenGLProfile hint.
  134. const (
  135. OpenGLAnyProfile int = C.GLFW_OPENGL_ANY_PROFILE
  136. OpenGLCoreProfile int = C.GLFW_OPENGL_CORE_PROFILE
  137. OpenGLCompatProfile int = C.GLFW_OPENGL_COMPAT_PROFILE
  138. )
  139. // Other values.
  140. const (
  141. True int = 1 // GL_TRUE
  142. False int = 0 // GL_FALSE
  143. DontCare int = C.GLFW_DONT_CARE
  144. )
  145. // Window represents a window.
  146. type Window struct {
  147. data *C.GLFWwindow
  148. // Window.
  149. fPosHolder func(w *Window, xpos int, ypos int)
  150. fSizeHolder func(w *Window, width int, height int)
  151. fFramebufferSizeHolder func(w *Window, width int, height int)
  152. fCloseHolder func(w *Window)
  153. fMaximizeHolder func(w *Window, maximized bool)
  154. fContentScaleHolder func(w *Window, x float32, y float32)
  155. fRefreshHolder func(w *Window)
  156. fFocusHolder func(w *Window, focused bool)
  157. fIconifyHolder func(w *Window, iconified bool)
  158. // Input.
  159. fMouseButtonHolder func(w *Window, button MouseButton, action Action, mod ModifierKey)
  160. fCursorPosHolder func(w *Window, xpos float64, ypos float64)
  161. fCursorEnterHolder func(w *Window, entered bool)
  162. fScrollHolder func(w *Window, xoff float64, yoff float64)
  163. fKeyHolder func(w *Window, key Key, scancode int, action Action, mods ModifierKey)
  164. fCharHolder func(w *Window, char rune)
  165. fCharModsHolder func(w *Window, char rune, mods ModifierKey)
  166. fDropHolder func(w *Window, names []string)
  167. }
  168. // Handle returns a *C.GLFWwindow reference (i.e. the GLFW window itself).
  169. // This can be used for passing the GLFW window handle to external libraries
  170. // like vulkan-go.
  171. func (w *Window) Handle() unsafe.Pointer {
  172. return unsafe.Pointer(w.data)
  173. }
  174. // GoWindow creates a Window from a *C.GLFWwindow reference.
  175. // Used when an external C library is calling your Go handlers.
  176. func GoWindow(window unsafe.Pointer) *Window {
  177. return &Window{data: (*C.GLFWwindow)(window)}
  178. }
  179. //export goWindowPosCB
  180. func goWindowPosCB(window unsafe.Pointer, xpos, ypos C.int) {
  181. w := windows.get((*C.GLFWwindow)(window))
  182. w.fPosHolder(w, int(xpos), int(ypos))
  183. }
  184. //export goWindowSizeCB
  185. func goWindowSizeCB(window unsafe.Pointer, width, height C.int) {
  186. w := windows.get((*C.GLFWwindow)(window))
  187. w.fSizeHolder(w, int(width), int(height))
  188. }
  189. //export goFramebufferSizeCB
  190. func goFramebufferSizeCB(window unsafe.Pointer, width, height C.int) {
  191. w := windows.get((*C.GLFWwindow)(window))
  192. w.fFramebufferSizeHolder(w, int(width), int(height))
  193. }
  194. //export goWindowCloseCB
  195. func goWindowCloseCB(window unsafe.Pointer) {
  196. w := windows.get((*C.GLFWwindow)(window))
  197. w.fCloseHolder(w)
  198. }
  199. //export goWindowMaximizeCB
  200. func goWindowMaximizeCB(window unsafe.Pointer, maximized C.int) {
  201. w := windows.get((*C.GLFWwindow)(window))
  202. w.fMaximizeHolder(w, glfwbool(maximized))
  203. }
  204. //export goWindowRefreshCB
  205. func goWindowRefreshCB(window unsafe.Pointer) {
  206. w := windows.get((*C.GLFWwindow)(window))
  207. w.fRefreshHolder(w)
  208. }
  209. //export goWindowFocusCB
  210. func goWindowFocusCB(window unsafe.Pointer, focused C.int) {
  211. w := windows.get((*C.GLFWwindow)(window))
  212. isFocused := glfwbool(focused)
  213. w.fFocusHolder(w, isFocused)
  214. }
  215. //export goWindowIconifyCB
  216. func goWindowIconifyCB(window unsafe.Pointer, iconified C.int) {
  217. isIconified := glfwbool(iconified)
  218. w := windows.get((*C.GLFWwindow)(window))
  219. w.fIconifyHolder(w, isIconified)
  220. }
  221. //export goWindowContentScaleCB
  222. func goWindowContentScaleCB(window unsafe.Pointer, x C.float, y C.float) {
  223. w := windows.get((*C.GLFWwindow)(window))
  224. w.fContentScaleHolder(w, float32(x), float32(y))
  225. }
  226. // DefaultWindowHints resets all window hints to their default values.
  227. //
  228. // This function may only be called from the main thread.
  229. func DefaultWindowHints() {
  230. C.glfwDefaultWindowHints()
  231. panicError()
  232. }
  233. // WindowHint sets hints for the next call to CreateWindow. The hints,
  234. // once set, retain their values until changed by a call to WindowHint or
  235. // DefaultWindowHints, or until the library is terminated with Terminate.
  236. //
  237. // This function may only be called from the main thread.
  238. func WindowHint(target Hint, hint int) {
  239. C.glfwWindowHint(C.int(target), C.int(hint))
  240. panicError()
  241. }
  242. // WindowHintString sets hints for the next call to CreateWindow. The hints,
  243. // once set, retain their values until changed by a call to this function or
  244. // DefaultWindowHints, or until the library is terminated.
  245. //
  246. // Only string type hints can be set with this function. Integer value hints are
  247. // set with WindowHint.
  248. //
  249. // This function does not check whether the specified hint values are valid. If
  250. // you set hints to invalid values this will instead be reported by the next
  251. // call to CreateWindow.
  252. //
  253. // Some hints are platform specific. These may be set on any platform but they
  254. // will only affect their specific platform. Other platforms will ignore them.
  255. // Setting these hints requires no platform specific headers or functions.
  256. //
  257. // This function must only be called from the main thread.
  258. func WindowHintString(hint Hint, value string) {
  259. str := C.CString(value)
  260. defer C.free(unsafe.Pointer(str))
  261. C.glfwWindowHintString(C.int(hint), str)
  262. }
  263. // CreateWindow creates a window and its associated context. Most of the options
  264. // controlling how the window and its context should be created are specified
  265. // through Hint.
  266. //
  267. // Successful creation does not change which context is current. Before you can
  268. // use the newly created context, you need to make it current using
  269. // MakeContextCurrent.
  270. //
  271. // Note that the created window and context may differ from what you requested,
  272. // as not all parameters and hints are hard constraints. This includes the size
  273. // of the window, especially for full screen windows. To retrieve the actual
  274. // attributes of the created window and context, use queries like
  275. // Window.GetAttrib and Window.GetSize.
  276. //
  277. // To create the window at a specific position, make it initially invisible using
  278. // the Visible window hint, set its position and then show it.
  279. //
  280. // If a fullscreen window is active, the screensaver is prohibited from starting.
  281. //
  282. // Windows: If the executable has an icon resource named GLFW_ICON, it will be
  283. // set as the icon for the window. If no such icon is present, the IDI_WINLOGO
  284. // icon will be used instead.
  285. //
  286. // Mac OS X: The GLFW window has no icon, as it is not a document window, but the
  287. // dock icon will be the same as the application bundle's icon. Also, the first
  288. // time a window is opened the menu bar is populated with common commands like
  289. // Hide, Quit and About. The (minimal) about dialog uses information from the
  290. // application's bundle. For more information on bundles, see the Bundle
  291. // Programming Guide provided by Apple.
  292. //
  293. // This function may only be called from the main thread.
  294. func CreateWindow(width, height int, title string, monitor *Monitor, share *Window) (*Window, error) {
  295. var (
  296. m *C.GLFWmonitor
  297. s *C.GLFWwindow
  298. )
  299. t := C.CString(title)
  300. defer C.free(unsafe.Pointer(t))
  301. if monitor != nil {
  302. m = monitor.data
  303. }
  304. if share != nil {
  305. s = share.data
  306. }
  307. w := C.glfwCreateWindow(C.int(width), C.int(height), t, m, s)
  308. if w == nil {
  309. return nil, acceptError(APIUnavailable, VersionUnavailable)
  310. }
  311. wnd := &Window{data: w}
  312. windows.put(wnd)
  313. return wnd, nil
  314. }
  315. // Destroy destroys the specified window and its context. On calling this
  316. // function, no further callbacks will be called for that window.
  317. //
  318. // This function may only be called from the main thread.
  319. func (w *Window) Destroy() {
  320. windows.remove(w.data)
  321. C.glfwDestroyWindow(w.data)
  322. panicError()
  323. }
  324. // ShouldClose reports the value of the close flag of the specified window.
  325. func (w *Window) ShouldClose() bool {
  326. ret := glfwbool(C.glfwWindowShouldClose(w.data))
  327. panicError()
  328. return ret
  329. }
  330. // SetShouldClose sets the value of the close flag of the window. This can be
  331. // used to override the user's attempt to close the window, or to signal that it
  332. // should be closed.
  333. func (w *Window) SetShouldClose(value bool) {
  334. if !value {
  335. C.glfwSetWindowShouldClose(w.data, C.int(False))
  336. } else {
  337. C.glfwSetWindowShouldClose(w.data, C.int(True))
  338. }
  339. panicError()
  340. }
  341. // SetTitle sets the window title, encoded as UTF-8, of the window.
  342. //
  343. // This function may only be called from the main thread.
  344. func (w *Window) SetTitle(title string) {
  345. t := C.CString(title)
  346. defer C.free(unsafe.Pointer(t))
  347. C.glfwSetWindowTitle(w.data, t)
  348. panicError()
  349. }
  350. // SetIcon sets the icon of the specified window. If passed an array of candidate images,
  351. // those of or closest to the sizes desired by the system are selected. If no images are
  352. // specified, the window reverts to its default icon.
  353. //
  354. // The image is ideally provided in the form of *image.NRGBA.
  355. // The pixels are 32-bit, little-endian, non-premultiplied RGBA, i.e. eight
  356. // bits per channel with the red channel first. They are arranged canonically
  357. // as packed sequential rows, starting from the top-left corner. If the image
  358. // type is not *image.NRGBA, it will be converted to it.
  359. //
  360. // The desired image sizes varies depending on platform and system settings. The selected
  361. // images will be rescaled as needed. Good sizes include 16x16, 32x32 and 48x48.
  362. func (w *Window) SetIcon(images []image.Image) {
  363. count := len(images)
  364. cimages := make([]C.GLFWimage, count)
  365. freePixels := make([]func(), count)
  366. for i, img := range images {
  367. var pixels []uint8
  368. b := img.Bounds()
  369. switch img := img.(type) {
  370. case *image.NRGBA:
  371. pixels = img.Pix
  372. default:
  373. m := image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
  374. draw.Draw(m, m.Bounds(), img, b.Min, draw.Src)
  375. pixels = m.Pix
  376. }
  377. pix, free := bytes(pixels)
  378. freePixels[i] = free
  379. cimages[i].width = C.int(b.Dx())
  380. cimages[i].height = C.int(b.Dy())
  381. cimages[i].pixels = (*C.uchar)(pix)
  382. }
  383. var p *C.GLFWimage
  384. if count > 0 {
  385. p = &cimages[0]
  386. }
  387. C.glfwSetWindowIcon(w.data, C.int(count), p)
  388. for _, v := range freePixels {
  389. v()
  390. }
  391. panicError()
  392. }
  393. // GetPos returns the position, in screen coordinates, of the upper-left
  394. // corner of the client area of the window.
  395. func (w *Window) GetPos() (x, y int) {
  396. var xpos, ypos C.int
  397. C.glfwGetWindowPos(w.data, &xpos, &ypos)
  398. panicError()
  399. return int(xpos), int(ypos)
  400. }
  401. // SetPos sets the position, in screen coordinates, of the upper-left corner
  402. // of the client area of the window.
  403. //
  404. // If it is a full screen window, this function does nothing.
  405. //
  406. // If you wish to set an initial window position you should create a hidden
  407. // window (using Hint and Visible), set its position and then show it.
  408. //
  409. // It is very rarely a good idea to move an already visible window, as it will
  410. // confuse and annoy the user.
  411. //
  412. // The window manager may put limits on what positions are allowed.
  413. //
  414. // This function may only be called from the main thread.
  415. func (w *Window) SetPos(xpos, ypos int) {
  416. C.glfwSetWindowPos(w.data, C.int(xpos), C.int(ypos))
  417. panicError()
  418. }
  419. // GetSize returns the size, in screen coordinates, of the client area of the
  420. // specified window.
  421. func (w *Window) GetSize() (width, height int) {
  422. var wi, h C.int
  423. C.glfwGetWindowSize(w.data, &wi, &h)
  424. panicError()
  425. return int(wi), int(h)
  426. }
  427. // SetSize sets the size, in screen coordinates, of the client area of the
  428. // window.
  429. //
  430. // For full screen windows, this function selects and switches to the resolution
  431. // closest to the specified size, without affecting the window's context. As the
  432. // context is unaffected, the bit depths of the framebuffer remain unchanged.
  433. //
  434. // The window manager may put limits on what window sizes are allowed.
  435. //
  436. // This function may only be called from the main thread.
  437. func (w *Window) SetSize(width, height int) {
  438. C.glfwSetWindowSize(w.data, C.int(width), C.int(height))
  439. panicError()
  440. }
  441. // SetSizeLimits sets the size limits of the client area of the specified window.
  442. // If the window is full screen or not resizable, this function does nothing.
  443. //
  444. // The size limits are applied immediately and may cause the window to be resized.
  445. func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) {
  446. C.glfwSetWindowSizeLimits(w.data, C.int(minw), C.int(minh), C.int(maxw), C.int(maxh))
  447. panicError()
  448. }
  449. // SetAspectRatio sets the required aspect ratio of the client area of the specified window.
  450. // If the window is full screen or not resizable, this function does nothing.
  451. //
  452. // The aspect ratio is specified as a numerator and a denominator and both values must be greater
  453. // than zero. For example, the common 16:9 aspect ratio is specified as 16 and 9, respectively.
  454. //
  455. // If the numerator and denominator is set to glfw.DontCare then the aspect ratio limit is disabled.
  456. //
  457. // The aspect ratio is applied immediately and may cause the window to be resized.
  458. func (w *Window) SetAspectRatio(numer, denom int) {
  459. C.glfwSetWindowAspectRatio(w.data, C.int(numer), C.int(denom))
  460. panicError()
  461. }
  462. // GetFramebufferSize retrieves the size, in pixels, of the framebuffer of the
  463. // specified window.
  464. func (w *Window) GetFramebufferSize() (width, height int) {
  465. var wi, h C.int
  466. C.glfwGetFramebufferSize(w.data, &wi, &h)
  467. panicError()
  468. return int(wi), int(h)
  469. }
  470. // GetFrameSize retrieves the size, in screen coordinates, of each edge of the frame
  471. // of the specified window. This size includes the title bar, if the window has one.
  472. // The size of the frame may vary depending on the window-related hints used to create it.
  473. //
  474. // Because this function retrieves the size of each window frame edge and not the offset
  475. // along a particular coordinate axis, the retrieved values will always be zero or positive.
  476. func (w *Window) GetFrameSize() (left, top, right, bottom int) {
  477. var l, t, r, b C.int
  478. C.glfwGetWindowFrameSize(w.data, &l, &t, &r, &b)
  479. panicError()
  480. return int(l), int(t), int(r), int(b)
  481. }
  482. // GetContentScale function retrieves the content scale for the specified
  483. // window. The content scale is the ratio between the current DPI and the
  484. // platform's default DPI. If you scale all pixel dimensions by this scale then
  485. // your content should appear at an appropriate size. This is especially
  486. // important for text and any UI elements.
  487. //
  488. // This function may only be called from the main thread.
  489. func (w *Window) GetContentScale() (float32, float32) {
  490. var x, y C.float
  491. C.glfwGetWindowContentScale(w.data, &x, &y)
  492. return float32(x), float32(y)
  493. }
  494. // GetOpacity function returns the opacity of the window, including any
  495. // decorations.
  496. //
  497. // The opacity (or alpha) value is a positive finite number between zero and
  498. // one, where zero is fully transparent and one is fully opaque. If the system
  499. // does not support whole window transparency, this function always returns one.
  500. //
  501. // The initial opacity value for newly created windows is one.
  502. //
  503. // This function may only be called from the main thread.
  504. func (w *Window) GetOpacity() float32 {
  505. return float32(C.glfwGetWindowOpacity(w.data))
  506. }
  507. // SetOpacity function sets the opacity of the window, including any
  508. // decorations. The opacity (or alpha) value is a positive finite number between
  509. // zero and one, where zero is fully transparent and one is fully opaque.
  510. //
  511. // The initial opacity value for newly created windows is one.
  512. //
  513. // A window created with framebuffer transparency may not use whole window
  514. // transparency. The results of doing this are undefined.
  515. //
  516. // This function may only be called from the main thread.
  517. func (w *Window) SetOpacity(opacity float32) {
  518. C.glfwSetWindowOpacity(w.data, C.float(opacity))
  519. }
  520. // RequestWindowAttention funciton requests user attention to the specified
  521. // window. On platforms where this is not supported, attention is requested to
  522. // the application as a whole.
  523. //
  524. // Once the user has given attention, usually by focusing the window or
  525. // application, the system will end the request automatically.
  526. //
  527. // This function must only be called from the main thread.
  528. func (w *Window) RequestAttention() {
  529. C.glfwRequestWindowAttention(w.data)
  530. }
  531. // Focus brings the specified window to front and sets input focus.
  532. // The window should already be visible and not iconified.
  533. //
  534. // By default, both windowed and full screen mode windows are focused when initially created.
  535. // Set the glfw.Focused to disable this behavior.
  536. //
  537. // Do not use this function to steal focus from other applications unless you are certain that
  538. // is what the user wants. Focus stealing can be extremely disruptive.
  539. func (w *Window) Focus() {
  540. C.glfwFocusWindow(w.data)
  541. }
  542. // Iconify iconifies/minimizes the window, if it was previously restored. If it
  543. // is a full screen window, the original monitor resolution is restored until the
  544. // window is restored. If the window is already iconified, this function does
  545. // nothing.
  546. //
  547. // This function may only be called from the main thread.
  548. func (w *Window) Iconify() {
  549. C.glfwIconifyWindow(w.data)
  550. }
  551. // Maximize maximizes the specified window if it was previously not maximized.
  552. // If the window is already maximized, this function does nothing.
  553. //
  554. // If the specified window is a full screen window, this function does nothing.
  555. func (w *Window) Maximize() {
  556. C.glfwMaximizeWindow(w.data)
  557. }
  558. // Restore restores the window, if it was previously iconified/minimized. If it
  559. // is a full screen window, the resolution chosen for the window is restored on
  560. // the selected monitor. If the window is already restored, this function does
  561. // nothing.
  562. //
  563. // This function may only be called from the main thread.
  564. func (w *Window) Restore() {
  565. C.glfwRestoreWindow(w.data)
  566. }
  567. // Show makes the window visible, if it was previously hidden. If the window is
  568. // already visible or is in full screen mode, this function does nothing.
  569. //
  570. // This function may only be called from the main thread.
  571. func (w *Window) Show() {
  572. C.glfwShowWindow(w.data)
  573. panicError()
  574. }
  575. // Hide hides the window, if it was previously visible. If the window is already
  576. // hidden or is in full screen mode, this function does nothing.
  577. //
  578. // This function may only be called from the main thread.
  579. func (w *Window) Hide() {
  580. C.glfwHideWindow(w.data)
  581. panicError()
  582. }
  583. // GetMonitor returns the handle of the monitor that the window is in
  584. // fullscreen on.
  585. //
  586. // Returns nil if the window is in windowed mode.
  587. func (w *Window) GetMonitor() *Monitor {
  588. m := C.glfwGetWindowMonitor(w.data)
  589. panicError()
  590. if m == nil {
  591. return nil
  592. }
  593. return &Monitor{m}
  594. }
  595. // SetMonitor sets the monitor that the window uses for full screen mode or,
  596. // if the monitor is NULL, makes it windowed mode.
  597. //
  598. // When setting a monitor, this function updates the width, height and refresh
  599. // rate of the desired video mode and switches to the video mode closest to it.
  600. // The window position is ignored when setting a monitor.
  601. //
  602. // When the monitor is NULL, the position, width and height are used to place
  603. // the window client area. The refresh rate is ignored when no monitor is specified.
  604. // If you only wish to update the resolution of a full screen window or the size of
  605. // a windowed mode window, see window.SetSize.
  606. //
  607. // When a window transitions from full screen to windowed mode, this function
  608. // restores any previous window settings such as whether it is decorated, floating,
  609. // resizable, has size or aspect ratio limits, etc..
  610. func (w *Window) SetMonitor(monitor *Monitor, xpos, ypos, width, height, refreshRate int) {
  611. var m *C.GLFWmonitor
  612. if monitor == nil {
  613. m = nil
  614. } else {
  615. m = monitor.data
  616. }
  617. C.glfwSetWindowMonitor(w.data, m, C.int(xpos), C.int(ypos), C.int(width), C.int(height), C.int(refreshRate))
  618. panicError()
  619. }
  620. // GetAttrib returns an attribute of the window. There are many attributes,
  621. // some related to the window and others to its context.
  622. func (w *Window) GetAttrib(attrib Hint) int {
  623. ret := int(C.glfwGetWindowAttrib(w.data, C.int(attrib)))
  624. panicError()
  625. return ret
  626. }
  627. // SetAttrib function sets the value of an attribute of the specified window.
  628. //
  629. // The supported attributes are Decorated, Resizeable, Floating and AutoIconify.
  630. //
  631. // Some of these attributes are ignored for full screen windows. The new value
  632. // will take effect if the window is later made windowed.
  633. //
  634. // Some of these attributes are ignored for windowed mode windows. The new value
  635. // will take effect if the window is later made full screen.
  636. //
  637. // This function may only be called from the main thread.
  638. func (w *Window) SetAttrib(attrib Hint, value int) {
  639. C.glfwSetWindowAttrib(w.data, C.int(attrib), C.int(value))
  640. }
  641. // SetUserPointer sets the user-defined pointer of the window. The current value
  642. // is retained until the window is destroyed. The initial value is nil.
  643. func (w *Window) SetUserPointer(pointer unsafe.Pointer) {
  644. C.glfwSetWindowUserPointer(w.data, pointer)
  645. panicError()
  646. }
  647. // GetUserPointer returns the current value of the user-defined pointer of the
  648. // window. The initial value is nil.
  649. func (w *Window) GetUserPointer() unsafe.Pointer {
  650. ret := C.glfwGetWindowUserPointer(w.data)
  651. panicError()
  652. return ret
  653. }
  654. // PosCallback is the window position callback.
  655. type PosCallback func(w *Window, xpos int, ypos int)
  656. // SetPosCallback sets the position callback of the window, which is called
  657. // when the window is moved. The callback is provided with the screen position
  658. // of the upper-left corner of the client area of the window.
  659. func (w *Window) SetPosCallback(cbfun PosCallback) (previous PosCallback) {
  660. previous = w.fPosHolder
  661. w.fPosHolder = cbfun
  662. if cbfun == nil {
  663. C.glfwSetWindowPosCallback(w.data, nil)
  664. } else {
  665. C.glfwSetWindowPosCallbackCB(w.data)
  666. }
  667. panicError()
  668. return previous
  669. }
  670. // SizeCallback is the window size callback.
  671. type SizeCallback func(w *Window, width int, height int)
  672. // SetSizeCallback sets the size callback of the window, which is called when
  673. // the window is resized. The callback is provided with the size, in screen
  674. // coordinates, of the client area of the window.
  675. func (w *Window) SetSizeCallback(cbfun SizeCallback) (previous SizeCallback) {
  676. previous = w.fSizeHolder
  677. w.fSizeHolder = cbfun
  678. if cbfun == nil {
  679. C.glfwSetWindowSizeCallback(w.data, nil)
  680. } else {
  681. C.glfwSetWindowSizeCallbackCB(w.data)
  682. }
  683. panicError()
  684. return previous
  685. }
  686. // FramebufferSizeCallback is the framebuffer size callback.
  687. type FramebufferSizeCallback func(w *Window, width int, height int)
  688. // SetFramebufferSizeCallback sets the framebuffer resize callback of the specified
  689. // window, which is called when the framebuffer of the specified window is resized.
  690. func (w *Window) SetFramebufferSizeCallback(cbfun FramebufferSizeCallback) (previous FramebufferSizeCallback) {
  691. previous = w.fFramebufferSizeHolder
  692. w.fFramebufferSizeHolder = cbfun
  693. if cbfun == nil {
  694. C.glfwSetFramebufferSizeCallback(w.data, nil)
  695. } else {
  696. C.glfwSetFramebufferSizeCallbackCB(w.data)
  697. }
  698. panicError()
  699. return previous
  700. }
  701. // CloseCallback is the window close callback.
  702. type CloseCallback func(w *Window)
  703. // SetCloseCallback sets the close callback of the window, which is called when
  704. // the user attempts to close the window, for example by clicking the close
  705. // widget in the title bar.
  706. //
  707. // The close flag is set before this callback is called, but you can modify it at
  708. // any time with SetShouldClose.
  709. //
  710. // Mac OS X: Selecting Quit from the application menu will trigger the close
  711. // callback for all windows.
  712. func (w *Window) SetCloseCallback(cbfun CloseCallback) (previous CloseCallback) {
  713. previous = w.fCloseHolder
  714. w.fCloseHolder = cbfun
  715. if cbfun == nil {
  716. C.glfwSetWindowCloseCallback(w.data, nil)
  717. } else {
  718. C.glfwSetWindowCloseCallbackCB(w.data)
  719. }
  720. panicError()
  721. return previous
  722. }
  723. // MaximizeCallback is the function signature for window maximize callback
  724. // functions.
  725. type MaximizeCallback func(w *Window, maximized bool)
  726. // SetMaximizeCallback sets the maximization callback of the specified window,
  727. // which is called when the window is maximized or restored.
  728. //
  729. // This function must only be called from the main thread.
  730. func (w *Window) SetMaximizeCallback(cbfun MaximizeCallback) MaximizeCallback {
  731. previous := w.fMaximizeHolder
  732. w.fMaximizeHolder = cbfun
  733. if cbfun == nil {
  734. C.glfwSetWindowMaximizeCallback(w.data, nil)
  735. } else {
  736. C.glfwSetWindowMaximizeCallbackCB(w.data)
  737. }
  738. return previous
  739. }
  740. // ContentScaleCallback is the function signature for window content scale
  741. // callback functions.
  742. type ContentScaleCallback func(w *Window, x float32, y float32)
  743. // SetContentScaleCallback function sets the window content scale callback of
  744. // the specified window, which is called when the content scale of the specified
  745. // window changes.
  746. //
  747. // This function must only be called from the main thread.
  748. func (w *Window) SetContentScaleCallback(cbfun ContentScaleCallback) ContentScaleCallback {
  749. previous := w.fContentScaleHolder
  750. w.fContentScaleHolder = cbfun
  751. if cbfun == nil {
  752. C.glfwSetWindowContentScaleCallback(w.data, nil)
  753. } else {
  754. C.glfwSetWindowContentScaleCallbackCB(w.data)
  755. }
  756. return previous
  757. }
  758. // RefreshCallback is the window refresh callback.
  759. type RefreshCallback func(w *Window)
  760. // SetRefreshCallback sets the refresh callback of the window, which
  761. // is called when the client area of the window needs to be redrawn, for example
  762. // if the window has been exposed after having been covered by another window.
  763. //
  764. // On compositing window systems such as Aero, Compiz or Aqua, where the window
  765. // contents are saved off-screen, this callback may be called only very
  766. // infrequently or never at all.
  767. func (w *Window) SetRefreshCallback(cbfun RefreshCallback) (previous RefreshCallback) {
  768. previous = w.fRefreshHolder
  769. w.fRefreshHolder = cbfun
  770. if cbfun == nil {
  771. C.glfwSetWindowRefreshCallback(w.data, nil)
  772. } else {
  773. C.glfwSetWindowRefreshCallbackCB(w.data)
  774. }
  775. panicError()
  776. return previous
  777. }
  778. // FocusCallback is the window focus callback.
  779. type FocusCallback func(w *Window, focused bool)
  780. // SetFocusCallback sets the focus callback of the window, which is called when
  781. // the window gains or loses focus.
  782. //
  783. // After the focus callback is called for a window that lost focus, synthetic key
  784. // and mouse button release events will be generated for all such that had been
  785. // pressed. For more information, see SetKeyCallback and SetMouseButtonCallback.
  786. func (w *Window) SetFocusCallback(cbfun FocusCallback) (previous FocusCallback) {
  787. previous = w.fFocusHolder
  788. w.fFocusHolder = cbfun
  789. if cbfun == nil {
  790. C.glfwSetWindowFocusCallback(w.data, nil)
  791. } else {
  792. C.glfwSetWindowFocusCallbackCB(w.data)
  793. }
  794. panicError()
  795. return previous
  796. }
  797. // IconifyCallback is the window iconification callback.
  798. type IconifyCallback func(w *Window, iconified bool)
  799. // SetIconifyCallback sets the iconification callback of the window, which is
  800. // called when the window is iconified or restored.
  801. func (w *Window) SetIconifyCallback(cbfun IconifyCallback) (previous IconifyCallback) {
  802. previous = w.fIconifyHolder
  803. w.fIconifyHolder = cbfun
  804. if cbfun == nil {
  805. C.glfwSetWindowIconifyCallback(w.data, nil)
  806. } else {
  807. C.glfwSetWindowIconifyCallbackCB(w.data)
  808. }
  809. panicError()
  810. return previous
  811. }
  812. // SetClipboardString sets the system clipboard to the specified UTF-8 encoded
  813. // string.
  814. //
  815. // Ownership to the Window is no longer necessary, see
  816. // glfw.SetClipboardString(string)
  817. //
  818. // This function may only be called from the main thread.
  819. func (w *Window) SetClipboardString(str string) {
  820. cp := C.CString(str)
  821. defer C.free(unsafe.Pointer(cp))
  822. C.glfwSetClipboardString(w.data, cp)
  823. panicError()
  824. }
  825. // GetClipboardString returns the contents of the system clipboard, if it
  826. // contains or is convertible to a UTF-8 encoded string.
  827. //
  828. // Ownership to the Window is no longer necessary, see
  829. // glfw.GetClipboardString()
  830. //
  831. // This function may only be called from the main thread.
  832. func (w *Window) GetClipboardString() string {
  833. cs := C.glfwGetClipboardString(w.data)
  834. if cs == nil {
  835. acceptError(FormatUnavailable)
  836. return ""
  837. }
  838. return C.GoString(cs)
  839. }
  840. // panicErrorExceptForInvalidValue is the same as panicError but ignores
  841. // invalidValue.
  842. func panicErrorExceptForInvalidValue() {
  843. // invalidValue can happen when specific joysticks are used. This issue
  844. // will be fixed in GLFW 3.3.5. As a temporary fix, ignore this error.
  845. // See go-gl/glfw#292, go-gl/glfw#324, and glfw/glfw#1763.
  846. err := acceptError(invalidValue)
  847. if e, ok := err.(*Error); ok && e.Code == invalidValue {
  848. return
  849. }
  850. if err != nil {
  851. panic(err)
  852. }
  853. }
  854. // PollEvents processes only those events that have already been received and
  855. // then returns immediately. Processing events will cause the window and input
  856. // callbacks associated with those events to be called.
  857. //
  858. // This function is not required for joystick input to work.
  859. //
  860. // This function may not be called from a callback.
  861. //
  862. // This function may only be called from the main thread.
  863. func PollEvents() {
  864. C.glfwPollEvents()
  865. panicErrorExceptForInvalidValue()
  866. }
  867. // WaitEvents puts the calling thread to sleep until at least one event has been
  868. // received. Once one or more events have been recevied, it behaves as if
  869. // PollEvents was called, i.e. the events are processed and the function then
  870. // returns immediately. Processing events will cause the window and input
  871. // callbacks associated with those events to be called.
  872. //
  873. // Since not all events are associated with callbacks, this function may return
  874. // without a callback having been called even if you are monitoring all
  875. // callbacks.
  876. //
  877. // This function may not be called from a callback.
  878. //
  879. // This function may only be called from the main thread.
  880. func WaitEvents() {
  881. C.glfwWaitEvents()
  882. panicErrorExceptForInvalidValue()
  883. }
  884. // WaitEventsTimeout puts the calling thread to sleep until at least one event is available in the
  885. // event queue, or until the specified timeout is reached. If one or more events are available,
  886. // it behaves exactly like PollEvents, i.e. the events in the queue are processed and the function
  887. // then returns immediately. Processing events will cause the window and input callbacks associated
  888. // with those events to be called.
  889. //
  890. // The timeout value must be a positive finite number.
  891. //
  892. // Since not all events are associated with callbacks, this function may return without a callback
  893. // having been called even if you are monitoring all callbacks.
  894. //
  895. // On some platforms, a window move, resize or menu operation will cause event processing to block.
  896. // This is due to how event processing is designed on those platforms. You can use the window
  897. // refresh callback to redraw the contents of your window when necessary during such operations.
  898. //
  899. // On some platforms, certain callbacks may be called outside of a call to one of the event
  900. // processing functions.
  901. //
  902. // If no windows exist, this function returns immediately. For synchronization of threads in
  903. // applications that do not create windows, use native Go primitives.
  904. //
  905. // Event processing is not required for joystick input to work.
  906. func WaitEventsTimeout(timeout float64) {
  907. C.glfwWaitEventsTimeout(C.double(timeout))
  908. panicErrorExceptForInvalidValue()
  909. }
  910. // PostEmptyEvent posts an empty event from the current thread to the main
  911. // thread event queue, causing WaitEvents to return.
  912. //
  913. // If no windows exist, this function returns immediately. For synchronization of threads in
  914. // applications that do not create windows, use native Go primitives.
  915. //
  916. // This function may be called from secondary threads.
  917. func PostEmptyEvent() {
  918. C.glfwPostEmptyEvent()
  919. panicError()
  920. }