Support minimal dependency linux build.

This commit is contained in:
James Grogan 2022-11-10 09:06:02 +00:00
parent 92a35a5bc9
commit 7ce29ce8ae
14 changed files with 421 additions and 176 deletions

View file

@ -1,5 +1,39 @@
# limmto
limmto (Light Multi-Media Tool) is a free, low-dependency C++ library for working with digital media.
# MediaTool
This is a collection of tools and apps for working with various media (Web, Desktop Publishing, Video/Audio) on different platforms (Linux, Windows).
It tries to have low dependencies and lots of things are built from scratch.
This is a personal/hobby project - things will break/change a lot.
# Build Dependencies
## Linux
### Minimal
```bash
sudo apt-get install build-essential cmake
```
### Optional
#### Audio
```bash
sudo apt-get install libasound2-dev
```
#### Image Formats
```bash
sudo apt-get install libpng-dev
```
# Build from Source
## Linux
```bash
mkdir build

View file

@ -1,24 +1,9 @@
# Sample GUI
add_executable(sample_gui gui-main.cpp)
target_include_directories(sample_gui PUBLIC
"${PROJECT_SOURCE_DIR}/src/console"
"${PROJECT_SOURCE_DIR}/src/client"
)
target_link_libraries(sample_gui PUBLIC client windows console core
network database geometry audio web)
add_subdirectory(sample-gui)
if(WIN32)
add_executable(sample_gui_win WIN32 gui-main-win.cpp)
target_include_directories(sample_gui PUBLIC
"${PROJECT_SOURCE_DIR}/src/console"
"${PROJECT_SOURCE_DIR}/src/client"
)
target_link_libraries(sample_gui_win PUBLIC client windows console core
network database geometry audio web)
set_property(TARGET sample_gui_win PROPERTY FOLDER apps)
add_subdirectory(directx-practice)
endif()
# Sample Console
@ -30,7 +15,6 @@ target_link_libraries(sample_console PUBLIC console core network
database geometry audio web)
set_property(TARGET sample_console PROPERTY FOLDER apps)
set_property(TARGET sample_gui PROPERTY FOLDER apps)
add_subdirectory(website-generator)

View file

@ -0,0 +1,27 @@
if(WIN32)
add_executable(sample_gui_win WIN32 gui-main-win.cpp)
target_include_directories(sample_gui PUBLIC
"${PROJECT_SOURCE_DIR}/src/console"
"${PROJECT_SOURCE_DIR}/src/client"
)
target_link_libraries(sample_gui_win PUBLIC client windows console core
network database geometry audio web)
set_property(TARGET sample_gui_win PROPERTY FOLDER apps)
else()
find_package(X11 QUIET)
if(X11_FOUND)
add_executable(sample_gui gui-main.cpp)
target_include_directories(sample_gui PUBLIC
"${PROJECT_SOURCE_DIR}/src/console"
"${PROJECT_SOURCE_DIR}/src/client"
)
target_link_libraries(sample_gui PUBLIC client windows console core
network database geometry audio web)
set_property(TARGET sample_gui PROPERTY FOLDER apps)
else()
message(STATUS "Skipping sample GUI as no X11 dev support")
endif()
endif()

View file

@ -50,8 +50,7 @@ void WebsiteGenerator::parseTemplateFiles()
const auto template_files = Directory::getFilesWithExtension(template_path, ".html");
for (const auto& path : template_files)
{
auto file = TemplateFile(path);
mTemplateFiles[path.stem().string()] = file;
mTemplateFiles[path.stem().string()] = std::make_unique<TemplateFile>(path);
}
}

View file

@ -44,6 +44,7 @@ public:
{
}
private:
Path mPath;
};
@ -80,5 +81,5 @@ private:
GeneratorConfig mConfig;
std::vector<ContentPage> mPages;
std::vector<ContentArticle> mArticles;
std::unordered_map<std::string, TemplateFile> mTemplateFiles;
std::unordered_map<std::string, std::unique_ptr<TemplateFile> > mTemplateFiles;
};

View file

@ -3,17 +3,26 @@ set(platform_HEADERS "")
set(platform_INCLUDES "")
if (UNIX)
find_package(ALSA REQUIRED)
find_package(ALSA QUIET)
if(ALSA_FOUND)
list(APPEND platform_HEADERS
audio_interfaces/AlsaInterface.h
${ALSA_INCLUDE_DIRS}
)
${ALSA_INCLUDE_DIRS})
list(APPEND platform_INCLUDES
audio_interfaces/AlsaInterface.cpp
)
list(APPEND platform_LIBS
${ALSA_LIBRARIES}
)
else()
message(STATUS "ALSA package not found - not building audio interface.")
list(APPEND platform_HEADERS
audio_interfaces/NullAudioInterface.h
)
list(APPEND platform_INCLUDES
audio_interfaces/NullAudioInterface.cpp
)
endif()
else()
list(APPEND platform_HEADERS
audio_interfaces/WasapiInterface.h

View file

@ -0,0 +1,141 @@
#include "AlsaInterface.h"
#include "FileLogger.h"
#include "AudioSynth.h"
#include <vector>
AlsaInterface::AlsaInterface()
:mHandle(),
mHardwareParams(),
mPeriodSize(8192)
{
}
AlsaInterface::~AlsaInterface()
{
}
std::unique_ptr<AlsaInterface> AlsaInterface::Create()
{
return std::make_unique<AlsaInterface>();
}
void AlsaInterface::OpenDevice(const AudioDevicePtr& device)
{
MLOG_INFO("Opening Device");
snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
if (snd_pcm_open(&mHandle, device->GetName().c_str(), stream, 0) < 0)
{
MLOG_ERROR("Error opening PCM device: " + device->GetName());
return;
}
snd_pcm_hw_params_alloca(&mHardwareParams);
if (snd_pcm_hw_params_any(mHandle, mHardwareParams) < 0)
{
MLOG_ERROR("Can not configure this PCM device.");
return;
}
SetAccessType(device);
SetSampleFormat(device);
SetSampleRate(device);
SetPeriod(device);
SetBufferSize(device);
SetChannelNumber(device);
/* Apply HW parameter settings to */
/* PCM device and prepare device */
if (snd_pcm_hw_params(mHandle, mHardwareParams) < 0) {
MLOG_ERROR("Error setting HW params.");
return;
}
}
void AlsaInterface::SetAccessType(const AudioDevicePtr& device)
{
if (snd_pcm_hw_params_set_access(mHandle, mHardwareParams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
MLOG_ERROR("Error setting device access.");
return;
}
}
void AlsaInterface::SetSampleFormat(const AudioDevicePtr& device)
{
/* Set sample format */
if (snd_pcm_hw_params_set_format(mHandle, mHardwareParams, SND_PCM_FORMAT_S16_LE) < 0) {
MLOG_ERROR("Error setting format. ");
return;
}
}
void AlsaInterface::SetSampleRate(const AudioDevicePtr& device)
{
unsigned rate = device->GetSampleRate();
unsigned exact_rate = rate;
if (snd_pcm_hw_params_set_rate_near(mHandle, mHardwareParams, &exact_rate, 0) < 0)
{
MLOG_ERROR("Error setting rate. ");
return;
}
if (rate != exact_rate) {
MLOG_ERROR("The rate is not supported by your hardware.");
}
}
void AlsaInterface::SetPeriod(const AudioDevicePtr& device)
{
/* Set number of periods. Periods used to be called fragments. */
if (snd_pcm_hw_params_set_periods(mHandle, mHardwareParams, device->GetPeriod(), 0) < 0)
{
MLOG_ERROR("Error setting periods. ");
return;
}
}
void AlsaInterface::SetBufferSize(const AudioDevicePtr& device)
{
int periods = static_cast<int>(device->GetPeriod());
/* Set buffer size (in frames). The resulting latency is given by */
/* latency = periodsize * periods / (rate * bytes_per_frame) */
if (snd_pcm_hw_params_set_buffer_size(mHandle, mHardwareParams, (mPeriodSize * periods)>>2) < 0)
{
MLOG_ERROR("Error setting buffersize. ");
return;
}
}
void AlsaInterface::SetChannelNumber(const AudioDevicePtr& device)
{
/* Set number of channels */
if (snd_pcm_hw_params_set_channels(mHandle, mHardwareParams, device->GetNumChannels()) < 0)
{
MLOG_ERROR("Error setting channels");
return;
}
}
void AlsaInterface::Play(const AudioDevicePtr& device)
{
MLOG_INFO("Playing audio");
AudioSynth synth;
const unsigned duration = 100;
double freq = 440;
auto data = synth.GetSineWave(freq, duration)->GetChannelData(0);
int numFrames = mPeriodSize >> 2;
for(int count = 0; count < duration; count++)
{
const auto offset= count*4*numFrames;
unsigned char frame[4] = {data[offset], data[offset+1], data[offset+2], data[offset+3]};
while ((snd_pcm_writei(mHandle, frame, numFrames)) < 0)
{
snd_pcm_prepare(mHandle);
MLOG_ERROR("<<<<<<<<<<<<<<< Buffer Underrun >>>>>>>>>>>>>>>");
}
}
}

View file

@ -0,0 +1,40 @@
#pragma once
#include "IAudioInterface.h"
#include "AudioDevice.h"
#include <memory>
#include <alsa/asoundlib.h>
class AlsaInterface : public IAudioInterface
{
snd_pcm_t* mHandle;
snd_pcm_hw_params_t* mHardwareParams;
snd_pcm_uframes_t mPeriodSize;
public:
AlsaInterface();
~AlsaInterface();
static std::unique_ptr<AlsaInterface> Create();
void OpenDevice(const AudioDevicePtr& device) override;
void SetAccessType(const AudioDevicePtr& device);
void SetSampleFormat(const AudioDevicePtr& device);
void SetSampleRate(const AudioDevicePtr& device);
void SetPeriod(const AudioDevicePtr& device);
void SetBufferSize(const AudioDevicePtr& device);
void SetChannelNumber(const AudioDevicePtr& device);
void Play(const AudioDevicePtr& device) override;
};
using AlsaInterfacePtr = std::shared_ptr<AlsaInterface>;

View file

@ -2,6 +2,7 @@
#include <iostream>
#include <locale>
#include <algorithm>
TomlTable::TomlTable(const std::string& header)
: mHeader(header)

View file

@ -3,32 +3,29 @@ set(platform_INCLUDE_DIRS "")
set(platform_HEADERS "")
set(platform_LIBS "")
if (UNIX)
list(APPEND platform_LIBSs
GL
)
else()
list(APPEND platform_LIBS
OpenGL32.lib
)
endif()
list(APPEND graphics_LIB_INCLUDES
DrawingContext.cpp
DrawingManager.cpp
DrawingSurface.cpp
Rasterizer.cpp
opengl/OpenGlInterface.cpp
${platform_LIB_INCLUDES}
)
list(APPEND graphics_HEADERS
opengl/OpenGlInterface.h
${platform_HEADERS}
Rasterizer.h
INativeDrawingSurface.h
INativeDrawingContext.h)
INativeDrawingContext.h
)
find_package(OpenGL QUIET)
if (OpenGL_FOUND)
list(APPEND platform_LIBS OpenGL::GL)
list(APPEND graphics_LIB_INCLUDES opengl/OpenGlInterface.cpp)
list(APPEND graphics_HEADERS opengl/OpenGlInterface.h)
else()
message(STATUS "OpenGL headers not found - skipping OpenGL support")
endif()
add_library(graphics SHARED
${graphics_LIB_INCLUDES}

View file

@ -4,6 +4,8 @@ set (platform_LIBS "")
set(_HAS_WAYLAND Off)
if(UNIX)
find_package(X11 QUIET)
if(X11_FOUND)
list(APPEND platform_INCLUDES
ui_interfaces/x11/XcbInterface.cpp
ui_interfaces/x11/XcbEventInterface.cpp
@ -15,20 +17,16 @@ if(UNIX)
ui_interfaces/x11/XcbKeyboard.cpp
ui_interfaces/x11/GlxInterface.cpp
)
list(APPEND platform_LIBS ${X11_LIBRARIES} ${X11_xcb_LIB} ${X11_X11_xcb_LIB})
else()
message(STATUS "x11 development headers not found - skipping support")
endif()
list(APPEND platform_LIBS
X11 X11-xcb xcb )
find_path(
WAYLAND_CLIENT_INCLUDE_DIR
NAMES wayland-client.h
)
find_path(WAYLAND_CLIENT_INCLUDE_DIR NAMES wayland-client.h)
if (NOT ${WAYLAND_CLIENT_INCLUDE_DIR-NOTFOUND})
list(APPEND WAYLAND_INCLUDE_DIRS ${WAYLAND_CLIENT_INCLUDE_DIR})
find_path(
WAYLAND_EXTENSIONS_INCLUDE_DIR
NAMES xdg-shell-client-protocol.h
find_path(WAYLAND_EXTENSIONS_INCLUDE_DIR NAMES xdg-shell-client-protocol.h
HINTS ENV WAYLAND_EXTENSION_DIR
)
if(NOT ${WAYLAND_EXTENSIONS_INCLUDE_DIR-NOTFOUND})
@ -49,6 +47,9 @@ if(UNIX)
else()
message(STATUS "Wayland Extensions Header not found - not building Wayland support")
endif()
else()
message(STATUS "Wayland Client Header not found - not building Wayland support")
endif()
else()
list(APPEND platform_INCLUDES

View file

@ -17,9 +17,7 @@ list(APPEND TestFiles
compression/TestStreamCompressor.cpp
database/TestDatabase.cpp
fonts/TestFontReader.cpp
graphics/TestOpenGlRendering.cpp
graphics/TestRasterizer.cpp
ipc/TestDbus.cpp
image/TestPngReader.cpp
image/TestPngWriter.cpp
network/TestNetworkManagerClient.cpp
@ -40,10 +38,23 @@ list(APPEND TestFiles
)
endif()
find_package(OpenGL QUIET)
if (OpenGL_FOUND)
list(APPEND TestFiles
graphics/TestOpenGlRendering.cpp
)
endif()
find_package(PkgConfig)
pkg_check_modules(DBUS dbus-1)
if (DBUS_FOUND)
include_directories(${DBUS_INCLUDE_DIRS})
link_directories(${DBUS_LIBRARY_DIRS})
list(APPEND TestFiles
ipc/TestDbus.cpp
)
endif()
foreach(TestFile ${TestFiles})
cmake_path(GET TestFile FILENAME TestFileName)