static struct eglut_window * _eglutCreateWindow(const char *title, int x, int y, int w, int h) { struct eglut_window *win; win = calloc(1, sizeof(*win)); if (!win) _eglutFatal("failed to allocate window"); win->config = _eglutChooseConfig(); eglBindAPI(EGL_OPENGL_API); win->context = eglCreateContext(_eglut->dpy, win->config, EGL_NO_CONTEXT, NULL); if (!win->context) _eglutFatal("failed to create context"); _eglutNativeInitWindow(win, title, x, y, w, h); switch (_eglut->surface_type) { case EGL_WINDOW_BIT: win->surface = eglCreateWindowSurface(_eglut->dpy, win->config, win->native.u.window, NULL); break; default: break; } if (win->surface == EGL_NO_SURFACE) _eglutFatal("failed to create surface"); return win; }
void _eglutNativeInitWindow(struct eglut_window *win, const char *title, int x, int y, int w, int h) { EGLint surf_attribs[16]; EGLint i; const char *exts; exts = eglQueryString(_eglut->dpy, EGL_EXTENSIONS); if (!exts || !strstr(exts, "EGL_MESA_screen_surface")) _eglutFatal("EGL_MESA_screen_surface is not supported\n"); init_kms(); i = 0; surf_attribs[i++] = EGL_WIDTH; surf_attribs[i++] = kms_width; surf_attribs[i++] = EGL_HEIGHT; surf_attribs[i++] = kms_height; surf_attribs[i++] = EGL_NONE; /* create surface */ win->native.u.surface = eglCreateScreenSurfaceMESA(_eglut->dpy, win->config, surf_attribs); if (win->native.u.surface == EGL_NO_SURFACE) _eglutFatal("eglCreateScreenSurfaceMESA failed\n"); if (!eglShowScreenSurfaceMESA(_eglut->dpy, kms_screen, win->native.u.surface, kms_mode)) _eglutFatal("eglShowScreenSurfaceMESA failed\n"); win->native.width = kms_width; win->native.height = kms_height; }
static struct eglut_window * _eglutCreateWindow(const char *title, int x, int y, int w, int h) { struct eglut_window *win; EGLint context_attribs[4]; EGLint api, i; win = calloc(1, sizeof(*win)); if (!win) _eglutFatal("failed to allocate window"); win->config = _eglutChooseConfig(); i = 0; context_attribs[i] = EGL_NONE; /* multiple APIs? */ api = EGL_OPENGL_ES_API; if (_eglut->api_mask & EGLUT_OPENGL_BIT) { api = EGL_OPENGL_API; } else if (_eglut->api_mask & EGLUT_OPENVG_BIT) { api = EGL_OPENVG_API; } else if (_eglut->api_mask & EGLUT_OPENGL_ES2_BIT) { context_attribs[i++] = EGL_CONTEXT_CLIENT_VERSION; context_attribs[i++] = 2; } context_attribs[i] = EGL_NONE; eglBindAPI(api); win->context = eglCreateContext(_eglut->dpy, win->config, EGL_NO_CONTEXT, context_attribs); if (!win->context) _eglutFatal("failed to create context"); _eglutNativeInitWindow(win, title, x, y, w, h); switch (_eglut->surface_type) { case EGL_WINDOW_BIT: win->surface = eglCreateWindowSurface(_eglut->dpy, win->config, win->native.u.window, NULL); break; case EGL_PIXMAP_BIT: win->surface = eglCreatePixmapSurface(_eglut->dpy, win->config, win->native.u.pixmap, NULL); break; case EGL_PBUFFER_BIT: case EGL_SCREEN_BIT_MESA: win->surface = win->native.u.surface; break; default: break; } if (win->surface == EGL_NO_SURFACE) _eglutFatal("failed to create surface"); return win; }
void _eglutNativeInitWindow(struct eglut_window *win, const char *title, int x, int y, int w, int h) { XVisualInfo *visInfo, visTemplate; int num_visuals; Window root, xwin; XSetWindowAttributes attr; unsigned long mask; EGLint vid; if (!eglGetConfigAttrib(_eglut->dpy, win->config, EGL_NATIVE_VISUAL_ID, &vid)) _eglutFatal("failed to get visual id"); /* The X window visual must match the EGL config */ visTemplate.visualid = vid; visInfo = XGetVisualInfo(_eglut->native_dpy, VisualIDMask, &visTemplate, &num_visuals); if (!visInfo) _eglutFatal("failed to get an visual of id 0x%x", vid); root = RootWindow(_eglut->native_dpy, DefaultScreen(_eglut->native_dpy)); /* window attributes */ attr.background_pixel = 0; attr.border_pixel = 0; attr.colormap = XCreateColormap(_eglut->native_dpy, root, visInfo->visual, AllocNone); attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; xwin = XCreateWindow(_eglut->native_dpy, root, x, y, w, h, 0, visInfo->depth, InputOutput, visInfo->visual, mask, &attr); if (!xwin) _eglutFatal("failed to create a window"); XFree(visInfo); /* set hints and properties */ { XSizeHints sizehints; sizehints.x = x; sizehints.y = y; sizehints.width = w; sizehints.height = h; sizehints.flags = USSize | USPosition; XSetNormalHints(_eglut->native_dpy, xwin, &sizehints); XSetStandardProperties(_eglut->native_dpy, xwin, title, title, None, (char **) NULL, 0, &sizehints); } XMapWindow(_eglut->native_dpy, xwin); win->native.u.window = xwin; win->native.width = w; win->native.height = h; }
void eglutInit(int argc, char **argv) { int i; for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-display") == 0) _eglut->display_name = argv[++i]; else if (strcmp(argv[i], "-info") == 0) { _eglut->verbose = 1; } } _eglutNativeInitDisplay(); _eglut->dpy = eglGetDisplay(_eglut->native_dpy); if (!eglInitialize(_eglut->dpy, &_eglut->major, &_eglut->minor)) _eglutFatal("failed to initialize EGL display"); _eglut->init_time = _eglutNow(); printf("EGL_VERSION = %s\n", eglQueryString(_eglut->dpy, EGL_VERSION)); if (_eglut->verbose) { printf("EGL_VENDOR = %s\n", eglQueryString(_eglut->dpy, EGL_VENDOR)); printf("EGL_EXTENSIONS = %s\n", eglQueryString(_eglut->dpy, EGL_EXTENSIONS)); printf("EGL_CLIENT_APIS = %s\n", eglQueryString(_eglut->dpy, EGL_CLIENT_APIS)); } }
void _eglutNativeInitDisplay(void) { _eglut->native_dpy = XOpenDisplay(_eglut->display_name); if (!_eglut->native_dpy) _eglutFatal("failed to initialize native display"); _eglut->surface_type = EGL_WINDOW_BIT; }
static void init_kms(void) { EGLModeMESA modes[MAX_MODES]; EGLint num_screens, num_modes; EGLint width, height, best_mode; EGLint i; if (!eglGetScreensMESA(_eglut->dpy, &kms_screen, 1, &num_screens) || !num_screens) _eglutFatal("eglGetScreensMESA failed\n"); if (!eglGetModesMESA(_eglut->dpy, kms_screen, modes, MAX_MODES, &num_modes) || !num_modes) _eglutFatal("eglGetModesMESA failed!\n"); printf("Found %d modes:\n", num_modes); best_mode = 0; width = 0; height = 0; for (i = 0; i < num_modes; i++) { EGLint w, h; eglGetModeAttribMESA(_eglut->dpy, modes[i], EGL_WIDTH, &w); eglGetModeAttribMESA(_eglut->dpy, modes[i], EGL_HEIGHT, &h); printf("%3d: %d x %d\n", i, w, h); if (w > width && h > height) { width = w; height = h; best_mode = i; } } printf("Will use screen size: %d x %d\n", width, height); kms_mode = modes[best_mode]; kms_width = width; kms_height = height; }
void eglutMainLoop(void) { struct eglut_window *win = _eglut->current; if (!win) _eglutFatal("no window is created\n"); if (win->reshape_cb) win->reshape_cb(win->native.width, win->native.height); _eglutNativeEventLoop(); }
int eglutCreateWindow(const char *title) { struct eglut_window *win; win = _eglutCreateWindow(title, 0, 0, _eglut->window_width, _eglut->window_height); win->index = _eglut->num_windows++; win->reshape_cb = NULL; win->display_cb = NULL; win->keyboard_cb = _eglutDefaultKeyboard; win->special_cb = NULL; if (!eglMakeCurrent(_eglut->dpy, win->surface, win->surface, win->context)) _eglutFatal("failed to make window current"); _eglut->current = win; return win->index; }
static EGLConfig _eglutChooseConfig(void) { EGLConfig config; EGLint config_attribs[32]; EGLint num_configs, i; i = 0; config_attribs[i++] = EGL_RED_SIZE; config_attribs[i++] = 1; config_attribs[i++] = EGL_GREEN_SIZE; config_attribs[i++] = 1; config_attribs[i++] = EGL_BLUE_SIZE; config_attribs[i++] = 1; config_attribs[i++] = EGL_DEPTH_SIZE; config_attribs[i++] = 1; config_attribs[i++] = EGL_SURFACE_TYPE; config_attribs[i++] = EGL_WINDOW_BIT; config_attribs[i++] = EGL_RENDERABLE_TYPE; config_attribs[i++] = EGL_OPENGL_BIT; config_attribs[i] = EGL_NONE; // renderable_type = 0x0; // if (_eglut->api_mask & EGLUT_OPENGL_BIT) // renderable_type |= EGL_OPENGL_BIT; // if (_eglut->api_mask & EGLUT_OPENGL_ES1_BIT) // renderable_type |= EGL_OPENGL_ES_BIT; // if (_eglut->api_mask & EGLUT_OPENGL_ES2_BIT) // renderable_type |= EGL_OPENGL_ES2_BIT; // if (_eglut->api_mask & EGLUT_OPENVG_BIT) // renderable_type |= EGL_OPENVG_BIT; if (!eglChooseConfig(_eglut->dpy, config_attribs, &config, 1, &num_configs) || !num_configs) _eglutFatal("failed to choose a config"); return config; }
int eglutCreateWindow(const char *title) { struct eglut_window *win; int skinh; win = _eglutCreateWindow(title, 20, 20, _eglut->window_width, _eglut->window_height); win->index = _eglut->num_windows++; win->reshape_cb = NULL; win->display_cb = NULL; win->keyboard_cb = _eglutDefaultKeyboard; win->special_cb = NULL; if (!eglMakeCurrent(_eglut->dpy, win->surface, win->surface, win->context)) _eglutFatal("failed to make window current"); _eglut->current = win; skinh = get_skin_height(); _eglut->render = create_render(_eglut->dpy, win->surface, TYPE_3_BORDER_WIDTH, skinh); return win->index; }