-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore-window.c
376 lines (334 loc) · 9.96 KB
/
core-window.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
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
/*
* Copyright Neil Brown ©2023 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* Core per-window functionality.
*
* Provide a pane that is instantiated between the root and any window
* stack, to provide common functionality. These includes:
*
* - setting per-window attributes
* - registering and forwarding per-window notifications
* - Being an intermediary for per-window selections.
*
* ==============================================================
* Allow any pane to "claim ownership" of "the selection", or to
* "commit" the selection. A pane can also "discard" the selection,
* but that only works if the pane owns it.
*
* This can be used for mouse-based copy/paste and interaction with the
* X11 "PRIMARY" clipboard.
* When a selection is made in any pane it claims "the selection".
* When a mouse-based paste request is made, the receiving pane can ask for
* the selection to be "commited", and then access the most recent copy-buffer.
* The owner of a selection will, if the selection is still valid, call
* copy:save to save the selected content.
* When a "paste" request is made where the location is based on the "point"
* (current cursor) it is unlikely that a selection in the same pane should be
* used - if there is one it is more likely to be intended to receive the paste.
* So the target pane can first "discard" the selection, then "commit", then call
* "copy:get". If the selection is in this pane, the "discard" will succeed,
* the "commit" will be a no-op, and the top copy buf will be used.
* If the selection is in another pane (or another app via X11), the "discard"
* will fail (wrong owner), the "commit" will succeed and copy the selection,
* and the "copy:get" will get it.
*
* Operations are "selection:claim", "selection:commit" and "selection:discard".
* When the selection is claimed, the old owner gets called (not notified)
* "Notify:selection:claimed", and when a commit request is made,
* "Notify:selection:commit" is sent.
*
* A client can declare itself to be a fall-back handler by calling
* select:claim with num==1. Then if any other client discards its selection,
* the ownership reverse to the fallback. The fallback typically provides
* access to some selection external to edlib, such as the x11 selections.
*/
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#define PANE_DATA_TYPE struct window_data
#include "core.h"
#include "internal.h"
struct window_data {
struct pane *sel_owner;
int sel_committed;
struct pane *sel_owner_fallback;
};
#include "core-pane.h"
DEF_CMD(request_notify)
{
pane_add_notify(ci->focus, ci->home, ksuffix(ci, "Window:request:"));
return 1;
}
DEF_CMD(send_notify)
{
/* Window:notify:... */
return home_pane_notify(ci->home, ksuffix(ci, "Window:notify:"),
ci->focus,
ci->num, ci->mark, ci->str,
ci->num2, ci->mark2, ci->str2, ci->comm2);
}
DEF_CMD(window_set)
{
const char *val = ksuffix(ci, "Window:set:");
if (!*val)
val = ci->str2;
if (!val)
return Enoarg;
attr_set_str(&ci->home->attrs, val, ci->str);
return 1;
}
DEF_CMD(selection_claim)
{
struct window_data *wd = ci->home->data;
if (wd->sel_owner && wd->sel_owner != ci->focus) {
call("Notify:selection:claimed", wd->sel_owner);
//pane_drop_notifiers(ci->home, "Notify:Close", wd->sel_owner);
}
wd->sel_owner = ci->focus;
if (ci->num == 1)
wd->sel_owner_fallback = ci->focus;
wd->sel_committed = 0;
pane_add_notify(ci->home, ci->focus, "Notify:Close");
return 1;
}
DEF_CMD(selection_commit)
{
struct window_data *wd = ci->home->data;
if (wd->sel_owner && !wd->sel_committed) {
if (call("Notify:selection:commit", wd->sel_owner) != 2)
wd->sel_committed = 1;
}
return 1;
}
DEF_CMD(selection_discard)
{
struct window_data *wd = ci->home->data;
struct pane *op, *fp;
if (!wd->sel_owner)
return Efalse;
if (wd->sel_owner_fallback == ci->focus)
wd->sel_owner_fallback = NULL;
/* Don't require exactly same pane for sel_owner,
* but ensure they have the same focus.
*/
op = pane_focus(wd->sel_owner);
fp = pane_focus(ci->focus);
if (fp != op)
return Efalse;
wd->sel_owner = wd->sel_owner_fallback;
wd->sel_committed = 0;
return 1;
}
DEF_CMD(scale_image)
{
/* This is a helper for Draw:image which interprets str2
* with other values and calls comm2 with:
* "width" returns image width
* "height" returns image height
* "scale" num=new width, num2=new height
* "crop" x,y is top-left num,num2 - width,height
* These numbers apply after scaling.
* "draw" num,num2 = offset
* "cursor" x,y=pos, num,num2=size
*
* Inputs are:
* 'str2' container 'mode' information.
* By default the image is placed centrally in the pane
* and scaled to use either fully height or fully width.
* Various letters modify this:
* 'S' - stretch to use full height *and* full width
* 'L' - place on left if full width isn't used
* 'R' - place on right if full width isn't used
* 'T' - place at top if full height isn't used
* 'B' - place at bottom if full height isn't used.
*
* Also a suffix ":NNxNN" will be parse and the two numbers used
* to give number of rows and cols to overlay on the image for
* the purpose of cursor positioning. If these are present and
* p->cx,cy are not negative, draw a cursor at p->cx,cy highlighting
* the relevant cell.
*
* num,num2, if both positive, override the automatic scaling.
* The image is scaled to this many pixels.
* x,y is top-left pixel in the scaled image to start display at.
* Negative values allow a margin between pane edge and this image.
*/
struct pane *p = ci->focus;
const char *mode = ci->str2 ?: "";
bool stretch = strchr(mode, 'S');
int w, h;
int x = 0, y = 0;
int pw, ph;
int xo = 0, yo = 0;
int cix, ciy;
const char *pxl;
short px, py;
if (!ci->comm2)
return Enoarg;
pxl = pane_attr_get(p, "Display:pixels");
if (sscanf(pxl ?: "1x1", "%hdx%hx", &px, &py) != 2)
px = py = 1;
w = p->w * px;
h = p->h * py;
if (ci->num > 0 && ci->num2 > 0) {
w = ci->num;
h = ci->num2;
} else if (ci->num > 0) {
int iw = comm_call(ci->comm2, "width", p);
int ih = comm_call(ci->comm2, "height", p);
if (iw <= 0 || ih <= 0)
return Efail;
w = iw * ci->num / 1024;
h = ih * ci->num / 1024;
} else if (!stretch) {
int iw = comm_call(ci->comm2, "width", p);
int ih = comm_call(ci->comm2, "height", p);
if (iw <= 0 || ih <= 0)
return Efail;
if (iw * h > ih * w) {
/* Image is wider than space, use less height */
ih = ih * w / iw;
if (strchr(mode, 'B'))
/* bottom */
y = h - ih;
else if (!strchr(mode, 'T'))
/* center */
y = (h - ih) / 2;
/* Round up to pixels-per-cell */
h = ((ih + py - 1) / py) * py;
} else {
/* image is too tall, use less width */
iw = iw * h / ih;
if (strchr(mode, 'R'))
/* right */
x = w - iw;
else if (!strchr(mode, 'L'))
x = (w - iw) / 2;
w = ((iw + px - 1) / px) * px;
}
}
comm_call(ci->comm2, "scale", p, w, NULL, NULL, h);
pw = p->w * px;
ph = p->h * py;
cix = ci->x;
ciy = ci->y;
if (cix < 0) {
xo -= cix;
pw += cix;
cix = 0;
}
if (ciy < 0) {
yo -= ciy;
ph += ciy;
ciy = 0;
}
if (w - cix <= pw)
w -= cix;
else
w = pw;
if (h - ciy <= ph)
h -= ciy;
else
h = ph;
comm_call(ci->comm2, "crop", p, w, NULL, NULL, h, NULL, NULL, cix, ciy);
comm_call(ci->comm2, "draw", p, x + xo, NULL, NULL, y + yo);
if (p->cx >= 0) {
int rows, cols;
char *cl = strchr(mode, ':');
if (cl && sscanf(cl, ":%dx%d", &cols, &rows) == 2)
comm_call(ci->comm2, "cursor", p,
w/cols, NULL, NULL, h/rows, NULL, NULL,
p->cx + xo, p->cy + yo);
}
return 1;
}
DEF_CMD(window_activate_display)
{
/* Given a display attached to the root, integrate it
* into a full initial stack of panes.
* The display is the focus of this pane. This doc to
* attach there is the focus in the command.
*/
struct pane *disp = ci->home->focus;
struct pane *p, *p2;
bool display_added = False;
char *ip;
char *save, *t, *m;
if (!disp || !list_empty(&disp->children))
return Efail;
ip = pane_attr_get(disp, "window-initial-panes");
if (!ip)
return Efail;
ip = strdup(ip);
p = ci->home;
for (t = strtok_r(ip, " \t\n", &save);
t;
t = strtok_r(NULL, " \t\n", &save)) {
if (!*t)
continue;
if (strcmp(t, "DISPLAY") == 0) {
if (!display_added) {
pane_reparent(disp, p);
p = disp;
display_added = True;
}
} else {
m = strconcat(NULL, "attach-", t);
p2 = call_ret(pane, m, p);
free(m);
if (p2)
p = p2;
}
}
free(ip);
if (p && ci->focus != disp)
p = home_call_ret(pane, ci->focus, "doc:attach-view", p, 1);
if (p)
comm_call(ci->comm2, "cb", p);
return 1;
}
DEF_CMD(close_notify)
{
struct window_data *wd = ci->home->data;
if (wd->sel_owner_fallback == ci->focus)
wd->sel_owner_fallback = NULL;
if (wd->sel_owner == ci->focus)
wd->sel_owner = wd->sel_owner_fallback;
return 1;
}
static struct map *window_map;
DEF_LOOKUP_CMD(window_handle, window_map);
DEF_CMD(window_attach)
{
struct pane *p;
p = pane_register(pane_root(ci->focus), 0, &window_handle.c);
if (!p)
return Efail;
comm_call(ci->comm2, "cb", p);
return 1;
}
DEF_CMD(window_close)
{
pane_close(ci->home);
return 1;
}
void window_setup(struct pane *ed safe)
{
window_map = key_alloc();
key_add_prefix(window_map, "Window:request:", &request_notify);
key_add_prefix(window_map, "Window:notify:", &send_notify);
key_add(window_map, "Window:close", &window_close);
key_add_prefix(window_map, "Window:set:", &window_set);
key_add(window_map, "selection:claim", &selection_claim);
key_add(window_map, "selection:commit", &selection_commit);
key_add(window_map, "selection:discard", &selection_discard);
key_add(window_map, "Notify:Close", &close_notify);
key_add(window_map, "Draw:scale-image", &scale_image);
key_add(window_map, "Window:activate-display",
&window_activate_display);
call_comm("global-set-command", ed, &window_attach, 0, NULL,
"attach-window-core");
}