chiark / gitweb /
Imported Upstream version 1.0.0
[e16] / src / cursors.c
1 /*
2  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3  * Copyright (C) 2004-2008 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 "E.h"
25 #include "conf.h"
26 #include "cursors.h"
27 #include "e16-ecore_list.h"
28 #include "emodule.h"
29 #include "xwin.h"
30 #include <X11/cursorfont.h>
31 #if HAVE_X11_EXTENSIONS_XRENDER_H
32 #include <X11/extensions/Xrender.h>
33 #endif
34
35 struct _ecursor {
36    char               *name;
37    Cursor              cursor;
38    unsigned int        ref_count;
39 };
40
41 static Ecore_List  *cursor_list = NULL;
42
43 #define ColorToPixel(ec) (0xff000000 + (ec->red << 16) + (ec->green <<8) + ec->blue)
44
45 static              Cursor
46 ECreatePixmapCursor(Pixmap cpmap, Pixmap cmask, unsigned int w, unsigned int h,
47                     int xh, int yh, EColor * fg, EColor * bg)
48 {
49    Cursor              curs;
50
51 #if HAVE_X11_EXTENSIONS_XRENDER_H
52    /* Assuming we have XRenderCreateCursor (render >= 0.5) */
53    Pixmap              pmap;
54    XGCValues           gcv;
55    GC                  gc;
56    Picture             pict;
57    XRenderPictFormat  *pictfmt;
58
59    pictfmt = XRenderFindStandardFormat(disp, PictStandardARGB32);
60
61    pmap = XCreatePixmap(disp, WinGetXwin(VROOT), w, h, 32);
62
63    gcv.foreground = 0;
64    gc = EXCreateGC(pmap, GCForeground, &gcv);
65    XFillRectangle(disp, pmap, gc, 0, 0, w, h);
66    gcv.fill_style = FillOpaqueStippled;
67    gcv.stipple = cpmap;
68    gcv.clip_mask = cmask;
69    gcv.foreground = ColorToPixel(fg);
70    gcv.background = ColorToPixel(bg);
71    XChangeGC(disp, gc,
72              GCForeground | GCBackground | GCFillStyle | GCStipple | GCClipMask,
73              &gcv);
74    XFillRectangle(disp, pmap, gc, 0, 0, w, h);
75    EXFreeGC(gc);
76
77    pict = XRenderCreatePicture(disp, pmap, pictfmt, 0, 0);
78    curs = XRenderCreateCursor(disp, pict, xh, yh);
79
80    XFreePixmap(disp, pmap);
81    XRenderFreePicture(disp, pict);
82 #else
83    XColor              fgxc, bgxc;
84
85    EAllocXColor(WinGetCmap(VROOT), &fgxc, fg);
86    EAllocXColor(WinGetCmap(VROOT), &bgxc, bg);
87
88    curs = XCreatePixmapCursor(disp, cpmap, cmask, &fgxc, &bgxc, xh, yh);
89    w = h = 0;
90 #endif
91    return curs;
92 }
93
94 static ECursor     *
95 ECursorCreate(const char *name, const char *image, int native_id,
96               EColor * fg, EColor * bg)
97 {
98    Cursor              curs;
99    Pixmap              pmap, mask;
100    int                 xh, yh;
101    unsigned int        w, h, ww, hh;
102    char               *img, msk[FILEPATH_LEN_MAX];
103    ECursor            *ec;
104
105    if ((!name) || (!image && native_id == -1))
106       return NULL;
107
108    if (image)
109      {
110         img = FindFile(image, Mode.theme.path);
111         if (!img)
112            return NULL;
113
114         Esnprintf(msk, sizeof(msk), "%s.mask", img);
115         pmap = 0;
116         mask = 0;
117         xh = 0;
118         yh = 0;
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);
122         curs = None;
123         if ((w <= ww) && (h <= hh) && (pmap))
124           {
125              if (xh < 0 || xh >= (int)w)
126                 xh = (int)w / 2;
127              if (yh < 0 || yh >= (int)h)
128                 yh = (int)h / 2;
129              curs = ECreatePixmapCursor(pmap, mask, w, h, xh, yh, fg, bg);
130           }
131
132         if (!curs)
133            Eprintf("*** Failed to create cursor \"%s\" from %s,%s\n",
134                    name, img, msk);
135
136         if (pmap)
137            EFreePixmap(pmap);
138         if (mask)
139            EFreePixmap(mask);
140         Efree(img);
141
142         if (!curs)
143            return NULL;
144      }
145    else
146      {
147         curs = (native_id == 999) ? None : XCreateFontCursor(disp, native_id);
148      }
149
150    ec = EMALLOC(ECursor, 1);
151    ec->name = Estrdup(name);
152    ec->cursor = curs;
153    ec->ref_count = 0;
154
155    if (!cursor_list)
156       cursor_list = ecore_list_new();
157    ecore_list_prepend(cursor_list, ec);
158
159    return ec;
160 }
161
162 static void
163 ECursorDestroy(ECursor * ec)
164 {
165    if (!ec)
166       return;
167
168    if (ec->ref_count > 0)
169      {
170         DialogOK("ECursor Error!", _("%u references remain\n"), ec->ref_count);
171         return;
172      }
173
174    ecore_list_node_remove(cursor_list, ec);
175
176    Efree(ec->name);
177
178    Efree(ec);
179 }
180
181 static int
182 _ECursorMatchName(const void *data, const void *match)
183 {
184    return strcmp(((const ECursor *)data)->name, (const char *)match);
185 }
186
187 static ECursor     *
188 ECursorFind(const char *name)
189 {
190    if (!name || !name[0])
191       return NULL;
192    return (ECursor *) ecore_list_find(cursor_list, _ECursorMatchName, name);
193 }
194
195 ECursor            *
196 ECursorAlloc(const char *name)
197 {
198    ECursor            *ec;
199
200    if (!name)
201       return NULL;
202
203    ec = ECursorFind(name);
204    if (ec)
205       ec->ref_count++;
206
207    return ec;
208 }
209
210 void
211 ECursorFree(ECursor * ec)
212 {
213    if (ec)
214       ec->ref_count--;
215 }
216
217 static int
218 ECursorConfigLoad(FILE * fs)
219 {
220    int                 err = 0;
221    EColor              clr, clr2;
222    char                s[FILEPATH_LEN_MAX];
223    char                s2[FILEPATH_LEN_MAX];
224    char               *p2;
225    int                 i1, i2, r, g, b;
226    char                name[FILEPATH_LEN_MAX], *pname;
227    char                file[FILEPATH_LEN_MAX], *pfile;
228    int                 native_id = -1;
229
230    SET_COLOR(&clr, 0, 0, 0);
231    SET_COLOR(&clr2, 255, 255, 255);
232
233    pname = pfile = NULL;
234
235    while (GetLine(s, sizeof(s), fs))
236      {
237         i1 = ConfigParseline1(s, s2, &p2, NULL);
238         switch (i1)
239           {
240           case CONFIG_CURSOR:
241              err = -1;
242              i2 = atoi(s2);
243              if (i2 != CONFIG_OPEN)
244                 goto done;
245              SET_COLOR(&clr, 0, 0, 0);
246              SET_COLOR(&clr2, 255, 255, 255);
247              pname = pfile = NULL;
248              native_id = -1;
249              break;
250           case CONFIG_CLOSE:
251              ECursorCreate(pname, pfile, native_id, &clr, &clr2);
252              err = 0;
253              break;
254
255           case CONFIG_CLASSNAME:
256              if (ECursorFind(s2))
257                {
258                   SkipTillEnd(fs);
259                   goto done;
260                }
261              strcpy(name, s2);
262              pname = name;
263              break;
264           case CURS_BG_RGB:
265              r = g = b = 0;
266              sscanf(p2, "%d %d %d", &r, &g, &b);
267              SET_COLOR(&clr, r, g, b);
268              break;
269           case CURS_FG_RGB:
270              r = g = b = 255;
271              sscanf(p2, "%d %d %d", &r, &g, &b);
272              SET_COLOR(&clr2, r, g, b);
273              break;
274           case XBM_FILE:
275              strcpy(file, s2);
276              pfile = file;
277              break;
278           case NATIVE_ID:
279              native_id = atoi(s2);
280              break;
281           default:
282              break;
283           }
284      }
285
286  done:
287    if (err)
288       ConfigAlertLoad("Cursor");
289
290    return err;
291 }
292
293 void
294 ECursorApply(ECursor * ec, Win win)
295 {
296    if (!ec)
297       return;
298    XDefineCursor(disp, WinGetXwin(win), ec->cursor);
299 }
300
301 static              Cursor
302 ECursorGetByName(const char *name, const char *name2, unsigned int fallback)
303 {
304    ECursor            *ec;
305
306    ec = ECursorAlloc(name);
307    if (!ec && name2)
308       ec = ECursorAlloc(name2);
309    if (ec)
310       return ec->cursor;
311
312    return XCreateFontCursor(disp, fallback);
313 }
314
315 static Cursor       ECsrs[ECSR_COUNT];
316
317 Cursor
318 ECsrGet(int which)
319 {
320    return (which >= 0 && which < ECSR_COUNT) ? ECsrs[which] : None;
321 }
322
323 void
324 ECsrApply(int which, Window win)
325 {
326    XDefineCursor(disp, win, ECsrGet(which));
327 }
328
329 /*
330  * Set up some basic cursors
331  */
332 static void
333 CursorsInit(void)
334 {
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);
353 }
354
355 /*
356  * Cursor module
357  */
358
359 static void
360 CursorSighan(int sig, void *prm __UNUSED__)
361 {
362    switch (sig)
363      {
364      case ESIGNAL_INIT:
365         ConfigFileLoad("cursors.cfg", Mode.theme.path, ECursorConfigLoad, 1);
366         CursorsInit();
367         break;
368      }
369 }
370
371 static void
372 CursorsIpc(const char *params)
373 {
374    const char         *p;
375    char                cmd[128], prm[4096];
376    int                 len;
377    ECursor            *ec;
378
379    cmd[0] = prm[0] = '\0';
380    p = params;
381    if (p)
382      {
383         len = 0;
384         sscanf(p, "%100s %4000s %n", cmd, prm, &len);
385         p += len;
386      }
387
388    if (!strncmp(cmd, "add", 3))
389      {
390         /* TBD */
391         IpcPrintf("Not implemented\n");
392      }
393    else if (!strncmp(cmd, "del", 3))
394      {
395         ECursorDestroy(ECursorFind(prm));
396      }
397    else if (!strncmp(cmd, "list", 2))
398      {
399         ECORE_LIST_FOR_EACH(cursor_list, ec) IpcPrintf("%s\n", ec->name);
400      }
401 }
402
403 static const IpcItem CursorIpcArray[] = {
404    {
405     CursorsIpc,
406     "cursor", "csr",
407     "Cursor functions",
408     "  cursor add <classname> ...        Create cursor\n"
409     "  cursor del <classname>            Delete cursor\n"
410     "  cursor list                       Show all cursors\n"}
411 };
412 #define N_IPC_FUNCS (sizeof(CursorIpcArray)/sizeof(IpcItem))
413
414 /*
415  * Module descriptor
416  */
417 extern const EModule ModCursors;
418 const EModule       ModCursors = {
419    "cursor", "csr",
420    CursorSighan,
421    {N_IPC_FUNCS, CursorIpcArray}
422    ,
423    {0, NULL}
424 };