Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new method set_window_icon for macos, windows, html5 platform #71

Merged
merged 5 commits into from Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -9,3 +9,4 @@ builtins
.internal
.clang_complete
.vscode/
*.txt
30 changes: 29 additions & 1 deletion defos/src/defos.cpp
Expand Up @@ -3,11 +3,12 @@
#define MODULE_NAME "defos"

#define DLIB_LOG_DOMAIN LIB_NAME

#include <dmsdk/sdk.h>

#if defined(DM_PLATFORM_OSX) || defined(DM_PLATFORM_WINDOWS) || defined(DM_PLATFORM_HTML5)

#include "defos_private.h"
#include <stdlib.h>

static bool checkboolean(lua_State *L, int index)
{
Expand Down Expand Up @@ -142,6 +143,21 @@ static int is_maximized(lua_State *L)
return 1;
}

static int set_window_icon(lua_State *L)
{
const char *icon_path = luaL_checkstring(L, 1);
defos_set_window_icon(icon_path);
return 0;
}

static int get_bundle_root(lua_State *L)
{
char* bundle_path = defos_get_bundle_root();
lua_pushstring(L, bundle_path);
free(bundle_path);
return 1;
}

// Windows console

static int set_console_visible(lua_State *L)
Expand Down Expand Up @@ -401,12 +417,15 @@ static const luaL_reg Module_methods[] =
{"get_view_size", get_view_size},
{"set_cursor", set_cursor},
{"reset_cursor", reset_cursor},
{"set_window_icon", set_window_icon},
{"get_bundle_root", get_bundle_root},
{0, 0}};

static void LuaInit(lua_State *L)
{
int top = lua_gettop(L);
luaL_register(L, MODULE_NAME, Module_methods);

lua_pushnumber(L, DEFOS_CURSOR_ARROW);
lua_setfield(L, -2, "CURSOR_ARROW");
lua_pushnumber(L, DEFOS_CURSOR_CROSSHAIR);
Expand All @@ -415,6 +434,15 @@ static void LuaInit(lua_State *L)
lua_setfield(L, -2, "CURSOR_HAND");
lua_pushnumber(L, DEFOS_CURSOR_IBEAM);
lua_setfield(L, -2, "CURSOR_IBEAM");

#if defined(DM_PLATFORM_WINDOWS)
lua_pushstring(L, "\\");
lua_setfield(L, -2, "PATH_SEP");
#else
lua_pushstring(L, "/");
lua_setfield(L, -2, "PATH_SEP");
#endif

lua_pop(L, 1);
assert(top == lua_gettop(L));
}
Expand Down
26 changes: 26 additions & 0 deletions defos/src/defos_html5.cpp
Expand Up @@ -144,6 +144,32 @@ void defos_set_window_title(const char* title_lua) {
EM_ASM_({document.title = UTF8ToString($0)}, title_lua);
}

void defos_set_window_icon(const char *icon_path)
{
EM_ASM_({
function changeFavicon(src) {
var oldLink = document.querySelector("link[rel*='icon']");
if (oldLink) { document.head.removeChild(oldLink); }
var link = document.createElement('link');
link.rel = 'shortcut icon';
link.href = src;
document.head.appendChild(link);
}
changeFavicon(UTF8ToString($0));
}, icon_path);
}

char* defos_get_bundle_root() {
char*bundlePath = (char*)EM_ASM_INT({
var jsString = location.href.substring(0, location.href.lastIndexOf("/"));
var lengthBytes = lengthBytesUTF8(jsString)+1; // 'jsString.length' would return the length of the string as UTF-16 units, but Emscripten C strings operate as UTF-8.
var stringOnWasmHeap = _malloc(lengthBytes);
stringToUTF8(jsString, stringOnWasmHeap, lengthBytes+1);
return stringOnWasmHeap;
},0);
return bundlePath;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This leaks the bundlePath string after it gets passed to Lua in get_bundle_root(). Maybe always make sure we return a string we own and free() it in get_bundle_root()?

Copy link
Collaborator Author

@AGulev AGulev Mar 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to do this (I asked you aboutt free()), it was my fault with const, thx!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

void defos_set_window_size(float x, float y, float w, float h) {
defos_set_view_size(x, y, w, h);
}
Expand Down
17 changes: 17 additions & 0 deletions defos/src/defos_mac.mm
Expand Up @@ -93,6 +93,23 @@ void defos_set_window_title(const char* title_lua) {
[window setTitle:title];
}

void defos_set_window_icon(const char *icon_path){
@autoreleasepool {
NSString *path = [NSString stringWithUTF8String:icon_path];
NSImage* image = [[NSImage alloc] initWithContentsOfFile: path];
[window setRepresentedURL:[NSURL URLWithString:path]];
[[window standardWindowButton:NSWindowDocumentIconButton] setImage:image];
[image release];
}
}

char* defos_get_bundle_root() {
const char *bundlePath = [[[NSBundle mainBundle] bundlePath] UTF8String];
char *bundlePath_lua = (char*)malloc(strlen(bundlePath) + 1);
strcpy(bundlePath_lua, bundlePath);
return bundlePath_lua;
}

void defos_set_window_size(float x, float y, float w, float h) {
if (isnan(x)) {
NSRect frame = window.screen.frame;
Expand Down
4 changes: 3 additions & 1 deletion defos/src/defos_private.h
Expand Up @@ -50,6 +50,8 @@ extern bool defos_is_fullscreen();
extern bool defos_is_maximized();

extern void defos_set_window_title(const char* title_lua);
extern void defos_set_window_icon(const char *icon_path);
extern char* defos_get_bundle_root();

extern void defos_set_window_size(float x, float y, float w, float h);
extern WinRect defos_get_window_size();
Expand All @@ -75,4 +77,4 @@ extern void defos_set_custom_cursor_html5(const char *url);
extern void defos_set_custom_cursor_win(const char *filename);
extern void defos_set_custom_cursor_mac(dmBuffer::HBuffer buffer, float hotSpotX, float hotSpotY);
extern void defos_set_cursor(DefosCursor cursor);
extern void defos_reset_cursor();
extern void defos_reset_cursor();
16 changes: 16 additions & 0 deletions defos/src/defos_win.cpp
Expand Up @@ -206,6 +206,22 @@ void defos_set_window_title(const char *title_lua)
SetWindowTextW(dmGraphics::GetNativeWindowsHWND(), CA2W(title_lua));
}

void defos_set_window_icon(const char *icon_path)
{
HANDLE icon = LoadImage(NULL, icon_path, IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
if (icon)
{
HWND window = dmGraphics::GetNativeWindowsHWND();
SendMessage(window, (UINT)WM_SETICON, ICON_BIG, (LPARAM)icon);
}
}

char* defos_get_bundle_root() {
char *bundlePath = (char*)malloc(MAX_PATH);
GetCurrentDirectory(MAX_PATH, bundlePath);
return bundlePath;
}

WinRect defos_get_window_size()
{
HWND window = dmGraphics::GetNativeWindowsHWND();
Expand Down