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
[] = {
45 EGL_RENDERABLE_TYPE
, EGL_OPENVG_BIT
,
50 XSetWindowAttributes attr
;
54 XVisualInfo
*visInfo
, visTemplate
;
61 scrnum
= DefaultScreen( x_dpy
);
62 root
= RootWindow( x_dpy
, scrnum
);
64 if (!eglChooseConfig( egl_dpy
, attribs
, &config
, 1, &num_configs
) ||
66 printf("Error: couldn't get an EGL visual config\n");
72 if (!eglGetConfigAttrib(egl_dpy
, config
, EGL_NATIVE_VISUAL_ID
, &vid
)) {
73 printf("Error: eglGetConfigAttrib() failed\n");
77 /* The X window visual must match the EGL config */
78 visTemplate
.visualid
= vid
;
79 visInfo
= XGetVisualInfo(x_dpy
, VisualIDMask
, &visTemplate
, &num_visuals
);
81 printf("Error: couldn't get X visual\n");
85 /* window attributes */
86 attr
.background_pixel
= 0;
87 attr
.border_pixel
= 0;
88 attr
.colormap
= XCreateColormap( x_dpy
, root
, visInfo
->visual
, AllocNone
);
89 attr
.event_mask
= StructureNotifyMask
| ExposureMask
| KeyPressMask
;
90 mask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
;
92 win
= XCreateWindow( x_dpy
, root
, 0, 0, width
, height
,
93 0, visInfo
->depth
, InputOutput
,
94 visInfo
->visual
, mask
, &attr
);
96 /* set hints and properties */
101 sizehints
.width
= width
;
102 sizehints
.height
= height
;
103 sizehints
.flags
= USSize
| USPosition
;
104 XSetNormalHints(x_dpy
, win
, &sizehints
);
105 XSetStandardProperties(x_dpy
, win
, name
, name
,
106 None
, (char **)NULL
, 0, &sizehints
);
109 eglBindAPI(EGL_OPENVG_API
);
111 ctx
= eglCreateContext(egl_dpy
, config
, EGL_NO_CONTEXT
, NULL
);
113 printf("Error: eglCreateContext failed\n");
117 *surfRet
= eglCreateWindowSurface(egl_dpy
, config
, win
, NULL
);
120 printf("Error: eglCreateWindowSurface failed\n");
131 event_loop(Display
*dpy
, Window win
,
132 EGLDisplay egl_dpy
, EGLSurface egl_surf
)
138 XNextEvent(dpy
, &event
);
140 switch (event
.type
) {
144 case ConfigureNotify
:
146 width
= event
.xconfigure
.width
;
147 height
= event
.xconfigure
.height
;
148 reshape(event
.xconfigure
.width
, event
.xconfigure
.height
);
155 code
= XLookupKeysym(&event
.xkey
, 0);
156 if (!keyPress
|| !keyPress(code
)) {
157 r
= XLookupString(&event
.xkey
, buffer
, sizeof(buffer
),
159 if (buffer
[0] == 27) {
173 eglSwapBuffers(egl_dpy
, egl_surf
);
178 int window_width(void)
183 int window_height(void)
192 printf(" -display <displayname> set the display to run on\n");
193 printf(" -info display OpenGL renderer info\n");
196 int run(int argc
, char **argv
,
202 const int winWidth
= width
, winHeight
= height
;
208 char *dpyName
= NULL
;
209 GLboolean printInfo
= GL_FALSE
;
210 EGLint egl_major
, egl_minor
;
219 for (i
= 1; i
< argc
; i
++) {
220 if (strcmp(argv
[i
], "-display") == 0) {
224 else if (strcmp(argv
[i
], "-info") == 0) {
229 x_dpy
= XOpenDisplay(dpyName
);
231 printf("Error: couldn't open display %s\n",
232 dpyName
? dpyName
: getenv("DISPLAY"));
236 egl_dpy
= eglGetDisplay(x_dpy
);
238 printf("Error: eglGetDisplay() failed\n");
242 if (!eglInitialize(egl_dpy
, &egl_major
, &egl_minor
)) {
243 printf("Error: eglInitialize() failed\n");
247 s
= eglQueryString(egl_dpy
, EGL_VERSION
);
248 printf("EGL_VERSION = %s\n", s
);
250 make_x_window(x_dpy
, egl_dpy
,
251 "OpenVG Example", 0, 0, winWidth
, winHeight
,
252 &win
, &egl_ctx
, &egl_surf
);
254 XMapWindow(x_dpy
, win
);
255 if (!eglMakeCurrent(egl_dpy
, egl_surf
, egl_surf
, egl_ctx
)) {
256 printf("Error: eglMakeCurrent() failed\n");
261 printf("VG_RENDERER = %s\n", (char *) vgGetString(VG_RENDERER
));
262 printf("VG_VERSION = %s\n", (char *) vgGetString(VG_VERSION
));
263 printf("VG_VENDOR = %s\n", (char *) vgGetString(VG_VENDOR
));
269 /* Set initial projection/viewing transformation.
270 * We can't be sure we'll get a ConfigureNotify event when the window
274 reshape(winWidth
, winHeight
);
276 event_loop(x_dpy
, win
, egl_dpy
, egl_surf
);
278 eglMakeCurrent(egl_dpy
, 0, 0, 0);
279 eglDestroyContext(egl_dpy
, egl_ctx
);
280 eglDestroySurface(egl_dpy
, egl_surf
);
281 eglTerminate(egl_dpy
);
284 XDestroyWindow(x_dpy
, win
);
285 XCloseDisplay(x_dpy
);