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
;
164 typedef struct W_BagFunctions
{
165 int (*getItemCount
)(WMBag
*self
);
166 int (*appendBag
)(WMBag
*self
, WMBag
*bag
);
167 int (*putInBag
)(WMBag
*self
, void *item
);
168 int (*insertInBag
)(WMBag
*self
, int index
, void *item
);
169 int (*removeFromBag
)(WMBag
*bag
, void *item
);
170 int (*eraseFromBag
)(WMBag
*bag
, int index
);
171 int (*deleteFromBag
)(WMBag
*bag
, int index
);
172 void *(*getFromBag
)(WMBag
*bag
, int index
);
173 int (*firstInBag
)(WMBag
*bag
, void *item
);
174 int (*countInBag
)(WMBag
*bag
, void *item
);
175 void *(*replaceInBag
)(WMBag
*bag
, int index
, void *item
);
176 int (*sortBag
)(WMBag
*bag
, int (*comparer
)(const void*, const void*));
177 void (*emptyBag
)(WMBag
*bag
);
178 void (*freeBag
)(WMBag
*bag
);
179 void (*mapBag
)(WMBag
*bag
, void (*function
)(void*, void*), void *data
);
180 int (*findInBag
)(WMBag
*bag
, int (*match
)(void*, void*), void *cdata
);
181 void *(*first
)(WMBag
*bag
, WMBagIterator
*ptr
);
182 void *(*last
)(WMBag
*bag
, WMBagIterator
*ptr
);
183 void *(*next
)(WMBag
*bag
, WMBagIterator
*ptr
);
184 void *(*previous
)(WMBag
*bag
, WMBagIterator
*ptr
);
185 void *(*iteratorAtIndex
)(WMBag
*bag
, int index
, WMBagIterator
*ptr
);
186 int (*indexForIterator
)(WMBag
*bag
, WMBagIterator ptr
);
193 void (*destructor
)(void *item
);
202 char character
; /* the escape character */
203 char *value
; /* value to place */
208 /* The connection callbacks */
209 typedef struct ConnectionDelegate
{
212 void (*didCatchException
)(struct ConnectionDelegate
*self
,
215 void (*didDie
)(struct ConnectionDelegate
*self
, WMConnection
*cPtr
);
217 void (*didInitialize
)(struct ConnectionDelegate
*self
, WMConnection
*cPtr
);
219 void (*didReceiveInput
)(struct ConnectionDelegate
*self
, WMConnection
*cPtr
);
221 void (*didTimeout
)(struct ConnectionDelegate
*self
, WMConnection
*cPtr
);
223 } ConnectionDelegate
;
226 typedef void WMNotificationObserverAction(void *observerData
,
227 WMNotification
*notification
);
231 /*......................................................................*/
233 typedef void (waborthandler
)(int);
235 waborthandler
*wsetabort(waborthandler
*);
238 /* don't free the returned string */
239 char *wstrerror(int errnum
);
241 void wfatal(const char *msg
, ...);
242 void wwarning(const char *msg
, ...);
243 void wsyserror(const char *msg
, ...);
244 void wsyserrorwithcode(int error
, const char *msg
, ...);
246 char *wfindfile(char *paths
, char *file
);
248 char *wfindfileinlist(char **path_list
, char *file
);
250 char *wfindfileinarray(proplist_t array
, char *file
);
252 char *wexpandpath(char *path
);
254 /* don't free the returned string */
257 void *wmalloc(size_t size
);
258 void *wrealloc(void *ptr
, size_t newsize
);
259 void wfree(void *ptr
);
262 void wrelease(void *ptr
);
263 void *wretain(void *ptr
);
265 char *wstrdup(char *str
);
267 char *wstrappend(char *dst
, char *src
);
272 void wtokensplit(char *command
, char ***argv
, int *argc
);
274 char *wtokenjoin(char **list
, int count
);
276 void wtokenfree(char **tokens
, int count
);
278 char *wtrimspace(char *s
);
283 char *wusergnusteppath();
285 char *wdefaultspathfordomain(char *domain
);
287 void wusleep(unsigned int microsec
);
290 int wsprintesc(char *buffer
, int length
, char *format
, WMSEscapes
**escapes
,
294 /*......................................................................*/
296 /* This function is used _only_ if you create a NON-GUI program.
297 * For GUI based programs use WMNextEvent()/WMHandleEvent() instead.
298 * This function will handle all input/timer/idle events, then return.
301 void WHandleEvents();
303 /*......................................................................*/
306 WMHashTable
*WMCreateHashTable(WMHashTableCallbacks callbacks
);
308 void WMFreeHashTable(WMHashTable
*table
);
310 void WMResetHashTable(WMHashTable
*table
);
312 void *WMHashGet(WMHashTable
*table
, const void *key
);
314 /* put data in table, replacing already existing data and returning
316 void *WMHashInsert(WMHashTable
*table
, void *key
, void *data
);
318 void WMHashRemove(WMHashTable
*table
, const void *key
);
320 /* warning: do not manipulate the table while using these functions */
321 WMHashEnumerator
WMEnumerateHashTable(WMHashTable
*table
);
323 void *WMNextHashEnumeratorItem(WMHashEnumerator
*enumerator
);
325 unsigned WMCountHashTable(WMHashTable
*table
);
330 /* some predefined callback sets */
332 extern const WMHashTableCallbacks WMIntHashCallbacks
;
333 /* sizeof(keys) are <= sizeof(void*) */
335 extern const WMHashTableCallbacks WMStringHashCallbacks
;
336 /* keys are strings. Strings will be copied with wstrdup()
337 * and freed with wfree() */
339 extern const WMHashTableCallbacks WMStringPointerHashCallbacks
;
340 /* keys are strings, bug they are not copied */
343 /*......................................................................*/
347 * Array bags use an array to store the elements.
348 * Item indexes may be any integer number.
351 * Fast [O(1)] access to elements
352 * Fast [O(1)] push/pop
355 * A little slower [O(n)] for insertion/deletion of elements that
357 * Element indexes with large difference will cause large holes
360 WMBag
* WMCreateArrayBag(int initialSize
);
361 WMBag
* WMCreateArrayBagWithDestructor(int initialSize
,
362 void (*destructor
)(void*));
365 * Tree bags use a red-black tree for storage.
366 * Item indexes may be any integer number.
369 * O(lg n) insertion/deletion/search
370 * Good for large numbers of elements with sparse indexes
373 * O(lg n) insertion/deletion/search
374 * Slow for storing small numbers of elements
376 WMBag
*WMCreateTreeBag(void);
377 WMBag
*WMCreateTreeBagWithDestructor(void (*destructor
)(void*));
380 #define WMCreateArrayBag(a) WMCreateTreeBag()
381 #define WMCreateArrayBagWithDestructor(a,d) WMCreateTreeBagWithDestructor(d)
383 #define WMCreateBag(size) WMCreateTreeBag()
385 #define WMCreateBagWithDestructor(size, d) WMCreateTreeBagWithDestructor(d)
387 #define WMGetBagItemCount(bag) bag->func.getItemCount(bag)
389 #define WMAppendBag(bag, other) bag->func.appendBag(bag, other)
391 #define WMPutInBag(bag, item) bag->func.putInBag(bag, item)
393 /* insert will increment the index of elements after it by 1 */
394 #define WMInsertInBag(bag, index, item) bag->func.insertInBag(bag, index, item)
397 /* erase will remove the element from the bag,
398 * but will keep the index of the other elements unchanged */
399 #define WMEraseFromBag(bag, index) bag->func.eraseFromBag(bag, index)
401 /* delete and remove will remove the elements and cause the elements
402 * after them to decrement their indexes by 1 */
403 #define WMRemoveFromBag(bag, item) bag->func.removeFromBag(bag, item)
405 #define WMDeleteFromBag(bag, index) bag->func.deleteFromBag(bag, index)
407 #define WMGetFromBag(bag, index) bag->func.getFromBag(bag, index)
409 #define WMCountInBag(bag, item) bag->func.countInBag(bag, item)
411 #define WMReplaceInBag(bag, index, item) bag->func.replaceInBag(bag, index, item)
412 #define WMSetInBag(bag, index, item) bag->func.replaceInBag(bag, index, item)
414 /* comparer must return:
419 #define WMSortBag(bag, comparer) bag->func.sortBag(bag, comparer)
421 #define WMEmptyBag(bag) bag->func.emptyBag(bag)
423 #define WMFreeBag(bag) bag->func.freeBag(bag)
425 #define WMMapBag(bag, function, cdata) bag->func.mapBag(bag, function, cdata)
427 #define WMGetFirstInBag(bag, item) bag->func.firstInBag(bag, item)
429 #define WMCountInBag(bag, item) bag->func.countInBag(bag, item)
431 #define WMFindInBag(bag, match, cdata) bag->func.findInBag(bag, match, cdata)
433 #define WMBagFirst(bag, ptr) bag->func.first(bag, ptr)
435 #define WMBagLast(bag, ptr) bag->func.last(bag, ptr)
437 #define WMBagNext(bag, ptr) bag->func.next(bag, ptr)
439 #define WMBagPrevious(bag, ptr) bag->func.previous(bag, ptr)
441 #define WMBagIteratorAtIndex(bag, index, ptr) bag->func.iteratorAtIndex(bag, index, ptr)
443 #define WMBagIndexForIterator(bag, ptr) bag->func.indexForIterator(bag, ptr)
447 #define WM_ITERATE_BAG(bag, var, i) \
448 for (var = WMBagFirst(bag, &(i)); (i) != NULL; \
449 var = WMBagNext(bag, &(i)))
451 #define WM_ETARETI_BAG(bag, var, i) \
452 for (var = WMBagLast(bag, &(i)); (i) != NULL; \
453 var = WMBagPrevious(bag, &(i)))
457 /*-------------------------------------------------------------------------*/
459 /* WMData handling */
461 /* Creating/destroying data */
463 WMData
* WMCreateDataWithCapacity(unsigned capacity
);
465 WMData
* WMCreateDataWithLength(unsigned length
);
467 WMData
* WMCreateDataWithBytes(void *bytes
, unsigned length
);
469 /* destructor is a function called to free the data when releasing the data
470 * object, or NULL if no freeing of data is necesary. */
471 WMData
* WMCreateDataWithBytesNoCopy(void *bytes
, unsigned length
,
472 WMFreeDataProc
*destructor
);
474 WMData
* WMCreateDataWithData(WMData
*aData
);
476 WMData
* WMRetainData(WMData
*aData
);
478 void WMReleaseData(WMData
*aData
);
480 /* Adjusting capacity */
482 void WMSetDataCapacity(WMData
*aData
, unsigned capacity
);
484 void WMSetDataLength(WMData
*aData
, unsigned length
);
486 void WMIncreaseDataLengthBy(WMData
*aData
, unsigned extraLength
);
490 const void* WMDataBytes(WMData
*aData
);
492 void WMGetDataBytes(WMData
*aData
, void *buffer
);
494 void WMGetDataBytesWithLength(WMData
*aData
, void *buffer
, unsigned length
);
496 void WMGetDataBytesWithRange(WMData
*aData
, void *buffer
, WMRange aRange
);
498 WMData
* WMGetSubdataWithRange(WMData
*aData
, WMRange aRange
);
502 Bool
WMIsDataEqualToData(WMData
*aData
, WMData
*anotherData
);
504 unsigned WMGetDataLength(WMData
*aData
);
508 void WMAppendDataBytes(WMData
*aData
, void *bytes
, unsigned length
);
510 void WMAppendData(WMData
*aData
, WMData
*anotherData
);
514 void WMReplaceDataBytesInRange(WMData
*aData
, WMRange aRange
, void *bytes
);
516 void WMResetDataBytesInRange(WMData
*aData
, WMRange aRange
);
518 void WMSetData(WMData
*aData
, WMData
*anotherData
);
521 void WMSetDataFormat(WMData
*aData
, unsigned format
);
523 unsigned WMGetDataFormat(WMData
*aData
);
527 /*--------------------------------------------------------------------------*/
530 WMNotification
*WMCreateNotification(char *name
, void *object
, void *clientData
);
532 void WMReleaseNotification(WMNotification
*notification
);
534 WMNotification
*WMRetainNotification(WMNotification
*notification
);
536 void *WMGetNotificationClientData(WMNotification
*notification
);
538 void *WMGetNotificationObject(WMNotification
*notification
);
540 char *WMGetNotificationName(WMNotification
*notification
);
543 void WMAddNotificationObserver(WMNotificationObserverAction
*observerAction
,
544 void *observer
, char *name
, void *object
);
546 void WMPostNotification(WMNotification
*notification
);
548 void WMRemoveNotificationObserver(void *observer
);
550 void WMRemoveNotificationObserverWithName(void *observer
, char *name
,
553 void WMPostNotificationName(char *name
, void *object
, void *clientData
);
555 WMNotificationQueue
*WMGetDefaultNotificationQueue(void);
557 WMNotificationQueue
*WMCreateNotificationQueue(void);
559 void WMDequeueNotificationMatching(WMNotificationQueue
*queue
,
560 WMNotification
*notification
,
563 void WMEnqueueNotification(WMNotificationQueue
*queue
,
564 WMNotification
*notification
,
565 WMPostingStyle postingStyle
);
567 void WMEnqueueCoalesceNotification(WMNotificationQueue
*queue
,
568 WMNotification
*notification
,
569 WMPostingStyle postingStyle
,
570 unsigned coalesceMask
);
573 /*......................................................................*/
575 WMUserDefaults
*WMGetStandardUserDefaults(void);
577 WMUserDefaults
*WMGetDefaultsFromPath(char *path
);
579 void WMSynchronizeUserDefaults(WMUserDefaults
*database
);
581 void WMSaveUserDefaults(WMUserDefaults
*database
);
583 void WMEnableUDPeriodicSynchronization(WMUserDefaults
*database
, Bool enable
);
585 /* Returns a PLArray with all keys in the user defaults database.
586 * Free the returned array with PLRelease() when no longer needed,
587 * but do not free the elements of the array! They're just references. */
588 proplist_t
WMGetUDAllKeys(WMUserDefaults
*database
);
590 proplist_t
WMGetUDObjectForKey(WMUserDefaults
*database
, char *defaultName
);
592 void WMSetUDObjectForKey(WMUserDefaults
*database
, proplist_t object
,
595 void WMRemoveUDObjectForKey(WMUserDefaults
*database
, char *defaultName
);
597 char *WMGetUDStringForKey(WMUserDefaults
*database
, char *defaultName
);
599 int WMGetUDIntegerForKey(WMUserDefaults
*database
, char *defaultName
);
601 float WMGetUDFloatForKey(WMUserDefaults
*database
, char *defaultName
);
603 Bool
WMGetUDBoolForKey(WMUserDefaults
*database
, char *defaultName
);
605 void WMSetUDStringForKey(WMUserDefaults
*database
, char *value
,
608 void WMSetUDIntegerForKey(WMUserDefaults
*database
, int value
,
611 void WMSetUDFloatForKey(WMUserDefaults
*database
, float value
,
614 void WMSetUDBoolForKey(WMUserDefaults
*database
, Bool value
,
617 proplist_t
WMGetUDSearchList(WMUserDefaults
*database
);
619 void WMSetUDSearchList(WMUserDefaults
*database
, proplist_t list
);
621 extern char *WMUserDefaultsDidChangeNotification
;
624 /*-------------------------------------------------------------------------*/
626 /* WMHost: host handling */
628 WMHost
* WMGetCurrentHost();
630 WMHost
* WMGetHostWithName(char* name
);
632 WMHost
* WMGetHostWithAddress(char* address
);
634 WMHost
* WMRetainHost(WMHost
*hPtr
);
636 void WMReleaseHost(WMHost
*hPtr
);
639 * Host cache management
640 * If enabled, only one object representing each host will be created, and
641 * a shared instance will be returned by all methods that return a host.
642 * Enabled by default.
644 void WMSetHostCacheEnabled(Bool flag
);
646 Bool
WMIsHostCacheEnabled();
648 void WMFlushHostCache();
651 * Compare hosts: Hosts are equal if they share at least one address
653 Bool
WMIsHostEqualToHost(WMHost
* hPtr
, WMHost
* anotherHost
);
657 * WMGetHostName() will return first name (official) if a host has several.
658 * WMGetHostNames() will return a R/O WMBag with all the names of the host.
660 char* WMGetHostName(WMHost
* hPtr
);
662 /* The returned bag is R/O. Do not modify it, and do not free it! */
663 WMBag
* WMGetHostNames(WMHost
* hPtr
);
667 * Addresses are represented as "Dotted Decimal" strings, e.g. "192.42.172.1"
668 * WMGetHostAddress() will return an arbitrary address if a host has several.
669 * WMGetHostAddresses() will return a R/O WMBag with all addresses of the host.
671 char* WMGetHostAddress(WMHost
* hPtr
);
673 /* The returned bag is R/O. Do not modify it, and do not free it! */
674 WMBag
* WMGetHostAddresses(WMHost
* hPtr
);
677 /*-------------------------------------------------------------------------*/
679 /* WMConnection functions */
681 WMConnection
* WMCreateConnectionAsServerAtAddress(char *host
, char *service
,
684 WMConnection
* WMCreateConnectionToAddress(char *host
, char *service
,
687 WMConnection
* WMCreateConnectionToAddressAndNotify(char *host
, char *service
,
690 void WMCloseConnection(WMConnection
*cPtr
);
692 void WMDestroyConnection(WMConnection
*cPtr
);
694 WMConnection
* WMAcceptConnection(WMConnection
*listener
);
696 /* Release the returned data! */
697 WMData
* WMGetConnectionAvailableData(WMConnection
*cPtr
);
699 int WMSendConnectionData(WMConnection
*cPtr
, WMData
*data
);
701 Bool
WMEnqueueConnectionData(WMConnection
*cPtr
, WMData
*data
);
703 #define WMFlushConnection(cPtr) WMSendConnectionData((cPtr), NULL)
705 void WMSetConnectionDelegate(WMConnection
*cPtr
, ConnectionDelegate
*delegate
);
707 /* Connection info */
709 char* WMGetConnectionAddress(WMConnection
*cPtr
);
711 char* WMGetConnectionService(WMConnection
*cPtr
);
713 char* WMGetConnectionProtocol(WMConnection
*cPtr
);
715 void WMSetConnectionNonBlocking(WMConnection
*cPtr
, Bool flag
);
717 void* WMGetConnectionClientData(WMConnection
*cPtr
);
719 void WMSetConnectionClientData(WMConnection
*cPtr
, void *data
);
721 unsigned int WMGetConnectionFlags(WMConnection
*cPtr
);
723 void WMSetConnectionFlags(WMConnection
*cPtr
, unsigned int flags
);
725 int WMGetConnectionSocket(WMConnection
*cPtr
);
727 WMConnectionState
WMGetConnectionState(WMConnection
*cPtr
);
729 WMConnectionTimeoutState
WMGetConnectionTimeoutState(WMConnection
*cPtr
);
732 * Passing timeout==0 in the SetTimeout functions below, will reset that
733 * timeout to its default value.
736 /* The default timeout inherited by all WMConnection operations, if none set */
737 void WMSetConnectionDefaultTimeout(unsigned int timeout
);
739 /* Global timeout for all WMConnection objects, for opening a new connection */
740 void WMSetConnectionOpenTimeout(unsigned int timeout
);
742 /* Connection specific timeout for sending out data */
743 void WMSetConnectionSendTimeout(WMConnection
*cPtr
, unsigned int timeout
);
746 /* Global variables */
748 extern int WCErrorCode
;
751 /*-------------------------------------------------------------------------*/
757 #endif /* __cplusplus */