]>
Commit | Line | Data |
---|---|---|
4c1bd65b MG |
1 | /* -copyright- |
2 | #-# Copyright © 2021 Eric Bina, Dave Black, TJ Phan, | |
3 | #-# Vincent Renardias, Willem Vermin | |
4 | #-# | |
5 | #-# Permission is hereby granted, free of charge, to any person | |
6 | #-# obtaining a copy of this software and associated documentation | |
7 | #-# files (the “Software”), to deal in the Software without | |
8 | #-# restriction, including without limitation the rights to use, | |
9 | #-# copy, modify, merge, publish, distribute, sublicense, and/or | |
10 | #-# sell copies of the Software, and to permit persons to whom | |
11 | #-# the Software is furnished to do so, subject to the following | |
12 | #-# conditions: | |
13 | #-# | |
14 | #-# The above copyright notice and this permission notice shall | |
15 | #-# be included in all copies or substantial portions of the Software. | |
16 | #-# | |
17 | #-# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, | |
18 | #-# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
19 | #-# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
20 | #-# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
21 | #-# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
22 | #-# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
23 | #-# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
24 | #-# OTHER DEALINGS IN THE SOFTWARE. | |
25 | #-# | |
26 | */ | |
27 | #ifdef HAVE_CONFIG_H | |
28 | #include "config.h" | |
29 | #endif | |
30 | #include <stdio.h> | |
31 | #include <string.h> | |
32 | #include <gtk/gtk.h> | |
33 | #include <stdlib.h> | |
34 | #include <X11/Intrinsic.h> | |
35 | #include "utils.h" | |
36 | #include "debug.h" | |
37 | #include "xfishtank.h" | |
38 | ||
39 | ||
40 | Pixel Black, White; | |
41 | ||
42 | ssize_t mywrite(int fd, const void *buf, size_t count) | |
43 | { | |
44 | const size_t m = 4096; // max per write | |
45 | size_t w = 0; // # written chars | |
46 | char *b = (char *)buf; | |
47 | ||
48 | while (w < count) | |
49 | { | |
50 | size_t l = count - w; | |
51 | if (l > m) | |
52 | l = m; | |
53 | ssize_t x = write(fd, b+w, l); | |
54 | if (x < 0) | |
55 | return -1; | |
56 | w += x; | |
57 | } | |
58 | return 0; | |
59 | } | |
60 | ||
61 | FILE *HomeOpen(const char *file,const char *mode, char **path) | |
62 | { | |
63 | char *h = getenv("HOME"); | |
64 | if (h == NULL) | |
65 | return NULL; | |
66 | char *home = strdup(h); | |
67 | (*path) = (char *) malloc((strlen(home)+strlen(file)+2)*sizeof(char)); | |
68 | strcpy(*path,home); | |
69 | strcat(*path,"/"); | |
70 | strcat(*path,file); | |
71 | FILE *f = fopen(*path,mode); | |
72 | free(home); | |
73 | return f; | |
74 | } | |
75 | ||
76 | ||
77 | float sq3(float x, float y, float z) | |
78 | { | |
79 | return x*x + y*y + z*z; | |
80 | } | |
81 | ||
82 | float sq2(float x, float y) | |
83 | { | |
84 | return x*x + y*y; | |
85 | } | |
86 | ||
87 | float fsgnf(float x) | |
88 | { | |
89 | if (x>0) return 1.0f; | |
90 | if (x<0) return -1.0f; | |
91 | return 0.0f; | |
92 | } | |
93 | ||
94 | int sgnf(float x) | |
95 | { | |
96 | if (x > 0) return 1; | |
97 | if (x < 0) return -1; | |
98 | return 0; | |
99 | } | |
100 | ||
101 | ||
102 | int RandInt(int m) | |
103 | { | |
104 | if (m <=0 ) | |
105 | return 0; | |
106 | return drand48()*m; | |
107 | } | |
108 | ||
109 | int is_little_endian(void) | |
110 | { | |
111 | int endiantest = 1; | |
112 | return (*(char *)&endiantest) == 1; | |
113 | } | |
114 | ||
115 | void my_cairo_paint_with_alpha(cairo_t *cr, double alpha) | |
116 | { | |
117 | if (alpha > 0.9) | |
118 | cairo_paint(cr); | |
119 | else | |
120 | cairo_paint_with_alpha(cr,alpha); | |
121 | P("%d alpha %f\n",counter++,alpha); | |
122 | } | |
123 | ||
124 | /* from 'dsimple.c' | |
125 | * Window_With_Name: routine to locate a window with a given name on a display. | |
126 | * If no window with the given name is found, 0 is returned. | |
127 | * If more than one window has the given name, the first | |
128 | * one found will be returned. Only top and its subwindows | |
129 | * are looked at. Normally, top should be the RootWindow. | |
130 | */ | |
131 | Window Window_With_Name( Display *dpy, Window top, const char *name) | |
132 | { | |
133 | Window *children, dummy; | |
134 | unsigned int nchildren; | |
135 | int i; | |
136 | Window w=0; | |
137 | char *window_name; | |
138 | ||
139 | // this leaks memory: | |
140 | //if (XFetchName(dpy, top, &window_name) && !strcmp(window_name, name)) | |
141 | // return(top); | |
142 | // therefore: | |
143 | if (XFetchName(dpy, top, &window_name) && !strcmp(window_name, name)) | |
144 | { | |
145 | XFree(window_name); | |
146 | return(top); | |
147 | } | |
148 | XFree(window_name); | |
149 | ||
150 | if (!XQueryTree(dpy, top, &dummy, &dummy, &children, &nchildren)) | |
151 | { | |
152 | return(0); | |
153 | } | |
154 | ||
155 | for (i=0; (unsigned int)i<nchildren; i++) { | |
156 | w = Window_With_Name(dpy, children[i], name); | |
157 | if (w) | |
158 | break; | |
159 | } | |
160 | if (children) XFree ((char *)children); | |
161 | return(w); | |
162 | } | |
163 | ||
164 | ||
165 | double wallcl() | |
166 | { | |
167 | return (double)g_get_real_time()*1.0e-6; | |
168 | } | |
169 | ||
170 | double wallclock() | |
171 | { | |
172 | return (double)g_get_monotonic_time()*1.0e-6; | |
173 | } | |
174 | ||
175 | ||
176 | // usage of strncpy gives useless warning "-Wstringop-truncation", | |
177 | // so here is a local version: | |
178 | void mystrncpy(char *dest, const char *src, size_t n) | |
179 | { | |
180 | // see if there is a null-byte within src. In that case we do a strcpy: | |
181 | size_t i; | |
182 | for (i = 0; i<n; i++) | |
183 | { | |
184 | if (src[i] == '\0') | |
185 | { | |
186 | strcpy(dest, src); | |
187 | P("strcpy\n"); | |
188 | return; | |
189 | } | |
190 | } | |
191 | // simply copy n chars: | |
192 | P("memcpy\n"); | |
193 | memcpy(dest,src,n*sizeof(char)); | |
194 | } | |
195 | ||
196 | void rgba2color(GdkRGBA *c, char **s) | |
197 | { | |
198 | *s = (char *)malloc(8); | |
199 | sprintf(*s,"#%02lx%02lx%02lx",lrint(c->red*255),lrint(c->green*255),lrint(c->blue*255)); | |
200 | } | |
201 |