1 /*
2 * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3 * Copyright (C) 2004-2021 Kim Woelders
4 *
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:
11 *
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.
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 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.
23 */
24 #include "config.h"
25
26 #include <X11/Xlib.h>
27 #include <X11/cursorfont.h>
28 #if USE_COMPOSITE
29 #include <X11/extensions/Xfixes.h>
30 #include <X11/extensions/Xrender.h>
31 #endif
32
33 #include "E.h"
34 #include "conf.h"
35 #include "cursors.h"
36 #include "emodule.h"
37 #include "list.h"
38 #include "xwin.h"
39
40 struct _ecursor {
41 dlist_t list;
42 char *name;
43 EX_Cursor cursor;
44 unsigned int ref_count;
45 char *file;
46 unsigned int bg;
47 unsigned int fg;
48 int native_id;
49 };
50
51 static LIST_HEAD(cursor_list);
52
53 #if USE_COMPOSITE
54 /* Assuming we have XRenderCreateCursor (render >= 0.5) */
55 static EX_Cursor
ECreatePixmapCursor(EX_Pixmap cpmap,EX_Pixmap cmask,unsigned int w,unsigned int h,int xh,int yh,unsigned int fg,unsigned int bg)56 ECreatePixmapCursor(EX_Pixmap cpmap, EX_Pixmap cmask, unsigned int w,
57 unsigned int h, int xh, int yh, unsigned int fg,
58 unsigned int bg)
59 {
60 EX_Cursor curs;
61 EX_Picture pict;
62 EX_SrvRegion rgn1, rgn2;
63
64 pict = EPictureCreateBuffer(VROOT, w, h, 1, NULL);
65
66 /* Clear entirely (alpha = 0) */
67 EPictureFillRect(pict, 0, 0, w, h, 0);
68
69 /* Set bg color (cursor shape) */
70 rgn1 = ERegionCreateFromBitmap(cmask);
71 EPictureSetClip(pict, rgn1);
72 EPictureFillRect(pict, 0, 0, w, h, bg);
73
74 /* Set fg color */
75 rgn2 = ERegionCreateFromBitmap(cpmap);
76 ERegionIntersect(rgn1, rgn2);
77 EPictureSetClip(pict, rgn1);
78 EPictureFillRect(pict, 0, 0, w, h, fg);
79
80 curs = XRenderCreateCursor(disp, pict, xh, yh);
81
82 ERegionDestroy(rgn1);
83 ERegionDestroy(rgn2);
84 EPictureDestroy(pict);
85
86 return curs;
87 }
88 #else
89 static EX_Cursor
ECreatePixmapCursor(EX_Pixmap cpmap,EX_Pixmap cmask,unsigned int w __UNUSED__,unsigned int h __UNUSED__,int xh,int yh,unsigned int fg,unsigned int bg)90 ECreatePixmapCursor(EX_Pixmap cpmap, EX_Pixmap cmask,
91 unsigned int w __UNUSED__, unsigned int h __UNUSED__,
92 int xh, int yh, unsigned int fg, unsigned int bg)
93 {
94 EX_Cursor curs;
95 XColor fgxc, bgxc;
96
97 COLOR32_TO_RGB16(fg, fgxc.red, fgxc.green, fgxc.blue);
98 COLOR32_TO_RGB16(bg, bgxc.red, bgxc.green, bgxc.blue);
99 XAllocColor(disp, WinGetCmap(VROOT), &fgxc);
100 XAllocColor(disp, WinGetCmap(VROOT), &bgxc);
101
102 curs = XCreatePixmapCursor(disp, cpmap, cmask, &fgxc, &bgxc, xh, yh);
103
104 return curs;
105 }
106 #endif
107
108 static void
ECursorCreate(const char * name,const char * image,int native_id,unsigned int fg,unsigned int bg)109 ECursorCreate(const char *name, const char *image, int native_id,
110 unsigned int fg, unsigned int bg)
111 {
112 ECursor *ec;
113
114 if ((!name) || (!image && native_id == -1))
115 return;
116
117 ec = ECALLOC(ECursor, 1);
118 if (!ec)
119 return;
120
121 ec->name = Estrdup(name);
122
123 ec->file = Estrdup(image);
124 ec->fg = 0xff000000 | fg;
125 ec->bg = 0xff000000 | bg;
126 ec->native_id = native_id;
127
128 LIST_PREPEND(ECursor, &cursor_list, ec);
129 }
130
131 static void
ECursorDestroy(ECursor * ec)132 ECursorDestroy(ECursor * ec)
133 {
134 if (!ec)
135 return;
136
137 if (ec->ref_count > 0)
138 {
139 DialogOK("ECursor Error!", _("%u references remain"), ec->ref_count);
140 return;
141 }
142
143 LIST_REMOVE(ECursor, &cursor_list, ec);
144
145 Efree(ec->name);
146 Efree(ec->file);
147
148 Efree(ec);
149 }
150
151 static ECursor *
ECursorRealize(ECursor * ec)152 ECursorRealize(ECursor * ec)
153 {
154 Pixmap pmap, mask;
155 int xh, yh;
156 unsigned int w, h, ww, hh;
157 char *img, msk[FILEPATH_LEN_MAX];
158
159 if (ec->file)
160 {
161 img = ThemeFileFind(ec->file, FILE_TYPE_CURSOR);
162 EFREE_NULL(ec->file); /* Ok or not - we never need file again */
163 if (!img)
164 goto done;
165
166 Esnprintf(msk, sizeof(msk), "%s.mask", img);
167 pmap = 0;
168 mask = 0;
169 xh = 0;
170 yh = 0;
171 XReadBitmapFile(disp, WinGetXwin(VROOT), msk, &w, &h, &mask, &xh, &yh);
172 XReadBitmapFile(disp, WinGetXwin(VROOT), img, &w, &h, &pmap, &xh, &yh);
173 XQueryBestCursor(disp, WinGetXwin(VROOT), w, h, &ww, &hh);
174 if ((w <= ww) && (h <= hh) && (pmap))
175 {
176 if (xh < 0 || xh >= (int)w)
177 xh = (int)w / 2;
178 if (yh < 0 || yh >= (int)h)
179 yh = (int)h / 2;
180 ec->cursor =
181 ECreatePixmapCursor(pmap, mask, w, h, xh, yh, ec->fg, ec->bg);
182 }
183
184 if (ec->cursor == NoXID)
185 {
186 Eprintf("*** Failed to create cursor \"%s\" from %s,%s\n",
187 ec->name, img, msk);
188 }
189
190 if (pmap)
191 EFreePixmap(pmap);
192 if (mask)
193 EFreePixmap(mask);
194 Efree(img);
195 }
196 else
197 {
198 ec->cursor = (ec->native_id == 999) ?
199 None : XCreateFontCursor(disp, ec->native_id);
200 }
201
202 done:
203 if (ec->cursor == NoXID)
204 {
205 ECursorDestroy(ec);
206 ec = NULL;
207 }
208
209 return ec;
210 }
211
212 static int
_ECursorMatchName(const void * data,const void * match)213 _ECursorMatchName(const void *data, const void *match)
214 {
215 return strcmp(((const ECursor *)data)->name, (const char *)match);
216 }
217
218 static ECursor *
ECursorFind(const char * name)219 ECursorFind(const char *name)
220 {
221 if (!name || !name[0])
222 return NULL;
223 return LIST_FIND(ECursor, &cursor_list, _ECursorMatchName, name);
224 }
225
226 ECursor *
ECursorAlloc(const char * name)227 ECursorAlloc(const char *name)
228 {
229 ECursor *ec;
230
231 if (!name)
232 return NULL;
233
234 ec = ECursorFind(name);
235 if (!ec)
236 return NULL;
237
238 if (ec->cursor == NoXID)
239 ec = ECursorRealize(ec);
240 if (!ec)
241 return NULL;
242
243 ec->ref_count++;
244
245 return ec;
246 }
247
248 void
ECursorFree(ECursor * ec)249 ECursorFree(ECursor * ec)
250 {
251 if (ec)
252 ec->ref_count--;
253 }
254
255 int
ECursorConfigLoad(FILE * fs)256 ECursorConfigLoad(FILE * fs)
257 {
258 int err = 0;
259 unsigned int clr, clr2;
260 char s[FILEPATH_LEN_MAX];
261 char s2[FILEPATH_LEN_MAX];
262 char *p2;
263 int i1, i2, r, g, b;
264 char name[FILEPATH_LEN_MAX], *pname;
265 char file[FILEPATH_LEN_MAX], *pfile;
266 int native_id = -1;
267
268 COLOR32_FROM_RGB(clr, 0, 0, 0);
269 COLOR32_FROM_RGB(clr2, 255, 255, 255);
270
271 pname = pfile = NULL;
272
273 while (GetLine(s, sizeof(s), fs))
274 {
275 i1 = ConfigParseline1(s, s2, &p2, NULL);
276 switch (i1)
277 {
278 case CONFIG_CURSOR:
279 err = -1;
280 i2 = atoi(s2);
281 if (i2 != CONFIG_OPEN)
282 goto done;
283 COLOR32_FROM_RGB(clr, 0, 0, 0);
284 COLOR32_FROM_RGB(clr2, 255, 255, 255);
285 pname = pfile = NULL;
286 native_id = -1;
287 break;
288 case CONFIG_CLOSE:
289 ECursorCreate(pname, pfile, native_id, clr, clr2);
290 err = 0;
291 break;
292
293 case CONFIG_CLASSNAME:
294 if (ECursorFind(s2))
295 {
296 SkipTillEnd(fs);
297 goto done;
298 }
299 strcpy(name, s2);
300 pname = name;
301 break;
302 case CURS_BG_RGB:
303 r = g = b = 0;
304 sscanf(p2, "%d %d %d", &r, &g, &b);
305 COLOR32_FROM_RGB(clr, r, g, b);
306 break;
307 case CURS_FG_RGB:
308 r = g = b = 255;
309 sscanf(p2, "%d %d %d", &r, &g, &b);
310 COLOR32_FROM_RGB(clr2, r, g, b);
311 break;
312 case XBM_FILE:
313 strcpy(file, s2);
314 pfile = file;
315 break;
316 case NATIVE_ID:
317 native_id = atoi(s2);
318 break;
319 default:
320 break;
321 }
322 }
323
324 done:
325 if (err)
326 ConfigAlertLoad("Cursor");
327
328 return err;
329 }
330
331 void
ECursorApply(ECursor * ec,Win win)332 ECursorApply(ECursor * ec, Win win)
333 {
334 if (!ec)
335 return;
336 XDefineCursor(disp, WinGetXwin(win), ec->cursor);
337 }
338
339 static EX_Cursor
ECursorGetByName(const char * name,const char * name2,unsigned int fallback)340 ECursorGetByName(const char *name, const char *name2, unsigned int fallback)
341 {
342 ECursor *ec;
343
344 ec = ECursorAlloc(name);
345 if (!ec && name2)
346 ec = ECursorAlloc(name2);
347 if (ec)
348 return ec->cursor;
349
350 return XCreateFontCursor(disp, fallback);
351 }
352
353 typedef struct {
354 const char *pri;
355 const char *sec;
356 unsigned int fallback;
357 } ECDataRec;
358
359 static const ECDataRec ECData[ECSR_COUNT] = {
360 {"DEFAULT", NULL, XC_left_ptr},
361 {"GRAB", NULL, XC_crosshair},
362 {"PGRAB", NULL, XC_X_cursor},
363 {"GRAB_MOVE", NULL, XC_fleur},
364 {"GRAB_RESIZE", NULL, XC_sizing},
365 {"RESIZE_H", NULL, XC_sb_h_double_arrow},
366 {"RESIZE_V", NULL, XC_sb_v_double_arrow},
367 {"RESIZE_TL", "RESIZE_BR", XC_top_left_corner},
368 {"RESIZE_TR", "RESIZE_BL", XC_top_right_corner},
369 {"RESIZE_BL", "RESIZE_TR", XC_bottom_left_corner},
370 {"RESIZE_BR", "RESIZE_TL", XC_bottom_right_corner},
371 };
372
373 static EX_Cursor ECsrs[ECSR_COUNT] = {
374 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
375 };
376
377 EX_Cursor
ECsrGet(int which)378 ECsrGet(int which)
379 {
380 if (which < 0 || which >= ECSR_COUNT)
381 return NoXID;
382 if (ECsrs[which] == 1)
383 ECsrs[which] = ECursorGetByName(ECData[which].pri, ECData[which].sec,
384 ECData[which].fallback);
385
386 return ECsrs[which];
387 }
388
389 void
ECsrApply(int which,EX_Window win)390 ECsrApply(int which, EX_Window win)
391 {
392 XDefineCursor(disp, win, ECsrGet(which));
393 }
394
395 static void
CursorsIpc(const char * params)396 CursorsIpc(const char *params)
397 {
398 const char *p;
399 char cmd[128], prm[4096];
400 int len;
401 ECursor *ec;
402
403 cmd[0] = prm[0] = '\0';
404 p = params;
405 if (p)
406 {
407 len = 0;
408 sscanf(p, "%100s %4000s %n", cmd, prm, &len);
409 }
410
411 if (!strncmp(cmd, "list", 2))
412 {
413 LIST_FOR_EACH(ECursor, &cursor_list, ec) IpcPrintf("%s\n", ec->name);
414 }
415 }
416
417 static const IpcItem CursorIpcArray[] = {
418 {
419 CursorsIpc,
420 "cursor", "csr",
421 "Cursor functions",
422 " cursor list Show all cursors\n"}
423 };
424
425 /*
426 * Module descriptor
427 */
428 extern const EModule ModCursors;
429
430 const EModule ModCursors = {
431 "cursor", "csr",
432 NULL,
433 MOD_ITEMS(CursorIpcArray),
434 {0, NULL}
435 };
436