memfile_t *memfile_open_memory(u8 *data,u32 size) { memfile_t *ret; ret = memfile_create(); ret->size = size; ret->data = (u8*)mem_dup(data,size); return(ret); }
int movie_load(char *filename) { memfile_t *file; u32 size; u8 flags; int devid[3]; char ident[5]; movie_unload(); if((file = memfile_open(filename,"rb")) == 0) { log_printf("movie_load: error opening movie '%s'\n",filename); return(1); } //need header and stuff here memfile_read(ident,1,4,file); if(memcmp(ident,movie_ident,4) != 0) { log_printf("movie_load: bad movie ident\n"); memfile_close(file); return(2); } //read movie flags memfile_read(&flags,1,sizeof(u8),file); if(flags & 1) nes->movie.mode |= MOVIE_TEST; //set required input devices memfile_read(&devid[0],1,sizeof(int),file); memfile_read(&devid[1],1,sizeof(int),file); memfile_read(&devid[2],1,sizeof(int),file); nes_set_inputdev(0,devid[0]); nes_set_inputdev(1,devid[1]); nes_set_inputdev(2,devid[2]); //read movie data memfile_read(&nes->movie.startframe,1,sizeof(u32),file); memfile_read(&nes->movie.endframe,1,sizeof(u32),file); memfile_read(&nes->movie.crc32,1,sizeof(u32),file); memfile_read(&nes->movie.len,1,sizeof(u32),file); log_printf("movie_load: start, end = %d, %d :: len = %d bytes\n",nes->movie.startframe,nes->movie.endframe,nes->movie.len); nes->movie.data = (u8*)mem_alloc(nes->movie.len); memfile_read(nes->movie.data,1,nes->movie.len,file); size = memfile_size(file) - memfile_tell(file); nes->movie.state = memfile_create(); memfile_copy(nes->movie.state,file,size); memfile_close(file); return(0); }
memfile_t *memfile_open(char *filename,char *mode) { FILE *fp; memfile_t *ret = 0; int n; //try to open the file if((fp = fopen(filename,mode)) == 0) { log_printf("memfile_open: error opening '%s'\n",filename); return(0); } //create new file ret = memfile_create(); //copy filename and mode ret->filename = mem_strdup(filename); ret->mode = mem_strdup(mode); //save file handle ret->handle = (void*)fp; //get file size fseek(fp,0,SEEK_END); ret->size = (u32)ftell(fp); fseek(fp,0,SEEK_SET); //allocate file data and read the file into it ret->data = (u8*)mem_alloc(ret->size); if((n = fread(ret->data,1,ret->size,fp)) != ret->size) { log_printf("memfile_open: error reading file, wanted %d bytes but read %d\n",ret->size,n); memfile_close(ret); return(0); } //if append mode, seek to end if(strchr(mode,'a')) { memfile_seek(ret,0,SEEK_END); } //initialize the current position and changed var ret->curpos = 0; ret->changed = 0; return(ret); }
//initialize movie data for recording int movie_record() { //get rid of previous data if(nes->movie.data) movie_unload(); //initialize the movie struct nes->movie.mode &= ~7; nes->movie.mode |= MOVIE_RECORD; nes->movie.pos = 0; nes->movie.len = MOVIE_PREALLOC_SIZE; nes->movie.data = (u8*)mem_alloc(nes->movie.len); nes->movie.startframe = nes->ppu.frames; //save state here for loading nes->movie.state = memfile_create(); state_save(nes->movie.state); return(0); }
shm_buffer_t* shm_buffer_create(uint32_t width, uint32_t height, format_t fmt, struct wl_shm *shm, const struct wl_buffer_listener *listener) { int8_t bytes = format_get_bytes(&fmt); uint32_t stride = SHM_BUFFER_STRIDE(width, bytes); uint32_t size = stride * height; shm_buffer_t *buffer = calloc(1, sizeof(shm_buffer_t)); int fd = memfile_create(size); if (fd < 0) return NULL; buffer->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (buffer->data == MAP_FAILED) { close(fd); return NULL; } buffer->shm_pool = wl_shm_create_pool(shm, fd, size); buffer->buffer = wl_shm_pool_create_buffer(buffer->shm_pool, 0, width, height, stride, fmt.wl_format); wl_buffer_add_listener(buffer->buffer, listener, buffer); buffer->fd = fd; buffer->height = height; buffer->stride = stride; buffer->format = fmt; buffer->bytes = bytes; buffer->pool_size = size; buffer->pending_height = 0; buffer->pending_width = 0; return buffer; }