diff options
| author | Fabien Costantini <fabien@onepost.net> | 2014-05-21 04:10:12 +0000 |
|---|---|---|
| committer | Fabien Costantini <fabien@onepost.net> | 2014-05-21 04:10:12 +0000 |
| commit | 54b0123eab05295c4f510692bef48c0ac7618607 (patch) | |
| tree | bacc68bc8db89fba29363b8e8d0e8bfc57cea4ba | |
| parent | 2c820fd53dad8a753721a9a4ff5d8daced7345ee (diff) | |
Added a new portable sleep API(ABI compatible). Now features a flexible multi-usage sleep() api accepting decimals, msleep() for millisecs only, usleep() for microsecs. Updated Makefiles, cmakefiles, vs2008, vs2010 with the new Fl_sleep add-on. Documented new API. still needs to be added in Xcode.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@10150 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
| -rw-r--r-- | FL/Fl.H | 10 | ||||
| -rw-r--r-- | ide/VisualC2008/fltk.lib.vcproj | 46 | ||||
| -rw-r--r-- | ide/VisualC2008/fltkdll.vcproj | 42 | ||||
| -rw-r--r-- | ide/VisualC2010/fltk.lib.vcxproj | 3 | ||||
| -rw-r--r-- | ide/VisualC2010/fltk.lib.vcxproj.filters | 3 | ||||
| -rw-r--r-- | src/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/Fl_sleep.cxx | 68 | ||||
| -rw-r--r-- | src/Makefile | 5 | ||||
| -rw-r--r-- | test/sudoku.cxx | 13 |
9 files changed, 181 insertions, 10 deletions
@@ -1075,6 +1075,16 @@ public: static void awake(void* message = 0); /** See void awake(void* message=0). */ static int awake(Fl_Awake_Handler cb, void* message = 0); + + // Cross platform sleep API + + /** flexible sleep function in seconds, accepts decimals typically up to 100usec resolution (depends on your hardware) */ + static void sleep(double seconds); + /** milli seconds sleep */ + static void msleep(unsigned long milliseconds); + /** microseconds sleep */ + static void usleep(unsigned long long microseconds); + /** The thread_message() method returns the last message that was sent from a child by the awake() method. diff --git a/ide/VisualC2008/fltk.lib.vcproj b/ide/VisualC2008/fltk.lib.vcproj index dc8112aec..a33314ebc 100644 --- a/ide/VisualC2008/fltk.lib.vcproj +++ b/ide/VisualC2008/fltk.lib.vcproj @@ -3661,6 +3661,52 @@ </FileConfiguration>
</File>
<File
+ RelativePath="..\..\src\Fl_sleep.cxx"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories=""
+ PreprocessorDefinitions=""
+ BrowseInformation="1"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ FavorSizeOrSpeed="0"
+ AdditionalIncludeDirectories=""
+ PreprocessorDefinitions=""
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Debug Cairo|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories=""
+ PreprocessorDefinitions=""
+ BrowseInformation="1"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release Cairo|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ FavorSizeOrSpeed="0"
+ AdditionalIncludeDirectories=""
+ PreprocessorDefinitions=""
+ />
+ </FileConfiguration>
+ </File>
+ <File
RelativePath="..\..\src\Fl_Menu.cxx"
>
<FileConfiguration
diff --git a/ide/VisualC2008/fltkdll.vcproj b/ide/VisualC2008/fltkdll.vcproj index 8f06b2e32..a77b1869c 100644 --- a/ide/VisualC2008/fltkdll.vcproj +++ b/ide/VisualC2008/fltkdll.vcproj @@ -3392,6 +3392,48 @@ PreprocessorDefinitions="_CRT_SECURE_NO_DEPRECATE;FL_DLL;FL_LIBRARY;WIN32;NDEBUG;_WINDOWS;WIN32_LEAN_AND_MEAN;VC_EXTRA_LEAN;WIN32_EXTRA_LEAN;$(NoInherit)"
/>
</FileConfiguration>
+ </File>
+ <File
+ RelativePath="..\..\src\Fl_sleep.cxx"
+ >
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ AdditionalIncludeDirectories=""
+ PreprocessorDefinitions="_CRT_SECURE_NO_DEPRECATE;FL_DLL;FL_LIBRARY;WIN32;NDEBUG;_WINDOWS;WIN32_LEAN_AND_MEAN;VC_EXTRA_LEAN;WIN32_EXTRA_LEAN;$(NoInherit)"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories=""
+ PreprocessorDefinitions="_CRT_SECURE_NO_DEPRECATE;FL_DLL;FL_LIBRARY;WIN32;_DEBUG;_WINDOWS;WIN32_LEAN_AND_MEAN;VC_EXTRA_LEAN;WIN32_EXTRA_LEAN;$(NoInherit)"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Debug Cairo|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories=""
+ PreprocessorDefinitions="_CRT_SECURE_NO_DEPRECATE;FL_DLL;FL_LIBRARY;WIN32;_DEBUG;_WINDOWS;WIN32_LEAN_AND_MEAN;VC_EXTRA_LEAN;WIN32_EXTRA_LEAN;$(NoInherit)"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release Cairo|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ AdditionalIncludeDirectories=""
+ PreprocessorDefinitions="_CRT_SECURE_NO_DEPRECATE;FL_DLL;FL_LIBRARY;WIN32;NDEBUG;_WINDOWS;WIN32_LEAN_AND_MEAN;VC_EXTRA_LEAN;WIN32_EXTRA_LEAN;$(NoInherit)"
+ />
+ </FileConfiguration>
</File>
<File
RelativePath="..\..\src\Fl_Menu.cxx"
diff --git a/ide/VisualC2010/fltk.lib.vcxproj b/ide/VisualC2010/fltk.lib.vcxproj index 00f237fa4..3cbd80e3e 100644 --- a/ide/VisualC2010/fltk.lib.vcxproj +++ b/ide/VisualC2010/fltk.lib.vcxproj @@ -291,6 +291,7 @@ <ClInclude Include="..\..\FL\Fl_XPM_Image.H" />
</ItemGroup>
<ItemGroup>
+ <ClCompile Include="..\..\src\Fl_sleep.cxx" />
<ClCompile Include="..\..\src\xutf8\case.c" />
<ClCompile Include="..\..\src\filename_absolute.cxx">
<Optimization Condition="'$(Configuration)|$(Platform)'=='Debug Cairo|Win32'">Disabled</Optimization>
@@ -2453,4 +2454,4 @@ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
-</Project>
+</Project>
\ No newline at end of file diff --git a/ide/VisualC2010/fltk.lib.vcxproj.filters b/ide/VisualC2010/fltk.lib.vcxproj.filters index fd4bab66c..8c5db338f 100644 --- a/ide/VisualC2010/fltk.lib.vcxproj.filters +++ b/ide/VisualC2010/fltk.lib.vcxproj.filters @@ -517,5 +517,6 @@ <ClCompile Include="..\..\src\scandir.c" />
<ClCompile Include="..\..\src\screen_xywh.cxx" />
<ClCompile Include="..\..\src\vsnprintf.c" />
+ <ClCompile Include="..\..\src\Fl_sleep.cxx" />
</ItemGroup>
-</Project>
+</Project>
\ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 778a1d6ea..9da98d6e2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -90,6 +90,7 @@ set(CPPFILES Fl_get_system_colors.cxx Fl_grab.cxx Fl_lock.cxx + Fl_sleep.cxx Fl_own_colormap.cxx Fl_visual.cxx Fl_x.cxx diff --git a/src/Fl_sleep.cxx b/src/Fl_sleep.cxx new file mode 100644 index 000000000..43fbec46c --- /dev/null +++ b/src/Fl_sleep.cxx @@ -0,0 +1,68 @@ +// +// "$Id: Fl_sleep.cxx $" +// +// Multi-threading support code for the Fast Light Tool Kit (FLTK). +// +// Copyright 1998-2010 by Bill Spitzak and others. +// +// This library is free software. Distribution and use rights are outlined in +// the file "COPYING" which should have been included with this file. If this +// file is missing or damaged, see the license at: +// +// http://www.fltk.org/COPYING.php +// +// Please report all bugs and problems on the following page: +// +// http://www.fltk.org/str.php +// + +// Cross platform sleep API for FLTK, F. Costantini, May 20th, 2014 + +#include <FL/Fl.H> +#include <config.h> + +#include <stdlib.h> + +#ifdef WIN32 +#include <windows.h> +#else +#include <unistd.h> +#endif + +/** Make the current thread sleep for n seconds, support decimals ... */ +void Fl::sleep(double seconds) +{ + Fl::usleep((unsigned long long) (seconds*1000000)); +} + +/** Make the current thread to sleep for n milliseconds */ +void Fl::msleep(unsigned long milliseconds) +{ +#ifdef WIN32 + ::Sleep( (DWORD) milliseconds); +#else + ::usleep((useconds_t) (milliseconds*1000)); +#endif +} + +/** Make the current thread to sleep for n microseconds */ +void Fl::usleep(unsigned long long microseconds) +// unsigned long long more should be more portable than int64_t before c++ 2011 ... +{ +#ifdef WIN32 + HANDLE timer; + LARGE_INTEGER reltime; + + reltime.QuadPart = (LONGLONG) -(10*microseconds); // Convert to 100 nanosecond relative time interval + timer = CreateWaitableTimer(NULL, TRUE, NULL); + SetWaitableTimer(timer, &reltime, 0, NULL, NULL, 0); + WaitForSingleObject(timer, INFINITE); + CloseHandle(timer); +#else + ::usleep((useconds_t) microseconds); +#endif +} + +// +// End of "$Id: $". +// diff --git a/src/Makefile b/src/Makefile index 5df7ea124..2c236f47a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -106,6 +106,7 @@ CPPFILES = \ Fl_get_system_colors.cxx \ Fl_grab.cxx \ Fl_lock.cxx \ + Fl_sleep.cxx \ Fl_own_colormap.cxx \ Fl_visual.cxx \ Fl_x.cxx \ @@ -157,13 +158,13 @@ CPPFILES = \ screen_xywh.cxx \ fl_utf8.cxx \ ps_image.cxx - + OBJCPPFILES = \ Fl_cocoa.mm \ Fl_Quartz_Printer.mm \ Fl_Native_File_Chooser_MAC.mm \ Fl_Sys_Menu_Bar.mm - + FLCPPFILES = \ forms_compatability.cxx \ forms_bitmap.cxx \ diff --git a/test/sudoku.cxx b/test/sudoku.cxx index 27677b3e9..6eadbcb6a 100644 --- a/test/sudoku.cxx +++ b/test/sudoku.cxx @@ -418,18 +418,19 @@ SudokuSound::audio_cb(AudioDeviceID device, } #endif // __APPLE__ +#define NOTE_DURATION_MS 50 // Play a note for 250ms... void SudokuSound::play(char note) { Fl::check(); - + #ifdef __APPLE__ // Point to the next note... data = sample_data[note - 'A']; remaining = sample_size * 2; // Wait for the sound to complete... - usleep(50000); + Fl::msleep(NOTE_DURATION_MS); #elif defined(WIN32) if (sample_size) { @@ -437,8 +438,8 @@ void SudokuSound::play(char note) { waveOutWrite(device, header_ptr, sizeof(WAVEHDR)); - Sleep(50); - } else Beep(frequencies[note - 'A'], 50); + Fl::msleep(NOTE_DURATION_MS); + } else Beep(frequencies[note - 'A'], NOTE_DURATION_MS); #else # ifdef HAVE_ALSA_ASOUNDLIB_H @@ -448,7 +449,7 @@ void SudokuSound::play(char note) { snd_pcm_prepare(handle); snd_pcm_writei(handle, sample_data[note - 'A'], sample_size); } - usleep(50000); + Fl::msleep(NOTE_DURATION_MS); return; } # endif // HAVE_ALSA_ASOUNDLIB_H @@ -463,7 +464,7 @@ void SudokuSound::play(char note) { // Sound a tone for the given note... control.bell_percent = 100; control.bell_pitch = frequencies[note - 'A']; - control.bell_duration = 50; + control.bell_duration = NOTE_DURATION_MS; XChangeKeyboardControl(fl_display, KBBellPercent | KBBellPitch | KBBellDuration, |
