File: hints.c

package info (click to toggle)
oroborus 2.0.13-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,660 kB
  • ctags: 607
  • sloc: ansic: 6,650; sh: 647; makefile: 528
file content (520 lines) | stat: -rw-r--r-- 12,972 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/* 
 * Oroborus Window Manager
 * X11 Hints Utility Library 
 *
 * Copyright (C) 2001 Ken Lynch
 * Copyright (C) 2002 Stefan Pfetzing
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
 */

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xmd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "hints.h"
#include "xmalloc.h"

Atom wm_state, wm_change_state, wm_delete_window, wm_protocols;
Atom motif_wm_hints;
Atom win_hints, win_state, win_client_list, win_layer, win_workspace,
  win_workspace_count, win_desktop_button_proxy, win_supporting_wm_check,
  gnome_panel_desktop_area;
Atom net_atoms[NET_ATOM_COUNT];
Display *dpy;

void
initHints (Display * d)
{
  dpy = d;
  initICCCMHints ();
  initMotifHints ();
  initGnomeHints ();
  initNETHints ();
}

void
initICCCMHints ()
{
#ifdef DEBUG
  printf ("entering initICCCMHints\n");
#endif

  wm_state = XInternAtom (dpy, "WM_STATE", False);
  wm_change_state = XInternAtom (dpy, "WM_CHANGE_STATE", False);
  wm_delete_window = XInternAtom (dpy, "WM_DELETE_WINDOW", False);
  wm_protocols = XInternAtom (dpy, "WM_PROTOCOLS", False);
}

void
initNETHints ()
{
  Window root;
#ifdef DEBUG
  printf ("entering initNETHints\n");
#endif

  root = XDefaultRootWindow (dpy);

  net_atoms[NET_CLIENT_LIST] = XInternAtom (dpy, "_NET_CLIENT_LIST", False);
  net_atoms[NET_CLIENT_LIST_STACKING] =
    XInternAtom (dpy, "_NET_CLIENT_LIST_STACKING", False);
  net_atoms[NET_WM_WINDOW_TYPE] =
    XInternAtom (dpy, "_NET_WM_WINDOW_TYPE", False);
  net_atoms[NET_WM_WINDOW_TYPE_DESKTOP] =
    XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DESKTOP", False);
  net_atoms[NET_WM_WINDOW_TYPE_DOCK] =
    XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DOCK", False);
  net_atoms[NET_WM_WINDOW_TYPE_TOOLBAR] =
    XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_TOOLBAR", False);
  net_atoms[NET_WM_WINDOW_TYPE_MENU] =
    XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_MENU", False);
  net_atoms[NET_WM_WINDOW_TYPE_UTILITY] =
    XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_UTILITY", False);
  net_atoms[NET_WM_WINDOW_TYPE_SPLASH] =
    XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
  net_atoms[NET_WM_WINDOW_TYPE_DIALOG] =
    XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
  net_atoms[NET_WM_WINDOW_TYPE_NORMAL] =
    XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
  net_atoms[NET_WM_STATE] = XInternAtom (dpy, "_NET_WM_STATE", False);
  net_atoms[NET_WM_STATE_MODAL] =
    XInternAtom (dpy, "_NET_WM_STATE_MODAL", False);
  net_atoms[NET_WM_STATE_STICKY] =
    XInternAtom (dpy, "_NET_WM_STATE_STICKY", False);
  net_atoms[NET_WM_STATE_MAXIMIZED_VERT] =
    XInternAtom (dpy, "_NET_WM_STATE_MAXIMIZED_VERT", False);
  net_atoms[NET_WM_STATE_MAXIMIZED_HORZ] =
    XInternAtom (dpy, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
  net_atoms[NET_WM_STATE_SHADED] =
    XInternAtom (dpy, "_NET_WM_STATE_SHADED", False);
  net_atoms[NET_WM_STATE_SKIP_TASKBAR] =
    XInternAtom (dpy, "_NET_WM_STATE_SKIP_TASKBAR", False);
  net_atoms[NET_WM_STATE_SKIP_PAGER] =
    XInternAtom (dpy, "_NET_WM_STATE_SKIP_PAGER", False);
  net_atoms[NET_WM_STATE_HIDDEN] =
    XInternAtom (dpy, "_NET_WM_STATE_HIDDEN", False);
  net_atoms[NET_WM_STATE_ABOVE] =
    XInternAtom (dpy, "_NET_WM_STATE_ABOVE", False);
  net_atoms[NET_WM_STATE_FULLSCREEN] =
    XInternAtom (dpy, "_NET_WM_STATE_FULLSCREEN", False);
  net_atoms[NET_WM_NAME] = XInternAtom (dpy, "_NET_WM_NAME", False);
  net_atoms[NET_WM_DESKTOP] = XInternAtom (dpy, "_NET_WM_DESKTOP", False);
  net_atoms[NET_NUMBER_OF_DESKTOPS] =
    XInternAtom (dpy, "_NET_NUMBER_OF_DESKTOPS", False);
  net_atoms[NET_CURRENT_DESKTOP] =
    XInternAtom (dpy, "_NET_CURRENT_DESKTOP", False);
  net_atoms[NET_ACTIVE_WINDOW] =
    XInternAtom (dpy, "_NET_ACTIVE_WINDOW", False);
  net_atoms[NET_SUPPORTING_WM_CHECK] =
    XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
  net_atoms[NET_WORKAREA] = XInternAtom (dpy, "_NET_WORKAREA", False);
  net_atoms[NET_WM_WORKAREA] = XInternAtom (dpy, "_NET_WM_WORKAREA", False);
  net_atoms[NET_CLOSE_WINDOW] = XInternAtom (dpy, "_NET_CLOSE_WINDOW", False);
  net_atoms[NET_WM_STRUT] = XInternAtom (dpy, "_NET_WM_STRUT", False);
  XChangeProperty (dpy, root, XInternAtom (dpy, "_NET_SUPPORTED", False),
		   XA_ATOM, 32, PropModeReplace, (unsigned char *) net_atoms,
		   NET_ATOM_COUNT);
}

void
initMotifHints ()
{
#ifdef DEBUG
  printf ("entering initMotifHints\n");
#endif

  motif_wm_hints = XInternAtom (dpy, "_MOTIF_WM_HINTS", False);
}

void
initGnomeHints ()
{
  Window root;

#ifdef DEBUG
  printf ("entering initGnomeHints\n");
#endif

  root = XDefaultRootWindow (dpy);
  win_hints = XInternAtom (dpy, "_WIN_HINTS", False);
  win_state = XInternAtom (dpy, "_WIN_STATE", False);
  win_client_list = XInternAtom (dpy, "_WIN_CLIENT_LIST", False);
  win_layer = XInternAtom (dpy, "_WIN_LAYER", False);
  win_workspace = XInternAtom (dpy, "_WIN_WORKSPACE", False);
  win_workspace_count = XInternAtom (dpy, "_WIN_WORKSPACE_COUNT", False);
  win_desktop_button_proxy =
    XInternAtom (dpy, "_WIN_DESKTOP_BUTTON_PROXY", False);
  win_supporting_wm_check =
    XInternAtom (dpy, "_WIN_SUPPORTING_WM_CHECK", False);
  gnome_panel_desktop_area =
    XInternAtom (dpy, "GNOME_PANEL_DESKTOP_AREA", False);
}

long
getWMState (Window w)
{
  unsigned long *data = NULL, state = WithdrawnState;

#ifdef DEBUG
  printf ("entering getWmState\n");
#endif

  data = getPropData (w, wm_state, wm_state, NULL);
  if (data)
    {
      state = *data;
      XFree (data);
    }
  return state;
}

void
setWMState (Window w, long state)
{
  CARD32 data[2];

#ifdef DEBUG
  printf ("entering setWmState\n");
#endif

  data[0] = state;
  data[1] = None;

  XChangeProperty (dpy, w, wm_state, wm_state, 32, PropModeReplace,
		   (unsigned char *) data, 2);
}

PropMwmHints *
getMotifHints (Window w)
{
#ifdef DEBUG
  printf ("entering getMotifHints\n");
#endif

  return getPropData (w, motif_wm_hints, motif_wm_hints, NULL);
}

int
getGnomeHint (Window w, Atom a, long *value)
{
  int success = False;
  long *data = NULL;

#ifdef DEBUG
  printf ("entering getGnomeHint\n");
#endif

  if (a == win_layer)
    *value = WIN_LAYER_NORMAL;
  else
    *value = 0;

  data = getPropData (w, a, XA_CARDINAL, NULL);
  if (data)
    {
      *value = *data;
      success = True;
      XFree (data);
    }
  return success;
}

void
setGnomeHint (Window w, Atom a, long value)
{
#ifdef DEBUG
  printf ("entering setGnomeHint\n");
#endif

  XChangeProperty (dpy, w, a, XA_CARDINAL, 32, PropModeReplace,
		   (unsigned char *) &value, 1);
}

void
setNetWmName (Window w, Atom a, char *value)
{
#ifdef DEBUG
  printf ("entering setNetWmName\n");
#endif

  XChangeProperty (dpy, w, a, XA_STRING, 8, PropModeReplace,
		   (unsigned char *) value, strlen (value));
}

void
setSupportingWmCheck (Window w, Window w2)
{
#ifdef DEBUG
  printf ("entering setSupportingWmCheck\n");
#endif

  XChangeProperty (dpy, w, net_atoms[NET_SUPPORTING_WM_CHECK], XA_WINDOW, 32,
		   PropModeReplace, (unsigned char *) &w2, 1);
}

void
getEWMHState (Window w, long *value)
{
  unsigned long *data;
  unsigned long items = 0;
  unsigned long i;

  data = getPropData (w, net_atoms[NET_WM_STATE], XA_ATOM, &items);
  if (data)
    {
      for (i = 0; i < items; i++)
	{
	  char *currentName = XGetAtomName (dpy, data[i]);
	  char *configuredName =
	    XGetAtomName (dpy, net_atoms[NET_WM_STATE_STICKY]);
	  if (equals (currentName, configuredName))
	    *value |= WIN_STATE_STICKY;
	  XFree (currentName);
	  XFree (configuredName);
	}
      XFree (data);
    }
}

void
getGnomeDesktopMargins (CARD32 * margins)
{
  unsigned long items_read;
  CARD32 *data = NULL;

#ifdef DEBUG
  printf ("entering getGnomeDesktopMargins\n");
#endif

  data =
    getPropData (XDefaultRootWindow (dpy), gnome_panel_desktop_area,
		 XA_CARDINAL, &items_read);
  if (data && items_read >= 4)
    {
      margins[0] = data[0];
      margins[1] = data[1];
      margins[2] = data[2];
      margins[3] = data[3];
      XFree (data);
    }
  else
    {
      margins[0] = 0;
      margins[1] = 0;
      margins[2] = 0;
      margins[3] = 0;
    }
}

void
getNetWMStrut (Window win, CARD32 * margins, CARD32 * window_margins)
{
  unsigned long items_read;
  CARD32 *data = NULL;

#ifdef DEBUG
  printf ("entering getGnomeDesktopMargins\n");
#endif

  data = getPropData (win, net_atoms[NET_WM_STRUT], XA_CARDINAL, &items_read);
  if (data && items_read >= 4)
    {
      window_margins[0] = data[0];
      window_margins[1] = data[1];
      window_margins[2] = data[2];
      window_margins[3] = data[3];
      margins[0] += data[0];
      margins[1] += data[1];
      margins[2] += data[2];
      margins[3] += data[3];
      XFree (data);
    }
  else
    {
      window_margins[0] = 0;
      window_margins[1] = 0;
      window_margins[2] = 0;
      window_margins[3] = 0;
    }
}

void
delNetWMStrut (CARD32 * margins, CARD32 * data)
{
#ifdef DEBUG
  printf ("entering delNetWMStrut\n");
#endif

  if (data[0] || data[1] || data[2] || data[3])
    {
      margins[0] -= data[0];
      margins[1] -= data[1];
      margins[2] -= data[2];
      margins[3] -= data[3];
    }
}

void
setNetWorkarea (CARD32 * margins, int workspaces)
{
  struct workarea
  {
    CARD32 left, top, right, bottom;
  }
   *workarea;
  int i;

  if (workspaces <= 0)
    return;

  workarea = xcalloc (workspaces, sizeof (struct workarea));

  for (i = 0; i < workspaces; i++)
    {
      workarea[i].left = margins[0];
      workarea[i].top = margins[1];
      workarea[i].right =
	XDisplayWidth (dpy, XDefaultScreen (dpy)) - margins[1];
      workarea[i].bottom =
	XDisplayHeight (dpy, XDefaultScreen (dpy)) - margins[3];
    }

  XChangeProperty (dpy, XDefaultRootWindow (dpy),
		   net_atoms[NET_WORKAREA], XA_CARDINAL, 32,
		   PropModeReplace, (unsigned char *) workarea,
		   4 * workspaces);
  XChangeProperty (dpy, XDefaultRootWindow (dpy), net_atoms[NET_WM_WORKAREA],
		   XA_CARDINAL, 32, PropModeReplace,
		   (unsigned char *) workarea, 4 * workspaces);
  free (workarea);
}

int
typeDesktop (Window w)
{
  return typeEWMH (w, "_NET_WM_WINDOW_TYPE_DESKTOP");
}

int
typeDock (Window w)
{
  return typeEWMH (w, "_NET_WM_WINDOW_TYPE_DOCK");
}

int
typeAbove (Window w)
{
  return typeEWMH (w, "_NET_WM_STATE_ABOVE");
}

int
typeEWMH (Window w, char *type)
{
  unsigned long *data;
  unsigned long items = 0;
  unsigned long item = 0;
  int ret = 0;

#ifdef DEBUG
  printf ("entering typeEWMH\n");
#endif

  data = getPropData (w, net_atoms[NET_WM_WINDOW_TYPE], XA_ATOM, &items);
  while (data && items && item < items && !ret)
    {
      char *name;
      name = XGetAtomName (dpy, data[item]);
#ifdef DEBUG
      printf ("%s\n", name);
#endif
      ret = equals (name, type);
      XFree (name);
      item++;
    }
  if (data)
    XFree (data);

  return ret;
}

void *
getPropData (Window w, Atom prop, Atom type, unsigned long *items_ret)
{
  Atom type_ret;
  int format_ret;
  unsigned long after_ret;
  unsigned char *prop_data;

#ifdef DEBUG
  printf ("entering getPropData\n");
#endif
  prop_data = NULL;

  if (items_ret == NULL)
    {
      items_ret = (unsigned long *) xmalloc (sizeof (unsigned long));
      XGetWindowProperty (dpy, w, prop, 0, 0x7fffffff, False,
			  type, &type_ret, &format_ret, items_ret,
			  &after_ret, &prop_data);
      if (prop_data && *items_ret)
	{
	  free (items_ret);
	  return prop_data;
	}
      else
	{
	  free (items_ret);
	  return NULL;
	}
    }
  else
    {
      XGetWindowProperty (dpy, w, prop, 0, 0x7fffffff, False,
			  type, &type_ret, &format_ret, items_ret,
			  &after_ret, &prop_data);
      if (prop_data && *items_ret)
	return prop_data;
      else
	return NULL;
    }
}

void
setFocusHint (Window w)
{
#ifdef DEBUG
  printf ("entering setFocusHint\n");
#endif

  XChangeProperty (dpy, XDefaultRootWindow (dpy),
		   net_atoms[NET_ACTIVE_WINDOW], XA_WINDOW, 32,
		   PropModeReplace, (unsigned char *) &w, 1);
}

int
equals (char *left, char *right)
{
  if (left && right && (strcmp (left, right) == 0))
    return 1;
  else
    return 0;
}

/***This must remain at the end of the file.***********************************************
 * vi:set sw=2 cindent cinoptions={1s,>2s,^-1s,n-1s foldmethod=marker foldmarker=,: *
 ******************************************************************************************/