1 /**************************************************************************
3 * Tint2 : common windows function
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr) from Omega distribution
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 **************************************************************************/
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
30 #include <cairo-xlib.h>
40 void set_active (Window win
)
42 send_event32 (win
, server
.atom
._NET_ACTIVE_WINDOW
, 2, CurrentTime
, 0);
46 void set_desktop (int desktop
)
48 send_event32 (server
.root_win
, server
.atom
._NET_CURRENT_DESKTOP
, desktop
, 0, 0);
52 void windows_set_desktop (Window win
, int desktop
)
54 send_event32 (win
, server
.atom
._NET_WM_DESKTOP
, desktop
, 2, 0);
58 void set_close (Window win
)
60 send_event32 (win
, server
.atom
._NET_CLOSE_WINDOW
, 0, 2, 0);
64 void window_toggle_shade (Window win
)
66 send_event32 (win
, server
.atom
._NET_WM_STATE
, 2, server
.atom
._NET_WM_STATE_SHADED
, 0);
70 void window_maximize_restore (Window win
)
72 send_event32 (win
, server
.atom
._NET_WM_STATE
, 2, server
.atom
._NET_WM_STATE_MAXIMIZED_VERT
, 0);
73 send_event32 (win
, server
.atom
._NET_WM_STATE
, 2, server
.atom
._NET_WM_STATE_MAXIMIZED_HORZ
, 0);
77 int window_is_hidden (Window win
)
83 at
= server_get_property (win
, server
.atom
._NET_WM_STATE
, XA_ATOM
, &count
);
84 for (i
= 0; i
< count
; i
++) {
85 if (at
[i
] == server
.atom
._NET_WM_STATE_SKIP_TASKBAR
) {
89 // do not add transient_for windows if the transient window is already in the taskbar
91 while ( XGetTransientForHint(server
.dsp
, window
, &window
) ) {
92 if ( task_get_tasks(window
) ) {
100 at
= server_get_property (win
, server
.atom
._NET_WM_WINDOW_TYPE
, XA_ATOM
, &count
);
101 for (i
= 0; i
< count
; i
++) {
102 if (at
[i
] == server
.atom
._NET_WM_WINDOW_TYPE_DOCK
|| at
[i
] == server
.atom
._NET_WM_WINDOW_TYPE_DESKTOP
|| at
[i
] == server
.atom
._NET_WM_WINDOW_TYPE_TOOLBAR
|| at
[i
] == server
.atom
._NET_WM_WINDOW_TYPE_MENU
|| at
[i
] == server
.atom
._NET_WM_WINDOW_TYPE_SPLASH
) {
109 for (i
=0 ; i
< nb_panel
; i
++) {
110 if (panel1
[i
].main_win
== win
) {
116 // Windows with neither _NET_WM_WINDOW_TYPE nor WM_TRANSIENT_FOR set
117 // MUST be taken as top-level window.
122 int window_get_desktop (Window win
)
124 return get_property32(win
, server
.atom
._NET_WM_DESKTOP
, XA_CARDINAL
);
128 int window_get_monitor (Window win
)
133 XTranslateCoordinates(server
.dsp
, win
, server
.root_win
, 0, 0, &x
, &y
, &src
);
136 for (i
= 0; i
< server
.nb_monitor
; i
++) {
137 if (x
>= server
.monitor
[i
].x
&& x
<= (server
.monitor
[i
].x
+ server
.monitor
[i
].width
))
138 if (y
>= server
.monitor
[i
].y
&& y
<= (server
.monitor
[i
].y
+ server
.monitor
[i
].height
))
142 //printf("window %lx : ecran %d, (%d, %d)\n", win, i, x, y);
143 if (i
== server
.nb_monitor
) return 0;
148 int window_is_iconified (Window win
)
150 // EWMH specification : minimization of windows use _NET_WM_STATE_HIDDEN.
151 // WM_STATE is not accurate for shaded window and in multi_desktop mode.
154 at
= server_get_property (win
, server
.atom
._NET_WM_STATE
, XA_ATOM
, &count
);
155 for (i
= 0; i
< count
; i
++) {
156 if (at
[i
] == server
.atom
._NET_WM_STATE_HIDDEN
) {
166 int window_is_urgent (Window win
)
171 at
= server_get_property (win
, server
.atom
._NET_WM_STATE
, XA_ATOM
, &count
);
172 for (i
= 0; i
< count
; i
++) {
173 if (at
[i
] == server
.atom
._NET_WM_STATE_DEMANDS_ATTENTION
) {
183 int window_is_skip_taskbar (Window win
)
188 at
= server_get_property(win
, server
.atom
._NET_WM_STATE
, XA_ATOM
, &count
);
189 for (i
=0; i
<count
; i
++) {
190 if (at
[i
] == server
.atom
._NET_WM_STATE_SKIP_TASKBAR
) {
200 int server_get_number_of_desktop ()
202 return get_property32(server
.root_win
, server
.atom
._NET_NUMBER_OF_DESKTOPS
, XA_CARDINAL
);
206 int server_get_current_desktop ()
208 return get_property32(server
.root_win
, server
.atom
._NET_CURRENT_DESKTOP
, XA_CARDINAL
);
212 Window
window_get_active ()
214 return get_property32(server
.root_win
, server
.atom
._NET_ACTIVE_WINDOW
, XA_WINDOW
);
218 int window_is_active (Window win
)
220 return (win
== get_property32(server
.root_win
, server
.atom
._NET_ACTIVE_WINDOW
, XA_WINDOW
));
224 int get_icon_count (gulong
*data
, int num
)
226 int count
, pos
, w
, h
;
230 while (pos
+2 < num
) {
234 if (pos
> num
|| w
<= 0 || h
<= 0) break;
242 gulong
*get_best_icon (gulong
*data
, int icon_count
, int num
, int *iw
, int *ih
, int best_icon_size
)
244 int width
[icon_count
], height
[icon_count
], pos
, i
, w
, h
;
245 gulong
*icon_data
[icon_count
];
253 if (pos
+ w
* h
> num
) break;
257 icon_data
[i
] = &data
[pos
];
262 /* Try to find exact size */
264 for (i
= 0; i
< icon_count
; i
++) {
265 if (width
[i
] == best_icon_size
) {
271 /* Take the biggest or whatever */
274 for (i
= 0; i
< icon_count
; i
++) {
275 if (width
[i
] > highest
) {
282 *iw
= width
[icon_num
];
283 *ih
= height
[icon_num
];
284 return icon_data
[icon_num
];
288 void get_text_size(PangoFontDescription
*font
, int *height_ink
, int *height
, int panel_height
, char *text
, int len
)
290 PangoRectangle rect_ink
, rect
;
292 Pixmap pmap
= XCreatePixmap (server
.dsp
, server
.root_win
, panel_height
, panel_height
, server
.depth
);
294 cairo_surface_t
*cs
= cairo_xlib_surface_create (server
.dsp
, pmap
, server
.visual
, panel_height
, panel_height
);
295 cairo_t
*c
= cairo_create (cs
);
297 PangoLayout
*layout
= pango_cairo_create_layout (c
);
298 pango_layout_set_font_description (layout
, font
);
299 pango_layout_set_text (layout
, text
, len
);
301 pango_layout_get_pixel_extents(layout
, &rect_ink
, &rect
);
302 *height_ink
= rect_ink
.height
;
303 *height
= rect
.height
;
304 //printf("dimension : %d - %d\n", rect_ink.height, rect.height);
306 g_object_unref (layout
);
308 cairo_surface_destroy (cs
);
309 XFreePixmap (server
.dsp
, pmap
);
313 void get_text_size2(PangoFontDescription
*font
, int *height_ink
, int *height
, int *width
, int panel_height
, int panel_with
, char *text
, int len
)
315 PangoRectangle rect_ink
, rect
;
317 Pixmap pmap
= XCreatePixmap (server
.dsp
, server
.root_win
, panel_height
, panel_height
, server
.depth
);
319 cairo_surface_t
*cs
= cairo_xlib_surface_create (server
.dsp
, pmap
, server
.visual
, panel_height
, panel_with
);
320 cairo_t
*c
= cairo_create (cs
);
322 PangoLayout
*layout
= pango_cairo_create_layout (c
);
323 pango_layout_set_font_description (layout
, font
);
324 pango_layout_set_text (layout
, text
, len
);
326 pango_layout_get_pixel_extents(layout
, &rect_ink
, &rect
);
327 *height_ink
= rect_ink
.height
;
328 *height
= rect
.height
;
330 //printf("dimension : %d - %d\n", rect_ink.height, rect.height);
332 g_object_unref (layout
);
334 cairo_surface_destroy (cs
);
335 XFreePixmap (server
.dsp
, pmap
);