Skip to content

Commit

Permalink
Midi sender only + review parsing json
Browse files Browse the repository at this point in the history
  • Loading branch information
totetmatt committed Sep 22, 2024
1 parent f8b6d6a commit c08dff0
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 58 deletions.
18 changes: 6 additions & 12 deletions src/MIDI.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
#ifndef BONZOMATIC_MIDI_H
#define BONZOMATIC_MIDI_H
#pragma once
namespace MIDI
{
//////////////////////////////////////////////////////////////////////////

enum
{
MIDIMSG_NOTE_OFF = 8,
MIDIMSG_NOTE_ON = 9,
MIDIMSG_POLYPHONIC_KEY = 10,
MIDIMSG_CONTROL_CHANGE = 11,
MIDIMSG_PROGRAM_CHANGE = 12,
MIDIMSG_CHANNEL_PRESSURE = 13,
MIDIMSG_PITCH_BEND = 14,
MIDIMSG_SYSTEM = 15,
} MIDI_MESSAGE_TYPE;


bool Open();
bool Close();
float GetCCValue( unsigned char cc );

//////////////////////////////////////////////////////////////////////////
};
};
#endif
13 changes: 10 additions & 3 deletions src/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define BONZOMATIC_NETWORK_H
#pragma once
#include <string>

#include "mongoose.h"
#include <thread>
#include <jsonxx.h>
Expand All @@ -14,10 +15,13 @@ namespace Network {
GRABBER
};


struct NetworkConfig {
char* Url;
NetworkMode Mode;
float updateInterval = 0.3;
float updateInterval = 0.3f;
bool sendMidiControls;
bool grabMidiControls;
};
struct ShaderMessage {
std::string Code;
Expand All @@ -41,14 +45,17 @@ namespace Network {
void Init();

void ParseSettings(jsonxx::Object* options);
void UpdateShader(ShaderEditor* mShaderEditor, float shaderTime);
void UpdateShader(ShaderEditor* mShaderEditor, float shaderTime, std::map<int, std::string> *midiRoutes);
char* GetUrl();
void SetUrl(char*);
Network::NetworkMode GetNetworkMode();
void SetNetworkMode(Network::NetworkMode mode);
void SetNeedRecompile(bool needToRecompile);
void UpdateShaderFileName( const char** shaderName);
void SplitUrl(std::string *host,std::string *roomname,std::string* name);

bool IsGrabber();
bool IsSender();
bool IsOffline();
void GenerateWindowsTitle(char** originalTitle);
}
#endif // BONZOMATIC_NETWORK_H
27 changes: 20 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,16 +426,17 @@ int main( int argc, const char * argv[] )
{
if ( bShowGui )
{

switch ( Renderer::mouseEventBuffer[ i ].eventType )
{
case Renderer::MOUSEEVENTTYPE_MOVE:
mShaderEditor.ButtonMovePublic( Scintilla::Point( Renderer::mouseEventBuffer[ i ].x, Renderer::mouseEventBuffer[ i ].y ) );
break;
case Renderer::MOUSEEVENTTYPE_DOWN:
mShaderEditor.ButtonDown( Scintilla::Point( Renderer::mouseEventBuffer[ i ].x, Renderer::mouseEventBuffer[ i ].y ), time * 1000, false, false, false );
if(!Network::IsGrabber()) mShaderEditor.ButtonDown( Scintilla::Point( Renderer::mouseEventBuffer[ i ].x, Renderer::mouseEventBuffer[ i ].y ), time * 1000, false, false, false );
break;
case Renderer::MOUSEEVENTTYPE_UP:
mShaderEditor.ButtonUp( Scintilla::Point( Renderer::mouseEventBuffer[ i ].x, Renderer::mouseEventBuffer[ i ].y ), time * 1000, false );
if(!Network::IsGrabber()) mShaderEditor.ButtonUp( Scintilla::Point( Renderer::mouseEventBuffer[ i ].x, Renderer::mouseEventBuffer[ i ].y ), time * 1000, false );
break;
case Renderer::MOUSEEVENTTYPE_SCROLL:
if ( Renderer::mouseEventBuffer[ i ].ctrl )
Expand All @@ -445,7 +446,7 @@ int main( int argc, const char * argv[] )
}
else
{
mShaderEditor.WndProc( SCI_LINESCROLL, (int) ( -Renderer::mouseEventBuffer[ i ].x * fScrollXFactor ), (int) ( -Renderer::mouseEventBuffer[ i ].y * fScrollYFactor ) );
if (!Network::IsGrabber()) mShaderEditor.WndProc( SCI_LINESCROLL, (int) ( -Renderer::mouseEventBuffer[ i ].x * fScrollXFactor ), (int) ( -Renderer::mouseEventBuffer[ i ].y * fScrollYFactor ) );
}
break;
}
Expand Down Expand Up @@ -500,7 +501,7 @@ int main( int argc, const char * argv[] )
{
bShowGui = !bShowGui;
}
else if ( bShowGui )
else if ( bShowGui && !Network::IsGrabber())
{
bool consumed = false;
if ( Renderer::keyEventBuffer[ i ].scanCode )
Expand All @@ -523,7 +524,7 @@ int main( int argc, const char * argv[] )
}
}

Network::UpdateShader(&mShaderEditor, time);
Network::UpdateShader(&mShaderEditor, time, &midiRoutes);

Renderer::keyEventBufferCount = 0;
if (Network::ReloadShader()) {
Expand Down Expand Up @@ -629,8 +630,20 @@ int main( int argc, const char * argv[] )
sHelp += szLayout;
surface->DrawTextNoClip( Scintilla::PRectangle( 20, Renderer::nHeight - 20, 100, Renderer::nHeight ), *mShaderEditor.GetTextFont(), Renderer::nHeight - 5.0, sHelp.c_str(), (int) sHelp.length(), 0x80FFFFFF, 0x00000000 );
}


{
int TexPreviewOffset = bTexPreviewVisible ? nTexPreviewWidth + nMargin : 0;
std::string Status = "totetmatt";
Scintilla::Font *CoderFont = mShaderEditor.GetTextFont();
int CoderFontSize = surface->Height(*CoderFont);
Scintilla::XYPOSITION WidthText = surface->WidthText(*CoderFont, Status.c_str(), (int)Status.length());

Scintilla::XYPOSITION RightPosition = Renderer::nWidth - CoderFontSize * 0.35 - 0;
Scintilla::XYPOSITION LeftPosition = RightPosition - WidthText - CoderFontSize * 0.25;
Scintilla::XYPOSITION BaseY = Renderer::nHeight - CoderFontSize * 1.1;
surface->RectangleDraw(Scintilla::PRectangle(LeftPosition, BaseY, RightPosition + CoderFontSize * 0.25, BaseY + CoderFontSize), 0x00000000, 0x80000000);
surface->DrawTextNoClip(Scintilla::PRectangle(RightPosition - WidthText, BaseY, RightPosition, BaseY + CoderFontSize), *CoderFont, BaseY + CoderFontSize, Status.c_str(), (int)Status.length(), 0xFFFFFFFF, 0xFFFFFFFF);
surface->RectangleDraw(Scintilla::PRectangle(LeftPosition - 20, BaseY, LeftPosition, BaseY + CoderFontSize), 0x00000000, 0x808080FF);
}
Renderer::EndTextRendering();

Renderer::EndFrame();
Expand Down
166 changes: 138 additions & 28 deletions src/platform_common/Network.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Network.h"
#include "MIDI.h"
#define SHADER_FILENAME(mode) (std::string(mode)+ "_" + RoomName + "_" + NickName + ".glsl")
#define LOG(header,message) printf("[" header "] " message " \n")
namespace Network {

Network::NetworkConfig config;
Expand Down Expand Up @@ -173,9 +175,9 @@ namespace Network {
else if(config.Mode == GRABBER) {
filename = SHADER_FILENAME("grabber");
}
*shaderName = strdup(filename.c_str());
*shaderName = _strdup(filename.c_str());
}
void UpdateShader(ShaderEditor* mShaderEditor, float shaderTime) {
void UpdateShader(ShaderEditor* mShaderEditor, float shaderTime, std::map<int, std::string> *midiRoutes) {
if (Network::config.Mode != Network::NetworkMode::OFFLINE) { // If we arn't offline mode
if (config.Mode == Network::GRABBER && Network::HasNewShader()) { // Grabber mode

Expand Down Expand Up @@ -216,50 +218,158 @@ namespace Network {
Data << "NickName" << "NickName";
Data << "ShaderTime" << shaderMessage.shaderTime;

if(config.sendMidiControls) { // Sending Midi Controls
jsonxx::Object networkShaderParameters;
for (std::map<int, std::string>::iterator it = midiRoutes->begin(); it != midiRoutes->end(); it++)
{
networkShaderParameters << it->second << MIDI::GetCCValue(it->first);
}
Data << "Parameters" << networkShaderParameters;
}
jsonxx::Object Message = jsonxx::Object("Data", Data);
std::string TextJson = Message.json();
if (connected) {

if (connected) {
mg_ws_send(c, TextJson.c_str(), TextJson.length(), WEBSOCKET_OP_TEXT);
shaderMessage.NeedRecompile = false;
}

}
}
}
bool IsGrabber() {
return config.Mode == GRABBER;
}
bool IsSender() {
return config.Mode = SENDER;
}
bool IsOffline() {
return config.Mode = OFFLINE;
}
void GenerateWindowsTitle(char** originalTitle) {
if (config.Mode == OFFLINE) {
return;
}
std::string host, roomname, user, title(*originalTitle), newName;
Network::SplitUrl(&host, &roomname, &user);
if (config.Mode == GRABBER) {
newName = title + " grabber " + user;
}
if (config.Mode == SENDER) {
newName = title + " sender " + user;
}
*originalTitle = _strdup(newName.c_str());
}
/* From here are methods for parsing json */
void ParseNetworkGrabMidiControls(jsonxx::Object * network) {
if (!network->has<jsonxx::Boolean>("grabMidiControls")) {
LOG("Network Configuration", "Can't find 'grabMidiControls', set to false");
config.grabMidiControls = false;
return;
}
config.grabMidiControls = network->get<jsonxx::Boolean>("grabMidiControls");
}
void ParseNetworkSendMidiControls(jsonxx::Object* network) {
if (!network->has<jsonxx::Boolean>("sendMidiControls")) {
LOG("Network Configuration", "Can't find 'sendMidiControls', set to false");
config.sendMidiControls = false;
return;
}
config.sendMidiControls = network->get<jsonxx::Boolean>("sendMidiControls");
}
void ParseNetworkUpdateInterval(jsonxx::Object* network) {
if (!network->has<jsonxx::Boolean>("updateInterval")) {
LOG("Network Configuration", "Can't find 'updateInterval', set to 0.3");
config.updateInterval = 0.3f;
return;
}

config.updateInterval = network->get<jsonxx::Number>("updateInterval");
}
void ParseNetworkMode(jsonxx::Object* network) {
if (!network->has<jsonxx::String>("networkMode")) {
LOG("Network Configuration", "Can't find 'networkMode' Set to OFFLINE");
config.Mode = OFFLINE;
return;
}

void ParseSettings(jsonxx::Object* options) {
const char* mode = network->get<jsonxx::String>("networkMode").c_str();
bool isSenderMode = strcmp(mode, "sender");
bool isGrabberMode = strcmp(mode, "grabber");
if (!isSenderMode && !isGrabberMode) {
LOG("Network Configuration", "networkMode is neither SENDER or GRABBER, fallback config to OFFLINE");
config.Mode = OFFLINE;
return;
}
if(isSenderMode){
config.Mode = SENDER;
}
if(isGrabberMode){
config.Mode = GRABBER;
}

if (options->has<jsonxx::Object>("network")) {
jsonxx::Object network = options->get<jsonxx::Object>("network");
if (network.has<jsonxx::String>("serverURL")) {
config.Url = strdup(network.get<jsonxx::String>("serverURL").c_str());
}
if (network.get<jsonxx::Boolean>("enabled")) {
// From now on, we have a minimal config working we can try to parse extra option
ParseNetworkGrabMidiControls(network);
ParseNetworkSendMidiControls(network);
ParseNetworkUpdateInterval(network);
}
void ParseNetworkUrl(jsonxx::Object* network) {
if (!network->has<jsonxx::String>("serverURL")) {
LOG("Network Configuration", "Can't find 'serverURL', set to 'OFFLINE'");
config.Mode = OFFLINE;
config.Url = "";
return;
}

config.Url = _strdup(network->get<jsonxx::String>("serverURL").c_str());

if (network.has<jsonxx::String>("networkMode")) {
const char* mode = network.get<jsonxx::String>("networkMode").c_str();
if (strcmp(mode, "sender") == 0) {
config.Mode = SENDER;
}
else if (strcmp(mode, "grabber") == 0) {
config.Mode = GRABBER;
}
else {
config.Mode = GRABBER;
printf("Can't find 'networkMode', set to 'GRABBER'\n");
}
}
else {
printf("Can't find 'networkMode', set to 'OFFLINE'\n");
}
}
ParseNetworkMode(network);

}
void ParseNetworkEnabled(jsonxx::Object* network) {

if (!network->has<jsonxx::Boolean>("enabled")) {
LOG("Network Configuration", "Can't find 'enabled', set to 'OFFLINE'");
config.Mode = OFFLINE;
config.Url = "";
return;
}

if (!network->get<jsonxx::Boolean>("enabled")) {
LOG("Network Configuration", "Set to 'OFFLINE'");
config.Mode = OFFLINE;
config.Url = "";
// As we can activate this on setup dialog, let's try to get serverURL
if (network->has<jsonxx::String>("serverURL")) {
config.Url = _strdup(network->get<jsonxx::String>("serverURL").c_str());
}
return;
}
else {
ParseNetworkUrl(network);


}
/*
Parse the json settings. Cascading calls, not perfect but keep clear code
- Check that Network block exists on json
- Check that 'enabled' exists and is true
- Check that 'serverUrl' exists
- Check that 'networkMode' exists
If something doesn't match above path, will fallback to OFFLINE Mode
*/
void ParseSettings(jsonxx::Object* options) {

LOG("Network Configuration", "Parsing network configuration data from json");
if (!options->has<jsonxx::Object>("network")) {
LOG("Network Configuration", "Can't find 'network' block, set to 'OFFLINE'");
config.Mode = OFFLINE;
config.Url = "";
return;
}
jsonxx::Object network = options->get<jsonxx::Object>("network");
ParseNetworkEnabled(&network);


}
}
7 changes: 4 additions & 3 deletions src/platform_glfw/Renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

#include <cstdio>

#include "Network.h"
#ifdef _WIN32
#include <windows.h>
#endif
Expand Down Expand Up @@ -260,8 +260,9 @@ bool Open( Renderer::Settings * settings )
glfwWindowHint( GLFW_AUTO_ICONIFY, GL_FALSE );

GLFWmonitor * monitor = settings->windowMode == WINDOWMODE_FULLSCREEN ? glfwGetPrimaryMonitor() : NULL;

mWindow = glfwCreateWindow( nWidth, nHeight, "BONZOMATIC - GLFW edition", monitor, NULL );
char * windowsTitle = "BONZOMATIC - GLFW edition";
Network::GenerateWindowsTitle(&windowsTitle);
mWindow = glfwCreateWindow( nWidth, nHeight, windowsTitle, monitor, NULL );
if ( !mWindow )
{
printf( "[GLFW] Window creation failed\n" );
Expand Down
Loading

0 comments on commit c08dff0

Please sign in to comment.