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.
27 #include "e16-ecore_list.h"
30 #include <X11/cursorfont.h>
31 #if HAVE_X11_EXTENSIONS_XRENDER_H
32 #include <X11/extensions/Xrender.h>
38 unsigned int ref_count;
41 static Ecore_List *cursor_list = NULL;
43 #define ColorToPixel(ec) (0xff000000 + (ec->red << 16) + (ec->green <<8) + ec->blue)
46 ECreatePixmapCursor(Pixmap cpmap, Pixmap cmask, unsigned int w, unsigned int h,
47 int xh, int yh, EColor * fg, EColor * bg)
51 #if HAVE_X11_EXTENSIONS_XRENDER_H
52 /* Assuming we have XRenderCreateCursor (render >= 0.5) */
57 XRenderPictFormat *pictfmt;
59 pictfmt = XRenderFindStandardFormat(disp, PictStandardARGB32);
61 pmap = XCreatePixmap(disp, WinGetXwin(VROOT), w, h, 32);
64 gc = EXCreateGC(pmap, GCForeground, &gcv);
65 XFillRectangle(disp, pmap, gc, 0, 0, w, h);
66 gcv.fill_style = FillOpaqueStippled;
68 gcv.clip_mask = cmask;
69 gcv.foreground = ColorToPixel(fg);
70 gcv.background = ColorToPixel(bg);
72 GCForeground | GCBackground | GCFillStyle | GCStipple | GCClipMask,
74 XFillRectangle(disp, pmap, gc, 0, 0, w, h);
77 pict = XRenderCreatePicture(disp, pmap, pictfmt, 0, 0);
78 curs = XRenderCreateCursor(disp, pict, xh, yh);
80 XFreePixmap(disp, pmap);
81 XRenderFreePicture(disp, pict);
85 EAllocXColor(WinGetCmap(VROOT), &fgxc, fg);
86 EAllocXColor(WinGetCmap(VROOT), &bgxc, bg);
88 curs = XCreatePixmapCursor(disp, cpmap, cmask, &fgxc, &bgxc, xh, yh);
95 ECursorCreate(const char *name, const char *image, int native_id,
96 EColor * fg, EColor * bg)
101 unsigned int w, h, ww, hh;
102 char *img, msk[FILEPATH_LEN_MAX];
105 if ((!name) || (!image && native_id == -1))
110 img = FindFile(image, Mode.theme.path);
114 Esnprintf(msk, sizeof(msk), "%s.mask", img);
119 XReadBitmapFile(disp, WinGetXwin(VROOT), msk, &w, &h, &mask, &xh, &yh);
120 XReadBitmapFile(disp, WinGetXwin(VROOT), img, &w, &h, &pmap, &xh, &yh);
121 XQueryBestCursor(disp, WinGetXwin(VROOT), w, h, &ww, &hh);
123 if ((w <= ww) && (h <= hh) && (pmap))
125 if (xh < 0 || xh >= (int)w)
127 if (yh < 0 || yh >= (int)h)
129 curs = ECreatePixmapCursor(pmap, mask, w, h, xh, yh, fg, bg);
133 Eprintf("*** Failed to create cursor \"%s\" from %s,%s\n",
147 curs = (native_id == 999) ? None : XCreateFontCursor(disp, native_id);
150 ec = EMALLOC(ECursor, 1);
151 ec->name = Estrdup(name);
156 cursor_list = ecore_list_new();
157 ecore_list_prepend(cursor_list, ec);
163 ECursorDestroy(ECursor * ec)
168 if (ec->ref_count > 0)
170 DialogOK("ECursor Error!", _("%u references remain\n"), ec->ref_count);
174 ecore_list_node_remove(cursor_list, ec);
182 _ECursorMatchName(const void *data, const void *match)
184 return strcmp(((const ECursor *)data)->name, (const char *)match);
188 ECursorFind(const char *name)
190 if (!name || !name[0])
192 return (ECursor *) ecore_list_find(cursor_list, _ECursorMatchName, name);
196 ECursorAlloc(const char *name)
203 ec = ECursorFind(name);
211 ECursorFree(ECursor * ec)
218 ECursorConfigLoad(FILE * fs)
222 char s[FILEPATH_LEN_MAX];
223 char s2[FILEPATH_LEN_MAX];
226 char name[FILEPATH_LEN_MAX], *pname;
227 char file[FILEPATH_LEN_MAX], *pfile;
230 SET_COLOR(&clr, 0, 0, 0);
231 SET_COLOR(&clr2, 255, 255, 255);
233 pname = pfile = NULL;
235 while (GetLine(s, sizeof(s), fs))
237 i1 = ConfigParseline1(s, s2, &p2, NULL);
243 if (i2 != CONFIG_OPEN)
245 SET_COLOR(&clr, 0, 0, 0);
246 SET_COLOR(&clr2, 255, 255, 255);
247 pname = pfile = NULL;
251 ECursorCreate(pname, pfile, native_id, &clr, &clr2);
255 case CONFIG_CLASSNAME:
266 sscanf(p2, "%d %d %d", &r, &g, &b);
267 SET_COLOR(&clr, r, g, b);
271 sscanf(p2, "%d %d %d", &r, &g, &b);
272 SET_COLOR(&clr2, r, g, b);
279 native_id = atoi(s2);
288 ConfigAlertLoad("Cursor");
294 ECursorApply(ECursor * ec, Win win)
298 XDefineCursor(disp, WinGetXwin(win), ec->cursor);
302 ECursorGetByName(const char *name, const char *name2, unsigned int fallback)
306 ec = ECursorAlloc(name);
308 ec = ECursorAlloc(name2);
312 return XCreateFontCursor(disp, fallback);
315 static Cursor ECsrs[ECSR_COUNT];
320 return (which >= 0 && which < ECSR_COUNT) ? ECsrs[which] : None;
324 ECsrApply(int which, Window win)
326 XDefineCursor(disp, win, ECsrGet(which));
330 * Set up some basic cursors
335 ECsrs[ECSR_NONE] = None;
336 ECsrs[ECSR_ROOT] = ECursorGetByName("DEFAULT", NULL, XC_left_ptr);
337 ECsrs[ECSR_GRAB] = ECursorGetByName("GRAB", NULL, XC_crosshair);
338 ECsrs[ECSR_PGRAB] = ECursorGetByName("PGRAB", NULL, XC_X_cursor);
339 ECsrs[ECSR_ACT_MOVE] = ECursorGetByName("GRAB_MOVE", NULL, XC_fleur);
340 ECsrs[ECSR_ACT_RESIZE] = ECursorGetByName("GRAB_RESIZE", NULL, XC_sizing);
341 ECsrs[ECSR_ACT_RESIZE_H] =
342 ECursorGetByName("RESIZE_H", NULL, XC_sb_h_double_arrow);
343 ECsrs[ECSR_ACT_RESIZE_V] =
344 ECursorGetByName("RESIZE_V", NULL, XC_sb_v_double_arrow);
345 ECsrs[ECSR_ACT_RESIZE_TL] =
346 ECursorGetByName("RESIZE_TL", "RESIZE_BR", XC_top_left_corner);
347 ECsrs[ECSR_ACT_RESIZE_TR] =
348 ECursorGetByName("RESIZE_TR", "RESIZE_BL", XC_top_right_corner);
349 ECsrs[ECSR_ACT_RESIZE_BL] =
350 ECursorGetByName("RESIZE_BL", "RESIZE_TR", XC_bottom_left_corner);
351 ECsrs[ECSR_ACT_RESIZE_BR] =
352 ECursorGetByName("RESIZE_BR", "RESIZE_TL", XC_bottom_right_corner);
360 CursorSighan(int sig, void *prm __UNUSED__)
365 ConfigFileLoad("cursors.cfg", Mode.theme.path, ECursorConfigLoad, 1);
372 CursorsIpc(const char *params)
375 char cmd[128], prm[4096];
379 cmd[0] = prm[0] = '\0';
384 sscanf(p, "%100s %4000s %n", cmd, prm, &len);
388 if (!strncmp(cmd, "add", 3))
391 IpcPrintf("Not implemented\n");
393 else if (!strncmp(cmd, "del", 3))
395 ECursorDestroy(ECursorFind(prm));
397 else if (!strncmp(cmd, "list", 2))
399 ECORE_LIST_FOR_EACH(cursor_list, ec) IpcPrintf("%s\n", ec->name);
403 static const IpcItem CursorIpcArray[] = {
408 " cursor add <classname> ... Create cursor\n"
409 " cursor del <classname> Delete cursor\n"
410 " cursor list Show all cursors\n"}
412 #define N_IPC_FUNCS (sizeof(CursorIpcArray)/sizeof(IpcItem))
417 extern const EModule ModCursors;
418 const EModule ModCursors = {
421 {N_IPC_FUNCS, CursorIpcArray}