Skip to content

Commit

Permalink
Add support for SDL as a base library on OS X.
Browse files Browse the repository at this point in the history
This commit adds the basic setup in CMake to allow SDL as a base
library for FLTK on OS X (and probably for other platforms as well).

The SDL library driver set is derived from yet another new driver
set named 'Pico'. 'Pico' is a base class for a driver that will
allow porting of FLTK with the tinyest amount of effort. This 
implementation of the SDL driver shall be documented very well
to explain the porting process.


git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3-porting@11262 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
  • Loading branch information
Matthias Melcher committed Mar 2, 2016
1 parent f14de40 commit 53859c5
Show file tree
Hide file tree
Showing 36 changed files with 399 additions and 14 deletions.
169 changes: 169 additions & 0 deletions CMake/FindSDL2.cmake
@@ -0,0 +1,169 @@

# This module defines
# SDL2_LIBRARY, the name of the library to link against
# SDL2_FOUND, if false, do not try to link to SDL2
# SDL2_INCLUDE_DIR, where to find SDL.h
#
# This module responds to the the flag:
# SDL2_BUILDING_LIBRARY
# If this is defined, then no SDL2main will be linked in because
# only applications need main().
# Otherwise, it is assumed you are building an application and this
# module will attempt to locate and set the the proper link flags
# as part of the returned SDL2_LIBRARY variable.
#
# Don't forget to include SDLmain.h and SDLmain.m your project for the
# OS X framework based version. (Other versions link to -lSDL2main which
# this module will try to find on your behalf.) Also for OS X, this
# module will automatically add the -framework Cocoa on your behalf.
#
#
# Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration
# and no SDL2_LIBRARY, it means CMake did not find your SDL2 library
# (SDL2.dll, libsdl2.so, SDL2.framework, etc).
# Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again.
# Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value
# as appropriate. These values are used to generate the final SDL2_LIBRARY
# variable, but when these values are unset, SDL2_LIBRARY does not get created.
#
#
# $SDL2DIR is an environment variable that would
# correspond to the ./configure --prefix=$SDL2DIR
# used in building SDL2.
# l.e.galup 9-20-02
#
# Modified by Eric Wing.
# Added code to assist with automated building by using environmental variables
# and providing a more controlled/consistent search behavior.
# Added new modifications to recognize OS X frameworks and
# additional Unix paths (FreeBSD, etc).
# Also corrected the header search path to follow "proper" SDL guidelines.
# Added a search for SDL2main which is needed by some platforms.
# Added a search for threads which is needed by some platforms.
# Added needed compile switches for MinGW.
#
# On OSX, this will prefer the Framework version (if found) over others.
# People will have to manually change the cache values of
# SDL2_LIBRARY to override this selection or set the CMake environment
# CMAKE_INCLUDE_PATH to modify the search paths.
#
# Note that the header path has changed from SDL2/SDL.h to just SDL.h
# This needed to change because "proper" SDL convention
# is #include "SDL.h", not <SDL2/SDL.h>. This is done for portability
# reasons because not all systems place things in SDL2/ (see FreeBSD).

#=============================================================================
# Copyright 2003-2009 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)

message("<FindSDL2.cmake>")

SET(SDL2_SEARCH_PATHS
~/Library/Frameworks
/Library/Frameworks
/usr/local
/usr
/sw # Fink
/opt/local # DarwinPorts
/opt/csw # Blastwave
/opt
${SDL2_PATH}
)

FIND_PATH(SDL2_INCLUDE_DIR SDL.h
HINTS
$ENV{SDL2DIR}
PATH_SUFFIXES include/SDL2 include
PATHS ${SDL2_SEARCH_PATHS}
)

FIND_LIBRARY(SDL2_LIBRARY_TEMP
NAMES SDL2
HINTS
$ENV{SDL2DIR}
PATH_SUFFIXES lib64 lib
PATHS ${SDL2_SEARCH_PATHS}
)

IF(NOT SDL2_BUILDING_LIBRARY)
IF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
# Non-OS X framework versions expect you to also dynamically link to
# SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms
# seem to provide SDL2main for compatibility even though they don't
# necessarily need it.
FIND_LIBRARY(SDL2MAIN_LIBRARY
NAMES SDL2main
HINTS
$ENV{SDL2DIR}
PATH_SUFFIXES lib64 lib
PATHS ${SDL2_SEARCH_PATHS}
)
ENDIF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
ENDIF(NOT SDL2_BUILDING_LIBRARY)

# SDL2 may require threads on your system.
# The Apple build may not need an explicit flag because one of the
# frameworks may already provide it.
# But for non-OSX systems, I will use the CMake Threads package.
IF(NOT APPLE)
FIND_PACKAGE(Threads)
ENDIF(NOT APPLE)

# MinGW needs an additional library, mwindows
# It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -lmwindows
# (Actually on second look, I think it only needs one of the m* libraries.)
IF(MINGW)
SET(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW")
ENDIF(MINGW)

IF(SDL2_LIBRARY_TEMP)
# For SDL2main
IF(NOT SDL2_BUILDING_LIBRARY)
IF(SDL2MAIN_LIBRARY)
SET(SDL2_LIBRARY_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY_TEMP})
ENDIF(SDL2MAIN_LIBRARY)
ENDIF(NOT SDL2_BUILDING_LIBRARY)

# For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa.
# CMake doesn't display the -framework Cocoa string in the UI even
# though it actually is there if I modify a pre-used variable.
# I think it has something to do with the CACHE STRING.
# So I use a temporary variable until the end so I can set the
# "real" variable in one-shot.
IF(APPLE)
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa")
ENDIF(APPLE)

# For threads, as mentioned Apple doesn't need this.
# In fact, there seems to be a problem if I used the Threads package
# and try using this line, so I'm just skipping it entirely for OS X.
IF(NOT APPLE)
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
ENDIF(NOT APPLE)

# For MinGW library
IF(MINGW)
SET(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
ENDIF(MINGW)

# Set the final string here so the GUI reflects the final state.
SET(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found")
# Set the temp variable to INTERNAL so it is not seen in the CMake GUI
SET(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "")
ENDIF(SDL2_LIBRARY_TEMP)

message("</FindSDL2.cmake>")

INCLUDE(FindPackageHandleStandardArgs)

FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR)

8 changes: 4 additions & 4 deletions CMake/macros.cmake
Expand Up @@ -119,7 +119,7 @@ macro(CREATE_EXAMPLE NAME SOURCES LIBRARIES)
FLTK_RUN_FLUID(FLUID_SOURCES "${flsrcs}")
endif(flsrcs)

if(APPLE AND NOT OPTION_APPLE_X11)
if(APPLE AND (NOT OPTION_APPLE_X11) AND (NOT OPTION_APPLE_SDL))
unset(RESOURCE_PATH)
if(${tname} STREQUAL "blocks" OR ${tname} STREQUAL "checkers" OR ${tname} STREQUAL "sudoku")
set( ICON_NAME ${tname}.icns )
Expand All @@ -138,7 +138,7 @@ macro(CREATE_EXAMPLE NAME SOURCES LIBRARIES)
endif(DEFINED RESOURCE_PATH)
else()
add_executable(${tname} WIN32 ${srcs} ${FLUID_SOURCES})
endif(APPLE AND NOT OPTION_APPLE_X11)
endif(APPLE AND (NOT OPTION_APPLE_X11) AND (NOT OPTION_APPLE_SDL))

set_target_properties(${tname}
PROPERTIES OUTPUT_NAME ${oname}
Expand All @@ -149,9 +149,9 @@ macro(CREATE_EXAMPLE NAME SOURCES LIBRARIES)
endif(NOT ${tname} STREQUAL "demo")
set_target_properties(${tname} PROPERTIES RESOURCE ${RESOURCE_PATH})
endif(APPLE AND DEFINED RESOURCE_PATH)
if(APPLE AND (NOT OPTION_APPLE_X11) AND ${tname} STREQUAL "editor")
if(APPLE AND (NOT OPTION_APPLE_X11) AND (NOT OPTION_APPLE_SDL) AND ${tname} STREQUAL "editor")
set_target_properties("editor" PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${PROJECT_SOURCE_DIR}/test/editor-Info.plist" )
endif(APPLE AND (NOT OPTION_APPLE_X11) AND ${tname} STREQUAL "editor")
endif(APPLE AND (NOT OPTION_APPLE_X11) AND (NOT OPTION_APPLE_SDL) AND ${tname} STREQUAL "editor")

target_link_libraries(${tname} ${LIBRARIES})

Expand Down
12 changes: 12 additions & 0 deletions CMake/options.cmake
Expand Up @@ -50,6 +50,7 @@ endif(UNIX)
#######################################################################
if(APPLE)
option(OPTION_APPLE_X11 "use X11" OFF)
option(OPTION_APPLE_SDL "use SDL" OFF)
endif(APPLE)

if((NOT APPLE OR OPTION_APPLE_X11) AND NOT WIN32)
Expand All @@ -63,6 +64,15 @@ if((NOT APPLE OR OPTION_APPLE_X11) AND NOT WIN32)
endif(X11_FOUND)
endif((NOT APPLE OR OPTION_APPLE_X11) AND NOT WIN32)

if (OPTION_APPLE_SDL)
find_package(SDL2 REQUIRED)
if (SDL2_FOUND)
set(USE_SDL 1)
set(FL_PORTING 1)
list(APPEND FLTK_LDLIBS SDL2_LIBRARIES)
endif(SDL2_FOUND)
endif(OPTION_APPLE_SDL)

#######################################################################
option(OPTION_USE_POLL "use poll if available" OFF)

Expand Down Expand Up @@ -134,6 +144,8 @@ if(OPTION_USE_GL)
set(OPENGL_FOUND TRUE)
get_filename_component(PATH_TO_XLIBS ${X11_X11_LIB} PATH)
set(OPENGL_LIBRARIES -L${PATH_TO_XLIBS} -lGLU -lGL)
elseif(OPTION_APPLE_SDL)
set(OPENGL_FOUND FALSE)
else()
include(FindOpenGL)
endif(OPTION_APPLE_X11)
Expand Down
3 changes: 3 additions & 0 deletions CMake/setup.cmake
Expand Up @@ -99,6 +99,9 @@ if(APPLE)
if(OPTION_APPLE_X11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -U__APPLE__")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -U__APPLE__")
elseif(OPTION_APPLE_SDL)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SDL2_INCLUDE_DIRS} -U__APPLE__ -DFL_PORTING")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SDL2_INCLUDE_DIRS} -U__APPLE__ -DFL_PORTING")
else()
set(__APPLE_QUARTZ__ 1)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Cocoa")
Expand Down
2 changes: 1 addition & 1 deletion FL/Fl.H
Expand Up @@ -205,7 +205,7 @@ public: // should be private!
#elif defined(WIN32)
// not needed in WIN32
#elif defined(FL_PORTING)
# pragma message "FL_PORTING: define a type for FL_SOCKET"
# pragma message "FL_PORTING: add these functions to all platforms?"
// no default implementation
#else
// not needed in X11
Expand Down
1 change: 1 addition & 0 deletions FL/fl_utf8.h
Expand Up @@ -63,6 +63,7 @@
#elif defined(FL_PORTING)
# pragma message "FL_PORTING: include utf8 support files and define utf8 types"
# define xchar unsigned short
// TODO: the condition below is not portable!
#else /* X11 */
# include <sys/types.h>
# include <sys/stat.h>
Expand Down
94 changes: 93 additions & 1 deletion FL/porting.H
Expand Up @@ -78,7 +78,7 @@ public:
int wait_for_expose;
// NSCursor *cursor;
static Fl_X* first;
static Fl_X* i(const Fl_Window* w) {return w->i;}
static Fl_X* i(const Fl_Window* w) {return (Fl_X*)w->i;}
static int fake_X_wm(const Fl_Window*,int&,int&,int&,int&,int&);
static Fl_X* make(Fl_Window*);
void flush();
Expand Down Expand Up @@ -155,6 +155,98 @@ extern void fl_end_offscreen();
extern int fl_parse_color(const char* p, uchar& r, uchar& g, uchar& b);
extern void fl_open_display();

/*
The following symbols are not found if we naively compile the core modules and
no specific platform implementations. This list is a hint at all the functions
and methods that probably need to be refactored into the driver system.
Undefined symbols for architecture x86_64:
"fl_set_spot(int, int, int, int, int, int, Fl_Window*)", referenced from:
Fl_Input_::drawtext(int, int, int, int) in libfltkd.a(Fl_Input_.o)
Fl_Input_::handletext(int, int, int, int, int) in libfltkd.a(Fl_Input_.o)
"fl_reset_spot()", referenced from:
Fl_Input_::handletext(int, int, int, int, int) in libfltkd.a(Fl_Input_.o)
"fl_filename_name(char const*)", referenced from:
Fl_Window::show(int, char**) in libfltkd.a(Fl_arg.o)
"fl_clipboard_notify_change()", referenced from:
Fl::add_clipboard_notify(void (*)(int, void*), void*) in libfltkd.a(Fl.o)
Fl::remove_clipboard_notify(void (*)(int, void*)) in libfltkd.a(Fl.o)
"Fl_Screen_Driver::newScreenDriver()", referenced from:
Fl::screen_driver() in libfltkd.a(Fl.o)
"Fl_Graphics_Driver::newMainGraphicsDriver()", referenced from:
Fl_Display_Device::display_device() in libfltkd.a(Fl_Device.o)
"Fl_Graphics_Driver::global_gc()", referenced from:
Fl_Surface_Device::set_current() in libfltkd.a(Fl_Device.o)
"Fl::dnd()", referenced from:
Fl_Input::handle(int) in libfltkd.a(Fl_Input.o)
"Fl::copy(char const*, int, int, char const*)", referenced from:
Fl::selection(Fl_Widget&, char const*, int) in libfltkd.a(Fl.o)
Fl_Input_::copy(int) in libfltkd.a(Fl_Input_.o)
Fl_Input_::copy_cuts() in libfltkd.a(Fl_Input_.o)
"Fl::paste(Fl_Widget&, int, char const*)", referenced from:
Fl::paste(Fl_Widget&) in libfltkd.a(Fl.o)
Fl_Input::kf_paste() in libfltkd.a(Fl_Input.o)
Fl_Input::handle(int) in libfltkd.a(Fl_Input.o)
"Fl::get_mouse(int&, int&)", referenced from:
Fl_Screen_Driver::screen_xywh(int&, int&, int&, int&) in libfltkd.a(Fl_Screen_Driver.o)
Fl_Screen_Driver::screen_work_area(int&, int&, int&, int&) in libfltkd.a(Fl_Screen_Driver.o)
Fl_Window::hotspot(int, int, int) in libfltkd.a(Fl_Window_hotspot.o)
"Fl::set_color(unsigned int, unsigned int)", referenced from:
Fl::set_color(unsigned int, unsigned char, unsigned char, unsigned char) in libfltkd.a(fl_color.o)
Fl::background2(unsigned char, unsigned char, unsigned char) in libfltkd.a(Fl_get_system_colors.o)
"Fl_X::set_cursor(Fl_Cursor)", referenced from:
Fl_Window::cursor(Fl_Cursor) in libfltkd.a(fl_cursor.o)
"Fl_X::set_cursor(Fl_RGB_Image const*, int, int)", referenced from:
Fl_Window::cursor(Fl_RGB_Image const*, int, int) in libfltkd.a(fl_cursor.o)
"Fl_X::set_default_icons(Fl_RGB_Image const**, int)", referenced from:
Fl_Window::default_icons(Fl_RGB_Image const**, int) in libfltkd.a(Fl_Window.o)
"Fl_X::flush()", referenced from:
Fl::flush() in libfltkd.a(Fl.o)
"Fl_X::set_icons()", referenced from:
Fl_Window::icons(Fl_RGB_Image const**, int) in libfltkd.a(Fl_Window.o)
"Fl_Window::size_range_()", referenced from:
Fl_Window::size_range(int, int, int, int, int, int, int) in libfltkd.a(fl_ask.o)
"Fl_Window::fullscreen_x()", referenced from:
Fl_Window::fullscreen() in libfltkd.a(Fl_Window_fullscreen.o)
Fl_Window::fullscreen_screens(int, int, int, int) in libfltkd.a(Fl_Window_fullscreen.o)
"Fl_Window::make_current()", referenced from:
Fl_Window::flush() in libfltkd.a(Fl.o)
Fl_Double_Window::flush(int) in libfltkd.a(Fl_Double_Window.o)
traverse_to_gl_subwindows(Fl_Group*, unsigned char*, int, int, int, int, int, Fl_RGB_Image*) in libfltkd.a(fl_read_image.o)
Fl_Widget_Surface::print_window_part(Fl_Window*, int, int, int, int, int, int) in libfltkd.a(Fl_Widget_Surface.o)
"Fl_Window::fullscreen_off_x(int, int, int, int)", referenced from:
Fl_Window::fullscreen_off(int, int, int, int) in libfltkd.a(Fl_Window_fullscreen.o)
"Fl_Window::show()", referenced from:
vtable for Fl_Window in libfltkd.a(Fl_Window_shape.o)
Fl_Double_Window::show() in libfltkd.a(Fl_Double_Window.o)
Fl_Single_Window::show() in libfltkd.a(Fl_Single_Window.o)
"Fl_Window::label(char const*, char const*)", referenced from:
Fl_Window::label(char const*) in libfltkd.a(Fl_Window.o)
Fl_Window::copy_label(char const*) in libfltkd.a(Fl_Window.o)
Fl_Window::iconlabel(char const*) in libfltkd.a(Fl_Window.o)
"Fl_Window::resize(int, int, int, int)", referenced from:
vtable for Fl_TooltipBox in libfltkd.a(Fl_Tooltip.o)
vtable for Fl_Window in libfltkd.a(Fl_Window_shape.o)
vtable for Fl_Menu_Window in libfltkd.a(Fl_Menu_Window.o)
Fl_Double_Window::resize(int, int, int, int) in libfltkd.a(Fl_Double_Window.o)
vtable for menutitle in libfltkd.a(Fl_Menu.o)
vtable for menuwindow in libfltkd.a(Fl_Menu.o)
vtable for Fl_Single_Window in libfltkd.a(Fl_Single_Window.o)
...
"Fl_Window::current_", referenced from:
Fl_Window::current() in libfltkd.a(Fl_Window.o)
"vtable for Fl_Image_Surface", referenced from:
Fl_Image_Surface::Fl_Image_Surface(int, int, int) in libfltkd.a(Fl_Image_Surface.o)
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"_fl_show_iconic", referenced from:
Fl::arg(int, char**, int&) in libfltkd.a(Fl_arg.o)
"_fl_window", referenced from:
fl_read_image(unsigned char*, int, int, int, int, int) in libfltkd.a(fl_read_image.o)
Fl_Image_Surface::set_current() in libfltkd.a(Fl_Image_Surface.o)
Fl_Image_Surface::end_current() in libfltkd.a(Fl_Image_Surface.o)
*/

//
// End of "$Id$".
//
Expand Down
19 changes: 19 additions & 0 deletions FL/x.H
Expand Up @@ -33,6 +33,25 @@
# include "mac.H"
# elif defined(ANDROID)
# pragma message "A clean port requires a driver-style system for Fl_X"
# elif defined(USE_SDL)
# pragma message "FL_PORTING: write a header file based on this file, win32.H, or mac.H to define the FLTK core internals"

//typedef void *Window;
//typedef void *Fl_Region;
//typedef void *Fl_Bitmask;
//typedef void *Fl_Offscreen;
//#include <FL/Fl_Window.H>
//class FL_EXPORT Fl_X {
//public:
// Window xid;
// Window other_xid;
// Fl_X *next;
// char wait_for_expose;
// static Fl_X* first;
// static Fl_X* i(const Fl_Window* wi) {return (Fl_X*)wi->i;}
//};

# include "porting.H"
# elif defined(FL_PORTING)
# pragma message "FL_PORTING: write a header file based on this file, win32.H, or mac.H to define the FLTK core internals"
# include "porting.H"
Expand Down

0 comments on commit 53859c5

Please sign in to comment.