Exemplo n.º 1
0
bool XSettingsManager::init(Display* dpy, int screen) {
	if(manager_data)
		return true;

	manager_data = new XSettingsData;
	manager_data->display = dpy;
	manager_data->screen = screen;
	manager_data->manager_win = None;
	manager_data->settings = NULL;

	char buff[256];
	sprintf(buff, "_XSETTINGS_S%d", manager_data->screen);

	manager_data->selection_atom = XInternAtom(manager_data->display, buff, False);
	manager_data->xsettings_atom = XInternAtom(manager_data->display, "_XSETTINGS_SETTINGS", False);
	manager_data->manager_atom = XInternAtom(manager_data->display, "MANAGER", False);

	manager_data->serial = 0;

	Window root = RootWindow(manager_data->display, manager_data->screen);
	manager_data->manager_win = XCreateSimpleWindow(manager_data->display, root, 0, 0, 10, 10, 0,
			WhitePixel(manager_data->display, manager_data->screen),
			WhitePixel(manager_data->display, manager_data->screen));

	XSelectInput(manager_data->display, manager_data->manager_win, PropertyChangeMask);
	Time timestamp = get_server_time(manager_data->display, manager_data->manager_win);

	XSetSelectionOwner(manager_data->display, manager_data->selection_atom, manager_data->manager_win, timestamp);

	// check if we got ownership
	if(XGetSelectionOwner(manager_data->display, manager_data->selection_atom) == manager_data->manager_win) {
		XClientMessageEvent xev;
		xev.type = ClientMessage;
		xev.window = root;
		xev.message_type = manager_data->manager_atom;
		xev.format = 32;
		xev.data.l[0] = timestamp;
		xev.data.l[1] = manager_data->selection_atom;
		xev.data.l[2] = manager_data->manager_win;
		xev.data.l[3] = 0; // manager specific data
		xev.data.l[4] = 0; // manager specific data

		XSendEvent(manager_data->display, root, False, StructureNotifyMask, (XEvent*)&xev);
		return true;
	}

	/*
	 * allow init() be called again
	 * FIXME: really should this exists here ?
	 */
	clear();
	return false;
}
XSettingsManager *
xsettings_manager_new (Display                *display,
		       int                     screen,
		       XSettingsTerminateFunc  terminate,
		       void                   *cb_data)
{
  XSettingsManager *manager;
  Time timestamp;
  XClientMessageEvent xev;

  char buffer[256];
  
  manager = malloc (sizeof *manager);
  if (!manager)
    return NULL;

  manager->display = display;
  manager->screen = screen;

  sprintf(buffer, "_XSETTINGS_S%d", screen);
  manager->selection_atom = XInternAtom (display, buffer, False);
  manager->xsettings_atom = XInternAtom (display, "_XSETTINGS_SETTINGS", False);
  manager->manager_atom = XInternAtom (display, "MANAGER", False);

  manager->terminate = terminate;
  manager->cb_data = cb_data;

  manager->settings = NULL;
  manager->serial = 0;

  manager->window = XCreateSimpleWindow (display,
					 RootWindow (display, screen),
					 0, 0, 10, 10, 0,
					 WhitePixel (display, screen),
					 WhitePixel (display, screen));

  XSelectInput (display, manager->window, PropertyChangeMask);
  timestamp = get_server_time (display, manager->window);

  XSetSelectionOwner (display, manager->selection_atom,
		      manager->window, timestamp);

  /* Check to see if we managed to claim the selection. If not,
   * we treat it as if we got it then immediately lost it
   */

  if (XGetSelectionOwner (display, manager->selection_atom) ==
      manager->window)
    {
      xev.type = ClientMessage;
      xev.window = RootWindow (display, screen);
      xev.message_type = manager->manager_atom;
      xev.format = 32;
      xev.data.l[0] = timestamp;
      xev.data.l[1] = manager->selection_atom;
      xev.data.l[2] = manager->window;
      xev.data.l[3] = 0;	/* manager specific data */
      xev.data.l[4] = 0;	/* manager specific data */
      
      XSendEvent (display, RootWindow (display, screen),
		  False, StructureNotifyMask, (XEvent *)&xev);
    }
  else
    {
      manager->terminate (manager->cb_data);
    }
  
  return manager;
}