/* * Create a surface on the stack for quick blit operations */ static SDL_INLINE SDL_bool SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format, void * pixels, int pitch, SDL_Surface * surface, SDL_PixelFormat * format, SDL_BlitMap * blitmap) { if (SDL_ISPIXELFORMAT_INDEXED(pixel_format)) { SDL_SetError("Indexed pixel formats not supported"); return SDL_FALSE; } if (SDL_InitFormat(format, pixel_format) < 0) { return SDL_FALSE; } SDL_zerop(surface); surface->flags = SDL_PREALLOC; surface->format = format; surface->pixels = pixels; surface->w = width; surface->h = height; surface->pitch = pitch; /* We don't actually need to set up the clip rect for our purposes */ /* SDL_SetClipRect(surface, NULL); */ /* Allocate an empty mapping */ SDL_zerop(blitmap); blitmap->info.r = 0xFF; blitmap->info.g = 0xFF; blitmap->info.b = 0xFF; blitmap->info.a = 0xFF; surface->map = blitmap; /* The surface is ready to go */ surface->refcount = 1; return SDL_TRUE; }
SDL_DataQueue * SDL_NewDataQueue(const size_t _packetlen, const size_t initialslack) { SDL_DataQueue *queue = (SDL_DataQueue *) SDL_malloc(sizeof (SDL_DataQueue)); if (!queue) { SDL_OutOfMemory(); return NULL; } else { const size_t packetlen = _packetlen ? _packetlen : 1024; const size_t wantpackets = (initialslack + (packetlen - 1)) / packetlen; size_t i; SDL_zerop(queue); queue->packet_size = packetlen; for (i = 0; i < wantpackets; i++) { SDL_DataQueuePacket *packet = (SDL_DataQueuePacket *) SDL_malloc(sizeof (SDL_DataQueuePacket) + packetlen); if (packet) { /* don't care if this fails, we'll deal later. */ packet->datalen = 0; packet->startpos = 0; packet->next = queue->pool; queue->pool = packet; } } } return queue; }
void SDL_MouseQuit(void) { SDL_Cursor *cursor, *next; SDL_Mouse *mouse = SDL_GetMouse(); if (mouse->CaptureMouse) { SDL_CaptureMouse(SDL_FALSE); } SDL_SetRelativeMouseMode(SDL_FALSE); SDL_ShowCursor(1); cursor = mouse->cursors; while (cursor) { next = cursor->next; SDL_FreeCursor(cursor); cursor = next; } if (mouse->def_cursor && mouse->FreeCursor) { mouse->FreeCursor(mouse->def_cursor); } if (mouse->clickstate) { SDL_free(mouse->clickstate); } SDL_zerop(mouse); }
static void WIN_GL_SetupPixelFormat(_THIS, PIXELFORMATDESCRIPTOR * pfd) { SDL_zerop(pfd); pfd->nSize = sizeof(*pfd); pfd->nVersion = 1; pfd->dwFlags = (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL); if (_this->gl_config.double_buffer) { pfd->dwFlags |= PFD_DOUBLEBUFFER; } if (_this->gl_config.stereo) { pfd->dwFlags |= PFD_STEREO; } pfd->iLayerType = PFD_MAIN_PLANE; pfd->iPixelType = PFD_TYPE_RGBA; pfd->cRedBits = _this->gl_config.red_size; pfd->cGreenBits = _this->gl_config.green_size; pfd->cBlueBits = _this->gl_config.blue_size; pfd->cAlphaBits = _this->gl_config.alpha_size; if (_this->gl_config.buffer_size) { pfd->cColorBits = _this->gl_config.buffer_size - _this->gl_config.alpha_size; } else { pfd->cColorBits = (pfd->cRedBits + pfd->cGreenBits + pfd->cBlueBits); } pfd->cAccumRedBits = _this->gl_config.accum_red_size; pfd->cAccumGreenBits = _this->gl_config.accum_green_size; pfd->cAccumBlueBits = _this->gl_config.accum_blue_size; pfd->cAccumAlphaBits = _this->gl_config.accum_alpha_size; pfd->cAccumBits = (pfd->cAccumRedBits + pfd->cAccumGreenBits + pfd->cAccumBlueBits + pfd->cAccumAlphaBits); pfd->cDepthBits = _this->gl_config.depth_size; pfd->cStencilBits = _this->gl_config.stencil_size; }
void SDL_MouseQuit(void) { SDL_Cursor *cursor, *next; SDL_Mouse *mouse = SDL_GetMouse(); if (mouse->CaptureMouse) { SDL_CaptureMouse(SDL_FALSE); } SDL_SetRelativeMouseMode(SDL_FALSE); SDL_ShowCursor(1); cursor = mouse->cursors; while (cursor) { next = cursor->next; SDL_FreeCursor(cursor); cursor = next; } if (mouse->def_cursor && mouse->FreeCursor) { mouse->FreeCursor(mouse->def_cursor); } if (mouse->clickstate) { SDL_free(mouse->clickstate); } SDL_zerop(mouse); SDL_DelHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE, SDL_MouseNormalSpeedScaleChanged, mouse); SDL_DelHintCallback(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE, SDL_MouseRelativeSpeedScaleChanged, mouse); }
/* * Creates a new haptic effect. */ int SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * base) { int result; /* Alloc the effect. */ effect->hweffect = (struct haptic_hweffect *) SDL_malloc(sizeof(struct haptic_hweffect)); if (effect->hweffect == NULL) { SDL_OutOfMemory(); return -1; } SDL_zerop(effect->hweffect); if (haptic->hwdata->bXInputHaptic) { result = SDL_XINPUT_HapticNewEffect(haptic, effect, base); } else { result = SDL_DINPUT_HapticNewEffect(haptic, effect, base); } if (result < 0) { SDL_free(effect->hweffect); effect->hweffect = NULL; } return result; }
/* Public functions */ int SDL_MouseInit(void) { SDL_Mouse *mouse = SDL_GetMouse(); SDL_zerop(mouse); SDL_AddHintCallback(SDL_HINT_MOUSE_DOUBLE_CLICK_TIME, SDL_MouseDoubleClickTimeChanged, mouse); SDL_AddHintCallback(SDL_HINT_MOUSE_DOUBLE_CLICK_RADIUS, SDL_MouseDoubleClickRadiusChanged, mouse); SDL_AddHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE, SDL_MouseNormalSpeedScaleChanged, mouse); SDL_AddHintCallback(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE, SDL_MouseRelativeSpeedScaleChanged, mouse); SDL_AddHintCallback(SDL_HINT_TOUCH_MOUSE_EVENTS, SDL_TouchMouseEventsChanged, mouse); SDL_AddHintCallback(SDL_HINT_MOUSE_TOUCH_EVENTS, SDL_MouseTouchEventsChanged, mouse); mouse->cursor_shown = SDL_TRUE; return (0); }
static void AddXInputDevice(const Uint8 userid, BYTE SubType, JoyStick_DeviceData **pContext) { JoyStick_DeviceData *pPrevJoystick = NULL; JoyStick_DeviceData *pNewJoystick = *pContext; if (SDL_XInputUseOldJoystickMapping() && SubType != XINPUT_DEVSUBTYPE_GAMEPAD) return; if (SubType == XINPUT_DEVSUBTYPE_UNKNOWN) return; while (pNewJoystick) { if (pNewJoystick->bXInputDevice && (pNewJoystick->XInputUserId == userid) && (pNewJoystick->SubType == SubType)) { /* if we are replacing the front of the list then update it */ if (pNewJoystick == *pContext) { *pContext = pNewJoystick->pNext; } else if (pPrevJoystick) { pPrevJoystick->pNext = pNewJoystick->pNext; } pNewJoystick->pNext = SYS_Joystick; SYS_Joystick = pNewJoystick; return; /* already in the list. */ } pPrevJoystick = pNewJoystick; pNewJoystick = pNewJoystick->pNext; } pNewJoystick = (JoyStick_DeviceData *)SDL_malloc(sizeof(JoyStick_DeviceData)); if (!pNewJoystick) { return; /* better luck next time? */ } SDL_zerop(pNewJoystick); pNewJoystick->joystickname = GetXInputName(userid, SubType); if (!pNewJoystick->joystickname) { SDL_free(pNewJoystick); return; /* better luck next time? */ } pNewJoystick->bXInputDevice = SDL_TRUE; if (SDL_XInputUseOldJoystickMapping()) { SDL_zero(pNewJoystick->guid); } else { pNewJoystick->guid.data[0] = 'x'; pNewJoystick->guid.data[1] = 'i'; pNewJoystick->guid.data[2] = 'n'; pNewJoystick->guid.data[3] = 'p'; pNewJoystick->guid.data[4] = 'u'; pNewJoystick->guid.data[5] = 't'; pNewJoystick->guid.data[6] = SubType; } pNewJoystick->SubType = SubType; pNewJoystick->XInputUserId = userid; SDL_SYS_AddJoystickDevice(pNewJoystick); }
int Android_AddJoystick(int device_id, const char *name, const char *desc, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs) { SDL_JoystickGUID guid; SDL_joylist_item *item; if(JoystickByDeviceId(device_id) != NULL || name == NULL) { return -1; } /* the GUID is just the first 16 chars of the name for now */ SDL_zero( guid ); SDL_memcpy( &guid, desc, SDL_min( sizeof(guid), SDL_strlen( desc) ) ); item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item)); if (item == NULL) { return -1; } SDL_zerop(item); item->guid = guid; item->device_id = device_id; item->name = SDL_strdup(name); if ( item->name == NULL ) { SDL_free(item); return -1; } item->is_accelerometer = is_accelerometer; if (nbuttons > -1) { item->nbuttons = nbuttons; } else { item->nbuttons = ANDROID_MAX_NBUTTONS; } item->naxes = naxes; item->nhats = nhats; item->nballs = nballs; item->device_instance = instance_counter++; if (SDL_joylist_tail == NULL) { SDL_joylist = SDL_joylist_tail = item; } else { SDL_joylist_tail->next = item; SDL_joylist_tail = item; } /* Need to increment the joystick count before we post the event */ ++numjoysticks; SDL_PrivateJoystickAdded(numjoysticks - 1); #ifdef DEBUG_JOYSTICK SDL_Log("Added joystick %s with device_id %d", name, device_id); #endif return numjoysticks; }
static int PSPAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) { int format, mixlen, i; this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc(sizeof(*this->hidden)); if (this->hidden == NULL) { return SDL_OutOfMemory(); } SDL_zerop(this->hidden); switch (this->spec.format & 0xff) { case 8: case 16: this->spec.format = AUDIO_S16LSB; break; default: return SDL_SetError("Unsupported audio format"); } /* The sample count must be a multiple of 64. */ this->spec.samples = PSP_AUDIO_SAMPLE_ALIGN(this->spec.samples); this->spec.freq = 44100; /* Update the fragment size as size in bytes. */ SDL_CalculateAudioSpec(&this->spec); /* Allocate the mixing buffer. Its size and starting address must be a multiple of 64 bytes. Our sample count is already a multiple of 64, so spec->size should be a multiple of 64 as well. */ mixlen = this->spec.size * NUM_BUFFERS; this->hidden->rawbuf = (Uint8 *) memalign(64, mixlen); if (this->hidden->rawbuf == NULL) { return SDL_SetError("Couldn't allocate mixing buffer"); } /* Setup the hardware channel. */ if (this->spec.channels == 1) { format = PSP_AUDIO_FORMAT_MONO; } else { format = PSP_AUDIO_FORMAT_STEREO; } this->hidden->channel = sceAudioChReserve(PSP_AUDIO_NEXT_CHANNEL, this->spec.samples, format); if (this->hidden->channel < 0) { free(this->hidden->rawbuf); this->hidden->rawbuf = NULL; return SDL_SetError("Couldn't reserve hardware channel"); } memset(this->hidden->rawbuf, 0, mixlen); for (i = 0; i < NUM_BUFFERS; i++) { this->hidden->mixbufs[i] = &this->hidden->rawbuf[i * this->spec.size]; } this->hidden->next_buffer = 0; return 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 */ }
/* * Creates a new haptic effect. */ int SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * base) { HRESULT ret; REFGUID type = SDL_SYS_HapticEffectType(base); if (type == NULL) { goto err_hweffect; } /* Alloc the effect. */ effect->hweffect = (struct haptic_hweffect *) SDL_malloc(sizeof(struct haptic_hweffect)); if (effect->hweffect == NULL) { SDL_OutOfMemory(); goto err_hweffect; } SDL_zerop(effect->hweffect); if (haptic->hwdata->bXInputHaptic) { SDL_assert(base->type == SDL_HAPTIC_SINE); /* should catch this at higher level */ return SDL_SYS_HapticUpdateEffect(haptic, effect, base); } /* Get the effect. */ if (SDL_SYS_ToDIEFFECT(haptic, &effect->hweffect->effect, base) < 0) { goto err_effectdone; } /* Create the actual effect. */ ret = IDirectInputDevice8_CreateEffect(haptic->hwdata->device, type, &effect->hweffect->effect, &effect->hweffect->ref, NULL); if (FAILED(ret)) { DI_SetError("Unable to create effect", ret); goto err_effectdone; } return 0; err_effectdone: SDL_SYS_HapticFreeDIEFFECT(&effect->hweffect->effect, base->type); err_hweffect: if (effect->hweffect != NULL) { SDL_free(effect->hweffect); effect->hweffect = NULL; } return -1; }
SDL_PixelFormat * SDL_InitFormat(SDL_PixelFormat * format, int bpp, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) { Uint32 mask; /* Set up the format */ SDL_zerop(format); format->BitsPerPixel = bpp; format->BytesPerPixel = (bpp + 7) / 8; if (Rmask || Bmask || Gmask) { /* Packed pixels with custom mask */ format->Rshift = 0; format->Rloss = 8; if (Rmask) { for (mask = Rmask; !(mask & 0x01); mask >>= 1) ++format->Rshift; for (; (mask & 0x01); mask >>= 1) --format->Rloss; } format->Gshift = 0; format->Gloss = 8; if (Gmask) { for (mask = Gmask; !(mask & 0x01); mask >>= 1) ++format->Gshift; for (; (mask & 0x01); mask >>= 1) --format->Gloss; } format->Bshift = 0; format->Bloss = 8; if (Bmask) { for (mask = Bmask; !(mask & 0x01); mask >>= 1) ++format->Bshift; for (; (mask & 0x01); mask >>= 1) --format->Bloss; } format->Ashift = 0; format->Aloss = 8; if (Amask) { for (mask = Amask; !(mask & 0x01); mask >>= 1) ++format->Ashift; for (; (mask & 0x01); mask >>= 1) --format->Aloss; } format->Rmask = Rmask; format->Gmask = Gmask; format->Bmask = Bmask; format->Amask = Amask; } else if (bpp > 8) { /* Packed pixels with standard mask */
int SDL_XINPUT_MaybeAddDevice(const DWORD dwUserid) { const Uint8 userid = (Uint8)dwUserid; SDL_hapticlist_item *item; XINPUT_VIBRATION state; if ((!loaded_xinput) || (dwUserid >= XUSER_MAX_COUNT)) { return -1; } /* Make sure we don't already have it */ for (item = SDL_hapticlist; item; item = item->next) { if (item->bXInputHaptic && item->userid == userid) { return -1; /* Already added */ } } SDL_zero(state); if (XINPUTSETSTATE(dwUserid, &state) != ERROR_SUCCESS) { return -1; /* no force feedback on this device. */ } item = (SDL_hapticlist_item *)SDL_malloc(sizeof(SDL_hapticlist_item)); if (item == NULL) { return SDL_OutOfMemory(); } SDL_zerop(item); /* !!! FIXME: I'm not bothering to query for a real name right now (can we even?) */ { char buf[64]; SDL_snprintf(buf, sizeof(buf), "XInput Controller #%u", (unsigned int)(userid + 1)); item->name = SDL_strdup(buf); } if (!item->name) { SDL_free(item); return -1; } /* Copy the instance over, useful for creating devices. */ item->bXInputHaptic = SDL_TRUE; item->userid = userid; return SDL_SYS_AddHapticDevice(item); }
/* Routine to get the thread-specific error variable */ SDL_error * SDL_GetErrBuf(void) { static SDL_SpinLock tls_lock; static SDL_bool tls_being_created; static SDL_TLSID tls_errbuf; static SDL_error SDL_global_errbuf; const SDL_error *ALLOCATION_IN_PROGRESS = (SDL_error *)-1; SDL_error *errbuf; /* tls_being_created is there simply to prevent recursion if SDL_TLSCreate() fails. It also means it's possible for another thread to also use SDL_global_errbuf, but that's very unlikely and hopefully won't cause issues. */ if (!tls_errbuf && !tls_being_created) { SDL_AtomicLock(&tls_lock); if (!tls_errbuf) { SDL_TLSID slot; tls_being_created = SDL_TRUE; slot = SDL_TLSCreate(); tls_being_created = SDL_FALSE; SDL_MemoryBarrierRelease(); tls_errbuf = slot; } SDL_AtomicUnlock(&tls_lock); } if (!tls_errbuf) { return &SDL_global_errbuf; } SDL_MemoryBarrierAcquire(); errbuf = (SDL_error *)SDL_TLSGet(tls_errbuf); if (errbuf == ALLOCATION_IN_PROGRESS) { return &SDL_global_errbuf; } if (!errbuf) { /* Mark that we're in the middle of allocating our buffer */ SDL_TLSSet(tls_errbuf, ALLOCATION_IN_PROGRESS, NULL); errbuf = (SDL_error *)SDL_malloc(sizeof(*errbuf)); if (!errbuf) { SDL_TLSSet(tls_errbuf, NULL, NULL); return &SDL_global_errbuf; } SDL_zerop(errbuf); SDL_TLSSet(tls_errbuf, errbuf, SDL_free); } return errbuf; }
void SDL_MouseQuit(void) { SDL_Cursor *cursor, *next; SDL_Mouse *mouse = SDL_GetMouse(); SDL_ShowCursor(1); cursor = mouse->cursors; while (cursor) { next = cursor->next; SDL_FreeCursor(cursor); cursor = next; } if (mouse->def_cursor && mouse->FreeCursor) { mouse->FreeCursor(mouse->def_cursor); } SDL_zerop(mouse); }
static SDL_bool PixelFormatToDDPIXELFORMAT(Uint32 format, LPDDPIXELFORMAT dst) { SDL_zerop(dst); dst->dwSize = sizeof(*dst); if (SDL_ISPIXELFORMAT_FOURCC(format)) { dst->dwFlags = DDPF_FOURCC; dst->dwFourCC = format; } else if (SDL_ISPIXELFORMAT_INDEXED(format)) { SDL_SetError("Indexed pixelformats are not supported."); return SDL_FALSE; } else { int bpp; Uint32 Rmask, Gmask, Bmask, Amask; if (!SDL_PixelFormatEnumToMasks (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) { SDL_SetError("pixelformat not supported"); return SDL_FALSE; } if (!Rmask && !Gmask && !Bmask) { dst->dwFlags = DDPF_ALPHA; dst->dwAlphaBitDepth = bpp; } else { dst->dwFlags = DDPF_RGB; dst->dwRGBBitCount = bpp; dst->dwRBitMask = Rmask; dst->dwGBitMask = Gmask; dst->dwBBitMask = Bmask; if (Amask) { dst->dwFlags |= DDPF_ALPHAPIXELS; dst->dwRGBAlphaBitMask = Amask; } } } return SDL_TRUE; }
static SDL_bool PrepWaveFormat(_THIS, UINT devId, WAVEFORMATEX *pfmt, const int iscapture) { SDL_zerop(pfmt); if (SDL_AUDIO_ISFLOAT(this->spec.format)) { pfmt->wFormatTag = WAVE_FORMAT_IEEE_FLOAT; } else { pfmt->wFormatTag = WAVE_FORMAT_PCM; } pfmt->wBitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format); pfmt->nChannels = this->spec.channels; pfmt->nSamplesPerSec = this->spec.freq; pfmt->nBlockAlign = pfmt->nChannels * (pfmt->wBitsPerSample / 8); pfmt->nAvgBytesPerSec = pfmt->nSamplesPerSec * pfmt->nBlockAlign; if (iscapture) { return (waveInOpen(0, devId, pfmt, 0, 0, WAVE_FORMAT_QUERY) == 0); } else { return (waveOutOpen(0, devId, pfmt, 0, 0, WAVE_FORMAT_QUERY) == 0); } }
/* * Initializes the haptic device for simple rumble playback. */ int SDL_HapticRumbleInit(SDL_Haptic * haptic) { SDL_HapticEffect *efx = &haptic->rumble_effect; if (!ValidHaptic(haptic)) { return -1; } /* Already allocated. */ if (haptic->rumble_id >= 0) { return 0; } SDL_zerop(efx); if (haptic->supported & SDL_HAPTIC_SINE) { efx->type = SDL_HAPTIC_SINE; efx->periodic.period = 1000; efx->periodic.magnitude = 0x4000; efx->periodic.length = 5000; efx->periodic.attack_length = 0; efx->periodic.fade_length = 0; } else if (haptic->supported & SDL_HAPTIC_LEFTRIGHT) { /* XInput? */ efx->type = SDL_HAPTIC_LEFTRIGHT; efx->leftright.length = 5000; efx->leftright.large_magnitude = 0x4000; efx->leftright.small_magnitude = 0x4000; } else { return SDL_SetError("Device doesn't support rumble"); } haptic->rumble_id = SDL_HapticNewEffect(haptic, &haptic->rumble_effect); if (haptic->rumble_id >= 0) { return 0; } return -1; }
int main(int argc, char **argv) { // Init stuff //srand((unsigned int) time(NULL)); SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER); Hero_PrintSDLVersion(); // Audio stuff Hero_AudioDef audio_def = Hero_InitAudio(); // Create the window and renderer SDL_Window *window = SDL_CreateWindow( "Handmade Hero", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, k_window_width, k_window_height, SDL_WINDOW_RESIZABLE); // Get the window surface, make a copy of it and update the window SDL_Surface *source = SDL_GetWindowSurface(window); g_backbuffer = SDL_ConvertSurfaceFormat(source, source->format->format, 0); Hero_ResizeAndUpdateWindow(window, g_backbuffer, SDL_TRUE); SDL_FreeSurface(source); // Loop things SDL_bool sound_is_playing = SDL_FALSE; Uint32 frame_step = 0; SDL_bool running = SDL_TRUE; //SDL_SetCursor(Hero_InitSystemCursor(arrow)); //SDL_ShowCursor(SDL_ENABLE); // Game Setup _Hero_UpdateGameState Hero_UpdateGameState = NULL; Hero_GameInput *game_input = SDL_malloc(sizeof(Hero_GameInput)); SDL_zerop(game_input); Hero_GameState *game_state = SDL_malloc(sizeof(Hero_GameState)); SDL_zerop(game_state); game_state->player_position.abs_tile_x = 3; game_state->player_position.abs_tile_y = 3; game_state->player_position.tile_rel_x = 5.0f; game_state->player_position.tile_rel_y = 5.0f; while (running) { // Performance Uint64 perf_freq = SDL_GetPerformanceFrequency(); Uint64 perf_counter_start = SDL_GetPerformanceCounter(); #ifdef HERO_DYNLOAD // Load our library every n frames if ((frame_step % 30) == 0 || Hero_UpdateGameState == NULL) { SDL_UnloadObject(g_logic_lib); //log_debug("reloading symbols"); g_logic_lib = SDL_LoadObject("libherologic.so"); Hero_UpdateGameState = SDL_LoadFunction(g_logic_lib, "Hero_UpdateGameState"); } #endif // Actual game stuff running = Hero_HandleEvents(game_input); //SDL_GetMouseState(&g_mouse_position.x, &g_mouse_position.y); SDL_assert(Hero_UpdateGameState); Hero_UpdateGameState(game_state, game_input, g_backbuffer, audio_def); Hero_ResizeAndUpdateWindow(window, g_backbuffer, SDL_FALSE); //Hero_DebugPlayTestSquareWave(audio_def); if (!sound_is_playing) { SDL_PauseAudioDevice(g_audio_device, 0); sound_is_playing = SDL_TRUE; } // Performance Uint64 perf_counter_end = SDL_GetPerformanceCounter(); Uint64 perf_counter_elapsed = perf_counter_end - perf_counter_start; double perf_per_frame = (( (1000.0f * (double) perf_counter_elapsed) / (double) perf_freq)); // Ensure we are at a constant framerate double fps_padding_time = k_display_msmax - perf_per_frame; // Save the value for movement speed adjustment game_input->frame_dt = (float) fps_padding_time * 0.01f; if (fps_padding_time > 0) SDL_Delay((Uint32) fps_padding_time); if ((frame_step % 30) == 0) log_debug("Frame stats: %f ms, max %f ms, padding %f ms", perf_per_frame, k_display_msmax, fps_padding_time); frame_step++; } log_debug("Shutting down..."); if (g_game_controller != NULL) SDL_GameControllerClose(g_game_controller); SDL_DestroyWindow(window); SDL_Quit(); return 0; }
static int XAUDIO2_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) { HRESULT result = S_OK; WAVEFORMATEX waveformat; int valid_format = 0; SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format); IXAudio2 *ixa2 = NULL; IXAudio2SourceVoice *source = NULL; #if defined(SDL_XAUDIO2_WIN8) LPCWSTR devId = NULL; #else UINT32 devId = 0; /* 0 == system default device. */ #endif static IXAudio2VoiceCallbackVtbl callbacks_vtable = { VoiceCBOnVoiceProcessPassStart, VoiceCBOnVoiceProcessPassEnd, VoiceCBOnStreamEnd, VoiceCBOnBufferStart, VoiceCBOnBufferEnd, VoiceCBOnLoopEnd, VoiceCBOnVoiceError }; static IXAudio2VoiceCallback callbacks = { &callbacks_vtable }; #if defined(SDL_XAUDIO2_WIN8) /* !!! FIXME: hook up hotplugging. */ #else if (handle != NULL) { /* specific device requested? */ /* -1 because we increment the original value to avoid NULL. */ const size_t val = ((size_t) handle) - 1; devId = (UINT32) val; } #endif if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) { return SDL_SetError("XAudio2: XAudio2Create() failed at open."); } /* XAUDIO2_DEBUG_CONFIGURATION debugConfig; debugConfig.TraceMask = XAUDIO2_LOG_ERRORS; //XAUDIO2_LOG_WARNINGS | XAUDIO2_LOG_DETAIL | XAUDIO2_LOG_FUNC_CALLS | XAUDIO2_LOG_TIMING | XAUDIO2_LOG_LOCKS | XAUDIO2_LOG_MEMORY | XAUDIO2_LOG_STREAMING; debugConfig.BreakMask = XAUDIO2_LOG_ERRORS; //XAUDIO2_LOG_WARNINGS; debugConfig.LogThreadID = TRUE; debugConfig.LogFileline = TRUE; debugConfig.LogFunctionName = TRUE; debugConfig.LogTiming = TRUE; ixa2->SetDebugConfiguration(&debugConfig); */ /* Initialize all variables that we clean on shutdown */ this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc((sizeof *this->hidden)); if (this->hidden == NULL) { IXAudio2_Release(ixa2); return SDL_OutOfMemory(); } SDL_zerop(this->hidden); this->hidden->ixa2 = ixa2; this->hidden->semaphore = SDL_CreateSemaphore(1); if (this->hidden->semaphore == NULL) { return SDL_SetError("XAudio2: CreateSemaphore() failed!"); } while ((!valid_format) && (test_format)) { switch (test_format) { case AUDIO_U8: case AUDIO_S16: case AUDIO_S32: case AUDIO_F32: this->spec.format = test_format; valid_format = 1; break; } test_format = SDL_NextAudioFormat(); } if (!valid_format) { return SDL_SetError("XAudio2: Unsupported audio format"); } /* Update the fragment size as size in bytes */ SDL_CalculateAudioSpec(&this->spec); /* We feed a Source, it feeds the Mastering, which feeds the device. */ this->hidden->mixlen = this->spec.size; this->hidden->mixbuf = (Uint8 *) SDL_malloc(2 * this->hidden->mixlen); if (this->hidden->mixbuf == NULL) { return SDL_OutOfMemory(); } this->hidden->nextbuf = this->hidden->mixbuf; SDL_memset(this->hidden->mixbuf, this->spec.silence, 2 * this->hidden->mixlen); /* We use XAUDIO2_DEFAULT_CHANNELS instead of this->spec.channels. On Xbox360, this means 5.1 output, but on Windows, it means "figure out what the system has." It might be preferable to let XAudio2 blast stereo output to appropriate surround sound configurations instead of clamping to 2 channels, even though we'll configure the Source Voice for whatever number of channels you supply. */ #if SDL_XAUDIO2_WIN8 result = IXAudio2_CreateMasteringVoice(ixa2, &this->hidden->mastering, XAUDIO2_DEFAULT_CHANNELS, this->spec.freq, 0, devId, NULL, AudioCategory_GameEffects); #else result = IXAudio2_CreateMasteringVoice(ixa2, &this->hidden->mastering, XAUDIO2_DEFAULT_CHANNELS, this->spec.freq, 0, devId, NULL); #endif if (result != S_OK) { return SDL_SetError("XAudio2: Couldn't create mastering voice"); } SDL_zero(waveformat); if (SDL_AUDIO_ISFLOAT(this->spec.format)) { waveformat.wFormatTag = WAVE_FORMAT_IEEE_FLOAT; } else { waveformat.wFormatTag = WAVE_FORMAT_PCM; } waveformat.wBitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format); waveformat.nChannels = this->spec.channels; waveformat.nSamplesPerSec = this->spec.freq; waveformat.nBlockAlign = waveformat.nChannels * (waveformat.wBitsPerSample / 8); waveformat.nAvgBytesPerSec = waveformat.nSamplesPerSec * waveformat.nBlockAlign; waveformat.cbSize = sizeof(waveformat); #ifdef __WINRT__ // DLudwig: for now, make XAudio2 do sample rate conversion, just to // get the loopwave test to work. // // TODO, WinRT: consider removing WinRT-specific source-voice creation code from SDL_xaudio2.c result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat, 0, 1.0f, &callbacks, NULL, NULL); #else result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat, XAUDIO2_VOICE_NOSRC | XAUDIO2_VOICE_NOPITCH, 1.0f, &callbacks, NULL, NULL); #endif if (result != S_OK) { return SDL_SetError("XAudio2: Couldn't create source voice"); } this->hidden->source = source; /* Start everything playing! */ result = IXAudio2_StartEngine(ixa2); if (result != S_OK) { return SDL_SetError("XAudio2: Couldn't start engine"); } result = IXAudio2SourceVoice_Start(source, 0, XAUDIO2_COMMIT_NOW); if (result != S_OK) { return SDL_SetError("XAudio2: Couldn't start source voice"); } return 0; /* good to go. */ }
static int DSP_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) { const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT); int format; int value; int frag_spec; SDL_AudioFormat test_format; /* We don't care what the devname is...we'll try to open anything. */ /* ...but default to first name in the list... */ if (devname == NULL) { devname = SDL_GetAudioDeviceName(0, iscapture); if (devname == NULL) { return SDL_SetError("No such audio device"); } } /* Make sure fragment size stays a power of 2, or OSS fails. */ /* I don't know which of these are actually legal values, though... */ if (this->spec.channels > 8) this->spec.channels = 8; else if (this->spec.channels > 4) this->spec.channels = 4; else if (this->spec.channels > 2) this->spec.channels = 2; /* Initialize all variables that we clean on shutdown */ this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc((sizeof *this->hidden)); if (this->hidden == NULL) { return SDL_OutOfMemory(); } SDL_zerop(this->hidden); /* Open the audio device */ this->hidden->audio_fd = open(devname, flags, 0); if (this->hidden->audio_fd < 0) { return SDL_SetError("Couldn't open %s: %s", devname, strerror(errno)); } /* Make the file descriptor use blocking i/o with fcntl() */ { long ctlflags; ctlflags = fcntl(this->hidden->audio_fd, F_GETFL); ctlflags &= ~O_NONBLOCK; if (fcntl(this->hidden->audio_fd, F_SETFL, ctlflags) < 0) { return SDL_SetError("Couldn't set audio blocking mode"); } } /* Get a list of supported hardware formats */ if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0) { perror("SNDCTL_DSP_GETFMTS"); return SDL_SetError("Couldn't get audio format list"); } /* Try for a closest match on audio format */ format = 0; for (test_format = SDL_FirstAudioFormat(this->spec.format); !format && test_format;) { #ifdef DEBUG_AUDIO fprintf(stderr, "Trying format 0x%4.4x\n", test_format); #endif switch (test_format) { case AUDIO_U8: if (value & AFMT_U8) { format = AFMT_U8; } break; case AUDIO_S16LSB: if (value & AFMT_S16_LE) { format = AFMT_S16_LE; } break; case AUDIO_S16MSB: if (value & AFMT_S16_BE) { format = AFMT_S16_BE; } break; #if 0 /* * These formats are not used by any real life systems so they are not * needed here. */ case AUDIO_S8: if (value & AFMT_S8) { format = AFMT_S8; } break; case AUDIO_U16LSB: if (value & AFMT_U16_LE) { format = AFMT_U16_LE; } break; case AUDIO_U16MSB: if (value & AFMT_U16_BE) { format = AFMT_U16_BE; } break; #endif default: format = 0; break; } if (!format) { test_format = SDL_NextAudioFormat(); } } if (format == 0) { return SDL_SetError("Couldn't find any hardware audio formats"); } this->spec.format = test_format; /* Set the audio format */ value = format; if ((ioctl(this->hidden->audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) || (value != format)) { perror("SNDCTL_DSP_SETFMT"); return SDL_SetError("Couldn't set audio format"); } /* Set the number of channels of output */ value = this->spec.channels; if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0) { perror("SNDCTL_DSP_CHANNELS"); return SDL_SetError("Cannot set the number of channels"); } this->spec.channels = value; /* Set the DSP frequency */ value = this->spec.freq; if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_SPEED, &value) < 0) { perror("SNDCTL_DSP_SPEED"); return SDL_SetError("Couldn't set audio frequency"); } this->spec.freq = value; /* Calculate the final parameters for this audio specification */ SDL_CalculateAudioSpec(&this->spec); /* Determine the power of two of the fragment size */ for (frag_spec = 0; (0x01U << frag_spec) < this->spec.size; ++frag_spec); if ((0x01U << frag_spec) != this->spec.size) { return SDL_SetError("Fragment size must be a power of two"); } frag_spec |= 0x00020000; /* two fragments, for low latency */ /* Set the audio buffering parameters */ #ifdef DEBUG_AUDIO fprintf(stderr, "Requesting %d fragments of size %d\n", (frag_spec >> 16), 1 << (frag_spec & 0xFFFF)); #endif if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0) { perror("SNDCTL_DSP_SETFRAGMENT"); } #ifdef DEBUG_AUDIO { audio_buf_info info; ioctl(this->hidden->audio_fd, SNDCTL_DSP_GETOSPACE, &info); fprintf(stderr, "fragments = %d\n", info.fragments); fprintf(stderr, "fragstotal = %d\n", info.fragstotal); fprintf(stderr, "fragsize = %d\n", info.fragsize); fprintf(stderr, "bytes = %d\n", info.bytes); } #endif /* Allocate mixing buffer */ if (!iscapture) { this->hidden->mixlen = this->spec.size; this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->hidden->mixlen); if (this->hidden->mixbuf == NULL) { return SDL_OutOfMemory(); } SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size); } /* We're ready to rock and roll. :-) */ return 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 */ }
int Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs) { SDL_JoystickGUID guid; SDL_joylist_item *item; #if !SDL_EVENTS_DISABLED SDL_Event event; #endif if(JoystickByDeviceId(device_id) != NULL || name == NULL) { return -1; } /* the GUID is just the first 16 chars of the name for now */ SDL_zero( guid ); SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) ); item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item)); if (item == NULL) { return -1; } SDL_zerop(item); item->guid = guid; item->device_id = device_id; item->name = SDL_strdup(name); if ( item->name == NULL ) { SDL_free(item); return -1; } item->is_accelerometer = is_accelerometer; if (nbuttons > -1) { item->nbuttons = nbuttons; } else { item->nbuttons = ANDROID_MAX_NBUTTONS; } item->naxes = naxes; item->nhats = nhats; item->nballs = nballs; item->device_instance = instance_counter++; if (SDL_joylist_tail == NULL) { SDL_joylist = SDL_joylist_tail = item; } else { SDL_joylist_tail->next = item; SDL_joylist_tail = item; } /* Need to increment the joystick count before we post the event */ ++numjoysticks; #if !SDL_EVENTS_DISABLED event.type = SDL_JOYDEVICEADDED; if (SDL_GetEventState(event.type) == SDL_ENABLE) { event.jdevice.which = (numjoysticks - 1); if ( (SDL_EventOK == NULL) || (*SDL_EventOK) (SDL_EventOKParam, &event) ) { SDL_PushEvent(&event); } } #endif /* !SDL_EVENTS_DISABLED */ SDL_Log("Added joystick %s with device_id %d", name, device_id); return numjoysticks; }
static int PAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) { const char *workaround = SDL_getenv("SDL_DSP_NOSELECT"); char audiodev[1024]; const char *err = NULL; int format; int bytes_per_sample; SDL_AudioFormat test_format; audio_init paud_init; audio_buffer paud_bufinfo; audio_status paud_status; audio_control paud_control; audio_change paud_change; int fd = -1; /* Initialize all variables that we clean on shutdown */ this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc((sizeof *this->hidden)); if (this->hidden == NULL) { return SDL_OutOfMemory(); } SDL_zerop(this->hidden); /* Open the audio device */ fd = OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0); this->hidden->audio_fd = fd; if (fd < 0) { return SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno)); } /* * We can't set the buffer size - just ask the device for the maximum * that we can have. */ if (ioctl(fd, AUDIO_BUFFER, &paud_bufinfo) < 0) { return SDL_SetError("Couldn't get audio buffer information"); } if (this->spec.channels > 1) this->spec.channels = 2; else this->spec.channels = 1; /* * Fields in the audio_init structure: * * Ignored by us: * * paud.loadpath[LOAD_PATH]; * DSP code to load, MWave chip only? * paud.slot_number; * slot number of the adapter * paud.device_id; * adapter identification number * * Input: * * paud.srate; * the sampling rate in Hz * paud.bits_per_sample; * 8, 16, 32, ... * paud.bsize; * block size for this rate * paud.mode; * ADPCM, PCM, MU_LAW, A_LAW, SOURCE_MIX * paud.channels; * 1=mono, 2=stereo * paud.flags; * FIXED - fixed length data * * LEFT_ALIGNED, RIGHT_ALIGNED (var len only) * * TWOS_COMPLEMENT - 2's complement data * * SIGNED - signed? comment seems wrong in sys/audio.h * * BIG_ENDIAN * paud.operation; * PLAY, RECORD * * Output: * * paud.flags; * PITCH - pitch is supported * * INPUT - input is supported * * OUTPUT - output is supported * * MONITOR - monitor is supported * * VOLUME - volume is supported * * VOLUME_DELAY - volume delay is supported * * BALANCE - balance is supported * * BALANCE_DELAY - balance delay is supported * * TREBLE - treble control is supported * * BASS - bass control is supported * * BESTFIT_PROVIDED - best fit returned * * LOAD_CODE - DSP load needed * paud.rc; * NO_PLAY - DSP code can't do play requests * * NO_RECORD - DSP code can't do record requests * * INVALID_REQUEST - request was invalid * * CONFLICT - conflict with open's flags * * OVERLOADED - out of DSP MIPS or memory * paud.position_resolution; * smallest increment for position */ paud_init.srate = this->spec.freq; paud_init.mode = PCM; paud_init.operation = PLAY; paud_init.channels = this->spec.channels; /* Try for a closest match on audio format */ format = 0; for (test_format = SDL_FirstAudioFormat(this->spec.format); !format && test_format;) { #ifdef DEBUG_AUDIO fprintf(stderr, "Trying format 0x%4.4x\n", test_format); #endif switch (test_format) { case AUDIO_U8: bytes_per_sample = 1; paud_init.bits_per_sample = 8; paud_init.flags = TWOS_COMPLEMENT | FIXED; format = 1; break; case AUDIO_S8: bytes_per_sample = 1; paud_init.bits_per_sample = 8; paud_init.flags = SIGNED | TWOS_COMPLEMENT | FIXED; format = 1; break; case AUDIO_S16LSB: bytes_per_sample = 2; paud_init.bits_per_sample = 16; paud_init.flags = SIGNED | TWOS_COMPLEMENT | FIXED; format = 1; break; case AUDIO_S16MSB: bytes_per_sample = 2; paud_init.bits_per_sample = 16; paud_init.flags = BIG_ENDIAN | SIGNED | TWOS_COMPLEMENT | FIXED; format = 1; break; case AUDIO_U16LSB: bytes_per_sample = 2; paud_init.bits_per_sample = 16; paud_init.flags = TWOS_COMPLEMENT | FIXED; format = 1; break; case AUDIO_U16MSB: bytes_per_sample = 2; paud_init.bits_per_sample = 16; paud_init.flags = BIG_ENDIAN | TWOS_COMPLEMENT | FIXED; format = 1; break; default: break; } if (!format) { test_format = SDL_NextAudioFormat(); } } if (format == 0) { #ifdef DEBUG_AUDIO fprintf(stderr, "Couldn't find any hardware audio formats\n"); #endif return SDL_SetError("Couldn't find any hardware audio formats"); } this->spec.format = test_format; /* * We know the buffer size and the max number of subsequent writes * that can be pending. If more than one can pend, allow the application * to do something like double buffering between our write buffer and * the device's own buffer that we are filling with write() anyway. * * We calculate this->spec.samples like this because * SDL_CalculateAudioSpec() will give put paud_bufinfo.write_buf_cap * (or paud_bufinfo.write_buf_cap/2) into this->spec.size in return. */ if (paud_bufinfo.request_buf_cap == 1) { this->spec.samples = paud_bufinfo.write_buf_cap / bytes_per_sample / this->spec.channels; } else { this->spec.samples = paud_bufinfo.write_buf_cap / bytes_per_sample / this->spec.channels / 2; } paud_init.bsize = bytes_per_sample * this->spec.channels; SDL_CalculateAudioSpec(&this->spec); /* * The AIX paud device init can't modify the values of the audio_init * structure that we pass to it. So we don't need any recalculation * of this stuff and no reinit call as in linux dsp code. * * /dev/paud supports all of the encoding formats, so we don't need * to do anything like reopening the device, either. */ if (ioctl(fd, AUDIO_INIT, &paud_init) < 0) { switch (paud_init.rc) { case 1: err = "Couldn't set audio format: DSP can't do play requests"; break; case 2: err = "Couldn't set audio format: DSP can't do record requests"; break; case 4: err = "Couldn't set audio format: request was invalid"; break; case 5: err = "Couldn't set audio format: conflict with open's flags"; break; case 6: err = "Couldn't set audio format: out of DSP MIPS or memory"; break; default: err = "Couldn't set audio format: not documented in sys/audio.h"; break; } } if (err != NULL) { return SDL_SetError("Paudio: %s", err); } /* Allocate mixing buffer */ this->hidden->mixlen = this->spec.size; this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->hidden->mixlen); if (this->hidden->mixbuf == NULL) { return SDL_OutOfMemory(); } SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size); /* * Set some paramters: full volume, first speaker that we can find. * Ignore the other settings for now. */ paud_change.input = AUDIO_IGNORE; /* the new input source */ paud_change.output = OUTPUT_1; /* EXTERNAL_SPEAKER,INTERNAL_SPEAKER,OUTPUT_1 */ paud_change.monitor = AUDIO_IGNORE; /* the new monitor state */ paud_change.volume = 0x7fffffff; /* volume level [0-0x7fffffff] */ paud_change.volume_delay = AUDIO_IGNORE; /* the new volume delay */ paud_change.balance = 0x3fffffff; /* the new balance */ paud_change.balance_delay = AUDIO_IGNORE; /* the new balance delay */ paud_change.treble = AUDIO_IGNORE; /* the new treble state */ paud_change.bass = AUDIO_IGNORE; /* the new bass state */ paud_change.pitch = AUDIO_IGNORE; /* the new pitch state */ paud_control.ioctl_request = AUDIO_CHANGE; paud_control.request_info = (char *) &paud_change; if (ioctl(fd, AUDIO_CONTROL, &paud_control) < 0) { #ifdef DEBUG_AUDIO fprintf(stderr, "Can't change audio display settings\n"); #endif } /* * Tell the device to expect data. Actual start will wait for * the first write() call. */ paud_control.ioctl_request = AUDIO_START; paud_control.position = 0; if (ioctl(fd, AUDIO_CONTROL, &paud_control) < 0) { #ifdef DEBUG_AUDIO fprintf(stderr, "Can't start audio play\n"); #endif return SDL_SetError("Can't start audio play"); } /* Check to see if we need to use select() workaround */ if (workaround != NULL) { this->hidden->frame_ticks = (float) (this->spec.samples * 1000) / this->spec.freq; this->hidden->next_frame = SDL_GetTicks() + this->hidden->frame_ticks; } /* We're ready to rock and roll. :-) */ return 0; }
/* !!! FIXME: I would love to dump this code and use libudev instead. */ static int MaybeAddDevice(const char *path) { struct stat sb; int fd = -1; int isstick = 0; char namebuf[128]; SDL_JoystickGUID guid; SDL_joylist_item *item; if (path == NULL) { return -1; } if (stat(path, &sb) == -1) { return -1; } /* Check to make sure it's not already in list. */ for (item = SDL_joylist; item != NULL; item = item->next) { if (sb.st_rdev == item->devnum) { return -1; /* already have this one */ } } fd = open(path, O_RDONLY, 0); if (fd < 0) { return -1; } #ifdef DEBUG_INPUT_EVENTS printf("Checking %s\n", path); #endif isstick = IsJoystick(fd, namebuf, sizeof (namebuf), &guid); close(fd); if (!isstick) { return -1; } item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item)); if (item == NULL) { return -1; } SDL_zerop(item); item->devnum = sb.st_rdev; item->path = SDL_strdup(path); item->name = SDL_strdup(namebuf); item->guid = guid; if ( (item->path == NULL) || (item->name == NULL) ) { SDL_free(item->path); SDL_free(item->name); SDL_free(item); return -1; } item->device_instance = instance_counter++; if (SDL_joylist_tail == NULL) { SDL_joylist = SDL_joylist_tail = item; } else { SDL_joylist_tail->next = item; SDL_joylist_tail = item; } /* Need to increment the joystick count before we post the event */ ++numjoysticks; SDL_PrivateJoystickAdded(numjoysticks - 1); return numjoysticks; }
static int SDL_FS_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) { int bytes; SDL_AudioFormat test_format = 0, format = 0; FSSampleFormat fs_format; FSStreamDescription desc; DirectResult ret; /* Initialize all variables that we clean on shutdown */ this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc((sizeof *this->hidden)); if (this->hidden == NULL) { return SDL_OutOfMemory(); } SDL_zerop(this->hidden); /* Try for a closest match on audio format */ for (test_format = SDL_FirstAudioFormat(this->spec.format); !format && test_format;) { #ifdef DEBUG_AUDIO fprintf(stderr, "Trying format 0x%4.4x\n", test_format); #endif switch (test_format) { case AUDIO_U8: fs_format = FSSF_U8; bytes = 1; format = 1; break; case AUDIO_S16SYS: fs_format = FSSF_S16; bytes = 2; format = 1; break; case AUDIO_S32SYS: fs_format = FSSF_S32; bytes = 4; format = 1; break; case AUDIO_F32SYS: fs_format = FSSF_FLOAT; bytes = 4; format = 1; break; default: format = 0; break; } if (!format) { test_format = SDL_NextAudioFormat(); } } if (format == 0) { return SDL_SetError("Couldn't find any hardware audio formats"); } this->spec.format = test_format; /* Retrieve the main sound interface. */ ret = SDL_NAME(FusionSoundCreate) (&this->hidden->fs); if (ret) { return SDL_SetError("Unable to initialize FusionSound: %d", ret); } this->hidden->mixsamples = this->spec.size / bytes / this->spec.channels; /* Fill stream description. */ desc.flags = FSSDF_SAMPLERATE | FSSDF_BUFFERSIZE | FSSDF_CHANNELS | FSSDF_SAMPLEFORMAT | FSSDF_PREBUFFER; desc.samplerate = this->spec.freq; desc.buffersize = this->spec.size * FUSION_BUFFERS; desc.channels = this->spec.channels; desc.prebuffer = 10; desc.sampleformat = fs_format; ret = this->hidden->fs->CreateStream(this->hidden->fs, &desc, &this->hidden->stream); if (ret) { return SDL_SetError("Unable to create FusionSoundStream: %d", ret); } /* See what we got */ desc.flags = FSSDF_SAMPLERATE | FSSDF_BUFFERSIZE | FSSDF_CHANNELS | FSSDF_SAMPLEFORMAT; ret = this->hidden->stream->GetDescription(this->hidden->stream, &desc); this->spec.freq = desc.samplerate; this->spec.size = desc.buffersize / FUSION_BUFFERS * bytes * desc.channels; this->spec.channels = desc.channels; /* Calculate the final parameters for this audio specification */ SDL_CalculateAudioSpec(&this->spec); /* Allocate mixing buffer */ this->hidden->mixlen = this->spec.size; this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->hidden->mixlen); if (this->hidden->mixbuf == NULL) { return SDL_OutOfMemory(); } SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size); /* We're ready to rock and roll. :-) */ return 0; }
static void AddXInputDevice(Uint8 userid, BYTE SubType, JoyStick_DeviceData **pContext) { Uint16 vendor = 0; Uint16 product = 0; Uint16 version = 0; JoyStick_DeviceData *pPrevJoystick = NULL; JoyStick_DeviceData *pNewJoystick = *pContext; if (SDL_XInputUseOldJoystickMapping() && SubType != XINPUT_DEVSUBTYPE_GAMEPAD) return; if (SubType == XINPUT_DEVSUBTYPE_UNKNOWN) return; while (pNewJoystick) { if (pNewJoystick->bXInputDevice && (pNewJoystick->XInputUserId == userid) && (pNewJoystick->SubType == SubType)) { /* if we are replacing the front of the list then update it */ if (pNewJoystick == *pContext) { *pContext = pNewJoystick->pNext; } else if (pPrevJoystick) { pPrevJoystick->pNext = pNewJoystick->pNext; } pNewJoystick->pNext = SYS_Joystick; SYS_Joystick = pNewJoystick; return; /* already in the list. */ } pPrevJoystick = pNewJoystick; pNewJoystick = pNewJoystick->pNext; } pNewJoystick = (JoyStick_DeviceData *)SDL_malloc(sizeof(JoyStick_DeviceData)); if (!pNewJoystick) { return; /* better luck next time? */ } SDL_zerop(pNewJoystick); pNewJoystick->joystickname = GetXInputName(userid, SubType); if (!pNewJoystick->joystickname) { SDL_free(pNewJoystick); return; /* better luck next time? */ } pNewJoystick->bXInputDevice = SDL_TRUE; if (SDL_XInputUseOldJoystickMapping()) { SDL_zero(pNewJoystick->guid); } else { Uint16 *guid16 = (Uint16 *)pNewJoystick->guid.data; GuessXInputDevice(userid, &vendor, &product, &version); *guid16++ = SDL_SwapLE16(SDL_HARDWARE_BUS_USB); *guid16++ = 0; *guid16++ = SDL_SwapLE16(vendor); *guid16++ = 0; *guid16++ = SDL_SwapLE16(product); *guid16++ = 0; *guid16++ = SDL_SwapLE16(version); *guid16++ = 0; /* Note that this is an XInput device and what subtype it is */ pNewJoystick->guid.data[14] = 'x'; pNewJoystick->guid.data[15] = SubType; } pNewJoystick->SubType = SubType; pNewJoystick->XInputUserId = userid; if (SDL_ShouldIgnoreJoystick(pNewJoystick->joystickname, pNewJoystick->guid)) { SDL_free(pNewJoystick); return; } #ifdef SDL_JOYSTICK_HIDAPI if (HIDAPI_IsDevicePresent(vendor, product, version)) { /* The HIDAPI driver is taking care of this device */ SDL_free(pNewJoystick); return; } #endif WINDOWS_AddJoystickDevice(pNewJoystick); }
static int PULSEAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) { struct SDL_PrivateAudioData *h = NULL; Uint16 test_format = 0; pa_sample_spec paspec; pa_buffer_attr paattr; pa_channel_map pacmap; pa_stream_flags_t flags = 0; int state = 0; int rc = 0; /* Initialize all variables that we clean on shutdown */ h = this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc((sizeof *this->hidden)); if (this->hidden == NULL) { return SDL_OutOfMemory(); } SDL_zerop(this->hidden); paspec.format = PA_SAMPLE_INVALID; /* Try for a closest match on audio format */ for (test_format = SDL_FirstAudioFormat(this->spec.format); (paspec.format == PA_SAMPLE_INVALID) && test_format;) { #ifdef DEBUG_AUDIO fprintf(stderr, "Trying format 0x%4.4x\n", test_format); #endif switch (test_format) { case AUDIO_U8: paspec.format = PA_SAMPLE_U8; break; case AUDIO_S16LSB: paspec.format = PA_SAMPLE_S16LE; break; case AUDIO_S16MSB: paspec.format = PA_SAMPLE_S16BE; break; case AUDIO_S32LSB: paspec.format = PA_SAMPLE_S32LE; break; case AUDIO_S32MSB: paspec.format = PA_SAMPLE_S32BE; break; case AUDIO_F32LSB: paspec.format = PA_SAMPLE_FLOAT32LE; break; case AUDIO_F32MSB: paspec.format = PA_SAMPLE_FLOAT32BE; break; default: paspec.format = PA_SAMPLE_INVALID; break; } if (paspec.format == PA_SAMPLE_INVALID) { test_format = SDL_NextAudioFormat(); } } if (paspec.format == PA_SAMPLE_INVALID) { return SDL_SetError("Couldn't find any hardware audio formats"); } this->spec.format = test_format; /* Calculate the final parameters for this audio specification */ #ifdef PA_STREAM_ADJUST_LATENCY this->spec.samples /= 2; /* Mix in smaller chunck to avoid underruns */ #endif SDL_CalculateAudioSpec(&this->spec); /* Allocate mixing buffer */ if (!iscapture) { h->mixlen = this->spec.size; h->mixbuf = (Uint8 *) SDL_malloc(h->mixlen); if (h->mixbuf == NULL) { return SDL_OutOfMemory(); } SDL_memset(h->mixbuf, this->spec.silence, this->spec.size); } paspec.channels = this->spec.channels; paspec.rate = this->spec.freq; /* Reduced prebuffering compared to the defaults. */ #ifdef PA_STREAM_ADJUST_LATENCY /* 2x original requested bufsize */ paattr.tlength = h->mixlen * 4; paattr.prebuf = -1; paattr.maxlength = -1; /* -1 can lead to pa_stream_writable_size() >= mixlen never being true */ paattr.minreq = h->mixlen; flags = PA_STREAM_ADJUST_LATENCY; #else paattr.tlength = h->mixlen*2; paattr.prebuf = h->mixlen*2; paattr.maxlength = h->mixlen*2; paattr.minreq = h->mixlen; #endif if (ConnectToPulseServer(&h->mainloop, &h->context) < 0) { return SDL_SetError("Could not connect to PulseAudio server"); } if (!FindDeviceName(h, iscapture, handle)) { return SDL_SetError("Requested PulseAudio sink/source missing?"); } /* The SDL ALSA output hints us that we use Windows' channel mapping */ /* http://bugzilla.libsdl.org/show_bug.cgi?id=110 */ PULSEAUDIO_pa_channel_map_init_auto(&pacmap, this->spec.channels, PA_CHANNEL_MAP_WAVEEX); h->stream = PULSEAUDIO_pa_stream_new( h->context, "Simple DirectMedia Layer", /* stream description */ &paspec, /* sample format spec */ &pacmap /* channel map */ ); if (h->stream == NULL) { return SDL_SetError("Could not set up PulseAudio stream"); } /* now that we have multi-device support, don't move a stream from a device that was unplugged to something else, unless we're default. */ if (h->device_name != NULL) { flags |= PA_STREAM_DONT_MOVE; } if (iscapture) { rc = PULSEAUDIO_pa_stream_connect_record(h->stream, h->device_name, &paattr, flags); } else { rc = PULSEAUDIO_pa_stream_connect_playback(h->stream, h->device_name, &paattr, flags, NULL, NULL); } if (rc < 0) { return SDL_SetError("Could not connect PulseAudio stream"); } do { if (PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) { return SDL_SetError("pa_mainloop_iterate() failed"); } state = PULSEAUDIO_pa_stream_get_state(h->stream); if (!PA_STREAM_IS_GOOD(state)) { return SDL_SetError("Could not connect PulseAudio stream"); } } while (state != PA_STREAM_READY); /* We're ready to rock and roll. :-) */ return 0; }
/* !!! FIXME: I would love to dump this code and use libudev instead. */ static int MaybeAddDevice(const char *path) { struct stat sb; int fd = -1; int isstick = 0; char namebuf[128]; SDL_JoystickGUID guid; SDL_joylist_item *item; #if !SDL_EVENTS_DISABLED SDL_Event event; #endif if (path == NULL) { return -1; } if (stat(path, &sb) == -1) { return -1; } /* Check to make sure it's not already in list. */ for (item = SDL_joylist; item != NULL; item = item->next) { if (sb.st_rdev == item->devnum) { return -1; /* already have this one */ } } fd = open(path, O_RDONLY, 0); if (fd < 0) { return -1; } #ifdef DEBUG_INPUT_EVENTS printf("Checking %s\n", path); #endif isstick = IsJoystick(fd, namebuf, sizeof (namebuf), &guid); close(fd); if (!isstick) { return -1; } item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item)); if (item == NULL) { return -1; } SDL_zerop(item); item->devnum = sb.st_rdev; item->path = SDL_strdup(path); item->name = SDL_strdup(namebuf); item->guid = guid; if ( (item->path == NULL) || (item->name == NULL) ) { SDL_free(item->path); SDL_free(item->name); SDL_free(item); return -1; } item->device_instance = instance_counter++; if (SDL_joylist_tail == NULL) { SDL_joylist = SDL_joylist_tail = item; } else { SDL_joylist_tail->next = item; SDL_joylist_tail = item; } /* Need to increment the joystick count before we post the event */ ++numjoysticks; /* !!! FIXME: Move this to an SDL_PrivateJoyDeviceAdded() function? */ #if !SDL_EVENTS_DISABLED event.type = SDL_JOYDEVICEADDED; if (SDL_GetEventState(event.type) == SDL_ENABLE) { event.jdevice.which = (numjoysticks - 1); if ( (SDL_EventOK == NULL) || (*SDL_EventOK) (SDL_EventOKParam, &event) ) { SDL_PushEvent(&event); } } #endif /* !SDL_EVENTS_DISABLED */ return numjoysticks; }