alfilesel/Makefile |
| 1 | # |
| 2 | # Copyright (C) 2012 David Kuehling <dvdkhlng TA gmx TOD de> |
| 3 | # |
| 4 | # License GPLv2 or later. NO WARRANTY. |
| 5 | # |
| 6 | # OpenWRT package that compile a simple allegro game library file dialog |
| 7 | # helper application. |
| 8 | |
| 9 | include $(TOPDIR)/rules.mk |
| 10 | |
| 11 | PKG_NAME:=alfilesel |
| 12 | PKG_VERSION:=0.1.0 |
| 13 | PKG_RELEASE:=2 |
| 14 | #PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz |
| 15 | #PKG_SOURCE_URL:=@SF/forth-brainless |
| 16 | #PKG_MD5SUM:=ed4a4cbbe23628b17edc5aa01f32f7fb |
| 17 | #PKG_INSTALL:=1 |
| 18 | |
| 19 | include $(INCLUDE_DIR)/package.mk |
| 20 | |
| 21 | define Package/alfilesel |
| 22 | SECTION:=utilities |
| 23 | CATEGORY:=Utilities |
| 24 | CATEGORY:=Games |
| 25 | MAINTAINER:="David Kuehling" <dvdkhlng TA gmx TOD de> |
| 26 | TITLE:=Graphical file selector app to use in shell scripts |
| 27 | DEPENDS:=+liballegro +liballegro-jpeg +liballegro-png |
| 28 | endef |
| 29 | |
| 30 | define Package/alfilesel/description |
| 31 | A graphical file selector dialog using the allegro game library. Use it |
| 32 | to make allow gmenu2x shortcuts start applications that don\'t provide a menu |
| 33 | themselves (such as mplayer). |
| 34 | endef |
| 35 | |
| 36 | define Build/Configure |
| 37 | |
| 38 | endef |
| 39 | |
| 40 | TARGET_LDFLAGS += -ljpgalleg -lloadpng -lalleg -lpng -lz -lm -lpthread -lrt |
| 41 | |
| 42 | define Build/Configure |
| 43 | $(CP) ./files/alfilesel.c $(PKG_BUILD_DIR)/ |
| 44 | endef |
| 45 | |
| 46 | define Build/Compile |
| 47 | $(call Build/Compile/Default, alfilesel) |
| 48 | endef |
| 49 | |
| 50 | define Package/alfilesel/install |
| 51 | $(INSTALL_DIR) $(1)/usr/bin |
| 52 | $(INSTALL_BIN) $(PKG_BUILD_DIR)/alfilesel $(1)/usr/bin/ |
| 53 | endef |
| 54 | |
| 55 | $(eval $(call BuildPackage,alfilesel)) |
| 56 | |
| 57 | # The following comments configure the Emacs editor. Just ignore them. |
| 58 | # Local Variables: |
| 59 | # compile-command: "make -C ~/h/src/qi/openwrt-xburst package/alfilesel/compile -j2 V=99" |
| 60 | # End: |
alfilesel/files/alfilesel.c |
| 1 | /* Simple liballegro based file selector helper utility. |
| 2 | * |
| 3 | * Copyright (C) 2011 David Kühling <dvdkhlng TA gmx TOD de> |
| 4 | * |
| 5 | * License: GPL3 or later, NO WARRANTY |
| 6 | * |
| 7 | * Created: Apr 2012 |
| 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <getopt.h> |
| 13 | |
| 14 | #include <allegro.h> |
| 15 | #include <loadpng.h> |
| 16 | #include <jpgalleg.h> |
| 17 | |
| 18 | |
| 19 | static int verbose_flag = 0; |
| 20 | static int run_flag = 0; |
| 21 | static int mouse_flag = 0; |
| 22 | static int help_flag = 0; |
| 23 | |
| 24 | char * program_invocation_short_name; |
| 25 | |
| 26 | static void print_help(const char *name) { |
| 27 | const char *help = |
| 28 | "[OPTION]..." |
| 29 | "Options:" |
| 30 | "\t-t --title=STRING\n" |
| 31 | "\t\tSet file selector dialog's title\n" |
| 32 | "\t-w --wallpaper=FILENAME\n" |
| 33 | "\t\tSet file to use as wallpaper (supports jpeg, png, pcx, bmp, tga)\n" |
| 34 | "\t-p --path=PATH\n" |
| 35 | "\t\tSet initial directory and/or filename\n" |
| 36 | "\t-f --filter=STRING\n" |
| 37 | "\t\tFilter files by extension and/or mode bits\n" |
| 38 | "\t\tUse 'png;jpeg' to show only .png and .jpeg files, use '/+x' to\n" |
| 39 | "\t\tonly show executable files etc.\n" |
| 40 | "\t-m --mouse\n" |
| 41 | "\t\tenable mouse (disabled by default)\n"; |
| 42 | |
| 43 | printf ("USAGE: %s %s", program_invocation_short_name, |
| 44 | help); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | |
| 49 | int main (int argc, char *argv[]) |
| 50 | { |
| 51 | int c; |
| 52 | const int hborder = 32; |
| 53 | const int vborder = 32; |
| 54 | const char *title = "Select file"; |
| 55 | const char *init_path = 0; |
| 56 | const char *filter = 0; |
| 57 | const char *wallpaper = |
| 58 | "/usr/share/gmenu2x/skins/Default/wallpapers/default.png"; |
| 59 | char path[4096]; |
| 60 | char **cmd = NULL; |
| 61 | int num_cmd = 0; |
| 62 | |
| 63 | PALETTE pal; |
| 64 | BITMAP *backbmp; |
| 65 | |
| 66 | while (1) |
| 67 | { |
| 68 | static struct option long_options[] = |
| 69 | { |
| 70 | /* These options set a flag. */ |
| 71 | {"verbose", no_argument, &verbose_flag, 1}, |
| 72 | {"help", no_argument, &help_flag, 1}, |
| 73 | {"mouse", no_argument, &mouse_flag, 1}, |
| 74 | {"run", no_argument, &run_flag, 1}, |
| 75 | /* These options don't set a flag. |
| 76 | We distinguish them by their indices. */ |
| 77 | {"title", required_argument, 0, 't'}, |
| 78 | {"path", required_argument, 0, 'p'}, |
| 79 | {"filter", required_argument, 0, 'f'}, |
| 80 | {"wallpaper", required_argument, 0, 'w'}, |
| 81 | {0, 0, 0, 0} |
| 82 | }; |
| 83 | /* `getopt_long' stores the option index here. */ |
| 84 | int option_index = 0; |
| 85 | |
| 86 | c = getopt_long (argc, argv, "t:p:f:w:mrh", |
| 87 | long_options, &option_index); |
| 88 | |
| 89 | /* Detect the end of the options. */ |
| 90 | if (c == -1) |
| 91 | break; |
| 92 | |
| 93 | switch (c) |
| 94 | { |
| 95 | case 0: |
| 96 | /* If this option set a flag, do nothing else now. */ |
| 97 | if (long_options[option_index].flag != 0) |
| 98 | break; |
| 99 | break; |
| 100 | |
| 101 | case 't': |
| 102 | title = optarg; |
| 103 | break; |
| 104 | |
| 105 | case 'p': |
| 106 | init_path = optarg; |
| 107 | break; |
| 108 | |
| 109 | case 'f': |
| 110 | filter = optarg; |
| 111 | break; |
| 112 | |
| 113 | case 'w': |
| 114 | wallpaper = optarg; |
| 115 | break; |
| 116 | |
| 117 | case '?': |
| 118 | /* `getopt_long' already printed an error message. */ |
| 119 | print_help(); |
| 120 | return 1; |
| 121 | |
| 122 | default: |
| 123 | abort (); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (help_flag) |
| 128 | { |
| 129 | print_help(); |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /* Print any remaining command line arguments (not options). */ |
| 135 | if (optind < argc) |
| 136 | { |
| 137 | run_flag = 1; |
| 138 | cmd = &argv[optind]; |
| 139 | num_cmd = argc - optind; |
| 140 | printf ("non-option ARGV-elements: "); |
| 141 | while (optind < argc) |
| 142 | printf ("%s ", argv[optind++]); |
| 143 | putchar ('\n'); |
| 144 | } |
| 145 | |
| 146 | if (allegro_init() != 0) |
| 147 | return 1; |
| 148 | install_keyboard(); |
| 149 | if (mouse_flag) |
| 150 | install_mouse(); |
| 151 | install_timer(); |
| 152 | loadpng_init(); |
| 153 | jpgalleg_init(); |
| 154 | |
| 155 | if (set_gfx_mode(GFX_SAFE, 320, 240, 0, 0) != 0) { |
| 156 | set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); |
| 157 | allegro_message("Unable to set any graphic mode\n%s\n", allegro_error); |
| 158 | return 1; |
| 159 | } |
| 160 | |
| 161 | backbmp = load_bitmap(wallpaper, pal); |
| 162 | if (backbmp) |
| 163 | { |
| 164 | if (bitmap_color_depth(backbmp) == 8) |
| 165 | set_palette(pal); |
| 166 | |
| 167 | /* can't stretch_blit() between color depths, so doing the stretching on |
| 168 | a memory bitmap of matching depth, then blit to the screen */ |
| 169 | BITMAP *buffer = create_bitmap_ex(bitmap_color_depth(backbmp), |
| 170 | SCREEN_W, SCREEN_H); |
| 171 | |
| 172 | if (!buffer) |
| 173 | { |
| 174 | set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); |
| 175 | allegro_message("Unable to allocate bitmap\n%s\n", allegro_error); |
| 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | stretch_blit(backbmp, buffer, 0, 0, backbmp->w, backbmp->h, |
| 180 | 0, 0, SCREEN_W, SCREEN_H); |
| 181 | blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); |
| 182 | destroy_bitmap(buffer); |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | /* couln't load file (maybe default filename not present...?) */ |
| 187 | clear_to_color(screen, makecol(0,0,0)); |
| 188 | } |
| 189 | |
| 190 | set_palette(pal); |
| 191 | |
| 192 | gui_fg_color = makecol(230,210,140); |
| 193 | gui_bg_color = makecol(30,40,50); |
| 194 | gui_mg_color = makecol(140,150,160); |
| 195 | |
| 196 | if (!init_path) |
| 197 | getcwd(path, sizeof(path)); |
| 198 | else |
| 199 | strncpy(path, init_path, sizeof(path)); |
| 200 | |
| 201 | path[sizeof(path)-1] = '\0'; |
| 202 | |
| 203 | |
| 204 | |
| 205 | int ok = file_select_ex( |
| 206 | title, path, filter, sizeof(path), SCREEN_W-2*hborder, SCREEN_H-2*vborder); |
| 207 | path[sizeof(path)-1] = '\0'; |
| 208 | |
| 209 | allegro_exit(); |
| 210 | |
| 211 | if (ok) |
| 212 | { |
| 213 | printf ("%s", path); |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | return 1; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /* The following comments configure the Emacs editor. Just ignore them. |
| 222 | * Local Variables: |
| 223 | * compile-command: "make -C ~/h/src/qi/openwrt-xburst package/alfilesel/compile -j2 V=99" |
| 224 | * End: |
| 225 | */ |