Remake of the WMList code to allow multiple selection. Not complete yet.
[wmaker-crm.git] / WINGs / WUtil.h
blob8a14ddfb2bf995b758cb15681e910b20d118f062
1 #ifndef _WUTIL_H_
2 #define _WUTIL_H_
4 #include <X11/Xlib.h>
5 #include <limits.h>
6 #include <sys/types.h>
8 /* SunOS 4.x Blargh.... */
9 #ifndef NULL
10 #define NULL ((void*)0)
11 #endif
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.
19 #include <proplist.h>
22 #ifndef WMAX
23 # define WMAX(a,b) ((a)>(b) ? (a) : (b))
24 #endif
25 #ifndef WMIN
26 # define WMIN(a,b) ((a)<(b) ? (a) : (b))
27 #endif
30 #if (!defined (__GNUC__) || __GNUC__ < 2 || \
31 __GNUC_MINOR__ < (defined (__cplusplus) ? 6 : 4))
32 #define __ASSERT_FUNCTION ((char *) 0)
33 #else
34 #define __ASSERT_FUNCTION __PRETTY_FUNCTION__
35 #endif
38 #ifdef NDEBUG
40 #define wassertr(expr) {}
41 #define wassertrv(expr, val) {}
43 #else /* !NDEBUG */
45 #ifdef DEBUG
47 #include <assert.h>
49 #define wassertr(expr) assert(expr)
51 #define wassertrv(expr, val) assert(expr)
53 #else /* !DEBUG */
55 #define wassertr(expr) \
56 if (!(expr)) { \
57 wwarning("%s line %i (%s): assertion %s failed",\
58 __FILE__, __LINE__, __ASSERT_FUNCTION, #expr);\
59 return;\
62 #define wassertrv(expr, val) \
63 if (!(expr)) { \
64 wwarning("%s line %i (%s): assertion %s failed",\
65 __FILE__, __LINE__, __ASSERT_FUNCTION, #expr);\
66 return (val);\
68 #endif /* !DEBUG */
70 #endif /* !NDEBUG */
73 #ifdef __cplusplus
74 extern "C" {
75 #endif /* __cplusplus */
78 typedef enum {
79 WMPostWhenIdle = 1,
80 WMPostASAP = 2,
81 WMPostNow = 3
82 } WMPostingStyle;
85 typedef enum {
86 WNCNone = 0,
87 WNCOnName = 1,
88 WNCOnSender = 2
89 } WMNotificationCoalescing;
92 /* The possible states for connections */
93 typedef enum {
94 WCNotConnected=0,
95 WCListening,
96 WCInProgress,
97 WCFailed,
98 WCConnected,
99 WCTimedOut,
100 WCDied,
101 WCClosed
102 } WMConnectionState;
105 /* The possible states for connection timeouts */
106 typedef enum {
107 WCTNone=0,
108 WCTWhileOpening,
109 WCTWhileSending
110 } WMConnectionTimeoutState;
114 enum {
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);
143 typedef struct {
144 int position;
145 int count;
146 } WMRange;
150 /* DO NOT ACCESS THE CONTENTS OF THIS STRUCT */
151 typedef struct {
152 void *table;
153 void *nextItem;
154 int index;
155 } WMHashEnumerator;
158 typedef 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;
173 #if 0
174 typedef struct {
175 char character; /* the escape character */
176 char *value; /* value to place */
177 } WMSEscapes;
178 #endif
181 /* The connection callbacks */
182 typedef struct ConnectionDelegate {
183 void *data;
185 void (*didCatchException)(struct ConnectionDelegate *self,
186 WMConnection *cPtr);
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 */
228 char *wgethomedir();
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);
262 #if 0
263 int wsprintesc(char *buffer, int length, char *format, WMSEscapes **escapes,
264 int count);
265 #endif
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
288 * the old value */
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.
323 * Pros:
324 * Fast [O(1)] access to elements
325 * Fast [O(1)] push/pop
327 * Cons:
328 * A little slower [O(n)] for insertion/deletion of elements that
329 * aren't in the end
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:
383 * < 0 if a < b
384 * > 0 if a > b
385 * = 0 if a = b
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.
399 * Pros:
400 * O(lg n) insertion/deletion/search
401 * Good for large numbers of elements with sparse indexes
403 * Cons:
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);
425 /* this is slow */
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:
443 * < 0 if a < b
444 * > 0 if a > b
445 * = 0 if a = b
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);
515 /* Accessing data */
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);
527 /* Testing data */
529 Bool WMIsDataEqualToData(WMData *aData, WMData *anotherData);
531 unsigned WMGetDataLength(WMData *aData);
533 /* Adding data */
535 void WMAppendDataBytes(WMData *aData, void *bytes, unsigned length);
537 void WMAppendData(WMData *aData, WMData *anotherData);
539 /* Modifying data */
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);
551 /* Storing data */
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,
578 void *object);
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,
588 unsigned mask);
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,
620 char *defaultName);
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,
633 char *defaultName);
635 void WMSetUDIntegerForKey(WMUserDefaults *database, int value,
636 char *defaultName);
638 void WMSetUDFloatForKey(WMUserDefaults *database, float value,
639 char *defaultName);
641 void WMSetUDBoolForKey(WMUserDefaults *database, Bool value,
642 char *defaultName);
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);
683 * Host names.
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);
693 * Host addresses.
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,
709 char *protocol);
711 WMConnection* WMCreateConnectionToAddress(char *host, char *service,
712 char *protocol);
714 WMConnection* WMCreateConnectionToAddressAndNotify(char *host, char *service,
715 char *protocol);
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 /*-------------------------------------------------------------------------*/
782 #ifdef __cplusplus
784 #endif /* __cplusplus */
787 #endif