all repos — openbox @ 6b79b47f232445b428d415faf47008631b822168

openbox fork - make it a bit more like ryudo

plugins/focus.c (raw)

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
#include "../kernel/dispatch.h"
#include "../kernel/screen.h"
#include "../kernel/client.h"
#include "../kernel/frame.h"
#include "../kernel/focus.h"
#include "../kernel/stacking.h"
#include "../kernel/openbox.h"
#include "../kernel/config.h"

void plugin_setup_config()
{
    ConfigValue val;

    config_def_set(config_def_new("focus.followMouse", Config_Bool,
                                  "Focus Follows Mouse",
                                  "Focus windows when the mouse pointer "
                                  "enters them."));
    val.bool = TRUE;
    config_set("focus.followMouse", Config_Bool, val);
    config_def_set(config_def_new("focus.focusNew", Config_Bool,
                                  "Focus New Windows",
                                  "Focus windows when they first appear."));
    val.bool = TRUE;
    config_set("focus.focusNew", Config_Bool, val);
/*
    config_def_set(config_def_new("focus.warpOnDeskSwitch", Config_Bool,
                                  "Warp Pointer On Desktop Switch",
                                  "Warps the pointer to the focused window "
                                  "when switching desktops."));
    config_set("focus.warpOnDeskSwitch", Config_Bool, FALSE);
*/
}

/*static int skip_enter = 0;*/

static gboolean focus_under_pointer()
{
    Window w;
    int i, x, y;
    guint u;
    GList *it;

    if (XQueryPointer(ob_display, ob_root, &w, &w, &x, &y, &i, &i, &u))
    {
        for (it = stacking_list; it != NULL; it = it->next) {
            Client *c = it->data;
            if (c->desktop == screen_desktop &&
                RECT_CONTAINS(c->frame->area, x, y))
                break;
        }
        if (it != NULL) {
            return client_normal(it->data) && client_focus(it->data);
        }
    }
    return FALSE;
}

static void chew_enter_events()
{
    XEvent e;

    /* XXX... not anymore
       skip the next enter event from the desktop switch so focus
       doesn't skip briefly to what was under the pointer */

    /* kill all enter events from prior to the desktop switch, we
       aren't interested in them if we have found our own target
       to focus.
       XXX this is rude to other plugins...can this be done
       better? count the events in the queue? */
    while (XCheckTypedEvent(ob_display, EnterNotify, &e));
/*
    {
        XPutBackEvent(ob_display, &e);
        g_message("skip");
        ++skip_enter;
    }
*/
}

static void focus_fallback(gboolean switching_desks)
{
    GList *it;

    for (it = focus_order[screen_desktop]; it != NULL; it = it->next)
        if (client_normal(it->data) && client_focus(it->data)) {
            if (switching_desks) {
                Client *c = it->data;

                chew_enter_events();

                if (FALSE /*warp_on_desk_switch*/) {
                    /* I have to do this warp twice! Otherwise windows dont get
                       Enter/Leave events when i warp on a desktop switch! */
                    XWarpPointer(ob_display, None, c->window, 0, 0, 0, 0,
                                 c->area.width / 2, c->area.height / 2);
                    XWarpPointer(ob_display, None, c->window, 0, 0, 0, 0,
                                 c->area.width / 2, c->area.height / 2);
                }
            }
            break;
        }
}

static void focus_desktop()
{
    GList *it;

    for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
        Client *client = it->data;
        if (client->type == Type_Desktop && client->frame->visible)
            if (client_focus(client))
                break;
    }
    chew_enter_events();
}

static void event(ObEvent *e, void *foo)
{
    ConfigValue follow_mouse, focus_new;
    gboolean r;
    
    r = config_get("focus.followMouse", Config_Bool, &follow_mouse);
    g_assert(r);

    switch (e->type) {
    case Event_Client_Mapped:
        r = config_get("focus.focusNew", Config_Bool, &focus_new);
        if (focus_new.bool && client_normal(e->data.c.client))
            client_focus(e->data.c.client);
        break;

    case Event_Client_Unmapped:
        if (ob_state == State_Exiting) break;

        if (client_focused(e->data.c.client))
            if (!follow_mouse.bool || !focus_under_pointer())
                focus_fallback(FALSE);
        break;

    case Event_Client_Desktop:
        /* focus the next available target if moving from the current
           desktop. */
        if ((unsigned)e->data.c.num[1] == screen_desktop)
            if (!follow_mouse.bool || !focus_under_pointer())
                focus_fallback(FALSE);

    case Event_Ob_Desktop:
        focus_fallback(TRUE);
        break;

    case Event_Ob_ShowDesktop:
        if (!e->data.o.num[0]) { /* hiding the desktop, showing the clients */
            if (!follow_mouse.bool || !focus_under_pointer())
                focus_fallback(TRUE);
        } else /* hiding clients, showing the desktop */
            focus_desktop();
        break;

    case Event_X_EnterNotify:
/*        if (skip_enter) {
            if (e->data.x.client != NULL)
                g_message("skipped enter %lx", e->data.x.client->window);
            else
                g_message("skipped enter 'root'");
            --skip_enter;
        }
        else*/
        if (follow_mouse.bool)
            if (e->data.x.client != NULL && client_normal(e->data.x.client))
                client_focus(e->data.x.client);
        break;

    default:
        g_assert_not_reached();
    }
}

void plugin_startup()
{
    dispatch_register(Event_Client_Mapped | 
                      Event_Ob_Desktop | 
                      Event_Client_Unmapped |
                      Event_X_EnterNotify |
                      Event_Ob_ShowDesktop,
                      (EventHandler)event, NULL);
}

void plugin_shutdown()
{
    dispatch_register(0, (EventHandler)event, NULL);
}