git @ Cat's Eye Technologies linapple / master src / DiskChoose.cpp
master

Tree @master (Download .tar.gz)

DiskChoose.cpp @masterraw · history · blame

  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
/*
////////////////////////////////////////////////////////////////////////////
////////////  Choose disk image for given slot number? ////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
//// Adapted for linapple - apple][ emulator for Linux by beom beotiger Nov 2007   /////
///////////////////////////////////////////////////////////////////////////////////////
// Original source from one of Brain Games (http://www.braingames.getput.com)
//  game Super Transball 2.(http://www.braingames.getput.com/stransball2/default.asp)
//
// Brain Games crew creates brilliant retro-remakes! Please visit their site to find out more.
//
*/
#ifdef _WIN32
#include "windows.h"
#else
#include <stddef.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include "ctype.h"
#endif

#include "stdafx.h"

#include "list.h"

#include "DiskChoose.h"
#include <errno.h>
//#include "ctype.h"

//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
//#include "SDL/SDL.h"
//#include "SDL_mixer.h"
//#include "SDL_image.h"

/*


#include "auxiliar.h"

#include "tiles.h"
#include "maps.h"
#include "transball.h"

#include "encoder.h"
*/



// how many file names we are able to see at once!
#define FILES_IN_SCREEN    21

// delay after key pressed (in milliseconds??)
#define KEY_DELAY    25

#define MAX_FILENAME    36

/////////////////////////////////////////////////////////////////////
/* FONT prev decls */
//bool fonts_initialization(void);
//void font_print(int x,int y,char *text,SDL_Surface *surface);
//void font_print_centered(int x,int y, char *text, SDL_Surface *surface);

/////////////////////////////////////////////////////////////////////
/* AUX funcs prev decls */
//void rectangle(SDL_Surface *surface, int x, int y, int w, int h, Uint32 pixel);
//void surface_fader(SDL_Surface *surface,float r_factor,float g_factor,float b_factor,float a_factor,SDL_Rect *r);


////////////////////////////////////////////////////////////////////////////////////////
#ifndef _WIN32
int getstat(char *catalog, char *fname, int * size)
{
  // gets file status and returns: 0 - special or error, 1 - file is a directory, 2 - file is a normal file
  // In: catalog - working directory, fname - file name
  struct stat info;
  char tempname[MAX_PATH];

  snprintf(tempname, MAX_PATH, "%s/%s", catalog, fname);  // get full path for the file
  if(stat(tempname, &info) == -1) return 0;
  if(S_ISDIR(info.st_mode)) return 1;  // seems to be directory
  if(S_ISREG(info.st_mode)) {
    if(size != NULL)
            *size = (int)(info.st_size / 1024);  // get file size in Kbytes?!
    return 2;  // regular file
  }

  return 0;
}
#endif
////////////////////////////////////////////////////////////////////////////////////////
bool ChooseAnImage(int sx,int sy, char *incoming_dir, int slot, char **filename, bool *isdir, int *index_file)
{
/*  Parameters:
 sx, sy - window size,
 incoming_dir - in what dir find files,
 slot - in what slot should an image go (common: #6 for 5.25' 140Kb floppy disks, and #7 for hard-disks).
   slot #5 - for 800Kb floppy disks, but we do not use them in Apple][?
  (They are as a rule with .2mg extension)
 index_file  - from which file we should start cursor (should be static and 0 when changing dir)

 Out:  filename  - chosen file name (or dir name)
  isdir    - if chosen name is a directory
*/
  /* Surface: */
  SDL_Surface *my_screen;  // for background

  if(font_sfc == NULL)
    if(!fonts_initialization()) return false;  //if we don't have a fonts, we just can do none

  List<char> files;    // our files
  List<char> sizes;    // and their sizes (or 'dir' for directories)

  int act_file;    // current file
  int first_file;    // from which we output files

  printf("Disckchoose! We are here: %s\n",incoming_dir);
//  files.Delete();
//  sizes.Delete();
#ifndef _WIN32
/* POSIX specific routines of reading directory structure */
    DIR *dp;

    dp = opendir (incoming_dir);  // open and read incoming directory
    char *tmp;

    int i;  // for cycles, beginning and end of list

// build prev dir
  if(strcmp(incoming_dir, "/")) {
#if 0  
    files.Add("..");
    sizes.Add("<UP>");  // add sign of directory
#else
    tmp = new char[3];
    strcpy(tmp, "..");
    files.Add(tmp);
    tmp = new char[5];
    strcpy(tmp, "<UP>");
    sizes.Add(tmp);  // add sign of directory
#endif    
  }
    if (dp != NULL)
    {
            {
                struct dirent **namelist;
                int n;

                n = scandir(incoming_dir, &namelist, NULL, alphasort);
                if (n < 0)
                    perror("scandir");
                else {
                    i = 0;
                    while (i < n) {
                        int what = getstat(incoming_dir, namelist[i]->d_name, NULL);
                        if (namelist[i]->d_name && strlen(namelist[i]->d_name) > 0 &&
                            namelist[i]->d_name[0] != '.' && what == 1) // is directory!
                        {
                            tmp = new char[strlen(namelist[i]->d_name)+1];  // add this entity to list
                            strcpy(tmp, namelist[i]->d_name);
                            files.Add(tmp);
                            tmp = new char[6];
                            strcpy(tmp, "<DIR>");
                            sizes.Add(tmp);  // add sign of directory
                        }
                        i++;
                    }
                    free(namelist);
               }
            }
    }
#else
/* Windows specific functions of reading directory structure */
    /* Find subdirs: */
  if(strcmp(incoming_dir, "/")) {
  // we are not in upper direcory
    tmp = new char[3];
    strcpy(tmp, "..");
    files.Add(tmp);
    tmp = new char[5];
    strcpy(tmp, "<UP>");
    sizes.Add(tmp);  // add sign of directory
  }


    WIN32_FIND_DATA finfo;
    HANDLE h;



    h=FindFirstFile(incoming_dir,&finfo);

    if (h!=INVALID_HANDLE_VALUE) {
      char *tmp;
      if(finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0) {
      // add this entry if it is directory
        tmp=new char[strlen(finfo.cFileName)+1];
        strcpy(tmp,finfo.cFileName);
        files.Add(tmp);
        tmp = new char[6];
        strcpy(tmp, "<DIR>");
        sizes.Add(tmp);  // add sign of directory
      }
      while(FindNextFile(h,&finfo)==TRUE) {
        if(finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0) {
        // add this entry if it is directory
          tmp=new char[strlen(finfo.cFileName)+1];
          strcpy(tmp,finfo.cFileName);
          files.Add(tmp);
          tmp = new char[6];
          strcpy(tmp, "<DIR>");
          sizes.Add(tmp);  // add sign of directory
        }
      } /* while */
    } /* if */

#endif
#ifndef _WIN32
/* POSIX specific routines of reading directory structure */

      if( dp ) {
        rewinddir (dp);  // to the start
              {
                  struct dirent **namelist;
                  int n, fsize = 0;

                  n = scandir(incoming_dir, &namelist, NULL, alphasort);
                  if (n < 0)
                      perror("scandir");
                  else {
                      int i = 0;
                      while (i < n) {
                          if (strlen(namelist[i]->d_name) > 4 && namelist[i]->d_name[0] != '.'
                              && (getstat(incoming_dir, namelist[i]->d_name, &fsize) == 2)) // is normal file!
                          {
                              tmp = new char[strlen(namelist[i]->d_name)+1];  // add this entity to list
                              strcpy(tmp, namelist[i]->d_name);
                              files.Add(tmp);
                              tmp = new char[10];  // 1400000KB
                              if (1000 > fsize) {
                                  snprintf(tmp, 9, "%4dK", fsize);
                              } else if (1000000 > fsize) {
                                  snprintf(tmp, 9, "%4dM", (int) (fsize / 1000));
                              } else {
                                  snprintf(tmp, 9, "%4dG", (int) (fsize / 1000000));
                              }
                              sizes.Add(tmp);  // add this size to list
                          }
                          i++;
                      }
                      free(namelist);
                 }
              }

        closedir (dp);
      }
#else
/* Windows specific functions of reading directory structure */
    /* Find files: */

    h=FindFirstFile(incoming_dir,&finfo);

    if (h!=INVALID_HANDLE_VALUE) {
//      char *tmp; - must be defined in previous section, when searching subdirs
      if(finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY == 0) {
      // add this entry if it is NOT directory!
        tmp=new char[strlen(finfo.cFileName)+1];
        strcpy(tmp,finfo.cFileName);
        files.Add(tmp);
        tmp = new char[10];  // 1400000KB
        snprintf(tmp, 9, "%dKB",
          ((finfo.nFileSizeHigh * (MAXDWORD+1)) + finfo.nFileSizeLow));
        sizes.Add(tmp);  // add this size to list
      }
      while(FindNextFile(h,&finfo)==TRUE) {
        if(finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY == 0) {
        // add this entry if it is NOT directory!
          tmp=new char[strlen(finfo.cFileName)+1];
          strcpy(tmp,finfo.cFileName);
          files.Add(tmp);
          tmp = new char[10];  // 1400000KB
          snprintf(tmp, 9, "%dKB",
            ((finfo.nFileSizeHigh * (MAXDWORD+1)) + finfo.nFileSizeLow));
          sizes.Add(tmp);  // add this size to list
        }
      } /* while */
    } /* if */

#endif
// do sorting for files

//  Count out cursor position and file number output
  act_file = *index_file;
  if(act_file >= files.Length()) act_file = 0;    // cannot be more than files in list
  first_file = act_file - (FILES_IN_SCREEN / 2);
  if (first_file < 0) first_file = 0;  // cannot be negativ...

// Show all directories (first) and files then
//  char *tmp;
  char *siz = NULL;
//  int i;

// prepare screen
    double facx = double(g_ScreenWidth) / double(SCREEN_WIDTH);
    double facy = double(g_ScreenHeight) / double(SCREEN_HEIGHT);

  SDL_Surface *tempSurface = NULL;
  if(!g_WindowResized) {
    if(g_nAppMode == MODE_LOGO) tempSurface = g_hLogoBitmap;  // use logobitmap
      else tempSurface = g_hDeviceBitmap;
  }
  else tempSurface = g_origscreen;

  if(tempSurface == NULL)
    tempSurface = screen;  // use screen, if none available

  my_screen = SDL_CreateRGBSurface(SDL_SWSURFACE, tempSurface->w, tempSurface->h, tempSurface->format->BitsPerPixel, 0, 0, 0, 0);
  if(tempSurface->format->palette && my_screen->format->palette)
    SDL_SetColors(my_screen, tempSurface->format->palette->colors,
            0, tempSurface->format->palette->ncolors);

  surface_fader(my_screen, 0.2F, 0.2F, 0.2F, -1, 0);  // fade it out to 20% of normal
  SDL_BlitSurface(tempSurface, NULL, my_screen, NULL);

  while(true)
  {

    SDL_BlitSurface(my_screen, NULL, screen, NULL);    // show background

    font_print_centered(sx/2 ,5*facy , incoming_dir, screen, 1.5*facx, 1.3*facy);
    if (slot == 6) font_print_centered(sx/2,20*facy,"Choose image for floppy 140KB drive", screen, 1*facx, 1*facy);
    else
      if (slot == 7) font_print_centered(sx/2,20*facy,"Choose image for Hard Disk", screen, 1*facx, 1*facy);
    else
      if (slot == 5) font_print_centered(sx/2,20*facy,"Choose image for floppy 800KB drive", screen, 1*facx, 1*facy);
    else
      if (slot == 1) font_print_centered(sx/2,20*facy,"Select file name for saving snapshot", screen, 1*facx, 1*facy);
    else
      if (slot == 0) font_print_centered(sx/2,20*facy,"Select snapshot file name for loading", screen, 1*facx, 1*facy);

    font_print_centered(sx/2,30*facy, "Press ENTER to choose, or ESC to cancel",screen, 1.4*facx, 1.1*facy);

    files.Rewind();  // from start
    sizes.Rewind();
    i = 0;

//    printf("We've printed some messages, go to file list!\n");
// show all fetched dirs and files
// topX of first fiel visible
    int TOPX  = int(45*facy);

    while(files.Iterate(tmp)) {

      sizes.Iterate(siz);  // also fetch size string

      if (i >= first_file && i < first_file + FILES_IN_SCREEN)
      { // FILES_IN_SCREEN items on screen
//        char tmp2[80],tmp3[256];

        if (i == act_file) { // show item under cursor (in inverse mode)
          SDL_Rect r;
          r.x= 2;
          r.y= TOPX + (i-first_file) * 15 * facy - 1;
          if(strlen(tmp) > 46) r.w = 46 * FONT_SIZE_X /* 6 */ * 1.7 * facx + 2;
             else r.w= strlen(tmp) * FONT_SIZE_X /* 6 */ * 1.7 * facx + 2;  // 6- FONT_SIZE_X
          r.h= 9 * 1.5 * facy;
          SDL_FillRect(screen, &r, SDL_MapRGB(screen->format,255,0,0));// in RED
        } /* if */

        // print file name with enlarged font
        char ch;
        ch = 0;
        if(strlen(tmp) > MAX_FILENAME) { ch = tmp[MAX_FILENAME]; tmp[MAX_FILENAME] = 0;} //cut-off too long string
        font_print(4, TOPX + (i - first_file) * 15 * facy, tmp, screen, 1.7*facx, 1.5*facy); // show name
        font_print(sx - 70 * facx, TOPX + (i - first_file) * 15 * facy, siz, screen, 1.7*facx, 1.5*facy);// show info (dir or size)
        if(ch) tmp[MAX_FILENAME] = ch; //restore cut-off char

      } /* if */
      i++;    // next item
    } /* while */

/////////////////////////////////////////////////////////////////////////////////////////////
// draw rectangles
  rectangle(screen, 0, TOPX - 5, sx, 320*facy, SDL_MapRGB(screen->format, 255, 255, 255));
  rectangle(screen, 480*facx, TOPX - 5, 0, 320*facy, SDL_MapRGB(screen->format, 255, 255, 255));

  SDL_Flip(screen);  // show the screen
  SDL_Delay(KEY_DELAY);  // wait some time to be not too fast

  //////////////////////////////////
  // Wait for keypress
  //////////////////////////////////
  SDL_Event event;  // event
  Uint8 *keyboard;  // key state

  event.type = 0;
  while(event.type != SDL_KEYDOWN) {  // wait for key pressed
        // GPH Honor quit even if we're in the diskchoose state.
        if (SDL_QUIT == event.type) {
      files.Delete();
      sizes.Delete();
      SDL_FreeSurface(my_screen);

        SDL_PushEvent(&event);// push quit event
            return false;
        }
    SDL_Delay(10);
    SDL_PollEvent(&event);
  }

// control cursor
    keyboard = SDL_GetKeyState(NULL);  // get current state of pressed (and not pressed) keys

    if (keyboard[SDLK_UP] || keyboard[SDLK_LEFT]) {
      if (act_file>0) act_file--;  // up one position
      if (act_file<first_file) first_file=act_file;
    } /* if */

    if (keyboard[SDLK_DOWN] || keyboard[SDLK_RIGHT]) {
      if (act_file < (files.Length() - 1)) act_file++;
      if (act_file >= (first_file + FILES_IN_SCREEN)) first_file=act_file - FILES_IN_SCREEN + 1;
    } /* if */

    if (keyboard[SDLK_PAGEUP]) {
      act_file-=FILES_IN_SCREEN;
      if (act_file<0) act_file=0;
      if (act_file<first_file) first_file=act_file;
    } /* if */

    if (keyboard[SDLK_PAGEDOWN]) {
      act_file+=FILES_IN_SCREEN;
      if (act_file>=files.Length()) act_file=(files.Length()-1);
      if (act_file>=(first_file+FILES_IN_SCREEN)) first_file=act_file-FILES_IN_SCREEN + 1;
    } /* if */

    // choose an item?
    if (keyboard[SDLK_RETURN]) {
      // dup string from selected file name
      *filename = strdup(files[act_file]);
//      printf("files[act_file]=%s, *filename=%s\n\n", files[act_file], *filename);
      if(!strcmp(sizes[act_file], "<DIR>") || !strcmp(sizes[act_file], "<UP>"))
             *isdir = true;
        else *isdir = false;  // this is directory (catalog in Apple][ terminology)
      *index_file = act_file;  // remember current index
      files.Delete();
      sizes.Delete();
      SDL_FreeSurface(my_screen);

      return true;
    } /* if */

    if (keyboard[SDLK_ESCAPE]) {
      files.Delete();
      sizes.Delete();
      SDL_FreeSurface(my_screen);

      return false;    // ESC has been pressed
    } /* if */

    if (keyboard[SDLK_HOME]) {  // HOME?
      act_file=0;
      first_file=0;
    } /* if */
    if (keyboard[SDLK_END]) {  // END?
      act_file=files.Length() - 1;  // go to the last possible file in list
      first_file=act_file - FILES_IN_SCREEN + 1;
      if(first_file < 0) first_file = 0;
    } /* if */

  }
} /* ChooseAnImage */