Made the floppy path in the file panel be configurable via the FloppyPath
[wmaker-crm.git] / WINGs / WUtil.h
blobe650785010db0d098b1cf2b3eedd2f5330dc225e
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 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);
134 typedef struct {
135 int position;
136 int count;
137 } WMRange;
141 /* DO NOT ACCESS THE CONTENTS OF THIS STRUCT */
142 typedef struct {
143 void *table;
144 void *nextItem;
145 int index;
146 } WMHashEnumerator;
149 typedef 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);
186 } W_BagFunctions;
189 struct W_Bag {
190 void *data;
192 void (*destructor)(void *item);
194 W_BagFunctions func;
199 #if 0
200 typedef struct {
201 char character; /* the escape character */
202 char *value; /* value to place */
203 } WMSEscapes;
204 #endif
207 /* The connection callbacks */
208 typedef struct ConnectionDelegate {
209 void *data;
211 void (*didCatchException)(struct ConnectionDelegate *self,
212 WMConnection *cPtr);
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 */
254 char *wgethomedir();
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);
274 #if 0
275 int wsprintesc(char *buffer, int length, char *format, WMSEscapes **escapes,
276 int count);
277 #endif
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
300 * the old value */
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.
335 * Pros:
336 * Fast [O(1)] access to elements
337 * Fast [O(1)] push/pop
339 * Cons:
340 * A little slower [O(n)] for insertion/deletion of elements that
341 * arent in the end
342 * Element indexes with large difference will cause large holes
344 #if 0
345 WMBag* WMCreateArrayBag(int initialSize);
346 WMBag* WMCreateArrayBagWithDestructor(int initialSize,
347 void (*destructor)(void*));
348 #endif
350 * Tree bags use a red-black tree for storage.
351 * Item indexes may be any integer number.
353 * Pros:
354 * O(lg n) insertion/deletion/search
355 * Good for large numbers of elements with sparse indexes
357 * Cons:
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)
381 /* this is slow */
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:
400 * < 0 if a < b
401 * > 0 if a > b
402 * = 0 if a = b
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);
473 /* Accessing data */
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);
485 /* Testing data */
487 Bool WMIsDataEqualToData(WMData *aData, WMData *anotherData);
489 unsigned WMGetDataLength(WMData *aData);
491 /* Adding data */
493 void WMAppendDataBytes(WMData *aData, void *bytes, unsigned length);
495 void WMAppendData(WMData *aData, WMData *anotherData);
497 /* Modifying data */
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);
509 /* Storing data */
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,
536 void *object);
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,
546 unsigned mask);
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,
578 char *defaultName);
580 void WMRemoveUDObjectForKey(WMUserDefaults *database, char *defaultName);
582 /* Free the returned string when no longer needed */
583 char *WMGetUDStringForKey(WMUserDefaults *database, char *defaultName);
585 int WMGetUDIntegerForKey(WMUserDefaults *database, char *defaultName);
587 float WMGetUDFloatForKey(WMUserDefaults *database, char *defaultName);
589 Bool WMGetUDBoolForKey(WMUserDefaults *database, char *defaultName);
591 void WMSetUDStringForKey(WMUserDefaults *database, char *value,
592 char *defaultName);
594 void WMSetUDIntegerForKey(WMUserDefaults *database, int value,
595 char *defaultName);
597 void WMSetUDFloatForKey(WMUserDefaults *database, float value,
598 char *defaultName);
600 void WMSetUDBoolForKey(WMUserDefaults *database, Bool value,
601 char *defaultName);
603 proplist_t WMGetUDSearchList(WMUserDefaults *database);
605 void WMSetUDSearchList(WMUserDefaults *database, proplist_t list);
607 extern char *WMUserDefaultsDidChangeNotification;
610 /*-------------------------------------------------------------------------*/
612 /* WMHost: host handling */
614 WMHost* WMGetCurrentHost();
616 WMHost* WMGetHostWithName(char* name);
618 WMHost* WMGetHostWithAddress(char* address);
620 WMHost* WMRetainHost(WMHost *hPtr);
622 void WMReleaseHost(WMHost *hPtr);
625 * Host cache management
626 * If enabled, only one object representing each host will be created, and
627 * a shared instance will be returned by all methods that return a host.
628 * Enabled by default.
630 void WMSetHostCacheEnabled(Bool flag);
632 Bool WMIsHostCacheEnabled();
634 void WMFlushHostCache();
637 * Compare hosts: Hosts are equal if they share at least one address
639 Bool WMIsHostEqualToHost(WMHost* hPtr, WMHost* anotherHost);
642 * Host names.
643 * WMGetHostName() will return first name (official) if a host has several.
644 * WMGetHostNames() will return a R/O WMBag with all the names of the host.
646 char* WMGetHostName(WMHost* hPtr);
648 /* The returned bag is R/O. Do not modify it, and do not free it! */
649 WMBag* WMGetHostNames(WMHost* hPtr);
652 * Host addresses.
653 * Addresses are represented as "Dotted Decimal" strings, e.g. "192.42.172.1"
654 * WMGetHostAddress() will return an arbitrary address if a host has several.
655 * WMGetHostAddresses() will return a R/O WMBag with all addresses of the host.
657 char* WMGetHostAddress(WMHost* hPtr);
659 /* The returned bag is R/O. Do not modify it, and do not free it! */
660 WMBag* WMGetHostAddresses(WMHost* hPtr);
663 /*-------------------------------------------------------------------------*/
665 /* WMConnection functions */
667 WMConnection* WMCreateConnectionAsServerAtAddress(char *host, char *service,
668 char *protocol);
670 WMConnection* WMCreateConnectionToAddress(char *host, char *service,
671 char *protocol);
673 WMConnection* WMCreateConnectionToAddressAndNotify(char *host, char *service,
674 char *protocol);
676 void WMCloseConnection(WMConnection *cPtr);
678 void WMDestroyConnection(WMConnection *cPtr);
680 WMConnection* WMAcceptConnection(WMConnection *listener);
682 /* Release the returned data! */
683 WMData* WMGetConnectionAvailableData(WMConnection *cPtr);
685 int WMSendConnectionData(WMConnection *cPtr, WMData *data);
687 Bool WMEnqueueConnectionData(WMConnection *cPtr, WMData *data);
689 #define WMFlushConnection(cPtr) WMSendConnectionData((cPtr), NULL)
691 void WMSetConnectionDelegate(WMConnection *cPtr, ConnectionDelegate *delegate);
693 /* Connection info */
695 char* WMGetConnectionAddress(WMConnection *cPtr);
697 char* WMGetConnectionService(WMConnection *cPtr);
699 char* WMGetConnectionProtocol(WMConnection *cPtr);
701 void WMSetConnectionNonBlocking(WMConnection *cPtr, Bool flag);
703 void* WMGetConnectionClientData(WMConnection *cPtr);
705 void WMSetConnectionClientData(WMConnection *cPtr, void *data);
707 unsigned int WMGetConnectionFlags(WMConnection *cPtr);
709 void WMSetConnectionFlags(WMConnection *cPtr, unsigned int flags);
711 int WMGetConnectionSocket(WMConnection *cPtr);
713 WMConnectionState WMGetConnectionState(WMConnection *cPtr);
715 WMConnectionTimeoutState WMGetConnectionTimeoutState(WMConnection *cPtr);
718 * Passing timeout==0 in the SetTimeout functions below, will reset that
719 * timeout to its default value.
722 /* The default timeout inherited by all WMConnection operations, if none set */
723 void WMSetConnectionDefaultTimeout(unsigned int timeout);
725 /* Global timeout for all WMConnection objects, for opening a new connection */
726 void WMSetConnectionOpenTimeout(unsigned int timeout);
728 /* Connection specific timeout for sending out data */
729 void WMSetConnectionSendTimeout(WMConnection *cPtr, unsigned int timeout);
732 /* Global variables */
734 extern int WCErrorCode;
737 /*-------------------------------------------------------------------------*/
741 #ifdef __cplusplus
743 #endif /* __cplusplus */
746 #endif