Add new title-folder plugin

develop
lethosor 2016-08-09 20:09:50 -04:00
parent 02eef95147
commit 53a0d73d23
4 changed files with 130 additions and 0 deletions

@ -30,6 +30,13 @@ Changelog
.. contents::
:depth: 2
DFHack future
=============
New Plugins
-----------
- `title-folder`: shows DF folder name in window title bar when enabled
DFHack 0.43.03-r1
=================

@ -601,6 +601,12 @@ resume
Allows automatic resumption of suspended constructions, along with colored
UI hints for construction status.
.. _title-folder:
title-folder
=============
Displays the DF folder name in the window title bar when enabled.
.. _title-version:
title-version

@ -139,6 +139,7 @@ if (BUILD_SUPPORTED)
DFHACK_PLUGIN(stocks stocks.cpp)
DFHACK_PLUGIN(strangemood strangemood.cpp)
DFHACK_PLUGIN(tiletypes tiletypes.cpp Brushes.h)
DFHACK_PLUGIN(title-folder title-folder.cpp)
DFHACK_PLUGIN(title-version title-version.cpp)
DFHACK_PLUGIN(trackstop trackstop.cpp)
# DFHACK_PLUGIN(treefarm treefarm.cpp)

@ -0,0 +1,116 @@
#include "Console.h"
#include "Core.h"
#include "DataDefs.h"
#include "Export.h"
#include "MemAccess.h"
#include "PluginManager.h"
using namespace DFHack;
DFHACK_PLUGIN("title-folder");
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
// SDL frees the old window title when changed
static std::string original_title;
static DFLibrary *sdl_handle = NULL;
static const std::vector<std::string> sdl_libs {
"SDLreal.dll",
"SDL.framework/SDL",
"libSDL-1.2.so.0"
};
void (*_SDL_WM_GetCaption)(const char**, const char**) = NULL;
void SDL_WM_GetCaption(const char **title, const char **icon) {
_SDL_WM_GetCaption(title, icon);
}
void (*_SDL_WM_SetCaption)(const char*, const char*) = NULL;
void SDL_WM_SetCaption(const char *title, const char *icon) {
_SDL_WM_SetCaption(title, icon);
}
DFhackCExport command_result plugin_enable (color_ostream &out, bool state);
DFhackCExport command_result plugin_shutdown (color_ostream &out);
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands)
{
for (auto it = sdl_libs.begin(); it != sdl_libs.end(); ++it)
{
if ((sdl_handle = OpenPlugin(it->c_str())))
break;
}
if (!sdl_handle)
{
out.printerr("title-folder: Could not load SDL.\n");
return CR_FAILURE;
}
#define bind(name) \
_##name = (decltype(_##name))LookupPlugin(sdl_handle, #name); \
if (!_##name) { \
out.printerr("title-folder: Bind failed: " #name "\n"); \
plugin_shutdown(out); \
return CR_FAILURE; \
}
bind(SDL_WM_GetCaption);
bind(SDL_WM_SetCaption);
#undef bind
const char *title = NULL;
SDL_WM_GetCaption(&title, NULL);
if (!title)
{
out.printerr("title-folder: Failed to get original title\n");
plugin_shutdown(out);
return CR_FAILURE;
}
original_title = title;
return CR_OK;
}
DFhackCExport command_result plugin_shutdown (color_ostream &out)
{
if (is_enabled)
{
plugin_enable(out, false);
}
if (sdl_handle)
{
ClosePlugin(sdl_handle);
sdl_handle = NULL;
}
return CR_OK;
}
DFhackCExport command_result plugin_enable (color_ostream &out, bool state)
{
if (state == is_enabled)
return CR_OK;
if (state)
{
std::string path = Core::getInstance().p->getPath();
std::string folder;
size_t pos = path.find_last_of('/');
if (pos == std::string::npos)
pos = path.find_last_of('\\');
if (pos != std::string::npos)
folder = path.substr(pos + 1);
else
folder = path;
std::string title = original_title + " (" + folder + ")";
SDL_WM_SetCaption(title.c_str(), NULL);
}
else
{
SDL_WM_SetCaption(original_title.c_str(), NULL);
}
is_enabled = state;
return CR_OK;
}