2015-07-25 21:11:11 +00:00
|
|
|
//--------------------------------------------------------------------------//
|
|
|
|
/* Copyright (c) 2015 Carsten Larsen
|
2015-03-07 22:29:12 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2015-07-25 21:11:11 +00:00
|
|
|
//--------------------------------------------------------------------------//
|
2015-04-18 20:08:39 +00:00
|
|
|
#include "ntimed_platform.h"
|
2015-04-30 22:44:54 +00:00
|
|
|
#include "ntimed.h"
|
2015-07-25 21:11:11 +00:00
|
|
|
//--------------------------------------------------------------------------//
|
2015-04-30 22:44:54 +00:00
|
|
|
#ifdef Debug
|
|
|
|
#undef Debug
|
|
|
|
#endif
|
2015-07-25 21:11:11 +00:00
|
|
|
//--------------------------------------------------------------------------//
|
2015-03-07 22:29:12 +00:00
|
|
|
#include <exec/io.h>
|
2015-03-17 18:28:07 +00:00
|
|
|
#include <exec/types.h>
|
2015-03-17 23:37:25 +00:00
|
|
|
#include <exec/memory.h>
|
2015-04-30 22:44:54 +00:00
|
|
|
#include <exec/devices.h>
|
2015-03-17 18:28:07 +00:00
|
|
|
#include <devices/timer.h>
|
|
|
|
#include <proto/timer.h>
|
2015-03-17 23:37:25 +00:00
|
|
|
#include <proto/battclock.h>
|
|
|
|
#include <clib/alib_protos.h>
|
|
|
|
#include <clib/exec_protos.h>
|
2015-04-30 22:44:54 +00:00
|
|
|
#include <clib/timer_protos.h>
|
2015-04-18 20:08:39 +00:00
|
|
|
#include <clib/locale_protos.h>
|
2015-03-17 23:37:25 +00:00
|
|
|
#include <clib/utility_protos.h>
|
|
|
|
#include <clib/battclock_protos.h>
|
2015-07-25 21:11:11 +00:00
|
|
|
// -------------------------------------------------------------------------- //
|
|
|
|
#ifdef AOS3
|
|
|
|
# include "clib/timezone_protos.h"
|
|
|
|
# include "inline/timezone.h"
|
|
|
|
#endif
|
2015-07-26 08:45:18 +00:00
|
|
|
#ifdef AROS
|
|
|
|
# include <time.h>
|
|
|
|
#endif
|
2015-07-25 21:11:11 +00:00
|
|
|
// -------------------------------------------------------------------------- //
|
|
|
|
// 2922 is the number of days between 1.1.1970 and 1.1.1978
|
|
|
|
// (2 leap years and 6 normal)
|
|
|
|
// 2922 * 24 * 60 * 60 = 252460800
|
|
|
|
// -------------------------------------------------------------------------- //
|
|
|
|
#define AMIGAOFFSET 252460800
|
|
|
|
//--------------------------------------------------------------------------//
|
2015-04-18 20:08:39 +00:00
|
|
|
int create_timer();
|
|
|
|
int delete_timer();
|
|
|
|
void adjust_timeval(struct timeval *tv, double offset);
|
2015-07-25 21:11:11 +00:00
|
|
|
//--------------------------------------------------------------------------//
|
2015-04-18 20:08:39 +00:00
|
|
|
struct Library *BattClockBase;
|
2015-07-25 21:11:11 +00:00
|
|
|
struct Library *DOSBase = NULL;
|
|
|
|
struct Library *UtilityBase = NULL;
|
|
|
|
struct Library *LocaleBase = NULL;
|
|
|
|
struct Library *SocketBase = NULL;
|
|
|
|
struct Library *TimezoneBase = NULL;
|
|
|
|
struct Locale *locale = NULL;
|
|
|
|
struct Device *TimerBase = NULL;
|
|
|
|
struct timerequest *request = NULL;
|
2015-03-17 10:57:13 +00:00
|
|
|
struct timeval sync_time;
|
2015-07-25 21:11:11 +00:00
|
|
|
char *zone_message = NULL;
|
2015-03-17 23:37:25 +00:00
|
|
|
int validtime = 0;
|
2015-03-17 10:57:13 +00:00
|
|
|
int limited_sync = 0;
|
2015-04-30 22:44:54 +00:00
|
|
|
long offset = 0;
|
2015-07-25 21:11:11 +00:00
|
|
|
//--------------------------------------------------------------------------//
|
2015-07-10 21:00:05 +00:00
|
|
|
#ifdef AOS3
|
|
|
|
int errno;
|
|
|
|
int h_errno;
|
|
|
|
#endif
|
2015-07-25 21:11:11 +00:00
|
|
|
//--------------------------------------------------------------------------//
|
2015-07-10 21:00:05 +00:00
|
|
|
void amiga_open_error(char *name)
|
|
|
|
{
|
2015-07-25 21:11:11 +00:00
|
|
|
Put(NULL, OCX_DIAG, OPEN_ERROR, name);
|
2015-07-10 21:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void amiga_open_lib_error(char *name, int version)
|
|
|
|
{
|
|
|
|
Put(NULL, OCX_DIAG, OPEN_VER_ERROR, name, version);
|
|
|
|
}
|
2015-07-25 21:11:11 +00:00
|
|
|
//--------------------------------------------------------------------------//
|
2015-07-10 21:00:05 +00:00
|
|
|
int amiga_open_libs()
|
2015-03-07 22:29:12 +00:00
|
|
|
{
|
2015-07-10 21:00:05 +00:00
|
|
|
if(!(DOSBase = OpenLibrary((STRPTR)DOSLIB_NAME, DOSLIB_REV))) {
|
|
|
|
amiga_open_lib_error(DOSLIB_NAME, DOSLIB_REV);
|
|
|
|
return 5;
|
2015-04-18 20:08:39 +00:00
|
|
|
}
|
2015-03-07 22:29:12 +00:00
|
|
|
|
2015-07-10 21:00:05 +00:00
|
|
|
if(!(LocaleBase = OpenLibrary((STRPTR)LOCALELIB_NAME, LOCALELIB_REV))) {
|
|
|
|
amiga_open_lib_error(LOCALELIB_NAME, LOCALELIB_REV);
|
|
|
|
return 5;
|
2015-04-30 22:44:54 +00:00
|
|
|
}
|
2015-03-07 22:29:12 +00:00
|
|
|
|
2015-04-30 22:44:54 +00:00
|
|
|
if(!(locale = OpenLocale(NULL))) {
|
2015-07-10 21:00:05 +00:00
|
|
|
amiga_open_error("Locale");
|
|
|
|
return 10;
|
2015-04-30 22:44:54 +00:00
|
|
|
}
|
2015-03-07 22:29:12 +00:00
|
|
|
|
2015-07-10 21:00:05 +00:00
|
|
|
if(!(UtilityBase = OpenLibrary((STRPTR)UTILLIB_NAME, UTILLIB_REV))) {
|
|
|
|
amiga_open_lib_error(UTILLIB_NAME, UTILLIB_REV);
|
|
|
|
return 5;
|
2015-04-18 20:08:39 +00:00
|
|
|
}
|
2015-03-07 22:29:12 +00:00
|
|
|
|
2015-07-10 21:00:05 +00:00
|
|
|
if(!(BattClockBase = OpenResource((STRPTR)BATTCLOCK_NAME))) {
|
|
|
|
amiga_open_error(BATTCLOCK_NAME);
|
|
|
|
return 10;
|
|
|
|
}
|
2015-03-07 22:29:12 +00:00
|
|
|
|
2015-07-10 21:00:05 +00:00
|
|
|
if(!(SocketBase = OpenLibrary((STRPTR)BSDLIB_NAME, BSDLIB_REV))) {
|
|
|
|
amiga_open_lib_error(BSDLIB_NAME, BSDLIB_REV);
|
|
|
|
return 5;
|
2015-04-18 20:08:39 +00:00
|
|
|
}
|
2015-03-07 22:29:12 +00:00
|
|
|
|
2015-07-10 21:00:05 +00:00
|
|
|
if(SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), (int)&errno,
|
|
|
|
SBTM_SETVAL(SBTC_HERRNOLONGPTR), (int)&h_errno, TAG_DONE)) {
|
|
|
|
amiga_open_error(BSDLIB_NAME);
|
|
|
|
return 10;
|
2015-04-18 20:08:39 +00:00
|
|
|
}
|
2015-03-07 22:29:12 +00:00
|
|
|
|
2015-04-18 20:08:39 +00:00
|
|
|
if (create_timer() != 0) {
|
2015-07-10 21:00:05 +00:00
|
|
|
amiga_open_error(TIMER_NAME);
|
|
|
|
return 10;
|
2015-04-18 20:08:39 +00:00
|
|
|
}
|
2015-04-30 22:44:54 +00:00
|
|
|
|
2015-07-25 21:11:11 +00:00
|
|
|
TimezoneBase = OpenLibrary((STRPTR)TIMEZONELIB_NAME, TIMEZONELIB_REV);
|
|
|
|
|
2015-07-10 21:00:05 +00:00
|
|
|
return 0;
|
2015-03-07 22:29:12 +00:00
|
|
|
}
|
2015-07-25 21:11:11 +00:00
|
|
|
//--------------------------------------------------------------------------//
|
2015-04-18 20:08:39 +00:00
|
|
|
void amiga_close_libs()
|
2015-03-07 22:29:12 +00:00
|
|
|
{
|
2015-07-25 21:11:11 +00:00
|
|
|
if (TimezoneBase != NULL) {
|
|
|
|
CloseLibrary(TimezoneBase);
|
|
|
|
}
|
|
|
|
|
2015-04-18 20:08:39 +00:00
|
|
|
if (request != NULL) {
|
|
|
|
delete_timer();
|
2015-03-17 10:57:13 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 20:08:39 +00:00
|
|
|
if (locale != NULL) {
|
|
|
|
CloseLocale(locale);
|
|
|
|
locale = NULL;
|
2015-03-12 21:48:56 +00:00
|
|
|
}
|
2015-03-13 14:57:55 +00:00
|
|
|
|
2015-04-18 20:08:39 +00:00
|
|
|
if (LocaleBase != NULL) {
|
|
|
|
CloseLibrary(LocaleBase);
|
|
|
|
LocaleBase = NULL;
|
|
|
|
}
|
2015-03-07 22:29:12 +00:00
|
|
|
|
2015-04-18 20:08:39 +00:00
|
|
|
if (UtilityBase != NULL) {
|
|
|
|
CloseLibrary(UtilityBase);
|
|
|
|
UtilityBase = NULL;
|
|
|
|
}
|
2015-03-07 22:29:12 +00:00
|
|
|
|
2015-04-30 22:44:54 +00:00
|
|
|
if (DOSBase != NULL) {
|
|
|
|
CloseLibrary(DOSBase);
|
|
|
|
DOSBase = NULL;
|
|
|
|
}
|
|
|
|
|
2015-04-18 20:08:39 +00:00
|
|
|
if (SocketBase != NULL) {
|
|
|
|
CloseLibrary(SocketBase);
|
|
|
|
SocketBase = NULL;
|
|
|
|
}
|
2015-03-07 22:29:12 +00:00
|
|
|
|
2015-07-25 21:11:11 +00:00
|
|
|
if (zone_message != NULL) {
|
|
|
|
freemem(zone_message);
|
|
|
|
zone_message = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//--------------------------------------------------------------------------//
|
|
|
|
void amiga_init_offset()
|
2015-07-10 21:00:05 +00:00
|
|
|
{
|
2015-07-25 21:11:11 +00:00
|
|
|
time_t now;
|
2015-07-10 21:00:05 +00:00
|
|
|
struct tm tm;
|
2015-07-25 21:11:11 +00:00
|
|
|
int gmtoffset;
|
|
|
|
char *abbr;
|
2015-07-10 21:00:05 +00:00
|
|
|
|
2015-07-25 21:11:11 +00:00
|
|
|
if (TimezoneBase) {
|
|
|
|
now = time(NULL);
|
|
|
|
localtime_r(&now, &tm);
|
2015-07-10 21:00:05 +00:00
|
|
|
gmtoffset = tm.tm_gmtoff;
|
2015-07-26 08:45:18 +00:00
|
|
|
abbr = (char*)tm.tm_zone;
|
2015-07-10 21:00:05 +00:00
|
|
|
} else {
|
2015-07-26 08:45:18 +00:00
|
|
|
gmtoffset = locale->loc_GMTOffset * 60;
|
2015-07-25 21:11:11 +00:00
|
|
|
abbr = '\0';
|
2015-07-10 21:00:05 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 21:11:11 +00:00
|
|
|
if (!zone_message) {
|
|
|
|
zone_message = allocmem(40);
|
|
|
|
snprintf(zone_message, 40,
|
|
|
|
"Time zone is GMT %s %.f%s%s%s",
|
|
|
|
(gmtoffset >= 0.0 ? "+" : "-"),
|
|
|
|
fabs((float)gmtoffset / 60.0 / 60.0),
|
2015-07-26 08:45:18 +00:00
|
|
|
(abbr != '\0' ? " (" : ""),
|
|
|
|
(abbr != '\0' ? abbr : ""),
|
|
|
|
(abbr != '\0' ? ")" : "")
|
2015-07-25 21:11:11 +00:00
|
|
|
);
|
|
|
|
}
|
2015-07-10 21:00:05 +00:00
|
|
|
|
2015-07-25 21:11:11 +00:00
|
|
|
offset = -gmtoffset + AMIGAOFFSET;
|
2015-07-10 21:00:05 +00:00
|
|
|
}
|
2015-07-25 21:11:11 +00:00
|
|
|
//--------------------------------------------------------------------------//
|
2015-04-18 20:08:39 +00:00
|
|
|
int create_timer()
|
2015-03-07 22:29:12 +00:00
|
|
|
{
|
2015-03-08 21:47:50 +00:00
|
|
|
LONG error;
|
2015-03-07 22:29:12 +00:00
|
|
|
struct MsgPort *port = CreatePort(0, 0);
|
|
|
|
if (port == NULL)
|
2015-04-18 20:08:39 +00:00
|
|
|
return 1;
|
2015-03-07 22:29:12 +00:00
|
|
|
|
2015-03-08 21:47:50 +00:00
|
|
|
request = (struct timerequest*)CreateExtIO(port, sizeof(struct timerequest));
|
2015-03-07 22:29:12 +00:00
|
|
|
if (request == NULL)
|
|
|
|
{
|
|
|
|
DeletePort(port);
|
2015-04-18 20:08:39 +00:00
|
|
|
return 1;
|
2015-03-07 22:29:12 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 21:00:05 +00:00
|
|
|
error = OpenDevice(
|
|
|
|
(STRPTR)TIMER_NAME, UNIT_MICROHZ,
|
|
|
|
(struct IORequest*)request, 0L
|
|
|
|
);
|
|
|
|
|
2015-03-07 22:29:12 +00:00
|
|
|
if (error != 0)
|
|
|
|
{
|
|
|
|
delete_timer(request);
|
2015-04-18 20:08:39 +00:00
|
|
|
return 1;
|
2015-03-07 22:29:12 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 22:44:54 +00:00
|
|
|
TimerBase = (struct Device*)request->tr_node.io_Device;
|
|
|
|
|
2015-04-18 20:08:39 +00:00
|
|
|
return 0;
|
2015-03-07 22:29:12 +00:00
|
|
|
}
|
2015-07-25 21:11:11 +00:00
|
|
|
//--------------------------------------------------------------------------//
|
2015-04-18 20:08:39 +00:00
|
|
|
int delete_timer()
|
2015-03-07 22:29:12 +00:00
|
|
|
{
|
2015-03-08 21:47:50 +00:00
|
|
|
struct MsgPort *port;
|
2015-03-07 22:29:12 +00:00
|
|
|
if (request == NULL)
|
2015-04-18 20:08:39 +00:00
|
|
|
return 1;
|
2015-03-07 22:29:12 +00:00
|
|
|
|
2015-03-08 21:47:50 +00:00
|
|
|
port = request->tr_node.io_Message.mn_ReplyPort;
|
2015-03-07 22:29:12 +00:00
|
|
|
|
|
|
|
if (port != 0)
|
|
|
|
DeletePort(port);
|
|
|
|
|
|
|
|
CloseDevice((struct IORequest*)request);
|
|
|
|
DeleteExtIO((struct IORequest*)request);
|
2015-04-18 20:08:39 +00:00
|
|
|
|
|
|
|
return 0;
|
2015-03-07 22:29:12 +00:00
|
|
|
}
|
2015-07-25 21:11:11 +00:00
|
|
|
//--------------------------------------------------------------------------//
|
2015-04-18 20:08:39 +00:00
|
|
|
int gettimeofday(struct timeval *tv, struct timezone *tz)
|
2015-03-07 22:29:12 +00:00
|
|
|
{
|
|
|
|
request->tr_node.io_Command = TR_GETSYSTIME;
|
|
|
|
DoIO((struct IORequest*)request);
|
2015-04-30 22:44:54 +00:00
|
|
|
tv->tv_secs = (long)request->tr_time.tv_secs + offset;
|
2015-04-18 20:08:39 +00:00
|
|
|
tv->tv_micro = request->tr_time.tv_micro;
|
2015-03-07 22:29:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-18 20:08:39 +00:00
|
|
|
int settimeofday(const struct timeval *tv, const struct timezone *tz)
|
2015-03-07 22:29:12 +00:00
|
|
|
{
|
|
|
|
request->tr_node.io_Command = TR_SETSYSTIME;
|
2015-04-30 22:44:54 +00:00
|
|
|
request->tr_time.tv_secs = (long)tv->tv_secs - offset;
|
2015-04-18 20:08:39 +00:00
|
|
|
request->tr_time.tv_micro = tv->tv_micro;
|
2015-03-07 22:29:12 +00:00
|
|
|
DoIO((struct IORequest*)request);
|
2015-07-25 21:11:11 +00:00
|
|
|
validtime++;
|
|
|
|
|
|
|
|
// Make sure we are in the right timezone
|
|
|
|
if (validtime == 2) {
|
|
|
|
amiga_init_offset();
|
|
|
|
}
|
|
|
|
|
2015-03-07 22:29:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-07-25 21:11:11 +00:00
|
|
|
//--------------------------------------------------------------------------//
|
2015-03-12 21:48:56 +00:00
|
|
|
void amiga_save_time(void)
|
|
|
|
{
|
2015-07-25 21:11:11 +00:00
|
|
|
time_t now;
|
|
|
|
struct tm tm;
|
2015-03-17 23:53:36 +00:00
|
|
|
struct timeval tv;
|
2015-07-25 21:11:11 +00:00
|
|
|
int gmtoffset = 0;
|
2015-07-26 08:45:18 +00:00
|
|
|
char *message = "";
|
2015-07-10 21:00:05 +00:00
|
|
|
|
2015-07-25 21:11:11 +00:00
|
|
|
// Assume HW clock is in GMT if timezone library is present
|
|
|
|
if (TimezoneBase) {
|
|
|
|
now = time(NULL);
|
|
|
|
localtime_r(&now, &tm);
|
|
|
|
gmtoffset = tm.tm_gmtoff;
|
|
|
|
message = " GMT";
|
|
|
|
}
|
2015-07-10 21:00:05 +00:00
|
|
|
|
2015-04-18 20:08:39 +00:00
|
|
|
gettimeofday(&tv, NULL);
|
2015-07-25 21:11:11 +00:00
|
|
|
WriteBattClock((long)tv.tv_secs - offset - gmtoffset);
|
|
|
|
Put(NULL, OCX_DEBUG, "Saved%s to real-time clock\n", message);
|
2015-03-12 21:48:56 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 10:57:13 +00:00
|
|
|
void amiga_sync_time(int seconds)
|
2015-03-17 09:03:48 +00:00
|
|
|
{
|
2015-03-17 10:57:13 +00:00
|
|
|
limited_sync = 1;
|
|
|
|
sync_time.tv_secs = seconds;
|
|
|
|
sync_time.tv_micro = 0;
|
2015-03-17 09:03:48 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 20:08:39 +00:00
|
|
|
int amiga_sleep_time(double dur)
|
|
|
|
{
|
|
|
|
ULONG sigs, timersig;
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_secs = 0;
|
|
|
|
tv.tv_micro = 0;
|
|
|
|
|
|
|
|
if (limited_sync) {
|
|
|
|
adjust_timeval(&sync_time, -dur);
|
|
|
|
if ((LONG)sync_time.tv_secs < 0L) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
adjust_timeval(&tv, dur);
|
|
|
|
|
|
|
|
request->tr_node.io_Command = TR_ADDREQUEST;
|
|
|
|
request->tr_time = tv;
|
|
|
|
|
|
|
|
timersig = (1L << request->tr_node.io_Message.mn_ReplyPort->mp_SigBit);
|
|
|
|
SendIO((struct IORequest*)request);
|
|
|
|
sigs = Wait(SIGBREAKF_CTRL_C | timersig);
|
|
|
|
|
|
|
|
if (sigs & SIGBREAKF_CTRL_C) {
|
|
|
|
AbortIO((struct IORequest*)request);
|
|
|
|
WaitIO((struct IORequest*)request);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-07-25 21:11:11 +00:00
|
|
|
//--------------------------------------------------------------------------//
|
2015-04-18 20:08:39 +00:00
|
|
|
void adjust_timeval(struct timeval *tv, double offset)
|
2015-03-07 22:29:12 +00:00
|
|
|
{
|
2015-03-08 21:47:50 +00:00
|
|
|
double d1, d2;
|
2015-04-18 20:08:39 +00:00
|
|
|
int secs, micro;
|
2015-03-08 21:47:50 +00:00
|
|
|
|
|
|
|
d1 = floor(offset);
|
|
|
|
d2 = offset - d1;
|
2015-03-07 22:29:12 +00:00
|
|
|
|
2015-04-18 20:08:39 +00:00
|
|
|
secs = tv->tv_secs + (int)d1;
|
|
|
|
micro = tv->tv_micro + (int)floor(d2 * 1e6);
|
2015-03-07 22:29:12 +00:00
|
|
|
|
|
|
|
if (micro < 0) {
|
2015-04-18 20:08:39 +00:00
|
|
|
secs -= 1;
|
2015-03-07 22:29:12 +00:00
|
|
|
micro += 1000000;
|
|
|
|
} else if (micro >= 1000000) {
|
2015-04-18 20:08:39 +00:00
|
|
|
secs += 1;
|
2015-03-07 22:29:12 +00:00
|
|
|
micro -= 1000000;
|
|
|
|
}
|
|
|
|
|
2015-04-18 20:08:39 +00:00
|
|
|
tv->tv_secs = secs;
|
2015-03-07 22:29:12 +00:00
|
|
|
tv->tv_micro = micro;
|
|
|
|
}
|
2015-07-25 21:11:11 +00:00
|
|
|
//--------------------------------------------------------------------------//
|
2015-04-18 20:08:39 +00:00
|
|
|
#ifndef HAVE_POLL
|
|
|
|
int poll(struct pollfd *pfds, nfds_t nfds, int timeout)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
int maxfd = -1, ret;
|
|
|
|
fd_set rset,wset,xset;
|
|
|
|
struct timeval timeout_tv, *tvp = NULL;
|
|
|
|
|
|
|
|
if(pfds == NULL || nfds < 1)
|
|
|
|
{
|
|
|
|
return amiga_sleep_time(timeout / 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout >= 0)
|
|
|
|
{
|
|
|
|
timeout_tv.tv_sec = (timeout / 1000);
|
|
|
|
timeout_tv.tv_usec = (timeout % 1000) * 1000;
|
|
|
|
tvp = &timeout_tv;
|
|
|
|
}
|
|
|
|
|
|
|
|
FD_ZERO(&rset);
|
|
|
|
FD_ZERO(&wset);
|
|
|
|
FD_ZERO(&xset);
|
|
|
|
|
|
|
|
for (i = 0; i < nfds; i++)
|
|
|
|
{
|
|
|
|
pfds[i].revents = 0;
|
|
|
|
|
|
|
|
if (pfds[i].events == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (pfds[i].fd > maxfd)
|
|
|
|
maxfd = pfds[i].fd;
|
|
|
|
|
|
|
|
if (pfds[i].events & POLLIN)
|
|
|
|
FD_SET(pfds[i].fd, &rset);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_WAITSELECT
|
|
|
|
ret = WaitSelect(maxfd + 1, &rset, &wset, &xset, tvp, NULL);
|
|
|
|
#else
|
|
|
|
ret = select(maxfd + 1, &rset, &wset, &xset, tvp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(ret == -1)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
for (i = 0; i < nfds; i++)
|
|
|
|
{
|
|
|
|
if (pfds[i].events == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (FD_ISSET(pfds[i].fd, &rset))
|
|
|
|
pfds[i].revents |= POLLIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
2015-07-25 21:11:11 +00:00
|
|
|
//--------------------------------------------------------------------------//
|