Skip to content

Commit

Permalink
Make everything ready for macOS with fixes. (#64)
Browse files Browse the repository at this point in the history
* Add correct Calculator linking for macOS

Add the latest calc version recompiled for a 32bit build with threading and vibro removed and added the correct linking path the the CMake.

* Fix the function overloading on return type in RageUtil

std::string only gets used in Lua so renamed it to luajoin to remove the ambiguous function.

* Removed namespacing for avcodec

Namespacing avcodec creates issue on macOS interfering with local.c in the stdlib.

* Replace emplace_back with push_back for vector.

* Correctly return pointer.

* Fix clang errors about narrowing from int to unsigned int.

Non-constant-expression cannot be narrowed from type 'int' to 'unsigned int' in initializer list

* Fix fullscreen issue on macOS

Pulled from the vanilla master branch.

* Moved everything to std::chrono instead of RageTimer

This fix is required for macOS and should probably be ported to all platforms if possible.

* Delete clockid_t definitions.

Only required for older versions of macOS
  • Loading branch information
TheROPFather authored and martensm committed Sep 8, 2017
1 parent 0cbe46e commit a63348f
Show file tree
Hide file tree
Showing 22 changed files with 96 additions and 102 deletions.
2 changes: 2 additions & 0 deletions StepmaniaCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ elseif(MACOSX)
set(HAS_FFMPEG TRUE)
endif()

link_libraries(${SM_EXTERN_DIR}/MinaCalc/libMinaCalc.a)

set(SYSTEM_PCRE_FOUND FALSE)
set(WITH_CRASH_HANDLER TRUE)
# Apple Archs needs to be 32-bit for now.
Expand Down
Binary file modified extern/MinaCalc/libMinaCalc.a
Binary file not shown.
2 changes: 1 addition & 1 deletion src/LuaManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ static int GetLuaStack( lua_State *L )
{
sErr += ssprintf( " unknown" );
}
sErr += ssprintf( "(%s)", join(",", vArgs).c_str() );
sErr += ssprintf( "(%s)", luajoin(",", vArgs).c_str() );
}

LuaHelpers::Push( L, sErr );
Expand Down
78 changes: 39 additions & 39 deletions src/RageSoundReader_MP3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ namespace

RageSoundReader_MP3::RageSoundReader_MP3()
{
buffer = static_cast<uint8_t *>(avcodec::av_malloc(MP3_BUFFER_SIZE));
buffer = static_cast<uint8_t *>(av_malloc(MP3_BUFFER_SIZE));
}

RageSoundReader_MP3::~RageSoundReader_MP3()
{
//Free everything if it isn't already
avcodec::avformat_close_input(&formatCtx); // AVFormatContext is released by avformat_close_input
avformat_close_input(&formatCtx); // AVFormatContext is released by avformat_close_input
if (IOCtx) {
avcodec::av_freep(&IOCtx->buffer);
avcodec::av_freep(&IOCtx);
av_freep(&IOCtx->buffer);
av_freep(&IOCtx);
}
avcodec::avcodec_close(codecCtx);
avcodec::av_frame_free(&decodedFrame);
avcodec_close(codecCtx);
av_frame_free(&decodedFrame);
}

int ReadFunc(void* ptr, uint8_t* buf, int buf_size)
Expand All @@ -104,36 +104,36 @@ int64_t SeekFunc(void* ptr, int64_t pos, int whence)

RageSoundReader_FileReader::OpenResult RageSoundReader_MP3::Open(RageFileBasic *pFile)
{
avcodec::av_register_all();
av_register_all();
m_pFile = pFile;

//Free everything if it isn't already
avcodec::avformat_close_input(&formatCtx); // AVFormatContext is released by avformat_close_input
avformat_close_input(&formatCtx); // AVFormatContext is released by avformat_close_input
if (IOCtx) {
avcodec::av_freep(&IOCtx->buffer);
avcodec::av_freep(&IOCtx);
av_freep(&IOCtx->buffer);
av_freep(&IOCtx);
}
avcodec::avcodec_close(codecCtx);
avcodec::av_frame_free(&decodedFrame);
avcodec_close(codecCtx);
av_frame_free(&decodedFrame);

IOCtx = avcodec::avio_alloc_context(buffer, MP3_BUFFER_SIZE, // internal Buffer and its size
IOCtx = avio_alloc_context(buffer, MP3_BUFFER_SIZE, // internal Buffer and its size
0, // bWriteable (1 = true, 0 = false)
&m_pFile, // user data -- will be passed to our callback functions
ReadFunc,
nullptr, // Write callback function
SeekFunc);

formatCtx = avcodec::avformat_alloc_context();
formatCtx = avformat_alloc_context();
formatCtx->pb = IOCtx;
formatCtx->flags = AVFMT_FLAG_CUSTOM_IO;

if (avcodec::avformat_open_input(&formatCtx, nullptr, nullptr, nullptr) != 0) {
if (avformat_open_input(&formatCtx, nullptr, nullptr, nullptr) != 0) {
SetError("Error opening file");
return OPEN_UNKNOWN_FILE_FORMAT;
}

// Retrieve stream information
if (avcodec::avformat_find_stream_info(formatCtx, nullptr) < 0) {
if (avformat_find_stream_info(formatCtx, nullptr) < 0) {
SetError("Couldn't find stream information");
return OPEN_UNKNOWN_FILE_FORMAT;
}
Expand All @@ -142,7 +142,7 @@ RageSoundReader_FileReader::OpenResult RageSoundReader_MP3::Open(RageFileBasic *
audioStream = -1;
int nbStreams = formatCtx->nb_streams;
for (int i = 0; i<nbStreams; i++) {
if (formatCtx->streams[i]->codecpar->codec_type == avcodec::AVMEDIA_TYPE_AUDIO) {
if (formatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
audioStream = i;
break;
}
Expand All @@ -152,17 +152,17 @@ RageSoundReader_FileReader::OpenResult RageSoundReader_MP3::Open(RageFileBasic *
return OPEN_UNKNOWN_FILE_FORMAT;
}
// Find the decoder for the audio stream
codec = avcodec::avcodec_find_decoder(formatCtx->streams[audioStream]->codecpar->codec_id);
codec = avcodec_find_decoder(formatCtx->streams[audioStream]->codecpar->codec_id);
if (codec == nullptr) {
SetError("Codec not found\n");
return OPEN_UNKNOWN_FILE_FORMAT;
}
// Get a pointer to the codec context for the audio stream
avcodec::AVCodecParameters * codecParams = formatCtx->streams[audioStream]->codecpar;
codecCtx = avcodec::avcodec_alloc_context3(codec);
avcodec::avcodec_parameters_to_context(codecCtx, codecParams);
AVCodecParameters * codecParams = formatCtx->streams[audioStream]->codecpar;
codecCtx = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(codecCtx, codecParams);

if (avcodec::avcodec_open2(codecCtx, codec, nullptr) < 0) {
if (avcodec_open2(codecCtx, codec, nullptr) < 0) {
SetError("Error opening decoder");
return OPEN_UNKNOWN_FILE_FORMAT;
}
Expand Down Expand Up @@ -218,17 +218,17 @@ int RageSoundReader_MP3::SetPosition(int iFrame)
int ret = -1;

// Free the last read frame if there is one (So when we read after this we read from the frame we seeked)
avcodec::av_frame_free(&decodedFrame);
av_frame_free(&decodedFrame);

avcodec::AVStream * stream = formatCtx->streams[audioStream];
AVStream * stream = formatCtx->streams[audioStream];
// Calculate what we need to pass to the seek function (In the stream's time units)
timeBase = ((stream->time_base.den) / (stream->time_base.num));
double sec = (static_cast<double>(iFrame) / sampleRate);
auto seekFrame = static_cast<unsigned int>(sec * timeBase);

if (seekFrame >= 0 && sec <= stream->duration) {
const int flags = AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD;
ret = avcodec::av_seek_frame(formatCtx, audioStream, seekFrame, flags);
ret = av_seek_frame(formatCtx, audioStream, seekFrame, flags);
avcodec_flush_buffers(codecCtx);
}
return ret;
Expand Down Expand Up @@ -302,7 +302,7 @@ int RageSoundReader_MP3::WriteSamplesForAllChannels(void *pBuf, int samplesToRea
samplesWritten++;
}
// Free the frame since we've read it all
avcodec::av_frame_free(&decodedFrame);
av_frame_free(&decodedFrame);
decodedFrame = nullptr;
curSample = 0;
}
Expand All @@ -328,31 +328,31 @@ int RageSoundReader_MP3::GetNextSourceFrame() const
// Return: -1 => Error already set. -2 => EOF. >=0 => bytesRead
int RageSoundReader_MP3::ReadAFrame()
{
avcodec::AVPacket avpkt;
avcodec::av_init_packet(&avpkt);
AVPacket avpkt;
av_init_packet(&avpkt);
avpkt.data = buffer;

avcodec::av_frame_free(&decodedFrame);
if (!(decodedFrame = avcodec::av_frame_alloc())) {
av_frame_free(&decodedFrame);
if (!(decodedFrame = av_frame_alloc())) {
SetError("Error allocating memory for frame");
return -1;
}

while (avcodec::av_read_frame(formatCtx, &avpkt) >= 0) {
while (av_read_frame(formatCtx, &avpkt) >= 0) {
avpkt.dts = avpkt.pts = AV_NOPTS_VALUE;
if (avpkt.stream_index == audioStream) {
int ret = avcodec::avcodec_send_packet(codecCtx, &avpkt);
int ret = avcodec_send_packet(codecCtx, &avpkt);
if (ret < 0) {
SetError("Error submitting the packet to the decoder\n");
return -1;
}

while (ret >= 0) {
ret = avcodec::avcodec_receive_frame(codecCtx, decodedFrame);
ret = avcodec_receive_frame(codecCtx, decodedFrame);
if (ret == AVERROR_EOF)
return -2;
if (ret == AVERROR(EAGAIN)) {
ret = avcodec::avcodec_send_packet(codecCtx, &avpkt);
ret = avcodec_send_packet(codecCtx, &avpkt);
if (ret < 0) {
SetError("Error submitting the packet to the decoder\n");
return -1;
Expand All @@ -364,19 +364,19 @@ int RageSoundReader_MP3::ReadAFrame()
return -1;
}

numChannels = avcodec::av_get_channel_layout_nb_channels(decodedFrame->channel_layout);
dataSize = avcodec::av_get_bytes_per_sample(codecCtx->sample_fmt);
numChannels = av_get_channel_layout_nb_channels(decodedFrame->channel_layout);
dataSize = av_get_bytes_per_sample(codecCtx->sample_fmt);
numSamples = decodedFrame->nb_samples;
curChannel = 0;
curSample = 0;
avcodec::av_packet_unref(&avpkt);
av_packet_unref(&avpkt);
return numSamples*numChannels*dataSize;
}
break;
}
}

avcodec::av_packet_unref(&avpkt);
av_packet_unref(&avpkt);
return -2;
}

Expand All @@ -403,4 +403,4 @@ int RageSoundReader_MP3::ReadAFrame()
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
*/
22 changes: 10 additions & 12 deletions src/RageSoundReader_MP3.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@

constexpr unsigned int MP3_BUFFER_SIZE = 4096;

namespace avcodec

extern "C"
{
extern "C"
{
#include <libavformat/avformat.h>
}
}; // namespace avcodec
#include <libavformat/avformat.h>
}

class RageSoundReader_MP3: public RageSoundReader_FileReader
{
Expand Down Expand Up @@ -45,11 +43,11 @@ class RageSoundReader_MP3: public RageSoundReader_FileReader
int curSample{};
double timeBase{};
int curChannel{};
avcodec::AVIOContext* IOCtx{nullptr};
avcodec::AVFormatContext* formatCtx{nullptr};
avcodec::AVCodec *codec{nullptr};
avcodec::AVCodecContext *codecCtx{nullptr};
avcodec::AVFrame *decodedFrame{nullptr};
AVIOContext* IOCtx{nullptr};
AVFormatContext* formatCtx{nullptr};
AVCodec *codec{nullptr};
AVCodecContext *codecCtx{nullptr};
AVFrame *decodedFrame{nullptr};
};

#endif
Expand Down Expand Up @@ -77,4 +75,4 @@ class RageSoundReader_MP3: public RageSoundReader_FileReader
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
*/
4 changes: 2 additions & 2 deletions src/RageUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ RString join( const RString &sDelimitor, vector<RString>::const_iterator begin,
}


std::string join(const std::string &sDeliminator, const vector<std::string> &sSource)
std::string luajoin(const std::string &sDeliminator, const vector<std::string> &sSource)
{
if (sSource.empty())
return std::string();
Expand All @@ -723,7 +723,7 @@ std::string join(const std::string &sDeliminator, const vector<std::string> &sSo
return sTmp;
}

std::string join(const std::string &sDelimitor, vector<std::string>::const_iterator begin, vector<std::string>::const_iterator end)
std::string luajoin(const std::string &sDelimitor, vector<std::string>::const_iterator begin, vector<std::string>::const_iterator end)
{
if (begin == end)
return std::string();
Expand Down
4 changes: 2 additions & 2 deletions src/RageUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,8 @@ void split( const wstring &sSource, const wstring &sDelimitor, int &iBegin, int
RString join( const RString &sDelimitor, const vector<RString>& sSource );
RString join( const RString &sDelimitor, vector<RString>::const_iterator begin, vector<RString>::const_iterator end );

std::string join(const std::string &sDelimitor, const vector<std::string>& sSource);
std::string join(const std::string &sDelimitor, vector<std::string>::const_iterator begin, vector<std::string>::const_iterator end);
std::string luajoin(const std::string &sDelimitor, const vector<std::string>& sSource);
std::string luajoin(const std::string &sDelimitor, vector<std::string>::const_iterator begin, vector<std::string>::const_iterator end);

// These methods escapes a string for saving in a .sm or .crs file
RString SmEscape( const RString &sUnescaped );
Expand Down
4 changes: 2 additions & 2 deletions src/ScoreManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void ScoreManager::RatingOverTime() {
sort(AllScores.begin(), AllScores.end(), compdate);

for (auto& n : AllScores) {
wasvalid.emplace_back(n->GetEtternaValid());
wasvalid.push_back(n->GetEtternaValid());
n->SetEtternaValid(false);
}

Expand Down Expand Up @@ -581,4 +581,4 @@ class LunaScoreManager : public Luna<ScoreManager>
}
};

LUA_REGISTER_CLASS(ScoreManager)
LUA_REGISTER_CLASS(ScoreManager)
6 changes: 2 additions & 4 deletions src/arch/InputHandler/InputHandler_MacOSX_HID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void InputHandler_MacOSX_HID::QueueCallback( void *target, int result, void *ref
// The result seems useless as you can't actually return anything...
// refcon is the Device number

RageTimer now;
auto now = std::chrono::steady_clock::now();
InputHandler_MacOSX_HID *This = (InputHandler_MacOSX_HID *)target;
IOHIDQueueInterface **queue = (IOHIDQueueInterface **)sender;
IOHIDEventStruct event;
Expand All @@ -36,7 +36,7 @@ void InputHandler_MacOSX_HID::QueueCallback( void *target, int result, void *ref
continue;
}
//LOG->Trace( "Got event with cookie %p, value %d", event.elementCookie, int(event.value) );
dev->GetButtonPresses( vPresses, event.elementCookie, event.value, now );
dev->GetButtonPresses( vPresses, event.elementCookie, event.value, now);
}
FOREACH_CONST( DeviceInput, vPresses, i )
INPUTFILTER->ButtonPressed( *i );
Expand Down Expand Up @@ -173,10 +173,8 @@ static HIDDevice *MakeDevice( InputDevice id )
{
if( id == DEVICE_KEYBOARD )
return new KeyboardDevice;
/*
if( id == DEVICE_MOUSE )
return new MouseDevice;
*/
if( IsJoystick(id) )
return new JoystickDevice;
if( IsPump(id) )
Expand Down
2 changes: 1 addition & 1 deletion src/arch/LowLevelWindow/LowLevelWindow_MacOSX.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class LowLevelWindow_MacOSX : public LowLevelWindow
void SwapBuffers();
void Update();

const VideoModeParams* GetActualVideoModeParams() const { return m_CurrentParams; }
const VideoModeParams* GetActualVideoModeParams() const { return &m_CurrentParams; }

bool SupportsRenderToTexture() const { return true; }
RenderTarget *CreateRenderTarget();
Expand Down
4 changes: 2 additions & 2 deletions src/arch/LowLevelWindow/LowLevelWindow_MacOSX.mm
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ - (void) closeWindow
- (void) setParams:(NSValue *)params
{
const VideoModeParams &p = *(const VideoModeParams *)[params pointerValue];
NSRect contentRect = { { 0, 0 }, { p.width, p.height } };
NSRect contentRect = { { 0, 0 }, { static_cast<CGFloat>(p.width), static_cast<CGFloat>(p.height) } };

[m_Window setContentSize:contentRect.size];
[m_Window setTitle:[NSString stringWithUTF8String:p.sWindowTitle.c_str()]];
Expand Down Expand Up @@ -582,7 +582,7 @@ static bool GetBoolValue( CFTypeRef r )
continue;
if( safe && !GetBoolValue( safe ) )
continue;
DisplayResolution res = { width, height, stretched };
DisplayResolution res = { static_cast<unsigned int>(width), static_cast<unsigned int>(height), stretched };
dr.insert( res );
}
// Do not release modes! We don't own them here.
Expand Down
4 changes: 0 additions & 4 deletions src/arch/Threads/Threads_Pthreads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,6 @@ MutexImpl *MakeMutex( RageMutex *pParent )
#if defined(UNIX)
#include <dlfcn.h>
#include "arch/ArchHooks/ArchHooks_Unix.h"
#elif defined(MACOSX)
typedef int clockid_t;
static const clockid_t CLOCK_REALTIME = 0;
static const clockid_t CLOCK_MONOTONIC = 1;
#endif // On MinGW clockid_t is defined in pthread.h
namespace
{
Expand Down
2 changes: 1 addition & 1 deletion src/archutils/Darwin/HIDDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class HIDDevice
* presses may be generated by a single element. The value of the element is
* passed to determine if this is a push or a release. The time is provided
* as an optimization. */
virtual void GetButtonPresses( vector<DeviceInput>& vPresses, IOHIDElementCookie cookie, int value, const RageTimer& now ) const = 0;
virtual void GetButtonPresses( vector<DeviceInput>& vPresses, IOHIDElementCookie cookie, int value, const std::chrono::time_point<std::chrono::steady_clock> &now) const = 0;

/* Returns the number of IDs assigned starting from startID. This is not
* meaningful for devices like keyboards that all share the same InputDevice
Expand Down
Loading

0 comments on commit a63348f

Please sign in to comment.