alfilesel/files/alfilesel.c |
1 | 1 | /* Simple liballegro based file selector helper utility. |
2 | 2 | * |
3 | | * Copyright (C) 2011 David Kühling <dvdkhlng TA gmx TOD de> |
| 3 | * Copyright (C) 2012 David Kühling <dvdkhlng TA gmx TOD de> |
4 | 4 | * |
5 | 5 | * License: GPL3 or later, NO WARRANTY |
6 | 6 | * |
... | ... | |
17 | 17 | |
18 | 18 | |
19 | 19 | static int verbose_flag = 0; |
20 | | static int run_flag = 0; |
21 | 20 | static int mouse_flag = 0; |
22 | 21 | static int help_flag = 0; |
23 | 22 | |
... | ... | |
25 | 24 | |
26 | 25 | static void print_help() { |
27 | 26 | const char *help = |
28 | | "[OPTION]...\n" |
| 27 | "[OPTION] [--] [PROGRAM_TO_RUN] [PROGRAM_ARGS]..\n" |
| 28 | "\n" |
| 29 | "If PROGRAM_TO_RUN is specfied, run it with arguments PROGRAM_ARGS and\n" |
| 30 | "the selected file appended as final argument.\n" |
| 31 | "Else just print the selected filename and newline to stdout.\n" |
| 32 | "\n" |
29 | 33 | "Options:\n" |
30 | 34 | "\t-t --title=STRING\n" |
31 | 35 | "\t\tSet file selector dialog's title\n" |
32 | 36 | "\t-w --wallpaper=FILENAME\n" |
33 | 37 | "\t\tSet file to use as wallpaper (supports jpeg, png, pcx, bmp, tga)\n" |
34 | 38 | "\t-p --path=PATH\n" |
35 | | "\t\tSet initial directory and/or filename\n" |
| 39 | "\t\tSet initial directory and/or filename. When giving an initial\n" |
| 40 | "\t\tdirectory, make sure that PATH ends in a slash.\n" |
36 | 41 | "\t-f --filter=STRING\n" |
37 | 42 | "\t\tFilter files by extension and/or mode bits\n" |
38 | 43 | "\t\tUse 'png;jpeg' to show only .png and .jpeg files, use '/+x' to\n" |
... | ... | |
44 | 49 | help); |
45 | 50 | } |
46 | 51 | |
47 | | |
48 | | |
49 | 52 | int main (int argc, char *argv[]) |
50 | 53 | { |
51 | 54 | int c; |
52 | 55 | const int hborder = 32; |
53 | 56 | const int vborder = 32; |
| 57 | const int trborder = 6; |
54 | 58 | const char *title = "Select file"; |
55 | 59 | const char *init_path = 0; |
56 | 60 | const char *filter = 0; |
... | ... | |
59 | 63 | char path[4096]; |
60 | 64 | char **cmd = NULL; |
61 | 65 | int num_cmd = 0; |
| 66 | int run = 0; |
62 | 67 | |
63 | 68 | PALETTE pal; |
64 | 69 | BITMAP *backbmp; |
... | ... | |
71 | 76 | {"verbose", no_argument, &verbose_flag, 1}, |
72 | 77 | {"help", no_argument, &help_flag, 1}, |
73 | 78 | {"mouse", no_argument, &mouse_flag, 1}, |
74 | | {"run", no_argument, &run_flag, 1}, |
75 | 79 | /* These options don't set a flag. |
76 | 80 | We distinguish them by their indices. */ |
77 | 81 | {"title", required_argument, 0, 't'}, |
... | ... | |
83 | 87 | /* `getopt_long' stores the option index here. */ |
84 | 88 | int option_index = 0; |
85 | 89 | |
86 | | c = getopt_long (argc, argv, "t:p:f:w:mrh", |
| 90 | c = getopt_long (argc, argv, "t:p:f:w:mh", |
87 | 91 | long_options, &option_index); |
88 | 92 | |
89 | 93 | /* Detect the end of the options. */ |
... | ... | |
98 | 102 | break; |
99 | 103 | break; |
100 | 104 | |
| 105 | case 'h': |
| 106 | help_flag = 1; |
| 107 | break; |
| 108 | |
| 109 | case 'm': |
| 110 | mouse_flag = 1; |
| 111 | break; |
| 112 | |
101 | 113 | case 't': |
102 | 114 | title = optarg; |
103 | 115 | break; |
... | ... | |
130 | 142 | return 0; |
131 | 143 | } |
132 | 144 | |
133 | | |
134 | 145 | /* Print any remaining command line arguments (not options). */ |
135 | 146 | if (optind < argc) |
136 | 147 | { |
137 | | run_flag = 1; |
| 148 | run = 1; |
138 | 149 | cmd = &argv[optind]; |
139 | 150 | num_cmd = argc - optind; |
140 | 151 | printf ("non-option ARGV-elements: "); |
... | ... | |
152 | 163 | loadpng_init(); |
153 | 164 | jpgalleg_init(); |
154 | 165 | |
| 166 | set_color_depth(32); |
155 | 167 | if (set_gfx_mode(GFX_SAFE, 320, 240, 0, 0) != 0) { |
156 | 168 | set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); |
157 | 169 | allegro_message("Unable to set any graphic mode\n%s\n", allegro_error); |
... | ... | |
189 | 201 | |
190 | 202 | set_palette(pal); |
191 | 203 | |
| 204 | /* fourth parameter is alpha value (0=fully transparent) */ |
192 | 205 | gui_fg_color = makecol(230,210,140); |
193 | | gui_bg_color = makecol(30,40,50); |
194 | 206 | gui_mg_color = makecol(140,150,160); |
| 207 | gui_bg_color = makecol(30,40,50); |
| 208 | |
| 209 | /* add some translucent border around file selector dialog. We'd like to |
| 210 | make the whole dialog translucent by using makeacol() above and |
| 211 | set_alpha_blender(), however the dialog can't cope with some of the |
| 212 | changes in drawing function semantics that go along with that */ |
| 213 | drawing_mode(DRAW_MODE_TRANS, 0, 0, 0); |
| 214 | set_trans_blender(0,0,0,128); |
| 215 | rectfill(screen, hborder-trborder, vborder-trborder, |
| 216 | SCREEN_W-hborder+trborder, SCREEN_H-vborder+trborder, |
| 217 | gui_bg_color); |
| 218 | drawing_mode(DRAW_MODE_SOLID, 0, 0, 0); |
195 | 219 | |
196 | 220 | if (!init_path) |
197 | 221 | getcwd(path, sizeof(path)); |
... | ... | |
200 | 224 | |
201 | 225 | path[sizeof(path)-1] = '\0'; |
202 | 226 | |
203 | | |
204 | | |
205 | 227 | int ok = file_select_ex( |
206 | 228 | title, path, filter, sizeof(path), SCREEN_W-2*hborder, SCREEN_H-2*vborder); |
207 | 229 | path[sizeof(path)-1] = '\0'; |
... | ... | |
210 | 232 | |
211 | 233 | if (ok) |
212 | 234 | { |
213 | | printf ("%s", path); |
| 235 | printf ("%s\n", path); |
| 236 | |
| 237 | if (run) |
| 238 | { |
| 239 | char **argv_exec = malloc(sizeof(char*)*(num_cmd+2)); |
| 240 | if (!argv_exec) |
| 241 | return 2; |
| 242 | memcpy (argv_exec, cmd, sizeof(char*)*(num_cmd)); |
| 243 | argv_exec[num_cmd] = path; |
| 244 | argv_exec[num_cmd+1] = 0; |
| 245 | |
| 246 | if (execvp(cmd[0], argv_exec)) |
| 247 | { |
| 248 | perror ("Can't exec"); |
| 249 | return 3; |
| 250 | } |
| 251 | |
| 252 | /* exec shouldn't return! */ |
| 253 | return 4; |
| 254 | } |
| 255 | |
214 | 256 | return 0; |
215 | 257 | } |
216 | 258 | |