8 /* SunOS 4.x Blargh.... */
10 #define NULL ((void*)0)
14 * Warning: proplist.h #defines BOOL which will clash with the
15 * typedef BOOL in Xmd.h
16 * proplist.h should use Bool (which is a #define in Xlib.h) instead.
23 # define WMAX(a,b) ((a)>(b) ? (a) : (b))
26 # define WMIN(a,b) ((a)<(b) ? (a) : (b))
30 #if (!defined (__GNUC__) || __GNUC__ < 2 || \
31 __GNUC_MINOR__ < (defined (__cplusplus) ? 6 : 4))
32 #define __ASSERT_FUNCTION ((char *) 0)
34 #define __ASSERT_FUNCTION __PRETTY_FUNCTION__
40 #define wassertr(expr) {}
41 #define wassertrv(expr, val) {}
49 #define wassertr(expr) assert(expr)
51 #define wassertrv(expr, val) assert(expr)
55 #define wassertr(expr) \
57 wwarning("%s line %i (%s): assertion %s failed",\
58 __FILE__, __LINE__, __ASSERT_FUNCTION, #expr);\
62 #define wassertrv(expr, val) \
64 wwarning("%s line %i (%s): assertion %s failed",\
65 __FILE__, __LINE__, __ASSERT_FUNCTION, #expr);\
75 #endif /* __cplusplus */
89 } WMNotificationCoalescing
;
92 /* The possible states for connections */
105 /* The possible states for connection timeouts */
110 } WMConnectionTimeoutState
;
115 WBNotFound
= INT_MIN
/* element was not found in bag */
119 typedef struct W_Array WMArray
;
120 typedef struct W_Bag WMBag
;
121 typedef struct W_Data WMData
;
122 typedef struct W_HashTable WMHashTable
;
123 typedef struct W_UserDefaults WMUserDefaults
;
124 typedef struct W_Notification WMNotification
;
125 typedef struct W_NotificationQueue WMNotificationQueue
;
126 typedef struct W_Host WMHost
;
127 typedef struct W_Connection WMConnection
;
131 typedef void WMFreeDataProc(void *data
);
142 /* DO NOT ACCESS THE CONTENTS OF THIS STRUCT */
151 /* NULL is pointer hash */
152 unsigned (*hash
)(const void *);
153 /* NULL is pointer compare */
154 Bool (*keyIsEqual
)(const void *, const void *);
155 /* NULL does nothing */
156 void* (*retainKey
)(const void *);
157 /* NULL does nothing */
158 void (*releaseKey
)(const void *);
159 } WMHashTableCallbacks
;
162 typedef void *WMBagIterator
;
169 void (*destructor
)(void *item
);
179 char character
; /* the escape character */
180 char *value
; /* value to place */
185 /* The connection callbacks */
186 typedef struct ConnectionDelegate
{
189 void (*didCatchException
)(struct ConnectionDelegate
*self
,
192 void (*didDie
)(struct ConnectionDelegate
*self
, WMConnection
*cPtr
);
194 void (*didInitialize
)(struct ConnectionDelegate
*self
, WMConnection
*cPtr
);
196 void (*didReceiveInput
)(struct ConnectionDelegate
*self
, WMConnection
*cPtr
);
198 void (*didTimeout
)(struct ConnectionDelegate
*self
, WMConnection
*cPtr
);
200 } ConnectionDelegate
;
203 typedef void WMNotificationObserverAction(void *observerData
,
204 WMNotification
*notification
);
208 /*......................................................................*/
210 typedef void (waborthandler
)(int);
212 waborthandler
*wsetabort(waborthandler
*);
215 /* don't free the returned string */
216 char *wstrerror(int errnum
);
218 void wfatal(const char *msg
, ...);
219 void wwarning(const char *msg
, ...);
220 void wsyserror(const char *msg
, ...);
221 void wsyserrorwithcode(int error
, const char *msg
, ...);
223 char *wfindfile(char *paths
, char *file
);
225 char *wfindfileinlist(char **path_list
, char *file
);
227 char *wfindfileinarray(proplist_t array
, char *file
);
229 char *wexpandpath(char *path
);
231 /* don't free the returned string */
234 void *wmalloc(size_t size
);
235 void *wrealloc(void *ptr
, size_t newsize
);
236 void wfree(void *ptr
);
239 void wrelease(void *ptr
);
240 void *wretain(void *ptr
);
242 char *wstrdup(char *str
);
244 char *wstrappend(char *dst
, char *src
);
248 void wtokensplit(char *command
, char ***argv
, int *argc
);
250 char *wtokenjoin(char **list
, int count
);
252 void wtokenfree(char **tokens
, int count
);
254 char *wtrimspace(char *s
);
258 char *wusergnusteppath();
260 char *wdefaultspathfordomain(char *domain
);
262 void wusleep(unsigned int microsec
);
265 int wsprintesc(char *buffer
, int length
, char *format
, WMSEscapes
**escapes
,
269 /*......................................................................*/
271 /* This function is used _only_ if you create a NON-GUI program.
272 * For GUI based programs use WMNextEvent()/WMHandleEvent() instead.
273 * This function will handle all input/timer/idle events, then return.
276 void WHandleEvents();
278 /*......................................................................*/
281 WMHashTable
*WMCreateHashTable(WMHashTableCallbacks callbacks
);
283 void WMFreeHashTable(WMHashTable
*table
);
285 void WMResetHashTable(WMHashTable
*table
);
287 void *WMHashGet(WMHashTable
*table
, const void *key
);
289 /* put data in table, replacing already existing data and returning
291 void *WMHashInsert(WMHashTable
*table
, void *key
, void *data
);
293 void WMHashRemove(WMHashTable
*table
, const void *key
);
295 /* warning: do not manipulate the table while using these functions */
296 WMHashEnumerator
WMEnumerateHashTable(WMHashTable
*table
);
298 void *WMNextHashEnumeratorItem(WMHashEnumerator
*enumerator
);
300 unsigned WMCountHashTable(WMHashTable
*table
);
305 /* some predefined callback sets */
307 extern const WMHashTableCallbacks WMIntHashCallbacks
;
308 /* sizeof(keys) are <= sizeof(void*) */
310 extern const WMHashTableCallbacks WMStringHashCallbacks
;
311 /* keys are strings. Strings will be copied with wstrdup()
312 * and freed with wfree() */
314 extern const WMHashTableCallbacks WMStringPointerHashCallbacks
;
315 /* keys are strings, bug they are not copied */
318 /*......................................................................*/
322 * Array bags use an array to store the elements.
323 * Item indexes may be any integer number.
326 * Fast [O(1)] access to elements
327 * Fast [O(1)] push/pop
330 * A little slower [O(n)] for insertion/deletion of elements that
332 * Element indexes with large difference will cause large holes
336 * Tree bags use a red-black tree for storage.
337 * Item indexes may be any integer number.
340 * O(lg n) insertion/deletion/search
341 * Good for large numbers of elements with sparse indexes
344 * O(lg n) insertion/deletion/search
345 * Slow for storing small numbers of elements
348 #define WMCreateBag(size) WMCreateTreeBag()
350 #define WMCreateBagWithDestructor(size, d) WMCreateTreeBagWithDestructor(d)
352 WMBag
* WMCreateTreeBag(void);
354 WMBag
* WMCreateTreeBagWithDestructor(void (*destructor
)(void*));
356 int WMGetBagItemCount(WMBag
*self
);
358 int WMAppendBag(WMBag
*self
, WMBag
*bag
);
360 int WMPutInBag(WMBag
*self
, void *item
);
362 /* insert will increment the index of elements after it by 1 */
363 int WMInsertInBag(WMBag
*self
, int index
, void *item
);
366 /* erase will remove the element from the bag,
367 * but will keep the index of the other elements unchanged */
368 int WMEraseFromBag(WMBag
*self
, int index
);
370 /* delete and remove will remove the elements and cause the elements
371 * after them to decrement their indexes by 1 */
372 int WMRemoveFromBag(WMBag
*self
, void *item
);
374 int WMDeleteFromBag(WMBag
*self
, int index
);
376 void* WMGetFromBag(WMBag
*self
, int index
);
378 void* WMReplaceInBag(WMBag
*self
, int index
, void *item
);
380 #define WMSetInBag(bag, index, item) WMReplaceInBag(bag, index, item)
382 /* comparer must return:
387 int WMSortBag(WMBag
*self
, int (*comparer
)(const void*, const void*));
389 void WMEmptyBag(WMBag
*self
);
391 void WMFreeBag(WMBag
*self
);
393 void WMMapBag(WMBag
*self
, void (*function
)(void*, void*), void *data
);
395 int WMGetFirstInBag(WMBag
*self
, void *item
);
397 int WMCountInBag(WMBag
*self
, void *item
);
399 int WMFindInBag(WMBag
*self
, int (*match
)(void*,void*), void *cdata
);
401 void* WMBagFirst(WMBag
*self
, WMBagIterator
*ptr
);
403 void* WMBagLast(WMBag
*self
, WMBagIterator
*ptr
);
405 void* WMBagNext(WMBag
*self
, WMBagIterator
*ptr
);
407 void* WMBagPrevious(WMBag
*self
, WMBagIterator
*ptr
);
409 void* WMBagIteratorAtIndex(WMBag
*self
, int index
, WMBagIterator
*ptr
);
411 int WMBagIndexForIterator(WMBag
*bag
, WMBagIterator ptr
);
414 #define WM_ITERATE_BAG(bag, var, i) \
415 for (var = WMBagFirst(bag, &(i)); (i) != NULL; \
416 var = WMBagNext(bag, &(i)))
418 #define WM_ETARETI_BAG(bag, var, i) \
419 for (var = WMBagLast(bag, &(i)); (i) != NULL; \
420 var = WMBagPrevious(bag, &(i)))
424 /*-------------------------------------------------------------------------*/
426 /* WMData handling */
428 /* Creating/destroying data */
430 WMData
* WMCreateDataWithCapacity(unsigned capacity
);
432 WMData
* WMCreateDataWithLength(unsigned length
);
434 WMData
* WMCreateDataWithBytes(void *bytes
, unsigned length
);
436 /* destructor is a function called to free the data when releasing the data
437 * object, or NULL if no freeing of data is necesary. */
438 WMData
* WMCreateDataWithBytesNoCopy(void *bytes
, unsigned length
,
439 WMFreeDataProc
*destructor
);
441 WMData
* WMCreateDataWithData(WMData
*aData
);
443 WMData
* WMRetainData(WMData
*aData
);
445 void WMReleaseData(WMData
*aData
);
447 /* Adjusting capacity */
449 void WMSetDataCapacity(WMData
*aData
, unsigned capacity
);
451 void WMSetDataLength(WMData
*aData
, unsigned length
);
453 void WMIncreaseDataLengthBy(WMData
*aData
, unsigned extraLength
);
457 const void* WMDataBytes(WMData
*aData
);
459 void WMGetDataBytes(WMData
*aData
, void *buffer
);
461 void WMGetDataBytesWithLength(WMData
*aData
, void *buffer
, unsigned length
);
463 void WMGetDataBytesWithRange(WMData
*aData
, void *buffer
, WMRange aRange
);
465 WMData
* WMGetSubdataWithRange(WMData
*aData
, WMRange aRange
);
469 Bool
WMIsDataEqualToData(WMData
*aData
, WMData
*anotherData
);
471 unsigned WMGetDataLength(WMData
*aData
);
475 void WMAppendDataBytes(WMData
*aData
, void *bytes
, unsigned length
);
477 void WMAppendData(WMData
*aData
, WMData
*anotherData
);
481 void WMReplaceDataBytesInRange(WMData
*aData
, WMRange aRange
, void *bytes
);
483 void WMResetDataBytesInRange(WMData
*aData
, WMRange aRange
);
485 void WMSetData(WMData
*aData
, WMData
*anotherData
);
488 void WMSetDataFormat(WMData
*aData
, unsigned format
);
490 unsigned WMGetDataFormat(WMData
*aData
);
494 /*--------------------------------------------------------------------------*/
497 WMNotification
*WMCreateNotification(char *name
, void *object
, void *clientData
);
499 void WMReleaseNotification(WMNotification
*notification
);
501 WMNotification
*WMRetainNotification(WMNotification
*notification
);
503 void *WMGetNotificationClientData(WMNotification
*notification
);
505 void *WMGetNotificationObject(WMNotification
*notification
);
507 char *WMGetNotificationName(WMNotification
*notification
);
510 void WMAddNotificationObserver(WMNotificationObserverAction
*observerAction
,
511 void *observer
, char *name
, void *object
);
513 void WMPostNotification(WMNotification
*notification
);
515 void WMRemoveNotificationObserver(void *observer
);
517 void WMRemoveNotificationObserverWithName(void *observer
, char *name
,
520 void WMPostNotificationName(char *name
, void *object
, void *clientData
);
522 WMNotificationQueue
*WMGetDefaultNotificationQueue(void);
524 WMNotificationQueue
*WMCreateNotificationQueue(void);
526 void WMDequeueNotificationMatching(WMNotificationQueue
*queue
,
527 WMNotification
*notification
,
530 void WMEnqueueNotification(WMNotificationQueue
*queue
,
531 WMNotification
*notification
,
532 WMPostingStyle postingStyle
);
534 void WMEnqueueCoalesceNotification(WMNotificationQueue
*queue
,
535 WMNotification
*notification
,
536 WMPostingStyle postingStyle
,
537 unsigned coalesceMask
);
540 /*......................................................................*/
542 WMUserDefaults
*WMGetStandardUserDefaults(void);
544 WMUserDefaults
*WMGetDefaultsFromPath(char *path
);
546 void WMSynchronizeUserDefaults(WMUserDefaults
*database
);
548 void WMSaveUserDefaults(WMUserDefaults
*database
);
550 void WMEnableUDPeriodicSynchronization(WMUserDefaults
*database
, Bool enable
);
552 /* Returns a PLArray with all keys in the user defaults database.
553 * Free the returned array with PLRelease() when no longer needed,
554 * but do not free the elements of the array! They're just references. */
555 proplist_t
WMGetUDAllKeys(WMUserDefaults
*database
);
557 proplist_t
WMGetUDObjectForKey(WMUserDefaults
*database
, char *defaultName
);
559 void WMSetUDObjectForKey(WMUserDefaults
*database
, proplist_t object
,
562 void WMRemoveUDObjectForKey(WMUserDefaults
*database
, char *defaultName
);
564 char *WMGetUDStringForKey(WMUserDefaults
*database
, char *defaultName
);
566 int WMGetUDIntegerForKey(WMUserDefaults
*database
, char *defaultName
);
568 float WMGetUDFloatForKey(WMUserDefaults
*database
, char *defaultName
);
570 Bool
WMGetUDBoolForKey(WMUserDefaults
*database
, char *defaultName
);
572 void WMSetUDStringForKey(WMUserDefaults
*database
, char *value
,
575 void WMSetUDIntegerForKey(WMUserDefaults
*database
, int value
,
578 void WMSetUDFloatForKey(WMUserDefaults
*database
, float value
,
581 void WMSetUDBoolForKey(WMUserDefaults
*database
, Bool value
,
584 proplist_t
WMGetUDSearchList(WMUserDefaults
*database
);
586 void WMSetUDSearchList(WMUserDefaults
*database
, proplist_t list
);
588 extern char *WMUserDefaultsDidChangeNotification
;
591 /*-------------------------------------------------------------------------*/
593 /* WMHost: host handling */
595 WMHost
* WMGetCurrentHost();
597 WMHost
* WMGetHostWithName(char* name
);
599 WMHost
* WMGetHostWithAddress(char* address
);
601 WMHost
* WMRetainHost(WMHost
*hPtr
);
603 void WMReleaseHost(WMHost
*hPtr
);
606 * Host cache management
607 * If enabled, only one object representing each host will be created, and
608 * a shared instance will be returned by all methods that return a host.
609 * Enabled by default.
611 void WMSetHostCacheEnabled(Bool flag
);
613 Bool
WMIsHostCacheEnabled();
615 void WMFlushHostCache();
618 * Compare hosts: Hosts are equal if they share at least one address
620 Bool
WMIsHostEqualToHost(WMHost
* hPtr
, WMHost
* anotherHost
);
624 * WMGetHostName() will return first name (official) if a host has several.
625 * WMGetHostNames() will return a R/O WMBag with all the names of the host.
627 char* WMGetHostName(WMHost
* hPtr
);
629 /* The returned bag is R/O. Do not modify it, and do not free it! */
630 WMBag
* WMGetHostNames(WMHost
* hPtr
);
634 * Addresses are represented as "Dotted Decimal" strings, e.g. "192.42.172.1"
635 * WMGetHostAddress() will return an arbitrary address if a host has several.
636 * WMGetHostAddresses() will return a R/O WMBag with all addresses of the host.
638 char* WMGetHostAddress(WMHost
* hPtr
);
640 /* The returned bag is R/O. Do not modify it, and do not free it! */
641 WMBag
* WMGetHostAddresses(WMHost
* hPtr
);
644 /*-------------------------------------------------------------------------*/
646 /* WMConnection functions */
648 WMConnection
* WMCreateConnectionAsServerAtAddress(char *host
, char *service
,
651 WMConnection
* WMCreateConnectionToAddress(char *host
, char *service
,
654 WMConnection
* WMCreateConnectionToAddressAndNotify(char *host
, char *service
,
657 void WMCloseConnection(WMConnection
*cPtr
);
659 void WMDestroyConnection(WMConnection
*cPtr
);
661 WMConnection
* WMAcceptConnection(WMConnection
*listener
);
663 /* Release the returned data! */
664 WMData
* WMGetConnectionAvailableData(WMConnection
*cPtr
);
666 int WMSendConnectionData(WMConnection
*cPtr
, WMData
*data
);
668 Bool
WMEnqueueConnectionData(WMConnection
*cPtr
, WMData
*data
);
670 #define WMFlushConnection(cPtr) WMSendConnectionData((cPtr), NULL)
672 void WMSetConnectionDelegate(WMConnection
*cPtr
, ConnectionDelegate
*delegate
);
674 /* Connection info */
676 char* WMGetConnectionAddress(WMConnection
*cPtr
);
678 char* WMGetConnectionService(WMConnection
*cPtr
);
680 char* WMGetConnectionProtocol(WMConnection
*cPtr
);
682 void WMSetConnectionNonBlocking(WMConnection
*cPtr
, Bool flag
);
684 void* WMGetConnectionClientData(WMConnection
*cPtr
);
686 void WMSetConnectionClientData(WMConnection
*cPtr
, void *data
);
688 unsigned int WMGetConnectionFlags(WMConnection
*cPtr
);
690 void WMSetConnectionFlags(WMConnection
*cPtr
, unsigned int flags
);
692 int WMGetConnectionSocket(WMConnection
*cPtr
);
694 WMConnectionState
WMGetConnectionState(WMConnection
*cPtr
);
696 WMConnectionTimeoutState
WMGetConnectionTimeoutState(WMConnection
*cPtr
);
699 * Passing timeout==0 in the SetTimeout functions below, will reset that
700 * timeout to its default value.
703 /* The default timeout inherited by all WMConnection operations, if none set */
704 void WMSetConnectionDefaultTimeout(unsigned int timeout
);
706 /* Global timeout for all WMConnection objects, for opening a new connection */
707 void WMSetConnectionOpenTimeout(unsigned int timeout
);
709 /* Connection specific timeout for sending out data */
710 void WMSetConnectionSendTimeout(WMConnection
*cPtr
, unsigned int timeout
);
713 /* Global variables */
715 extern int WCErrorCode
;
718 /*-------------------------------------------------------------------------*/
724 #endif /* __cplusplus */