rem: if (!monitor->have_GLX_SGI_video_sync) {
add: if (!monitor->have_GLX_SGI_video_sync && !swapbuffer_syncs_to_vblank()) {
context: struct glgfx_monitor* glgfx_monitor_create_a(char const* display_name,					     struct glgfx_tagitem const* tags) {  struct glgfx_monitor* monitor;  struct glgfx_tagitem const* tag;  if (display_name == NULL) {    errno = EINVAL;    return NULL;  }  monitor = calloc(1, sizeof (*monitor));  if (monitor == NULL) {    errno = ENOMEM;    return NULL;  }  monitor->fullscreen = true;  pthread_cond_init(&monitor->vsync_cond, NULL);  while ((tag = glgfx_nexttagitem(&tags)) != NULL) {    switch ((enum glgfx_monitor_attr) tag->tag) {      case glgfx_monitor_attr_friend:	monitor->friend = (struct glgfx_monitor*) tag->data;	break;      case glgfx_monitor_attr_fullscreen:	monitor->fullscreen = tag->data;	break;      case glgfx_monitor_attr_width:      case glgfx_monitor_attr_height:      case glgfx_monitor_attr_format:      case glgfx_monitor_attr_vsync:      case glgfx_monitor_attr_hsync:      case glgfx_monitor_attr_dotclock:      case glgfx_monitor_attr_unknown:      case glgfx_monitor_attr_max:	/* Make compiler happy */	break;    }  }  D(BUG("Opening display %s\n", display_name));  // Default error code  errno = ENOTSUP;  monitor->name = strdup(display_name);  monitor->xa_win_state = None;  monitor->gl_extensions = g_hash_table_new(g_str_hash, g_str_equal);  if (monitor->gl_extensions == NULL) {    free(monitor);    errno = ENOMEM;    return NULL;  }  monitor->display = XOpenDisplay(display_name);  if (monitor->display == NULL) {    glgfx_monitor_destroy(monitor);    errno = ENXIO;    return NULL;  }  int dummy;  if (!XF86DGAQueryExtension(monitor->display, &dummy, &dummy)) {    BUG("The XF86DGA extension is missing from display %s!\n", display_name);    glgfx_monitor_destroy(monitor);    return NULL;  }  if (!XF86VidModeQueryExtension(monitor->display, &dummy, &dummy)) {    BUG("The XF86VidMode extension is missing from display %s!\n", display_name);    glgfx_monitor_destroy(monitor);    return NULL;  }    if (!XF86VidModeGetModeLine(monitor->display,			      DefaultScreen(monitor->display),			      &monitor->dotclock,			      &monitor->mode)) {    BUG("Unable to get current mode line for display %s!\n", display_name);    glgfx_monitor_destroy(monitor);    return NULL;  }  D(BUG("Pixel clock:  %.1f MHz\n", monitor->dotclock / 1000.0));  D(BUG("Width:        %d pixels\n", monitor->mode.hdisplay));  D(BUG("Height:       %d pixels\n", monitor->mode.vdisplay));  D(BUG("Refresh rate: %.1f Hz\n",	monitor->dotclock * 1000.0 / (monitor->mode.htotal * monitor->mode.vtotal)));        if (!XF86VidModeGetMonitor(monitor->display,			     DefaultScreen(monitor->display),			     &monitor->monitor_info)) {    BUG("Unable to get current monitor info for display %s!\n", display_name);    glgfx_monitor_destroy(monitor);    return NULL;  }  D(BUG("Vendor:  %s\n", monitor->monitor_info.vendor));  D(BUG("Model:   %s\n", monitor->monitor_info.model));  int i;  for (i = 0; i < monitor->monitor_info.nhsync; ++i) {    D(BUG("HSync: %.1f-%.1f kHz\n",	  monitor->monitor_info.hsync->lo,	  monitor->monitor_info.hsync->hi));  }  for (i = 0; i < monitor->monitor_info.nvsync; ++i) {    D(BUG("VSync: %.1f-%.1f Hz\n",	  monitor->monitor_info.vsync->lo,	  monitor->monitor_info.hsync->hi));  }  if (!glXQueryExtension(monitor->display, NULL, NULL)) {    BUG("The GLX extension is missing from display %s!\n", display_name);    glgfx_monitor_destroy(monitor);    return NULL;  }  static int const fb_attribs[] = {     GLX_RENDER_TYPE,   GLX_RGBA_BIT,    GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT | GLX_PBUFFER_BIT,    GLX_DOUBLEBUFFER,  True,    GLX_RED_SIZE,      4,    GLX_GREEN_SIZE,    4,    GLX_BLUE_SIZE,     4,    GLX_DEPTH_SIZE,    16,    GLX_CONFIG_CAVEAT, GLX_NONE,    None  };  monitor->fb_config = glXChooseFBConfig(monitor->display, 					 DefaultScreen(monitor->display), 					 fb_attribs, 					 &monitor->fb_configs);  D(BUG("Found %d suitable FBConfigs\n", monitor->fb_configs));  if (monitor->fb_configs == 0) {    BUG("Unable to get a sane FBConfig for display %s!\n", display_name);    glgfx_monitor_destroy(monitor);    return NULL;  }  monitor->vinfo = glXGetVisualFromFBConfig(monitor->display, monitor->fb_config[0]);  if (monitor->vinfo == NULL) {    BUG("Unable to get a GL visual for display %s!\n", display_name);    glgfx_monitor_destroy(monitor);    return NULL;  }  struct glgfx_tagitem const px_tags[] = {    { glgfx_pixel_attr_rgb,       true                       },    { glgfx_pixel_attr_redmask,   monitor->vinfo->red_mask   },    { glgfx_pixel_attr_greenmask, monitor->vinfo->green_mask },    { glgfx_pixel_attr_bluemask,  monitor->vinfo->blue_mask  },    { glgfx_tag_end,              0                          }  };  monitor->format = glgfx_pixel_getformat_a(px_tags);  if (monitor->format == glgfx_pixel_format_unknown) {    BUG("Visual for display %s is not supported. Sorry.\n", display_name);    glgfx_monitor_destroy(monitor);    return NULL;  }    XSetWindowAttributes swa;  swa.colormap = XCreateColormap(monitor->display,				 RootWindow(monitor->display,					    monitor->vinfo->screen),				 monitor->vinfo->visual,				 AllocNone);  swa.background_pixel = BlackPixel(monitor->display,				    monitor->vinfo->screen);  swa.border_pixel = BlackPixel(monitor->display,				monitor->vinfo->screen);  monitor->window = XCreateWindow(monitor->display,				  RootWindow(monitor->display,					     monitor->vinfo->screen),				  0, 0,				  monitor->mode.hdisplay,				  monitor->mode.vdisplay,				  0, 				  monitor->vinfo->depth,				  CopyFromParent,				  monitor->vinfo->visual,				  (CWBackPixel |				   CWBorderPixel |				   CWColormap),				  &swa);        if (monitor->window == 0) {    BUG("Unable to create a window on display %s!\n", display_name);    glgfx_monitor_destroy(monitor);    return NULL;  }  monitor->glx_window = glXCreateWindow(monitor->display, 					monitor->fb_config[0],					monitor->window,					NULL);    if (monitor->glx_window == 0) {    BUG("Unable to create a GLX window on display %s!\n", display_name);    glgfx_monitor_destroy(monitor);    return NULL;  }  monitor->main_context = glgfx_monitor_createcontext(monitor);  if (monitor->main_context == NULL) {    BUG("Unable to create a GL context for window on display %s!\n",	display_name);    glgfx_monitor_destroy(monitor);    return NULL;  }  if (!monitor->have_GLX_SGI_video_sync) {    // No video sync: Create a timer for vsync emulation    struct sigevent ev;    ev.sigev_notify = SIGEV_SIGNAL;    ev.sigev_signo  = glgfx_signum;    ev.sigev_value.sival_ptr = monitor;    if (timer_create(CLOCK_REALTIME, &ev, &monitor->vsync_timer) == -1) {      BUG("Unable to create vsync emulation timer\n");      glgfx_monitor_destroy(monitor);      return NULL;    }    else {      monitor->vsync_timer_valid = true;            long ns = (long) (1e9 / (monitor->dotclock * 1000.0 /			       (monitor->mode.htotal * monitor->mode.vtotal)));      struct timespec now;      if (clock_gettime(CLOCK_REALTIME, &now) != 0) {	BUG("Unable to get current time\n");	glgfx_monitor_destroy(monitor);	return NULL;      }      monitor->vsync_itimerspec.it_value.tv_nsec = now.tv_nsec + ns;      monitor->vsync_itimerspec.it_value.tv_sec  = now.tv_sec;      while (monitor->vsync_itimerspec.it_value.tv_nsec > 1e9) {	monitor->vsync_itimerspec.it_value.tv_nsec -= 1e9;	++monitor->vsync_itimerspec.it_value.tv_sec;      }      if (timer_settime(monitor->vsync_timer, TIMER_ABSTIME, 			&monitor->vsync_itimerspec, NULL) != 0) {	BUG("Unable to start vsync emulation timer\n");	glgfx_monitor_destroy(monitor);	return NULL;      }    }  }  go_fullscreen(monitor, monitor->fullscreen);  XMapRaised(monitor->display, monitor->window);  XSelectInput(monitor->display, monitor->window,	       (ButtonPressMask |		ButtonReleaseMask |		ButtonMotionMask |		PointerMotionMask |		KeyPressMask |		KeyReleaseMask));  XFlush(monitor->display);  glgfx_context_select(monitor->main_context);  errno = 0;  return monitor;}