4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* tasklist.c - code for tracking windows
24 * Loosly based on code in GNOME's libwnck.
36 #include <X11/Xatom.h>
43 #include "gui_support.h"
48 /* There is one of these for each window controlled by the window
49 * manager (all tasks) in the _NET_CLIENT_LIST property.
51 typedef struct _IconWindow IconWindow
;
54 GtkWidget
*widget
; /* Widget used for icon when iconified */
61 static GdkAtom xa_WM_STATE
= GDK_NONE
;
62 static GdkAtom xa_WM_NAME
= GDK_NONE
;
63 static GdkAtom xa_WM_ICON_NAME
= GDK_NONE
;
64 static GdkAtom xa_UTF8_STRING
= GDK_NONE
;
65 static GdkAtom xa_TEXT
= GDK_NONE
;
66 static GdkAtom xa__NET_WM_VISIBLE_NAME
= GDK_NONE
;
67 static GdkAtom xa__NET_WM_ICON_NAME
= GDK_NONE
;
68 static GdkAtom xa__NET_CLIENT_LIST
= GDK_NONE
;
69 static GdkAtom xa__NET_WM_ICON_GEOMETRY
= GDK_NONE
;
71 /* We have selected destroy and property events on every window in
74 static GHashTable
*known
= NULL
; /* XID -> IconWindow */
76 /* Static prototypes */
77 static void remove_window(Window win
);
78 static void tasklist_update(gboolean to_empty
);
79 static GdkFilterReturn
window_filter(GdkXEvent
*xevent
,
82 static guint
xid_hash(XID
*xid
);
83 static gboolean
xid_equal(XID
*a
, XID
*b
);
84 static void state_changed(IconWindow
*win
);
85 static void show_icon(IconWindow
*win
);
86 static void icon_win_free(IconWindow
*win
);
87 static void update_style(gpointer key
, gpointer data
, gpointer user_data
);
89 /****************************************************************
90 * EXTERNAL INTERFACE *
91 ****************************************************************/
93 void tasklist_set_active(gboolean active
)
95 static gboolean need_init
= TRUE
;
96 static gboolean tasklist_active
= FALSE
;
98 if (active
== tasklist_active
)
100 tasklist_active
= active
;
106 root
= gdk_get_default_root_window();
108 xa_WM_STATE
= gdk_atom_intern("WM_STATE", FALSE
);
109 xa_WM_ICON_NAME
= gdk_atom_intern("WM_ICON_NAME", FALSE
);
110 xa_WM_NAME
= gdk_atom_intern("WM_NAME", FALSE
);
111 xa_UTF8_STRING
= gdk_atom_intern("UTF8_STRING", FALSE
);
112 xa_TEXT
= gdk_atom_intern("TEXT", FALSE
);
113 xa__NET_CLIENT_LIST
=
114 gdk_atom_intern("_NET_CLIENT_LIST", FALSE
);
115 xa__NET_WM_VISIBLE_NAME
=
116 gdk_atom_intern("_NET_WM_VISIBLE_NAME", FALSE
);
117 xa__NET_WM_ICON_NAME
=
118 gdk_atom_intern("_NET_WM_ICON_NAME", FALSE
);
119 xa__NET_WM_ICON_GEOMETRY
=
120 gdk_atom_intern("_NET_WM_ICON_GEOMETRY", FALSE
);
122 known
= g_hash_table_new_full((GHashFunc
) xid_hash
,
123 (GEqualFunc
) xid_equal
,
125 (GDestroyNotify
) icon_win_free
);
126 gdk_window_set_events(root
, gdk_window_get_events(root
) |
127 GDK_PROPERTY_CHANGE_MASK
);
132 gdk_window_add_filter(NULL
, window_filter
, NULL
);
134 gdk_window_remove_filter(NULL
, window_filter
, NULL
);
136 tasklist_update(!active
);
139 /* User has changes the colours in the options box... */
140 void tasklist_style_changed(void)
142 g_hash_table_foreach(known
, update_style
, NULL
);
145 /****************************************************************
146 * INTERNAL FUNCTIONS *
147 ****************************************************************/
149 static void icon_win_free(IconWindow
*win
)
151 g_return_if_fail(win
->widget
== NULL
);
152 g_return_if_fail(win
->label
== NULL
);
160 static guint
xid_hash(XID
*xid
)
166 static gboolean
xid_equal(XID
*a
, XID
*b
)
171 static int wincmp(const void *a
, const void *b
)
173 const Window
*aw
= a
;
174 const Window
*bw
= b
;
184 /* Read the list of WINDOWs from (xwindow,atom), returning them
185 * in a (sorted) Array of Windows. On error, an empty array is
187 * Free the array afterwards.
189 static GArray
*get_window_list(Window xwindow
, GdkAtom atom
)
200 array
= g_array_new(FALSE
, FALSE
, sizeof(Window
));
202 gdk_error_trap_push();
204 result
= XGetWindowProperty(gdk_display
,
206 gdk_x11_atom_to_xatom(atom
),
208 False
, XA_WINDOW
, &type
, &format
, &nitems
,
209 &bytes_after
, (guchar
**)&data
);
210 err
= gdk_error_trap_pop();
212 if (err
!= Success
|| result
!= Success
)
215 if (type
== XA_WINDOW
)
217 for (i
= 0; i
< nitems
; i
++)
218 g_array_append_val(array
, data
[i
]);
221 g_array_sort(array
, wincmp
);
229 static gchar
*get_str(IconWindow
*win
, GdkAtom atom
)
235 char *data
, *str
= NULL
;
237 if (XGetWindowProperty(gdk_display
, win
->xwindow
,
238 gdk_x11_atom_to_xatom(atom
),
241 &rtype
, &format
, &nitems
,
242 &bytes_after
, (guchar
**) &data
) == Success
&& data
)
245 str
= g_strdup(data
);
252 static void get_icon_name(IconWindow
*win
)
260 win
->text
= get_str(win
, xa__NET_WM_ICON_NAME
);
262 win
->text
= get_str(win
, xa__NET_WM_VISIBLE_NAME
);
264 win
->text
= get_str(win
, xa_WM_ICON_NAME
);
266 win
->text
= get_str(win
, xa_WM_NAME
);
268 win
->text
= g_strdup(_("Window"));
271 /* Call from within error_push/pop */
272 static void window_check_status(IconWindow
*win
)
281 if (XGetWindowProperty(gdk_display
, win
->xwindow
,
282 gdk_x11_atom_to_xatom(xa_WM_STATE
),
284 gdk_x11_atom_to_xatom(xa_WM_STATE
),
285 &type
, &format
, &nitems
,
286 &bytes_after
, (guchar
**) &data
) == Success
&& data
)
288 iconic
= data
[0] == 3;
294 if (win
->iconified
== iconic
)
297 win
->iconified
= iconic
;
302 /* Called for all events on all windows */
303 static GdkFilterReturn
window_filter(GdkXEvent
*xevent
,
307 XEvent
*xev
= (XEvent
*) xevent
;
310 if (xev
->type
== PropertyNotify
)
312 GdkAtom atom
= gdk_x11_xatom_to_atom(xev
->xproperty
.atom
);
313 Window win
= ((XPropertyEvent
*) xev
)->window
;
315 if (atom
== xa_WM_STATE
)
317 w
= g_hash_table_lookup(known
, &win
);
321 gdk_error_trap_push();
322 window_check_status(w
);
323 if (gdk_error_trap_pop() != Success
)
324 g_hash_table_remove(known
, &win
);
328 if (atom
== xa__NET_CLIENT_LIST
)
329 tasklist_update(FALSE
);
332 return GDK_FILTER_CONTINUE
;
335 /* Window has been added to list of managed windows */
336 static void add_window(Window win
)
340 /* g_print("[ New window %ld ]\n", (long) win); */
342 w
= g_hash_table_lookup(known
, &win
);
346 XWindowAttributes attr
;
348 gdk_error_trap_push();
350 XGetWindowAttributes(gdk_display
, win
, &attr
);
352 XSelectInput(gdk_display
, win
, attr
.your_event_mask
|
355 if (gdk_error_trap_pop() != Success
)
358 w
= g_new(IconWindow
, 1);
363 w
->iconified
= FALSE
;
365 g_hash_table_insert(known
, &w
->xwindow
, w
);
368 gdk_error_trap_push();
370 window_check_status(w
);
376 if (gdk_error_trap_pop() != Success
)
377 g_hash_table_remove(known
, &win
);
380 /* Window is no longer managed, but hasn't been destroyed yet */
381 static void remove_window(Window win
)
385 /* g_print("[ Remove window %ld ]\n", (long) win); */
387 w
= g_hash_table_lookup(known
, &win
);
392 w
->iconified
= FALSE
;
396 g_hash_table_remove(known
, &win
);
400 /* Make sure the window list is up-to-date. Call once to start, and then
401 * everytime _NET_CLIENT_LIST changes.
402 * If 'to_empty' is set them pretend all windows have disappeared.
404 static void tasklist_update(gboolean to_empty
)
406 static GArray
*old_mapping
= NULL
;
407 GArray
*mapping
= NULL
;
412 old_mapping
= g_array_new(FALSE
, FALSE
, sizeof(Window
));
416 mapping
= g_array_new(FALSE
, FALSE
, sizeof(Window
));
418 mapping
= get_window_list(gdk_x11_get_default_root_xwindow(),
419 gdk_atom_intern("_NET_CLIENT_LIST", FALSE
));
423 while (new_i
< mapping
->len
&& old_i
< old_mapping
->len
)
425 Window
new = g_array_index(mapping
, Window
, new_i
);
426 Window old
= g_array_index(old_mapping
, Window
, old_i
);
444 while (new_i
< mapping
->len
)
446 add_window(g_array_index(mapping
, Window
, new_i
));
449 while (old_i
< old_mapping
->len
)
451 remove_window(g_array_index(old_mapping
, Window
, old_i
));
455 g_array_free(old_mapping
, TRUE
);
456 old_mapping
= mapping
;
459 /* Called when the user clicks on the button */
460 static void uniconify(IconWindow
*win
)
462 XClientMessageEvent sev
;
464 sev
.type
= ClientMessage
;
465 sev
.display
= gdk_display
;
467 sev
.window
= win
->xwindow
;
468 sev
.message_type
= gdk_x11_atom_to_xatom(
469 gdk_atom_intern("_NET_ACTIVE_WINDOW", FALSE
));
472 gdk_error_trap_push();
474 XSendEvent(gdk_display
, DefaultRootWindow(gdk_display
), False
,
475 SubstructureNotifyMask
| SubstructureRedirectMask
,
477 XSync (gdk_display
, False
);
479 gdk_error_trap_pop();
482 static gint drag_start_x
= -1;
483 static gint drag_start_y
= -1;
484 static gboolean drag_started
= FALSE
;
485 static gint drag_off_x
= -1;
486 static gint drag_off_y
= -1;
488 static void icon_button_press(GtkWidget
*widget
,
489 GdkEventButton
*event
,
492 if (event
->button
== 1)
494 drag_start_x
= event
->x_root
;
495 drag_start_y
= event
->y_root
;
496 drag_started
= FALSE
;
498 drag_off_x
= event
->x
;
499 drag_off_y
= event
->y
;
503 static gboolean
icon_motion_notify(GtkWidget
*widget
,
504 GdkEventMotion
*event
,
507 if (event
->state
& GDK_BUTTON1_MASK
)
509 int dx
= event
->x_root
- drag_start_x
;
510 int dy
= event
->y_root
- drag_start_y
;
514 if (abs(dx
) < 5 && abs(dy
) < 5)
519 fixed_move_fast(GTK_FIXED(win
->widget
->parent
),
521 event
->x_root
- drag_off_x
,
522 event
->y_root
- drag_off_y
);
528 static void button_released(GtkWidget
*widget
, IconWindow
*win
)
534 /* A window has been iconified -- display it on the screen */
535 static void show_icon(IconWindow
*win
)
537 static MaskedPixmap
*icon
= NULL
;
540 g_return_if_fail(win
->widget
== NULL
);
541 g_return_if_fail(win
->label
== NULL
);
544 icon
= load_pixmap("images/iconified.png");
546 win
->widget
= gtk_button_new();
547 vbox
= gtk_vbox_new(FALSE
, 0);
548 gtk_container_add(GTK_CONTAINER(win
->widget
), vbox
);
550 gtk_box_pack_start(GTK_BOX(vbox
),
551 gtk_image_new_from_pixbuf(icon
->pixbuf
),
554 gtk_button_set_relief(GTK_BUTTON(win
->widget
), GTK_RELIEF_NONE
);
556 win
->label
= wrapped_label_new(win
->text
, 180);
558 update_style(NULL
, win
, NULL
);
560 gtk_box_pack_start(GTK_BOX(vbox
), win
->label
, FALSE
, TRUE
, 0);
562 gtk_widget_add_events(win
->widget
, GDK_BUTTON1_MOTION_MASK
);
563 g_signal_connect(win
->widget
, "button-press-event",
564 G_CALLBACK(icon_button_press
), win
);
565 g_signal_connect(win
->widget
, "motion-notify-event",
566 G_CALLBACK(icon_motion_notify
), win
);
567 g_signal_connect(win
->widget
, "released",
568 G_CALLBACK(button_released
), win
);
570 gtk_widget_show_all(vbox
); /* So the size comes out right */
571 pinboard_add_widget(win
->widget
);
572 gtk_widget_show(win
->widget
);
575 /* A window has been destroyed/expanded -- remove its icon */
576 static void hide_icon(IconWindow
*win
)
578 GtkWidget
*widget
= win
->widget
;
580 g_return_if_fail(widget
!= NULL
);
584 gtk_widget_destroy(widget
);
587 static void state_changed(IconWindow
*win
)
599 /* Set the _NET_WM_ICON_GEOMETRY property, which indicates where this window
600 * will be iconified to. Should be inside a push/pop.
602 static void set_iconify_pos(IconWindow
*win
)
606 data
[0] = iconify_next_x
;
607 data
[1] = iconify_next_y
;
611 XChangeProperty(gdk_display
, win
->xwindow
,
612 gdk_x11_atom_to_xatom(xa__NET_WM_ICON_GEOMETRY
),
613 XA_CARDINAL
, 32, PropModeReplace
, (guchar
*) data
, 4);
617 static void update_style(gpointer key
, gpointer data
, gpointer user_data
)
619 IconWindow
*win
= (IconWindow
*) data
;
623 gtk_widget_modify_fg(win
->label
,
624 GTK_STATE_NORMAL
, &pin_text_fg_col
);
625 gtk_widget_modify_bg(win
->label
,
626 GTK_STATE_NORMAL
, &pin_text_bg_col
);