-
Notifications
You must be signed in to change notification settings - Fork 2
/
key_event.c
255 lines (255 loc) · 6.68 KB
/
key_event.c
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
/* Copyright 2020, Alisa Bedard */
/* Module: */
#include "key_event.h"
/* Local: */
#include "JBWMClient.h"
#include "JBWMKeys.h"
#include "JBWMScreen.h"
#include "client.h"
#include "config.h"
#include "drag.h"
#include "exec.h"
#include "log.h"
#include "max.h"
#include "move_resize.h"
#include "select.h"
#include "snap.h"
#include "title_bar.h"
#include "vdesk.h"
#include "wm_state.h"
/* System: */
#include <stdbool.h>
#include <stdlib.h>
/* Program: */
__attribute__((nonnull))
static void point(struct JBWMClient * c,
const int16_t x, const int16_t y)
{
Display * d = c->screen->xlib->display;
XRaiseWindow(d, c->parent);
jbwm_warp(d, c->window, x, y);
}
__attribute__((nonnull))
static void commit_key_move(struct JBWMClient * c)
{
jbwm_snap_border(c);
jbwm_move_resize(c);
point(c, 1, 1);
}
enum KeyMoveFlags {
KEY_MOVE_HORIZONTAL = 1,
KEY_MOVE_POSITIVE = 2,
KEY_MOVE_MODIFIER = 4
};
static inline bool can_resize(struct JBWMClientOptions * opt)
{
return !opt->shaped && !opt->no_resize;
}
static inline bool has_sufficient_size(const int value)
{
return value > JBWM_RESIZE_INCREMENT << 1;
}
static int16_t * get_antecedent(struct JBWMClient * c, const
uint8_t flags)
{
int16_t * ret;
union JBWMRectangle * s = &c->size;
int16_t * wh = flags & KEY_MOVE_HORIZONTAL ? &s->width : &s->height;
if((flags & KEY_MOVE_MODIFIER) && has_sufficient_size(*wh)
&& can_resize(&c->opt))
ret = wh;
else
ret = (flags & KEY_MOVE_HORIZONTAL ? &s->x : &s->y);
return ret;
}
__attribute__((nonnull))
static void key_move(struct JBWMClient * c, uint8_t const flags)
{
int8_t const d = flags & KEY_MOVE_POSITIVE
? JBWM_RESIZE_INCREMENT : - JBWM_RESIZE_INCREMENT;
*get_antecedent(c, flags) += d;
commit_key_move(c);
}
static uint8_t get_move_flags(const KeySym k, const bool mod)
{
uint8_t flags = (mod ? KEY_MOVE_MODIFIER : 0) | KEY_MOVE_POSITIVE;
switch (k) {
case JBWM_KEY_LEFT:
flags &= ~KEY_MOVE_POSITIVE;
/* FALLTHROUGH */
case JBWM_KEY_RIGHT:
flags |= KEY_MOVE_HORIZONTAL;
break;
case JBWM_KEY_UP:
flags &= ~KEY_MOVE_POSITIVE;
}
return flags;
}
static void handle_key_move(struct JBWMClient * c,
const KeySym k, const bool mod)
{
/* These operations invalid when fullscreen. */
if (c->opt.fullscreen)
return;
key_move(c, get_move_flags(k, mod));
}
static void toggle_maximize(struct JBWMClient * c)
{
const struct JBWMClientOptions o = c->opt;
/* Honor mwm hints. Do not maximize shaped windows. */
/* Ignore fullscreen windows. Let the fullscreen code handle them. */
if (!o.no_max && !o.fullscreen && !o.shaped) {
if (o.max_horz && o.max_vert) {
jbwm_set_not_horz(c);
jbwm_set_not_vert(c);
} else {
jbwm_set_horz(c);
jbwm_set_vert(c);
}
}
}
__attribute__((nonnull))
static void handle_client_key_event(struct JBWMClient ** head_client,
struct JBWMClient ** current_client,
const bool mod, const KeySym key)
{
struct JBWMClient * c;
JBWM_LOG("handle_client_key_event: %d", (int)key);
c=*current_client;
if (c) {
if (c->opt.fullscreen) {
/* only allow exiting from fullscreen */
if (key == JBWM_KEY_FS)
jbwm_set_not_fullscreen(c);
return; /* prevent other operations while fullscreen */
}
switch (key) {
case JBWM_KEY_LEFT:
case JBWM_KEY_RIGHT:
case JBWM_KEY_UP:
case JBWM_KEY_DOWN:
handle_key_move(c, key, mod);
break;
case JBWM_KEY_KILL:
jbwm_send_wm_delete(c);
break;
case JBWM_KEY_LOWER:
case JBWM_KEY_ALTLOWER:
XLowerWindow(c->screen->xlib->display, c->parent);
break;
case JBWM_KEY_RAISE:
XRaiseWindow(c->screen->xlib->display, c->parent);
break;
case JBWM_KEY_FS:
jbwm_set_fullscreen(c);
break;
case JBWM_KEY_MAX:
toggle_maximize(c);
break;
case JBWM_KEY_MAX_H:
(c->opt.max_horz ? jbwm_set_not_horz : jbwm_set_horz)(c);
break;
case JBWM_KEY_MAX_V:
(c->opt.max_vert ? jbwm_set_not_vert : jbwm_set_vert)(c);
break;
case JBWM_KEY_STICK:
jbwm_toggle_sticky(c, current_client);
break;
case JBWM_KEY_MOVE:
jbwm_drag(c, head_client, false);
break;
case JBWM_KEY_SHADE:
jbwm_toggle_shade(c);
break;
}
}
}
static void next(struct JBWMClient * c,
struct JBWMClient ** current_client, struct JBWMClient ** head_client,
uint8_t const v){
if(!c->next)
c=*(head_client);
else
c=c->next;
if (c!=*current_client) { /* prevent infinite recursion with dessktop */
if(c->vdesk != v) {
next(c, current_client, head_client, v);
} else {
point(c, 0, 0);
point(c, c->size.width-1, c->size.height-1);
jbwm_select_client(c, current_client);
}
}
}
static void cond_set_vdesk(struct JBWMClient * c,
struct JBWMClient * head_client,
struct JBWMScreen * s, const uint8_t desktop, const bool mod)
{
if (mod && c)
jbwm_set_client_vdesk(c, desktop);
else
jbwm_set_vdesk(s, head_client, desktop);
}
void jbwm_handle_key_event(struct JBWMScreen * s, struct JBWMClient * target,
struct JBWMClient ** head_client, struct JBWMClient ** current_client,
XKeyEvent * e)
{
enum { MOD = 1 << 0, ZERO = 1 << 1, VDESK_ROW_INCREMENT=16};
uint8_t flags;
const KeySym key = XLookupKeysym(e, 0);
flags = e->state & JBWM_KEYMASK_MOD ? MOD : 0;
JBWM_LOG("jbwm_handle_key_event mod: %d", flags);
switch (key) {
case JBWM_KEY_NEW:
jbwm_exec(JBWM_TERM);
break;
case JBWM_KEY_QUIT:
exit(0);
case JBWM_KEY_NEXT:
if(*current_client)
next(*current_client, current_client, head_client, s->vdesk);
break;
case JBWM_KEY_0:
flags |= ZERO;
/* FALLTHROUGH */
case JBWM_KEY_1:
case JBWM_KEY_2:
case JBWM_KEY_3:
case JBWM_KEY_4:
case JBWM_KEY_5:
case JBWM_KEY_6:
case JBWM_KEY_7:
case JBWM_KEY_8:
case JBWM_KEY_9:
/* First desktop 0, per wm-spec */
cond_set_vdesk(target, *head_client, s, flags & ZERO
? 10 : key - JBWM_KEY_1, flags & MOD);
break;
case JBWM_KEY_PREVDESK:
cond_set_vdesk(target, *head_client, s, s->vdesk - 1, flags & MOD);
break;
case JBWM_KEY_NEXTDESK:
cond_set_vdesk(target, *head_client, s, s->vdesk + 1, flags & MOD);
break;
case JBWM_KEY_VDESK_NEXT_ROW:
cond_set_vdesk(target, *head_client, s,
s->vdesk - VDESK_ROW_INCREMENT, flags & MOD);
break;
case JBWM_KEY_VDESK_PREV_ROW:
cond_set_vdesk(target, *head_client, s,
s->vdesk + VDESK_ROW_INCREMENT, flags & MOD);
break;
default:
if (!target)
target = *current_client;
if (!target)
target = *head_client;
if (target) {
handle_client_key_event(head_client, current_client, flags & MOD, key);
#ifdef DEBUG
} else {
JBWM_LOG("target is NULL");
#endif // DEBUG
}
}
}