summaryrefslogtreecommitdiffstats
path: root/mini-wm.c
blob: e2bbe9d4bedd0a4705918cce7602aa81bed8ed55 (plain)
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
/*
 * mini-wm.c - simple keyboard focus handling 'wm'.
 *
 * Owen Taylor <otaylor@redhat.com>
 *
 * Copyright 2002 Red Hat, Inc.
 *
 * This software may be freely redistributed under the terms of the GNU
 * public license.
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


#include <stdio.h>
#include <unistd.h>

#include <gdk/gdkx.h>
#include <gtk/gtk.h>

static gboolean
is_focusable (Window window)
{
  Display *xdisplay = GDK_DISPLAY ();
  XWindowAttributes xwa;
  gboolean result = FALSE;

  gdk_error_trap_push ();
  if (XGetWindowAttributes (xdisplay, window, &xwa))
    {
      if (!xwa.override_redirect && xwa.map_state == IsViewable)
	result = TRUE;
    }
  gdk_error_trap_pop ();

  return result;
}

static void
check_focus ()
{
  Window *children;
  unsigned int n_children;
  Window root;
  Window parent;
  
  XQueryTree (GDK_DISPLAY(), GDK_ROOT_WINDOW(),
	      &root, &parent, &children, &n_children);

  while (n_children > 0) {
      if (is_focusable (children[n_children-1])) {
	  gdk_error_trap_push ();
	  XSetInputFocus (GDK_DISPLAY(), children[n_children-1], 
			  RevertToPointerRoot, CurrentTime);
	  XSync (GDK_DISPLAY(), 0);
	  if (gdk_error_trap_pop () == 0)
	    break;
      }
      n_children--;
  }

  XFree (children);
}

GdkFilterReturn
mini_wm_root_filter (GdkXEvent *xevent,
		     GdkEvent *event,
		     gpointer  data)
{
  XEvent *xev = xevent;

  if (xev->xany.type == MapNotify ||
      xev->xany.type == UnmapNotify ||
      xev->xany.type == ConfigureNotify ||
      xev->xany.type == DestroyNotify)
    {
      check_focus ();
    }
    
  return GDK_FILTER_CONTINUE;
}

void
mini_wm_start (void)
{
  XWindowAttributes attrs;

  XGetWindowAttributes (GDK_DISPLAY(), GDK_ROOT_WINDOW(), &attrs);
  XSelectInput (GDK_DISPLAY(), GDK_ROOT_WINDOW(),
		attrs.your_event_mask | SubstructureNotifyMask);

  gdk_window_add_filter (GDK_ROOT_PARENT (), mini_wm_root_filter, NULL);

  check_focus ();
}

int main( int   argc,
          char *argv[] )
{

    gtk_init (&argc, &argv);

    mini_wm_start ();

    /* Indicate back to anaconda that we now have established
     * connection to the display. */
    write(1, "#", 1);

    gtk_main();

    return(0);
}