- Added a test in configure for the version of libPropList that is installed
[wmaker-crm.git] / WINGs / WUtil.h
blob43f043f595ed8946bbe3ebf27c1899f6f43043a4
1 #ifndef _WUTIL_H_
2 #define _WUTIL_H_
4 #include <X11/Xlib.h>
6 #include <sys/types.h>
8 /* SunOS 4.x Blargh.... */
9 #ifndef NULL
10 #define NULL ((void*)0)
11 #endif
15 * Warning: proplist.h #defines BOOL which will clash with the
16 * typedef BOOL in Xmd.h
17 * proplist.h should use Bool (which is a #define in Xlib.h) instead.
20 #include <proplist.h>
23 #ifndef WMAX
24 # define WMAX(a,b) ((a)>(b) ? (a) : (b))
25 #endif
26 #ifndef WMIN
27 # define WMIN(a,b) ((a)<(b) ? (a) : (b))
28 #endif
31 #if (!defined (__GNUC__) || __GNUC__ < 2 || \
32 __GNUC_MINOR__ < (defined (__cplusplus) ? 6 : 4))
33 #define __ASSERT_FUNCTION ((char *) 0)
34 #else
35 #define __ASSERT_FUNCTION __PRETTY_FUNCTION__
36 #endif
39 #ifdef NDEBUG
41 #define wassertr(expr) {}
42 #define wassertrv(expr, val) {}
44 #else /* !NDEBUG */
46 #ifdef DEBUG
48 #include <assert.h>
50 #define wassertr(expr) assert(expr)
52 #define wassertrv(expr, val) assert(expr)
54 #else /* !DEBUG */
56 #define wassertr(expr) \
57 if (!(expr)) { \
58 wwarning("%s line %i (%s): assertion %s failed",\
59 __FILE__, __LINE__, __ASSERT_FUNCTION, #expr);\
60 return;\
63 #define wassertrv(expr, val) \
64 if (!(expr)) { \
65 wwarning("%s line %i (%s): assertion %s failed",\
66 __FILE__, __LINE__, __ASSERT_FUNCTION, #expr);\
67 return (val);\
69 #endif /* !DEBUG */
71 #endif /* !NDEBUG */
74 #ifdef __cplusplus
75 extern "C" {
76 #endif /* __cplusplus */
79 typedef enum {
80 WMPostWhenIdle = 1,
81 WMPostASAP = 2,
82 WMPostNow = 3
83 } WMPostingStyle;
86 typedef enum {
87 WNCNone = 0,
88 WNCOnName = 1,
89 WNCOnSender = 2
90 } WMNotificationCoalescing;
93 /* The possible states for connections */
94 typedef enum {
95 WCNotConnected=0,
96 WCListening,
97 WCInProgress,
98 WCFailed,
99 WCConnected,
100 WCDied,
101 WCClosed
102 } WMConnectionState;
106 typedef struct W_Bag WMBag; /* equivalent to a linked list or array */
107 typedef struct W_Data WMData;
108 typedef struct W_HashTable WMHashTable;
109 typedef struct W_UserDefaults WMUserDefaults;
110 typedef struct W_Notification WMNotification;
111 typedef struct W_NotificationQueue WMNotificationQueue;
112 typedef struct W_Host WMHost;
113 typedef struct W_Connection WMConnection;
117 typedef struct {
118 int position;
119 int count;
120 } WMRange;
124 /* DO NOT ACCESS THE CONTENTS OF THIS STRUCT */
125 typedef struct {
126 void *table;
127 void *nextItem;
128 int index;
129 } WMHashEnumerator;
132 typedef struct {
133 /* NULL is pointer hash */
134 unsigned (*hash)(const void *);
135 /* NULL is pointer compare */
136 Bool (*keyIsEqual)(const void *, const void *);
137 /* NULL does nothing */
138 void* (*retainKey)(const void *);
139 /* NULL does nothing */
140 void (*releaseKey)(const void *);
141 } WMHashTableCallbacks;
145 #if 0
146 typedef struct {
147 char character; /* the escape character */
148 char *value; /* value to place */
149 } WMSEscapes;
150 #endif
153 /* The connection callbacks */
154 typedef struct ConnectionDelegate {
155 void *data;
157 void (*didCatchException)(struct ConnectionDelegate *self,
158 WMConnection *cPtr);
160 void (*didDie)(struct ConnectionDelegate *self, WMConnection *cPtr);
162 void (*didInitialize)(struct ConnectionDelegate *self, WMConnection *cPtr);
164 void (*didReceiveInput)(struct ConnectionDelegate *self, WMConnection *cPtr);
166 void (*didTimeout)(struct ConnectionDelegate *self, WMConnection *cPtr);
168 } ConnectionDelegate;
171 typedef void WMNotificationObserverAction(void *observerData,
172 WMNotification *notification);
176 /*......................................................................*/
178 typedef void (waborthandler)(int);
180 waborthandler *wsetabort(waborthandler*);
183 /* don't free the returned string */
184 char *wstrerror(int errnum);
186 void wfatal(const char *msg, ...);
187 void wwarning(const char *msg, ...);
188 void wsyserror(const char *msg, ...);
189 void wsyserrorwithcode(int error, const char *msg, ...);
191 char *wfindfile(char *paths, char *file);
193 char *wfindfileinlist(char **path_list, char *file);
195 char *wfindfileinarray(proplist_t array, char *file);
197 char *wexpandpath(char *path);
199 /* don't free the returned string */
200 char *wgethomedir();
202 void *wmalloc(size_t size);
203 void *wrealloc(void *ptr, size_t newsize);
204 void wfree(void *ptr);
207 void wrelease(void *ptr);
208 void *wretain(void *ptr);
210 char *wstrdup(char *str);
212 char *wstrappend(char *dst, char *src);
214 char *wusergnusteppath();
216 char *wdefaultspathfordomain(char *domain);
218 void wusleep(unsigned int microsec);
220 #if 0
221 int wsprintesc(char *buffer, int length, char *format, WMSEscapes **escapes,
222 int count);
223 #endif
225 /*......................................................................*/
227 /* This function is used _only_ if you create a NON-GUI program.
228 * For GUI based programs use WMNextEvent()/WMHandleEvent() instead.
229 * This function will handle all input/timer/idle events, then return.
232 void WHandleEvents();
234 /*......................................................................*/
237 WMHashTable *WMCreateHashTable(WMHashTableCallbacks callbacks);
239 void WMFreeHashTable(WMHashTable *table);
241 void WMResetHashTable(WMHashTable *table);
243 void *WMHashGet(WMHashTable *table, const void *key);
245 /* put data in table, replacing already existing data and returning
246 * the old value */
247 void *WMHashInsert(WMHashTable *table, void *key, void *data);
249 void WMHashRemove(WMHashTable *table, const void *key);
251 /* warning: do not manipulate the table while using these functions */
252 WMHashEnumerator WMEnumerateHashTable(WMHashTable *table);
254 void *WMNextHashEnumeratorItem(WMHashEnumerator *enumerator);
256 unsigned WMCountHashTable(WMHashTable *table);
261 /* some predefined callback sets */
263 extern const WMHashTableCallbacks WMIntHashCallbacks;
264 /* sizeof(keys) are <= sizeof(void*) */
266 extern const WMHashTableCallbacks WMStringHashCallbacks;
267 /* keys are strings. Strings will be copied with wstrdup()
268 * and freed with free() */
270 extern const WMHashTableCallbacks WMStringPointerHashCallbacks;
271 /* keys are strings, bug they are not copied */
274 /*......................................................................*/
277 WMBag *WMCreateBag(int size);
279 int WMGetBagItemCount(WMBag *bag);
281 void WMAppendBag(WMBag *bag, WMBag *appendedBag);
283 void WMPutInBag(WMBag *bag, void *item);
285 void WMInsertInBag(WMBag *bag, int index, void *item);
287 int WMGetFirstInBag(WMBag *bag, void *item);
289 int WMGetLastInBag(WMBag *bag, void *item);
291 void WMRemoveFromBag(WMBag *bag, void *item);
293 void WMDeleteFromBag(WMBag *bag, int index);
295 void *WMGetFromBag(WMBag *bag, int index);
297 int WMCountInBag(WMBag *bag, void *item);
299 void *WMReplaceInBag(WMBag *bag, int index, void *item);
301 /* comparer must return:
302 * < 0 if a < b
303 * > 0 if a > b
304 * = 0 if a = b
306 void WMSortBag(WMBag *bag, int (*comparer)(const void*, const void*));
308 void WMEmptyBag(WMBag *bag);
310 void WMFreeBag(WMBag *bag);
312 WMBag *WMMapBag(WMBag *bag, void* (*function)(void*));
314 /*-------------------------------------------------------------------------*/
316 /* WMData handling */
318 /* Creating/destroying data */
320 WMData* WMCreateDataWithCapacity(unsigned capacity);
322 WMData* WMCreateDataWithLength(unsigned length);
324 WMData* WMCreateDataWithBytes(void *bytes, unsigned length);
326 WMData* WMCreateDataWithBytesNoCopy(void *bytes, unsigned length);
328 WMData* WMCreateDataWithData(WMData *aData);
330 WMData* WMRetainData(WMData *aData);
332 void WMReleaseData(WMData *aData);
334 /* Adjusting capacity */
336 void WMSetDataCapacity(WMData *aData, unsigned capacity);
338 void WMSetDataLength(WMData *aData, unsigned length);
340 void WMIncreaseDataLengthBy(WMData *aData, unsigned extraLength);
342 /* Accessing data */
344 const void* WMDataBytes(WMData *aData);
346 void WMGetDataBytes(WMData *aData, void *buffer);
348 void WMGetDataBytesWithLength(WMData *aData, void *buffer, unsigned length);
350 void WMGetDataBytesWithRange(WMData *aData, void *buffer, WMRange aRange);
352 WMData* WMGetSubdataWithRange(WMData *aData, WMRange aRange);
354 /* Testing data */
356 Bool WMIsDataEqualToData(WMData *aData, WMData *anotherData);
358 unsigned WMGetDataLength(WMData *aData);
360 unsigned WMGetDataHash(WMData *aData);
362 /* Adding data */
364 void WMAppendDataBytes(WMData *aData, void *bytes, unsigned length);
366 void WMAppendData(WMData *aData, WMData *anotherData);
368 /* Modifying data */
370 void WMReplaceDataBytesInRange(WMData *aData, WMRange aRange, void *bytes);
372 void WMResetDataBytesInRange(WMData *aData, WMRange aRange);
374 void WMSetData(WMData *aData, WMData *anotherData);
376 /* Storing data */
379 /*--------------------------------------------------------------------------*/
382 WMNotification *WMCreateNotification(char *name, void *object, void *clientData);
384 void WMReleaseNotification(WMNotification *notification);
386 WMNotification *WMRetainNotification(WMNotification *notification);
388 void *WMGetNotificationClientData(WMNotification *notification);
390 void *WMGetNotificationObject(WMNotification *notification);
392 char *WMGetNotificationName(WMNotification *notification);
395 void WMAddNotificationObserver(WMNotificationObserverAction *observerAction,
396 void *observer, char *name, void *object);
398 void WMPostNotification(WMNotification *notification);
400 void WMRemoveNotificationObserver(void *observer);
402 void WMRemoveNotificationObserverWithName(void *observer, char *name,
403 void *object);
405 void WMPostNotificationName(char *name, void *object, void *clientData);
407 WMNotificationQueue *WMGetDefaultNotificationQueue(void);
409 WMNotificationQueue *WMCreateNotificationQueue(void);
411 void WMDequeueNotificationMatching(WMNotificationQueue *queue,
412 WMNotification *notification,
413 unsigned mask);
415 void WMEnqueueNotification(WMNotificationQueue *queue,
416 WMNotification *notification,
417 WMPostingStyle postingStyle);
419 void WMEnqueueCoalesceNotification(WMNotificationQueue *queue,
420 WMNotification *notification,
421 WMPostingStyle postingStyle,
422 unsigned coalesceMask);
425 /*......................................................................*/
427 WMUserDefaults *WMGetStandardUserDefaults(void);
429 WMUserDefaults *WMGetDefaultsFromPath(char *path);
431 void WMSynchronizeUserDefaults(WMUserDefaults *database);
433 void WMSaveUserDefaults(WMUserDefaults *database);
435 void WMEnableUDPeriodicSynchronization(WMUserDefaults *database, Bool enable);
437 proplist_t WMGetUDObjectForKey(WMUserDefaults *database, char *defaultName);
439 void WMSetUDObjectForKey(WMUserDefaults *database, proplist_t object,
440 char *defaultName);
442 void WMRemoveUDObjectForKey(WMUserDefaults *database, char *defaultName);
444 /* you can free the returned string */
445 char *WMGetUDStringForKey(WMUserDefaults *database, char *defaultName);
447 int WMGetUDIntegerForKey(WMUserDefaults *database, char *defaultName);
449 float WMGetUDFloatForKey(WMUserDefaults *database, char *defaultName);
451 Bool WMGetUDBoolForKey(WMUserDefaults *database, char *defaultName);
453 void WMSetUDStringForKey(WMUserDefaults *database, char *value,
454 char *defaultName);
456 void WMSetUDIntegerForKey(WMUserDefaults *database, int value,
457 char *defaultName);
459 void WMSetUDFloatForKey(WMUserDefaults *database, float value,
460 char *defaultName);
462 void WMSetUDBoolForKey(WMUserDefaults *database, Bool value,
463 char *defaultName);
465 proplist_t WMGetUDSearchList(WMUserDefaults *database);
467 void WMSetUDSearchList(WMUserDefaults *database, proplist_t list);
469 extern char *WMUserDefaultsDidChangeNotification;
472 /*-------------------------------------------------------------------------*/
474 /* WMHost: host handling */
476 WMHost* WMGetCurrentHost();
478 WMHost* WMGetHostWithName(char* name);
480 WMHost* WMGetHostWithAddress(char* address);
482 WMHost* WMRetainHost(WMHost *hPtr);
484 void WMReleaseHost(WMHost *hPtr);
487 * Host cache management
488 * If enabled, only one object representing each host will be created, and
489 * a shared instance will be returned by all methods that return a host.
490 * Enabled by default.
492 void WMSetHostCacheEnabled(Bool flag);
494 Bool WMIsHostCacheEnabled();
496 void WMFlushHostCache();
499 * Compare hosts: Hosts are equal if they share at least one address
501 Bool WMIsHostEqualToHost(WMHost* hPtr, WMHost* anotherHost);
504 * Host names.
505 * WMGetHostName() will return first name (official) if a host has several.
506 * WMGetHostNames() will return a R/O WMBag with all the names of the host.
508 char* WMGetHostName(WMHost* hPtr);
510 /* The returned bag is R/O. Do not modify it, and do not free it! */
511 WMBag* WMGetHostNames(WMHost* hPtr);
514 * Host addresses.
515 * Addresses are represented as "Dotted Decimal" strings, e.g. "192.42.172.1"
516 * WMGetHostAddress() will return an arbitrary address if a host has several.
517 * WMGetHostAddresses() will return a R/O WMBag with all addresses of the host.
519 char* WMGetHostAddress(WMHost* hPtr);
521 /* The returned bag is R/O. Do not modify it, and do not free it! */
522 WMBag* WMGetHostAddresses(WMHost* hPtr);
525 /*-------------------------------------------------------------------------*/
527 /* WMConnection functions */
529 WMConnection* WMCreateConnectionAsServerAtAddress(char *host, char *service,
530 char *protocol);
532 WMConnection* WMCreateConnectionToAddress(char *host, char *service,
533 char *protocol);
535 WMConnection* WMCreateConnectionToAddressAndNotify(char *host, char *service,
536 char *protocol);
538 void WMCloseConnection(WMConnection *cPtr);
540 void WMDestroyConnection(WMConnection *cPtr);
542 WMConnection* WMAcceptConnection(WMConnection *listener);
544 /* Release the returned data! */
545 WMData* WMGetConnectionAvailableData(WMConnection *cPtr);
547 int WMSendConnectionData(WMConnection *cPtr, WMData *data);
549 Bool WMEnqueueConnectionData(WMConnection *cPtr, WMData *data);
551 #define WMFlushConnection(cPtr) WMSendConnectionData((cPtr), NULL)
553 void WMSetConnectionDelegate(WMConnection *cPtr, ConnectionDelegate *delegate);
555 /* Connection info */
557 char* WMGetConnectionAddress(WMConnection *cPtr);
559 char* WMGetConnectionService(WMConnection *cPtr);
561 char* WMGetConnectionProtocol(WMConnection *cPtr);
563 void WMSetConnectionNonBlocking(WMConnection *cPtr, Bool flag);
565 void* WMGetConnectionClientData(WMConnection *cPtr);
567 void WMSetConnectionClientData(WMConnection *cPtr, void *data);
569 unsigned int WMGetConnectionFlags(WMConnection *cPtr);
571 void WMSetConnectionFlags(WMConnection *cPtr, unsigned int flags);
573 int WMGetConnectionSocket(WMConnection *cPtr);
575 WMConnectionState WMGetConnectionState(WMConnection *cPtr);
577 void WMSetConnectionSendTimeout(WMConnection *cPtr, unsigned int timeout);
580 /* Global variables */
582 extern int WCErrorCode;
585 /*-------------------------------------------------------------------------*/
589 #ifdef __cplusplus
591 #endif /* __cplusplus */
594 #endif