[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/livecodeali/livecode/develop/engine/src/em-dc.cpp [Back]  [Original]

/*                                                                     -*-c++-*-

Copyright (C) 2003-2015 LiveCode Ltd.

This file is part of LiveCode.

LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.

LiveCode 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 LiveCode.  If not see .  */

#include "prefix.h"

#include "em-dc.h"
#include "em-javascript.h"
#include "em-view.h"
#include "em-async.h"
#include "em-event.h"
#include "em-util.h"
#include "em-liburl.h"

#include "osspec.h"
#include "eventqueue.h"
#include "redraw.h"
#include "dispatch.h"
#include "globals.h"
#include "graphics_util.h"

/* ================================================================
 * Helper Functions
 * ================================================================ */

MCRectangle MCEmscriptenGetWindowRect(uint32_t p_window_id)
{
	uint32_t t_left, t_top, t_right, t_bottom;
	MCEmscriptenGetWindowRect(p_window_id, &t_left, &t_top, &t_right, &t_bottom);
	
	return MCRectangleMake(t_left, t_top, t_right - t_left, t_bottom - t_top);
}

void MCEmscriptenSetWindowRect(uint32_t p_window_id, const MCRectangle &p_rect)
{
	MCEmscriptenSetWindowRect(p_window_id, p_rect.x, p_rect.y, p_rect.x + p_rect.width, p_rect.y + p_rect.height);
}

MCRectangle MCEmscriptenGetDisplayRect()
{
	uint32_t t_left, t_top, t_right, t_bottom;
	MCEmscriptenGetDisplayRect(&t_left, &t_top, &t_right, &t_bottom);
	
	return MCRectangleMake(t_left, t_top, t_right - t_left, t_bottom - t_top);
}

/* ================================================================
 * Initialization / Finalization
 * ================================================================ */

extern "C" bool MCEmscriptenDCInitializeJS();
bool MCEmscriptenDCInitialize()
{
	return MCEmscriptenDCInitializeJS();
}

extern "C" void MCEmscriptenDCFinalizeJS();
void MCEmscriptenDCFinalize()
{
	MCEmscriptenDCFinalizeJS();
}

/* ================================================================
 * Construction/Destruction
 * ================================================================ */

MCUIDC *
MCCreateScreenDC()
{
	return new MCScreenDC;
}

MC_DLLEXPORT_DEF MCStack *
MCEmscriptenGetStackForWindow(Window p_window)
{
	if (MCnoui) return nil;
	
	MCStack *t_stack = MCdispatcher->findstackd(p_window);
	
	return t_stack;
}

MC_DLLEXPORT_DEF
bool MCEmscriptenHandleMousePress(MCStack *p_stack, uint32_t p_time, uint32_t p_modifiers, MCMousePressState p_state, int32_t p_button)
{
	if (MCnoui) return false;
	
	MCScreenDC *t_dc = static_cast(MCscreen);
	t_dc->update_mouse_press_state(p_state, p_button);
	
	MCEventQueuePostMousePress(p_stack, p_time, p_modifiers, p_state, p_button);
	
	return true;
}

static inline MCPoint MCEmscriptenWindowToGlobalLoc(MCStack *p_stack, const MCPoint &p_loc)
{
	MCRectangle t_window_rect = p_stack->view_getrect();
	return MCPointOffset(p_loc, t_window_rect.x, t_window_rect.y);
}

MC_DLLEXPORT_DEF
bool MCEmscriptenHandleMousePosition(MCStack *p_stack, uint32_t p_time, uint32_t p_modifiers, int32_t p_x, int32_t p_y)
{
	if (MCnoui) return false;
	
	MCScreenDC *t_dc = static_cast(MCscreen);
	
	MCPoint t_position = MCPointMake(p_x, p_y);
	if (p_stack)
		t_position = MCEmscriptenWindowToGlobalLoc(p_stack, t_position);
	if (t_dc->update_mouse_position(t_position))
		MCEventQueuePostMousePosition(p_stack, p_time, p_modifiers, p_x, p_y);
	
	return true;
}

MCScreenDC::MCScreenDC()
	: m_main_window(nil), m_mouse_button_state(0)
{
	m_mouse_position = MCPointMake(-1,-1);
}

MCScreenDC::~MCScreenDC()
{
}

Boolean
MCScreenDC::open()
{
	return
		MCEmscriptenJSInitialize() &&
		MCEmscriptenEventInitialize() &&
		MCEmscriptenViewInitialize() &&
        MCEmscriptenLibUrlInitialize() &&
        MCEmscriptenDCInitialize();
}


Boolean
MCScreenDC::close(Boolean force)
{
	MCEmscriptenDCFinalize();
	MCEmscriptenViewFinalize();
	MCEmscriptenEventFinalize();
    MCEmscriptenLibUrlFinalize();
    MCEmscriptenJSFinalize();

	return true;
}

/* ================================================================
 * Window management
 * ================================================================ */

void
MCScreenDC::openwindow(Window p_window,
                       Boolean override)
{
	uint32_t t_window = reinterpret_cast(p_window);
	MCLog("set window visible");
	MCEmscriptenSetWindowVisible(t_window, true);

	MCLog("find stack");
	MCStack *t_stack = MCdispatcher->findstackd(p_window);

	/* Enable drawing */
	MCLog("activate tilecache");
	t_stack->view_activatetilecache();

	t_stack->setextendedstate(false, ECS_DONTDRAW);

	/* Set up view to match window, as far as possible */
	/* FIXME Implement HiDPI support */

	MCEmscriptenSetWindowRect(t_window, t_stack->view_getrect());

	t_stack->view_configure(true);
	t_stack->dirtyall();
}

void
MCScreenDC::closewindow(Window p_window)
{
	MCAssert(p_window);

	uint32_t t_window = reinterpret_cast(p_window);
	MCEmscriptenSetWindowVisible(t_window, false);
}

void
MCScreenDC::destroywindow(Window & x_window)
{
	uint32_t t_window = reinterpret_cast(x_window);
	MCEmscriptenDestroyWindow(t_window);
	
	x_window = nil;
}

void
MCScreenDC::raisewindow(Window p_window)
{
	uint32_t t_window = reinterpret_cast(p_window);
	MCEmscriptenRaiseWindow(t_window);
}

uintptr_t
MCScreenDC::dtouint(Drawable p_window)
{
	return reinterpret_cast(p_window);
}

Boolean
MCScreenDC::uinttowindow(uintptr_t p_uint, Window &r_window)
{
	r_window = reinterpret_cast(p_uint);
	return True;
}

void *MCScreenDC::GetNativeWindowHandle(Window p_window)
{
    return (void*)dtouint(p_window);
}

bool
MCScreenDC::platform_getwindowgeometry(Window p_window,
                                       MCRectangle & r_rect)
{
	/* FIXME Implement HiDPI support */

	uint32_t t_window = reinterpret_cast(p_window);
	r_rect = MCEmscriptenGetWindowRect(t_window);
	return true;
}

/* ================================================================
 * Display management
 * ================================================================ */

bool MCScreenDC::platform_getdisplays(bool p_effective, MCDisplay *&r_displays, uint32_t &r_count)
{
	MCDisplay *t_display = nil;
	if (!MCMemoryNew(t_display))
		return false;
	
	t_display->viewport = t_display->workarea = MCEmscriptenGetDisplayRect();
	
	r_displays = t_display;
	r_count = 1;
	return true;
}

/* ================================================================
 * Event loop
 * ================================================================ */

// Important: This function is on the emterpreter whitelist. If its
// signature function changes, the mangled name must be updated in
// em-whitelist.json

/* Returns true if quit is requested, or from any inner main loop. */
Boolean
MCScreenDC::wait(real64_t p_duration,
                 Boolean p_allow_dispatch,
                 Boolean p_accept_any_event)
{
	/* Don't permit inner main loops.  They cause amusing "-12" assertion
	 * failures from Emterpreter. */
	if (0 < int(MCwaitdepth))
	{
		return true;
	}

	p_duration = MCMax(p_duration, 0.0);

	/* We allow p_duration to be infinite, but only if
	 * p_accept_any_event is set. */
	MCAssert(isfinite(p_duration) || p_accept_any_event);

	MCDeletedObjectsEnterWait(p_allow_dispatch);
	++MCwaitdepth;

	real64_t t_current_time = MCS_time();
	real64_t t_exit_time = t_current_time + p_duration;
	real64_t t_event_time = t_exit_time;

	bool t_first = true;
	bool t_done = false;

	while (!t_done && !MCquit)
	{
		/* If more than one iteration through the wait loop is needed
		 * (i.e. doing the work that was already queued didn't take
		 * long enough), wait for a timeout. */
		if (!t_first)
		{
			/* Update estimate of time remaining */
			t_current_time = MCS_time();

			real64_t t_sleep_time = MCMin(t_event_time - t_current_time,
			                              t_exit_time - t_current_time);

			/* Check if the requested wait duration has already
			 * elapsed */
			if (!t_done)
			{
				t_done = (t_sleep_time 

Web Proxy Viewer  |  New URL  |  Original Page