-
Notifications
You must be signed in to change notification settings - Fork 11
/
actions.c
5896 lines (4907 loc) · 142 KB
/
actions.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2000, 2001, 2002, 2003, 2004 Shawn Betts <sabetts@vcn.bc.ca>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* 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., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include <unistd.h>
#include <ctype.h>
#include <sys/wait.h>
#include <X11/keysym.h>
#include <string.h>
#include <strings.h>
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <limits.h>
#include <X11/Xproto.h>
#include <X11/extensions/XTest.h>
#include <sys/ioctl.h>
#include "sdorfehs.h"
/* arg_REST and arg_SHELLCMD eat the rest of the input. */
enum argtype {
arg_REST,
arg_NUMBER,
arg_STRING,
arg_FRAME,
arg_WINDOW,
arg_COMMAND,
arg_SHELLCMD,
arg_KEYMAP,
arg_KEY,
arg_GRAVITY,
arg_VSCREEN,
arg_HOOK,
arg_VARIABLE,
arg_RAW,
};
union arg_union {
rp_frame *frame;
int number;
float fnumber;
rp_window *win;
rp_keymap *keymap;
rp_vscreen *vscreen;
struct list_head *hook;
struct set_var *variable;
struct rp_key *key;
int gravity;
};
struct cmdarg {
int type;
char *string;
union arg_union arg;
struct list_head node;
};
#define ARG_STRING(elt) args[elt]->string
#define ARG(elt, type) args[elt]->arg.type
struct argspec {
int type;
char *prompt;
};
struct set_var {
char *var;
cmdret *(*set_fn)(struct cmdarg **);
int nargs;
struct argspec *args;
struct list_head node;
};
struct user_command {
char *name;
cmdret *(*func)(int, struct cmdarg **);
struct argspec *args;
int num_args;
/*
* The number of required arguments. Any arguments after that are
* optional and won't be filled in when called interactively.
* ni_required_args is used when called non-interactively,
* i_required_args when called interactively.
*/
int ni_required_args, i_required_args;
struct list_head node;
};
typedef struct {
char *name;
char *alias;
} alias_t;
typedef struct rp_frame_undo {
char *frames;
rp_vscreen *vscreen;
struct list_head node;
} rp_frame_undo;
static LIST_HEAD(user_commands);
static LIST_HEAD(rp_keymaps);
static LIST_HEAD(set_vars);
static LIST_HEAD(rp_frame_undos);
static LIST_HEAD(rp_frame_redos);
static alias_t *alias_list;
static int alias_list_size;
static int alias_list_last;
static const char invalid_negative_arg[] = "invalid negative argument";
/* setter function prototypes */
static cmdret *set_barborder(struct cmdarg **args);
static cmdret *set_barbordercolor(struct cmdarg **args);
static cmdret *set_bargravity(struct cmdarg **args);
static cmdret *set_barinpadding(struct cmdarg **args);
static cmdret *set_barpadding(struct cmdarg **args);
static cmdret *set_barsticky(struct cmdarg **args);
static cmdret *set_bgcolor(struct cmdarg **args);
static cmdret *set_border(struct cmdarg **args);
static cmdret *set_bwcolor(struct cmdarg **args);
static cmdret *set_fgcolor(struct cmdarg **args);
static cmdret *set_font(struct cmdarg **args);
static cmdret *set_framefmt(struct cmdarg **args);
static cmdret *set_framemsgwait(struct cmdarg **args);
static cmdret *set_framesels(struct cmdarg **args);
static cmdret *set_fwcolor(struct cmdarg **args);
static cmdret *set_gap(struct cmdarg **args);
static cmdret *set_historysize(struct cmdarg **args);
static cmdret *set_ignoreresizehints(struct cmdarg **args);
static cmdret *set_infofmt(struct cmdarg **args);
static cmdret *set_inputwidth(struct cmdarg **args);
static cmdret *set_maxsizegravity(struct cmdarg **args);
static cmdret *set_maxundos(struct cmdarg **args);
static cmdret *set_msgwait(struct cmdarg **args);
static cmdret *set_onlyborder(struct cmdarg **args);
static cmdret *set_padding(struct cmdarg **args);
static cmdret *set_resizefmt(struct cmdarg **args);
static cmdret *set_resizeunit(struct cmdarg **args);
static cmdret *set_rudeness(struct cmdarg **args);
static cmdret *set_startupmessage(struct cmdarg **args);
static cmdret *set_stickyfmt(struct cmdarg **args);
static cmdret *set_topkmap(struct cmdarg **args);
static cmdret *set_transgravity(struct cmdarg **args);
static cmdret *set_vscreens(struct cmdarg **args);
static cmdret *set_waitcursor(struct cmdarg **args);
static cmdret *set_warp(struct cmdarg **args);
static cmdret *set_winfmt(struct cmdarg **args);
static cmdret *set_wingravity(struct cmdarg **args);
static cmdret *set_winliststyle(struct cmdarg **args);
static cmdret *set_winname(struct cmdarg **args);
static cmdret *set_winaddcurvscreen(struct cmdarg **args);
/* command function prototypes. */
static cmdret *cmd_abort(int interactive, struct cmdarg **args);
static cmdret *cmd_addhook(int interactive, struct cmdarg **args);
static cmdret *cmd_alias(int interactive, struct cmdarg **args);
static cmdret *cmd_banish(int interactive, struct cmdarg **args);
static cmdret *cmd_banishrel(int interactive, struct cmdarg **args);
static cmdret *cmd_chdir(int interactive, struct cmdarg **args);
static cmdret *cmd_clrunmanaged(int interactive, struct cmdarg **args);
static cmdret *cmd_cnext(int interactive, struct cmdarg **args);
static cmdret *cmd_colon(int interactive, struct cmdarg **args);
static cmdret *cmd_commands(int interactive, struct cmdarg **args);
static cmdret *cmd_cother(int interactive, struct cmdarg **args);
static cmdret *cmd_cprev(int interactive, struct cmdarg **args);
static cmdret *cmd_curframe(int interactive, struct cmdarg **args);
static cmdret *cmd_dedicate(int interactive, struct cmdarg **args);
static cmdret *cmd_definekey(int interactive, struct cmdarg **args);
static cmdret *cmd_delete(int interactive, struct cmdarg **args);
static cmdret *cmd_delkmap(int interactive, struct cmdarg **args);
static cmdret *cmd_describekey(int interactive, struct cmdarg **args);
static cmdret *cmd_echo(int interactive, struct cmdarg **args);
static cmdret *cmd_escape(int interactive, struct cmdarg **args);
static cmdret *cmd_exchangedown(int interactive, struct cmdarg **args);
static cmdret *cmd_exchangeleft(int interactive, struct cmdarg **args);
static cmdret *cmd_exchangeright(int interactive, struct cmdarg **args);
static cmdret *cmd_exchangeup(int interactive, struct cmdarg **args);
static cmdret *cmd_exec(int interactive, struct cmdarg **args);
static cmdret *cmd_execa(int interactive, struct cmdarg **args);
static cmdret *cmd_execf(int interactive, struct cmdarg **args);
static cmdret *cmd_execv(int interactive, struct cmdarg **args);
static cmdret *cmd_execw(int interactive, struct cmdarg **args);
static cmdret *cmd_fdump(int interactive, struct cmdarg **args);
static cmdret *cmd_focusdown(int interactive, struct cmdarg **args);
static cmdret *cmd_focuslast(int interactive, struct cmdarg **args);
static cmdret *cmd_focusleft(int interactive, struct cmdarg **args);
static cmdret *cmd_focusright(int interactive, struct cmdarg **args);
static cmdret *cmd_focusup(int interactive, struct cmdarg **args);
static cmdret *cmd_frestore(int interactive, struct cmdarg **args);
static cmdret *cmd_fselect(int interactive, struct cmdarg **args);
static cmdret *cmd_getenv(int interactive, struct cmdarg **args);
static cmdret *cmd_getsel(int interactive, struct cmdarg **args);
static cmdret *cmd_gravity(int interactive, struct cmdarg **args);
static cmdret *cmd_hsplit(int interactive, struct cmdarg **args);
static cmdret *cmd_help(int interactive, struct cmdarg **args);
static cmdret *cmd_inext(int interactive, struct cmdarg **args);
static cmdret *cmd_info(int interactive, struct cmdarg **args);
static cmdret *cmd_iother(int interactive, struct cmdarg **args);
static cmdret *cmd_iprev(int interactive, struct cmdarg **args);
static cmdret *cmd_kill(int interactive, struct cmdarg **args);
static cmdret *cmd_lastmsg(int interactive, struct cmdarg **args);
static cmdret *cmd_link(int interactive, struct cmdarg **args);
static cmdret *cmd_listhook(int interactive, struct cmdarg **args);
static cmdret *cmd_meta(int interactive, struct cmdarg **args);
static cmdret *cmd_newkmap(int interactive, struct cmdarg **args);
static cmdret *cmd_next(int interactive, struct cmdarg **args);
static cmdret *cmd_nextframe(int interactive, struct cmdarg **args);
static cmdret *cmd_nextscreen(int interactive, struct cmdarg **args);
static cmdret *cmd_number(int interactive, struct cmdarg **args);
static cmdret *cmd_only(int interactive, struct cmdarg **args);
static cmdret *cmd_other(int interactive, struct cmdarg **args);
static cmdret *cmd_prev(int interactive, struct cmdarg **args);
static cmdret *cmd_prevframe(int interactive, struct cmdarg **args);
static cmdret *cmd_prevscreen(int interactive, struct cmdarg **args);
static cmdret *cmd_prompt(int interactive, struct cmdarg **args);
static cmdret *cmd_putsel(int interactive, struct cmdarg **args);
static cmdret *cmd_quit(int interactive, struct cmdarg **args);
static cmdret *cmd_ratclick(int interactive, struct cmdarg **args);
static cmdret *cmd_rathold(int interactive, struct cmdarg **args);
static cmdret *cmd_ratinfo(int interactive, struct cmdarg **args);
static cmdret *cmd_ratrelinfo(int interactive, struct cmdarg **args);
static cmdret *cmd_ratrelwarp(int interactive, struct cmdarg **args);
static cmdret *cmd_ratwarp(int interactive, struct cmdarg **args);
static cmdret *cmd_readkey(int interactive, struct cmdarg **args);
static cmdret *cmd_redisplay(int interactive, struct cmdarg **args);
static cmdret *cmd_redo(int interactive, struct cmdarg **args);
static cmdret *cmd_remhook(int interactive, struct cmdarg **args);
static cmdret *cmd_remove(int interactive, struct cmdarg **args);
static cmdret *cmd_rename(int interactive, struct cmdarg **args);
static cmdret *cmd_resize(int interactive, struct cmdarg **args);
static cmdret *cmd_restart(int interactive, struct cmdarg **args);
static cmdret *cmd_sdump(int interactive, struct cmdarg **args);
static cmdret *cmd_select(int interactive, struct cmdarg **args);
static cmdret *cmd_set(int interactive, struct cmdarg **args);
static cmdret *cmd_setenv(int interactive, struct cmdarg **args);
static cmdret *cmd_sfdump(int interactive, struct cmdarg **args);
static cmdret *cmd_sfrestore(int interactive, struct cmdarg **args);
static cmdret *cmd_shrink(int interactive, struct cmdarg **args);
static cmdret *cmd_smove(int interactive, struct cmdarg **args);
static cmdret *cmd_source(int interactive, struct cmdarg **args);
static cmdret *cmd_sselect(int interactive, struct cmdarg **args);
static cmdret *cmd_stick(int interactive, struct cmdarg **args);
static cmdret *cmd_swap(int interactive, struct cmdarg **args);
static cmdret *cmd_unalias(int interactive, struct cmdarg **args);
static cmdret *cmd_undefinekey(int interactive, struct cmdarg **args);
static cmdret *cmd_undo(int interactive, struct cmdarg **args);
static cmdret *cmd_unmanage(int interactive, struct cmdarg **args);
static cmdret *cmd_unsetenv(int interactive, struct cmdarg **args);
static cmdret *cmd_unstick(int interactive, struct cmdarg **args);
static cmdret *cmd_vsplit(int interactive, struct cmdarg **args);
static cmdret *cmd_verbexec(int interactive, struct cmdarg **args);
static cmdret *cmd_version(int interactive, struct cmdarg **args);
static cmdret *cmd_vmove(int interactive, struct cmdarg **args);
static cmdret *cmd_vnext(int interactive, struct cmdarg **args);
static cmdret *cmd_vother(int interactive, struct cmdarg **args);
static cmdret *cmd_vprev(int interactive, struct cmdarg **args);
static cmdret *cmd_vrename(int interactive, struct cmdarg **args);
static cmdret *cmd_vscreens(int interactive, struct cmdarg **args);
static cmdret *cmd_vselect(int interactive, struct cmdarg **args);
static cmdret *cmd_windows(int interactive, struct cmdarg **args);
static void
add_set_var(char *name, cmdret *(*fn)(struct cmdarg **), int nargs, ...)
{
int i = 0;
struct set_var *var;
va_list va;
var = xmalloc(sizeof(struct set_var));
var->var = name;
var->set_fn = fn;
var->nargs = nargs;
var->args = xmalloc(sizeof(struct argspec) * nargs);
/* Fill var->args */
va_start(va, nargs);
for (i = 0; i < nargs; i++) {
var->args[i].prompt = va_arg(va, char *);
var->args[i].type = va_arg(va, int);
}
va_end(va);
list_add_tail(&var->node, &set_vars);
}
static void
set_var_free(struct set_var *var)
{
if (var == NULL)
return;
free(var->args);
free(var);
}
static void
init_set_vars(void)
{
/* Keep this sorted alphabetically. */
add_set_var("barborder", set_barborder, 1, "", arg_NUMBER);
add_set_var("barbordercolor", set_barbordercolor, 1, "", arg_STRING);
add_set_var("bargravity", set_bargravity, 1, "", arg_GRAVITY);
add_set_var("barinpadding", set_barinpadding, 1, "", arg_NUMBER);
add_set_var("barpadding", set_barpadding, 2, "", arg_NUMBER, "",
arg_NUMBER);
add_set_var("barsticky", set_barsticky, 1, "", arg_NUMBER);
add_set_var("bgcolor", set_bgcolor, 1, "", arg_STRING);
add_set_var("border", set_border, 1, "", arg_NUMBER);
add_set_var("bwcolor", set_bwcolor, 1, "", arg_STRING);
add_set_var("fgcolor", set_fgcolor, 1, "", arg_STRING);
add_set_var("font", set_font, 1, "", arg_STRING);
add_set_var("framefmt", set_framefmt, 1, "", arg_REST);
add_set_var("framemsgwait", set_framemsgwait, 1, "", arg_NUMBER);
add_set_var("framesels", set_framesels, 1, "", arg_STRING);
add_set_var("fwcolor", set_fwcolor, 1, "", arg_STRING);
add_set_var("gap", set_gap, 1, "", arg_NUMBER);
add_set_var("historysize", set_historysize, 1, "", arg_NUMBER);
add_set_var("ignoreresizehints", set_ignoreresizehints, 1, "",
arg_NUMBER);
add_set_var("infofmt", set_infofmt, 1, "", arg_REST);
add_set_var("inputwidth", set_inputwidth, 1, "", arg_NUMBER);
add_set_var("maxsizegravity", set_maxsizegravity, 1, "", arg_GRAVITY);
add_set_var("maxundos", set_maxundos, 1, "", arg_NUMBER);
add_set_var("msgwait", set_msgwait, 1, "", arg_NUMBER);
add_set_var("onlyborder", set_onlyborder, 1, "", arg_NUMBER);
add_set_var("padding", set_padding, 4, "", arg_NUMBER, "", arg_NUMBER,
"", arg_NUMBER, "", arg_NUMBER);
add_set_var("resizefmt", set_resizefmt, 1, "", arg_REST);
add_set_var("resizeunit", set_resizeunit, 1, "", arg_NUMBER);
add_set_var("rudeness", set_rudeness, 1, "", arg_NUMBER);
add_set_var("startupmessage", set_startupmessage, 1, "", arg_NUMBER);
add_set_var("stickyfmt", set_stickyfmt, 1, "", arg_REST);
add_set_var("topkmap", set_topkmap, 1, "", arg_STRING);
add_set_var("transgravity", set_transgravity, 1, "", arg_GRAVITY);
add_set_var("vscreens", set_vscreens, 1, "", arg_NUMBER);
add_set_var("waitcursor", set_waitcursor, 1, "", arg_NUMBER);
add_set_var("warp", set_warp, 1, "", arg_NUMBER);
add_set_var("winfmt", set_winfmt, 1, "", arg_REST);
add_set_var("wingravity", set_wingravity, 1, "", arg_GRAVITY);
add_set_var("winliststyle", set_winliststyle, 1, "", arg_STRING);
add_set_var("winname", set_winname, 1, "", arg_STRING);
add_set_var("winaddcurvscreen", set_winaddcurvscreen, 1, "", arg_NUMBER);
}
/*
* i_nrequired is the number required when called interactively. ni_nrequired
* is when called non-interactively.
*/
static void
add_command(char *name, cmdret *(*fn)(int, struct cmdarg **), int nargs,
int i_nrequired, int ni_nrequired, ...)
{
int i = 0;
struct user_command *cmd;
va_list va;
cmd = xmalloc(sizeof(struct user_command));
cmd->name = name;
cmd->func = fn;
cmd->num_args = nargs;
cmd->ni_required_args = ni_nrequired;
cmd->i_required_args = i_nrequired;
cmd->args = nargs ? xmalloc(nargs * sizeof(struct argspec)) : NULL;
/* Fill cmd->args */
va_start(va, ni_nrequired);
for (i = 0; i < nargs; i++) {
cmd->args[i].prompt = va_arg(va, char *);
cmd->args[i].type = va_arg(va, int);
}
va_end(va);
list_add(&cmd->node, &user_commands);
}
static void
user_command_free(struct user_command *cmd)
{
if (cmd == NULL)
return;
free(cmd->args);
free(cmd);
}
void
init_user_commands(void)
{
/* @begin (tag required for genrpbindings) */
add_command("abort", cmd_abort, 0, 0, 0);
add_command("addhook", cmd_addhook, 2, 2, 2,
"Hook: ", arg_HOOK,
"Command: ", arg_COMMAND);
add_command("alias", cmd_alias, 2, 2, 2,
"Alias: ", arg_STRING,
"Command: ", arg_COMMAND);
add_command("banish", cmd_banish, 0, 0, 0);
add_command("chdir", cmd_chdir, 1, 0, 0,
"Dir: ", arg_REST);
add_command("clrunmanaged", cmd_clrunmanaged, 0, 0, 0);
add_command("cnext", cmd_cnext, 0, 0, 0);
add_command("colon", cmd_colon, 1, 0, 0,
"", arg_REST);
add_command("commands", cmd_commands, 0, 0, 0);
add_command("cother", cmd_cother, 0, 0, 0);
add_command("cprev", cmd_cprev, 0, 0, 0);
add_command("curframe", cmd_curframe, 0, 0, 0);
add_command("dedicate", cmd_dedicate, 1, 0, 0,
"", arg_NUMBER);
add_command("definekey", cmd_definekey, 3, 3, 3,
"Keymap: ", arg_KEYMAP,
"Key: ", arg_KEY,
"Command: ", arg_COMMAND);
add_command("delete", cmd_delete, 0, 0, 0);
add_command("delkmap", cmd_delkmap, 1, 1, 1,
"Keymap: ", arg_KEYMAP);
add_command("describekey", cmd_describekey, 1, 1, 1,
"Keymap: ", arg_KEYMAP);
add_command("echo", cmd_echo, 1, 1, 1,
"Echo: ", arg_RAW);
add_command("escape", cmd_escape, 1, 1, 1,
"Key: ", arg_KEY);
add_command("exchangedown", cmd_exchangedown, 0, 0, 0);
add_command("exchangeleft", cmd_exchangeleft, 0, 0, 0);
add_command("exchangeright", cmd_exchangeright, 0, 0, 0);
add_command("exchangeup", cmd_exchangeup, 0, 0, 0);
add_command("exec", cmd_exec, 1, 1, 1,
"/bin/sh -c ", arg_SHELLCMD);
add_command("execa", cmd_execa, 1, 1, 1,
"/bin/sh -c ", arg_SHELLCMD);
add_command("execf", cmd_execf, 2, 2, 2,
"frame to execute in:", arg_FRAME,
"/bin/sh -c ", arg_SHELLCMD);
add_command("execv", cmd_execv, 2, 2, 2,
"vscreen to execute in:", arg_VSCREEN,
"/bin/sh -c ", arg_SHELLCMD);
add_command("execw", cmd_execw, 1, 1, 1,
"/bin/sh -c ", arg_SHELLCMD);
add_command("fdump", cmd_fdump, 1, 0, 0,
"", arg_NUMBER);
add_command("focus", cmd_nextframe, 0, 0, 0);
add_command("focusdown", cmd_focusdown, 0, 0, 0);
add_command("focusprev", cmd_prevframe, 0, 0, 0);
add_command("focuslast", cmd_focuslast, 0, 0, 0);
add_command("focusleft", cmd_focusleft, 0, 0, 0);
add_command("focusright", cmd_focusright, 0, 0, 0);
add_command("focusup", cmd_focusup, 0, 0, 0);
add_command("frestore", cmd_frestore, 1, 1, 1,
"Frames: ", arg_REST);
add_command("fselect", cmd_fselect, 1, 1, 1,
"", arg_FRAME);
add_command("getenv", cmd_getenv, 1, 1, 1,
"Variable: ", arg_STRING);
add_command("getsel", cmd_getsel, 0, 0, 0);
add_command("gravity", cmd_gravity, 1, 0, 0,
"Gravity: ", arg_GRAVITY);
add_command("help", cmd_help, 1, 0, 0,
"Keymap: ", arg_KEYMAP);
add_command("hsplit", cmd_hsplit, 1, 0, 0,
"Split: ", arg_STRING);
add_command("inext", cmd_inext, 0, 0, 0);
add_command("info", cmd_info, 1, 0, 0,
"Format: ", arg_REST);
add_command("iother", cmd_iother, 0, 0, 0);
add_command("iprev", cmd_iprev, 0, 0, 0);
add_command("kill", cmd_kill, 0, 0, 0);
add_command("lastmsg", cmd_lastmsg, 0, 0, 0);
add_command("link", cmd_link, 2, 1, 1,
"Key: ", arg_STRING,
"Keymap: ", arg_KEYMAP);
add_command("listhook", cmd_listhook, 1, 1, 1,
"Hook: ", arg_HOOK);
add_command("meta", cmd_meta, 1, 0, 0,
"key: ", arg_KEY);
add_command("newkmap", cmd_newkmap, 1, 1, 1,
"Keymap: ", arg_STRING);
add_command("next", cmd_next, 0, 0, 0);
add_command("nextscreen", cmd_nextscreen, 0, 0, 0);
add_command("number", cmd_number, 2, 1, 1,
"Number: ", arg_NUMBER,
"Number: ", arg_NUMBER);
add_command("only", cmd_only, 0, 0, 0);
add_command("other", cmd_other, 0, 0, 0);
add_command("prev", cmd_prev, 0, 0, 0);
add_command("prevscreen", cmd_prevscreen, 0, 0, 0);
add_command("prompt", cmd_prompt, 1, 0, 0,
"", arg_REST);
add_command("putsel", cmd_putsel, 1, 1, 1,
"Text: ", arg_RAW);
add_command("quit", cmd_quit, 0, 0, 0);
add_command("ratinfo", cmd_ratinfo, 0, 0, 0);
add_command("ratrelinfo", cmd_ratrelinfo, 0, 0, 0);
add_command("banishrel", cmd_banishrel, 0, 0, 0);
add_command("ratwarp", cmd_ratwarp, 2, 2, 2,
"X: ", arg_NUMBER,
"Y: ", arg_NUMBER);
add_command("ratrelwarp", cmd_ratrelwarp, 2, 2, 2,
"X: ", arg_NUMBER,
"Y: ", arg_NUMBER);
add_command("ratclick", cmd_ratclick, 1, 0, 0,
"Button: ", arg_NUMBER);
add_command("rathold", cmd_rathold, 2, 1, 1,
"State: ", arg_STRING,
"Button: ", arg_NUMBER);
add_command("readkey", cmd_readkey, 1, 1, 1,
"Keymap: ", arg_KEYMAP);
add_command("redisplay", cmd_redisplay, 0, 0, 0);
add_command("redo", cmd_redo, 0, 0, 0);
add_command("remhook", cmd_remhook, 2, 2, 2,
"Hook: ", arg_HOOK,
"Command: ", arg_COMMAND);
add_command("remove", cmd_remove, 0, 0, 0);
add_command("resize", cmd_resize, 2, 0, 2,
"", arg_NUMBER,
"", arg_NUMBER);
add_command("restart", cmd_restart, 0, 0, 0);
add_command("sdump", cmd_sdump, 0, 0, 0);
add_command("select", cmd_select, 1, 0, 1,
"Select window: ", arg_REST);
add_command("set", cmd_set, 2, 0, 0,
"", arg_VARIABLE,
"", arg_REST);
add_command("setenv", cmd_setenv, 2, 2, 2,
"Variable: ", arg_STRING,
"Value: ", arg_REST);
add_command("sfdump", cmd_sfdump, 0, 0, 0);
add_command("sfrestore", cmd_sfrestore, 1, 1, 1,
"Frames: ", arg_REST);
add_command("shrink", cmd_shrink, 0, 0, 0);
add_command("source", cmd_source, 1, 1, 1,
"File: ", arg_REST);
add_command("smove", cmd_smove, 1, 1, 1,
"Screen: ", arg_NUMBER);
add_command("sselect", cmd_sselect, 1, 1, 1,
"Screen: ", arg_NUMBER);
add_command("stick", cmd_stick, 0, 0, 0);
add_command("swap", cmd_swap, 2, 1, 1,
"destination frame: ", arg_FRAME,
"source frame: ", arg_FRAME);
add_command("title", cmd_rename, 1, 1, 1,
"Set window's title to: ", arg_REST);
add_command("unalias", cmd_unalias, 1, 1, 1,
"Alias: ", arg_STRING);
add_command("undefinekey", cmd_undefinekey, 2, 2, 2,
"Keymap: ", arg_KEYMAP,
"Key: ", arg_KEY);
add_command("undo", cmd_undo, 0, 0, 0);
add_command("unmanage", cmd_unmanage, 1, 1, 0,
"Unmanage: ", arg_REST);
add_command("unsetenv", cmd_unsetenv, 1, 1, 1,
"Variable: ", arg_STRING);
add_command("unstick", cmd_unstick, 0, 0, 0);
add_command("verbexec", cmd_verbexec, 1, 1, 1,
"/bin/sh -c ", arg_SHELLCMD);
add_command("version", cmd_version, 0, 0, 0);
add_command("vmove", cmd_vmove, 1, 1, 1,
"Virtual Screen: ", arg_VSCREEN);
add_command("vnext", cmd_vnext, 0, 0, 0);
add_command("vother", cmd_vother, 0, 0, 0);
add_command("vprev", cmd_vprev, 0, 0, 0);
add_command("vrename", cmd_vrename, 1, 1, 1,
"Change virtual screen name to: ", arg_REST);
add_command("vscreens", cmd_vscreens, 0, 0, 0);
add_command("vselect", cmd_vselect, 1, 1, 1,
"Virtual Screen: ", arg_VSCREEN);
add_command("vsplit", cmd_vsplit, 1, 0, 0,
"Split: ", arg_STRING);
add_command("windows", cmd_windows, 1, 0, 0,
"", arg_REST);
/* @end (tag required for genrpbindings) */
init_set_vars();
}
/* Delete all entries in the redo list. */
static void
clear_frame_redos(void)
{
rp_frame_undo *cur;
struct list_head *tmp, *iter;
list_for_each_safe_entry(cur, iter, tmp, &rp_frame_redos, node) {
free(cur->frames);
list_del(&(cur->node));
}
}
static void
del_frame_undo(rp_frame_undo *u)
{
if (!u)
return;
free(u->frames);
list_del(&(u->node));
free(u);
}
void
clear_frame_undos(void)
{
while (list_size(&rp_frame_undos) > 0) {
/* Delete the oldest node */
rp_frame_undo *cur;
list_last(cur, &rp_frame_undos, node);
del_frame_undo(cur);
}
}
static void
push_frame_undo(rp_vscreen *vscreen)
{
rp_frame_undo *cur;
if (list_size(&rp_frame_undos) > defaults.maxundos) {
/* Delete the oldest node */
list_last(cur, &rp_frame_undos, node);
del_frame_undo(cur);
}
cur = xmalloc(sizeof(rp_frame_undo));
cur->frames = fdump(vscreen);
cur->vscreen = vscreen;
list_add(&cur->node, &rp_frame_undos);
/*
* Since we're creating new frames the redo list is now invalid, so
* clear it.
*/
clear_frame_redos();
}
static rp_frame_undo *
pop_frame_list(struct list_head *undo_list, struct list_head *redo_list)
{
rp_vscreen *vscreen = rp_current_vscreen;
rp_frame_undo *first, *new;
/* Is there something to restore? */
list_first(first, undo_list, node);
if (!first)
return NULL;
/* First save the current layout into undo */
new = xmalloc(sizeof(rp_frame_undo));
new->frames = fdump(vscreen);
new->vscreen = vscreen;
list_add(&new->node, redo_list);
list_del(&(first->node));
return first;
}
/* Pop the head of the frame undo list off and put it in the redo list. */
static rp_frame_undo *
pop_frame_undo(void)
{
return pop_frame_list(&rp_frame_undos, &rp_frame_redos);
}
/* Pop the head of the frame redo list off and put it in the undo list. */
static rp_frame_undo *
pop_frame_redo(void)
{
return pop_frame_list(&rp_frame_redos, &rp_frame_undos);
}
rp_action *
find_keybinding_by_action(char *action, rp_keymap *map)
{
int i;
for (i = 0; i < map->actions_last; i++) {
if (!strcmp(map->actions[i].data, action)) {
return &map->actions[i];
}
}
return NULL;
}
rp_action *
find_keybinding(KeySym keysym, unsigned int state, rp_keymap *map)
{
int i;
for (i = 0; i < map->actions_last; i++) {
if (map->actions[i].key == keysym &&
map->actions[i].state == state)
return &map->actions[i];
}
return NULL;
}
static char *
find_command_by_keydesc(char *desc, rp_keymap *map)
{
int i = 0;
char *keysym_name;
while (i < map->actions_last) {
keysym_name = keysym_to_string(map->actions[i].key,
map->actions[i].state);
if (!strcmp(keysym_name, desc)) {
free(keysym_name);
return map->actions[i].data;
}
free(keysym_name);
i++;
}
return NULL;
}
static char *
resolve_command_from_keydesc(char *desc, int depth, rp_keymap *map)
{
char *cmd, *c;
c = find_command_by_keydesc(desc, map);
if (!c)
return NULL;
/* is it a link? */
if (strncmp(c, "link", 4) || depth > MAX_LINK_DEPTH)
/* it is not */
return c;
cmd = resolve_command_from_keydesc(&c[5], depth + 1, map);
return (cmd != NULL) ? cmd : c;
}
static void
add_keybinding(KeySym keysym, int state, char *cmd, rp_keymap *map)
{
if (map->actions_last >= map->actions_size) {
/* double the key table size */
map->actions_size *= 2;
map->actions = xrealloc(map->actions,
sizeof(rp_action) * map->actions_size);
PRINT_DEBUG(("realloc()ed key_table %d\n", map->actions_size));
}
map->actions[map->actions_last].key = keysym;
map->actions[map->actions_last].state = state;
/* free this on shutdown, or re/unbinding */
map->actions[map->actions_last].data = xstrdup(cmd);
map->actions_last++;
}
static void
replace_keybinding(rp_action * key_action, char *newcmd)
{
free(key_action->data);
key_action->data = xstrdup(newcmd);
}
static int
remove_keybinding(KeySym keysym, unsigned int state, rp_keymap *map)
{
int i;
int found = -1;
for (i = 0; i < map->actions_last; i++) {
if (map->actions[i].key == keysym &&
map->actions[i].state == state) {
found = i;
break;
}
}
if (found >= 0) {
free(map->actions[found].data);
memmove(&map->actions[found], &map->actions[found + 1],
sizeof(rp_action) * (map->actions_last - found - 1));
map->actions_last--;
return 1;
}
return 0;
}
static rp_keymap *
keymap_new(char *name)
{
rp_keymap *map;
/* All keymaps must have a name. */
if (name == NULL)
return NULL;
map = xmalloc(sizeof(rp_keymap));
map->name = xstrdup(name);
map->actions_size = 1;
map->actions = xmalloc(sizeof(rp_action) * map->actions_size);
map->actions_last = 0;
return map;
}
rp_keymap *
find_keymap(char *name)
{
rp_keymap *cur;
list_for_each_entry(cur, &rp_keymaps, node) {
if (!strcmp(name, cur->name)) {
return cur;
}
}
return NULL;
}
/*
* Search the alias table for a match. If a match is found, return its index
* into the table. Otherwise return -1.
*/
static int
find_alias_index(char *name)
{
int i;
for (i = 0; i < alias_list_last; i++)
if (!strcmp(name, alias_list[i].name))
return i;
return -1;
}
static void
add_alias(char *name, char *alias)
{
int i;
/* Are we updating an existing alias, or creating a new one? */
i = find_alias_index(name);
if (i >= 0) {
free(alias_list[i].alias);
alias_list[i].alias = xstrdup(alias);
} else {
if (alias_list_last >= alias_list_size) {
alias_list_size *= 2;
alias_list = xrealloc(alias_list,
sizeof(alias_t) * alias_list_size);
}
alias_list[alias_list_last].name = xstrdup(name);
alias_list[alias_list_last].alias = xstrdup(alias);
alias_list_last++;
}
}
void
initialize_default_keybindings(void)
{
rp_keymap *map, *top;
map = keymap_new(ROOT_KEYMAP);
list_add(&map->node, &rp_keymaps);
top = keymap_new(defaults.top_kmap);
list_add(&top->node, &rp_keymaps);
/* Initialize the alias list. */
alias_list_size = 5;
alias_list_last = 0;
alias_list = xmalloc(sizeof(alias_t) * alias_list_size);
prefix_key.sym = KEY_PREFIX;
prefix_key.state = MODIFIER_PREFIX;
/* Add the prefix key to the top-level map. */
add_keybinding(prefix_key.sym, prefix_key.state, "readkey " ROOT_KEYMAP,
top);
add_keybinding(prefix_key.sym, prefix_key.state, "other", map);
add_keybinding(prefix_key.sym, 0, "meta", map);
add_keybinding(XK_g, RP_CONTROL_MASK, "abort", map);
add_keybinding(XK_0, 0, "select 0", map);
add_keybinding(XK_1, 0, "select 1", map);
add_keybinding(XK_2, 0, "select 2", map);
add_keybinding(XK_3, 0, "select 3", map);
add_keybinding(XK_4, 0, "select 4", map);
add_keybinding(XK_5, 0, "select 5", map);
add_keybinding(XK_6, 0, "select 6", map);
add_keybinding(XK_7, 0, "select 7", map);
add_keybinding(XK_8, 0, "select 8", map);
add_keybinding(XK_9, 0, "select 9", map);
add_keybinding(XK_minus, 0, "select -", map);
add_keybinding(XK_A, 0, "title", map);
add_keybinding(XK_A, RP_CONTROL_MASK, "title", map);
add_keybinding(XK_K, 0, "kill", map);
add_keybinding(XK_K, RP_CONTROL_MASK, "kill", map);
add_keybinding(XK_Return, 0, "next", map);
add_keybinding(XK_Return, RP_CONTROL_MASK, "next", map);
add_keybinding(XK_b, 0, "banish", map);
add_keybinding(XK_b, RP_CONTROL_MASK, "banish", map);
add_keybinding(XK_c, 0, "exec " TERM_PROG, map);
add_keybinding(XK_c, RP_CONTROL_MASK, "exec " TERM_PROG, map);
add_keybinding(XK_colon, 0, "colon", map);
add_keybinding(XK_exclam, 0, "exec", map);
add_keybinding(XK_exclam, RP_CONTROL_MASK,
"colon exec " TERM_PROG " -e ", map);
add_keybinding(XK_i, 0, "info", map);
add_keybinding(XK_i, RP_CONTROL_MASK, "info", map);
add_keybinding(XK_k, 0, "delete", map);
add_keybinding(XK_k, RP_CONTROL_MASK, "delete", map);
add_keybinding(XK_l, 0, "redisplay", map);
add_keybinding(XK_l, RP_CONTROL_MASK, "redisplay", map);
add_keybinding(XK_m, 0, "lastmsg", map);
add_keybinding(XK_m, RP_CONTROL_MASK, "lastmsg", map);
add_keybinding(XK_n, 0, "next", map);
add_keybinding(XK_n, RP_CONTROL_MASK, "next", map);
add_keybinding(XK_p, 0, "prev", map);
add_keybinding(XK_p, RP_CONTROL_MASK, "prev", map);
add_keybinding(XK_quoteright, 0, "select", map);
add_keybinding(XK_quoteright, RP_CONTROL_MASK, "select", map);
add_keybinding(XK_space, 0, "next", map);
add_keybinding(XK_space, RP_CONTROL_MASK, "next", map);
add_keybinding(XK_v, 0, "version", map);
add_keybinding(XK_v, RP_CONTROL_MASK, "version", map);
add_keybinding(XK_w, 0, "windows", map);
add_keybinding(XK_w, RP_CONTROL_MASK, "windows", map);
add_keybinding(XK_s, 0, "split", map);
add_keybinding(XK_s, RP_CONTROL_MASK, "split", map);
add_keybinding(XK_S, 0, "hsplit", map);
add_keybinding(XK_S, RP_CONTROL_MASK, "hsplit", map);
add_keybinding(XK_Tab, 0, "focus", map);
add_keybinding(XK_Tab, RP_META_MASK, "focuslast", map);
add_keybinding(XK_Left, RP_CONTROL_MASK, "exchangeleft", map);
add_keybinding(XK_Right, RP_CONTROL_MASK, "exchangeright", map);
add_keybinding(XK_Up, RP_CONTROL_MASK, "exchangeup", map);
add_keybinding(XK_Down, RP_CONTROL_MASK, "exchangedown", map);
add_keybinding(XK_Left, 0, "focusleft", map);
add_keybinding(XK_Right, 0, "focusright", map);
add_keybinding(XK_Up, 0, "focusup", map);
add_keybinding(XK_Down, 0, "focusdown", map);
add_keybinding(XK_Q, 0, "only", map);
add_keybinding(XK_R, 0, "remove", map);
add_keybinding(XK_f, 0, "fselect", map);
add_keybinding(XK_f, RP_CONTROL_MASK, "fselect", map);
add_keybinding(XK_F, 0, "curframe", map);
add_keybinding(XK_r, 0, "resize", map);
add_keybinding(XK_r, RP_CONTROL_MASK, "resize", map);
add_keybinding(XK_question, 0, "help " ROOT_KEYMAP, map);
add_keybinding(XK_underscore, RP_CONTROL_MASK, "undo", map);
add_keybinding(XK_u, 0, "undo", map);
add_keybinding(XK_u, RP_CONTROL_MASK, "undo", map);
add_keybinding(XK_U, 0, "redo", map);
add_keybinding(XK_x, 0, "swap", map);
add_keybinding(XK_x, RP_CONTROL_MASK, "swap", map);
add_keybinding(XK_N, 0, "nextscreen", map);
add_keybinding(XK_P, 0, "prevscreen", map);
add_keybinding(XK_F1, 0, "vselect 0", map);
add_keybinding(XK_F2, 0, "vselect 1", map);
add_keybinding(XK_F3, 0, "vselect 2", map);
add_keybinding(XK_F4, 0, "vselect 3", map);
add_keybinding(XK_F5, 0, "vselect 4", map);
add_keybinding(XK_F6, 0, "vselect 5", map);
add_keybinding(XK_F7, 0, "vselect 6", map);
add_keybinding(XK_F8, 0, "vselect 7", map);
add_keybinding(XK_F9, 0, "vselect 8", map);
add_keybinding(XK_F10, 0, "vselect 9", map);
add_keybinding(XK_F11, 0, "vselect 10", map);
add_keybinding(XK_F12, 0, "vselect 11", map);
add_alias("unbind", "undefinekey " ROOT_KEYMAP);
add_alias("bind", "definekey " ROOT_KEYMAP);
add_alias("split", "vsplit");
}
cmdret *
cmdret_new(int success, char *fmt,...)
{
cmdret *ret;
va_list ap;
ret = xmalloc(sizeof(cmdret));
ret->success = success;
if (fmt) {
va_start(ap, fmt);
ret->output = xvsprintf(fmt, ap);
va_end(ap);
} else