示例#1
0
bool
add_spaced_seed(char const * seed_string)
{
  int i;

  seed = (struct seed_type *)
    //xrealloc(seed, sizeof(struct seed_type) * (n_seeds + 1));
    my_realloc(seed, (n_seeds + 1) * sizeof(seed[0]), n_seeds * sizeof(seed[0]),
	       &mem_small, "seed");
  seed[n_seeds].mask[0] = 0x0;
  seed[n_seeds].span = strlen(seed_string);
  seed[n_seeds].weight = strchrcnt(seed_string, '1');

  if (seed[n_seeds].span < 1
      || seed[n_seeds].span > MAX_SEED_SPAN
      || seed[n_seeds].weight < 1
      || (int)strchrcnt(seed_string, '0') != seed[n_seeds].span - seed[n_seeds].weight)
    return false;

  for (i = 0; i < seed[n_seeds].span; i++)
    bitmap_prepend(seed[n_seeds].mask, 1, (seed_string[i] == '1' ? 1 : 0));

  max_seed_span = MAX(max_seed_span, seed[n_seeds].span);
  min_seed_span = MIN(min_seed_span, seed[n_seeds].span);

  n_seeds++;

  avg_seed_span = 0;
  for (i = 0; i < n_seeds; i++)
    avg_seed_span += seed[i].span;
  avg_seed_span = avg_seed_span/n_seeds;

  return true;
}
示例#2
0
int main (int argc, char *argv[]) {
	char *playlist_filename;
	int shuffle = 0;
	int random_play = 0;
	int repeat = 0;
	int slptime = 0;
	FILE *playlist_file;
	char **playlist;
	char *line;
	char *ptr;
	int volume = 80;
	int balance;
	int playlist_size;
	int argnr;
	int current_song = 0;
	int songs = 0;
	int i,x,j,pid;

#ifdef LCD
	lcddev = fopen(LCD_DEV, "w");
	if (lcddev == NULL) {
		fprintf(stderr, "Open failed for %s\n", LCD_DEV);
		return -1;
	}
	fprintf(lcddev, "\f");
	fflush(lcddev);
#else
	lcddev = stdout;
#endif
	atexit(exit_code);
#ifdef KEYPAD
//	keypad = openkeypad(KEYPAD_DEV);
	keypad = open(KEYPAD_DEV, O_RDONLY);
	if (keypad == -1) {
		fprintf(stderr, "Open failed for %s\n",KEYPAD_DEV);
		return -1;
	}
#else
	keypad = 0; // assign stdinput to keypad
	savelocaltermios();
	setlocaltermios();
#endif

	playlist_filename = NULL;
	mp3play_options = NULL;
	playlist_size = PLAYLIST_SIZE;
	i = 0;
	if (argc < 2)
		usage(0);

	// process options
    while ((i = getopt(argc, argv, "?hzvZR@:M:s:")) >= 0) {
        switch (i) {
		case 'z':
			shuffle = 1;
			break;
		case 'Z':
			random_play = 1;
			break;
		case 'R':
			repeat = 1;
			break;
		case 's':
            slptime = atoi(optarg);
			break;
        case '@':
			playlist_filename = (char *) malloc(strlen(optarg) + 1);
			strcpy(playlist_filename, optarg);
            break;
        case 'M':
			line = (char *) malloc(strlen(optarg) + 1);
			strcpy(line, optarg);
			mp3play_option_count = strchrcnt(line, ' ') + 1;
			mp3play_options = (char **) calloc(mp3play_option_count + 7, sizeof(char *));
			mp3play_options[0] = MP3PLAYER;
			ptr = line;
			for (i = 1; i <= mp3play_option_count; i++) {
				mp3play_options[i] = (char *) calloc(1, strlen(line) + 1);
				ptr = strtok(ptr, " ");
				strcpy(mp3play_options[i], ptr);
				ptr = NULL;
			}
			free(line);
			break;
		case 'v':
			verbose = 1;
			break;
        case 'h':
        case '?':
			usage(0);
			break;
		}
	}
	argnr = optind;
	if (!playlist_filename)
	    if (argnr >= argc)
			usage(1);

	if (!mp3play_options) {
		mp3play_options = (char **) calloc(4, sizeof(char *));
		mp3play_options[0] = MP3PLAYER;
		mp3play_option_count = 1;
		mp3play_options[mp3play_option_count] = (char *) malloc(strlen("-l") + 1);
		strcpy(mp3play_options[mp3play_option_count], "-t");
		mp3play_option_count++;
		mp3play_options[mp3play_option_count] = (char *) malloc(strlen("0") + 1);
		strcpy(mp3play_options[mp3play_option_count], "2");
	} else {
		mp3play_option_count++;
		mp3play_options[mp3play_option_count] = (char *) malloc(strlen("-l") + 1);
		strcpy(mp3play_options[mp3play_option_count], "-l");
		mp3play_option_count++;
		mp3play_options[mp3play_option_count] = (char *) malloc(strlen("0") + 1);
		strcpy(mp3play_options[mp3play_option_count], "0");
		mp3play_option_count++;
		mp3play_options[mp3play_option_count] = (char *) malloc(strlen("-l") + 1);
		strcpy(mp3play_options[mp3play_option_count], "-t");
		mp3play_option_count++;
		mp3play_options[mp3play_option_count] = (char *) malloc(strlen("0") + 1);
		strcpy(mp3play_options[mp3play_option_count], "2");
	}

	srandom(time(NULL));
	if (playlist_filename) {
		if (verbose)
		fprintf(stdout, "Reading Playlist from file:%s\n", playlist_filename);
		playlist_file = fopen(playlist_filename, "r");
		if (playlist_file == NULL) {
			fprintf(stderr, "Open failed for playlist file:%s\n", playlist_filename);
			return -1;
		}
		playlist = (char **) calloc(playlist_size, sizeof(char *));
		line = (char *) calloc(1, MAX_LINE + 1);
		if (verbose)
		fprintf(stdout, "Playlist entries:\n");
		while(line != NULL) {
			if (songs >= playlist_size) {
				playlist_size += PLAYLIST_SIZE;
				playlist = (char **) realloc(playlist, playlist_size * sizeof(char *));
			}
			line = fgets(line, MAX_LINE, playlist_file);
			if (*line != '\0') {
				playlist[songs] = (char *) calloc(1, strlen(line) + 1);
				line = strtok(line, "\n");
				playlist[songs] = strcpy(playlist[songs], line);
				if (verbose)
				fprintf(stdout, "[%d] - %s\n",songs, playlist[songs]);
				songs++;
			}
		}
		free(line);
		songs--;
	} else {
		playlist = (char **) calloc(argc, sizeof(char *));
		for (i = 0; i < (argc - argnr) ; i++) {
			playlist[i] = (char *) malloc(strlen(argv[i + argnr]) + 1);
			playlist[i] = strcpy(playlist[i], argv[i + argnr]);
		}
		songs = i - 1;
	}

	if (shuffle) {
		if (verbose)
		fprintf(stdout, "Shuffling.....\n");
        for (x = 0; (x < 10000); x++) {
			i = ((unsigned int) random()) % songs;
			j = ((unsigned int) random()) % songs;
			line = playlist[i];
			playlist[i] = playlist[j];
            playlist[j] = line;
        }
	}
	if (verbose)
	fprintf(stdout, "Init complete - %d songs in playlist\n", songs + 1);
    


	// main menu loop
	current_song = 0;
	pid = 1;
#ifdef KEYPAD
	for (;;) {
#else
	while (input != QUIT) {
#endif
		input = my_getchar(keypad);
		while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) {
			if (pid == mp3playpid) {
				if (verbose)
				fprintf(stdout, "mp3play child died\n");
			    if (lcddev) {
					fprintf(lcddev, "\f");
					fflush(lcddev);
				}
			} else {
				if (verbose)
				fprintf(stdout, "another child died\n");
			}
		}
#ifndef KEYPAD
		if (input == 0x1b) {
			input = my_getchar(keypad);
			input = my_getchar(keypad);
		}
#endif
		switch (input) {
			case UP:
				volume+=10;
				if (volume > 100) {
					volume = 100;
				} else {
					set_volume(volume);
					printtitle(playlist[current_song]);
				}
				input = 'c';
				break;
			case DOWN:
				volume-=10;
				if (volume < 0) {
					volume = 0;
					break;
				} else {
					set_volume(volume);
					printtitle(playlist[current_song]);
				}
				input = 'c';
				break;
			case RIGHT : 
				if (slptime)
					sleep(slptime);
				if (random_play) {
					current_song = ((unsigned int) random()) % songs;
					if (verbose)
					fprintf(stdout, "[Random Song] - %d\n",current_song);
				} else {
					current_song++;
					if (verbose)
					fprintf(stdout, "[Next song]\n");
				}
				if (current_song > songs) {
					if (repeat) {
						current_song = 0;
						play(playlist[current_song]);
					} else {
						current_song--;
					}
				} else
					play(playlist[current_song]);
				input = 'c';
				break;
			case SELECT :
				if (verbose)
				fprintf(stdout, "[Play]\n");
				play(playlist[current_song]);
				input = 'c';
				break;
			case LEFT :
				if (random_play) {
					current_song = ((unsigned int) random()) % songs;
					if (verbose)
					fprintf(stdout, "[Random Song] - %d\n",current_song);
				} else {
					current_song--;
					if (verbose)
					fprintf(stdout, "[Prev song]\n");
				}
				if (current_song < 0) {
					current_song = 0;
				} else
					play(playlist[current_song]);
				input = 'c';
				break;
		   	case EXIT :
				if (verbose)
				fprintf(stdout, "[Stop]\n");
				play(NULL);
				input = 'c';
				break;
			default :
				break;
		}
	}
	play(NULL);
}

/****************************************************************************/
int exec_command(char **argv) {
		int i = 0;
		int pid;

		if (verbose) {
			fprintf(stdout, "executing: ");
			for (i = 0; argv[i] != NULL; i++) {
				fprintf(stdout, "%s ",argv[i]);
			}
			fprintf(stdout, "\n");
		}

		pid = vfork();
		if(pid == 0) {
		//	close(0);
		//	close(1);
		//	close(2);
			execvp(argv[0], argv);
			_exit(0);
		}

		return pid;

}