aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/ecore_x/ecore_x_cursor.c
blob: 46ac42e59a79ba1d9e5bd14b05bb9343d773beec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* ifdef HAVE_CONFIG_H */

#include <stdlib.h>

#include "ecore_x_private.h"

EAPI Eina_Bool
ecore_x_cursor_color_supported_get(void)
{
   return _ecore_x_xcursor;
}

EAPI Ecore_X_Cursor
ecore_x_cursor_new(Ecore_X_Window win,
                   int *pixels,
                   int w,
                   int h,
                   int hot_x,
                   int hot_y)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(_ecore_x_disp, 0);
#ifdef ECORE_XCURSOR
   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (_ecore_x_xcursor)
     {
        Cursor c;
        XcursorImage *xci;

        xci = XcursorImageCreate(w, h);
        if (_ecore_xlib_sync) ecore_x_sync();
        if (xci)
          {
             int i;

             xci->xhot = hot_x;
             xci->yhot = hot_y;
             xci->delay = 0;
             for (i = 0; i < (w * h); i++)
               {
//		  int r, g, b, a;
//
//		  a = (pixels[i] >> 24) & 0xff;
//		  r = (((pixels[i] >> 16) & 0xff) * a) / 0xff;
//		  g = (((pixels[i] >> 8 ) & 0xff) * a) / 0xff;
//		  b = (((pixels[i]      ) & 0xff) * a) / 0xff;
                  xci->pixels[i] = pixels[i];
//		    (a << 24) | (r << 16) | (g << 8) | (b);
               }
             c = XcursorImageLoadCursor(_ecore_x_disp, xci);
             if (_ecore_xlib_sync) ecore_x_sync();
             XcursorImageDestroy(xci);
             return c;
          }
     }
   else
#endif /* ifdef ECORE_XCURSOR */
   {
      XColor c1, c2;
      Cursor c;
      Pixmap pmap, mask;
      GC gc;
      XGCValues gcv;
      XImage *xim;
      unsigned int *pix;
      int fr, fg, fb, br, bg, bb;
      int brightest = 0;
      int darkest = 255 * 3;
      int x, y;
      const int dither[2][2] =
      {
         {0, 2},
         {3, 1}
      };

      pmap = XCreatePixmap(_ecore_x_disp, win, w, h, 1);
      if (_ecore_xlib_sync) ecore_x_sync();
      mask = XCreatePixmap(_ecore_x_disp, win, w, h, 1);
      if (_ecore_xlib_sync) ecore_x_sync();
      xim = XCreateImage(_ecore_x_disp,
                         DefaultVisual(_ecore_x_disp, 0),
                         1, ZPixmap, 0, NULL, w, h, 32, 0);
      if (_ecore_xlib_sync) ecore_x_sync();
      xim->data = malloc(xim->bytes_per_line * xim->height);

      fr = 0x00; fg = 0x00; fb = 0x00;
      br = 0xff; bg = 0xff; bb = 0xff;
      pix = (unsigned int *)pixels;
      for (y = 0; y < h; y++)
        {
           for (x = 0; x < w; x++)
             {
                int r, g, b, a;

                a = (pix[0] >> 24) & 0xff;
                r = (pix[0] >> 16) & 0xff;
                g = (pix[0] >> 8) & 0xff;
                b = (pix[0]) & 0xff;
                if (a > 0)
                  {
                     if ((r + g + b) > brightest)
                       {
                          brightest = r + g + b;
                          br = r;
                          bg = g;
                          bb = b;
                       }

                     if ((r + g + b) < darkest)
                       {
                          darkest = r + g + b;
                          fr = r;
                          fg = g;
                          fb = b;
                       }
                  }

                pix++;
             }
        }

      pix = (unsigned int *)pixels;
      for (y = 0; y < h; y++)
        {
           for (x = 0; x < w; x++)
             {
                int v;
                int r, g, b;
                int d1, d2;

                r = (pix[0] >> 16) & 0xff;
                g = (pix[0] >> 8) & 0xff;
                b = (pix[0]) & 0xff;
                d1 =
                  ((r - fr) * (r - fr)) +
                  ((g - fg) * (g - fg)) +
                  ((b - fb) * (b - fb));
                d2 =
                  ((r - br) * (r - br)) +
                  ((g - bg) * (g - bg)) +
                  ((b - bb) * (b - bb));
                if (d1 + d2)
                  {
                     v = (((d2 * 255) / (d1 + d2)) * 5) / 256;
                     if (v > dither[x & 0x1][y & 0x1])
                       v = 1;
                     else
                       v = 0;
                  }
                else
                  v = 0;

                XPutPixel(xim, x, y, v);
                if (_ecore_xlib_sync) ecore_x_sync();
                pix++;
             }
        }
      gc = XCreateGC(_ecore_x_disp, pmap, 0, &gcv);
      if (_ecore_xlib_sync) ecore_x_sync();
      XPutImage(_ecore_x_disp, pmap, gc, xim, 0, 0, 0, 0, w, h);
      if (_ecore_xlib_sync) ecore_x_sync();
      XFreeGC(_ecore_x_disp, gc);

      pix = (unsigned int *)pixels;
      for (y = 0; y < h; y++)
        {
           for (x = 0; x < w; x++)
             {
                int v;

                v = (((pix[0] >> 24) & 0xff) * 5) / 256;
                if (v > dither[x & 0x1][y & 0x1])
                  v = 1;
                else
                  v = 0;

                XPutPixel(xim, x, y, v);
                pix++;
             }
        }
      gc = XCreateGC(_ecore_x_disp, mask, 0, &gcv);
      if (_ecore_xlib_sync) ecore_x_sync();
      XPutImage(_ecore_x_disp, mask, gc, xim, 0, 0, 0, 0, w, h);
      if (_ecore_xlib_sync) ecore_x_sync();
      XFreeGC(_ecore_x_disp, gc);

      free(xim->data);
      xim->data = NULL;
      XDestroyImage(xim);

      c1.pixel = 0;
      c1.red = fr << 8 | fr;
      c1.green = fg << 8 | fg;
      c1.blue = fb << 8 | fb;
      c1.flags = DoRed | DoGreen | DoBlue;

      c2.pixel = 0;
      c2.red = br << 8 | br;
      c2.green = bg << 8 | bg;
      c2.blue = bb << 8 | bb;
      c2.flags = DoRed | DoGreen | DoBlue;

      c = XCreatePixmapCursor(_ecore_x_disp,
                              pmap, mask,
                              &c1, &c2,
                              hot_x, hot_y);
      if (_ecore_xlib_sync) ecore_x_sync();
      XFreePixmap(_ecore_x_disp, pmap);
      XFreePixmap(_ecore_x_disp, mask);
      return c;
   }

   return 0;
}

EAPI void
ecore_x_cursor_free(Ecore_X_Cursor c)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   EINA_SAFETY_ON_NULL_RETURN(_ecore_x_disp);
   XFreeCursor(_ecore_x_disp, c);
   if (_ecore_xlib_sync) ecore_x_sync();
}

/*
 * Returns the cursor for the given shape.
 * Note that the return value must not be freed with
 * ecore_x_cursor_free()!
 */
EAPI Ecore_X_Cursor
ecore_x_cursor_shape_get(int shape)
{
   Ecore_X_Cursor cur;
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   EINA_SAFETY_ON_NULL_RETURN_VAL(_ecore_x_disp, 0);
   /* Shapes are defined in Ecore_X_Cursor.h */
   cur = XCreateFontCursor(_ecore_x_disp, shape);
   if (_ecore_xlib_sync) ecore_x_sync();
   return cur;
}

EAPI void
ecore_x_cursor_size_set(int size)
{
#ifdef ECORE_XCURSOR
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   EINA_SAFETY_ON_NULL_RETURN(_ecore_x_disp);
   XcursorSetDefaultSize(_ecore_x_disp, size);
   if (_ecore_xlib_sync) ecore_x_sync();
#else /* ifdef ECORE_XCURSOR */
   (void) size;
#endif /* ifdef ECORE_XCURSOR */
}

EAPI int
ecore_x_cursor_size_get(void)
{
#ifdef ECORE_XCURSOR
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   EINA_SAFETY_ON_NULL_RETURN_VAL(_ecore_x_disp, 0);
   return XcursorGetDefaultSize(_ecore_x_disp);
#else /* ifdef ECORE_XCURSOR */
   return 0;
#endif /* ifdef ECORE_XCURSOR */
}