unfinished changes - RENDERING_ENGINE_OPENGL is to be removed later.

This commit is contained in:
nullifiedcat 2017-11-13 23:58:28 +03:00
parent c78d9feb4c
commit 717b66c222
15 changed files with 179 additions and 674 deletions

View File

@ -110,6 +110,7 @@
<tool id="cdt.managedbuild.tool.gnu.assembler.exe.debug.1494960833" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.exe.debug"> <tool id="cdt.managedbuild.tool.gnu.assembler.exe.debug.1494960833" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.exe.debug">
<option id="gnu.both.asm.option.include.paths.536294380" name="Include paths (-I)" superClass="gnu.both.asm.option.include.paths" valueType="includePath"> <option id="gnu.both.asm.option.include.paths.536294380" name="Include paths (-I)" superClass="gnu.both.asm.option.include.paths" valueType="includePath">
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/cathook/include}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/cathook/include}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/cathook/xoverlay}&quot;"/>
</option> </option>
<inputType id="cdt.managedbuild.tool.gnu.assembler.input.517639342" superClass="cdt.managedbuild.tool.gnu.assembler.input"/> <inputType id="cdt.managedbuild.tool.gnu.assembler.input.517639342" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
</tool> </tool>

View File

@ -1,86 +0,0 @@
/*
* catpackets.h
*
* Created on: Nov 12, 2017
* Author: nullifiedcat
*/
#pragma once
#include <stdint.h>
enum
{
CATP_DRAW_BEGIN,
CATP_DRAW_END,
CATP_DRAW_RECT,
CATP_DRAW_RECT_OUTLINE,
CATP_DRAW_LINE,
CATP_DRAW_STRING,
CATP_DRAW_CIRCLE,
/* CATP_DRAW_ESP_BOX,
CATP_DRAW_ESP_BONE_LIST, */
CATP_TOTAL
};
struct catp_draw_begin_t
{
float world_to_screen[16];
};
struct catp_draw_rect_t
{
float x;
float y;
float w;
float h;
float rgba[4];
};
struct catp_draw_rect_outline_t
{
float x;
float y;
float w;
float h;
float rgba[4];
float thickness;
};
struct catp_draw_line_t
{
float x;
float y;
float dx;
float dy;
float rgba[4];
float thickness;
};
struct catp_draw_circle_t
{
float x;
float y;
float radius;
float rgba[4];
float thickness;
int steps;
};
struct catp_draw_string_t
{
float x;
float y;
float rgba[4];
uint32_t length;
};
/*
struct catp_draw_esp_box_t
{
};
*/

View File

@ -1,36 +0,0 @@
/*
* catsmclient.h
*
* Created on: Nov 12, 2017
* Author: nullifiedcat
*/
#pragma once
#include "xshm.h"
#include "xpcmutex.h"
#include "shmstream.h"
cat_shm_render_context_t
cat_shm_connect(const char *name);
void
cat_shm_render_begin(cat_shm_render_context_t *ctx, const float *world_to_screen);
void
cat_shm_render_end(cat_shm_render_context_t *ctx);
void
cat_shm_render_rect(cat_shm_render_context_t *ctx, float x, float y, float w, float h, const float *rgba);
void
cat_shm_render_rect_outline(cat_shm_render_context_t *ctx, float x, float y, float w, float h, const float *rgba, float thickness);
void
cat_shm_render_line(cat_shm_render_context_t *ctx, float x, float y, float dx, float dy, const float *rgba, float thickness);
void
cat_shm_render_string(cat_shm_render_context_t *ctx, float x, float y, const char *string, const float *rgba);
void
cat_shm_render_circle(cat_shm_render_context_t *ctx, float x, float y, float radius, const float *rgba, float thickness, int steps);

View File

@ -43,6 +43,11 @@
#include <elf.h> #include <elf.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h>=
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <assert.h>
#include "timer.hpp" #include "timer.hpp"
#include "averager.hpp" #include "averager.hpp"
@ -62,7 +67,8 @@ extern "C" {
#include <visual/ftrender.hpp> #include <visual/ftrender.hpp>
#include <visual/drawing.hpp> #include <visual/drawing.hpp>
#include "fidgetspinner.hpp" #include "fidgetspinner.hpp"
#include<visual/drawex.hpp>#include <visual/EffectGlow.hpp> #include <visual/drawex.hpp>
#include <visual/EffectGlow.hpp>
#include <visual/atlas.hpp> #include <visual/atlas.hpp>
#include <visual/EffectChams.hpp> #include <visual/EffectChams.hpp>
#include <visual/drawmgr.hpp> #include <visual/drawmgr.hpp>

View File

@ -42,8 +42,6 @@ void FireGameEvent_hook(void* _this, IGameEvent* event);
CUserCmd* GetUserCmd_hook(IInput*, int); CUserCmd* GetUserCmd_hook(IInput*, int);
void DrawModelExecute_hook(IVModelRender* _this, const DrawModelState_t& state, const ModelRenderInfo_t& info, matrix3x4_t* matrix); void DrawModelExecute_hook(IVModelRender* _this, const DrawModelState_t& state, const ModelRenderInfo_t& info, matrix3x4_t* matrix);
#ifdef RENDERING_ENGINE_OPENGL
/* SDL HOOKS */ /* SDL HOOKS */
union SDL_Event; union SDL_Event;
class SDL_Window; class SDL_Window;
@ -57,8 +55,6 @@ void SDL_GL_SwapWindow_hook(SDL_Window* window);
void DoSDLHooking(); void DoSDLHooking();
void DoSDLUnhooking(); void DoSDLUnhooking();
#endif
#include "CreateMove.hpp" #include "CreateMove.hpp"
#include "PaintTraverse.hpp" #include "PaintTraverse.hpp"
#include "others.hpp" #include "others.hpp"

View File

@ -11,7 +11,7 @@
extern "C" extern "C"
{ {
#include "catsmclient.h" #include "overlay.h"
} }
#define draw_api drawex::api #define draw_api drawex::api
@ -19,24 +19,36 @@ extern "C"
namespace drawex namespace drawex
{ {
extern cat_shm_render_context_t ctx;
extern std::thread rendering_thread;
void rendering_routine();
namespace api namespace api
{ {
extern bool ready_state; struct font_handle_t
{
xoverlay_font_handle_t handle;
};
struct texture_handle_t
{
xoverlay_texture_handle_t handle;
};
font_handle_t create_font(const char *path, float size);
texture_handle_t create_texture(const char *path);
void destroy_font(font_handle_t font);
void destroy_texture(texture_handle_t texture);
bool ready();
void initialize(); void initialize();
void draw_rect(float x, float y, float w, float h, const float* rgba); void draw_rect(float x, float y, float w, float h, const float* rgba);
void draw_rect_outlined(float x, float y, float w, float h, const float* rgba, float thickness); void draw_rect_outlined(float x, float y, float w, float h, const float* rgba, float thickness);
void draw_line(float x, float y, float dx, float dy, const float* rgba, float thickness); void draw_line(float x, float y, float dx, float dy, const float* rgba, float thickness);
void draw_rect_textured(float x, float y, float w, float h, const float* rgba, float u, float v, float s, float t); void draw_rect_textured(float x, float y, float w, float h, const float* rgba, texture_handle_t texture, float u, float v, float s, float t);
void draw_circle(float x, float y, float radius, const float *rgba, float thickness, int steps); void draw_circle(float x, float y, float radius, const float *rgba, float thickness, int steps);
void draw_string(float x, float y, const char *string, const float *rgba); void draw_string(float x, float y, const char *string, font_handle_t font, const float *rgba);
void draw_string_with_outline(float x, float y, const char *string, font_handle_t font, const float *rgba, float thickness);
void draw_begin(); void draw_begin();
void draw_end(); void draw_end();

View File

@ -1,36 +0,0 @@
/*
* xpcmutex.h
*
* Created on: Nov 12, 2017
* Author: nullifiedcat
*/
#pragma once
/*
* Cross-Process C shared mutex
*/
typedef struct xpcmutex_s
{
char name[64];
int fd;
} xpcmutex_t;
xpcmutex_t
xpcmutex_init(const char *name);
xpcmutex_t
xpcmutex_connect(const char *name);
void
xpcmutex_close(xpcmutex_t mutex);
void
xpcmutex_destroy(xpcmutex_t mutex);
void
xpcmutex_lock(xpcmutex_t mutex);
void
xpcmutex_unlock(xpcmutex_t mutex);

View File

@ -1,30 +0,0 @@
/*
* xshm.h
*
* Created on: Nov 12, 2017
* Author: nullifiedcat
*/
#pragma once
#include <stdint.h>
typedef struct xshm_s
{
void *data;
char name[64];
uint32_t size;
} xshm_t;
xshm_t
xshm_init(const char *name, uint32_t size);
xshm_t
xshm_connect(const char *name, uint32_t size);
void
xshm_destroy(xshm_t xshm);
void
xshm_close(xshm_t xshm);

View File

@ -1,103 +0,0 @@
/*
* catsmclient.c
*
* Created on: Nov 12, 2017
* Author: nullifiedcat
*/
#include "catsmclient.h"
#include "catpackets.h"
#include "shmstream.h"
cat_shm_render_context_t
cat_shm_connect(const char *name)
{
cat_shm_render_context_t ctx;
ctx.shm = xshm_connect(name, CAT_SHM_SIZE);
ctx.mutex = xpcmutex_connect(name);
ctx.curpos = 0;
ctx.lastpacket = 0;
return ctx;
}
void
cat_shm_render_begin(cat_shm_render_context_t *ctx, const float *world_to_screen)
{
xpcmutex_lock(ctx->mutex);
struct catp_draw_begin_t packet;
memcpy(packet.world_to_screen, world_to_screen, sizeof(float) * 16);
cat_shm_packet_send(ctx, CATP_DRAW_BEGIN, sizeof(packet), &packet);
}
void
cat_shm_render_end(cat_shm_render_context_t *ctx)
{
cat_shm_packet_send(ctx, CATP_DRAW_END, 0, 0);
xpcmutex_unlock(ctx->mutex);
}
void
cat_shm_render_rect(cat_shm_render_context_t *ctx, float x, float y, float w, float h, const float *rgba)
{
struct catp_draw_rect_t packet;
packet.x = x;
packet.y = y;
packet.h = h;
packet.w = w;
memcpy(packet.rgba, rgba, sizeof(float) * 4);
cat_shm_packet_send(ctx, CATP_DRAW_RECT, sizeof(packet), &packet);
}
void
cat_shm_render_rect_outline(cat_shm_render_context_t *ctx, float x, float y, float w, float h, const float *rgba, float thickness)
{
struct catp_draw_rect_outline_t packet;
packet.x = x;
packet.y = y;
packet.h = h;
packet.w = w;
memcpy(packet.rgba, rgba, sizeof(float) * 4);
packet.thickness = thickness;
cat_shm_packet_send(ctx, CATP_DRAW_RECT_OUTLINE, sizeof(packet), &packet);
}
void
cat_shm_render_line(cat_shm_render_context_t *ctx, float x, float y, float dx, float dy, const float *rgba, float thickness)
{
struct catp_draw_line_t packet;
packet.x = x;
packet.y = y;
packet.dx = dx;
packet.dy = dy;
memcpy(packet.rgba, rgba, sizeof(float) * 4);
packet.thickness = thickness;
cat_shm_packet_send(ctx, CATP_DRAW_LINE, sizeof(packet), &packet);
}
void
cat_shm_render_string(cat_shm_render_context_t *ctx, float x, float y, const char *string, const float *rgba)
{
struct catp_draw_string_t packet;
packet.x = x;
packet.y = y;
memcpy(packet.rgba, rgba, sizeof(float) * 4);
packet.length = strlen(string);
cat_shm_packet_send(ctx, CATP_DRAW_STRING, sizeof(packet), &packet);
cat_shm_packet_write_manual(ctx, packet.length, string);
}
void
cat_shm_render_circle(cat_shm_render_context_t *ctx, float x, float y, float radius, const float *rgba, float thickness, int steps)
{
struct catp_draw_circle_t packet;
packet.x = x;
packet.y = y;
packet.radius = radius;
packet.thickness = thickness;
packet.steps = steps;
memcpy(packet.rgba, rgba, sizeof(float) * 4);
cat_shm_packet_send(ctx, CATP_DRAW_CIRCLE, sizeof(packet), &packet);
}

View File

@ -299,10 +299,8 @@ void hack::Initialize() {
} }
logging::Info("SSE enabled.."); logging::Info("SSE enabled..");
#endif #endif
#if RENDERING_ENGINE_OPENGL
DoSDLHooking(); DoSDLHooking();
logging::Info("SDL hooking done"); logging::Info("SDL hooking done");
#endif
g_IGameEventManager->AddListener(&adv_event_listener, false); g_IGameEventManager->AddListener(&adv_event_listener, false);
#endif /* TEXTMODE */ #endif /* TEXTMODE */
@ -346,9 +344,7 @@ void hack::Shutdown() {
if (hack::shutdown) return; if (hack::shutdown) return;
hack::shutdown = true; hack::shutdown = true;
playerlist::Save(); playerlist::Save();
#if RENDERING_ENGINE_OPENGL
DoSDLUnhooking(); DoSDLUnhooking();
#endif
logging::Info("Unregistering convars.."); logging::Info("Unregistering convars..");
ConVar_Unregister(); ConVar_Unregister();
logging::Info("Shutting down killsay..."); logging::Info("Shutting down killsay...");

View File

@ -7,10 +7,8 @@
#include "common.hpp" #include "common.hpp"
#ifdef RENDERING_ENGINE_OPENGL #include "hooks/hookedmethods.hpp"
#include "hack.hpp"
#include "hookedmethods.h"
#include "../hack.h"
#include <link.h> #include <link.h>
@ -40,13 +38,22 @@ int SDL_PollEvent_hook(SDL_Event* event) {
void SDL_GL_SwapWindow_hook(SDL_Window* window) { void SDL_GL_SwapWindow_hook(SDL_Window* window) {
static SDL_GLContext ctx_tf2 = SDL_GL_GetCurrentContext(); static SDL_GLContext ctx_tf2 = SDL_GL_GetCurrentContext();
static SDL_GLContext ctx_imgui = nullptr; static SDL_GLContext ctx_imgui = nullptr;
static SDL_GLContext ctx_opengl = nullptr; static SDL_GLContext ctx_cathook = nullptr;
static bool init { false };
if (!disable_visuals) { if (!disable_visuals) {
PROF_SECTION(DRAW_cheat); PROF_SECTION(DRAW_cheat);
if (!ctx_imgui) { if (not init) {
#if RENDERING_ENGINE_OPENGL
ctx_imgui = SDL_GL_CreateContext(window); ctx_imgui = SDL_GL_CreateContext(window);
ImGui_ImplSdl_Init(window); ImGui_ImplSdl_Init(window);
ctx_opengl = SDL_GL_CreateContext(window); ctx_cathook = SDL_GL_CreateContext(window);
#elif RENDERING_ENGINE_XOVERLAY
#elif
#error "Unsupported rendering engine"
#endif
FTGL_Init(); FTGL_Init();
textures::Init(); textures::Init();
draw_api::initialize(); draw_api::initialize();
@ -58,7 +65,7 @@ void SDL_GL_SwapWindow_hook(SDL_Window* window) {
return; return;
} }
SDL_GL_MakeCurrent(window, ctx_opengl); SDL_GL_MakeCurrent(window, ctx_cathook);
{ {
std::lock_guard<std::mutex> draw_lock(drawing_mutex); std::lock_guard<std::mutex> draw_lock(drawing_mutex);
draw_api::render(); draw_api::render();
@ -85,14 +92,6 @@ void DoSDLHooking() {
SDL_GL_SwapWindow_o = *SDL_GL_SwapWindow_loc; SDL_GL_SwapWindow_o = *SDL_GL_SwapWindow_loc;
SDL_PollEvent_o = *SDL_PollEvent_loc; SDL_PollEvent_o = *SDL_PollEvent_loc;
/*char patch_1[4];
char patch_2[4];
*((unsigned*)patch_1) = (unsigned)SDL_GL_SwapWindow_hook;
*((unsigned*)patch_2) = (unsigned)SDL_PollEvent_hook;
Patch(SDL_GL_SwapWindow_loc, patch_1, 4);
Patch(SDL_PollEvent_loc, patch_2, 4);*/
*SDL_GL_SwapWindow_loc = SDL_GL_SwapWindow_hook; *SDL_GL_SwapWindow_loc = SDL_GL_SwapWindow_hook;
*SDL_PollEvent_loc = SDL_PollEvent_hook; *SDL_PollEvent_loc = SDL_PollEvent_hook;
} }
@ -102,4 +101,3 @@ void DoSDLUnhooking() {
*SDL_PollEvent_loc = SDL_PollEvent_o; *SDL_PollEvent_loc = SDL_PollEvent_o;
} }
#endif

View File

@ -10,56 +10,12 @@
extern "C" extern "C"
{ {
#include "catsmclient.h" #include "overlay.h"
} }
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
const char *drawex_pipe_name = "/tmp/cathook-rendering-pipe";
namespace drawex namespace drawex
{ {
cat_shm_render_context_t ctx;
std::thread rendering_thread;
void rendering_routine()
{
xpcmutex_t server_throttle_mutex = xpcmutex_connect("rendering-throttle");
while (true)
{
xpcmutex_lock(server_throttle_mutex);
PROF_SECTION(DRAWEX_rendering_routine);
if (hack::initialized && api::ready_state)
{
BeginCheatVisuals();
DrawCheatVisuals();
#if ENABLE_GUI
g_pGUI->Update();
#endif
EndCheatVisuals();
}
xpcmutex_unlock(server_throttle_mutex);
usleep(1000000 / 45);
//std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
CatCommand restart_render("debug_xoverlay_restart", "restart", []() {
api::ready_state = false;
xshm_close(ctx.shm);
xpcmutex_close(ctx.mutex);
api::initialize();
});
namespace api namespace api
{ {
@ -68,58 +24,65 @@ bool init = false;
void initialize() void initialize()
{ {
if (!init) int status = xoverlay_init();
rendering_thread = std::thread(rendering_routine); if (status < 0)
ctx = cat_shm_connect("cathook-rendering"); {
ready_state = true; logging::Info("ERROR: could not initialize Xoverlay");
init = true; }
} }
void draw_rect(float x, float y, float w, float h, const float* rgba) void draw_rect(float x, float y, float w, float h, const float* rgba)
{ {
PROF_SECTION(DRAWEX_draw_rect); PROF_SECTION(DRAWEX_draw_rect);
cat_shm_render_rect(&ctx, x, y, w, h, rgba); xoverlay_draw_rect(x, y, w, h, *reinterpret_cast<xoverlay_rgba_t *>(rgba));
} }
void draw_rect_outlined(float x, float y, float w, float h, const float* rgba, float thickness) void draw_rect_outlined(float x, float y, float w, float h, const float* rgba, float thickness)
{ {
PROF_SECTION(DRAWEX_draw_rect_outline); PROF_SECTION(DRAWEX_draw_rect_outline);
cat_shm_render_rect_outline(&ctx, x, y, w, h, rgba, thickness); xoverlay_draw_rect_outline(x, y, w, h, *reinterpret_cast<xoverlay_rgba_t *>(rgba), thickness);
} }
void draw_line(float x, float y, float dx, float dy, const float* rgba, float thickness) void draw_line(float x, float y, float dx, float dy, const float* rgba, float thickness)
{ {
PROF_SECTION(DRAWEX_draw_line); PROF_SECTION(DRAWEX_draw_line);
cat_shm_render_line(&ctx, x, y, dx, dy, rgba, thickness); xoverlay_draw_line(x, y, dx, dy, *reinterpret_cast<xoverlay_rgba_t *>(rgba), thickness);
} }
void draw_rect_textured(float x, float y, float w, float h, const float* rgba, float u, float v, float s, float t) void draw_rect_textured(float x, float y, float w, float h, const float* rgba, texture_handle_t texture, float u, float v, float s, float t)
{ {
assert(0); PROF_SECTION(DRAWEX_draw_rect_textured);
xoverlay_draw_rect_textured(x, y, w, h, *reinterpret_cast<xoverlay_rgba_t *>(rgba), texture.handle, u, v, s, t);
} }
void draw_circle(float x, float y, float radius, const float *rgba, float thickness, int steps) void draw_circle(float x, float y, float radius, const float *rgba, float thickness, int steps)
{ {
PROF_SECTION(DRAWEX_draw_circle); PROF_SECTION(DRAWEX_draw_circle);
cat_shm_render_circle(&ctx, x, y, radius, rgba, thickness, steps); xoverlay_draw_circle(x, y, radius, *reinterpret_cast<xoverlay_rgba_t *>(rgba), thickness, steps);
} }
void draw_string(float x, float y, const char *string, const float *rgba) void draw_string(float x, float y, const char *string, font_handle_t font, const float *rgba)
{ {
PROF_SECTION(DRAWEX_draw_string); PROF_SECTION(DRAWEX_draw_string);
cat_shm_render_string(&ctx, x, y, string, rgba); xoverlay_draw_string(x, y, string, font.handle, *reinterpret_cast<xoverlay_rgba_t *>(rgba), nullptr, nullptr);
}
void draw_string_with_outline(float x, float y, const char *string, font_handle_t font, const float *rgba, const float *rgba_outline, float thickness)
{
PROF_SECTION(DRAWEX_draw_string_with_outline);
xoverlay_draw_string_with_outline(x, y, string, font.handle, *reinterpret_cast<xoverlay_rgba_t *>(rgba), *reinterpret_cast<xoverlay_rgba_t *>(rgba_outline), thickness, 1, nullptr, nullptr);
} }
void draw_begin() void draw_begin()
{ {
PROF_SECTION(DRAWEX_draw_begin); PROF_SECTION(DRAWEX_draw_begin);
cat_shm_render_begin(&ctx, draw::wts.Base()); xoverlay_draw_begin();
} }
void draw_end() void draw_end()
{ {
PROF_SECTION(DRAWEX_draw_end); PROF_SECTION(DRAWEX_draw_end);
cat_shm_render_end(&ctx); xoverlay_draw_end();
} }
}} }}

View File

@ -12,10 +12,8 @@ void BeginCheatVisuals() {
#if RENDERING_ENGINE_OPENGL #if RENDERING_ENGINE_OPENGL
std::lock_guard<std::mutex> draw_lock(drawing_mutex); std::lock_guard<std::mutex> draw_lock(drawing_mutex);
#endif #endif
if (draw_api::ready_state) {
draw_api::draw_begin(); draw_api::draw_begin();
ResetStrings(); ResetStrings();
}
} }
std::mutex drawing_mutex; std::mutex drawing_mutex;
@ -27,7 +25,6 @@ void DrawCheatVisuals() {
#if RENDERING_ENGINE_OPENGL #if RENDERING_ENGINE_OPENGL
std::lock_guard<std::mutex> draw_lock(drawing_mutex); std::lock_guard<std::mutex> draw_lock(drawing_mutex);
#endif #endif
if (draw_api::ready_state) {
{ {
PROF_SECTION(DRAW_misc); PROF_SECTION(DRAW_misc);
SAFE_CALL(hacks::shared::misc::DrawText()); SAFE_CALL(hacks::shared::misc::DrawText());
@ -110,12 +107,9 @@ void DrawCheatVisuals() {
PROF_SECTION(DRAW_strings); PROF_SECTION(DRAW_strings);
DrawStrings(); DrawStrings();
} }
}
} }
void EndCheatVisuals() { void EndCheatVisuals() {
if (draw_api::ready_state) {
draw_api::draw_end(); draw_api::draw_end();
}
} }

View File

@ -1,75 +0,0 @@
/*
* xpcmutex.c
*
* Created on: Nov 12, 2017
* Author: nullifiedcat
*/
#include "xpcmutex.h"
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <assert.h>
xpcmutex_t
xpcmutex_init(const char *name)
{
xpcmutex_t mutex;
strncpy(mutex.name, name, sizeof(mutex.name) - 1);
char filename[128];
snprintf(filename, 127, "/tmp/.xpcmutex.%s", mutex.name);
int mask = umask(0);
remove(filename);
mkfifo(filename, 0666);
umask(mask);
mutex.fd = open(filename, O_RDWR);
xpcmutex_unlock(mutex);
return mutex;
}
xpcmutex_t
xpcmutex_connect(const char *name)
{
xpcmutex_t mutex;
strncpy(mutex.name, name, sizeof(mutex.name) - 1);
char filename[128];
snprintf(filename, 127, "/tmp/.xpcmutex.%s", mutex.name);
mutex.fd = open(filename, O_RDWR);
return mutex;
}
void
xpcmutex_close(xpcmutex_t mutex)
{
close(mutex.fd);
}
void
xpcmutex_destroy(xpcmutex_t mutex)
{
char filename[128];
snprintf(filename, 127, "/tmp/.xpcmutex.%s", mutex.name);
remove(filename);
}
void
xpcmutex_lock(xpcmutex_t mutex)
{
char buf[1];
while (1 != read(mutex.fd, buf, 1))
{
usleep(10);
}
}
void
xpcmutex_unlock(xpcmutex_t mutex)
{
while (1 != write(mutex.fd, "1", 1))
{
usleep(10);
}
}

View File

@ -1,95 +0,0 @@
/*
* xshm.c
*
* Created on: Nov 12, 2017
* Author: nullifiedcat
*/
#include "xshm.h"
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
xshm_t
xshm_init(const char *name, uint32_t size)
{
xshm_t xshm;
strncpy(xshm.name, name, sizeof(xshm.name) - 1);
char filename[128];
snprintf(filename, 127, "xshm_%s", xshm.name);
xshm.size = size;
shm_unlink(xshm.name);
int omask = umask(0);
int flags = O_RDWR | O_CREAT;
int fd = shm_open(xshm.name, flags, S_IRWXU | S_IRWXG | S_IRWXO);
if (fd < 0)
{
perror("xshm opening error\n");
return xshm;
}
if (ftruncate(fd, size) != 0)
{
perror("xshm opening error\n");
return xshm;
}
umask(omask);
xshm.data = mmap(0, size, PROT_WRITE | PROT_READ | PROT_EXEC, MAP_SHARED, fd, 0);
if (xshm.data == (void *) -1)
{
perror("xshm mapping error\n");
return xshm;
}
close(fd);
return xshm;
}
xshm_t
xshm_connect(const char *name, uint32_t size)
{
xshm_t xshm;
strncpy(xshm.name, name, sizeof(xshm.name) - 1);
char filename[128];
snprintf(filename, 127, "xshm_%s", xshm.name);
xshm.size = size;
int omask = umask(0);
int flags = O_RDWR;
int fd = shm_open(xshm.name, flags, S_IRWXU | S_IRWXG | S_IRWXO);
if (fd < 0)
{
perror("xshm opening error\n");
return xshm;
}
if (ftruncate(fd, size) != 0)
{
perror("xshm opening error\n");
return xshm;
}
umask(omask);
xshm.data = mmap(0, size, PROT_WRITE | PROT_READ | PROT_EXEC, MAP_SHARED, fd, 0);
if (xshm.data == (void *) -1)
{
perror("xshm mapping error\n");
return xshm;
}
close(fd);
return xshm;
}
void
xshm_destroy(xshm_t xshm)
{
munmap(xshm.data, xshm.size);
shm_unlink(xshm.name);
}
void
xshm_close(xshm_t xshm)
{
munmap(xshm.data, xshm.size);
}