2 * Copyright (c) 2016, S. Gilles <sgilles@math.umd.edu>
4 * Permission to use, copy, modify, and/or distribute this software
5 * for any purpose with or without fee is hereby granted, provided
6 * that the above copyright notice and this permission notice appear
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
13 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
14 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 * I am fully aware of how ugly this file is, which is why it's specially
29 * split out, so that it can be completely redone one day, when some
30 * kind of better solution exists.
32 static uint_fast8_t try_kdialog
= 1;
33 static uint_fast8_t try_qarma
= 1;
34 static uint_fast8_t try_yad
= 1;
35 static uint_fast8_t try_zenity
= 1;
36 static uint_fast8_t try_xdialog
= 1;
37 enum action
{ ACT_SAVE
, ACT_LOAD
};
38 static char *cmd_kdialog
[] = {
40 [ACT_SAVE
] = "kdialog --getsavefilename :clav 2>/dev/null", /* */
41 [ACT_LOAD
] = "kdialog --getopenfilename :clav 2>/dev/null", /* */
43 static char *cmd_qarma
[] = {
45 [ACT_SAVE
] = "qarma --title 'Save As' --file-selection --save 2>/dev/null", /* */
46 [ACT_LOAD
] = "qarma --title 'Load' --file-selection 2>/dev/null", /* */
48 static char *cmd_yad
[] = {
51 "yad --title 'Save As' --width=640 --height=480 --file-selection --save 2>/dev/null", /* */
53 "yad --title 'Load' --width=640 --height=480 --file-selection 2>/dev/null", /* */
55 static char *cmd_zenity
[] = {
57 [ACT_SAVE
] = "zenity --title 'Save As' --file-selection --save 2>/dev/null", /* */
58 [ACT_LOAD
] = "zenity --title 'Load' --file-selection 2>/dev/null", /* */
60 static char *cmd_xdialog
[] = {
63 "Xdialog --stdout --title 'Save As' --fselect '' 0 0 2>/dev/null", /* */
65 "Xdialog --stdout --title 'Load' --fselect '' 0 0 2>/dev/null", /* */
68 /* Naively slurp stream in completly, truncate last `\n'. */
69 static int slurp(FILE *f
, char **out
)
71 static size_t read_size
= 1 << 7;
75 size_t sz
= read_size
- 1;
78 if (!(buf
= malloc(sz
+ 1))) {
89 if (!fgets(buf
+ len
, read_size
, f
)) {
93 p
= strchr(buf
+ len
, '\0');
98 if (!(newmem
= realloc(buf
, sz
+ 1))) {
100 perror(L("realloc"));
107 if ((p
= strrchr(buf
+ len
- 1, '\n'))) {
117 WEXITSTATUS(pret
) != 127) {
129 static int choose_file(char **out_filename
, enum action act
)
138 if ((f
= popen(cmd_kdialog
[act
], "r"))) {
139 if (!(ret
= slurp(f
, &filename
))) {
150 if ((f
= popen(cmd_qarma
[act
], "r"))) {
151 if (!(ret
= slurp(f
, &filename
))) {
162 if ((f
= popen(cmd_yad
[act
], "r"))) {
163 if (!(ret
= slurp(f
, &filename
))) {
174 if ((f
= popen(cmd_zenity
[act
], "r"))) {
175 if (!(ret
= slurp(f
, &filename
))) {
186 if ((f
= popen(cmd_xdialog
[act
], "r"))) {
187 if (!(ret
= slurp(f
, &filename
))) {
201 *out_filename
= filename
;
209 int choose_save_file(char **out_filename
)
211 return choose_file(out_filename
, ACT_SAVE
);
214 int choose_load_file(char **out_filename
)
216 return choose_file(out_filename
, ACT_LOAD
);