2 * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3 * Copyright (C) 2004-2008 Kim Woelders
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies of the Software, its documentation and marketing & publicity
14 * materials, and acknowledgment shall be given in the documentation, materials
15 * and software packages that this Software was used.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 /* This is a general quicksort algorithm, using median-of-three strategy.
34 * a: array of items to be sorted (list of void pointers).
35 * l: left edge of sub-array to be sorted. Toplevel call has 0 here.
36 * r: right edge of sub-array to be sorted. Toplevel call has |a| - 1 here.
37 * CompareFunc: Pointer to a function that accepts two general items d1 and d2
38 * and returns values as follows:
40 * < 0 --> d1 "smaller" than d2
41 * > 0 --> d1 "larger" than d2
44 * See sample application in ipc.c's IPC_Help.
47 Quicksort(void **a, int l, int r, int (*CompareFunc) (void *d1, void *d2))
57 if (CompareFunc(a[l], a[r]) > 0)
63 if (CompareFunc(a[l], a[m]) > 0)
69 if (CompareFunc(a[r], a[m]) > 0)
82 while (CompareFunc(a[++i], v) < 0)
84 while (CompareFunc(a[--j], v) > 0)
95 Quicksort(a, l, i - 1, CompareFunc);
96 Quicksort(a, i + 1, r, CompareFunc);
101 * Stuff to make loops for animated effects.
103 struct timeval etl_tv_start;
104 static int etl_k1, etl_k2;
105 static double etl_k, etl_fac;
108 * Return elapsed time in seconds since t0
111 ETimeElapsed(struct timeval *t0)
116 gettimeofday(&tv, NULL);
117 sec = tv.tv_sec - t0->tv_sec;
118 usec = tv.tv_usec - t0->tv_usec;
119 return (double)sec + (((double)usec) / 1000000);
125 ETimeCurve(int k1, int k2, float k, int mode)
129 if (k >= k2 || mode == 0)
137 case 1: /* Sinuoidal - half cycle */
138 x = x / l - 0.5; /* x: -0.5 -> 0.5 */
139 x = (float)(0.5 * (1. + sin(x * M_PI)));
141 case 2: /* Sinuoidal - quarter cycle */
142 x = x / l; /* x: 0 -> 1 */
143 x = (float)sin(x * M_PI / 2);
151 ETimedLoopInit(int k1, int k2, int speed)
157 /* When speed is 1000 the loop will take one sec. */
158 etl_fac = (k2 - k1) * (double)speed / 1000.;
160 gettimeofday(&etl_tv_start, NULL);
169 /* Is this portable? */
172 /* Find elapsed time since loop start */
173 tm = ETimeElapsed(&etl_tv_start);
174 etl_k = etl_k1 + tm * etl_fac;
176 Eprintf("ETimedLoopNext k=%4f tm=%.3f\n", etl_k, tm);
178 y = ETimeCurve(etl_k1, etl_k2, (float)etl_k, 2);
186 * Debug/error message printing.
188 static struct timeval tv0 = { 0, 0 };
191 _tvdiff(struct timeval *tvd, const struct timeval *tv1,
192 const struct timeval *tv2)
196 tsec = tv2->tv_sec - tv1->tv_sec;
197 tus = tv2->tv_usec - tv1->tv_usec;
208 Eprintf(const char *fmt, ...)
214 gettimeofday(&tv0, NULL);
216 gettimeofday(&tv, NULL);
217 _tvdiff(&tv, &tv0, &tv);
221 static struct timeval tv1 = { 0, 0 };
225 _tvdiff(&tvd, &tv1, &tv);
228 nreq = (disp) ? NextRequest(disp) : 0;
229 fprintf(stdout, "[%d] %#8lx %4ld.%06ld [%3ld.%06ld]: ", getpid(), nreq,
230 (long)tv1.tv_sec, tv1.tv_usec, (long)tvd.tv_sec, tvd.tv_usec);
234 fprintf(stdout, "[%d] %4ld.%06ld: ", getpid(),
235 (long)tv.tv_sec, tv.tv_usec);
239 vfprintf(stdout, fmt, args);
243 #if ENABLE_DEBUG_EVENTS
247 #define N_DEBUG_FLAGS 256
248 static char ev_debug;
249 static char ev_debug_flags[N_DEBUG_FLAGS];
252 * param is <ItemNumber>[:<ItemNumber> ... ]
257 * [ 2; 35 [ : X11 event codes, see /usr/include/X11/X.h
258 * [ 64; ... [ : Remapped X11 events, see events.h
259 * [ 128; 256 [ : E events, see E.h
262 EDebugInit(const char *param)
272 s = strchr(param, ':');
276 ix = strtol(param, NULL, 0);
280 if (ix < N_DEBUG_FLAGS)
283 ev_debug_flags[ix]++;
285 ev_debug_flags[ix] = 0;
294 EDebug(unsigned int type)
297 (type < sizeof(ev_debug_flags))) ? ev_debug_flags[type] : 0;
301 EDebugSet(unsigned int type, int value)
303 if (type >= sizeof(ev_debug_flags))
307 ev_debug_flags[type] += value;
314 * Dynamic module loading
319 ModLoadSym(const char *lib, const char *sym, const char *name)
324 Esnprintf(buf, sizeof(buf), "%s/lib%s_%s.so", ENLIGHTENMENT_LIB, lib, name);
326 Eprintf("ModLoad %s\n", buf);
327 h = dlopen(buf, RTLD_NOW | RTLD_LOCAL);
329 Eprintf("*** ModLoad %s: %s\n", buf, dlerror());
333 Esnprintf(buf, sizeof(buf), "%s_%s", sym, name);
336 Eprintf("*** ModLoad %s: %s\n", buf, dlerror());
341 #endif /* USE_MODULES */