aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/wm/client.c
blob: 3139367a85b11faca1bff86421695959e2cfeb94 (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
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
#include "wm.h"

static char broken[] = "broken";

// -----------------------------------------------------------------------
// scripts

void *
move_client(Arg *arg)
{
    return nil;
}

void *
float_client(Arg *arg)
{
    return nil;
}

void *
resize_client(Arg *arg)
{
    return nil;
}

// -----------------------------------------------------------------------
// core

void
focus(Client *client, int lift)
{
    struct wlr_surface     *old, *new;
    struct wlr_xdg_surface *xdg;
    struct wlr_keyboard    *keyboard;

    if(!client) {
        wlr_seat_keyboard_notify_clear_focus(server.input.seat);
        return;
    }

    old = server.input.seat->keyboard_state.focused_surface;

    if(lift) {
        wl_list_remove(&client->stack);
        wl_list_insert(&server.client.stack, &client->stack);
    }

    new = client->xdg->surface;
    if(old==new)
        return;

    wl_list_remove(&client->focus);
    wl_list_insert(&server.client.focus, &client->focus);
    server.monitor.selected = client->monitor;
    client->isurgent = 0;

    // XXX: do we need for non-client case?
    if(old) {
        xdg = wlr_xdg_surface_from_wlr_surface(old);
        wlr_xdg_toplevel_set_activated(xdg, false);
    }

    keyboard = wlr_seat_get_keyboard(server.input.seat);

    wlr_seat_keyboard_notify_enter(server.input.seat, new,
        keyboard->keycodes,
        keyboard->num_keycodes,
        &keyboard->modifiers
    );

    wlr_xdg_toplevel_set_activated(client->xdg, true);
}

Client*
client_at(double x, double y)
{
    Client *client;
    wl_list_for_each(client, &server.client.list, link)
        if(VISIBLE_ON(client, client->monitor) && wlr_box_contains_point(&client->geometry, x, y))
            return client;
    return nil;
}

static
int
has(Client *client, double lx, double ly, struct wlr_surface **surface, double *sx, double *sy)
{
    double x, y, vsx = lx - client->geometry.x, vsy = ly - client->geometry.y;
    struct wlr_surface *find = nil;

    find = wlr_xdg_surface_surface_at(client->xdg, vsx, vsy, &x, &y);
    if(find) {
        *sx = x;
        *sy = y;
        *surface = find;
        return true;
    }

    return false;
}

struct wlr_surface *
client_surface_at(Client *client, double cx, double cy, double *sx, double *sy)
{
    return wlr_xdg_surface_surface_at(client->xdg, cx, cy, sx, sy);
}


static
void
constrain(Client *client, struct wlr_box *box)
{
    client->geometry.width  = MAX(1, client->geometry.width);
    client->geometry.height = MAX(1, client->geometry.height);

    if(client->geometry.x >= box->x + box->width)
        client->geometry.x = box->x + box->width  - client->geometry.width;
    if(client->geometry.y >= box->y + box->height)
        client->geometry.y = box->y + box->height - client->geometry.height;
    if(client->geometry.x + client->geometry.width  + 2*client->border <= box->x)
        client->geometry.x = box->x;
    if(client->geometry.y + client->geometry.height + 2*client->border <= box->y)
        client->geometry.y = box->y;
}

void
resize(Client *client, int x, int y, int w, int h, int interact)
{
    struct wlr_box *box = interact ? &server.monitor.geometry : &client->monitor->window;

    client->geometry.x = x;
    client->geometry.y = y;
    client->geometry.width  = w;
    client->geometry.height = h;

    constrain(client, box);

    client->resize = wlr_xdg_toplevel_set_size(client->xdg,
            client->geometry.width  - 2*client->border,
            client->geometry.height - 2*client->border
    );
}

void
attach(Client *client, Monitor *monitor, uint tags)
{
    Monitor *old = client->monitor;
    if(old == monitor)
        return;

    client->monitor = monitor;

    if(old) {
        wlr_surface_send_leave(client->xdg->surface, old->output);
        arrange(old);
    }

    if(monitor) {
        /* make sure window actually overlaps with the monitor */
        constrain(client, &monitor->geometry);
        wlr_surface_send_enter(client->xdg->surface, monitor->output);
        client->tags = tags ? tags : monitor->tag.set[monitor->tag.selected];
        arrange(monitor);
    }

    focus(focused_client(server.monitor.selected), 1);
}

void
rules(Client *client)
{
    /* rule matching */
    Rule *rule;
    uint i, tags;
    char *id, *title;
    Monitor *monitor, *it;

    monitor = server.monitor.selected;

    if (!(id=client->xdg->toplevel->app_id))
        id = broken;
    if (!(title=client->xdg->toplevel->title))
        title = broken;

    for(tags=0, rule=cfg·rule; rule != cfg·endrule; ++rule) {
        if ((!rule->title || strstr(title, rule->title)) 
         && (!rule->id    || strstr(id, rule->id))) {
            client->isfloating = rule->isfloating;
            tags |= rule->tags;
            i = 0;
            wl_list_for_each(it, &server.monitor.list, link)
                if(rule->monitor == i++)
                    monitor = it;
        }
    }

    attach(client, monitor, tags);
}