Date:2012-04-04 01:08:13 (11 years 11 months ago)
Author:David Kühling
Commit:2a118e01458f3bb805755b211ed1827d8fa6e9ec
Message:alfilesel: cleanup, fixes, add support for directly exec'ing applications

Files: alfilesel/Makefile (2 diffs)
alfilesel/files/alfilesel.c (13 diffs)

Change Details

alfilesel/Makefile
1010
1111PKG_NAME:=alfilesel
1212PKG_VERSION:=0.1.0
13PKG_RELEASE:=2
13PKG_RELEASE:=3
1414#PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
1515#PKG_SOURCE_URL:=@SF/forth-brainless
1616#PKG_MD5SUM:=ed4a4cbbe23628b17edc5aa01f32f7fb
...... 
2121define Package/alfilesel
2222  SECTION:=utilities
2323  CATEGORY:=Utilities
24  CATEGORY:=Games
2524  MAINTAINER:="David Kuehling" <dvdkhlng TA gmx TOD de>
2625  TITLE:=Graphical file selector app to use in shell scripts
2726  DEPENDS:=+liballegro +liballegro-jpeg +liballegro-png
alfilesel/files/alfilesel.c
11/* Simple liballegro based file selector helper utility.
22 *
3 * Copyright (C) 2011 David Kühling <dvdkhlng TA gmx TOD de>
3 * Copyright (C) 2012 David Kühling <dvdkhlng TA gmx TOD de>
44 *
55 * License: GPL3 or later, NO WARRANTY
66 *
...... 
1717
1818
1919static int verbose_flag = 0;
20static int run_flag = 0;
2120static int mouse_flag = 0;
2221static int help_flag = 0;
2322
...... 
2524
2625static void print_help() {
2726   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"
2933      "Options:\n"
3034      "\t-t --title=STRING\n"
3135      "\t\tSet file selector dialog's title\n"
3236      "\t-w --wallpaper=FILENAME\n"
3337      "\t\tSet file to use as wallpaper (supports jpeg, png, pcx, bmp, tga)\n"
3438      "\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"
3641      "\t-f --filter=STRING\n"
3742      "\t\tFilter files by extension and/or mode bits\n"
3843      "\t\tUse 'png;jpeg' to show only .png and .jpeg files, use '/+x' to\n"
...... 
4449       help);
4550}
4651
47
48
4952int main (int argc, char *argv[])
5053{
5154   int c;
5255   const int hborder = 32;
5356   const int vborder = 32;
57   const int trborder = 6;
5458   const char *title = "Select file";
5559   const char *init_path = 0;
5660   const char *filter = 0;
...... 
5963   char path[4096];
6064   char **cmd = NULL;
6165   int num_cmd = 0;
66   int run = 0;
6267
6368   PALETTE pal;
6469   BITMAP *backbmp;
...... 
7176        {"verbose", no_argument, &verbose_flag, 1},
7277        {"help", no_argument, &help_flag, 1},
7378        {"mouse", no_argument, &mouse_flag, 1},
74        {"run", no_argument, &run_flag, 1},
7579        /* These options don't set a flag.
7680           We distinguish them by their indices. */
7781        {"title", required_argument, 0, 't'},
...... 
8387      /* `getopt_long' stores the option index here. */
8488      int option_index = 0;
8589
86      c = getopt_long (argc, argv, "t:p:f:w:mrh",
90      c = getopt_long (argc, argv, "t:p:f:w:mh",
8791               long_options, &option_index);
8892
8993      /* Detect the end of the options. */
...... 
98102           break;
99103        break;
100104
105     case 'h':
106        help_flag = 1;
107        break;
108
109     case 'm':
110        mouse_flag = 1;
111        break;
112
101113     case 't':
102114        title = optarg;
103115        break;
...... 
130142      return 0;
131143   }
132144
133
134145   /* Print any remaining command line arguments (not options). */
135146   if (optind < argc)
136147   {
137      run_flag = 1;
148      run = 1;
138149      cmd = &argv[optind];
139150      num_cmd = argc - optind;
140151      printf ("non-option ARGV-elements: ");
...... 
152163   loadpng_init();
153164   jpgalleg_init();
154165
166   set_color_depth(32);
155167   if (set_gfx_mode(GFX_SAFE, 320, 240, 0, 0) != 0) {
156168      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
157169      allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
...... 
189201
190202   set_palette(pal);
191203
204   /* fourth parameter is alpha value (0=fully transparent) */
192205   gui_fg_color = makecol(230,210,140);
193   gui_bg_color = makecol(30,40,50);
194206   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);
195219
196220   if (!init_path)
197221      getcwd(path, sizeof(path));
...... 
200224
201225   path[sizeof(path)-1] = '\0';
202226
203
204
205227   int ok = file_select_ex(
206228      title, path, filter, sizeof(path), SCREEN_W-2*hborder, SCREEN_H-2*vborder);
207229   path[sizeof(path)-1] = '\0';
...... 
210232
211233   if (ok)
212234   {
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
214256      return 0;
215257   }
216258

Archive Download the corresponding diff file



interactive