Trying to understand detouring/hooking.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Trying to understand detouring/hooking.

Post by Sir_Silver »

As a preface, please forgive me if I am using some technical terms incorrectly here, just try to bare with me.

I am trying to change the love window's button's behavior: minimize, maximize, and maybe even close.

The first thing I tried doing was looking on the wiki side to see if there was a callback function for when any of these events occur, but I was unsuccessful in finding one.

After this, I decided to trying "hooking" into the minimize function to see if I could add additional behavior to it:

Code: Select all

	local o_minimize = love.window.minimize
	
	function love.window.minimize()
		o_minimize()
		--Added behavior here.
	end
Changing the value of love.window.minimize does not seem to alter the behavior of pressing the minimize button at all.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Trying to understand detouring/hooking.

Post by zorg »

Usually, one wants to run their custom behaviour before calling the original function, but of course, this depends on what you're trying to accomplish.

Also, i believe that only love.resized exists as a callback, the others are just functions that programmatically minimize/maximize/restore your window; depending on what you want to do, i'd just put the needed functions around those calls, in the order that's needed... then again, if you have to call those in more than a few places, i guess either defining a helper function or monkeypatching löve's ones may work as well.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Trying to understand detouring/hooking.

Post by s-ol »

Sir_Silver wrote:As a preface, please forgive me if I am using some technical terms incorrectly here, just try to bare with me.

I am trying to change the love window's button's behavior: minimize, maximize, and maybe even close.

The first thing I tried doing was looking on the wiki side to see if there was a callback function for when any of these events occur, but I was unsuccessful in finding one.

After this, I decided to trying "hooking" into the minimize function to see if I could add additional behavior to it:

Code: Select all

	local o_minimize = love.window.minimize
	
	function love.window.minimize()
		o_minimize()
		--Added behavior here.
	end
Changing the value of love.window.minimize does not seem to alter the behavior of pressing the minimize button at all.
That's because minimizing the window is done by the operating system, which might tell LÖVE about that happening and then proceeds to minimize the window. None of this happens in lua code. When you call love.window.minimze() yourself, that will work as expected, but the operating system won't call your lua function and LÖVE doesn't call it either.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: Trying to understand detouring/hooking.

Post by Sir_Silver »

That's because minimizing the window is done by the operating system, which might tell LÖVE about that happening and then proceeds to minimize the window. None of this happens in lua code. When you call love.window.minimze() yourself, that will work as expected, but the operating system won't call your lua function and LÖVE doesn't call it either.
Ah, you're absolutely right. I tried calling the detoured - modified version - of the function as such:

Code: Select all

		local o_minimize = love.window.minimize
   
   	function love.window.minimize()
     		o_minimize()
     		--Added behavior here.
  	 end
  	 
  	 love.window.minimize()
Calling the function myself seems to invoke the detoured function, however, pressing the minimize button does not, so it would seem you are correct in that the minimize button is an operating system call and not a love one.

I will look into love.resized as zorg mentioned to see if I can do what I'm trying to do in there. Thank you for the help!
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Trying to understand detouring/hooking.

Post by zorg »

A quick fix on my part, it's [wiki]love.resize[/wiki], without a d.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: Trying to understand detouring/hooking.

Post by Sir_Silver »

I'm checking it out right now. It would seem that love.window.minimize does not seem to call the resize function, however, maximize does.

My guess is that minimize is just hiding the window as opposed to actually minimizing it's size. With this new knowledge, I can add additional behavior to when the size of the window is changed, however, it still doesn't give me access to the minimize and maximize buttons of the window, but it will suffice.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Trying to understand detouring/hooking.

Post by zorg »

On the other hand, you can query for the window state, so you could use if-s inside your update callback to execute stuff when not minimized and such...
...except that it may depend on the OS under what circumstances it pauses the "main thread", therefore, as it is by default since löve uses that for both graphics and logic update along with inputs with the stock love.run, things may not run as how one would expect.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Trying to understand detouring/hooking.

Post by Positive07 »

You can use the [wiki]love.visible[/wiki] callback, it will be called with false when the window is minimized and true when it has been opened again. There is also [wiki]love.focus[/wiki] and [wiki]love.mousefocus[/wiki] that may also help.

When the window is maximized [wiki]love.resize[/wiki] is called whith the new dimensions, but is difficult to tell if the event came from pressing maximize or simply by resizing the window so you may get non-expected behaviour here, maybe use a resize treshold, like if the window is resized too suddenly then a human couldn't have done it or something like that, maybe you can use the screen size to tell if the size passed to love.resize is similar... Not sure

For the close button there is [wiki]love.quit[/wiki] this gets called whenever the LÖVE window is expected to close, if love.quit returns true then the game won't quit so you can make it do whatever you like instead of closing, you should provide a way to quit though, so maybe set an internal variable when actually quitting that is not commonly set by just pressing the exit button, say:

Code: Select all

local quit = false
love.keyreleased = function (k)
    if k == "escape" then
        quit = true
        love.event.quit()
    end
end

love.quit = function ()
    if not quit then
        --Do whatever you like
        return true
    end
end
I'm not sure if you can unminimize though, I don't think that calling [wiki]love.window.minimize[/wiki] when the window is already minimized does something, you could try [wiki]love.window.maximize[/wiki] but I don't think that would work either, so I don't think you can stop the standard behaviour, unless you add a function to LÖVE maybe, not sure.

I will investigate if there is something in SDL and try writting an FFI wrapper if there is, hope this helps!
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: Trying to understand detouring/hooking.

Post by Sir_Silver »

Thanks for the input Positive and zorg. Mostly, I was trying to see if I could make use of the buttons at the top right of the window: minimize, maximize, and close, more so than just trying to manipulate what happens when the window is resized. Based on what s-ol said, I've determined that this is something that is outside of the scope of lua and my ability, so I'm gonna let this one rest. :P
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Trying to understand detouring/hooking.

Post by Positive07 »

As I said, you can't restore a window from maximized or minimized state from code, unless you use this function:

Code: Select all

local ffi = require("ffi")

--SDL headers we use
ffi.cdef([[
    typedef struct SDL_Window SDL_Window;
    SDL_Window *SDL_GL_GetCurrentWindow(void);
    void SDL_RestoreWindow(SDL_Window *window);
]])

local sdl = (ffi.os == "Windows") and ffi.load("SDL2") or ffi.C

local restoreWindow = function ()
    sdl.SDL_RestoreWindow(sdl.SDL_GL_GetCurrentWindow())
end

return restoreWindow
Just save that in a file for example called restoreWindow.lua and use it like this

Code: Select all

local restoreWindow

love.load = function ()
    restoreWindow = require "restoreWindow"
end

love.visible = function (visible)
    if not visible then
        restoreWindow()
        --Do something instead of minimizing!
        --Will actually minimize and restore itself back though
    end
end
That should work, this is not tested though so it may FAIL! Good luck
Last edited by Positive07 on Tue Aug 23, 2016 11:16 pm, edited 1 time in total.
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Post Reply

Who is online

Users browsing this forum: No registered users and 66 guests