1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 loco.c for the Openbox window manager
4 Copyright (c) 2008 Derek Foreman
5 Copyright (c) 2008 Dana Jansens
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
21 #include "obt/mainloop.h"
22 #include "obt/display.h"
23 #include "obt/mainloop.h"
27 #include <GL/glxext.h>
28 #include <GL/glxtokens.h>
30 <dana> you want CreateNotify, DestroyNotify, MapNotify, UnmapNotify, and
32 <dana> and then xdamage or whatever
42 typedef struct _LocoList {
43 struct _LocoList *next;
44 struct _LocoList *prev;
48 static Window loco_root;
49 /* Maps X Window ID -> LocoWindow* */
50 static GHashTable *window_map;
51 /* Maps X Window ID -> LocoList* which is in the stacking_top/bottom list */
52 static GHashTable *stacking_map;
53 /* From top to bottom */
54 static LocoList *stacking_top;
55 static LocoList *stacking_bottom;
58 static void print_stacking()
62 for (it = stacking_top; it; it = it->next) {
63 printf("0x%lx\n", it->window->id);
68 static LocoList* loco_list_prepend(LocoList **top, LocoList **bottom,
71 LocoList *n = g_new(LocoList, 1);
76 if (n->next) n->next->prev = n;
79 if (!*bottom) *bottom = n;
83 static void loco_list_delete_link(LocoList **top, LocoList **bottom,
86 LocoList *prev = pos->prev;
87 LocoList *next = pos->next;
101 static void loco_list_move_before(LocoList **top, LocoList **bottom,
102 LocoList *move, LocoList *before)
104 LocoList *prev, *next;
106 /* these won't move it anywhere */
107 if (move == before || move->next == before) return;
112 /* remove it from the list */
113 if (next) next->prev = prev;
115 if (prev) prev->next = next;
121 move->prev = before->prev;
122 move->next->prev = move;
123 if (move->prev) move->prev->next = move;
126 /* after the bottom */
127 move->prev = *bottom;
129 if (move->prev) move->prev->next = move;
133 if (!move->prev) *top = move;
136 void composite_setup_window(LocoWindow *win)
138 if (win->input_only) return;
140 //XCompositeRedirectWindow(obt_display, win->id, CompositeRedirectAutomatic);
141 /*something useful = */XDamageCreate(obt_display, win->id, XDamageReportRawRectangles);
144 static void add_window(Window window)
148 XWindowAttributes attrib;
150 printf("add window 0x%lx\n", window);
152 if (!XGetWindowAttributes(obt_display, window, &attrib))
155 lw = g_new0(LocoWindow, 1);
157 lw->input_only = attrib.class == InputOnly;
160 lw->w = attrib.width;
161 lw->h = attrib.height;
162 g_hash_table_insert(window_map, &lw->id, lw);
163 /* new windows are at the top */
164 it = loco_list_prepend(&stacking_top, &stacking_bottom, lw);
165 g_hash_table_insert(stacking_map, &lw->id, it);
167 composite_setup_window(lw);
172 static void remove_window(Window window)
176 printf("remove window 0x%lx\n", window);
178 lw = g_hash_table_lookup(window_map, &window);
180 LocoList *pos = g_hash_table_lookup(stacking_map, &window);
183 loco_list_delete_link(&stacking_top, &stacking_bottom, pos);
184 g_hash_table_remove(stacking_map, &window);
185 g_hash_table_remove(window_map, &window);
193 void COMPOSTER_RAWR(const XEvent *e, gpointer data)
195 if (e->type == CreateNotify) {
196 add_window(e->xmap.window);
198 else if (e->type == DestroyNotify) {
199 remove_window(e->xdestroywindow.window);
201 else if (e->type == ReparentNotify) {
202 if (e->xreparent.parent == loco_root)
203 add_window(e->xreparent.window);
205 remove_window(e->xreparent.window);
207 else if (e->type == obt_display_extension_damage_basep + XDamageNotify) {
209 else if (e->type == ConfigureNotify) {
211 printf("Window 0x%lx moved or something\n", e->xconfigure.window);
213 lw = g_hash_table_lookup(window_map, &e->xconfigure.window);
215 LocoList *above, *pos;
217 pos = g_hash_table_lookup(stacking_map, &e->xconfigure.window);
218 above = g_hash_table_lookup(stacking_map, &e->xconfigure.above);
220 g_assert(pos != NULL && pos->window != NULL);
221 if (e->xconfigure.above && !above)
222 printf("missing windows from the stacking list!!\n");
224 lw->x = e->xconfigure.x;
225 lw->y = e->xconfigure.y;
226 lw->w = e->xconfigure.width;
227 lw->h = e->xconfigure.height;
228 printf("Window 0x%lx above 0x%lx\n", pos->window->id,
229 above ? above->window->id : 0);
230 loco_list_move_before(&stacking_top, &stacking_bottom, pos, above);
236 static void find_all_windows(gint screen)
241 if (!XQueryTree(obt_display, loco_root, &w, &w, &children, &nchild))
244 for (i = 0; i < nchild; ++i)
245 if (children[i] != None) add_window(children[i]);
247 if (children) XFree(children);
250 static guint window_hash(Window *w) { return *w; }
251 static gboolean window_comp(Window *w1, Window *w2) { return *w1 == *w2; }
253 void loco_set_mainloop(gint screen_num, ObtMainLoop *loop)
259 { GLX_DEPTH_SIZE, 1, GLX_DOUBLEBUFFER, GLX_RGBA, None };
261 loco_root = obt_root(screen_num);
263 vi = glXChooseVisual(obt_display, screen_num, config);
264 cont = glXCreateContext(obt_display, vi, NULL, GL_TRUE);
266 printf("context creation failed\n");
267 glXMakeCurrent(obt_display, loco_root, cont);
269 w = WidthOfScreen(ScreenOfDisplay(obt_display, screen_num));
270 h = HeightOfScreen(ScreenOfDisplay(obt_display, screen_num));
271 glViewport(0, 0, w, h);
272 glMatrixMode(GL_PROJECTION);
274 printf("Setting up an orthographic projection of %dx%d\n", w, h);
275 glOrtho(0, w, h, 0.0, -1.0, 100.0);
276 glMatrixMode(GL_MODELVIEW);
278 glClear(GL_COLOR_BUFFER_BIT);
279 glEnable(GL_TEXTURE_RECTANGLE_ARB);
280 glXSwapBuffers(obt_display, loco_root);
281 obt_main_loop_x_add(loop, COMPOSTER_RAWR, NULL, NULL);
282 window_map = g_hash_table_new((GHashFunc)window_hash,
283 (GEqualFunc)window_comp);
284 stacking_map = g_hash_table_new((GHashFunc)window_hash,
285 (GEqualFunc)window_comp);
286 stacking_top = stacking_bottom = NULL;
288 find_all_windows(screen_num);
291 void loco_shutdown(void)