forked from ryanflannery/vitunes
/
commands.c
1089 lines (890 loc) · 25.5 KB
/
commands.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) 2010, 2011 Ryan Flannery <ryan.flannery@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "commands.h"
bool sorts_need_saving = false;
#define swap(type, x, y) \
{ type temp = x; x = y; y = temp;}
/*
* List of command-mode commands. Take note of the following: XXX
* 1. See 'match_cmd_name()' for the handling of abbreviations.
* 2. Commands that accept a '!' after their names are handled
* in 'match_cmd_name()'.
*/
const cmd CommandPath[] = {
{ "bind", cmd_bind },
{ "color", cmd_color },
{ "display", cmd_display },
{ "filter", cmd_filter },
{ "mode", cmd_mode },
{ "new", cmd_new },
{ "playlist", cmd_playlist },
{ "q", cmd_quit },
{ "reload", cmd_reload },
{ "set", cmd_set },
{ "sort", cmd_sort },
{ "unbind", cmd_unbind },
{ "w", cmd_write },
{ "toggle", cmd_toggle }
};
const int CommandPathSize = (sizeof(CommandPath) / sizeof(cmd));
/****************************************************************************
* Toggleset related stuff
***************************************************************************/
toggle_list **toggleset;
size_t toggleset_size;
void
toggleset_init()
{
const int max_size = 52; /* since we only have registers a-z and A-Z */
if ((toggleset = calloc(max_size, sizeof(toggle_list*))) == NULL)
err(1, "%s: calloc(3) failed", __FUNCTION__);
toggleset_size = 0;
}
void
toggleset_free()
{
size_t i;
for (i = 0; i < toggleset_size; i++)
toggle_list_free(toggleset[i]);
free(toggleset);
toggleset_size = 0;
}
void
toggle_list_add_command(toggle_list *t, char *cmd)
{
char **new_cmds;
int idx, new_size;
/* resize array */
if (t->size == 0) {
if ((t->commands = malloc(sizeof(char*))) == NULL)
err(1, "%s: malloc(3) failed", __FUNCTION__);
idx = 0;
t->size = 1;
} else {
new_size = (t->size + 1) * sizeof(char*);
if ((new_cmds = realloc(t->commands, new_size)) == NULL)
err(1, "%s: realloc(3) failed", __FUNCTION__);
idx = t->size;
t->commands = new_cmds;
t->size++;
}
/* add command */
if ((t->commands[idx] = strdup(cmd)) == NULL)
err(1, "%s: strdup(3) failed", __FUNCTION__);
}
toggle_list*
toggle_list_create(int registr, int argc, char *argv[])
{
toggle_list *t;
char *cmd = NULL;
int i, j;
if ((t = malloc(sizeof(toggle_list))) == NULL)
err(1, "%s: malloc(3) failed", __FUNCTION__);
t->commands = NULL;
t->registr = registr;
t->size = 0;
/* parse the argv into the toggle list */
for (i = 0; i < argc; i++) {
if (!strcmp("/", argv[i]))
continue;
/* count number strings in this command and determine length */
for (j = i; j < argc && strcmp("/", argv[j]); j++)
/* now collapse them into a single string */
cmd = argv2str(j - i + 1, argv + i);
toggle_list_add_command(t, cmd);
free(cmd);
i += (j - i) - 1;
}
t->index = t->size - 1;
return t;
}
void
toggle_list_free(toggle_list *t)
{
size_t i;
for (i = 0; i < t->size; i++)
free(t->commands[i]);
free(t);
}
void
toggle_add(toggle_list *t)
{
if (toggle_get(t->registr) != NULL)
toggle_remove(t->registr);
toggleset[toggleset_size++] = t;
}
void
toggle_remove(int registr)
{
size_t i, idx;
bool found;
found = false;
idx = 0;
for (i = 0; i < toggleset_size; i++) {
if (toggleset[i]->registr == registr) {
idx = i;
found = true;
}
}
if (!found) return;
for (i = idx; i < toggleset_size - 1; i++)
toggleset[i] = toggleset[i + 1];
toggleset_size--;
}
toggle_list*
toggle_get(int registr)
{
size_t i;
for (i = 0; i < toggleset_size; i++) {
if (toggleset[i]->registr == registr)
return toggleset[i];
}
return NULL;
}
/****************************************************************************
* Misc handy functions
***************************************************************************/
void
setup_viewing_playlist(playlist *p)
{
viewing_playlist = p;
ui.playlist->nrows = p->nfiles;
ui.playlist->crow = 0;
ui.playlist->voffset = 0;
ui.playlist->hoffset = 0;
}
int
str2bool(const char *s, bool *b)
{
/* check true/t/yes/y */
if (strcasecmp(s, "true") == 0
|| strcasecmp(s, "t") == 0
|| strcasecmp(s, "yes") == 0
|| strcasecmp(s, "y") == 0) {
*b = true;
return 0;
}
/* check false/f/no/n */
if (strcasecmp(s, "false") == 0
|| strcasecmp(s, "f") == 0
|| strcasecmp(s, "no") == 0
|| strcasecmp(s, "n") == 0) {
*b = false;
return 0;
}
return -1;
}
/****************************************************************************
* Command handlers
***************************************************************************/
int
cmd_quit(int argc, char *argv[])
{
bool forced;
int i;
if (argc != 1) {
paint_error("usage: q[!]");
return 1;
}
/* is this a forced quit? */
forced = (strcmp(argv[0], "q!") == 0);
/* check if there are any unsaved changes if not forced */
if (!forced) {
for (i = 0; i < mdb.nplaylists; i++) {
if (mdb.playlists[i]->needs_saving) {
paint_error("there are playlists with unsaved changes. use \"q!\" to force.");
return 2;
}
}
}
VSIG_QUIT = 1;
return 0;
}
int
cmd_write(int argc, char *argv[])
{
playlist *dup;
char *filename;
bool forced;
bool will_clobber;
int i, clobber_index;
if (argc > 2) {
paint_error("usage: w[!] [name]");
return 1;
}
forced = (strcmp(argv[0], "w!") == 0);
if (argc == 1) { /* "save" */
/* can't save library or filter results */
if (viewing_playlist == mdb.library
|| viewing_playlist == mdb.filter_results) {
paint_error("use \"w name\" when saving psuedo-playlists like library/filter");
return 2;
}
/* can't save a new playlist that has no name */
if (viewing_playlist->filename == NULL) {
paint_error("use \"w name\" for new playlists");
return 3;
}
/* do the save... */
playlist_save(viewing_playlist);
viewing_playlist->needs_saving = false;
paint_library();
paint_message("\"%s\" %d songs written",
viewing_playlist->filename, viewing_playlist->nfiles);
} else { /* "save as" */
/* build filename for playlist */
asprintf(&filename, "%s/%s.playlist", mdb.playlist_dir, argv[1]);
if (filename == NULL)
err(1, "cmd_write: asprintf(3) failed");
/* check to see if playlist with that name already exists */
will_clobber = false;
clobber_index = -1;
for (i = 0; i < mdb.nplaylists; i++) {
if (mdb.playlists[i]->filename != NULL
&& strcmp(mdb.playlists[i]->filename, filename) == 0) {
will_clobber = true;
clobber_index = i;
}
}
if (will_clobber && !forced) {
paint_error("playlist with that name exists (use \"w!\" to overwrite)");
free(filename);
return 4;
}
/* if reached here, we're going to do the save-as... */
/* duplicate playlist */
dup = playlist_dup(viewing_playlist, filename, argv[1]);
if (will_clobber)
medialib_playlist_remove(clobber_index);
else
ui.library->nrows++;
/*
* TODO If the original playlist was a new one, with no name (i.e. if
* name == NULL) then we should remove that playlist from the medialib
* and display here.
*/
/* do the save-as... */
playlist_save(dup);
medialib_playlist_add(dup);
dup->needs_saving = false;
viewing_playlist->needs_saving = false;
paint_library();
paint_message("\"%s\" %d songs written",
filename, viewing_playlist->nfiles);
}
return 0;
}
int
cmd_mode(int argc, char *argv[])
{
if (argc != 2) {
paint_error("usage: mode [ linear | loop | random ]");
return 1;
}
if (strcasecmp(argv[1], "linear") == 0)
player_info.mode = MODE_LINEAR;
else if (strcasecmp(argv[1], "loop") == 0)
player_info.mode = MODE_LOOP;
else if (strcasecmp(argv[1], "random") == 0)
player_info.mode = MODE_RANDOM;
else {
paint_error("invalid mode \"%s\". must be one of: linear, loop, or random", argv[1]);
return 2;
}
paint_message("mode changed to: %s", argv[1]);
return 0;
}
int
cmd_new(int argc, char *argv[])
{
playlist *p;
char *name;
char *filename;
int i;
if (argc > 2) {
paint_error("usage: new [name]");
return 1;
}
/* defaults */
name = "untitled";
filename = NULL;
/* was a name specified? */
if (argc == 2) {
/* check for existing playlist with name */
for (i = 0; i < mdb.nplaylists; i++) {
if (strcmp(mdb.playlists[i]->name, argv[1]) == 0) {
paint_error("playlist \"%s\" already exists.", argv[1]);
return 2;
}
}
name = argv[1];
asprintf(&filename, "%s/%s.playlist", mdb.playlist_dir, name);
if (filename == NULL)
err(1, "cmd_new: asprintf(3) failed");
}
/* create & setup playlist */
p = playlist_new();
p->needs_saving = true;
p->filename = filename;
if ((p->name = strdup(name)) == NULL)
err(1, "cmd_new: strdup(3) failed");
/* add playlist to media library and update ui */
medialib_playlist_add(p);
ui.library->nrows++;
if (p->filename != NULL)
playlist_save(p);
/* redraw */
paint_library();
paint_message("playlist \"%s\" added", name);
return 0;
}
int
cmd_filter(int argc, char *argv[])
{
playlist *results;
char *search_phrase;
bool match;
int i;
if (argc == 1) {
paint_error("usage: filter[!] token [token2 ...]");
return 1;
}
/* determine what kind of filter we're doing */
match = argv[0][strlen(argv[0]) - 1] != '!';
/* set the raw query */
search_phrase = argv2str(argc - 1, argv + 1);
mi_query_setraw(search_phrase);
free(search_phrase);
/* clear existing global query & set new one */
mi_query_clear();
for (i = 1; i < argc; i++)
mi_query_add_token(argv[i]);
/* do actual filter */
results = playlist_filter(viewing_playlist, match);
/* swap necessary bits of results with filter playlist */
swap(meta_info **, results->files, mdb.filter_results->files);
swap(int, results->nfiles, mdb.filter_results->nfiles);
swap(int, results->capacity, mdb.filter_results->capacity);
playlist_free(results);
/* redraw */
setup_viewing_playlist(mdb.filter_results);
paint_library();
paint_playlist();
return 0;
}
int
cmd_sort(int argc, char *argv[])
{
const char *errmsg;
if (argc != 2) {
paint_error("usage: sort <sort-description>");
return 1;
}
/* setup global sort description */
if (mi_sort_set(argv[1], &errmsg) != 0) {
paint_error("%s: bad sort description: %s", argv[0], errmsg);
return 2;
}
/* do the actual sort */
qsort(viewing_playlist->files, viewing_playlist->nfiles,
sizeof(meta_info*), mi_compare);
if (!ui_is_init())
return 0;
/* redraw */
paint_playlist();
/* if we sorted a playlist other than library, and user wants to save sorts */
if (viewing_playlist != mdb.library && sorts_need_saving) {
viewing_playlist->needs_saving = true;
paint_library();
}
return 0;
}
int
cmd_display(int argc, char *argv[])
{
const char *errmsg;
if (argc != 2) {
paint_error("usage: display [ reset | show | <display-description> ]");
return 1;
}
/* show existng display? */
if (strcasecmp(argv[1], "show") == 0) {
paint_message(":display %s", mi_display_tostr());
return 0;
}
/* reset display to default? */
if (strcasecmp(argv[1], "reset") == 0) {
mi_display_reset();
if (ui_is_init())
paint_playlist();
return 0;
}
/* if reached here, setup global display description */
if (mi_display_set(argv[1], &errmsg) != 0) {
paint_error("%s: bad display description: %s", argv[0], errmsg);
return 1;
}
if(ui_is_init())
paint_playlist();
return 0;
}
int
cmd_color(int argc, char *argv[])
{
char *item;
char *fg, *bg;
int i_item, i_fg, i_bg, j;
if (argc != 2) {
paint_error("usage: %s ITEM=FG,BG", argv[0]);
return 1;
}
/* extract item and foreground/background colors */
item = argv[1];
if ((fg = strchr(item, '=')) == NULL) {
paint_error("usage: %s ITEM=FG,BG", argv[0]);
return 2;
}
*fg = '\0';
fg++;
if ((bg = strchr(fg, ',')) == NULL) {
paint_error("usage: %s ITEM=FG,BG", argv[0]);
return 3;
}
*bg = '\0';
bg++;
/* convert all */
if ((i_item = paint_str2item(item)) < 0) {
paint_error("invalid item '%s'", item);
return 4;
}
if ((i_fg = paint_str2color(fg)) == -2) {
paint_error("invalid foreground color '%s'", fg);
return 5;
}
if ((i_bg = paint_str2color(bg)) == -2) {
paint_error("invalid background color '%s'", bg);
return 6;
}
/* init color */
init_pair(i_item, i_fg, i_bg);
/* if this was a cinfo item, indicate that it was set */
for (j = 0; j < MI_NUM_CINFO; j++) {
if (i_item == colors.cinfos[j])
colors.cinfos_set[j] = true;
}
/* redraw */
if (ui_is_init()) {
ui_clear();
paint_all();
}
return 0;
}
int
cmd_set(int argc, char *argv[])
{
const char *err;
char *property;
char *value;
bool tf;
int max_w, max_h, new_width; /* lwidth */
bool player_is_setup;
if (argc != 2) {
paint_error("usage: %s <property>=<value>", argv[0]);
return 1;
}
/* determine if the player has been setup (needed for redraws below) */
player_is_setup = (player.name != NULL)
&& (*player.name != NULL);
/* extract property and value */
property = argv[1];
if ((value = strchr(property, '=')) == NULL) {
paint_error("usage: %s <property>=<value>", argv[0]);
return 2;
}
*value = '\0';
value++;
/* handle property */
if (strcasecmp(property, "lwidth") == 0) {
/* get max width and height */
getmaxyx(stdscr, max_h, max_w);
/* validate and convert width user provided */
new_width = (int)strtonum(value, 1, max_w, &err);
if (err != NULL) {
paint_error("%s %s: bad width: '%s' %s",
argv[0], property, value, err);
return 3;
}
/* resize & redraw (if we're past setup & curses is running) */
ui.lwidth = new_width;
ui_resize();
if(player_is_setup && ui_is_init()) {
ui_clear();
paint_all();
}
} else if (strcasecmp(property, "lhide") == 0) {
if (str2bool(value, &tf) < 0) {
paint_error("%s %s: value must be boolean",
argv[0], property);
return 4;
}
ui.lhide = tf;
if (ui.lhide) {
if (ui.active == ui.playlist)
ui_hide_library();
if (player_is_setup && ui_is_init()) {
ui_clear();
paint_all();
paint_message("library window hidden");
}
} else {
if (ui.library->cwin == NULL) ui_unhide_library();
if (player_is_setup && ui_is_init()) paint_all();
paint_message("library window un-hidden");
}
} else if (strcasecmp(property, "match-fname") == 0) {
if (str2bool(value, &tf) < 0) {
paint_error("%s %s: value must be boolean",
argv[0], property);
return 5;
}
mi_query_match_filename = tf;
if (mi_query_match_filename)
paint_message("filenames will be matched against");
else
paint_message("filenames will NOT be matched against");
} else if (strcasecmp(property, "save-sorts") == 0) {
if (str2bool(value, &tf) < 0) {
paint_error("%s %s: value must be boolean",
argv[0], property);
return 6;
}
sorts_need_saving = tf;
if (sorts_need_saving)
paint_message("changing sort will be prompted for saving");
else
paint_message("changing sort will NOT be prompted for saving");
} else {
paint_error("%s: unknown property '%s'", argv[0], property);
return 7;
}
return 0;
}
int
cmd_reload(int argc, char *argv[])
{
char *db_file;
char *playlist_dir;
if (argc != 2) {
paint_error("usage: %s [ db | conf ]", argv[0]);
return 1;
}
/* reload database or config file */
if (strcasecmp(argv[1], "db") == 0) {
db_file = strdup(mdb.db_file);
playlist_dir = strdup(mdb.playlist_dir);
if (db_file == NULL || playlist_dir == NULL)
err(1, "cmd_reload: strdup(3) failed");
/* stop playback TODO investigate a nice way around this */
player_stop();
/* reload db */
medialib_destroy();
medialib_load(db_file, playlist_dir);
free(db_file);
free(playlist_dir);
/* re-setup ui basics */
playing_playlist = NULL;
setup_viewing_playlist(mdb.library);
ui.library->voffset = 0;
ui.library->nrows = mdb.nplaylists;
ui.library->crow = 0;
paint_all();
} else if (strcasecmp(argv[1], "conf") == 0) {
load_config();
paint_message("configuration reloaded");
} else {
paint_error("usage: %s [ db | conf ]", argv[0]);
return 2;
}
return 0;
}
int
cmd_bind(int argc, char *argv[])
{
KeyAction action;
KeyCode code;
if (argc < 3 || argc > 4) {
paint_error("usage: %s <action> <keycode>", argv[0]);
return 1;
}
if (!kb_str2action(argv[1], &action)) {
paint_error("Unknown action '%s'", argv[1]);
return 1;
}
if (argc == 3) {
if ((code = kb_str2keycode(argv[2])) < 0) {
paint_error("Invalid keycode '%s'", argv[2]);
return 1;
}
} else {
if ((code = kb_str2keycode2(argv[2], argv[3])) < 0) {
paint_error("Invalid keycode '%s'", argv[2]);
return 1;
}
}
kb_bind(action, code);
return 0;
}
int
cmd_unbind(int argc, char *argv[])
{
KeyAction action;
KeyCode key;
/* unbind all case ("unbind *") */
if (argc == 2 && strcmp(argv[1], "*") == 0) {
kb_unbind_all();
return 0;
}
/* unbind action case ("unbind action <ACTION>") */
if (argc == 3 && strcasecmp(argv[1], "action") == 0) {
if (kb_str2action(argv[2], &action)) {
kb_unbind_action(action);
return 0;
} else {
paint_error("Unknown action '%s'", argv[2]);
return 1;
}
}
/* unbind key case, no control ("unbind key X") */
if (argc == 3 && strcasecmp(argv[1], "key") == 0) {
if ((key = kb_str2keycode(argv[2])) < 0) {
paint_error("Invalid keycode '%s'", argv[2]);
return 1;
}
kb_unbind_key(key);
return 0;
}
/* unbind key case, with control ("unbind key control X") */
if (argc == 4 && strcasecmp(argv[1], "key") == 0) {
if ((key = kb_str2keycode2(argv[2], argv[3])) < 0) {
paint_error("Invalid keycode '%s %s'", argv[2], argv[3]);
return 1;
}
kb_unbind_key(key);
return 0;
}
paint_error("usage: unbind [* | action <ACTION> | key <KEYCODE> ]");
return 1;
}
int
cmd_toggle(int argc, char *argv[])
{
toggle_list *t;
char **cmd_argv;
int cmd_argc;
int registr;
if (argc < 3) {
paint_error("usage: %s <register> <action1> / ...", argv[0]);
return 1;
}
if (strlen(argv[1]) != 1) {
paint_error("error: register name must be a single letter (in [a-zA-Z])");
return 1;
}
registr = *(argv[1]);
if (!( ('a' <= registr && registr <= 'z')
|| ('A' <= registr && registr <= 'Z'))) {
paint_error("error: invalid register name. Must be one of [a-zA-Z]");
return 1;
}
cmd_argc = argc - 2;
cmd_argv = argv + 2;
t = toggle_list_create(registr, cmd_argc, cmd_argv);
toggle_add(t);
return 0;
}
int
cmd_playlist(int argc, char *argv[])
{
int x;
int idx = -1;
if (argc != 2) {
paint_error("usage: playlist <list-name>");
return 1;
}
for(x = 0; x < mdb.nplaylists; x++) {
if(!strncmp(argv[1], mdb.playlists[x]->name, strlen(argv[1]))) {
if(idx > -1) {
idx = -2;
break;
}
if(idx == -1)
idx = x;
}
}
if(idx > -1) {
setup_viewing_playlist(mdb.playlists[idx]);
ui.active = ui.playlist;
paint_all();
paint_message("jumped to playlist: %s", mdb.playlists[idx]->name);
return 0;
}
if(idx == -1) {
paint_error("no match for: %s", argv[1]);
return 0;
}
if(idx == -2)
paint_error("no unique match for: %s", argv[1]);
return 0;
}
void
cmd_execute(char *cmd)
{
const char *errmsg = NULL;
bool found;
char **argv;
int argc;
int found_idx = 0;
int num_matches;
int i;
if (str2argv(cmd, &argc, &argv, &errmsg) != 0) {
paint_error("parse error: %s in '%s'", errmsg, cmd);
return;
}
found = false;
num_matches = 0;
for (i = 0; i < CommandPathSize; i++) {
if (match_command_name(argv[0], CommandPath[i].name)) {
found = true;
found_idx = i;
num_matches++;
}
}
if (found && num_matches == 1)
(CommandPath[found_idx].func)(argc, argv);
else if (num_matches > 1)
paint_error("Ambiguous abbreviation '%s'", argv[0]);
else
paint_error("Unknown commands '%s'", argv[0]);
argv_free(&argc, &argv);
}
/*****************************************************************************
* command window input methods
****************************************************************************/
/*
* Note: Both of these return 0 if input was successfull, and something else
* (1) if the user cancelled the input (such as, by hitting ESCAPE)
*/
int
user_getstr(const char *prompt, char **response)
{
const int MAX_INPUT_SIZE = 1000; /* TODO remove this limit */
char *input;
int pos, ch, ret;
/* display the prompt */
werase(ui.command);
mvwprintw(ui.command, 0, 0, "%s", prompt);
/* position the cursor */
curs_set(1);
wmove(ui.command, 0, strlen(prompt));
wrefresh(ui.command);
/* allocate input space and clear */
if ((input = calloc(MAX_INPUT_SIZE, sizeof(char))) == NULL)
err(1, "user_getstr: calloc(3) failed for input string");
bzero(input, MAX_INPUT_SIZE);
/* start getting input */
ret = 0;
pos = 0;
while ((ch = getch()) && !VSIG_QUIT) {
/*
* Handle any signals. Note that the use of curs_set, wmvoe, and
* wrefresh here are all necessary to ensure that the cursor does
* not show anywhere outside of the command window.
*/
curs_set(0);
process_signals();