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 WMBag */
116 WANotFound
= -1, /* element was not found in WMArray */
117 WLNotFound
= -1 /* element was not found in WMList */
121 typedef struct W_Array WMArray
;
122 typedef struct W_Bag WMBag
;
123 typedef struct W_Data WMData
;
124 typedef struct W_HashTable WMHashTable
;
125 typedef struct W_UserDefaults WMUserDefaults
;
126 typedef struct W_Notification WMNotification
;
127 typedef struct W_NotificationQueue WMNotificationQueue
;
128 typedef struct W_Host WMHost
;
129 typedef struct W_Connection WMConnection
;
133 typedef int WMCompareDataProc(const void *item1
, const void *item2
);
135 typedef void WMFreeDataProc(void *data
);
137 /* Used by WMBag or WMArray for matching data */
138 typedef int WMMatchDataProc(void *item
, void *cdata
);
150 /* DO NOT ACCESS THE CONTENTS OF THIS STRUCT */
159 /* NULL is pointer hash */
160 unsigned (*hash
)(const void *);
161 /* NULL is pointer compare */
162 Bool (*keyIsEqual
)(const void *, const void *);
163 /* NULL does nothing */
164 void* (*retainKey
)(const void *);
165 /* NULL does nothing */
166 void (*releaseKey
)(const void *);
167 } WMHashTableCallbacks
;
170 typedef void *WMBagIterator
;
175 char character
; /* the escape character */
176 char *value
; /* value to place */
181 /* The connection callbacks */
182 typedef struct ConnectionDelegate
{
185 void (*didCatchException
)(struct ConnectionDelegate
*self
,
188 void (*didDie
)(struct ConnectionDelegate
*self
, WMConnection
*cPtr
);
190 void (*didInitialize
)(struct ConnectionDelegate
*self
, WMConnection
*cPtr
);
192 void (*didReceiveInput
)(struct ConnectionDelegate
*self
, WMConnection
*cPtr
);
194 void (*didTimeout
)(struct ConnectionDelegate
*self
, WMConnection
*cPtr
);
196 } ConnectionDelegate
;
199 typedef void WMNotificationObserverAction(void *observerData
,
200 WMNotification
*notification
);
204 /*......................................................................*/
206 typedef void (waborthandler
)(int);
208 waborthandler
*wsetabort(waborthandler
*);
211 /* don't free the returned string */
212 char *wstrerror(int errnum
);
214 void wfatal(const char *msg
, ...);
215 void wwarning(const char *msg
, ...);
216 void wsyserror(const char *msg
, ...);
217 void wsyserrorwithcode(int error
, const char *msg
, ...);
219 char *wfindfile(char *paths
, char *file
);
221 char *wfindfileinlist(char **path_list
, char *file
);
223 char *wfindfileinarray(proplist_t array
, char *file
);
225 char *wexpandpath(char *path
);
227 /* don't free the returned string */
230 void *wmalloc(size_t size
);
231 void *wrealloc(void *ptr
, size_t newsize
);
232 void wfree(void *ptr
);
235 void wrelease(void *ptr
);
236 void *wretain(void *ptr
);
238 char *wstrdup(char *str
);
240 char *wstrappend(char *dst
, char *src
);
244 void wtokensplit(char *command
, char ***argv
, int *argc
);
246 char *wtokennext(char *word
, char **next
);
248 char *wtokenjoin(char **list
, int count
);
250 void wtokenfree(char **tokens
, int count
);
252 char *wtrimspace(char *s
);
256 char *wusergnusteppath();
258 char *wdefaultspathfordomain(char *domain
);
260 void wusleep(unsigned int microsec
);
263 int wsprintesc(char *buffer
, int length
, char *format
, WMSEscapes
**escapes
,
267 /*......................................................................*/
269 /* This function is used _only_ if you create a NON-GUI program.
270 * For GUI based programs use WMNextEvent()/WMHandleEvent() instead.
271 * This function will handle all input/timer/idle events, then return.
274 void WHandleEvents();
276 /*......................................................................*/
279 WMHashTable
*WMCreateHashTable(WMHashTableCallbacks callbacks
);
281 void WMFreeHashTable(WMHashTable
*table
);
283 void WMResetHashTable(WMHashTable
*table
);
285 void *WMHashGet(WMHashTable
*table
, const void *key
);
287 /* put data in table, replacing already existing data and returning
289 void *WMHashInsert(WMHashTable
*table
, void *key
, void *data
);
291 void WMHashRemove(WMHashTable
*table
, const void *key
);
293 /* warning: do not manipulate the table while using these functions */
294 WMHashEnumerator
WMEnumerateHashTable(WMHashTable
*table
);
296 void *WMNextHashEnumeratorItem(WMHashEnumerator
*enumerator
);
298 unsigned WMCountHashTable(WMHashTable
*table
);
303 /* some predefined callback sets */
305 extern const WMHashTableCallbacks WMIntHashCallbacks
;
306 /* sizeof(keys) are <= sizeof(void*) */
308 extern const WMHashTableCallbacks WMStringHashCallbacks
;
309 /* keys are strings. Strings will be copied with wstrdup()
310 * and freed with wfree() */
312 extern const WMHashTableCallbacks WMStringPointerHashCallbacks
;
313 /* keys are strings, bug they are not copied */
316 /*......................................................................*/
319 * WMArray use an array to store the elements.
320 * Item indexes may be only positive integer numbers.
321 * The array cannot contain holes in it.
324 * Fast [O(1)] access to elements
325 * Fast [O(1)] push/pop
328 * A little slower [O(n)] for insertion/deletion of elements that
332 WMArray
* WMCreateArray(int initialSize
);
334 WMArray
* WMCreateArrayWithDestructor(int initialSize
, WMFreeDataProc
*destructor
);
336 void WMEmptyArray(WMArray
*array
);
338 void WMFreeArray(WMArray
*array
);
340 int WMGetArrayItemCount(WMArray
*array
);
342 /* appends other to array. other remains unchanged */
343 int WMAppendArray(WMArray
*array
, WMArray
*other
);
345 /* add will place the element at the end of the array */
346 int WMAddToArray(WMArray
*array
, void *item
);
348 #define WMPushInArray(array, item) WMAddToArray(array, item)
350 /* insert will increment the index of elements after it by 1 */
351 int WMInsertInArray(WMArray
*array
, int index
, void *item
);
353 /* replace and set will return the old item WITHOUT calling the
354 * destructor on it even if its available. Free the returned item yourself.
356 void* WMReplaceInArray(WMArray
*array
, int index
, void *item
);
358 #define WMSetInArray(array, index, item) WMReplaceInArray(array, index, item)
360 /* delete and remove will remove the elements and cause the elements
361 * after them to decrement their indexes by 1. Also will call the
362 * destructor on the deleted element if there's one available.
364 int WMDeleteFromArray(WMArray
*array
, int index
);
366 int WMRemoveFromArray(WMArray
*array
, void *item
);
368 void* WMGetFromArray(WMArray
*array
, int index
);
370 #define WMGetFirstInArray(array, item) WMFindInArray(array, NULL, item)
372 /* pop will return the last element from the array, also removing it
373 * from the array. The destructor is NOT called, even if available.
374 * Free the returned element if needed by yourself
376 void* WMPopFromArray(WMArray
*array
);
378 int WMFindInArray(WMArray
*array
, WMMatchDataProc
*match
, void *cdata
);
380 int WMCountInArray(WMArray
*array
, void *item
);
382 /* comparer must return:
387 void WMSortArray(WMArray
*array
, WMCompareDataProc
*comparer
);
389 void WMMapArray(WMArray
*array
, void (*function
)(void*, void*), void *data
);
393 /*..........................................................................*/
396 * Tree bags use a red-black tree for storage.
397 * Item indexes may be any integer number.
400 * O(lg n) insertion/deletion/search
401 * Good for large numbers of elements with sparse indexes
404 * O(lg n) insertion/deletion/search
405 * Slow for storing small numbers of elements
408 #define WMCreateBag(size) WMCreateTreeBag()
410 #define WMCreateBagWithDestructor(size, d) WMCreateTreeBagWithDestructor(d)
412 WMBag
* WMCreateTreeBag(void);
414 WMBag
* WMCreateTreeBagWithDestructor(WMFreeDataProc
*destructor
);
416 int WMGetBagItemCount(WMBag
*bag
);
418 int WMAppendBag(WMBag
*bag
, WMBag
*other
);
420 int WMPutInBag(WMBag
*bag
, void *item
);
422 /* insert will increment the index of elements after it by 1 */
423 int WMInsertInBag(WMBag
*bag
, int index
, void *item
);
426 /* erase will remove the element from the bag,
427 * but will keep the index of the other elements unchanged */
428 int WMEraseFromBag(WMBag
*bag
, int index
);
430 /* delete and remove will remove the elements and cause the elements
431 * after them to decrement their indexes by 1 */
432 int WMDeleteFromBag(WMBag
*bag
, int index
);
434 int WMRemoveFromBag(WMBag
*bag
, void *item
);
436 void* WMGetFromBag(WMBag
*bag
, int index
);
438 void* WMReplaceInBag(WMBag
*bag
, int index
, void *item
);
440 #define WMSetInBag(bag, index, item) WMReplaceInBag(bag, index, item)
442 /* comparer must return:
447 void WMSortBag(WMBag
*bag
, WMCompareDataProc
*comparer
);
449 void WMEmptyBag(WMBag
*bag
);
451 void WMFreeBag(WMBag
*bag
);
453 void WMMapBag(WMBag
*bag
, void (*function
)(void*, void*), void *data
);
455 int WMGetFirstInBag(WMBag
*bag
, void *item
);
457 int WMCountInBag(WMBag
*bag
, void *item
);
459 int WMFindInBag(WMBag
*bag
, WMMatchDataProc
*match
, void *cdata
);
461 void* WMBagFirst(WMBag
*bag
, WMBagIterator
*ptr
);
463 void* WMBagLast(WMBag
*bag
, WMBagIterator
*ptr
);
465 void* WMBagNext(WMBag
*bag
, WMBagIterator
*ptr
);
467 void* WMBagPrevious(WMBag
*bag
, WMBagIterator
*ptr
);
469 void* WMBagIteratorAtIndex(WMBag
*bag
, int index
, WMBagIterator
*ptr
);
471 int WMBagIndexForIterator(WMBag
*bag
, WMBagIterator ptr
);
474 #define WM_ITERATE_BAG(bag, var, i) \
475 for (var = WMBagFirst(bag, &(i)); (i) != NULL; \
476 var = WMBagNext(bag, &(i)))
478 #define WM_ETARETI_BAG(bag, var, i) \
479 for (var = WMBagLast(bag, &(i)); (i) != NULL; \
480 var = WMBagPrevious(bag, &(i)))
484 /*-------------------------------------------------------------------------*/
486 /* WMData handling */
488 /* Creating/destroying data */
490 WMData
* WMCreateDataWithCapacity(unsigned capacity
);
492 WMData
* WMCreateDataWithLength(unsigned length
);
494 WMData
* WMCreateDataWithBytes(void *bytes
, unsigned length
);
496 /* destructor is a function called to free the data when releasing the data
497 * object, or NULL if no freeing of data is necesary. */
498 WMData
* WMCreateDataWithBytesNoCopy(void *bytes
, unsigned length
,
499 WMFreeDataProc
*destructor
);
501 WMData
* WMCreateDataWithData(WMData
*aData
);
503 WMData
* WMRetainData(WMData
*aData
);
505 void WMReleaseData(WMData
*aData
);
507 /* Adjusting capacity */
509 void WMSetDataCapacity(WMData
*aData
, unsigned capacity
);
511 void WMSetDataLength(WMData
*aData
, unsigned length
);
513 void WMIncreaseDataLengthBy(WMData
*aData
, unsigned extraLength
);
517 const void* WMDataBytes(WMData
*aData
);
519 void WMGetDataBytes(WMData
*aData
, void *buffer
);
521 void WMGetDataBytesWithLength(WMData
*aData
, void *buffer
, unsigned length
);
523 void WMGetDataBytesWithRange(WMData
*aData
, void *buffer
, WMRange aRange
);
525 WMData
* WMGetSubdataWithRange(WMData
*aData
, WMRange aRange
);
529 Bool
WMIsDataEqualToData(WMData
*aData
, WMData
*anotherData
);
531 unsigned WMGetDataLength(WMData
*aData
);
535 void WMAppendDataBytes(WMData
*aData
, void *bytes
, unsigned length
);
537 void WMAppendData(WMData
*aData
, WMData
*anotherData
);
541 void WMReplaceDataBytesInRange(WMData
*aData
, WMRange aRange
, void *bytes
);
543 void WMResetDataBytesInRange(WMData
*aData
, WMRange aRange
);
545 void WMSetData(WMData
*aData
, WMData
*anotherData
);
548 void WMSetDataFormat(WMData
*aData
, unsigned format
);
550 unsigned WMGetDataFormat(WMData
*aData
);
554 /*--------------------------------------------------------------------------*/
557 WMNotification
*WMCreateNotification(char *name
, void *object
, void *clientData
);
559 void WMReleaseNotification(WMNotification
*notification
);
561 WMNotification
*WMRetainNotification(WMNotification
*notification
);
563 void *WMGetNotificationClientData(WMNotification
*notification
);
565 void *WMGetNotificationObject(WMNotification
*notification
);
567 char *WMGetNotificationName(WMNotification
*notification
);
570 void WMAddNotificationObserver(WMNotificationObserverAction
*observerAction
,
571 void *observer
, char *name
, void *object
);
573 void WMPostNotification(WMNotification
*notification
);
575 void WMRemoveNotificationObserver(void *observer
);
577 void WMRemoveNotificationObserverWithName(void *observer
, char *name
,
580 void WMPostNotificationName(char *name
, void *object
, void *clientData
);
582 WMNotificationQueue
*WMGetDefaultNotificationQueue(void);
584 WMNotificationQueue
*WMCreateNotificationQueue(void);
586 void WMDequeueNotificationMatching(WMNotificationQueue
*queue
,
587 WMNotification
*notification
,
590 void WMEnqueueNotification(WMNotificationQueue
*queue
,
591 WMNotification
*notification
,
592 WMPostingStyle postingStyle
);
594 void WMEnqueueCoalesceNotification(WMNotificationQueue
*queue
,
595 WMNotification
*notification
,
596 WMPostingStyle postingStyle
,
597 unsigned coalesceMask
);
600 /*......................................................................*/
602 WMUserDefaults
*WMGetStandardUserDefaults(void);
604 WMUserDefaults
*WMGetDefaultsFromPath(char *path
);
606 void WMSynchronizeUserDefaults(WMUserDefaults
*database
);
608 void WMSaveUserDefaults(WMUserDefaults
*database
);
610 void WMEnableUDPeriodicSynchronization(WMUserDefaults
*database
, Bool enable
);
612 /* Returns a PLArray with all keys in the user defaults database.
613 * Free the returned array with PLRelease() when no longer needed,
614 * but do not free the elements of the array! They're just references. */
615 proplist_t
WMGetUDAllKeys(WMUserDefaults
*database
);
617 proplist_t
WMGetUDObjectForKey(WMUserDefaults
*database
, char *defaultName
);
619 void WMSetUDObjectForKey(WMUserDefaults
*database
, proplist_t object
,
622 void WMRemoveUDObjectForKey(WMUserDefaults
*database
, char *defaultName
);
624 char *WMGetUDStringForKey(WMUserDefaults
*database
, char *defaultName
);
626 int WMGetUDIntegerForKey(WMUserDefaults
*database
, char *defaultName
);
628 float WMGetUDFloatForKey(WMUserDefaults
*database
, char *defaultName
);
630 Bool
WMGetUDBoolForKey(WMUserDefaults
*database
, char *defaultName
);
632 void WMSetUDStringForKey(WMUserDefaults
*database
, char *value
,
635 void WMSetUDIntegerForKey(WMUserDefaults
*database
, int value
,
638 void WMSetUDFloatForKey(WMUserDefaults
*database
, float value
,
641 void WMSetUDBoolForKey(WMUserDefaults
*database
, Bool value
,
644 proplist_t
WMGetUDSearchList(WMUserDefaults
*database
);
646 void WMSetUDSearchList(WMUserDefaults
*database
, proplist_t list
);
648 extern char *WMUserDefaultsDidChangeNotification
;
651 /*-------------------------------------------------------------------------*/
653 /* WMHost: host handling */
655 WMHost
* WMGetCurrentHost();
657 WMHost
* WMGetHostWithName(char* name
);
659 WMHost
* WMGetHostWithAddress(char* address
);
661 WMHost
* WMRetainHost(WMHost
*hPtr
);
663 void WMReleaseHost(WMHost
*hPtr
);
666 * Host cache management
667 * If enabled, only one object representing each host will be created, and
668 * a shared instance will be returned by all methods that return a host.
669 * Enabled by default.
671 void WMSetHostCacheEnabled(Bool flag
);
673 Bool
WMIsHostCacheEnabled();
675 void WMFlushHostCache();
678 * Compare hosts: Hosts are equal if they share at least one address
680 Bool
WMIsHostEqualToHost(WMHost
* hPtr
, WMHost
* anotherHost
);
684 * WMGetHostName() will return first name (official) if a host has several.
685 * WMGetHostNames() will return a R/O WMBag with all the names of the host.
687 char* WMGetHostName(WMHost
* hPtr
);
689 /* The returned bag is R/O. Do not modify it, and do not free it! */
690 WMBag
* WMGetHostNames(WMHost
* hPtr
);
694 * Addresses are represented as "Dotted Decimal" strings, e.g. "192.42.172.1"
695 * WMGetHostAddress() will return an arbitrary address if a host has several.
696 * WMGetHostAddresses() will return a R/O WMBag with all addresses of the host.
698 char* WMGetHostAddress(WMHost
* hPtr
);
700 /* The returned bag is R/O. Do not modify it, and do not free it! */
701 WMBag
* WMGetHostAddresses(WMHost
* hPtr
);
704 /*-------------------------------------------------------------------------*/
706 /* WMConnection functions */
708 WMConnection
* WMCreateConnectionAsServerAtAddress(char *host
, char *service
,
711 WMConnection
* WMCreateConnectionToAddress(char *host
, char *service
,
714 WMConnection
* WMCreateConnectionToAddressAndNotify(char *host
, char *service
,
717 void WMCloseConnection(WMConnection
*cPtr
);
719 void WMDestroyConnection(WMConnection
*cPtr
);
721 WMConnection
* WMAcceptConnection(WMConnection
*listener
);
723 /* Release the returned data! */
724 WMData
* WMGetConnectionAvailableData(WMConnection
*cPtr
);
726 int WMSendConnectionData(WMConnection
*cPtr
, WMData
*data
);
728 Bool
WMEnqueueConnectionData(WMConnection
*cPtr
, WMData
*data
);
730 #define WMFlushConnection(cPtr) WMSendConnectionData((cPtr), NULL)
732 void WMSetConnectionDelegate(WMConnection
*cPtr
, ConnectionDelegate
*delegate
);
734 /* Connection info */
736 char* WMGetConnectionAddress(WMConnection
*cPtr
);
738 char* WMGetConnectionService(WMConnection
*cPtr
);
740 char* WMGetConnectionProtocol(WMConnection
*cPtr
);
742 void WMSetConnectionNonBlocking(WMConnection
*cPtr
, Bool flag
);
744 void* WMGetConnectionClientData(WMConnection
*cPtr
);
746 void WMSetConnectionClientData(WMConnection
*cPtr
, void *data
);
748 unsigned int WMGetConnectionFlags(WMConnection
*cPtr
);
750 void WMSetConnectionFlags(WMConnection
*cPtr
, unsigned int flags
);
752 int WMGetConnectionSocket(WMConnection
*cPtr
);
754 WMConnectionState
WMGetConnectionState(WMConnection
*cPtr
);
756 WMConnectionTimeoutState
WMGetConnectionTimeoutState(WMConnection
*cPtr
);
759 * Passing timeout==0 in the SetTimeout functions below, will reset that
760 * timeout to its default value.
763 /* The default timeout inherited by all WMConnection operations, if none set */
764 void WMSetConnectionDefaultTimeout(unsigned int timeout
);
766 /* Global timeout for all WMConnection objects, for opening a new connection */
767 void WMSetConnectionOpenTimeout(unsigned int timeout
);
769 /* Connection specific timeout for sending out data */
770 void WMSetConnectionSendTimeout(WMConnection
*cPtr
, unsigned int timeout
);
773 /* Global variables */
775 extern int WCErrorCode
;
778 /*-------------------------------------------------------------------------*/
784 #endif /* __cplusplus */