Skip to content

Commit

Permalink
[fix] Add util.ffiLoadCandidates to try multiple SDL2 library names (#…
Browse files Browse the repository at this point in the history
…873)

Fixes <koreader/koreader#4826 (comment)>.

A bit like the idea penned down [here](koreader/koreader#3108 (comment)).
  • Loading branch information
Frenzie authored Mar 21, 2019
1 parent 4d412ee commit 148eb32
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 5 additions & 1 deletion ffi/SDL2_0.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ local dummy = require("ffi/linux_input_h")

-----------------------------------------------------------------

local SDL = ffi.load("SDL2")
local SDL = util.ffiLoadCandidates{
"SDL2",
-- this unfortunately needs to be written in full due to the . in the name
"libSDL2-2.0.so",
}

-- for frontend SDL event handling
local EV_SDL = 53 -- ASCII code for S
Expand Down
23 changes: 22 additions & 1 deletion ffi/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,21 @@ function util.multiByteToUTF8(str, codepage)
end
end

function util.ffiLoadCandidates(candidates)
local lib_loaded, lib

for _, candidate in ipairs(candidates) do
lib_loaded, lib = pcall(ffi.load, candidate)

if lib_loaded then
return lib
end
end

-- we failed, this is the error message
return lib
end

--- Returns true if isWindows…
function util.isWindows()
return ffi.os == "Windows"
Expand Down Expand Up @@ -452,12 +467,18 @@ end
--- Returns true if SDL2
function util.haveSDL2()
local err

if haveSDL2 == nil then
haveSDL2, err = pcall(ffi.load, "SDL2")
haveSDL2, err = util.ffiLoadCandidates{
"SDL2",
-- this unfortunately needs to be written in full due to the . in the name
"libSDL2-2.0.so",
}
end
if not haveSDL2 then
print("SDL2 not loaded:", err)
end

return haveSDL2
end

Expand Down

0 comments on commit 148eb32

Please sign in to comment.