int main(int argc, char *argv[]) { int ret; SDL_Event event; struct fgui_event fgui_event; int button_clicks = 0; int button2_clicks = 0; struct btn_cb_data btn_cb_data = { &button, "hello button userdata", &button_clicks, }; struct btn_cb_data btn_cb_data2 = { &button2, "hello button2 userdata", &button2_clicks, }; fgui_application_init(&app); fgui_listbox_init(&listbox, 160, 4*LINEHEIGHT-6, 60, 15, NULL); fgui_checkbox_init(&checkbox, 160, 6*LINEHEIGHT+3, 82, 15, "check it!", NULL); fgui_button_init(&button, 160, 7*LINEHEIGHT, 82, 15, "hello world", NULL); fgui_button_init(&button2, 160, 8*LINEHEIGHT, 82, 15, "hello world 2", NULL); fgui_combobox_init(&combobox, 160, 9*LINEHEIGHT, 60, 15, NULL); fgui_label_init(&label, 160, 10*LINEHEIGHT, 150, 20, "hello fgui label", NULL); fgui_lineedit_init(&lineedit, 160, 11*LINEHEIGHT, 100, 15, NULL); fgui_combobox_add_item(&combobox, "item1"); fgui_combobox_add_item(&combobox, "item2"); fgui_combobox_add_item(&combobox, "item3"); fgui_combobox_set_index(&combobox, 0); fgui_listbox_add_item(&listbox, "item1"); fgui_listbox_add_item(&listbox, "item2"); fgui_listbox_add_item(&listbox, "item3"); fgui_listbox_set_index(&listbox, 0); fgui_application_add_widget(&app, (struct fgui_widget *)&listbox); fgui_application_add_widget(&app, (struct fgui_widget *)&checkbox); fgui_application_add_widget(&app, &button.base); fgui_application_add_widget(&app, &button2.base); fgui_application_add_widget(&app, (struct fgui_widget *)&label); fgui_application_add_widget(&app, (struct fgui_widget *)&combobox); fgui_application_add_widget(&app, (struct fgui_widget *)&lineedit); fgui_button_set_on_click_handler(&button, on_button_click, &btn_cb_data); fgui_button_set_on_click_handler(&button2, on_button_click, &btn_cb_data2); fgui_combobox_set_on_change_handler(&combobox, on_combobox_change, NULL); fgui_checkbox_set_on_click_handler(&checkbox, on_checkbox_change, NULL); fgui_dropmenu_init(&menubar, 0, 0, 80, 15, NULL); fgui_dropmenu_add_item(&dropmenu, "menuitem1"); fgui_dropmenu_add_item(&dropmenu, "menuitem2"); fgui_dropmenu_add_item(&dropmenu, "menuitem3"); fgui_dropmenu_set_index(&dropmenu, 0); fgui_menubar_init(&menubar, 0, 0, 320, 15, NULL); fgui_menubar_add_item(&menubar, "Menu1", (struct fgui_widget *)&dropmenu); fgui_menubar_add_item(&menubar, "Menu2", NULL); fgui_menubar_add_item(&menubar, "Help", NULL); fgui_application_add_widget(&app, (struct fgui_widget *)&menubar); if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); exit(1); } // make sure things are cleaned up when we quit atexit(SDL_Quit); // attempt to create a 640x480 window with 32bit pixels. gScreen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_SWSURFACE); if (gScreen == NULL) { fprintf(stderr, "Unable to set up video: %s\n", SDL_GetError()); exit(1); } SDL_EnableUNICODE(1); while (1) { render_stuff(); // update the whole screen SDL_UpdateRect(gScreen, 0, 0, 0, 0); // blocking wait (tip: SDL_PollEvent is non-blocking) ret = SDL_WaitEvent(&event); if (ret == 0) { fprintf(stderr, "ERROR: SDL_WaitEvent: %s\n", SDL_GetError()); exit(1); } switch (event.type) { case SDL_KEYDOWN: fgui_event.type = FGUI_EVENT_KEYDOWN; fgui_event.key.keycode = sdl_keysym_to_fgui_key(event.key.keysym.sym); if ((fgui_event.key.keycode >= 'a') && (fgui_event.key.keycode <= 'z')) { if (event.key.keysym.mod & KMOD_SHIFT) fgui_event.key.keycode = toupper(fgui_event.key.keycode); } fgui_application_process_event(&app, &fgui_event); break; case SDL_KEYUP: switch (event.key.keysym.sym) { case SDLK_ESCAPE: // return (and thus, quit) return 0; default: fgui_event.type = FGUI_EVENT_KEYUP; fgui_event.key.keycode = sdl_keysym_to_fgui_key(event.key.keysym.sym); fgui_application_process_event(&app, &fgui_event); break; } break; case SDL_QUIT: return 0; default: break; } } return 0; }
int main(int argc, char *argv[]) { EGLDisplay dpy; EGLContext ctx; EGLSurface surface; EGLConfig config; EGLint major, minor, n; const char *ver; uint32_t handle, stride; struct kms kms; int ret, fd; struct gbm_device *gbm; struct gbm_bo *bo; drmModeCrtcPtr saved_crtc; struct gbm_surface *gs; fd = open(device_name, O_RDWR); if (fd < 0) { /* Probably permissions error */ fprintf(stderr, "couldn't open %s, skipping\n", device_name); return -1; } gbm = gbm_create_device(fd); if (gbm == NULL) { fprintf(stderr, "couldn't create gbm device\n"); ret = -1; goto close_fd; } dpy = eglGetDisplay(gbm); if (dpy == EGL_NO_DISPLAY) { fprintf(stderr, "eglGetDisplay() failed\n"); ret = -1; goto destroy_gbm_device; } if (!eglInitialize(dpy, &major, &minor)) { printf("eglInitialize() failed\n"); ret = -1; goto egl_terminate; } ver = eglQueryString(dpy, EGL_VERSION); printf("EGL_VERSION = %s\n", ver); if (!setup_kms(fd, &kms)) { ret = -1; goto egl_terminate; } eglBindAPI(EGL_OPENGL_API); if (!eglChooseConfig(dpy, attribs, &config, 1, &n) || n != 1) { fprintf(stderr, "failed to choose argb config\n"); goto egl_terminate; } ctx = eglCreateContext(dpy, config, EGL_NO_CONTEXT, NULL); if (ctx == NULL) { fprintf(stderr, "failed to create context\n"); ret = -1; goto egl_terminate; } gs = gbm_surface_create(gbm, kms.mode.hdisplay, kms.mode.vdisplay, GBM_BO_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); surface = eglCreateWindowSurface(dpy, config, gs, NULL); if (!eglMakeCurrent(dpy, surface, surface, ctx)) { fprintf(stderr, "failed to make context current\n"); ret = -1; goto destroy_context; } render_stuff(kms.mode.hdisplay, kms.mode.vdisplay); eglSwapBuffers(dpy, surface); bo = gbm_surface_lock_front_buffer(gs); handle = gbm_bo_get_handle(bo).u32; stride = gbm_bo_get_stride(bo); printf("handle=%d, stride=%d\n", handle, stride); ret = drmModeAddFB(fd, kms.mode.hdisplay, kms.mode.vdisplay, 24, 32, stride, handle, &kms.fb_id); if (ret) { fprintf(stderr, "failed to create fb\n"); goto rm_fb; } saved_crtc = drmModeGetCrtc(fd, kms.encoder->crtc_id); if (saved_crtc == NULL) goto rm_fb; ret = drmModeSetCrtc(fd, kms.encoder->crtc_id, kms.fb_id, 0, 0, &kms.connector->connector_id, 1, &kms.mode); if (ret) { fprintf(stderr, "failed to set mode: %m\n"); goto free_saved_crtc; } getchar(); ret = drmModeSetCrtc(fd, saved_crtc->crtc_id, saved_crtc->buffer_id, saved_crtc->x, saved_crtc->y, &kms.connector->connector_id, 1, &saved_crtc->mode); if (ret) { fprintf(stderr, "failed to restore crtc: %m\n"); } free_saved_crtc: drmModeFreeCrtc(saved_crtc); rm_fb: drmModeRmFB(fd, kms.fb_id); eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); destroy_context: eglDestroyContext(dpy, ctx); egl_terminate: eglTerminate(dpy); destroy_gbm_device: gbm_device_destroy(gbm); close_fd: close(fd); return ret; }
int main(int argc, char *argv[]) { EGLDisplay dpy; EGLContext ctx; EGLConfig config; EGLSurface surface; EGLint major, minor, n; const char *ver; uint32_t handle, stride; int ret, fd, frames = 0; struct gbm_device *gbm; drmModeCrtcPtr saved_crtc; time_t start, end; char *data; char j; int i; int once; once = 0; signal (SIGINT, quit_handler); fd = open(device_name, O_RDWR); if (fd < 0) { /* Probably permissions error */ fprintf(stderr, "couldn't open %s, skipping\n", device_name); return -1; } gbm = gbm_create_device(fd); if (gbm == NULL) { fprintf(stderr, "couldn't create gbm device\n"); ret = -1; goto close_fd; } dpy = eglGetDisplay(gbm); if (dpy == EGL_NO_DISPLAY) { fprintf(stderr, "eglGetDisplay() failed\n"); ret = -1; goto destroy_gbm_device; } if (!eglInitialize(dpy, &major, &minor)) { printf("eglInitialize() failed\n"); ret = -1; goto egl_terminate; } ver = eglQueryString(dpy, EGL_VERSION); printf("EGL_VERSION = %s\n", ver); if (!setup_kms(fd, &kms)) { ret = -1; goto egl_terminate; } eglBindAPI(EGL_OPENGL_API); if (!eglChooseConfig(dpy, attribs, &config, 1, &n) || n != 1) { fprintf(stderr, "failed to choose argb config\n"); goto egl_terminate; } ctx = eglCreateContext(dpy, config, EGL_NO_CONTEXT, NULL); if (ctx == NULL) { fprintf(stderr, "failed to create context\n"); ret = -1; goto egl_terminate; } gs = gbm_surface_create(gbm, kms.mode.hdisplay, kms.mode.vdisplay, GBM_BO_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); if (gs == NULL) { fprintf(stderr, "unable to create gbm surface\n"); ret = -1; goto egl_terminate; } surface = eglCreateWindowSurface(dpy, config, gs, NULL); if (surface == EGL_NO_SURFACE) { fprintf(stderr, "failed to create surface\n"); ret = -1; goto destroy_gbm_surface; } if (!eglMakeCurrent(dpy, surface, surface, ctx)) { fprintf(stderr, "failed to make context current\n"); ret = -1; goto destroy_surface; } saved_crtc = drmModeGetCrtc(fd, kms.crtc_id); if (saved_crtc == NULL) { fprintf(stderr, "no valid graphic configuration active (VT ?)\n"); } time(&start); do { drmEventContext evctx; fd_set rfds; render_stuff(kms.mode.hdisplay, kms.mode.vdisplay); eglSwapBuffers(dpy, surface); if (!gbm_surface_has_free_buffers(gs)) fprintf(stderr, "out of free buffers\n"); next_bo = gbm_surface_lock_front_buffer(gs); if (!next_bo) fprintf(stderr, "failed to lock front buffer: %m\n"); handle = gbm_bo_get_handle(next_bo).u32; stride = gbm_bo_get_stride(next_bo); ret = drmModeAddFB(fd, kms.mode.hdisplay, kms.mode.vdisplay, 24, 32, stride, handle, &next_fb_id); if (ret) { fprintf(stderr, "failed to create fb\n"); goto out; } /* make sure to setup crtc once (fix for broken drivers) */ if(once == 0){ once = 1; drmModeSetCrtc(fd, kms.crtc_id, next_fb_id, 0, 0, &kms.connector->connector_id, 1, &kms.mode); } ret = drmModePageFlip(fd, kms.crtc_id, next_fb_id, DRM_MODE_PAGE_FLIP_EVENT, 0); if (ret) { fprintf(stderr, "failed to page flip: %m\n"); goto out; } FD_ZERO(&rfds); FD_SET(fd, &rfds); while (select(fd + 1, &rfds, NULL, NULL, NULL) == -1) NULL; memset(&evctx, 0, sizeof evctx); evctx.version = DRM_EVENT_CONTEXT_VERSION; evctx.page_flip_handler = page_flip_handler; drmHandleEvent(fd, &evctx); frames++; } while (!quit); time(&end); printf("Frames per second: %.2lf\n", frames / difftime(end, start)); out: if(saved_crtc){ drmModeSetCrtc(fd, saved_crtc->crtc_id, saved_crtc->buffer_id, saved_crtc->x, saved_crtc->y, &kms.connector->connector_id, 1, &saved_crtc->mode); } drmModeFreeCrtc(saved_crtc); if (current_fb_id) drmModeRmFB(fd, current_fb_id); if (next_fb_id) drmModeRmFB(fd, next_fb_id); eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); destroy_context: eglDestroyContext(dpy, ctx); destroy_surface: eglDestroySurface(dpy, surface); destroy_gbm_surface: gbm_surface_destroy(gs); egl_terminate: eglTerminate(dpy); destroy_gbm_device: gbm_device_destroy(gbm); close_fd: close(fd); return ret; }