コード例 #1
0
ファイル: menu_load.cpp プロジェクト: kerheol/uae4all
static int menuLoadLoop(char *curr_path)
{
	char *ret = NULL, *fname = NULL;
	struct dirent **namelist;
	DIR *dir;
	int n, sel = 0;
	unsigned long inp = 0;

	min_in_dir=0;
	max_in_dir=SHOW_MAX_FILES;

	// is this a dir or a full path?

	if ((dir = opendir(curr_path))) closedir(dir);
	else
	{
		char *p;
		for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);
		*p = 0;
		fname = p+1;
	}


	n = scandir(curr_path, &namelist, scandir_filter, scandir_cmp);


	if (n < 0)
	{
		// try root
		n = scandir("/", &namelist, scandir_filter, scandir_cmp);
		if (n < 0)
		{
			// oops, we failed
			printf("dir: "); printf(curr_path); printf("\n");
			perror("scandir");
			return 0;
		}
	}

	if (n<10) usleep(70*1024);
	else usleep(40*1024);
	// try to find sel
	if (fname != NULL)
	{
		//SDL_GP2X_VSync();
		int i;
		for (i = 1; i < n; i++)
		{
			if (strcmp(namelist[i]->d_name, fname) == 0)
			{
				sel = i - 1;
				break;
			}
		}
	}

	int loaded=0;
	int delay=0;
	for (;;)
	{
		unsigned long keys;

		draw_dirlist(curr_path, namelist, n, sel);

		delay ++;
		if (delay>3)
		{
			delay=0;
			keys = gp2x_joystick_read(0);
		}
		else keys=0;


		if(keys&GP2X_UP)  { sel--;   if (sel < 0)   sel = n-2; /*usleep(10*1024);*/ }
		if(keys&GP2X_DOWN)  { sel++;   if (sel > n-2) sel = 0;/*usleep(10*1024);*/}
		if(keys&GP2X_LEFT)  { sel-=10; if (sel < 0)   sel = 0;/*usleep(10*1024);*/}
		if(keys&GP2X_L)     { sel-=24; if (sel < 0)   sel = 0;/*usleep(10*1024);*/}
		if(keys&GP2X_RIGHT) { sel+=10; if (sel > n-2) sel = n-2;/*usleep(10*1024);*/}
		if(keys&GP2X_R)     { sel+=24; if (sel > n-2) sel = n-2;/*usleep(10*1024);*/}
#if !defined(GCW0)
		if(keys&GP2X_SELECT)     { run_menuFileinfo(namelist[sel+1]->d_name);}
#endif
		if(keys&GP2X_B || ((keys&GP2X_Y) && nr_drives>1) || ((keys&GP2X_A) && nr_drives>2) || ((keys&GP2X_X && nr_drives==4)))
		{
			if (namelist[sel+1]->d_type == DT_REG)
			{
				int df;
				int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;
				char *p;
				char *filename;
				filename=(char*)malloc(newlen);
				strcpy(filename, curr_path);
				p = filename + strlen(filename) - 1;
				while (*p == '/' && p >= filename) *p-- = 0;
				strcat(filename, "/");
				strcat(filename, namelist[sel+1]->d_name);
				printf("Selecting file %s\n",filename);
				if (keys&GP2X_B){strcpy(uae4all_image_file0,filename);df=0;}
				else if(keys&GP2X_Y) {strcpy(uae4all_image_file1,filename);df=1;}
				else if(keys&GP2X_A) {strcpy(uae4all_image_file2,filename);df=2;}
				else if(keys&GP2X_X) {strcpy(uae4all_image_file3,filename);df=3;}
				printf("DF0 %s\n",uae4all_image_file0);
				loaded=1;
				strcpy(currentDir,filename);
				free(filename);
				break;
			}
			else if (namelist[sel+1]->d_type == DT_DIR)
			{
				int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;
				char *p;
				char *newdir;
				newdir=(char*)malloc(newlen);
				if (strcmp(namelist[sel+1]->d_name, "..") == 0)
				{
					char *start = curr_path;
					p = start + strlen(start) - 1;
					while (*p == '/' && p > start) p--;
					while (*p != '/' && p > start) p--;
					if (p <= start) strcpy(newdir, "/");
					else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }
				}
				else
				{
					strcpy(newdir, curr_path);
					p = newdir + strlen(newdir) - 1;
					while (*p == '/' && p >= newdir) *p-- = 0;
					strcat(newdir, "/");
					strcat(newdir, namelist[sel+1]->d_name);
				}
				strcpy(currentDir,newdir);
				loaded = menuLoadLoop(newdir);
				free(newdir);
				break;
			}
		}
	if(keys&GP2X_START) break;
	}
	if (n > 0)
	{
		while(n--) free(namelist[n]);
		free(namelist);
	}

	return loaded;
}
コード例 #2
0
ファイル: menu_load.cpp プロジェクト: CypherXG/UAE4Droid
static int menuLoadLoop(char *curr_path)
{
	char *ret = NULL, *fname = NULL;
	struct dirent **namelist;
	DIR *dir;
	int n, sel = 0;
	int sel_last = 0;
	unsigned long inp = 0;

	min_in_dir=0;
	max_in_dir=SHOW_MAX_FILES;

	// is this a dir or a full path?
	if ((dir = opendir(curr_path)))
		closedir(dir);
	else 
	{
		char *p;
		for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);
		*p = 0;
		fname = p+1;
	}

#ifdef ANDROIDSDL
	n = scandir(curr_path, &namelist, scandir_filter, 0);
#else	
	n = scandir(curr_path, &namelist, scandir_filter, scandir_cmp);
#endif
	
	if (n < 0) 
	{
		// try root
#ifdef ANDROIDSDL
		n = scandir("/", &namelist, scandir_filter, 0);
#else
		n = scandir("/", &namelist, scandir_filter, scandir_cmp);
#endif
		if (n < 0) 
		{
			// oops, we failed
			printf("dir: "); printf(curr_path); printf("\n");
			perror("scandir");
			return 0;
		}
	}
	if (n<10) usleep(70*1024);
	else usleep(40*1024);
	// try to find sel
	if (fname != NULL) 
	{
		int i;
		for (i = 1; i < n; i++) 
		{
			if (strcmp(namelist[i]->d_name, fname) == 0) 
			{
				sel = i - 1;
				break;
			}
		}
	}

	int loaded=0;
	int delay=0;
	SDL_Event event;
	int left=0, right=0, up=0, down=0, hit0=0, hit1=0, hit2=0, hit3=0, hit4=0, hitL=0;
	while(hit0+hit1+hitL==0)
	{
		//unsigned long keys;
		draw_dirlist(curr_path, namelist, n, sel);
		delay ++;
		left=right=up=down=hit0=hit1=hit2=hit3=hit4=hitL=0;
		while (SDL_PollEvent(&event) > 0 && hit0+hit1+hitL==0)
		{
			if (event.type == SDL_KEYDOWN)
			{
				uae4all_play_click();
				switch(event.key.keysym.sym)
				{
					case SDLK_RIGHT: right=1; break;
					case SDLK_LEFT: left=1; break;
					case SDLK_UP: up=1; break;
					case SDLK_DOWN: down=1; break;
					case SDLK_PAGEDOWN: hit0=1; break;
					case SDLK_HOME: hit0=1; break;
					case SDLK_LALT: hit1=1; break;
					case SDLK_LCTRL: hit2=1; break;
					case SDLK_RSHIFT: hit3=1; break;
					case SDLK_RCTRL: hit4=1; break;
					case SDLK_END: hit0=1; break;
					case SDLK_PAGEUP: hit0=1; break;
					case SDLK_l: hitL=1;
				}
			}
			if(up)  { sel--;   if (sel < 0)   sel = n-2; /*usleep(10*1024);*/ }
			if(down)  { sel++;   if (sel > n-2) sel = 0;/*usleep(10*1024);*/}
			if(left)  { sel-=10; if (sel < 0)   sel = 0;/*usleep(10*1024);*/}
			if(hit3)     { sel-=24; if (sel < 0)   sel = 0;/*usleep(10*1024);*/}
			if(right) { sel+=10; if (sel > n-2) sel = n-2;/*usleep(10*1024);*/}
			if(hit4)     { sel+=24; if (sel > n-2) sel = n-2;/*usleep(10*1024);*/}
			if(hit2)     { run_menuFileinfo(namelist[sel+1]->d_name);}
			if(hit0 || hitL)
			{
				if (namelist[sel+1]->d_type == DT_REG) 
				{
					int df;
					int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;
					char *p; 
					char *filename;
					filename=(char*)malloc(newlen);
					strcpy(filename, curr_path);
					p = filename + strlen(filename) - 1;
					while (*p == '/' && p >= filename) *p-- = 0;
					strcat(filename, "/");
					strcat(filename, namelist[sel+1]->d_name);
					printf("Selecting file %s\n",filename);
					switch (menu_load_type)
					{
						case MENU_LOAD_FLOPPY:
							if (current_drive==0){strcpy(uae4all_image_file0,filename);extractFileName(uae4all_image_file0, filename0);df=0;}
							else if(current_drive==1) {strcpy(uae4all_image_file1,filename);extractFileName(uae4all_image_file1, filename1);df=1;}
							else if(current_drive==2) {strcpy(uae4all_image_file2,filename);extractFileName(uae4all_image_file2, filename2);df=2;}
							else if(current_drive==3) {strcpy(uae4all_image_file3,filename);extractFileName(uae4all_image_file3, filename3);df=3;}
							printf("DF0 %s\n",uae4all_image_file0);
							break;
						case MENU_LOAD_HDF:
							if (strstr(filename, ".hdf") == NULL)
								showWarning("HDF file must be selected");
							else
								strcpy(uae4all_hard_file, filename);
							break;
					}
					loaded=1;
					strcpy(currentDir,filename);
					free(filename);
					break;
				}
				else if (namelist[sel+1]->d_type == DT_DIR)
				{
					int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;
					char *p;
					char *newdir;
					/* Hard file dir is being selected ? (L-key is used to select dir) */
					if ((menu_load_type == MENU_LOAD_HD_DIR) && hitL)
					{
						strcpy(uae4all_hard_dir, curr_path);
						strcat(uae4all_hard_dir, "/");
						strcat(uae4all_hard_dir, namelist[sel+1]->d_name);
						loaded = 1;
						break;
					}
					else
					{
						newdir=(char*)malloc(newlen);
						if (strcmp(namelist[sel+1]->d_name, "..") == 0) 
						{
							char *start = curr_path;
							p = start + strlen(start) - 1;
							while (*p == '/' && p > start) p--;
							while (*p != '/' && p > start) p--;
							if (p <= start) strcpy(newdir, "/");
							else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }
						} 
						else 
						{
							strcpy(newdir, curr_path);
							p = newdir + strlen(newdir) - 1;
							while (*p == '/' && p >= newdir) *p-- = 0;
							strcat(newdir, "/");
							strcat(newdir, namelist[sel+1]->d_name);
						}
						strcpy(currentDir,newdir);
						loaded = menuLoadLoop(newdir);
						free(newdir);
						break;
					}
				} 
			}
			if(hit1)
				break;
		}
	}
	if (n > 0) 
	{
		while(n--) free(namelist[n]);
		free(namelist);
	}

	return loaded;
}