10 #include <X11/Xutil.h>
11 #include <X11/keysym.h>
12 #include <VG/openvg.h> /* using full OpenGL for now */
16 static init_func init
= 0;
17 static draw_func draw
= 0;
18 static reshape_func reshape
= 0;
19 static key_func keyPress
= 0;
20 static VGint width
= 300, height
= 300;
23 void set_window_size(int w
, int h
)
30 * Create an RGB, double-buffered X window.
31 * Return the window and context handles.
34 make_x_window(Display
*x_dpy
, EGLDisplay egl_dpy
,
36 int x
, int y
, int width
, int height
,
41 static const EGLint attribs
[] = {
49 XSetWindowAttributes attr
;
53 XVisualInfo
*visInfo
, visTemplate
;
60 scrnum
= DefaultScreen( x_dpy
);
61 root
= RootWindow( x_dpy
, scrnum
);
63 if (!eglChooseConfig( egl_dpy
, attribs
, &config
, 1, &num_configs
)) {
64 printf("Error: couldn't get an EGL visual config\n");
69 assert(num_configs
> 0);
71 if (!eglGetConfigAttrib(egl_dpy
, config
, EGL_NATIVE_VISUAL_ID
, &vid
)) {
72 printf("Error: eglGetConfigAttrib() failed\n");
76 /* The X window visual must match the EGL config */
77 visTemplate
.visualid
= vid
;
78 visInfo
= XGetVisualInfo(x_dpy
, VisualIDMask
, &visTemplate
, &num_visuals
);
80 printf("Error: couldn't get X visual\n");
84 /* window attributes */
85 attr
.background_pixel
= 0;
86 attr
.border_pixel
= 0;
87 attr
.colormap
= XCreateColormap( x_dpy
, root
, visInfo
->visual
, AllocNone
);
88 attr
.event_mask
= StructureNotifyMask
| ExposureMask
| KeyPressMask
;
89 mask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
;
91 win
= XCreateWindow( x_dpy
, root
, 0, 0, width
, height
,
92 0, visInfo
->depth
, InputOutput
,
93 visInfo
->visual
, mask
, &attr
);
95 /* set hints and properties */
100 sizehints
.width
= width
;
101 sizehints
.height
= height
;
102 sizehints
.flags
= USSize
| USPosition
;
103 XSetNormalHints(x_dpy
, win
, &sizehints
);
104 XSetStandardProperties(x_dpy
, win
, name
, name
,
105 None
, (char **)NULL
, 0, &sizehints
);
108 eglBindAPI(EGL_OPENVG_API
);
110 ctx
= eglCreateContext(egl_dpy
, config
, EGL_NO_CONTEXT
, NULL
);
112 printf("Error: eglCreateContext failed\n");
116 *surfRet
= eglCreateWindowSurface(egl_dpy
, config
, win
, NULL
);
119 printf("Error: eglCreateWindowSurface failed\n");
130 event_loop(Display
*dpy
, Window win
,
131 EGLDisplay egl_dpy
, EGLSurface egl_surf
)
137 XNextEvent(dpy
, &event
);
139 switch (event
.type
) {
143 case ConfigureNotify
:
145 width
= event
.xconfigure
.width
;
146 height
= event
.xconfigure
.height
;
147 reshape(event
.xconfigure
.width
, event
.xconfigure
.height
);
154 code
= XLookupKeysym(&event
.xkey
, 0);
155 if (!keyPress
|| !keyPress(code
)) {
156 r
= XLookupString(&event
.xkey
, buffer
, sizeof(buffer
),
158 if (buffer
[0] == 27) {
172 eglSwapBuffers(egl_dpy
, egl_surf
);
177 int window_width(void)
182 int window_height(void)
191 printf(" -display <displayname> set the display to run on\n");
192 printf(" -info display OpenGL renderer info\n");
195 int run(int argc
, char **argv
,
201 const int winWidth
= width
, winHeight
= height
;
207 char *dpyName
= NULL
;
208 GLboolean printInfo
= GL_FALSE
;
209 EGLint egl_major
, egl_minor
;
218 for (i
= 1; i
< argc
; i
++) {
219 if (strcmp(argv
[i
], "-display") == 0) {
223 else if (strcmp(argv
[i
], "-info") == 0) {
228 x_dpy
= XOpenDisplay(dpyName
);
230 printf("Error: couldn't open display %s\n",
231 dpyName
? dpyName
: getenv("DISPLAY"));
235 egl_dpy
= eglGetDisplay(x_dpy
);
237 printf("Error: eglGetDisplay() failed\n");
241 if (!eglInitialize(egl_dpy
, &egl_major
, &egl_minor
)) {
242 printf("Error: eglInitialize() failed\n");
246 s
= eglQueryString(egl_dpy
, EGL_VERSION
);
247 printf("EGL_VERSION = %s\n", s
);
249 make_x_window(x_dpy
, egl_dpy
,
250 "OpenVG Example", 0, 0, winWidth
, winHeight
,
251 &win
, &egl_ctx
, &egl_surf
);
253 XMapWindow(x_dpy
, win
);
254 if (!eglMakeCurrent(egl_dpy
, egl_surf
, egl_surf
, egl_ctx
)) {
255 printf("Error: eglMakeCurrent() failed\n");
260 printf("VG_RENDERER = %s\n", (char *) vgGetString(VG_RENDERER
));
261 printf("VG_VERSION = %s\n", (char *) vgGetString(VG_VERSION
));
262 printf("VG_VENDOR = %s\n", (char *) vgGetString(VG_VENDOR
));
268 /* Set initial projection/viewing transformation.
269 * We can't be sure we'll get a ConfigureNotify event when the window
273 reshape(winWidth
, winHeight
);
275 event_loop(x_dpy
, win
, egl_dpy
, egl_surf
);
277 eglMakeCurrent(egl_dpy
, 0, 0, 0);
278 eglDestroyContext(egl_dpy
, egl_ctx
);
279 eglDestroySurface(egl_dpy
, egl_surf
);
280 eglTerminate(egl_dpy
);
283 XDestroyWindow(x_dpy
, win
);
284 XCloseDisplay(x_dpy
);