💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Menkar › files › 19c75aa90f4f8fe93374f037f97… captured on 2023-12-28 at 15:39:49. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-09-08)
-=-=-=-=-=-=-
0 /*
1 * lib.c - various routines
2 */
3
4 /*
5 * Copyright 2006 Johan Veenhuizen
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 #include <X11/Xlib.h>
34 #include <X11/Xutil.h>
35
36 #include "global.h"
37 #include "lib.h"
38 #include "widget.h"
39
40 void error(const char *fmt, ...)
41 {
42 va_list ap;
43
44 va_start(ap, fmt);
45 fprintf(stderr, "karmen: ");
46 vfprintf(stderr, fmt, ap);
47 fprintf(stderr, "\n");
48 fflush(stderr);
49 va_end(ap);
50 }
51
52 void debug(const char *fmt, ...)
53 {
54 #if defined(DEBUG)
55 va_list ap;
56
57 va_start(ap, fmt);
58 fprintf(stderr, "karmen: DEBUG: ");
59 vfprintf(stderr, fmt, ap);
60 fprintf(stderr, "\n");
61 fflush(stderr);
62 va_end(ap);
63 #endif
64 }
65
66 void putimage(Display *display, Drawable d, GC gc, IMAGE *image,
67 int x, int y)
68 {
69 if (image->pixmap == None)
70 image->pixmap = XCreateBitmapFromData(display, d,
71 (char *)image->data, image->width, image->height);
72 XCopyPlane(display, image->pixmap, d, gc,
73 0, 0, image->width, image->height, x, y, 1);
74 }
75
76 void drawraised(Drawable d, GC gc, struct color *color,
77 int x, int y, int w, int h)
78 {
79 XSetForeground(display, gc, color->bright2);
80 XDrawLine(display, d, gc, x, y, x+w-3, y);
81 XDrawLine(display, d, gc, x, y+1, x, y+h-3);
82
83 XSetForeground(display, gc, BlackPixel(display, screen));
84 XDrawLine(display, d, gc, x+w-1, y, x+w-1, y+h-1);
85 XDrawLine(display, d, gc, x, y+h-1, x+w-2, y+h-1);
86
87 XSetForeground(display, gc, color->shadow2);
88 XDrawLine(display, d, gc, x+w-2, y+1, x+w-2, y+h-2);
89 XDrawLine(display, d, gc, x+1, y+h-2, x+w-3, y+h-2);
90
91 XSetForeground(display, gc, color->normal);
92 XDrawPoint(display, d, gc, x+w-2, y);
93 XDrawPoint(display, d, gc, x, y+h-2);
94 }
95
96 void drawlowered(Drawable d, GC gc, struct color *color,
97 int x, int y, int w, int h)
98 {
99 XSetForeground(display, gc, color->shadow2);
100 XDrawLine(display, d, gc, x, y, x+w-2, y);
101 XDrawLine(display, d, gc, x, y+1, x, y+h-2);
102
103 XSetForeground(display, gc, BlackPixel(display, screen));
104 XDrawLine(display, d, gc, x+1, y+1, x+w-2, y+1);
105 XDrawLine(display, d, gc, x+1, y+2, x+1, y+h-2);
106
107 XSetForeground(display, gc, color->bright2);
108 XDrawLine(display, d, gc, x+w-1, y+1, x+w-1, y+h-1);
109 XDrawLine(display, d, gc, x+1, y+h-1, x+w-2, y+h-1);
110
111 XSetForeground(display, gc, color->normal);
112 XDrawPoint(display, d, gc, x+w-1, y);
113 XDrawPoint(display, d, gc, x, x+h-1);
114 }
115
116 void drawdepressed(Drawable d, GC gc, struct color *color,
117 int x, int y, int w, int h)
118 {
119 XSetForeground(display, gc, BlackPixel(display, screen));
120 XDrawLine(display, d, gc, x, y, x+w-1, y);
121 XDrawLine(display, d, gc, x, y+1, x, y+h-1);
122
123 XSetForeground(display, gc, color->bright1);
124 XDrawLine(display, d, gc, x+1, y+1, x+w-2, y+1);
125 XDrawLine(display, d, gc, x+1, y+2, x+1, y+h-2);
126
127 XSetForeground(display, gc, color->shadow2);
128 XDrawLine(display, d, gc, x+w-1, y+2, x+w-1, y+h-1);
129 XDrawLine(display, d, gc, x+2, y+h-1, x+w-2, y+h-1);
130
131 XSetForeground(display, gc, color->normal);
132 XDrawPoint(display, d, gc, x+w-1, y+1);
133 XDrawPoint(display, d, gc, x+1, x+h-1);
134 }
135
136 int stringwidth(const char *str)
137 {
138 XCharStruct ch;
139 int direction;
140 int ascent;
141 int descent;
142
143 XTextExtents(font, str, strlen(str), &direction,
144 &ascent, &descent, &ch);
145 return ch.width;
146 }
147
148 char *stringfit(char *str, int width)
149 {
150 int len;
151
152 len = strlen(str);
153 while (stringwidth(str) > width) {
154 if (len < 3) {
155 str[0] = '\0';
156 break;
157 }
158 strcpy(str + len - 3, "...");
159 len--;
160 }
161 return str;
162 }
163
164 void *MALLOC(size_t size)
165 {
166 void *ptr;
167
168 while ((ptr = malloc(size)) == NULL && size != 0)
169 sleep(1);
170 return ptr;
171 }
172
173 void *REALLOC(void *optr, size_t size)
174 {
175 void *nptr;
176
177 while ((nptr = realloc(optr, size)) == NULL && size != 0)
178 sleep(1);
179 return nptr;
180 }
181
182 void FREE(void *ptr)
183 {
184 free(ptr);
185 }
186
187 char *STRDUP(const char *str)
188 {
189 char *new;
190
191 assert(str != NULL);
192 new = MALLOC(strlen(str) + 1);
193 return strcpy(new, str);
194 }
195
196 static Window fastmovewin = None;
197 Window fastmovewin_current = None;
198
199 void beginfastmove(Window xwin)
200 {
201 XSetWindowAttributes attr;
202
203 if (fastmovewin != None)
204 return;
205
206 attr.override_redirect = True;
207 fastmovewin = XCreateWindow(display, root,
208 0, 0, DisplayWidth(display, screen),
209 DisplayHeight(display, screen), 0, CopyFromParent, InputOnly,
210 CopyFromParent, CWOverrideRedirect, &attr);
211 XMapWindow(display, fastmovewin);
212 XGrabPointer(display, xwin, False,
213 ButtonMotionMask | ButtonReleaseMask,
214 GrabModeAsync, GrabModeAsync,
215 fastmovewin, None, CurrentTime);
216 fastmovewin_current = xwin;
217 }
218
219 void endfastmove(void)
220 {
221 fastmovewin_current = None;
222 if (fastmovewin == None)
223 return;
224
225 XUngrabPointer(display, CurrentTime);
226 XDestroyWindow(display, fastmovewin);
227 fastmovewin = None;
228 }
229
230 #define CASE(type) case type: return #type
231 const char *eventname(int type)
232 {
233 switch (type) {
234 CASE(KeyPress);
235 CASE(KeyRelease);
236 CASE(ButtonPress);
237 CASE(ButtonRelease);
238 CASE(MotionNotify);
239 CASE(EnterNotify);
240 CASE(LeaveNotify);
241 CASE(FocusIn);
242 CASE(FocusOut);
243 CASE(KeymapNotify);
244 CASE(Expose);
245 CASE(GraphicsExpose);
246 CASE(NoExpose);
247 CASE(VisibilityNotify);
248 CASE(CreateNotify);
249 CASE(DestroyNotify);
250 CASE(UnmapNotify);
251 CASE(MapNotify);
252 CASE(MapRequest);
253 CASE(ReparentNotify);
254 CASE(ConfigureNotify);
255 CASE(ConfigureRequest);
256 CASE(GravityNotify);
257 CASE(ResizeRequest);
258 CASE(CirculateNotify);
259 CASE(CirculateRequest);
260 CASE(PropertyNotify);
261 CASE(SelectionClear);
262 CASE(SelectionRequest);
263 CASE(SelectionNotify);
264 CASE(ColormapNotify);
265 CASE(ClientMessage);
266 CASE(MappingNotify);
267 default:
268 return "INVALID EVENT";
269 }
270 }
271