1 /*
2  *  KCemu -- The emulator for the KC85 homecomputer series and much more.
3  *  Copyright (C) 1997-2010 Torsten Paul
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License along
16  *  with this program; if not, write to the Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef __sys_sysdep_h
21 #define __sys_sysdep_h
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /*
28  *  Sleep for the specified number of microseconds.
29  */
30 void sys_usleep(long microseconds);
31 
32 /*
33  *  Return local time.
34  *
35  *  This works like localtime_r() as it copies the values
36  *  into a buffers supplied by the caller instead of returning
37  *  a pointer to some statically allocated memory
38  */
39 void sys_localtime(int *year, int *month, int *day, int *hour, int *minute, int *second);
40 
41 /*
42  *  Return local time converted from given time value.
43  *
44  *  This is the same as sys_localtime, but does not return
45  *  the current time but the time converted from the given
46  *  value.
47  */
48 void sys_converttime(long time, int *year, int *month, int *day, int *hour, int *minute, int *second);
49 
50 /*
51  *  Return system time.
52  *
53  *  This works like gettimeofday() but instead of returning the
54  *  values in a struct timeval the values are directly stored
55  *  in the specified variables.
56  */
57 void sys_gettimeofday(long *tv_sec, long *tv_usec);
58 
59 /*
60  *  Return system time
61  *
62  *  This works like gettimeofday() but instead of returning the
63  *  values in a struct timeval the values are directly stored
64  *  in the specified variables.
65  */
66 void sys_gettimeofday(long *tv_sec, long *tv_usec);
67 
68 /*
69  *  Return basename (filename without directory) for a given
70  *  full pathname.
71  *
72  *  This works like basename() but doesn't handle NULL arguments
73  *  and special values like "/".
74  *  The returned string is allocated with strdup() so it must
75  *  be freed with free().
76  */
77 char * sys_basename(const char *path);
78 
79 /*
80  *  Return true if the given path denotes an absolute path
81  *  name for the specific system (e.g. if it starts with
82  *  a '/' for linux or with a drive letter followed by a ':'
83  *  for mingw).
84  */
85 int sys_isabsolutepath(const char *path);
86 
87 /*
88  *  Return the directory of the currently running program.
89  *
90  *  Returns where the executable is located, not the current
91  *  working directory! if it's not possible to retrieve this
92  *  information the function returns NULL.
93  *
94  *  The returned string should be freed with free().
95  */
96 char * sys_getprogrampath(void);
97 
98 /*
99  *  Return the user home directory.
100  *
101  *  The returned string should be freed with free().
102  */
103 char * sys_gethome(void);
104 
105 /*
106  *  Create new directory, given permissions are ignored on
107  *  some platforms (e.g. MinGW).
108  */
109 int sys_mkdir(const char *pathname, int mode);
110 
111 /*
112  *  Return disk information for the given path.
113  *
114  *  The information is stored in the caller provided buffer. the
115  *  data is only valid if the return value of the call is 0, which
116  *  means there was no error while retrieving the data from the
117  *  operating system.
118  *
119  *  As the return value is currently 32 bit, the values are clamped
120  *  at 4GB.
121  */
122 int sys_getdiskinfo(const char *path, unsigned long *total, unsigned long *available, unsigned short *block_size);
123 
124 /*
125  *  Convert short integer (16-bit) from host to network byte order
126  */
127 unsigned short sys_htons(unsigned short hostshort);
128 
129 /*
130  *  Convert long integer (32-bit) from host to network byte order
131  */
132 unsigned long sys_htonl(unsigned long hostlong);
133 
134 #define SYS_SOCKET_ERR_INTR (-1)
135 #define SYS_SOCKET_ERR_INPROGRESS (-2)
136 #define SYS_SOCKET_ERR_ALREADY (-3)
137 #define SYS_SOCKET_ERR_ISCONN (-4)
138 
139 /*
140  *  Initialize system socket implementation (e.g. WinSocket)
141  *
142  *  This will be called once when starting the application, and
143  *  should make the socket interface usable. For Unix systems
144  *  this is normally a no-op, but it's required for the WinSocket
145  *  implementation to call WSAStartup() before using any socket
146  *  related functions.
147  *
148  *  On success sys_socket_init() returns 0.
149  */
150 int sys_socket_init(void);
151 
152 /*
153  *  Create a socket.
154  *
155  *  If stream is set to non-zero value, the socket is opened in
156  *  stream mode, otherwise in datagram mode. A non-zero value
157  *  for nonblocking will set stream to nonblocking mode.
158  */
159 int sys_socket_create(int stream, int nonblocking);
160 
161 /*
162  *  Close a socket.
163  */
164 void sys_socket_close(int socket);
165 
166 /*
167  *  Connect socket to given ip address and port.
168  */
169 int sys_socket_connect(int socket, unsigned char ip0, unsigned char ip1, unsigned char ip2, unsigned char ip3, unsigned short port);
170 
171 /*
172  *  Receive a datagram from a socket.
173  *
174  *  The data is placed into the given buffer and if the
175  *  ipX and port parameters are not NULL the ip address
176  *  and port of the source will be written into the
177  *  given variables.
178  */
179 int sys_socket_recvfrom(int socket, unsigned char *buf, int bufsize, unsigned char *ip0, unsigned char *ip1, unsigned char *ip2, unsigned char *ip3, unsigned short *port);
180 
181 /*
182  *  Send data to a connected socket.
183  */
184 int sys_socket_send(int socket, unsigned char *buf, int bufsize);
185 
186 /*
187  *  Send data to the given ip address and port.
188  */
189 int sys_socket_sendto(int socket, unsigned char *buf, int bufsize, unsigned char ip0, unsigned char ip1, unsigned char ip2, unsigned char ip3, unsigned short port);
190 
191 #ifdef __cplusplus
192 }
193 #endif
194 
195 #endif /* __sys_sysdep_h */
196