Lots of cleanups to fvwm-menu-desktop
[fvwm.git] / fvwm / ewmh_names.c
blob8fe74d8a5bc39e1c3f51451e6a61ab19489f99fa
1 /* -*-c-*- */
2 /* Copyright (C) 2001 Olivier Chapuis */
3 /* This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include "config.h"
20 #include <stdio.h>
21 #include <X11/Xlib.h>
22 #include <X11/Xmd.h>
24 #include "libs/fvwmlib.h"
25 #include "libs/Flocale.h"
26 #include "libs/FlocaleCharset.h"
27 #include "libs/Ficonv.h"
28 #include "fvwm.h"
29 #include "window_flags.h"
30 #include "cursor.h"
31 #include "functions.h"
32 #include "misc.h"
33 #include "screen.h"
34 #include "module_interface.h"
35 #include "borders.h"
36 #include "add_window.h"
37 #include "icons.h"
38 #include "ewmh.h"
39 #include "ewmh_intern.h"
40 #include "externs.h"
43 * set the visibale window name and icon name
45 void EWMH_SetVisibleName(FvwmWindow *fw, Bool is_icon_name)
47 unsigned char *val;
48 char *tmp_str;
49 FlocaleCharset *fc = NULL;
51 if (!FiconvSupport)
53 return;
56 /* set the ewmh visible name only if it is != wm name */
57 if (is_icon_name)
59 if ((fw->icon_name_count == 0 ||
60 !USE_INDEXED_ICON_NAME(fw)) &&
61 !HAS_EWMH_WM_ICON_NAME(fw) &&
62 !HAS_EWMH_WM_NAME(fw))
64 ewmh_DeleteProperty(
65 FW_W(fw), "_NET_WM_ICON_VISIBLE_NAME",
66 EWMH_ATOM_LIST_FVWM_WIN);
67 return;
69 if (IS_ICON_FONT_LOADED(fw) && fw->icon_font != NULL)
71 fc = fw->icon_font->str_fc;
73 tmp_str = fw->visible_icon_name;
75 else
77 if ((fw->name_count == 0 ||
78 !USE_INDEXED_WINDOW_NAME(fw)) &&
79 !HAS_EWMH_WM_NAME(fw) && !HAS_EWMH_WM_ICON_NAME(fw))
81 ewmh_DeleteProperty(
82 FW_W(fw), "_NET_WM_VISIBLE_NAME",
83 EWMH_ATOM_LIST_FVWM_WIN);
84 return;
86 if (IS_WINDOW_FONT_LOADED(fw) && fw->title_font != NULL)
88 fc = fw->title_font->str_fc;
90 tmp_str = fw->visible_name;
93 if (tmp_str == NULL)
95 return; /* should never happen */
98 val = (unsigned char *)FiconvCharsetToUtf8(
99 dpy, fc, tmp_str, strlen(tmp_str));
101 if (val == NULL)
103 return;
106 if (is_icon_name)
108 ewmh_ChangeProperty(
109 FW_W(fw), "_NET_WM_ICON_VISIBLE_NAME",
110 EWMH_ATOM_LIST_FVWM_WIN, (unsigned char *)val,
111 strlen((char *)val));
113 else
115 ewmh_ChangeProperty(
116 FW_W(fw), "_NET_WM_VISIBLE_NAME",
117 EWMH_ATOM_LIST_FVWM_WIN, (unsigned char *)val,
118 strlen((char *)val));
120 free(val);
124 * setup and property notify
126 int EWMH_WMIconName(EWMH_CMD_ARGS)
128 int size = 0;
129 char *val;
130 char *tmp_str;
131 FlocaleCharset *fc = NULL;
133 if (!FiconvSupport)
135 return 0;
138 val = ewmh_AtomGetByName(
139 FW_W(fw), "_NET_WM_ICON_NAME",
140 EWMH_ATOM_LIST_PROPERTY_NOTIFY, &size);
142 if (val == NULL)
144 SET_HAS_EWMH_WM_ICON_NAME(fw,0);
145 return 0;
147 if (IS_ICON_FONT_LOADED(fw) && fw->icon_font != NULL)
149 fc = fw->icon_font->str_fc;
152 tmp_str = (char *)FiconvUtf8ToCharset(
153 dpy, fc, (const char *) val, size);
154 free(val);
155 if (tmp_str == NULL)
157 SET_HAS_EWMH_WM_ICON_NAME(fw, 0);
158 return 0;
160 if (strlen(tmp_str) > MAX_ICON_NAME_LEN)
162 tmp_str[MAX_ICON_NAME_LEN] = 0;
164 SET_HAS_EWMH_WM_ICON_NAME(fw, 1);
165 if (fw->icon_name.name && strcmp(tmp_str, fw->icon_name.name) == 0)
167 /* migo: some apps update their names every second */
168 free(tmp_str);
169 return 0;
172 if (ev != NULL)
174 /* client message */
175 free_window_names(fw, False, True);
178 fw->icon_name.name = tmp_str;
180 SET_WAS_ICON_NAME_PROVIDED(fw, 1);
182 if (ev == NULL)
184 /* return now for setup */
185 return 1;
188 setup_visible_name(fw, True);
189 EWMH_SetVisibleName(fw, True);
190 BroadcastWindowIconNames(fw, False, True);
191 RedoIconName(fw);
192 return 1;
195 int EWMH_WMName(EWMH_CMD_ARGS)
197 int size = 0;
198 char *val;
199 char *tmp_str;
200 FlocaleCharset *fc = NULL;
202 if (!FiconvSupport)
203 return 0;
205 val = ewmh_AtomGetByName(
206 FW_W(fw), "_NET_WM_NAME",
207 EWMH_ATOM_LIST_PROPERTY_NOTIFY, &size);
209 if (val == NULL)
211 SET_HAS_EWMH_WM_NAME(fw,0);
212 return 0;
214 if (IS_WINDOW_FONT_LOADED(fw) && fw->title_font != NULL)
216 fc = fw->title_font->str_fc;
219 tmp_str = (char *)FiconvUtf8ToCharset(
220 dpy, fc, (const char *) val, size);
221 free(val);
222 if (tmp_str == NULL)
224 SET_HAS_EWMH_WM_NAME(fw,0);
225 return 0;
227 if (strlen(tmp_str) > MAX_WINDOW_NAME_LEN)
229 tmp_str[MAX_WINDOW_NAME_LEN] = 0;
231 SET_HAS_EWMH_WM_NAME(fw, 1);
232 if (fw->name.name && strcmp(tmp_str, fw->name.name) == 0)
234 /* migo: some apps update their names every second */
235 free(tmp_str);
236 return 0;
239 if (ev != NULL)
241 /* client message */
242 free_window_names(fw, True, False);
245 fw->name.name = tmp_str;
247 if (ev == NULL)
249 return 1;
252 setup_visible_name(fw, False);
253 SET_NAME_CHANGED(fw, 1);
254 EWMH_SetVisibleName(fw, False);
255 BroadcastWindowIconNames(fw, True, False);
257 /* fix the name in the title bar */
258 if (!IS_ICONIFIED(fw))
260 border_draw_decorations(
261 fw, PART_TITLE, (Scr.Hilite == fw),
262 True, CLEAR_ALL, NULL, NULL);
265 if (!WAS_ICON_NAME_PROVIDED(fw))
267 fw->icon_name = fw->name;
268 setup_visible_name(fw, True);
269 BroadcastWindowIconNames(fw, False, True);
270 EWMH_SetVisibleName(fw, True);
271 RedoIconName(fw);
273 return 0;
276 #define MAX(A,B) ((A)>(B)? (A):(B))
278 * set the desktop name
280 void EWMH_SetDesktopNames(void)
282 int nbr = 0;
283 int len = 0;
284 int i;
285 int j = 0;
286 DesktopsInfo *d,*s;
287 unsigned char **names;
288 unsigned char *val;
290 if (!FiconvSupport)
292 return;
295 d = Scr.Desktops->next;
296 /* skip negative desk */
297 while (d != NULL && d->desk < 0)
299 d = d->next;
301 s = d;
302 while (d != NULL && d->name != NULL && d->desk == nbr)
304 nbr++;
305 d = d->next;
307 if (nbr == 0)
309 return;
311 names = (void *)safemalloc(sizeof(*names)*nbr);
312 for (i = 0; i < nbr; i++)
314 names[i] = (unsigned char *)FiconvCharsetToUtf8(
315 dpy, NULL, s->name, strlen(s->name));
316 if (names[i])
318 len += strlen((char *)names[i]) + 1;
320 else
322 len++;
324 s = s->next;
326 val = (unsigned char *)safemalloc(len);
327 for (i = 0; i < nbr; i++)
329 if (names[i] != NULL)
331 memcpy(&val[j], names[i], strlen((char *)names[i]));
332 j += strlen((char *)names[i]);
333 free(names[i]);
335 val[j++] = '\0';
337 ewmh_ChangeProperty(
338 Scr.Root, "_NET_DESKTOP_NAMES", EWMH_ATOM_LIST_CLIENT_ROOT,
339 (unsigned char *)val, len);
340 free(names);
341 free(val);