Graphic-Window

Wraps the OS specific window and its OpenGL specific extension into a thin layer to make other modules OS independent.

Supports OpenGL / GLES for drawing operations.

Summary
Graphic-WindowWraps the OS specific window and its OpenGL specific extension into a thin layer to make other modules OS independent.
CopyrightThis program is free software.
Files
C-kern/api/graphic/window.hHeader file Graphic-Window.
C-kern/graphic/window.cImplementation file Graphic-Window impl.
Types
struct window_tExport window_t into global namespace.
struct window_evh_tExport window_evh_t into global namespace.
Functions
test
unittest_graphic_windowTest window_t functionality.
window_evh_tCallback interface for handling generic window events.
oncloseThe event handler is called if the user requested to close the window.
ondestroyThe event handler is called if the window was destroyed by another process.
onredrawThe event handler is called if the window was (partially) obscured and the obscured content has to be redrawn.
onreshapeThe event handler is called whenever the geometry of the window changes.
onvisibleThe event handler is called whenever the window changes from hidden to shown state or vice versa.
generic
window_evh_INITStatic initializer.
generic
genericcast_windowevhCasts parameter evhimpl into pointer to interface window_evh_t.
window_evh_DECLAREDeclares an interface to handle window events.
window_tWraps a native window and its OpenGL specific wrapper (if needed).
lifetime
window_FREEStatic initializer.
init_windowCreates a new native window and its OpenGL extension/wrapper type.
free_windowFrees win and its associated resources like native windows.
query
gl_windowReturns a pointer to a native opengl window.
os_windowReturns a pointer to a native window.
display_windowReturns a pointer to the display_t the window is associated with.
isvisible_windowReturns true if window is visible on the screen.
pos_windowReturns the position of the window in screen coordinates.
size_windowReturns the width and height of win in pixels.
update
show_windowMakes window visible to the user.
hide_windowHides win and removes it from the screen.
setpos_windowChanges the position of the window on the screen.
resize_windowChanges the size of the window.
sendclose_windowSends a close request to win.
sendredraw_windowSends a redraw event to win.
OpenGL
swapbuffer_windowSwaps the content of the fron buffer with the back buffer.
inline implementation
window_t
gl_windowImplements window_t.gl_window.
os_windowImplements window_t.os_window.
display_windowImplements window_t.display_window.
hide_windowImplements window_t.hide_window.
isvisible_windowImplements window_t.isvisible_window.
pos_windowImplements window_t.pos_window.
resize_windowImplements window_t.resize_window.
sendclose_windowImplements window_t.sendclose_window.
sendredraw_windowImplements window_t.sendredraw_window.
setpos_windowImplements window_t.setpos_window.
show_windowImplements window_t.show_window.
size_windowImplements window_t.size_window.
window_evh_t
genericcast_windowevhImplements window_evh_t.genericcast_windowevh.
window_evh_DECLAREImplements window_evh_t.window_evh_DECLARE.

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2014 Jörg Seebohn

Files

C-kern/api/graphic/window.h

Header file Graphic-Window.

C-kern/graphic/window.c

Implementation file Graphic-Window impl.

Types

struct window_t

typedef struct window_t window_t

Export window_t into global namespace.

struct window_evh_t

typedef struct window_evh_t window_evh_t

Export window_evh_t into global namespace.

Functions

Summary

test

unittest_graphic_window

int unittest_graphic_window(void)

Test window_t functionality.

window_evh_t

struct window_evh_t

Callback interface for handling generic window events.

Summary
oncloseThe event handler is called if the user requested to close the window.
ondestroyThe event handler is called if the window was destroyed by another process.
onredrawThe event handler is called if the window was (partially) obscured and the obscured content has to be redrawn.
onreshapeThe event handler is called whenever the geometry of the window changes.
onvisibleThe event handler is called whenever the window changes from hidden to shown state or vice versa.
generic
window_evh_INITStatic initializer.
generic
genericcast_windowevhCasts parameter evhimpl into pointer to interface window_evh_t.
window_evh_DECLAREDeclares an interface to handle window events.

onclose

void (*onclose) (window_t * win)

The event handler is called if the user requested to close the window.  You can save state information before calling <free_window>.

ondestroy

void (*ondestroy) (window_t * win)

The event handler is called if the window was destroyed by another process.  You must call <free_window> - any other function which uses window_t as parameter would cause an error which would cause the process to abort in case of X11.  Not calling <free_window> results in a memory leak.  Calling <free_window> in response to an onclose event does not trigger an ondestroy callback.

onredraw

void (*onredraw) (window_t * win)

The event handler is called if the window was (partially) obscured and the obscured content has to be redrawn.

onreshape

void (*onreshape) (window_t * win, uint32_t width, uint32_t height)

The event handler is called whenever the geometry of the window changes.  The x and y coordinates can be queried for with a call to pos_window.

onvisible

void (*onvisible) (window_t * win, bool isVisible)

The event handler is called whenever the window changes from hidden to shown state or vice versa.  If isVisible is set the window is in a shown state else it is in a hidden state and not visible.

generic

window_evh_INIT

#define window_evh_INIT(
   subwindow_fct_suffix
) { & onclose ## subwindow_fct_suffix, & ondestroy ## subwindow_fct_suffix, & onredraw ## subwindow_fct_suffix, & onreshape ## subwindow_fct_suffix, & onvisible ## subwindow_fct_suffix }

Static initializer.

generic

genericcast_windowevh

const window_evh_t * genericcast_windowevh(const void *evhimpl,
TYPENAME subwindow_t)

Casts parameter evhimpl into pointer to interface window_evh_t.  The parameter evhimpl has to be of type pointer to type declared with window_evh_DECLARE.  The other parameters must be the same as in window_evh_DECLARE without the first.

window_evh_DECLARE

void window_evh_DECLARE(TYPENAME declared_evh_t,
TYPENAME subwindow_t)

Declares an interface to handle window events.  The interface is structural compatible with window_evh_t.  See window_evh_t for a list of declared functions.

Parameter

declared_evh_tThe name of the structure which is declared as the interface.  The name should have the suffix “_evh_t”.
subwindow_tThe window subtype for which declared_evh_t implements the callback functions.

window_t

struct window_t

Wraps a native window and its OpenGL specific wrapper (if needed).

Summary
lifetime
window_FREEStatic initializer.
init_windowCreates a new native window and its OpenGL extension/wrapper type.
free_windowFrees win and its associated resources like native windows.
query
gl_windowReturns a pointer to a native opengl window.
os_windowReturns a pointer to a native window.
display_windowReturns a pointer to the display_t the window is associated with.
isvisible_windowReturns true if window is visible on the screen.
pos_windowReturns the position of the window in screen coordinates.
size_windowReturns the width and height of win in pixels.
update
show_windowMakes window visible to the user.
hide_windowHides win and removes it from the screen.
setpos_windowChanges the position of the window on the screen.
resize_windowChanges the size of the window.
sendclose_windowSends a close request to win.
sendredraw_windowSends a redraw event to win.
OpenGL
swapbuffer_windowSwaps the content of the fron buffer with the back buffer.

lifetime

window_FREE

#define window_FREE { x11window_FREE, surface_FREE_EMBEDDED }

Static initializer.

init_window

int init_window(/*out*/window_t *win,
struct display_t *disp,
uint32_t screennr,
const struct window_evh_t *eventhandler,
struct gconfig_t *gconf,
struct windowconfig_t *winattr)

Creates a new native window and its OpenGL extension/wrapper type.  A copy of the pointer disp is stored internally so do not free display_t disp until win has been freed.

free_window

int free_window(window_t *win)

Frees win and its associated resources like native windows.  Before you call this function make sure that every other resource which depends on win is freed.

query

gl_window

struct opengl_surface_t * gl_window(const window_t *win)

Returns a pointer to a native opengl window.

os_window

void * os_window(const window_t *win)

Returns a pointer to a native window.  This function is implemented as a macro and therefore returns a pointer to the real native type and not void.  It is possible that gl_window returns the same pointer except for the type.  In case of EGL as OpenGL adaption layer both pointers differ.

display_window

struct display_t * display_window(const window_t *win)

Returns a pointer to the display_t the window is associated with.

isvisible_window

bool isvisible_window(const window_t *win)

Returns true if window is visible on the screen.  In case of true it may be obscured by another window nonetheless.

pos_window

int pos_window(const window_t *win,
/*out*/int32_t *screen_x,
/*out*/int32_t *screen_y)

Returns the position of the window in screen coordinates.  The value (screen_x == 0, screen_y == 0) denotes the top left corner of the screen.

size_window

int size_window(const window_t *win,
/*out*/uint32_t *width,
/*out*/uint32_t *height)

Returns the width and height of win in pixels.

update

show_window

int show_window(window_t *win)

Makes window visible to the user.  After the event received from the windowing system has been processed the window state is changed to visible - see isvisible_window.

hide_window

int hide_window(window_t *win)

Hides win and removes it from the screen.  After the event received from the windowing system has been processed the window state is changed to hidden - see isvisible_window.

setpos_window

int setpos_window(window_t *win,
int32_t screen_x,
int32_t screen_y)

Changes the position of the window on the screen.  Coordinates are in screen coordinates.  The coordinate (0,0) is top left.

resize_window

int resize_window(window_t *win,
uint32_t width,
uint32_t height)

Changes the size of the window.  Parameter width and height must be > 0 else result is undefined.

sendclose_window

int sendclose_window(window_t *win)

Sends a close request to win.  The registered eventhandler receives an window_evh_t.onclose event.  If no event handler is registered the event is ignored.

sendredraw_window

int sendredraw_window(window_t *win)

Sends a redraw event to win.  The registered eventhandler receives an window_evh_t.onredraw event.  If no event handler is registered the event is ignored.

OpenGL

swapbuffer_window

int swapbuffer_window(window_t *win,
struct display_t *disp)

Swaps the content of the fron buffer with the back buffer.  Draw operations go to the back buffer.  The front is shown on the screen.  After return the content of the back buffer is undefined.

window_t

gl_window

#define gl_window(win) gl_surface(win)

Implements window_t.gl_window.

os_window

#define os_window(win) (&(win)->oswindow)

Implements window_t.os_window.

display_window

#define display_window(
   win
) (castfromos_display(display_x11window(os_window(win))))

Implements window_t.display_window.

hide_window

#define hide_window(win) hide_x11window(os_window(win))

Implements window_t.hide_window.

isvisible_window

#define isvisible_window(
   win
) (x11window_state_SHOWN == state_x11window(os_window(win)))

Implements window_t.isvisible_window.

pos_window

#define pos_window(win,
screen_x,
screen_y) pos_x11window(os_window(win), screen_x, screen_y)

Implements window_t.pos_window.

resize_window

#define resize_window(win,
width,
height) resize_x11window(os_window(win), width, height)

Implements window_t.resize_window.

sendclose_window

#define sendclose_window(win) sendclose_x11window(os_window(win))

Implements window_t.sendclose_window.

sendredraw_window

#define sendredraw_window(win) sendredraw_x11window(os_window(win))

Implements window_t.sendredraw_window.

setpos_window

#define setpos_window(
   win,
   screen_x,
   screen_y
) setpos_x11window(os_window(win), screen_x, screen_y)

Implements window_t.setpos_window.

show_window

#define show_window(win) show_x11window(os_window(win))

Implements window_t.show_window.

size_window

#define size_window(win,
width,
height) size_x11window(os_window(win), width, height)

Implements window_t.size_window.

window_evh_t

genericcast_windowevh

#define genericcast_windowevh(
   evhimpl,
   subwindow_t
) ( __extension__ ({ static_assert( offsetof(typeof(*(evhimpl)), onclose) == offsetof(window_evh_t, onclose) && offsetof(typeof(*(evhimpl)),ondestroy) == offsetof(window_evh_t, ondestroy) && offsetof(typeof(*(evhimpl)), onredraw) == offsetof(window_evh_t, onredraw) && offsetof(typeof(*(evhimpl)),onreshape) == offsetof(window_evh_t, onreshape) && offsetof(typeof(*(evhimpl)),onvisible) == offsetof(window_evh_t, onvisible), "ensure same structure"); if (0) { (evhimpl)->onclose((subwindow_t*)0); (evhimpl)->ondestroy((subwindow_t*)0); (evhimpl)->onredraw((subwindow_t*)0); (evhimpl)->onreshape((subwindow_t*)0, (uint32_t)0, (uint32_t)0); (evhimpl)->onvisible((subwindow_t*)0, (bool)0); } (const window_evh_t*) (evhimpl); }))

Implements window_evh_t.genericcast_windowevh.

window_evh_DECLARE

#define window_evh_DECLARE(
   declared_evh_t,
   subwindow_t
) typedef struct declared_evh_t declared_evh_t; struct declared_evh_t { void (* onclose) (subwindow_t * win); void (* ondestroy) (subwindow_t * win); void (* onredraw) (subwindow_t * win); void (* onreshape) (subwindow_t * win, uint32_t width, uint32_t height); void (* onvisible) (subwindow_t * win, bool isVisible); }

Implements window_evh_t.window_evh_DECLARE.

Wraps the OS specific window and its OpenGL specific extension into a thin layer to make other modules OS independent.
Implements Graphic-Window.
typedef struct window_t window_t
Export window_t into global namespace.
struct window_t
Wraps a native window and its OpenGL specific wrapper (if needed).
typedef struct window_evh_t window_evh_t
Export window_evh_t into global namespace.
struct window_evh_t
Callback interface for handling generic window events.
int unittest_graphic_window(void)
Test window_t functionality.
void (*onclose) (window_t * win)
The event handler is called if the user requested to close the window.
void (*ondestroy) (window_t * win)
The event handler is called if the window was destroyed by another process.
void (*onredraw) (window_t * win)
The event handler is called if the window was (partially) obscured and the obscured content has to be redrawn.
void (*onreshape) (window_t * win, uint32_t width, uint32_t height)
The event handler is called whenever the geometry of the window changes.
void (*onvisible) (window_t * win, bool isVisible)
The event handler is called whenever the window changes from hidden to shown state or vice versa.
#define window_evh_INIT(
   subwindow_fct_suffix
) { & onclose ## subwindow_fct_suffix, & ondestroy ## subwindow_fct_suffix, & onredraw ## subwindow_fct_suffix, & onreshape ## subwindow_fct_suffix, & onvisible ## subwindow_fct_suffix }
Static initializer.
const window_evh_t * genericcast_windowevh(const void *evhimpl,
TYPENAME subwindow_t)
Casts parameter evhimpl into pointer to interface window_evh_t.
void window_evh_DECLARE(TYPENAME declared_evh_t,
TYPENAME subwindow_t)
Declares an interface to handle window events.
#define window_FREE { x11window_FREE, surface_FREE_EMBEDDED }
Static initializer.
int init_window(/*out*/window_t *win,
struct display_t *disp,
uint32_t screennr,
const struct window_evh_t *eventhandler,
struct gconfig_t *gconf,
struct windowconfig_t *winattr)
Creates a new native window and its OpenGL extension/wrapper type.
int free_window(window_t *win)
Frees win and its associated resources like native windows.
struct opengl_surface_t * gl_window(const window_t *win)
Returns a pointer to a native opengl window.
void * os_window(const window_t *win)
Returns a pointer to a native window.
struct display_t * display_window(const window_t *win)
Returns a pointer to the display_t the window is associated with.
struct display_t
Wraps the OS specific graphics display.
bool isvisible_window(const window_t *win)
Returns true if window is visible on the screen.
int pos_window(const window_t *win,
/*out*/int32_t *screen_x,
/*out*/int32_t *screen_y)
Returns the position of the window in screen coordinates.
int size_window(const window_t *win,
/*out*/uint32_t *width,
/*out*/uint32_t *height)
Returns the width and height of win in pixels.
int show_window(window_t *win)
Makes window visible to the user.
int hide_window(window_t *win)
Hides win and removes it from the screen.
int setpos_window(window_t *win,
int32_t screen_x,
int32_t screen_y)
Changes the position of the window on the screen.
int resize_window(window_t *win,
uint32_t width,
uint32_t height)
Changes the size of the window.
int sendclose_window(window_t *win)
Sends a close request to win.
int sendredraw_window(window_t *win)
Sends a redraw event to win.
int swapbuffer_window(window_t *win,
struct display_t *disp)
Swaps the content of the fron buffer with the back buffer.
#define gl_window(win) gl_surface(win)
Implements window_t.gl_window.
#define os_window(win) (&(win)->oswindow)
Implements window_t.os_window.
#define display_window(
   win
) (castfromos_display(display_x11window(os_window(win))))
Implements window_t.display_window.
#define hide_window(win) hide_x11window(os_window(win))
Implements window_t.hide_window.
#define isvisible_window(
   win
) (x11window_state_SHOWN == state_x11window(os_window(win)))
Implements window_t.isvisible_window.
#define pos_window(win,
screen_x,
screen_y) pos_x11window(os_window(win), screen_x, screen_y)
Implements window_t.pos_window.
#define resize_window(win,
width,
height) resize_x11window(os_window(win), width, height)
Implements window_t.resize_window.
#define sendclose_window(win) sendclose_x11window(os_window(win))
Implements window_t.sendclose_window.
#define sendredraw_window(win) sendredraw_x11window(os_window(win))
Implements window_t.sendredraw_window.
#define setpos_window(
   win,
   screen_x,
   screen_y
) setpos_x11window(os_window(win), screen_x, screen_y)
Implements window_t.setpos_window.
#define show_window(win) show_x11window(os_window(win))
Implements window_t.show_window.
#define size_window(win,
width,
height) size_x11window(os_window(win), width, height)
Implements window_t.size_window.
#define genericcast_windowevh(
   evhimpl,
   subwindow_t
) ( __extension__ ({ static_assert( offsetof(typeof(*(evhimpl)), onclose) == offsetof(window_evh_t, onclose) && offsetof(typeof(*(evhimpl)),ondestroy) == offsetof(window_evh_t, ondestroy) && offsetof(typeof(*(evhimpl)), onredraw) == offsetof(window_evh_t, onredraw) && offsetof(typeof(*(evhimpl)),onreshape) == offsetof(window_evh_t, onreshape) && offsetof(typeof(*(evhimpl)),onvisible) == offsetof(window_evh_t, onvisible), "ensure same structure"); if (0) { (evhimpl)->onclose((subwindow_t*)0); (evhimpl)->ondestroy((subwindow_t*)0); (evhimpl)->onredraw((subwindow_t*)0); (evhimpl)->onreshape((subwindow_t*)0, (uint32_t)0, (uint32_t)0); (evhimpl)->onvisible((subwindow_t*)0, (bool)0); } (const window_evh_t*) (evhimpl); }))
Implements window_evh_t.genericcast_windowevh.
#define window_evh_DECLARE(
   declared_evh_t,
   subwindow_t
) typedef struct declared_evh_t declared_evh_t; struct declared_evh_t { void (* onclose) (subwindow_t * win); void (* ondestroy) (subwindow_t * win); void (* onredraw) (subwindow_t * win); void (* onreshape) (subwindow_t * win, uint32_t width, uint32_t height); void (* onvisible) (subwindow_t * win, bool isVisible); }
Implements window_evh_t.window_evh_DECLARE.
Close