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_Bag WMBag
;
120 typedef struct W_Data WMData
;
121 typedef struct W_HashTable WMHashTable
;
122 typedef struct W_UserDefaults WMUserDefaults
;
123 typedef struct W_Notification WMNotification
;
124 typedef struct W_NotificationQueue WMNotificationQueue
;
125 typedef struct W_Host WMHost
;
126 typedef struct W_Connection WMConnection
;
130 typedef void WMFreeDataProc(void *data
);
141 /* DO NOT ACCESS THE CONTENTS OF THIS STRUCT */
150 /* NULL is pointer hash */
151 unsigned (*hash
)(const void *);
152 /* NULL is pointer compare */
153 Bool (*keyIsEqual
)(const void *, const void *);
154 /* NULL does nothing */
155 void* (*retainKey
)(const void *);
156 /* NULL does nothing */
157 void (*releaseKey
)(const void *);
158 } WMHashTableCallbacks
;
161 typedef void *WMBagIterator
;
163 typedef struct W_BagFunctions
{
164 int (*getItemCount
)(WMBag
*self
);
165 int (*appendBag
)(WMBag
*self
, WMBag
*bag
);
166 int (*putInBag
)(WMBag
*self
, void *item
);
167 int (*insertInBag
)(WMBag
*self
, int index
, void *item
);
168 int (*removeFromBag
)(WMBag
*bag
, void *item
);
169 int (*eraseFromBag
)(WMBag
*bag
, int index
);
170 int (*deleteFromBag
)(WMBag
*bag
, int index
);
171 void *(*getFromBag
)(WMBag
*bag
, int index
);
172 int (*firstInBag
)(WMBag
*bag
, void *item
);
173 int (*countInBag
)(WMBag
*bag
, void *item
);
174 void *(*replaceInBag
)(WMBag
*bag
, int index
, void *item
);
175 int (*sortBag
)(WMBag
*bag
, int (*comparer
)(const void*, const void*));
176 void (*emptyBag
)(WMBag
*bag
);
177 void (*freeBag
)(WMBag
*bag
);
178 void (*mapBag
)(WMBag
*bag
, void (*function
)(void*, void*), void *data
);
179 int (*findInBag
)(WMBag
*bag
, int (*match
)(void*, void*), void *cdata
);
180 void *(*first
)(WMBag
*bag
, WMBagIterator
*ptr
);
181 void *(*last
)(WMBag
*bag
, WMBagIterator
*ptr
);
182 void *(*next
)(WMBag
*bag
, WMBagIterator
*ptr
);
183 void *(*previous
)(WMBag
*bag
, WMBagIterator
*ptr
);
184 void *(*iteratorAtIndex
)(WMBag
*bag
, int index
, WMBagIterator
*ptr
);
185 int (*indexForIterator
)(WMBag
*bag
, WMBagIterator ptr
);
192 void (*destructor
)(void *item
);
201 char character
; /* the escape character */
202 char *value
; /* value to place */
207 /* The connection callbacks */
208 typedef struct ConnectionDelegate
{
211 void (*didCatchException
)(struct ConnectionDelegate
*self
,
214 void (*didDie
)(struct ConnectionDelegate
*self
, WMConnection
*cPtr
);
216 void (*didInitialize
)(struct ConnectionDelegate
*self
, WMConnection
*cPtr
);
218 void (*didReceiveInput
)(struct ConnectionDelegate
*self
, WMConnection
*cPtr
);
220 void (*didTimeout
)(struct ConnectionDelegate
*self
, WMConnection
*cPtr
);
222 } ConnectionDelegate
;
225 typedef void WMNotificationObserverAction(void *observerData
,
226 WMNotification
*notification
);
230 /*......................................................................*/
232 typedef void (waborthandler
)(int);
234 waborthandler
*wsetabort(waborthandler
*);
237 /* don't free the returned string */
238 char *wstrerror(int errnum
);
240 void wfatal(const char *msg
, ...);
241 void wwarning(const char *msg
, ...);
242 void wsyserror(const char *msg
, ...);
243 void wsyserrorwithcode(int error
, const char *msg
, ...);
245 char *wfindfile(char *paths
, char *file
);
247 char *wfindfileinlist(char **path_list
, char *file
);
249 char *wfindfileinarray(proplist_t array
, char *file
);
251 char *wexpandpath(char *path
);
253 /* don't free the returned string */
256 void *wmalloc(size_t size
);
257 void *wrealloc(void *ptr
, size_t newsize
);
258 void wfree(void *ptr
);
261 void wrelease(void *ptr
);
262 void *wretain(void *ptr
);
264 char *wstrdup(char *str
);
266 char *wstrappend(char *dst
, char *src
);
268 char *wusergnusteppath();
270 char *wdefaultspathfordomain(char *domain
);
272 void wusleep(unsigned int microsec
);
275 int wsprintesc(char *buffer
, int length
, char *format
, WMSEscapes
**escapes
,
279 /*......................................................................*/
281 /* This function is used _only_ if you create a NON-GUI program.
282 * For GUI based programs use WMNextEvent()/WMHandleEvent() instead.
283 * This function will handle all input/timer/idle events, then return.
286 void WHandleEvents();
288 /*......................................................................*/
291 WMHashTable
*WMCreateHashTable(WMHashTableCallbacks callbacks
);
293 void WMFreeHashTable(WMHashTable
*table
);
295 void WMResetHashTable(WMHashTable
*table
);
297 void *WMHashGet(WMHashTable
*table
, const void *key
);
299 /* put data in table, replacing already existing data and returning
301 void *WMHashInsert(WMHashTable
*table
, void *key
, void *data
);
303 void WMHashRemove(WMHashTable
*table
, const void *key
);
305 /* warning: do not manipulate the table while using these functions */
306 WMHashEnumerator
WMEnumerateHashTable(WMHashTable
*table
);
308 void *WMNextHashEnumeratorItem(WMHashEnumerator
*enumerator
);
310 unsigned WMCountHashTable(WMHashTable
*table
);
315 /* some predefined callback sets */
317 extern const WMHashTableCallbacks WMIntHashCallbacks
;
318 /* sizeof(keys) are <= sizeof(void*) */
320 extern const WMHashTableCallbacks WMStringHashCallbacks
;
321 /* keys are strings. Strings will be copied with wstrdup()
322 * and freed with wfree() */
324 extern const WMHashTableCallbacks WMStringPointerHashCallbacks
;
325 /* keys are strings, bug they are not copied */
328 /*......................................................................*/
332 * Array bags use an array to store the elements.
333 * Item indexes may be any integer number.
336 * Fast [O(1)] access to elements
337 * Fast [O(1)] push/pop
340 * A little slower [O(n)] for insertion/deletion of elements that
342 * Element indexes with large difference will cause large holes
345 WMBag
* WMCreateArrayBag(int initialSize
);
346 WMBag
* WMCreateArrayBagWithDestructor(int initialSize
,
347 void (*destructor
)(void*));
350 * Tree bags use a red-black tree for storage.
351 * Item indexes may be any integer number.
354 * O(lg n) insertion/deletion/search
355 * Good for large numbers of elements with sparse indexes
358 * O(lg n) insertion/deletion/search
359 * Slow for storing small numbers of elements
361 WMBag
*WMCreateTreeBag(void);
362 WMBag
*WMCreateTreeBagWithDestructor(void (*destructor
)(void*));
365 #define WMCreateArrayBag(a) WMCreateTreeBag()
366 #define WMCreateArrayBagWithDestructor(a,d) WMCreateTreeBagWithDestructor(d)
368 #define WMCreateBag(size) WMCreateTreeBag()
370 #define WMCreateBagWithDestructor(size, d) WMCreateTreeBagWithDestructor(d)
372 #define WMGetBagItemCount(bag) bag->func.getItemCount(bag)
374 #define WMAppendBag(bag, other) bag->func.appendBag(bag, other)
376 #define WMPutInBag(bag, item) bag->func.putInBag(bag, item)
378 /* insert will increment the index of elements after it by 1 */
379 #define WMInsertInBag(bag, index, item) bag->func.insertInBag(bag, index, item)
382 /* erase will remove the element from the bag,
383 * but will keep the index of the other elements unchanged */
384 #define WMEraseFromBag(bag, index) bag->func.eraseFromBag(bag, index)
386 /* delete and remove will remove the elements and cause the elements
387 * after them to decrement their indexes by 1 */
388 #define WMRemoveFromBag(bag, item) bag->func.removeFromBag(bag, item)
390 #define WMDeleteFromBag(bag, index) bag->func.deleteFromBag(bag, index)
392 #define WMGetFromBag(bag, index) bag->func.getFromBag(bag, index)
394 #define WMCountInBag(bag, item) bag->func.countInBag(bag, item)
396 #define WMReplaceInBag(bag, index, item) bag->func.replaceInBag(bag, index, item)
397 #define WMSetInBag(bag, index, item) bag->func.replaceInBag(bag, index, item)
399 /* comparer must return:
404 #define WMSortBag(bag, comparer) bag->func.sortBag(bag, comparer)
406 #define WMEmptyBag(bag) bag->func.emptyBag(bag)
408 #define WMFreeBag(bag) bag->func.freeBag(bag)
410 #define WMMapBag(bag, function, cdata) bag->func.mapBag(bag, function, cdata)
412 #define WMGetFirstInBag(bag, item) bag->func.firstInBag(bag, item)
414 #define WMCountInBag(bag, item) bag->func.countInBag(bag, item)
416 #define WMFindInBag(bag, match, cdata) bag->func.findInBag(bag, match, cdata)
418 #define WMBagFirst(bag, ptr) bag->func.first(bag, ptr)
420 #define WMBagLast(bag, ptr) bag->func.last(bag, ptr)
422 #define WMBagNext(bag, ptr) bag->func.next(bag, ptr)
424 #define WMBagPrevious(bag, ptr) bag->func.previous(bag, ptr)
426 #define WMBagIteratorAtIndex(bag, index, ptr) bag->func.iteratorAtIndex(bag, index, ptr)
428 #define WMBagIndexForIterator(bag, ptr) bag->func.indexForIterator(bag, ptr)
432 #define WM_ITERATE_BAG(bag, var, i) \
433 for (var = WMBagFirst(bag, &(i)); (i) != NULL; \
434 var = WMBagNext(bag, &(i)))
436 #define WM_ETARETI_BAG(bag, var, i) \
437 for (var = WMBagLast(bag, &(i)); (i) != NULL; \
438 var = WMBagPrevious(bag, &(i)))
442 /*-------------------------------------------------------------------------*/
444 /* WMData handling */
446 /* Creating/destroying data */
448 WMData
* WMCreateDataWithCapacity(unsigned capacity
);
450 WMData
* WMCreateDataWithLength(unsigned length
);
452 WMData
* WMCreateDataWithBytes(void *bytes
, unsigned length
);
454 /* destructor is a function called to free the data when releasing the data
455 * object, or NULL if no freeing of data is necesary. */
456 WMData
* WMCreateDataWithBytesNoCopy(void *bytes
, unsigned length
,
457 WMFreeDataProc
*destructor
);
459 WMData
* WMCreateDataWithData(WMData
*aData
);
461 WMData
* WMRetainData(WMData
*aData
);
463 void WMReleaseData(WMData
*aData
);
465 /* Adjusting capacity */
467 void WMSetDataCapacity(WMData
*aData
, unsigned capacity
);
469 void WMSetDataLength(WMData
*aData
, unsigned length
);
471 void WMIncreaseDataLengthBy(WMData
*aData
, unsigned extraLength
);
475 const void* WMDataBytes(WMData
*aData
);
477 void WMGetDataBytes(WMData
*aData
, void *buffer
);
479 void WMGetDataBytesWithLength(WMData
*aData
, void *buffer
, unsigned length
);
481 void WMGetDataBytesWithRange(WMData
*aData
, void *buffer
, WMRange aRange
);
483 WMData
* WMGetSubdataWithRange(WMData
*aData
, WMRange aRange
);
487 Bool
WMIsDataEqualToData(WMData
*aData
, WMData
*anotherData
);
489 unsigned WMGetDataLength(WMData
*aData
);
493 void WMAppendDataBytes(WMData
*aData
, void *bytes
, unsigned length
);
495 void WMAppendData(WMData
*aData
, WMData
*anotherData
);
499 void WMReplaceDataBytesInRange(WMData
*aData
, WMRange aRange
, void *bytes
);
501 void WMResetDataBytesInRange(WMData
*aData
, WMRange aRange
);
503 void WMSetData(WMData
*aData
, WMData
*anotherData
);
506 void WMSetDataFormat(WMData
*aData
, unsigned format
);
508 unsigned WMGetDataFormat(WMData
*aData
);
512 /*--------------------------------------------------------------------------*/
515 WMNotification
*WMCreateNotification(char *name
, void *object
, void *clientData
);
517 void WMReleaseNotification(WMNotification
*notification
);
519 WMNotification
*WMRetainNotification(WMNotification
*notification
);
521 void *WMGetNotificationClientData(WMNotification
*notification
);
523 void *WMGetNotificationObject(WMNotification
*notification
);
525 char *WMGetNotificationName(WMNotification
*notification
);
528 void WMAddNotificationObserver(WMNotificationObserverAction
*observerAction
,
529 void *observer
, char *name
, void *object
);
531 void WMPostNotification(WMNotification
*notification
);
533 void WMRemoveNotificationObserver(void *observer
);
535 void WMRemoveNotificationObserverWithName(void *observer
, char *name
,
538 void WMPostNotificationName(char *name
, void *object
, void *clientData
);
540 WMNotificationQueue
*WMGetDefaultNotificationQueue(void);
542 WMNotificationQueue
*WMCreateNotificationQueue(void);
544 void WMDequeueNotificationMatching(WMNotificationQueue
*queue
,
545 WMNotification
*notification
,
548 void WMEnqueueNotification(WMNotificationQueue
*queue
,
549 WMNotification
*notification
,
550 WMPostingStyle postingStyle
);
552 void WMEnqueueCoalesceNotification(WMNotificationQueue
*queue
,
553 WMNotification
*notification
,
554 WMPostingStyle postingStyle
,
555 unsigned coalesceMask
);
558 /*......................................................................*/
560 WMUserDefaults
*WMGetStandardUserDefaults(void);
562 WMUserDefaults
*WMGetDefaultsFromPath(char *path
);
564 void WMSynchronizeUserDefaults(WMUserDefaults
*database
);
566 void WMSaveUserDefaults(WMUserDefaults
*database
);
568 void WMEnableUDPeriodicSynchronization(WMUserDefaults
*database
, Bool enable
);
570 /* Returns a PLArray with all keys in the user defaults database.
571 * Free the returned array with PLRelease() when no longer needed,
572 * but do not free the elements of the array! They're just references. */
573 proplist_t
WMGetUDAllKeys(WMUserDefaults
*database
);
575 proplist_t
WMGetUDObjectForKey(WMUserDefaults
*database
, char *defaultName
);
577 void WMSetUDObjectForKey(WMUserDefaults
*database
, proplist_t object
,
580 void WMRemoveUDObjectForKey(WMUserDefaults
*database
, char *defaultName
);
582 char *WMGetUDStringForKey(WMUserDefaults
*database
, char *defaultName
);
584 int WMGetUDIntegerForKey(WMUserDefaults
*database
, char *defaultName
);
586 float WMGetUDFloatForKey(WMUserDefaults
*database
, char *defaultName
);
588 Bool
WMGetUDBoolForKey(WMUserDefaults
*database
, char *defaultName
);
590 void WMSetUDStringForKey(WMUserDefaults
*database
, char *value
,
593 void WMSetUDIntegerForKey(WMUserDefaults
*database
, int value
,
596 void WMSetUDFloatForKey(WMUserDefaults
*database
, float value
,
599 void WMSetUDBoolForKey(WMUserDefaults
*database
, Bool value
,
602 proplist_t
WMGetUDSearchList(WMUserDefaults
*database
);
604 void WMSetUDSearchList(WMUserDefaults
*database
, proplist_t list
);
606 extern char *WMUserDefaultsDidChangeNotification
;
609 /*-------------------------------------------------------------------------*/
611 /* WMHost: host handling */
613 WMHost
* WMGetCurrentHost();
615 WMHost
* WMGetHostWithName(char* name
);
617 WMHost
* WMGetHostWithAddress(char* address
);
619 WMHost
* WMRetainHost(WMHost
*hPtr
);
621 void WMReleaseHost(WMHost
*hPtr
);
624 * Host cache management
625 * If enabled, only one object representing each host will be created, and
626 * a shared instance will be returned by all methods that return a host.
627 * Enabled by default.
629 void WMSetHostCacheEnabled(Bool flag
);
631 Bool
WMIsHostCacheEnabled();
633 void WMFlushHostCache();
636 * Compare hosts: Hosts are equal if they share at least one address
638 Bool
WMIsHostEqualToHost(WMHost
* hPtr
, WMHost
* anotherHost
);
642 * WMGetHostName() will return first name (official) if a host has several.
643 * WMGetHostNames() will return a R/O WMBag with all the names of the host.
645 char* WMGetHostName(WMHost
* hPtr
);
647 /* The returned bag is R/O. Do not modify it, and do not free it! */
648 WMBag
* WMGetHostNames(WMHost
* hPtr
);
652 * Addresses are represented as "Dotted Decimal" strings, e.g. "192.42.172.1"
653 * WMGetHostAddress() will return an arbitrary address if a host has several.
654 * WMGetHostAddresses() will return a R/O WMBag with all addresses of the host.
656 char* WMGetHostAddress(WMHost
* hPtr
);
658 /* The returned bag is R/O. Do not modify it, and do not free it! */
659 WMBag
* WMGetHostAddresses(WMHost
* hPtr
);
662 /*-------------------------------------------------------------------------*/
664 /* WMConnection functions */
666 WMConnection
* WMCreateConnectionAsServerAtAddress(char *host
, char *service
,
669 WMConnection
* WMCreateConnectionToAddress(char *host
, char *service
,
672 WMConnection
* WMCreateConnectionToAddressAndNotify(char *host
, char *service
,
675 void WMCloseConnection(WMConnection
*cPtr
);
677 void WMDestroyConnection(WMConnection
*cPtr
);
679 WMConnection
* WMAcceptConnection(WMConnection
*listener
);
681 /* Release the returned data! */
682 WMData
* WMGetConnectionAvailableData(WMConnection
*cPtr
);
684 int WMSendConnectionData(WMConnection
*cPtr
, WMData
*data
);
686 Bool
WMEnqueueConnectionData(WMConnection
*cPtr
, WMData
*data
);
688 #define WMFlushConnection(cPtr) WMSendConnectionData((cPtr), NULL)
690 void WMSetConnectionDelegate(WMConnection
*cPtr
, ConnectionDelegate
*delegate
);
692 /* Connection info */
694 char* WMGetConnectionAddress(WMConnection
*cPtr
);
696 char* WMGetConnectionService(WMConnection
*cPtr
);
698 char* WMGetConnectionProtocol(WMConnection
*cPtr
);
700 void WMSetConnectionNonBlocking(WMConnection
*cPtr
, Bool flag
);
702 void* WMGetConnectionClientData(WMConnection
*cPtr
);
704 void WMSetConnectionClientData(WMConnection
*cPtr
, void *data
);
706 unsigned int WMGetConnectionFlags(WMConnection
*cPtr
);
708 void WMSetConnectionFlags(WMConnection
*cPtr
, unsigned int flags
);
710 int WMGetConnectionSocket(WMConnection
*cPtr
);
712 WMConnectionState
WMGetConnectionState(WMConnection
*cPtr
);
714 WMConnectionTimeoutState
WMGetConnectionTimeoutState(WMConnection
*cPtr
);
717 * Passing timeout==0 in the SetTimeout functions below, will reset that
718 * timeout to its default value.
721 /* The default timeout inherited by all WMConnection operations, if none set */
722 void WMSetConnectionDefaultTimeout(unsigned int timeout
);
724 /* Global timeout for all WMConnection objects, for opening a new connection */
725 void WMSetConnectionOpenTimeout(unsigned int timeout
);
727 /* Connection specific timeout for sending out data */
728 void WMSetConnectionSendTimeout(WMConnection
*cPtr
, unsigned int timeout
);
731 /* Global variables */
733 extern int WCErrorCode
;
736 /*-------------------------------------------------------------------------*/
742 #endif /* __cplusplus */