+#include "efence.h"
+
#include "screen.h"
+#include "display.h"
+#include "plugin.h"
+#include "list.h"
+#include "time.h"
+#include "window.h"
#include "gettext.h"
#include <string.h>
+#include <stdlib.h>
#include <stdio.h>
+#include <xcb/composite.h>
+#include <xcb/xfixes.h>
+
+#define ROOT_MASK (XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | \
+ XCB_EVENT_MASK_STRUCTURE_NOTIFY | \
+ XCB_EVENT_MASK_PROPERTY_CHANGE)
+
+#define SELECTION_MASK (XCB_EVENT_MASK_STRUCTURE_NOTIFY | \
+ XCB_EVENT_MASK_PROPERTY_CHANGE)
+
+static gboolean screen_init(d_screen_t *sc);
+static xcb_timestamp_t screen_timestamp(d_screen_t *sc);
+static void screen_add_existing_windows(d_screen_t *sc);
+static void screen_set_next_repaint(d_screen_t *sc);
+
+static guint
+xcb_window_hash(xcb_window_t *w) { return *w; }
+
+static gboolean
+xcb_window_equal(xcb_window_t *w1, xcb_window_t *w2) { return *w1 == *w2; }
+
+
+d_screen_t*
+screen_new(struct d_display *dpy, int num, xcb_screen_t *xcb)
+{
+ d_screen_t *sc;
+
+ sc = malloc(sizeof(d_screen_t));
+ sc->super = *xcb;
+ sc->ref = 1;
+ sc->dpy = dpy;
+ sc->num = num;
+
+ gettimeofday(&sc->next_repaint, NULL);
+ sc->need_repaint = TRUE;
+
+ sc->winhash = g_hash_table_new((GHashFunc)xcb_window_hash,
+ (GEqualFunc)xcb_window_equal);
+ sc->stacking = list_new();
+ sc->plugin_data = list_new();
+
+ return sc;
+}
+
+void
+screen_ref(d_screen_t *sc)
+{
+ ++sc->ref;
+}
+
+void
+screen_unref(d_screen_t *sc)
+{
+ if (sc && --sc->ref == 0) {
+ d_list_it_t *it, *next;
+
+ /* clean up pending requests */
+ screen_get_root_pixmap(sc);
+
+ g_hash_table_unref(sc->winhash);
+ for (it = list_top(sc->stacking); it; it = next) {
+ next = it->next;
+ window_unref(it->data);
+ }
+ list_unref(sc->stacking);
+ list_unref(sc->plugin_data);
+ free(sc);
+ }
+}
gboolean
-screen_register(xcb_connection_t *conn, d_screen_t *sc)
+screen_register(d_screen_t *sc)
{
char *name;
- int len, s;
xcb_window_t w;
xcb_intern_atom_cookie_t ack;
xcb_intern_atom_reply_t *arep;
xcb_get_selection_owner_cookie_t sck;
xcb_get_selection_owner_reply_t *srep;
- gboolean taken;
+ uint32_t event_mask;
+ gboolean taken, ret;
- w = xcb_generate_id(conn);
- xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, sc->super.root,
+ w = xcb_generate_id(sc->dpy->conn);
+ event_mask = SELECTION_MASK;
+ xcb_create_window(sc->dpy->conn, XCB_COPY_FROM_PARENT, w, sc->super.root,
0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_ONLY,
- sc->super.root_visual, 0, NULL);
+ sc->super.root_visual, XCB_CW_EVENT_MASK, &event_mask);
name = g_strdup_printf("_NET_WM_CM_S%d", sc->num);
- ack = xcb_intern_atom(conn, FALSE, strlen(name), name);
- arep = xcb_intern_atom_reply(conn, ack, NULL);
+ ack = xcb_intern_atom(sc->dpy->conn, FALSE, strlen(name), name);
+ arep = xcb_intern_atom_reply(sc->dpy->conn, ack, NULL);
g_free(name);
- sck = xcb_get_selection_owner(conn, arep->atom);
- srep = xcb_get_selection_owner_reply(conn, sck, NULL);
+ xcb_grab_server(sc->dpy->conn);
+
+ sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
+ srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
taken = !!srep->owner;
free(srep);
if (taken) {
printf(_("screen %d already has a composite manager, skipping\n"),
sc->num);
- return FALSE;
+ ret = FALSE;
}
+ else {
+ xcb_timestamp_t time;
- xcb_set_selection_owner(conn, w, arep->atom, XCB_CURRENT_TIME);
- sck = xcb_get_selection_owner(conn, arep->atom);
- srep = xcb_get_selection_owner_reply(conn, sck, NULL);
- taken = srep->owner == w;
- if (taken) {
sc->selwin = w;
- return TRUE;
+ sc->selatom = arep->atom;
+
+ time = screen_timestamp(sc);
+
+ xcb_set_selection_owner(sc->dpy->conn, w, arep->atom, time);
+ sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
+ srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
+ taken = srep->owner == w;
+ free(srep);
+ if (taken && screen_init(sc)) {
+ screen_add_existing_windows(sc);
+ ret = TRUE;
+ }
+ else {
+ xcb_destroy_window(sc->dpy->conn, w);
+ ret = FALSE;
+ }
}
- else {
- xcb_destroy_window(conn, w);
+ g_free(arep);
+
+ xcb_ungrab_server(sc->dpy->conn);
+ xcb_flush(sc->dpy->conn);
+
+ return ret;
+}
+
+static gboolean
+screen_init(d_screen_t *sc)
+{
+ uint32_t mask;
+ xcb_generic_error_t *err;
+ xcb_void_cookie_t redir_ck;
+ xcb_composite_get_overlay_window_cookie_t overlay_ck;
+ xcb_composite_get_overlay_window_reply_t *overlay_rep;
+
+ redir_ck =
+ xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super.root,
+ XCB_COMPOSITE_REDIRECT_AUTOMATIC);
+
+#if 1
+ redir_ck =
+ xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super.root,
+ XCB_COMPOSITE_REDIRECT_MANUAL);
+
+ overlay_ck = xcb_composite_get_overlay_window(sc->dpy->conn,
+ sc->super.root);
+
+ /* check that the redirect worked */
+ err = xcb_request_check(sc->dpy->conn, redir_ck);
+ if (err) {
+ printf(_("unable to redirect rendering, another composite manager must be running"));
+ free(err);
+ return FALSE;
+ }
+
+ /* get the overlay window reply */
+ overlay_rep = xcb_composite_get_overlay_window_reply(sc->dpy->conn,
+ overlay_ck,
+ NULL);
+ if (!overlay_rep) {
+ printf(_("unable to get the composite overlay window\n"));
return FALSE;
}
+ sc->overlay = overlay_rep->overlay_win;
+ free(overlay_rep);
+
+ /* make the overlay window click-through */
+ if (sc->overlay) {
+ xcb_xfixes_region_t region;
+
+ region = xcb_generate_id(sc->dpy->conn);
+ xcb_xfixes_create_region(sc->dpy->conn, region, 0, NULL);
+ xcb_xfixes_set_window_shape_region(sc->dpy->conn,
+ sc->overlay,
+ XCB_SHAPE_SK_BOUNDING,
+ 0, 0, XCB_NONE);
+ xcb_xfixes_set_window_shape_region(sc->dpy->conn,
+ sc->overlay,
+ XCB_SHAPE_SK_INPUT,
+ 0, 0, region);
+ xcb_xfixes_destroy_region(sc->dpy->conn, region);
+ }
+#endif
+
+ mask = SELECTION_MASK;
+ xcb_change_window_attributes(sc->dpy->conn, sc->selwin,
+ XCB_CW_EVENT_MASK, &mask);
+ mask = ROOT_MASK;
+ xcb_change_window_attributes(sc->dpy->conn, sc->super.root,
+ XCB_CW_EVENT_MASK, &mask);
+
+ screen_update_root_pixmap(sc);
+
+ return TRUE;
+}
+
+void
+screen_update_root_pixmap(d_screen_t *sc)
+{
+ if (sc->root_pixmap_waiting) {
+ xcb_get_property_reply_t *rep;
+ rep = xcb_get_property_reply(sc->dpy->conn, sc->root_pixmap_ck[0],
+ NULL);
+ if (rep) free(rep);
+ rep = xcb_get_property_reply(sc->dpy->conn, sc->root_pixmap_ck[1],
+ NULL);
+ if (rep) free(rep);
+ rep = xcb_get_property_reply(sc->dpy->conn, sc->root_pixmap_ck[2],
+ NULL);
+ if (rep) free(rep);
+ }
+ sc->root_pixmap_ck[0] =
+ xcb_get_property_unchecked(sc->dpy->conn, FALSE,
+ sc->super.root, sc->dpy->a.xrootpmap_id,
+ sc->dpy->a.pixmap, 0, 1);
+ sc->root_pixmap_ck[1] =
+ xcb_get_property_unchecked(sc->dpy->conn, FALSE,
+ sc->super.root, sc->dpy->a.esetroot_pmap_id,
+ sc->dpy->a.pixmap, 0, 1);
+ sc->root_pixmap_ck[2] =
+ xcb_get_property_unchecked(sc->dpy->conn, FALSE,
+ sc->super.root, sc->dpy->a.xsetroot_id,
+ sc->dpy->a.pixmap, 0, 1);
+ sc->root_pixmap_waiting = TRUE;
+}
+
+d_window_t*
+screen_add_window(d_screen_t *sc, xcb_window_t wid)
+{
+ d_window_t *w;
+
+ w = window_new(wid, sc);
+ g_hash_table_insert(sc->winhash, &w->id, w);
+
+ window_create_damage(w);
+
+ //printf("screen added window 0x%x\n", w->id);
+ return w;
+}
+
+static void
+screen_add_existing_windows(d_screen_t *sc)
+{
+ xcb_query_tree_cookie_t ck;
+ xcb_query_tree_reply_t *rep;
+
+ ck = xcb_query_tree(sc->dpy->conn, sc->super.root);
+ rep = xcb_query_tree_reply(sc->dpy->conn, ck, NULL);
+ if (rep) {
+ xcb_window_iterator_t it;
+
+ it = xcb_query_tree_children_iterator(rep);
+ for (; it.rem; xcb_window_next(&it))
+ screen_add_window(sc, *it.data);
+
+ free(rep);
+ }
+}
+
+void
+screen_remove_window(d_screen_t *sc, struct d_window *w)
+{
+ //printf("screen removed window 0x%x\n", w->id);
+
+ window_destroy_damage(w);
+ g_hash_table_remove(sc->winhash, &w->id);
+ window_unref(w);
+}
+
+d_window_t*
+screen_find_window(d_screen_t *sc, xcb_window_t id)
+{
+ return g_hash_table_lookup(sc->winhash, &id);
+}
+
+static xcb_timestamp_t
+screen_timestamp(d_screen_t *sc)
+{
+ xcb_void_cookie_t ck;
+ xcb_timestamp_t time;
+
+ ck = xcb_change_property(sc->dpy->conn, XCB_PROP_MODE_REPLACE, sc->selwin,
+ sc->selatom, sc->selatom, 32, 0, NULL);
+ xcb_flush(sc->dpy->conn);
+ while (1) {
+ xcb_generic_event_t *ev;
+
+ ev = xcb_wait_for_event(sc->dpy->conn);
+ if (!ev) {
+ printf(_("IO error\n"));
+ exit(0);
+ }
+
+ /* expect errors.. */
+ if (!ev->response_type) {
+ display_error(sc->dpy, (xcb_generic_error_t*)ev);
+ free(ev);
+ continue;
+ }
+
+ if (ev->response_type == XCB_PROPERTY_NOTIFY &&
+ ev->full_sequence == ck.sequence)
+ {
+ time = ((xcb_property_notify_event_t*)ev)->time;
+ free(ev);
+ break;
+ }
+ }
+ //printf("created timestamp %lu\n", (unsigned long) time);
+ return time;
+}
+
+void
+screen_stacking_add(d_screen_t *sc, struct d_window *w)
+{
+ list_prepend(sc->stacking, w);
+}
+
+void
+screen_stacking_remove(d_screen_t *sc, struct d_window *w)
+{
+ list_remove(sc->stacking, w);
+}
+
+void
+screen_stacking_move_above(d_screen_t *sc, struct d_window *w,
+ struct d_window *above)
+{
+ d_list_it_t *wit = list_find(sc->stacking, w);
+ d_list_it_t *ait = list_find(sc->stacking, above);
+ list_move_before(sc->stacking, wit, ait);
+}
+
+void screen_stacking_move_to_top(d_screen_t *sc, struct d_window *w)
+{
+ d_list_it_t *wit = list_find(sc->stacking, w);
+ d_list_it_t *ait = list_top(sc->stacking);
+ list_move_before(sc->stacking, wit, ait);
+}
+
+void screen_stacking_move_to_bottom(d_screen_t *sc, struct d_window *w)
+{
+ d_list_it_t *wit = list_find(sc->stacking, w);
+ list_move_before(sc->stacking, wit, NULL);
+}
+
+static void
+screen_set_next_repaint(d_screen_t *sc)
+{
+ gettimeofday(&sc->next_repaint, NULL);
+ /* add time for the refresh rate (60 hz) */
+ time_add(&sc->next_repaint, 1000000/60);
+ sc->need_repaint = FALSE;
+}
+
+void
+screen_setup_default_functions(d_screen_t *sc)
+{
+ sc->screen_paint = screen_set_next_repaint;
+ sc->window_show = window_show;
+ sc->window_hide = window_hide;
+ sc->window_become_zombie = window_become_zombie;
+ sc->window_zombie_dead = window_zombie_dead;
+ sc->window_move = window_move;
+ sc->window_resize = window_resize;
+ sc->window_reshape = window_reshape;
+ sc->window_opacity_change = window_opacity_change;
+ sc->screen_root_pixmap_change = screen_update_root_pixmap;
+}
+
+void
+screen_add_plugin_data(d_screen_t *sc, int id, void *data)
+{
+ plugin_data_add(sc->plugin_data, id, data);
+}
+
+void*
+screen_find_plugin_data(d_screen_t *sc, int id)
+{
+ return plugin_data_find(sc->plugin_data, id);
+}
+
+void
+screen_remove_plugin_data(d_screen_t *sc, int id)
+{
+ plugin_data_remove(sc->plugin_data, id);
+}
+
+void
+screen_refresh(d_screen_t *sc)
+{
+ sc->need_repaint = TRUE;
+ //printf("*** need repaint! ***\n");
+}
+
+xcb_pixmap_t
+screen_get_root_pixmap(d_screen_t *sc)
+{
+ if (sc->root_pixmap_waiting) {
+ xcb_get_property_reply_t *rep;
+ int i;
+
+ sc->root_pixmap = XCB_NONE;
+ for (i = 2; i >= 0; --i) {
+ rep = xcb_get_property_reply(sc->dpy->conn, sc->root_pixmap_ck[i],
+ NULL);
+ if (rep) {
+ if (rep->type == sc->dpy->a.pixmap && rep->length >= 1) {
+ sc->root_pixmap =
+ ((xcb_pixmap_t*)xcb_get_property_value(rep))[0];
+ //printf("got root pixmap 0x%x\n", sc->root_pixmap);
+ }
+ free(rep);
+ }
+ }
+ }
+ return sc->root_pixmap;
}