Skip to content

Commit

Permalink
Working Update
Browse files Browse the repository at this point in the history
Colour capture + plugin system in place.
  • Loading branch information
Brandon authored and Brandon committed Jan 2, 2014
1 parent 33e2444 commit f5732e4
Show file tree
Hide file tree
Showing 22 changed files with 1,170 additions and 40 deletions.
57 changes: 57 additions & 0 deletions DXI/DXI.cbp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="DXI" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/DXI" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="3" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Option createStaticLib="1" />
<Compiler>
<Add option="-Wall" />
<Add option="-DBUILD_DLL" />
<Add option="-g" />
</Compiler>
<Linker>
<Add library="user32" />
</Linker>
</Target>
<Target title="Release">
<Option output="bin/Release/DXI" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="3" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Option createStaticLib="1" />
<Compiler>
<Add option="-O2" />
<Add option="-Wall" />
<Add option="-m32" />
<Add option="-DBUILD_DLL" />
</Compiler>
<Linker>
<Add option="-s" />
<Add option="-m32" />
<Add option="-static" />
<Add option="-static-libgcc" />
<Add option="-static-libstdc++" />
<Add library="user32" />
</Linker>
</Target>
</Build>
<Unit filename="SharedMemory.cpp" />
<Unit filename="SharedMemory.hpp" />
<Unit filename="main.cpp" />
<Extensions>
<code_completion />
<envvars />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>
15 changes: 15 additions & 0 deletions DXI/DXI.depend
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# depslib dependency file v1.0
1387667367 source:c:\users\brandon\desktop\dxi\sharedmemory.cpp
"SharedMemory.hpp"

1387667354 c:\users\brandon\desktop\dxi\sharedmemory.hpp
<windows.h>
<sys/types.h>
<sys/mman.h>
<dlfcn.h>
<fcntl.h>
<unistd.h>
<tchar.h>
<iostream>
<map>

19 changes: 19 additions & 0 deletions DXI/DXI.layout
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<ActiveTarget name="Release" />
<File name="main.cpp" open="1" top="1" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="2768" topLine="71" />
</Cursor>
</File>
<File name="SharedMemory.cpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="356" topLine="189" />
</Cursor>
</File>
<File name="SharedMemory.hpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="83" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>
238 changes: 238 additions & 0 deletions DXI/SharedMemory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
/** © 2013, Brandon T. All Rights Reserved.
*
* This file is part of the DXI Library.
* DXI is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DXI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DXI. If not, see <http://www.gnu.org/licenses/>.
*/

#include "SharedMemory.hpp"

SharedMemory::SharedMemory(std::string MapName) : hFileMap(nullptr), pData(nullptr), MapName(MapName), Size(0), Debug(false), Events() {}
SharedMemory::SharedMemory(std::string MapName, std::size_t Size) : hFileMap(nullptr), pData(nullptr), MapName(MapName), Size(Size), Debug(false), Events() {}
SharedMemory::~SharedMemory()
{
ReleaseMemory();
DeleteAllEvents();
}

void* SharedMemory::GetDataPointer()
{
void* Ptr = pData;
return Ptr;
}

bool SharedMemory::OpenMemoryMap(std::size_t Size)
{
this->Size = Size;

#if defined _WIN32 || defined _WIN64
if ((hFileMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, false, MapName.c_str())) == nullptr)
{
if (Debug) std::cout << _T("\nCould Not Open Shared Memory Map.\n");
return false;
}

if ((pData = MapViewOfFile(hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, Size)) == nullptr)
{
if (Debug) std::cout << _T("\nCould Not Map View Of File.\n");
CloseHandle(hFileMap);
return false;
}

#else

if ((hFileMap = open(MapName.c_str(), O_RDWR | O_CREAT, 438)) == -1)
{
if (Debug) std::cout << _T("\nCould Not Open Shared Memory Map.\n");
return false;
}

if ((pData = mmap(nullptr, Size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, hFileMap, 0)) == MAP_FAILED)
{
if (Debug) std::cout << _T("\nCould Not Map View Of File.\n");
close(hFileMap);
return false;
}
#endif

if (Debug) std::cout << _T("\nInter-Process Communication Successful.\n");
return true;
}

bool SharedMemory::MapMemory(std::size_t Size)
{
this->Size = Size;

#if defined _WIN32 || defined _WIN64
if ((hFileMap = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, Size, MapName.c_str())) == nullptr)
{
if (Debug) std::cout << _T("\nCould Not Create Shared Memory Map.\n");
return false;
}

if ((pData = MapViewOfFile(hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, Size)) == nullptr)
{
if (Debug) std::cout << _T("\nCould Not Map View Of File.\n");
CloseHandle(hFileMap);
return false;
}

#else

if ((hFileMap = open(MapName.c_str(), O_RDWR | O_CREAT, 438)) == -1)
{
if (Debug) std::cout << _T("\nCould Not Create Shared Memory Map.\n");
return false;
}

if ((pData = mmap(nullptr, Size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, hFileMap, 0)) == MAP_FAILED)
{
if (Debug) std::cout << _T("\nCould Not Map View Of File.\n");
close(hFileMap);
return false;
}
#endif

if (Debug) std::cout << _T("\nMapped Shared Memory Successfully.\n");
return true;
}

bool SharedMemory::ReleaseMemory()
{
bool Result = false;
#if defined _WIN32 || defined _WIN64
if (pData)
{
Result = UnmapViewOfFile(pData);
pData = nullptr;
if (Result && Debug)
{
std::cout << _T("\nMemory Un-Mapped Successfully.\n");
}
}

if (hFileMap)
{
if (CloseHandle(hFileMap))
{
hFileMap = nullptr;
Result = Result && true;
if (Debug) std::cout << _T("\nMemory Map Closed Successfully.\n");
}
}

#else

if (pData)
{
Result = munmap(pData, Size);
if (!Result && Debug)
{
std::cout << _T("\nMemory Un-Mapped Successfully.\n");
}
pData = nullptr;
return true;
}

if (hFileMap)
{
if (!close(hFileMap))
{
hFileMap = nullptr;
if (Debug) std::cout << _T("\nMemory Map Closed Successfully.\n");
}
}
#endif
return Result;
}

bool SharedMemory::CreateNewEvent(LPSECURITY_ATTRIBUTES lpEventAttributes, bool bManualReset, bool bInitialState, std::string EventName)
{
std::map<std::string, void*>::iterator it = Events.find(EventName);
if (it != Events.end())
{
if (Debug)
{
std::cout << _T("\nCreateNewEvent Error: An Event With That Key Already Exists!\n");
}
return false;
}

Events.insert(std::pair<std::string, void*>(EventName, CreateEvent(lpEventAttributes, bManualReset, bInitialState, EventName.c_str())));
it = Events.end();
return ((--it)->second != nullptr);
}

std::uint32_t SharedMemory::OpenSingleEvent(std::string EventName, bool InheritHandle, bool SaveHandle, std::uint32_t dwDesiredAccess, std::uint32_t dwMilliseconds)
{
void* hEvent = OpenEvent(dwDesiredAccess, InheritHandle, EventName.c_str());
if (hEvent)
{
if (SaveHandle)
{
std::map<std::string, void*>::iterator it = Events.find(EventName);
if (it != Events.end())
{
CloseHandle(it->second);
it->second = hEvent;
}
else
Events.insert(std::pair<std::string, void*>(EventName, hEvent));
}
std::uint32_t Result = WaitForSingleObject(hEvent, dwMilliseconds);
if (!SaveHandle) CloseHandle(hEvent);
return Result;
}
CloseHandle(hEvent);
return WAIT_FAILED;
}

bool SharedMemory::SetEventSignal(std::string EventName, bool Signaled)
{
std::map<std::string, void*>::iterator it = Events.find(EventName);
if (it == Events.end())
{
if (Debug)
{
std::cout << _T("\nSetEventSignal Error: No Event With That Key Exists!\n");
}
return false;
}
if (Signaled) return SetEvent(it->second);
return ResetEvent(it->second);
}

bool SharedMemory::DeleteSingleEvent(std::string EventName)
{
std::map<std::string, void*>::iterator it = Events.find(EventName);
if (it == Events.end()) return true;
bool Result = CloseHandle(it->second);
Events.erase(it);
return Result;
}

bool SharedMemory::DeleteAllEvents()
{
bool Result = false;
for (std::map<std::string, void*>::iterator it = Events.begin(); it != Events.end(); ++it)
{
Result = Result && CloseHandle(it->second);
}
Events.clear();
return Result;
}

void SharedMemory::SetDebug(bool On)
{
Debug = On;
}
Loading

0 comments on commit f5732e4

Please sign in to comment.