예제 #1
0
/*
 * grab the guid string from a mapping string
 */
static char *SDL_PrivateGetControllerGUIDFromMappingString(const char *pMapping)
{
    const char *pFirstComma = SDL_strchr(pMapping, ',');
    if (pFirstComma) {
        char *pchGUID = SDL_malloc(pFirstComma - pMapping + 1);
        if (!pchGUID) {
            SDL_OutOfMemory();
            return NULL;
        }
        SDL_memcpy(pchGUID, pMapping, pFirstComma - pMapping);
        pchGUID[pFirstComma - pMapping] = '\0';

        /* Convert old style GUIDs to the new style in 2.0.5 */
#if __WIN32__
        if (SDL_strlen(pchGUID) == 32 &&
            SDL_memcmp(&pchGUID[20], "504944564944", 12) == 0) {
            SDL_memcpy(&pchGUID[20], "000000000000", 12);
            SDL_memcpy(&pchGUID[16], &pchGUID[4], 4);
            SDL_memcpy(&pchGUID[8], &pchGUID[0], 4);
            SDL_memcpy(&pchGUID[0], "03000000", 8);
        }
#elif __MACOSX__
        if (SDL_strlen(pchGUID) == 32 &&
            SDL_memcmp(&pchGUID[4], "000000000000", 12) == 0 &&
            SDL_memcmp(&pchGUID[20], "000000000000", 12) == 0) {
            SDL_memcpy(&pchGUID[20], "000000000000", 12);
            SDL_memcpy(&pchGUID[8], &pchGUID[0], 4);
            SDL_memcpy(&pchGUID[0], "03000000", 8);
        }
#endif
        return pchGUID;
    }
    return NULL;
}
예제 #2
0
static int get_header(SDL_RWops *src, int *w, int *h)
{
    char line[1024];

    *w = 0;
    *h = 0;

    /* Check the header magic */
    if ( (get_line(src, line, sizeof(line)) < 0) ||
         (SDL_memcmp(line, "P7 332", 6) != 0) ) {
        return -1;
    }

    /* Read the header */
    while ( get_line(src, line, sizeof(line)) == 0 ) {
        if ( SDL_memcmp(line, "#BUILTIN:", 9) == 0 ) {
            /* Builtin image, no data */
            break;
        }
        if ( SDL_memcmp(line, "#END_OF_COMMENTS", 16) == 0 ) {
            if ( get_line(src, line, sizeof(line)) == 0 ) {
                SDL_sscanf(line, "%d %d", w, h);
                if ( *w >= 0 && *h >= 0 ) {
                    return 0;
                }
            }
            break;
        }
    }
    /* No image data */
    return -1;
}
예제 #3
0
static int
X11_SafetyNetErrHandler(Display * d, XErrorEvent * e)
{
    SDL_VideoDevice *device = NULL;
    /* if we trigger an error in our error handler, don't try again. */
    if (!safety_net_triggered) {
        safety_net_triggered = SDL_TRUE;
        device = SDL_GetVideoDevice();
        if (device != NULL) {
            int i;
            for (i = 0; i < device->num_displays; i++) {
                SDL_VideoDisplay *display = &device->displays[i];
                if (SDL_memcmp(&display->current_mode, &display->desktop_mode,
                               sizeof (SDL_DisplayMode)) != 0) {
                    X11_SetDisplayMode(device, display, &display->desktop_mode);
                }
            }
        }
    }

    if (orig_x11_errhandler != NULL) {
        return orig_x11_errhandler(d, e);  /* probably terminate. */
    }

    return 0;
}
예제 #4
0
/* Determine the number of CPUs in the system */
static int num_CPU(void)
{
    static int num_cpus = 0;

    if(!num_cpus) {
#if defined(__LINUX__)
        char line[BUFSIZ];
        FILE *pstat = fopen("/proc/stat", "r");
        if ( pstat ) {
            while ( fgets(line, sizeof(line), pstat) ) {
                if (SDL_memcmp(line, "cpu", 3) == 0 && line[3] != ' ') {
                    ++num_cpus;
                }
            }
            fclose(pstat);
        }
#elif defined(__IRIX__)
        num_cpus = sysconf(_SC_NPROC_ONLN);
#elif defined(_SC_NPROCESSORS_ONLN)
        /* number of processors online (SVR4.0MP compliant machines) */
        num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
#elif defined(_SC_NPROCESSORS_CONF)
        /* number of processors configured (SVR4.0MP compliant machines) */
        num_cpus = sysconf(_SC_NPROCESSORS_CONF);
#endif
        if ( num_cpus <= 0 ) {
            num_cpus = 1;
        }
    }
    return num_cpus;
}
예제 #5
0
파일: rwops.c 프로젝트: Bergasms/N64iOS
/**
 * @brief Does a generic rwops test.
 *
 * RWops should have "Hello World!" in it already if write is disabled.
 *
 *    @param write Test writing also.
 *    @return 1 if an assert is failed.
 */
static int rwops_testGeneric( SDL_RWops *rw, int write )
{
   char buf[sizeof(hello_world)];
   int i;

   /* Set to start. */
   i = SDL_RWseek( rw, 0, RW_SEEK_SET );
   if (SDL_ATvassert( i == 0,
            "Seeking with SDL_RWseek (RW_SEEK_SET): got %d, expected %d",
            i, 0 ))
      return 1;

   /* Test write. */
   i = SDL_RWwrite( rw, hello_world, sizeof(hello_world)-1, 1 );
   if (write) {
      if (SDL_ATassert( "Writing with SDL_RWwrite (failed to write)", i == 1 ))
         return 1;
   }
   else {
      if (SDL_ATassert( "Writing with SDL_RWwrite (wrote when shouldn't have)", i <= 0 ))
         return 1;
   }

   /* Test seek. */
   i = SDL_RWseek( rw, 6, RW_SEEK_SET );
   if (SDL_ATvassert( i == 6,
            "Seeking with SDL_RWseek (RW_SEEK_SET): got %d, expected %d",
            i, 0 ))
       return 1;

   /* Test seek. */
   i = SDL_RWseek( rw, 0, RW_SEEK_SET );
   if (SDL_ATvassert( i == 0,
            "Seeking with SDL_RWseek (RW_SEEK_SET): got %d, expected %d",
            i, 0 ))
      return 1;

   /* Test read. */
   i = SDL_RWread( rw, buf, 1, sizeof(hello_world)-1 );
   if (SDL_ATassert( "Reading with SDL_RWread", i == sizeof(hello_world)-1 ))
      return 1;
   if (SDL_ATassert( "Memory read does not match memory written",
            SDL_memcmp( buf, hello_world, sizeof(hello_world)-1 ) == 0 ))
      return 1;

   /* More seek tests. */
   i = SDL_RWseek( rw, -4, RW_SEEK_CUR );
   if (SDL_ATvassert( i == sizeof(hello_world)-5,
            "Seeking with SDL_RWseek (RW_SEEK_CUR): got %d, expected %d",
            i, sizeof(hello_world)-5 ))
      return 1;
   i = SDL_RWseek( rw, -1, RW_SEEK_END );
   if (SDL_ATvassert( i == sizeof(hello_world)-2,
            "Seeking with SDL_RWseek (RW_SEEK_END): got %d, expected %d",
            i, sizeof(hello_world)-2 ))
      return 1;

   return 0;
}
예제 #6
0
int
SDL_DINPUT_MaybeAddDevice(const DIDEVICEINSTANCE * pdidInstance)
{
    HRESULT ret;
    LPDIRECTINPUTDEVICE8 device;
    const DWORD needflags = DIDC_ATTACHED | DIDC_FORCEFEEDBACK;
    DIDEVCAPS capabilities;
    SDL_hapticlist_item *item = NULL;

    if (dinput == NULL) {
        return -1;  /* not initialized. We'll pick these up on enumeration if we init later. */
    }

    /* Make sure we don't already have it */
    for (item = SDL_hapticlist; item; item = item->next) {
        if ((!item->bXInputHaptic) && (SDL_memcmp(&item->instance, pdidInstance, sizeof(*pdidInstance)) == 0)) {
            return -1;  /* Already added */
        }
    }

    /* Open the device */
    ret = IDirectInput8_CreateDevice(dinput, &pdidInstance->guidInstance, &device, NULL);
    if (FAILED(ret)) {
        /* DI_SetError("Creating DirectInput device",ret); */
        return -1;
    }

    /* Get capabilities. */
    SDL_zero(capabilities);
    capabilities.dwSize = sizeof(DIDEVCAPS);
    ret = IDirectInputDevice8_GetCapabilities(device, &capabilities);
    IDirectInputDevice8_Release(device);
    if (FAILED(ret)) {
        /* DI_SetError("Getting device capabilities",ret); */
        return -1;
    }

    if ((capabilities.dwFlags & needflags) != needflags) {
        return -1;  /* not a device we can use. */
    }

    item = (SDL_hapticlist_item *)SDL_calloc(1, sizeof(SDL_hapticlist_item));
    if (item == NULL) {
        return SDL_OutOfMemory();
    }

    item->name = WIN_StringToUTF8(pdidInstance->tszProductName);
    if (!item->name) {
        SDL_free(item);
        return -1;
    }

    /* Copy the instance over, useful for creating devices. */
    SDL_memcpy(&item->instance, pdidInstance, sizeof(DIDEVICEINSTANCE));
    SDL_memcpy(&item->capabilities, &capabilities, sizeof(capabilities));

    return SDL_SYS_AddHapticDevice(item);
}
예제 #7
0
int main(int argc, char *argv[])
{
	const char * formats[] = {
		"UTF8",
		"UTF-8",
		"UTF16BE",
		"UTF-16BE",
		"UTF16LE",
		"UTF-16LE",
		"UTF32BE",
		"UTF-32BE",
		"UTF32LE",
		"UTF-32LE",
		"UCS4",
		"UCS-4",
	};
	char buffer[BUFSIZ];
	char *ucs4;
	char *test[2];
	int i, index = 0;
	FILE *file;
	int errors = 0;

	if ( !argv[1] ) {
		argv[1] = "utf8.txt";
	}
	file = fopen(argv[1], "rb");
	if ( !file ) {
		fprintf(stderr, "Unable to open %s\n", argv[1]);
		return (1);
	}

	while ( fgets(buffer, sizeof(buffer), file) ) {
		
		size_t len;
		ucs4 = SDL_iconv_string("UCS-4", "UTF-8", buffer, SDL_strlen(buffer)+1);
		len = (widelen(ucs4)+1)*4;
		for ( i = 0; i < SDL_arraysize(formats); ++i ) {
			test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
			test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
			if ( !test[1] || SDL_memcmp(test[1], ucs4, len) != 0 ) {
				fprintf(stderr, "FAIL: %s\n", formats[i]);
				++errors;
			}
			if ( test[0] ) {
				SDL_free(test[0]);
			}
			if ( test[1] ) {
				SDL_free(test[1]);
			}
		}
		test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
		SDL_free(ucs4);
		fputs(test[0], stdout);
		SDL_free(test[0]);
	}
	return (errors ? errors + 1 : 0);
}
예제 #8
0
static Uint32 get_colorhash(struct color_hash *hash, const char *key, int cpp)
{
    struct hash_entry *entry = hash->table[hash_key(key, cpp, hash->size)];
    while (entry) {
        if (SDL_memcmp(key, entry->key, cpp) == 0)
            return entry->color;
        entry = entry->next;
    }
    return 0;       /* garbage in - garbage out */
}
예제 #9
0
/* helper function for direct input, gets called for each connected joystick */
static BOOL CALLBACK
EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
{
    JoyStick_DeviceData *pNewJoystick;
    JoyStick_DeviceData *pPrevJoystick = NULL;
    const DWORD devtype = (pdidInstance->dwDevType & 0xFF);

    if (devtype == DI8DEVTYPE_SUPPLEMENTAL) {
        return DIENUM_CONTINUE;  /* Ignore touchpads, etc. */
    }

    if (SDL_IsXInputDevice(&pdidInstance->guidProduct)) {
        return DIENUM_CONTINUE;  /* ignore XInput devices here, keep going. */
    }

    pNewJoystick = *(JoyStick_DeviceData **)pContext;
    while (pNewJoystick) {
        if (!SDL_memcmp(&pNewJoystick->dxdevice.guidInstance, &pdidInstance->guidInstance, sizeof(pNewJoystick->dxdevice.guidInstance))) {
            /* if we are replacing the front of the list then update it */
            if (pNewJoystick == *(JoyStick_DeviceData **)pContext) {
                *(JoyStick_DeviceData **)pContext = pNewJoystick->pNext;
            } else if (pPrevJoystick) {
                pPrevJoystick->pNext = pNewJoystick->pNext;
            }

            pNewJoystick->pNext = SYS_Joystick;
            SYS_Joystick = pNewJoystick;

            return DIENUM_CONTINUE; /* already have this joystick loaded, just keep going */
        }

        pPrevJoystick = pNewJoystick;
        pNewJoystick = pNewJoystick->pNext;
    }

    pNewJoystick = (JoyStick_DeviceData *)SDL_malloc(sizeof(JoyStick_DeviceData));
    if (!pNewJoystick) {
        return DIENUM_CONTINUE; /* better luck next time? */
    }

    SDL_zerop(pNewJoystick);
    pNewJoystick->joystickname = WIN_StringToUTF8(pdidInstance->tszProductName);
    if (!pNewJoystick->joystickname) {
        SDL_free(pNewJoystick);
        return DIENUM_CONTINUE; /* better luck next time? */
    }

    SDL_memcpy(&(pNewJoystick->dxdevice), pdidInstance,
        sizeof(DIDEVICEINSTANCE));

    SDL_memcpy(&pNewJoystick->guid, &pdidInstance->guidProduct, sizeof(pNewJoystick->guid));
    SDL_SYS_AddJoystickDevice(pNewJoystick);

    return DIENUM_CONTINUE; /* get next device, please */
}
예제 #10
0
/* See if an image is contained in a data source */
int IMG_isGIF(SDL_RWops *src)
{
    Sint64 start;
    int is_GIF;
    char magic[6];

    if ( !src )
        return 0;
    start = SDL_RWtell(src);
    is_GIF = 0;
    if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
        if ( (SDL_strncmp(magic, "GIF", 3) == 0) &&
             ((SDL_memcmp(magic + 3, "87a", 3) == 0) ||
              (SDL_memcmp(magic + 3, "89a", 3) == 0)) ) {
            is_GIF = 1;
        }
    }
    SDL_RWseek(src, start, RW_SEEK_SET);
    return(is_GIF);
}
예제 #11
0
/*
 * Helper function to scan the mappings database for a controller with the specified GUID
 */
ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID *guid)
{
    ControllerMapping_t *pSupportedController = s_pSupportedControllers;
    while (pSupportedController) {
        if (SDL_memcmp(guid, &pSupportedController->guid, sizeof(*guid)) == 0) {
            return pSupportedController;
        }
        pSupportedController = pSupportedController->next;
    }
    return NULL;
}
예제 #12
0
int IMG_isLBM( SDL_RWops *src )
{
    Sint64 start;
    int   is_LBM;
    Uint8 magic[4+4+4];

    if ( !src )
        return 0;
    start = SDL_RWtell(src);
    is_LBM = 0;
    if ( SDL_RWread( src, magic, sizeof(magic), 1 ) )
    {
        if ( !SDL_memcmp( magic, "FORM", 4 ) &&
            ( !SDL_memcmp( magic + 8, "PBM ", 4 ) ||
              !SDL_memcmp( magic + 8, "ILBM", 4 ) ) )
        {
            is_LBM = 1;
        }
    }
    SDL_RWseek(src, start, RW_SEEK_SET);
    return( is_LBM );
}
예제 #13
0
void SDL_PrivateGameControllerRefreshMapping(ControllerMapping_t *pControllerMapping)
{
    SDL_GameController *gamecontrollerlist = SDL_gamecontrollers;
    while (gamecontrollerlist) {
        if (!SDL_memcmp(&gamecontrollerlist->mapping.guid, &pControllerMapping->guid, sizeof(pControllerMapping->guid))) {
            SDL_Event event;
            event.type = SDL_CONTROLLERDEVICEREMAPPED;
            event.cdevice.which = gamecontrollerlist->joystick->instance_id;
            SDL_PushEvent(&event);

            /* Not really threadsafe.  Should this lock access within SDL_GameControllerEventWatcher? */
            SDL_PrivateLoadButtonMapping(&gamecontrollerlist->mapping, pControllerMapping->guid, pControllerMapping->name, pControllerMapping->mapping);
        }

        gamecontrollerlist = gamecontrollerlist->next;
    }
}
예제 #14
0
/* See if an image is contained in a data source */
int IMG_isXPM(SDL_RWops *src)
{
    Sint64 start;
    int is_XPM;
    char magic[9];

    if ( !src )
        return 0;
    start = SDL_RWtell(src);
    is_XPM = 0;
    if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
        if ( SDL_memcmp(magic, "/* XPM */", sizeof(magic)) == 0 ) {
            is_XPM = 1;
        }
    }
    SDL_RWseek(src, start, RW_SEEK_SET);
    return(is_XPM);
}
예제 #15
0
int
SDL_DINPUT_MaybeRemoveDevice(const DIDEVICEINSTANCE * pdidInstance)
{
    SDL_hapticlist_item *item;
    SDL_hapticlist_item *prev = NULL;

    if (dinput == NULL) {
        return -1;  /* not initialized, ignore this. */
    }

    for (item = SDL_hapticlist; item != NULL; item = item->next) {
        if (!item->bXInputHaptic && SDL_memcmp(&item->instance, pdidInstance, sizeof(*pdidInstance)) == 0) {
            /* found it, remove it. */
            return SDL_SYS_RemoveHapticDevice(prev, item);
        }
        prev = item;
    }
    return -1;
}
예제 #16
0
void
WIN_UpdateClipCursor(SDL_Window *window)
{
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
    SDL_Mouse *mouse = SDL_GetMouse();
    RECT rect;

    if (data->focus_click_pending) {
        return;
    }

    if ((mouse->relative_mode || (window->flags & SDL_WINDOW_INPUT_GRABBED)) &&
        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
        if (mouse->relative_mode && !mouse->relative_mode_warp) {
            LONG cx, cy;
            GetWindowRect(data->hwnd, &rect);

            cx = (rect.left + rect.right) / 2;
            cy = (rect.top + rect.bottom) / 2;

            /* Make an absurdly small clip rect */
            rect.left = cx - 1;
            rect.right = cx + 1;
            rect.top = cy - 1;
            rect.bottom = cy + 1;

            if (ClipCursor(&rect)) {
                data->cursor_clipped_rect = rect;
            }
        } else {
            if (GetClientRect(data->hwnd, &rect) && !IsRectEmpty(&rect)) {
                ClientToScreen(data->hwnd, (LPPOINT) & rect);
                ClientToScreen(data->hwnd, (LPPOINT) & rect + 1);
                if (ClipCursor(&rect)) {
                    data->cursor_clipped_rect = rect;
                }
            }
        }
    } else if (GetClipCursor(&rect) && SDL_memcmp(&rect, &data->cursor_clipped_rect, sizeof(rect)) == 0) {
        ClipCursor(NULL);
        SDL_zero(data->cursor_clipped_rect);
    }
}
예제 #17
0
int main(int argc, char *argv[])
{
	SDL_RWops *rwops = NULL;
	char test_buf[30];
	
	cleanup();

	
	rwops = SDL_RWFromFile(NULL,NULL);
	if (rwops) RWOP_ERR_QUIT(rwops);
	rwops = SDL_RWFromFile(NULL,"ab+");
	if (rwops) RWOP_ERR_QUIT(rwops);
	rwops = SDL_RWFromFile(NULL,"sldfkjsldkfj");
	if (rwops) RWOP_ERR_QUIT(rwops);
	rwops = SDL_RWFromFile("something","");
	if (rwops) RWOP_ERR_QUIT(rwops);
	rwops = SDL_RWFromFile("something",NULL);
	if (rwops) RWOP_ERR_QUIT(rwops);	
	printf("test1 OK\n");

	rwops = SDL_RWFromFile(FBASENAME2,"rb"); 
	if (rwops) RWOP_ERR_QUIT(rwops);
	rwops = SDL_RWFromFile(FBASENAME2,"rb+"); 
	if (rwops) RWOP_ERR_QUIT(rwops);
	rwops = SDL_RWFromFile(FBASENAME2,"wb");
	if (!rwops) RWOP_ERR_QUIT(rwops); 
	rwops->close(rwops); unlink(FBASENAME2);
	rwops = SDL_RWFromFile(FBASENAME2,"wb+");
	if (!rwops) RWOP_ERR_QUIT(rwops); 
	rwops->close(rwops); unlink(FBASENAME2);
	rwops = SDL_RWFromFile(FBASENAME2,"ab"); 
	if (!rwops) RWOP_ERR_QUIT(rwops); 
	rwops->close(rwops); unlink(FBASENAME2);
	rwops = SDL_RWFromFile(FBASENAME2,"ab+");
	if (!rwops) RWOP_ERR_QUIT(rwops); 
	rwops->close(rwops); unlink(FBASENAME2);
	printf("test2 OK\n");

	rwops = SDL_RWFromFile(FBASENAME1,"wb"); 	
	if (!rwops)											RWOP_ERR_QUIT(rwops);
	if (1 != rwops->write(rwops,"1234567890",10,1) )	RWOP_ERR_QUIT(rwops);		
	if (10 != rwops->write(rwops,"1234567890",1,10) )	RWOP_ERR_QUIT(rwops);		
	if (7 != rwops->write(rwops,"1234567",1,7) )		RWOP_ERR_QUIT(rwops);		
	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
	if (0!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops); 
	rwops->close(rwops);

	rwops = SDL_RWFromFile(FBASENAME1,"rb"); 
	if (!rwops)											RWOP_ERR_QUIT(rwops);
	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
	if (20!=rwops->seek(rwops,-7,RW_SEEK_END))			RWOP_ERR_QUIT(rwops);					
	if (7!=rwops->read(rwops,test_buf,1,7))				RWOP_ERR_QUIT(rwops);		
	if (SDL_memcmp(test_buf,"1234567",7))				RWOP_ERR_QUIT(rwops);		
	if (0!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops);		
	if (0!=rwops->read(rwops,test_buf,10,100))			RWOP_ERR_QUIT(rwops);		
	if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR))			RWOP_ERR_QUIT(rwops);		
	if (2!=rwops->read(rwops,test_buf,10,3))			RWOP_ERR_QUIT(rwops);				
	if (SDL_memcmp(test_buf,"12345678901234567890",20))	RWOP_ERR_QUIT(rwops);
	if (0!=rwops->write(rwops,test_buf,1,1))			RWOP_ERR_QUIT(rwops); 
	rwops->close(rwops);

	rwops = SDL_RWFromFile(FBASENAME1,"wb+"); 		
	if (!rwops)											RWOP_ERR_QUIT(rwops);
	if (1 != rwops->write(rwops,"1234567890",10,1) )	RWOP_ERR_QUIT(rwops);		
	if (10 != rwops->write(rwops,"1234567890",1,10) )	RWOP_ERR_QUIT(rwops);		
	if (7 != rwops->write(rwops,"1234567",1,7) )		RWOP_ERR_QUIT(rwops);		
	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
	if (1!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops); 
	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
	if (20!=rwops->seek(rwops,-7,RW_SEEK_END))			RWOP_ERR_QUIT(rwops);					
	if (7!=rwops->read(rwops,test_buf,1,7))				RWOP_ERR_QUIT(rwops);		
	if (SDL_memcmp(test_buf,"1234567",7))				RWOP_ERR_QUIT(rwops);		
	if (0!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops);		
	if (0!=rwops->read(rwops,test_buf,10,100))			RWOP_ERR_QUIT(rwops);		
	if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR))			RWOP_ERR_QUIT(rwops);		
	if (2!=rwops->read(rwops,test_buf,10,3))			RWOP_ERR_QUIT(rwops);				
	if (SDL_memcmp(test_buf,"12345678901234567890",20))	RWOP_ERR_QUIT(rwops);
	rwops->close(rwops);
	printf("test3 OK\n");

	rwops = SDL_RWFromFile(FBASENAME1,"rb+"); 		
	if (!rwops)											RWOP_ERR_QUIT(rwops);
	if (1 != rwops->write(rwops,"1234567890",10,1) )	RWOP_ERR_QUIT(rwops);		
	if (10 != rwops->write(rwops,"1234567890",1,10) )	RWOP_ERR_QUIT(rwops);		
	if (7 != rwops->write(rwops,"1234567",1,7) )		RWOP_ERR_QUIT(rwops);		
	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
	if (1!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops); 
	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
	if (20!=rwops->seek(rwops,-7,RW_SEEK_END))			RWOP_ERR_QUIT(rwops);					
	if (7!=rwops->read(rwops,test_buf,1,7))				RWOP_ERR_QUIT(rwops);		
	if (SDL_memcmp(test_buf,"1234567",7))				RWOP_ERR_QUIT(rwops);		
	if (0!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops);		
	if (0!=rwops->read(rwops,test_buf,10,100))			RWOP_ERR_QUIT(rwops);		
	if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR))			RWOP_ERR_QUIT(rwops);		
	if (2!=rwops->read(rwops,test_buf,10,3))			RWOP_ERR_QUIT(rwops);				
	if (SDL_memcmp(test_buf,"12345678901234567890",20))	RWOP_ERR_QUIT(rwops);
	rwops->close(rwops);
	printf("test4 OK\n");

	rwops = SDL_RWFromFile(FBASENAME1,"ab+"); 		
	if (!rwops)											RWOP_ERR_QUIT(rwops);
	if (1 != rwops->write(rwops,"1234567890",10,1) )	RWOP_ERR_QUIT(rwops);		
	if (10 != rwops->write(rwops,"1234567890",1,10) )	RWOP_ERR_QUIT(rwops);		
	if (7 != rwops->write(rwops,"1234567",1,7) )		RWOP_ERR_QUIT(rwops); 	
	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
	
	if (1!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops); 
	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
	
	if (20+27!=rwops->seek(rwops,-7,RW_SEEK_END))		RWOP_ERR_QUIT(rwops);					
	if (7!=rwops->read(rwops,test_buf,1,7))				RWOP_ERR_QUIT(rwops);		
	if (SDL_memcmp(test_buf,"1234567",7))				RWOP_ERR_QUIT(rwops);		
	if (0!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops);		
	if (0!=rwops->read(rwops,test_buf,10,100))			RWOP_ERR_QUIT(rwops);		
	
	if (27!=rwops->seek(rwops,-27,RW_SEEK_CUR))			RWOP_ERR_QUIT(rwops);
	
	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);
	if (3!=rwops->read(rwops,test_buf,10,3))			RWOP_ERR_QUIT(rwops);				
	if (SDL_memcmp(test_buf,"123456789012345678901234567123",30))	
														RWOP_ERR_QUIT(rwops);
	rwops->close(rwops);
	printf("test5 OK\n");
	cleanup();
	return 0; 
}
예제 #18
0
파일: testfile.c 프로젝트: nint22/RayCaster
int
main(int argc, char *argv[])
{
    SDL_RWops *rwops = NULL;
    char test_buf[30];

    /* Enable standard application logging */
	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);

    cleanup();

/* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */

    rwops = SDL_RWFromFile(NULL, NULL);
    if (rwops)
        RWOP_ERR_QUIT(rwops);
    rwops = SDL_RWFromFile(NULL, "ab+");
    if (rwops)
        RWOP_ERR_QUIT(rwops);
    rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
    if (rwops)
        RWOP_ERR_QUIT(rwops);
    rwops = SDL_RWFromFile("something", "");
    if (rwops)
        RWOP_ERR_QUIT(rwops);
    rwops = SDL_RWFromFile("something", NULL);
    if (rwops)
        RWOP_ERR_QUIT(rwops);
    SDL_Log("test1 OK\n");

/* test 2 : check that inexistent file is not successfully opened/created when required */
/* modes : r, r+ imply that file MUST exist
   modes : a, a+, w, w+ checks that it succeeds (file may not exists)

 */
    rwops = SDL_RWFromFile(FBASENAME2, "rb");   /* this file doesn't exist that call must fail */
    if (rwops)
        RWOP_ERR_QUIT(rwops);
    rwops = SDL_RWFromFile(FBASENAME2, "rb+");  /* this file doesn't exist that call must fail */
    if (rwops)
        RWOP_ERR_QUIT(rwops);
    rwops = SDL_RWFromFile(FBASENAME2, "wb");
    if (!rwops)
        RWOP_ERR_QUIT(rwops);
    rwops->close(rwops);
    unlink(FBASENAME2);
    rwops = SDL_RWFromFile(FBASENAME2, "wb+");
    if (!rwops)
        RWOP_ERR_QUIT(rwops);
    rwops->close(rwops);
    unlink(FBASENAME2);
    rwops = SDL_RWFromFile(FBASENAME2, "ab");
    if (!rwops)
        RWOP_ERR_QUIT(rwops);
    rwops->close(rwops);
    unlink(FBASENAME2);
    rwops = SDL_RWFromFile(FBASENAME2, "ab+");
    if (!rwops)
        RWOP_ERR_QUIT(rwops);
    rwops->close(rwops);
    unlink(FBASENAME2);
    SDL_Log("test2 OK\n");

/* test 3 : creation, writing , reading, seeking,
            test : w mode, r mode, w+ mode
 */
    rwops = SDL_RWFromFile(FBASENAME1, "wb");   /* write only */
    if (!rwops)
        RWOP_ERR_QUIT(rwops);
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
        RWOP_ERR_QUIT(rwops);
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
        RWOP_ERR_QUIT(rwops);
    if (7 != rwops->write(rwops, "1234567", 1, 7))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->read(rwops, test_buf, 1, 1))
        RWOP_ERR_QUIT(rwops);   /* we are in write only mode */
    rwops->close(rwops);

    rwops = SDL_RWFromFile(FBASENAME1, "rb");   /* read mode, file must exists */
    if (!rwops)
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
        RWOP_ERR_QUIT(rwops);
    if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
        RWOP_ERR_QUIT(rwops);
    if (7 != rwops->read(rwops, test_buf, 1, 7))
        RWOP_ERR_QUIT(rwops);
    if (SDL_memcmp(test_buf, "1234567", 7))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->read(rwops, test_buf, 1, 1))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->read(rwops, test_buf, 10, 100))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
        RWOP_ERR_QUIT(rwops);
    if (2 != rwops->read(rwops, test_buf, 10, 3))
        RWOP_ERR_QUIT(rwops);
    if (SDL_memcmp(test_buf, "12345678901234567890", 20))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->write(rwops, test_buf, 1, 1))
        RWOP_ERR_QUIT(rwops);   /* readonly mode */
    rwops->close(rwops);

/* test 3: same with w+ mode */
    rwops = SDL_RWFromFile(FBASENAME1, "wb+");  /* write + read + truncation */
    if (!rwops)
        RWOP_ERR_QUIT(rwops);
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
        RWOP_ERR_QUIT(rwops);
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
        RWOP_ERR_QUIT(rwops);
    if (7 != rwops->write(rwops, "1234567", 1, 7))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
        RWOP_ERR_QUIT(rwops);
    if (1 != rwops->read(rwops, test_buf, 1, 1))
        RWOP_ERR_QUIT(rwops);   /* we are in read/write mode */
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
        RWOP_ERR_QUIT(rwops);
    if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
        RWOP_ERR_QUIT(rwops);
    if (7 != rwops->read(rwops, test_buf, 1, 7))
        RWOP_ERR_QUIT(rwops);
    if (SDL_memcmp(test_buf, "1234567", 7))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->read(rwops, test_buf, 1, 1))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->read(rwops, test_buf, 10, 100))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
        RWOP_ERR_QUIT(rwops);
    if (2 != rwops->read(rwops, test_buf, 10, 3))
        RWOP_ERR_QUIT(rwops);
    if (SDL_memcmp(test_buf, "12345678901234567890", 20))
        RWOP_ERR_QUIT(rwops);
    rwops->close(rwops);
    SDL_Log("test3 OK\n");

/* test 4: same in r+ mode */
    rwops = SDL_RWFromFile(FBASENAME1, "rb+");  /* write + read + file must exists, no truncation */
    if (!rwops)
        RWOP_ERR_QUIT(rwops);
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
        RWOP_ERR_QUIT(rwops);
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
        RWOP_ERR_QUIT(rwops);
    if (7 != rwops->write(rwops, "1234567", 1, 7))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
        RWOP_ERR_QUIT(rwops);
    if (1 != rwops->read(rwops, test_buf, 1, 1))
        RWOP_ERR_QUIT(rwops);   /* we are in read/write mode */
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
        RWOP_ERR_QUIT(rwops);
    if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
        RWOP_ERR_QUIT(rwops);
    if (7 != rwops->read(rwops, test_buf, 1, 7))
        RWOP_ERR_QUIT(rwops);
    if (SDL_memcmp(test_buf, "1234567", 7))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->read(rwops, test_buf, 1, 1))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->read(rwops, test_buf, 10, 100))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
        RWOP_ERR_QUIT(rwops);
    if (2 != rwops->read(rwops, test_buf, 10, 3))
        RWOP_ERR_QUIT(rwops);
    if (SDL_memcmp(test_buf, "12345678901234567890", 20))
        RWOP_ERR_QUIT(rwops);
    rwops->close(rwops);
    SDL_Log("test4 OK\n");

/* test5 : append mode */
    rwops = SDL_RWFromFile(FBASENAME1, "ab+");  /* write + read + append */
    if (!rwops)
        RWOP_ERR_QUIT(rwops);
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
        RWOP_ERR_QUIT(rwops);
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
        RWOP_ERR_QUIT(rwops);
    if (7 != rwops->write(rwops, "1234567", 1, 7))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
        RWOP_ERR_QUIT(rwops);

    if (1 != rwops->read(rwops, test_buf, 1, 1))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
        RWOP_ERR_QUIT(rwops);

    if (20 + 27 != rwops->seek(rwops, -7, RW_SEEK_END))
        RWOP_ERR_QUIT(rwops);
    if (7 != rwops->read(rwops, test_buf, 1, 7))
        RWOP_ERR_QUIT(rwops);
    if (SDL_memcmp(test_buf, "1234567", 7))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->read(rwops, test_buf, 1, 1))
        RWOP_ERR_QUIT(rwops);
    if (0 != rwops->read(rwops, test_buf, 10, 100))
        RWOP_ERR_QUIT(rwops);

    if (27 != rwops->seek(rwops, -27, RW_SEEK_CUR))
        RWOP_ERR_QUIT(rwops);

    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
        RWOP_ERR_QUIT(rwops);
    if (3 != rwops->read(rwops, test_buf, 10, 3))
        RWOP_ERR_QUIT(rwops);
    if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30))
        RWOP_ERR_QUIT(rwops);
    rwops->close(rwops);
    SDL_Log("test5 OK\n");
    cleanup();
    return 0;                   /* all ok */
}
예제 #19
0
/**
 * @brief Makes sure parameters work properly. Local helper function.
 *
 * \sa
 * http://wiki.libsdl.org/moin.cgi/SDL_RWseek
 * http://wiki.libsdl.org/moin.cgi/SDL_RWread
 */
void
_testGenericRWopsValidations(SDL_RWops *rw, int write)
{
   char buf[sizeof(RWopsHelloWorldTestString)];
   Sint64 i;
   size_t s;
   int seekPos = SDLTest_RandomIntegerInRange(4, 8);

   /* Clear buffer */
   SDL_zero(buf);

   /* Set to start. */
   i = SDL_RWseek(rw, 0, RW_SEEK_SET );
   SDLTest_AssertPass("Call to SDL_RWseek succeeded");
   SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_RWseek (RW_SEEK_SET), expected 0, got %i", i);

   /* Test write. */
   s = SDL_RWwrite(rw, RWopsHelloWorldTestString, sizeof(RWopsHelloWorldTestString)-1, 1);
   SDLTest_AssertPass("Call to SDL_RWwrite succeeded");
   if (write) {
        SDLTest_AssertCheck(s == (size_t)1, "Verify result of writing one byte with SDL_RWwrite, expected 1, got %i", s);
   }
   else {
        SDLTest_AssertCheck(s == (size_t)0, "Verify result of writing with SDL_RWwrite, expected: 0, got %i", s);
   }

   /* Test seek to random position */
   i = SDL_RWseek( rw, seekPos, RW_SEEK_SET );
   SDLTest_AssertPass("Call to SDL_RWseek succeeded");
   SDLTest_AssertCheck(i == (Sint64)seekPos, "Verify seek to %i with SDL_RWseek (RW_SEEK_SET), expected %i, got %i", seekPos, seekPos, i);

   /* Test seek back to start */
   i = SDL_RWseek(rw, 0, RW_SEEK_SET );
   SDLTest_AssertPass("Call to SDL_RWseek succeeded");
   SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_RWseek (RW_SEEK_SET), expected 0, got %i", i);

   /* Test read */
   s = SDL_RWread( rw, buf, 1, sizeof(RWopsHelloWorldTestString)-1 );
   SDLTest_AssertPass("Call to SDL_RWread succeeded");
   SDLTest_AssertCheck(
       s == (size_t)(sizeof(RWopsHelloWorldTestString)-1),
       "Verify result from SDL_RWread, expected %i, got %i",
       sizeof(RWopsHelloWorldTestString)-1,
       s);
   SDLTest_AssertCheck(
       SDL_memcmp(buf, RWopsHelloWorldTestString, sizeof(RWopsHelloWorldTestString)-1 ) == 0,
       "Verify read bytes match expected string, expected '%s', got '%s'", RWopsHelloWorldTestString, buf);

   /* More seek tests. */
   i = SDL_RWseek( rw, -4, RW_SEEK_CUR );
   SDLTest_AssertPass("Call to SDL_RWseek(...,-4,RW_SEEK_CUR) succeeded");
   SDLTest_AssertCheck(
       i == (Sint64)(sizeof(RWopsHelloWorldTestString)-5),
       "Verify seek to -4 with SDL_RWseek (RW_SEEK_CUR), expected %i, got %i",
       sizeof(RWopsHelloWorldTestString)-5,
       i);

   i = SDL_RWseek( rw, -1, RW_SEEK_END );
   SDLTest_AssertPass("Call to SDL_RWseek(...,-1,RW_SEEK_END) succeeded");
   SDLTest_AssertCheck(
       i == (Sint64)(sizeof(RWopsHelloWorldTestString)-2),
       "Verify seek to -1 with SDL_RWseek (RW_SEEK_END), expected %i, got %i",
       sizeof(RWopsHelloWorldTestString)-2,
       i);

   /* Invalid whence seek */
   i = SDL_RWseek( rw, 0, 999 );
   SDLTest_AssertPass("Call to SDL_RWseek(...,0,invalid_whence) succeeded");
   SDLTest_AssertCheck(
       i == (Sint64)(-1),
       "Verify seek with SDL_RWseek (invalid_whence); expected: -1, got %i",
       i);
}
예제 #20
0
SDL_Surface *IMG_LoadLBM_RW( SDL_RWops *src )
{
    Sint64 start;
    SDL_Surface *Image;
    Uint8       id[4], pbm, colormap[MAXCOLORS*3], *MiniBuf, *ptr, count, color, msk;
    Uint32      size, bytesloaded, nbcolors;
    Uint32      i, j, bytesperline, nbplanes, stencil, plane, h;
    Uint32      remainingbytes;
    Uint32      width;
    BMHD          bmhd;
    char        *error;
    Uint8       flagHAM,flagEHB;

    Image   = NULL;
    error   = NULL;
    MiniBuf = NULL;

    if ( !src ) {
        /* The error message has been set in SDL_RWFromFile */
        return NULL;
    }
    start = SDL_RWtell(src);

    if ( !SDL_RWread( src, id, 4, 1 ) )
    {
        error="error reading IFF chunk";
        goto done;
    }

    /* Should be the size of the file minus 4+4 ( 'FORM'+size ) */
    if ( !SDL_RWread( src, &size, 4, 1 ) )
    {
        error="error reading IFF chunk size";
        goto done;
    }

    /* As size is not used here, no need to swap it */

    if ( memcmp( id, "FORM", 4 ) != 0 )
    {
        error="not a IFF file";
        goto done;
    }

    if ( !SDL_RWread( src, id, 4, 1 ) )
    {
        error="error reading IFF chunk";
        goto done;
    }

    pbm = 0;

    /* File format : PBM=Packed Bitmap, ILBM=Interleaved Bitmap */
    if ( !SDL_memcmp( id, "PBM ", 4 ) ) pbm = 1;
    else if ( SDL_memcmp( id, "ILBM", 4 ) )
    {
        error="not a IFF picture";
        goto done;
    }

    nbcolors = 0;

    SDL_memset( &bmhd, 0, sizeof( BMHD ) );
    flagHAM = 0;
    flagEHB = 0;

    while ( SDL_memcmp( id, "BODY", 4 ) != 0 )
    {
        if ( !SDL_RWread( src, id, 4, 1 ) )
        {
            error="error reading IFF chunk";
            goto done;
        }

        if ( !SDL_RWread( src, &size, 4, 1 ) )
        {
            error="error reading IFF chunk size";
            goto done;
        }

        bytesloaded = 0;

        size = SDL_SwapBE32( size );

        if ( !SDL_memcmp( id, "BMHD", 4 ) ) /* Bitmap header */
        {
            if ( !SDL_RWread( src, &bmhd, sizeof( BMHD ), 1 ) )
            {
                error="error reading BMHD chunk";
                goto done;
            }

            bytesloaded = sizeof( BMHD );

            bmhd.w      = SDL_SwapBE16( bmhd.w );
            bmhd.h      = SDL_SwapBE16( bmhd.h );
            bmhd.x      = SDL_SwapBE16( bmhd.x );
            bmhd.y      = SDL_SwapBE16( bmhd.y );
            bmhd.tcolor = SDL_SwapBE16( bmhd.tcolor );
            bmhd.Lpage  = SDL_SwapBE16( bmhd.Lpage );
            bmhd.Hpage  = SDL_SwapBE16( bmhd.Hpage );
        }

        if ( !SDL_memcmp( id, "CMAP", 4 ) ) /* palette ( Color Map ) */
        {
            if ( !SDL_RWread( src, &colormap, size, 1 ) )
            {
                error="error reading CMAP chunk";
                goto done;
            }

            bytesloaded = size;
            nbcolors = size / 3;
        }

        if ( !memcmp( id, "CAMG", 4 ) ) /* Amiga ViewMode  */
        {
            Uint32 viewmodes;
            if ( !SDL_RWread( src, &viewmodes, sizeof(viewmodes), 1 ) )
            {
                error="error reading CAMG chunk";
                goto done;
            }

            bytesloaded = size;
            viewmodes = SDL_SwapBE32( viewmodes );
            if ( viewmodes & 0x0800 )
                flagHAM = 1;
            if ( viewmodes & 0x0080 )
                flagEHB = 1;
        }

        if ( SDL_memcmp( id, "BODY", 4 ) )
        {
            if ( size & 1 ) ++size;     /* padding ! */
            size -= bytesloaded;
            /* skip the remaining bytes of this chunk */
            if ( size ) SDL_RWseek( src, size, RW_SEEK_CUR );
        }
    }

    /* compute some usefull values, based on the bitmap header */

    width = ( bmhd.w + 15 ) & 0xFFFFFFF0;  /* Width in pixels modulo 16 */

    bytesperline = ( ( bmhd.w + 15 ) / 16 ) * 2;

    nbplanes = bmhd.planes;

    if ( pbm )                         /* File format : 'Packed Bitmap' */
    {
        bytesperline *= 8;
        nbplanes = 1;
    }

    stencil = (bmhd.mask & 1);   /* There is a mask ( 'stencil' ) */

    /* Allocate memory for a temporary buffer ( used for
           decompression/deinterleaving ) */

    MiniBuf = (Uint8 *)SDL_malloc( bytesperline * (nbplanes + stencil) );
    if ( MiniBuf == NULL )
    {
        error="no enough memory for temporary buffer";
        goto done;
    }

    if ( ( Image = SDL_CreateRGBSurface( SDL_SWSURFACE, width, bmhd.h, (bmhd.planes==24 || flagHAM==1)?24:8, 0, 0, 0, 0 ) ) == NULL )
       goto done;

    if ( bmhd.mask & 2 )               /* There is a transparent color */
        SDL_SetColorKey( Image, SDL_TRUE, bmhd.tcolor );

    /* Update palette informations */

    /* There is no palette in 24 bits ILBM file */
    if ( nbcolors>0 && flagHAM==0 )
    {
        /* FIXME: Should this include the stencil? See comment below */
        int nbrcolorsfinal = 1 << (nbplanes + stencil);
        ptr = &colormap[0];

        for ( i=0; i<nbcolors; i++ )
        {
            Image->format->palette->colors[i].r = *ptr++;
            Image->format->palette->colors[i].g = *ptr++;
            Image->format->palette->colors[i].b = *ptr++;
        }

        /* Amiga EHB mode (Extra-Half-Bright) */
        /* 6 bitplanes mode with a 32 colors palette */
        /* The 32 last colors are the same but divided by 2 */
        /* Some Amiga pictures save 64 colors with 32 last wrong colors, */
        /* they shouldn't !, and here we overwrite these 32 bad colors. */
        if ( (nbcolors==32 || flagEHB ) && (1<<bmhd.planes)==64 )
        {
            nbcolors = 64;
            ptr = &colormap[0];
            for ( i=32; i<64; i++ )
            {
                Image->format->palette->colors[i].r = (*ptr++)/2;
                Image->format->palette->colors[i].g = (*ptr++)/2;
                Image->format->palette->colors[i].b = (*ptr++)/2;
            }
        }

        /* If nbcolors < 2^nbplanes, repeat the colormap */
        /* This happens when pictures have a stencil mask */
        if ( nbrcolorsfinal > (1<<bmhd.planes) ) {
            nbrcolorsfinal = (1<<bmhd.planes);
        }
        for ( i=nbcolors; i < (Uint32)nbrcolorsfinal; i++ )
        {
            Image->format->palette->colors[i].r = Image->format->palette->colors[i%nbcolors].r;
            Image->format->palette->colors[i].g = Image->format->palette->colors[i%nbcolors].g;
            Image->format->palette->colors[i].b = Image->format->palette->colors[i%nbcolors].b;
        }
        if ( !pbm )
            Image->format->palette->ncolors = nbrcolorsfinal;
    }

    /* Get the bitmap */

    for ( h=0; h < bmhd.h; h++ )
    {
        /* uncompress the datas of each planes */

        for ( plane=0; plane < (nbplanes+stencil); plane++ )
        {
            ptr = MiniBuf + ( plane * bytesperline );

            remainingbytes = bytesperline;

            if ( bmhd.tcomp == 1 )      /* Datas are compressed */
            {
                do
                {
                    if ( !SDL_RWread( src, &count, 1, 1 ) )
                    {
                        error="error reading BODY chunk";
                        goto done;
                    }

                    if ( count & 0x80 )
                    {
                        count ^= 0xFF;
                        count += 2; /* now it */

                        if ( ( count > remainingbytes ) || !SDL_RWread( src, &color, 1, 1 ) )
                        {
                            error="error reading BODY chunk";
                            goto done;
                        }
                        SDL_memset( ptr, color, count );
                    }
                    else
                    {
                        ++count;

                        if ( ( count > remainingbytes ) || !SDL_RWread( src, ptr, count, 1 ) )
                        {
                           error="error reading BODY chunk";
                            goto done;
                        }
                    }

                    ptr += count;
                    remainingbytes -= count;

                } while ( remainingbytes > 0 );
            }
            else
            {
                if ( !SDL_RWread( src, ptr, bytesperline, 1 ) )
                {
                    error="error reading BODY chunk";
                    goto done;
                }
            }
        }

        /* One line has been read, store it ! */

        ptr = (Uint8 *)Image->pixels;
        if ( nbplanes==24 || flagHAM==1 )
            ptr += h * width * 3;
        else
            ptr += h * width;

        if ( pbm )                 /* File format : 'Packed Bitmap' */
        {
           memcpy( ptr, MiniBuf, width );
        }
        else        /* We have to un-interlace the bits ! */
        {
            if ( nbplanes!=24 && flagHAM==0 )
            {
                size = ( width + 7 ) / 8;

                for ( i=0; i < size; i++ )
                {
                    memset( ptr, 0, 8 );

                    for ( plane=0; plane < (nbplanes + stencil); plane++ )
                    {
                        color = *( MiniBuf + i + ( plane * bytesperline ) );
                        msk = 0x80;

                        for ( j=0; j<8; j++ )
                        {
                            if ( ( plane + j ) <= 7 ) ptr[j] |= (Uint8)( color & msk ) >> ( 7 - plane - j );
                            else                        ptr[j] |= (Uint8)( color & msk ) << ( plane + j - 7 );

                            msk >>= 1;
                        }
                    }
                    ptr += 8;
                }
            }
            else
            {
예제 #21
0
/* helper function for direct input, gets called for each connected joystick */
static BOOL CALLBACK
EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
{
    const Uint16 BUS_USB = 0x03;
    const Uint16 BUS_BLUETOOTH = 0x05;
    JoyStick_DeviceData *pNewJoystick;
    JoyStick_DeviceData *pPrevJoystick = NULL;
    const DWORD devtype = (pdidInstance->dwDevType & 0xFF);
    Uint16 *guid16;

    if (devtype == DI8DEVTYPE_SUPPLEMENTAL) {
        /* Add any supplemental devices that should be ignored here */
#define MAKE_TABLE_ENTRY(VID, PID)	((((DWORD)PID)<<16)|VID)
		static DWORD ignored_devices[] = {
			MAKE_TABLE_ENTRY(0, 0)
		};
#undef MAKE_TABLE_ENTRY
		unsigned int i;

		for (i = 0; i < SDL_arraysize(ignored_devices); ++i) {
			if (pdidInstance->guidProduct.Data1 == ignored_devices[i]) {
				return DIENUM_CONTINUE;
			}
		}
    }

    if (SDL_IsXInputDevice(&pdidInstance->guidProduct)) {
        return DIENUM_CONTINUE;  /* ignore XInput devices here, keep going. */
    }

    pNewJoystick = *(JoyStick_DeviceData **)pContext;
    while (pNewJoystick) {
        if (!SDL_memcmp(&pNewJoystick->dxdevice.guidInstance, &pdidInstance->guidInstance, sizeof(pNewJoystick->dxdevice.guidInstance))) {
            /* if we are replacing the front of the list then update it */
            if (pNewJoystick == *(JoyStick_DeviceData **)pContext) {
                *(JoyStick_DeviceData **)pContext = pNewJoystick->pNext;
            } else if (pPrevJoystick) {
                pPrevJoystick->pNext = pNewJoystick->pNext;
            }

            pNewJoystick->pNext = SYS_Joystick;
            SYS_Joystick = pNewJoystick;

            return DIENUM_CONTINUE; /* already have this joystick loaded, just keep going */
        }

        pPrevJoystick = pNewJoystick;
        pNewJoystick = pNewJoystick->pNext;
    }

    pNewJoystick = (JoyStick_DeviceData *)SDL_malloc(sizeof(JoyStick_DeviceData));
    if (!pNewJoystick) {
        return DIENUM_CONTINUE; /* better luck next time? */
    }

    SDL_zerop(pNewJoystick);
    pNewJoystick->joystickname = WIN_StringToUTF8(pdidInstance->tszProductName);
    if (!pNewJoystick->joystickname) {
        SDL_free(pNewJoystick);
        return DIENUM_CONTINUE; /* better luck next time? */
    }

    SDL_memcpy(&(pNewJoystick->dxdevice), pdidInstance,
        sizeof(DIDEVICEINSTANCE));

    SDL_memset(pNewJoystick->guid.data, 0, sizeof(pNewJoystick->guid.data));

    guid16 = (Uint16 *)pNewJoystick->guid.data;
    if (SDL_memcmp(&pdidInstance->guidProduct.Data4[2], "PIDVID", 6) == 0) {
        *guid16++ = SDL_SwapLE16(BUS_USB);
        *guid16++ = 0;
        *guid16++ = SDL_SwapLE16((Uint16)LOWORD(pdidInstance->guidProduct.Data1)); /* vendor */
        *guid16++ = 0;
        *guid16++ = SDL_SwapLE16((Uint16)HIWORD(pdidInstance->guidProduct.Data1)); /* product */
        *guid16++ = 0;
        *guid16++ = 0; /* version */
        *guid16++ = 0;
    } else {
        *guid16++ = SDL_SwapLE16(BUS_BLUETOOTH);
        *guid16++ = 0;
        SDL_strlcpy((char*)guid16, pNewJoystick->joystickname, sizeof(pNewJoystick->guid.data) - 4);
    }

    if (SDL_IsGameControllerNameAndGUID(pNewJoystick->joystickname, pNewJoystick->guid) &&
        SDL_ShouldIgnoreGameController(pNewJoystick->joystickname, pNewJoystick->guid)) {
        SDL_free(pNewJoystick);
        return DIENUM_CONTINUE;
    }

    SDL_SYS_AddJoystickDevice(pNewJoystick);

    return DIENUM_CONTINUE; /* get next device, please */
}
예제 #22
0
static BOOL CALLBACK
EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev, LPVOID pvRef)
{
    SDL_Joystick *joystick = (SDL_Joystick *)pvRef;
    HRESULT result;
    input_t *in = &joystick->hwdata->Inputs[joystick->hwdata->NumInputs];

    if (dev->dwType & DIDFT_BUTTON) {
        in->type = BUTTON;
        in->num = joystick->nbuttons;
        in->ofs = DIJOFS_BUTTON(in->num);
        joystick->nbuttons++;
    } else if (dev->dwType & DIDFT_POV) {
        in->type = HAT;
        in->num = joystick->nhats;
        in->ofs = DIJOFS_POV(in->num);
        joystick->nhats++;
    } else if (dev->dwType & DIDFT_AXIS) {
        DIPROPRANGE diprg;
        DIPROPDWORD dilong;

        in->type = AXIS;
        in->num = joystick->naxes;
        if (!SDL_memcmp(&dev->guidType, &GUID_XAxis, sizeof(dev->guidType)))
            in->ofs = DIJOFS_X;
        else if (!SDL_memcmp(&dev->guidType, &GUID_YAxis, sizeof(dev->guidType)))
            in->ofs = DIJOFS_Y;
        else if (!SDL_memcmp(&dev->guidType, &GUID_ZAxis, sizeof(dev->guidType)))
            in->ofs = DIJOFS_Z;
        else if (!SDL_memcmp(&dev->guidType, &GUID_RxAxis, sizeof(dev->guidType)))
            in->ofs = DIJOFS_RX;
        else if (!SDL_memcmp(&dev->guidType, &GUID_RyAxis, sizeof(dev->guidType)))
            in->ofs = DIJOFS_RY;
        else if (!SDL_memcmp(&dev->guidType, &GUID_RzAxis, sizeof(dev->guidType)))
            in->ofs = DIJOFS_RZ;
        else if (!SDL_memcmp(&dev->guidType, &GUID_Slider, sizeof(dev->guidType))) {
            in->ofs = DIJOFS_SLIDER(joystick->hwdata->NumSliders);
            ++joystick->hwdata->NumSliders;
        } else {
            return DIENUM_CONTINUE; /* not an axis we can grok */
        }

        diprg.diph.dwSize = sizeof(diprg);
        diprg.diph.dwHeaderSize = sizeof(diprg.diph);
        diprg.diph.dwObj = dev->dwType;
        diprg.diph.dwHow = DIPH_BYID;
        diprg.lMin = SDL_JOYSTICK_AXIS_MIN;
        diprg.lMax = SDL_JOYSTICK_AXIS_MAX;

        result =
            IDirectInputDevice8_SetProperty(joystick->hwdata->InputDevice,
            DIPROP_RANGE, &diprg.diph);
        if (FAILED(result)) {
            return DIENUM_CONTINUE;     /* don't use this axis */
        }

        /* Set dead zone to 0. */
        dilong.diph.dwSize = sizeof(dilong);
        dilong.diph.dwHeaderSize = sizeof(dilong.diph);
        dilong.diph.dwObj = dev->dwType;
        dilong.diph.dwHow = DIPH_BYID;
        dilong.dwData = 0;
        result =
            IDirectInputDevice8_SetProperty(joystick->hwdata->InputDevice,
            DIPROP_DEADZONE, &dilong.diph);
        if (FAILED(result)) {
            return DIENUM_CONTINUE;     /* don't use this axis */
        }

        joystick->naxes++;
    } else {
        /* not supported at this time */
        return DIENUM_CONTINUE;
    }

    joystick->hwdata->NumInputs++;

    if (joystick->hwdata->NumInputs == MAX_INPUTS) {
        return DIENUM_STOP;     /* too many */
    }

    return DIENUM_CONTINUE;
}
예제 #23
0
static SDL_bool
SDL_IsXInputDevice(const GUID* pGuidProductFromDirectInput)
{
    static GUID IID_ValveStreamingGamepad = { MAKELONG(0x28DE, 0x11FF), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
    static GUID IID_X360WiredGamepad = { MAKELONG(0x045E, 0x02A1), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
    static GUID IID_X360WirelessGamepad = { MAKELONG(0x045E, 0x028E), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
    static GUID IID_XOneWiredGamepad = { MAKELONG(0x045E, 0x02FF), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
    static GUID IID_XOneWirelessGamepad = { MAKELONG(0x045E, 0x02DD), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
    static GUID IID_XOneNewWirelessGamepad = { MAKELONG(0x045E, 0x02D1), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
    static GUID IID_XOneSWirelessGamepad = { MAKELONG(0x045E, 0x02EA), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
    static GUID IID_XOneSBluetoothGamepad = { MAKELONG(0x045E, 0x02E0), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
    static GUID IID_XOneEliteWirelessGamepad = { MAKELONG(0x045E, 0x02E3), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };

    static const GUID *s_XInputProductGUID[] = {
        &IID_ValveStreamingGamepad,
        &IID_X360WiredGamepad,         /* Microsoft's wired X360 controller for Windows. */
        &IID_X360WirelessGamepad,      /* Microsoft's wireless X360 controller for Windows. */
        &IID_XOneWiredGamepad,         /* Microsoft's wired Xbox One controller for Windows. */
        &IID_XOneWirelessGamepad,      /* Microsoft's wireless Xbox One controller for Windows. */
        &IID_XOneNewWirelessGamepad,   /* Microsoft's updated wireless Xbox One controller (w/ 3.5 mm jack) for Windows. */
        &IID_XOneSWirelessGamepad,     /* Microsoft's wireless Xbox One S controller for Windows. */
        &IID_XOneSBluetoothGamepad,    /* Microsoft's Bluetooth Xbox One S controller for Windows. */
        &IID_XOneEliteWirelessGamepad  /* Microsoft's wireless Xbox One Elite controller for Windows. */
    };

    size_t iDevice;
    UINT i;

    if (!SDL_XINPUT_Enabled()) {
        return SDL_FALSE;
    }

    /* Check for well known XInput device GUIDs */
    /* This lets us skip RAWINPUT for popular devices. Also, we need to do this for the Valve Streaming Gamepad because it's virtualized and doesn't show up in the device list. */
    for (iDevice = 0; iDevice < SDL_arraysize(s_XInputProductGUID); ++iDevice) {
        if (SDL_memcmp(pGuidProductFromDirectInput, s_XInputProductGUID[iDevice], sizeof(GUID)) == 0) {
            return SDL_TRUE;
        }
    }

    /* Go through RAWINPUT (WinXP and later) to find HID devices. */
    /* Cache this if we end up using it. */
    if (SDL_RawDevList == NULL) {
        if ((GetRawInputDeviceList(NULL, &SDL_RawDevListCount, sizeof(RAWINPUTDEVICELIST)) == -1) || (!SDL_RawDevListCount)) {
            return SDL_FALSE;  /* oh well. */
        }

        SDL_RawDevList = (PRAWINPUTDEVICELIST)SDL_malloc(sizeof(RAWINPUTDEVICELIST) * SDL_RawDevListCount);
        if (SDL_RawDevList == NULL) {
            SDL_OutOfMemory();
            return SDL_FALSE;
        }

        if (GetRawInputDeviceList(SDL_RawDevList, &SDL_RawDevListCount, sizeof(RAWINPUTDEVICELIST)) == -1) {
            SDL_free(SDL_RawDevList);
            SDL_RawDevList = NULL;
            return SDL_FALSE;  /* oh well. */
        }
    }

    for (i = 0; i < SDL_RawDevListCount; i++) {
        RID_DEVICE_INFO rdi;
        char devName[128];
        UINT rdiSize = sizeof(rdi);
        UINT nameSize = SDL_arraysize(devName);

        rdi.cbSize = sizeof(rdi);
        if ((SDL_RawDevList[i].dwType == RIM_TYPEHID) &&
            (GetRawInputDeviceInfoA(SDL_RawDevList[i].hDevice, RIDI_DEVICEINFO, &rdi, &rdiSize) != ((UINT)-1)) &&
            (MAKELONG(rdi.hid.dwVendorId, rdi.hid.dwProductId) == ((LONG)pGuidProductFromDirectInput->Data1)) &&
            (GetRawInputDeviceInfoA(SDL_RawDevList[i].hDevice, RIDI_DEVICENAME, devName, &nameSize) != ((UINT)-1)) &&
            (SDL_strstr(devName, "IG_") != NULL)) {
            return SDL_TRUE;
        }
    }

    return SDL_FALSE;
}
예제 #24
0
/*
 * Convert a surface into the specified pixel format.
 */
SDL_Surface *
SDL_ConvertSurface(SDL_Surface * surface, const SDL_PixelFormat * format,
                   Uint32 flags)
{
    SDL_Surface *convert;
    Uint32 copy_flags;
    SDL_Color copy_color;
    SDL_Rect bounds;

    /* Check for empty destination palette! (results in empty image) */
    if (format->palette != NULL) {
        int i;
        for (i = 0; i < format->palette->ncolors; ++i) {
            if ((format->palette->colors[i].r != 0xFF) ||
                    (format->palette->colors[i].g != 0xFF) ||
                    (format->palette->colors[i].b != 0xFF))
                break;
        }
        if (i == format->palette->ncolors) {
            SDL_SetError("Empty destination palette");
            return (NULL);
        }
    }

    /* Create a new surface with the desired format */
    convert = SDL_CreateRGBSurface(flags, surface->w, surface->h,
                                   format->BitsPerPixel, format->Rmask,
                                   format->Gmask, format->Bmask,
                                   format->Amask);
    if (convert == NULL) {
        return (NULL);
    }

    /* Copy the palette if any */
    if (format->palette && convert->format->palette) {
        SDL_memcpy(convert->format->palette->colors,
                   format->palette->colors,
                   format->palette->ncolors * sizeof(SDL_Color));
        convert->format->palette->ncolors = format->palette->ncolors;
    }

    /* Save the original copy flags */
    copy_flags = surface->map->info.flags;
    copy_color.r = surface->map->info.r;
    copy_color.g = surface->map->info.g;
    copy_color.b = surface->map->info.b;
    copy_color.a = surface->map->info.a;
    surface->map->info.r = 0xFF;
    surface->map->info.g = 0xFF;
    surface->map->info.b = 0xFF;
    surface->map->info.a = 0xFF;
    surface->map->info.flags = 0;
    SDL_InvalidateMap(surface->map);

    /* Copy over the image data */
    bounds.x = 0;
    bounds.y = 0;
    bounds.w = surface->w;
    bounds.h = surface->h;
    SDL_LowerBlit(surface, &bounds, convert, &bounds);

    /* Clean up the original surface, and update converted surface */
    convert->map->info.r = copy_color.r;
    convert->map->info.g = copy_color.g;
    convert->map->info.b = copy_color.b;
    convert->map->info.a = copy_color.a;
    convert->map->info.flags =
        (copy_flags &
         ~(SDL_COPY_COLORKEY | SDL_COPY_BLEND
           | SDL_COPY_RLE_DESIRED | SDL_COPY_RLE_COLORKEY |
           SDL_COPY_RLE_ALPHAKEY));
    surface->map->info.r = copy_color.r;
    surface->map->info.g = copy_color.g;
    surface->map->info.b = copy_color.b;
    surface->map->info.a = copy_color.a;
    surface->map->info.flags = copy_flags;
    SDL_InvalidateMap(surface->map);
    if (copy_flags & SDL_COPY_COLORKEY) {
        SDL_bool set_colorkey_by_color = SDL_FALSE;

        if (surface->format->palette) {
            if (format->palette &&
                    surface->format->palette->ncolors <= format->palette->ncolors &&
                    (SDL_memcmp(surface->format->palette->colors, format->palette->colors,
                                surface->format->palette->ncolors * sizeof(SDL_Color)) == 0)) {
                /* The palette is identical, just set the same colorkey */
                SDL_SetColorKey(convert, 1, surface->map->info.colorkey);
            } else if (format->Amask) {
                /* The alpha was set in the destination from the palette */
            } else {
                set_colorkey_by_color = SDL_TRUE;
            }
        } else {
            set_colorkey_by_color = SDL_TRUE;
        }

        if (set_colorkey_by_color) {
            /* Set the colorkey by color, which needs to be unique */
            Uint8 keyR, keyG, keyB, keyA;

            SDL_GetRGBA(surface->map->info.colorkey, surface->format, &keyR,
                        &keyG, &keyB, &keyA);
            SDL_SetColorKey(convert, 1,
                            SDL_MapRGBA(convert->format, keyR, keyG, keyB, keyA));
            /* This is needed when converting for 3D texture upload */
            SDL_ConvertColorkeyToAlpha(convert);
        }
    }
    SDL_SetClipRect(convert, &surface->clip_rect);

    /* Enable alpha blending by default if the new surface has an
     * alpha channel or alpha modulation */
    if ((surface->format->Amask && format->Amask) ||
            (copy_flags & (SDL_COPY_COLORKEY|SDL_COPY_MODULATE_ALPHA))) {
        SDL_SetSurfaceBlendMode(convert, SDL_BLENDMODE_BLEND);
    }
    if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
        SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
    }

    /* We're ready to go! */
    return (convert);
}
예제 #25
0
/*
 * Checks to see if two GUID are the same.
 */
static int
DI_GUIDIsSame(const GUID * a, const GUID * b)
{
    return (SDL_memcmp(a, b, sizeof (GUID)) == 0);
}
예제 #26
0
/* helper function for direct input, gets called for each connected joystick */
static BOOL CALLBACK
EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
{
    const Uint16 BUS_USB = 0x03;
    const Uint16 BUS_BLUETOOTH = 0x05;
    JoyStick_DeviceData *pNewJoystick;
    JoyStick_DeviceData *pPrevJoystick = NULL;
    const DWORD devtype = (pdidInstance->dwDevType & 0xFF);
    Uint16 *guid16;

    if (devtype == DI8DEVTYPE_SUPPLEMENTAL) {
        return DIENUM_CONTINUE;  /* Ignore touchpads, etc. */
    }

    if (SDL_IsXInputDevice(&pdidInstance->guidProduct)) {
        return DIENUM_CONTINUE;  /* ignore XInput devices here, keep going. */
    }

    pNewJoystick = *(JoyStick_DeviceData **)pContext;
    while (pNewJoystick) {
        if (!SDL_memcmp(&pNewJoystick->dxdevice.guidInstance, &pdidInstance->guidInstance, sizeof(pNewJoystick->dxdevice.guidInstance))) {
            /* if we are replacing the front of the list then update it */
            if (pNewJoystick == *(JoyStick_DeviceData **)pContext) {
                *(JoyStick_DeviceData **)pContext = pNewJoystick->pNext;
            } else if (pPrevJoystick) {
                pPrevJoystick->pNext = pNewJoystick->pNext;
            }

            pNewJoystick->pNext = SYS_Joystick;
            SYS_Joystick = pNewJoystick;

            return DIENUM_CONTINUE; /* already have this joystick loaded, just keep going */
        }

        pPrevJoystick = pNewJoystick;
        pNewJoystick = pNewJoystick->pNext;
    }

    pNewJoystick = (JoyStick_DeviceData *)SDL_malloc(sizeof(JoyStick_DeviceData));
    if (!pNewJoystick) {
        return DIENUM_CONTINUE; /* better luck next time? */
    }

    SDL_zerop(pNewJoystick);
    pNewJoystick->joystickname = WIN_StringToUTF8(pdidInstance->tszProductName);
    if (!pNewJoystick->joystickname) {
        SDL_free(pNewJoystick);
        return DIENUM_CONTINUE; /* better luck next time? */
    }

    SDL_memcpy(&(pNewJoystick->dxdevice), pdidInstance,
        sizeof(DIDEVICEINSTANCE));

    SDL_memset(pNewJoystick->guid.data, 0, sizeof(pNewJoystick->guid.data));

    guid16 = (Uint16 *)pNewJoystick->guid.data;
    if (SDL_memcmp(&pdidInstance->guidProduct.Data4[2], "PIDVID", 6) == 0) {
        *guid16++ = SDL_SwapLE16(BUS_USB);
        *guid16++ = 0;
        *guid16++ = SDL_SwapLE16((Uint16)LOWORD(pdidInstance->guidProduct.Data1)); /* vendor */
        *guid16++ = 0;
        *guid16++ = SDL_SwapLE16((Uint16)HIWORD(pdidInstance->guidProduct.Data1)); /* product */
        *guid16++ = 0;
        *guid16++ = 0; /* version */
        *guid16++ = 0;
    } else {
        *guid16++ = SDL_SwapLE16(BUS_BLUETOOTH);
        *guid16++ = 0;
        SDL_strlcpy((char*)guid16, pNewJoystick->joystickname, sizeof(pNewJoystick->guid.data) - 4);
    }

    SDL_SYS_AddJoystickDevice(pNewJoystick);

    return DIENUM_CONTINUE; /* get next device, please */
}
예제 #27
0
파일: testiconv.c 프로젝트: Distrotech/SDL
int
main(int argc, char *argv[])
{
    const char *formats[] = {
        "UTF8",
        "UTF-8",
        "UTF16BE",
        "UTF-16BE",
        "UTF16LE",
        "UTF-16LE",
        "UTF32BE",
        "UTF-32BE",
        "UTF32LE",
        "UTF-32LE",
        "UCS4",
        "UCS-4",
    };
    char buffer[BUFSIZ];
    char *ucs4;
    char *test[2];
    int i;
    FILE *file;
    int errors = 0;

    /* Enable standard application logging */
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);

    if (!argv[1]) {
        argv[1] = "utf8.txt";
    }
    file = fopen(argv[1], "rb");
    if (!file) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to open %s\n", argv[1]);
        return (1);
    }

    while (fgets(buffer, sizeof(buffer), file)) {
        /* Convert to UCS-4 */
        size_t len;
        ucs4 =
            SDL_iconv_string("UCS-4", "UTF-8", buffer,
                             SDL_strlen(buffer) + 1);
        len = (widelen(ucs4) + 1) * 4;
        for (i = 0; i < SDL_arraysize(formats); ++i) {
            test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
            test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
            if (!test[1] || SDL_memcmp(test[1], ucs4, len) != 0) {
                SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "FAIL: %s\n", formats[i]);
                ++errors;
            }
            SDL_free(test[0]);
            SDL_free(test[1]);
        }
        test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
        SDL_free(ucs4);
        fputs(test[0], stdout);
        SDL_free(test[0]);
    }
    fclose(file);
    return (errors ? errors + 1 : 0);
}