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
);
271 void wtokensplit(char *command
, char ***argv
, int *argc
);
273 char *wtokenjoin(char **list
, int count
);
275 void wtokenfree(char **tokens
, int count
);
277 char *wtrimspace(char *s
);
282 char *wusergnusteppath();
284 char *wdefaultspathfordomain(char *domain
);
286 void wusleep(unsigned int microsec
);
289 int wsprintesc(char *buffer
, int length
, char *format
, WMSEscapes
**escapes
,
293 /*......................................................................*/
295 /* This function is used _only_ if you create a NON-GUI program.
296 * For GUI based programs use WMNextEvent()/WMHandleEvent() instead.
297 * This function will handle all input/timer/idle events, then return.
300 void WHandleEvents();
302 /*......................................................................*/
305 WMHashTable
*WMCreateHashTable(WMHashTableCallbacks callbacks
);
307 void WMFreeHashTable(WMHashTable
*table
);
309 void WMResetHashTable(WMHashTable
*table
);
311 void *WMHashGet(WMHashTable
*table
, const void *key
);
313 /* put data in table, replacing already existing data and returning
315 void *WMHashInsert(WMHashTable
*table
, void *key
, void *data
);
317 void WMHashRemove(WMHashTable
*table
, const void *key
);
319 /* warning: do not manipulate the table while using these functions */
320 WMHashEnumerator
WMEnumerateHashTable(WMHashTable
*table
);
322 void *WMNextHashEnumeratorItem(WMHashEnumerator
*enumerator
);
324 unsigned WMCountHashTable(WMHashTable
*table
);
329 /* some predefined callback sets */
331 extern const WMHashTableCallbacks WMIntHashCallbacks
;
332 /* sizeof(keys) are <= sizeof(void*) */
334 extern const WMHashTableCallbacks WMStringHashCallbacks
;
335 /* keys are strings. Strings will be copied with wstrdup()
336 * and freed with wfree() */
338 extern const WMHashTableCallbacks WMStringPointerHashCallbacks
;
339 /* keys are strings, bug they are not copied */
342 /*......................................................................*/
346 * Array bags use an array to store the elements.
347 * Item indexes may be any integer number.
350 * Fast [O(1)] access to elements
351 * Fast [O(1)] push/pop
354 * A little slower [O(n)] for insertion/deletion of elements that
356 * Element indexes with large difference will cause large holes
359 WMBag
* WMCreateArrayBag(int initialSize
);
360 WMBag
* WMCreateArrayBagWithDestructor(int initialSize
,
361 void (*destructor
)(void*));
364 * Tree bags use a red-black tree for storage.
365 * Item indexes may be any integer number.
368 * O(lg n) insertion/deletion/search
369 * Good for large numbers of elements with sparse indexes
372 * O(lg n) insertion/deletion/search
373 * Slow for storing small numbers of elements
375 WMBag
*WMCreateTreeBag(void);
376 WMBag
*WMCreateTreeBagWithDestructor(void (*destructor
)(void*));
379 #define WMCreateArrayBag(a) WMCreateTreeBag()
380 #define WMCreateArrayBagWithDestructor(a,d) WMCreateTreeBagWithDestructor(d)
382 #define WMCreateBag(size) WMCreateTreeBag()
384 #define WMCreateBagWithDestructor(size, d) WMCreateTreeBagWithDestructor(d)
386 #define WMGetBagItemCount(bag) bag->func.getItemCount(bag)
388 #define WMAppendBag(bag, other) bag->func.appendBag(bag, other)
390 #define WMPutInBag(bag, item) bag->func.putInBag(bag, item)
392 /* insert will increment the index of elements after it by 1 */
393 #define WMInsertInBag(bag, index, item) bag->func.insertInBag(bag, index, item)
396 /* erase will remove the element from the bag,
397 * but will keep the index of the other elements unchanged */
398 #define WMEraseFromBag(bag, index) bag->func.eraseFromBag(bag, index)
400 /* delete and remove will remove the elements and cause the elements
401 * after them to decrement their indexes by 1 */
402 #define WMRemoveFromBag(bag, item) bag->func.removeFromBag(bag, item)
404 #define WMDeleteFromBag(bag, index) bag->func.deleteFromBag(bag, index)
406 #define WMGetFromBag(bag, index) bag->func.getFromBag(bag, index)
408 #define WMCountInBag(bag, item) bag->func.countInBag(bag, item)
410 #define WMReplaceInBag(bag, index, item) bag->func.replaceInBag(bag, index, item)
411 #define WMSetInBag(bag, index, item) bag->func.replaceInBag(bag, index, item)
413 /* comparer must return:
418 #define WMSortBag(bag, comparer) bag->func.sortBag(bag, comparer)
420 #define WMEmptyBag(bag) bag->func.emptyBag(bag)
422 #define WMFreeBag(bag) bag->func.freeBag(bag)
424 #define WMMapBag(bag, function, cdata) bag->func.mapBag(bag, function, cdata)
426 #define WMGetFirstInBag(bag, item) bag->func.firstInBag(bag, item)
428 #define WMCountInBag(bag, item) bag->func.countInBag(bag, item)
430 #define WMFindInBag(bag, match, cdata) bag->func.findInBag(bag, match, cdata)
432 #define WMBagFirst(bag, ptr) bag->func.first(bag, ptr)
434 #define WMBagLast(bag, ptr) bag->func.last(bag, ptr)
436 #define WMBagNext(bag, ptr) bag->func.next(bag, ptr)
438 #define WMBagPrevious(bag, ptr) bag->func.previous(bag, ptr)
440 #define WMBagIteratorAtIndex(bag, index, ptr) bag->func.iteratorAtIndex(bag, index, ptr)
442 #define WMBagIndexForIterator(bag, ptr) bag->func.indexForIterator(bag, ptr)
446 #define WM_ITERATE_BAG(bag, var, i) \
447 for (var = WMBagFirst(bag, &(i)); (i) != NULL; \
448 var = WMBagNext(bag, &(i)))
450 #define WM_ETARETI_BAG(bag, var, i) \
451 for (var = WMBagLast(bag, &(i)); (i) != NULL; \
452 var = WMBagPrevious(bag, &(i)))
456 /*-------------------------------------------------------------------------*/
458 /* WMData handling */
460 /* Creating/destroying data */
462 WMData
* WMCreateDataWithCapacity(unsigned capacity
);
464 WMData
* WMCreateDataWithLength(unsigned length
);
466 WMData
* WMCreateDataWithBytes(void *bytes
, unsigned length
);
468 /* destructor is a function called to free the data when releasing the data
469 * object, or NULL if no freeing of data is necesary. */
470 WMData
* WMCreateDataWithBytesNoCopy(void *bytes
, unsigned length
,
471 WMFreeDataProc
*destructor
);
473 WMData
* WMCreateDataWithData(WMData
*aData
);
475 WMData
* WMRetainData(WMData
*aData
);
477 void WMReleaseData(WMData
*aData
);
479 /* Adjusting capacity */
481 void WMSetDataCapacity(WMData
*aData
, unsigned capacity
);
483 void WMSetDataLength(WMData
*aData
, unsigned length
);
485 void WMIncreaseDataLengthBy(WMData
*aData
, unsigned extraLength
);
489 const void* WMDataBytes(WMData
*aData
);
491 void WMGetDataBytes(WMData
*aData
, void *buffer
);
493 void WMGetDataBytesWithLength(WMData
*aData
, void *buffer
, unsigned length
);
495 void WMGetDataBytesWithRange(WMData
*aData
, void *buffer
, WMRange aRange
);
497 WMData
* WMGetSubdataWithRange(WMData
*aData
, WMRange aRange
);
501 Bool
WMIsDataEqualToData(WMData
*aData
, WMData
*anotherData
);
503 unsigned WMGetDataLength(WMData
*aData
);
507 void WMAppendDataBytes(WMData
*aData
, void *bytes
, unsigned length
);
509 void WMAppendData(WMData
*aData
, WMData
*anotherData
);
513 void WMReplaceDataBytesInRange(WMData
*aData
, WMRange aRange
, void *bytes
);
515 void WMResetDataBytesInRange(WMData
*aData
, WMRange aRange
);
517 void WMSetData(WMData
*aData
, WMData
*anotherData
);
520 void WMSetDataFormat(WMData
*aData
, unsigned format
);
522 unsigned WMGetDataFormat(WMData
*aData
);
526 /*--------------------------------------------------------------------------*/
529 WMNotification
*WMCreateNotification(char *name
, void *object
, void *clientData
);
531 void WMReleaseNotification(WMNotification
*notification
);
533 WMNotification
*WMRetainNotification(WMNotification
*notification
);
535 void *WMGetNotificationClientData(WMNotification
*notification
);
537 void *WMGetNotificationObject(WMNotification
*notification
);
539 char *WMGetNotificationName(WMNotification
*notification
);
542 void WMAddNotificationObserver(WMNotificationObserverAction
*observerAction
,
543 void *observer
, char *name
, void *object
);
545 void WMPostNotification(WMNotification
*notification
);
547 void WMRemoveNotificationObserver(void *observer
);
549 void WMRemoveNotificationObserverWithName(void *observer
, char *name
,
552 void WMPostNotificationName(char *name
, void *object
, void *clientData
);
554 WMNotificationQueue
*WMGetDefaultNotificationQueue(void);
556 WMNotificationQueue
*WMCreateNotificationQueue(void);
558 void WMDequeueNotificationMatching(WMNotificationQueue
*queue
,
559 WMNotification
*notification
,
562 void WMEnqueueNotification(WMNotificationQueue
*queue
,
563 WMNotification
*notification
,
564 WMPostingStyle postingStyle
);
566 void WMEnqueueCoalesceNotification(WMNotificationQueue
*queue
,
567 WMNotification
*notification
,
568 WMPostingStyle postingStyle
,
569 unsigned coalesceMask
);
572 /*......................................................................*/
574 WMUserDefaults
*WMGetStandardUserDefaults(void);
576 WMUserDefaults
*WMGetDefaultsFromPath(char *path
);
578 void WMSynchronizeUserDefaults(WMUserDefaults
*database
);
580 void WMSaveUserDefaults(WMUserDefaults
*database
);
582 void WMEnableUDPeriodicSynchronization(WMUserDefaults
*database
, Bool enable
);
584 /* Returns a PLArray with all keys in the user defaults database.
585 * Free the returned array with PLRelease() when no longer needed,
586 * but do not free the elements of the array! They're just references. */
587 proplist_t
WMGetUDAllKeys(WMUserDefaults
*database
);
589 proplist_t
WMGetUDObjectForKey(WMUserDefaults
*database
, char *defaultName
);
591 void WMSetUDObjectForKey(WMUserDefaults
*database
, proplist_t object
,
594 void WMRemoveUDObjectForKey(WMUserDefaults
*database
, char *defaultName
);
596 char *WMGetUDStringForKey(WMUserDefaults
*database
, char *defaultName
);
598 int WMGetUDIntegerForKey(WMUserDefaults
*database
, char *defaultName
);
600 float WMGetUDFloatForKey(WMUserDefaults
*database
, char *defaultName
);
602 Bool
WMGetUDBoolForKey(WMUserDefaults
*database
, char *defaultName
);
604 void WMSetUDStringForKey(WMUserDefaults
*database
, char *value
,
607 void WMSetUDIntegerForKey(WMUserDefaults
*database
, int value
,
610 void WMSetUDFloatForKey(WMUserDefaults
*database
, float value
,
613 void WMSetUDBoolForKey(WMUserDefaults
*database
, Bool value
,
616 proplist_t
WMGetUDSearchList(WMUserDefaults
*database
);
618 void WMSetUDSearchList(WMUserDefaults
*database
, proplist_t list
);
620 extern char *WMUserDefaultsDidChangeNotification
;
623 /*-------------------------------------------------------------------------*/
625 /* WMHost: host handling */
627 WMHost
* WMGetCurrentHost();
629 WMHost
* WMGetHostWithName(char* name
);
631 WMHost
* WMGetHostWithAddress(char* address
);
633 WMHost
* WMRetainHost(WMHost
*hPtr
);
635 void WMReleaseHost(WMHost
*hPtr
);
638 * Host cache management
639 * If enabled, only one object representing each host will be created, and
640 * a shared instance will be returned by all methods that return a host.
641 * Enabled by default.
643 void WMSetHostCacheEnabled(Bool flag
);
645 Bool
WMIsHostCacheEnabled();
647 void WMFlushHostCache();
650 * Compare hosts: Hosts are equal if they share at least one address
652 Bool
WMIsHostEqualToHost(WMHost
* hPtr
, WMHost
* anotherHost
);
656 * WMGetHostName() will return first name (official) if a host has several.
657 * WMGetHostNames() will return a R/O WMBag with all the names of the host.
659 char* WMGetHostName(WMHost
* hPtr
);
661 /* The returned bag is R/O. Do not modify it, and do not free it! */
662 WMBag
* WMGetHostNames(WMHost
* hPtr
);
666 * Addresses are represented as "Dotted Decimal" strings, e.g. "192.42.172.1"
667 * WMGetHostAddress() will return an arbitrary address if a host has several.
668 * WMGetHostAddresses() will return a R/O WMBag with all addresses of the host.
670 char* WMGetHostAddress(WMHost
* hPtr
);
672 /* The returned bag is R/O. Do not modify it, and do not free it! */
673 WMBag
* WMGetHostAddresses(WMHost
* hPtr
);
676 /*-------------------------------------------------------------------------*/
678 /* WMConnection functions */
680 WMConnection
* WMCreateConnectionAsServerAtAddress(char *host
, char *service
,
683 WMConnection
* WMCreateConnectionToAddress(char *host
, char *service
,
686 WMConnection
* WMCreateConnectionToAddressAndNotify(char *host
, char *service
,
689 void WMCloseConnection(WMConnection
*cPtr
);
691 void WMDestroyConnection(WMConnection
*cPtr
);
693 WMConnection
* WMAcceptConnection(WMConnection
*listener
);
695 /* Release the returned data! */
696 WMData
* WMGetConnectionAvailableData(WMConnection
*cPtr
);
698 int WMSendConnectionData(WMConnection
*cPtr
, WMData
*data
);
700 Bool
WMEnqueueConnectionData(WMConnection
*cPtr
, WMData
*data
);
702 #define WMFlushConnection(cPtr) WMSendConnectionData((cPtr), NULL)
704 void WMSetConnectionDelegate(WMConnection
*cPtr
, ConnectionDelegate
*delegate
);
706 /* Connection info */
708 char* WMGetConnectionAddress(WMConnection
*cPtr
);
710 char* WMGetConnectionService(WMConnection
*cPtr
);
712 char* WMGetConnectionProtocol(WMConnection
*cPtr
);
714 void WMSetConnectionNonBlocking(WMConnection
*cPtr
, Bool flag
);
716 void* WMGetConnectionClientData(WMConnection
*cPtr
);
718 void WMSetConnectionClientData(WMConnection
*cPtr
, void *data
);
720 unsigned int WMGetConnectionFlags(WMConnection
*cPtr
);
722 void WMSetConnectionFlags(WMConnection
*cPtr
, unsigned int flags
);
724 int WMGetConnectionSocket(WMConnection
*cPtr
);
726 WMConnectionState
WMGetConnectionState(WMConnection
*cPtr
);
728 WMConnectionTimeoutState
WMGetConnectionTimeoutState(WMConnection
*cPtr
);
731 * Passing timeout==0 in the SetTimeout functions below, will reset that
732 * timeout to its default value.
735 /* The default timeout inherited by all WMConnection operations, if none set */
736 void WMSetConnectionDefaultTimeout(unsigned int timeout
);
738 /* Global timeout for all WMConnection objects, for opening a new connection */
739 void WMSetConnectionOpenTimeout(unsigned int timeout
);
741 /* Connection specific timeout for sending out data */
742 void WMSetConnectionSendTimeout(WMConnection
*cPtr
, unsigned int timeout
);
745 /* Global variables */
747 extern int WCErrorCode
;
750 /*-------------------------------------------------------------------------*/
756 #endif /* __cplusplus */