summaryrefslogtreecommitdiff
path: root/imlibrenderer/xrenderer/xrenderer.c
blob: 7dde32b502767e984e50eec727b713cd44a34515 (plain)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//***************************************************************************
// Group VDR/GraphTFT
// File xrenderer.c
// Date 16.02.14 - Jörg Wendel
// This code is distributed under the terms and conditions of the
// GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
//--------------------------------------------------------------------------
// Class XRenderer
//***************************************************************************

#include <stdio.h>

#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#define XK_MISCELLANY
#include <X11/keysymdef.h>

#include <common.h>
#include "xrenderer.h"

//***************************************************************************
// Object
//***************************************************************************

XRenderer::XRenderer(int x, int y, int width, int height, 
                     string cfgPath, int utf, string thmPath)
   : ImlibRenderer(x, y, width, height, cfgPath, utf, thmPath)
{
   disp = 0;
   win = 0;
   pix = 0;
   screen = 0;
   initialized = no;
}

XRenderer::~XRenderer()
{
	deinit();
}

void XRenderer::setDisplaySize(int width, int height)
{
   if (dspWidth != width || dspHeight != height)
   {
      cMutexLock lock(&mutex);

      tell(0, "Changing display size to %dx%d", width, height);      
      deinit();
      dspWidth = width;
      dspHeight = height;
      init(no);
   }
}

//***************************************************************************
// Init
//***************************************************************************

int XRenderer::init(int lazy)
{
   const char* appName = "graphtft-fe";

   Visual* vis;
   Colormap cm;
   int depth;
   XClassHint* classHint;

   cMutexLock lock(&mutex);

   ImlibRenderer::init(lazy);

   if (Str::isEmpty(devname))
   {
      tell(0, "Can't open display 'null' continue in detached mode!");
      return fail;
   }

   // init X 

   if (!(disp = XOpenDisplay(devname)))
   {
      tell(0, "Can't open display '%s' continue in detached mode", devname);
      return lazy ? success : fail;
   }

   if (!XInitThreads()) 
      tell(0, "Can't initialize X11 thread support");

   // init display

   screen = DefaultScreen(disp);
   vis    = DefaultVisual(disp, screen);
   depth  = DefaultDepth(disp, screen);
   cm     = DefaultColormap(disp, screen);

   // create simple window

   win = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 
                             0, depth, dspWidth, dspHeight, 0, 0, 0);
  
   XSetStandardProperties(disp, win, appName, appName, None, 0, 0, 0);
   XStoreName(disp, win, appName);
  
   /* set the name and class hints for the window manager to use */
   
   classHint = XAllocClassHint();
   classHint->res_name = (char*)appName;
   classHint->res_class = (char*)appName;
   XSetClassHint(disp, win, classHint);
   XFree(classHint);
   
   XSelectInput(disp, win, 
                ButtonPressMask   |
                ButtonReleaseMask | 
                PointerMotionMask | 
                KeyPressMask      |
                ClientMessage     |
                SubstructureNotifyMask |
                ExposureMask);                    // events to receive

   XMapWindow(disp, win);                         // show
   XFlush(disp);

   Screen* scn = DefaultScreenOfDisplay(disp);
   pix = XCreatePixmap(disp, win, dspWidth, dspHeight, DefaultDepthOfScreen(scn));

   imlib_context_set_dither(1);         // dither for depths < 24bpp 
   imlib_context_set_display(disp);     // set the display
   imlib_context_set_visual(vis);       // visual,
   imlib_context_set_colormap(cm);      // colormap
   imlib_context_set_drawable(pix);     // and drawable we are using 

//    {
//       XWindowAttributes windowAttributes;
//       XGetWindowAttributes(disp, win, &windowAttributes);
//       int w = windowAttributes.width; 
//       int h = windowAttributes.height;
      
//       tell(0, "Created window with (%d/%d) got (%d/%d)", dspWidth, dspHeight, w, h);
//    }

   tell(0, "Connection to '%s' established", devname);
   initialized = yes;

   return success;
}

//***************************************************************************
// Deinit
//***************************************************************************

void XRenderer::deinit()
{
   cMutexLock lock(&mutex);

   ImlibRenderer::deinit();

   if (initialized)
   {
      imlib_context_disconnect_display();

      if (win)  XDestroyWindow(disp, win);
      if (pix)  XFreePixmap(disp, pix);

      XFlush(disp);

      if (XCloseDisplay(disp))
         tell(0, "Error closing display");

      disp = 0;
      win = 0;
      pix = 0;
      screen = 0;
      initialized = no;
   }

   tell(0, "Connection to '%s' closed, now detached", devname);
}

//***************************************************************************
// attach / detach
//***************************************************************************

int XRenderer::attach(const char* disp)   
{ 
   if (initialized)
   {
      tell(0, "Already attached, trying detach first");
      detach();
   }

   if (!Str::isEmpty(disp))
      setDevName(disp);

   tell(0, "Try to attach to '%s'", devname);

   return init(no);
}

int XRenderer::detach()
{ 
   if (!initialized)
   {
      tell(0, "Already detached");
      return done;
   }

   tell(0, "Detach from '%s'", devname);
   deinit();

   return success;
}

//***************************************************************************
// X Pending
//***************************************************************************

int XRenderer::xPending()
{
   cMutexLock lock(&mutex);
   XEvent ev;

   if (!initialized)
      return success;

   while (XPending(disp))
      XNextEvent(disp, &ev);

   return success;
}

//***************************************************************************
// Refresh
//***************************************************************************

void XRenderer::refresh(int force)
{
   cMutexLock lock(&mutex);

   if (!force || !initialized)    // don't need 'total' refresh every time since we refresh the areas
      return;

   tell(2, "refresh all");

   refreshPixmap();

   XClearWindow(disp, win);
}

void XRenderer::refreshArea(int x, int y, int width, int height)
{
   cMutexLock lock(&mutex);

   if (!initialized)
      return ;

   refreshPixmap();

   // scale coordinates from theme to display size

   double xScale = (double)dspWidth / (double)themeWidth;
   double yScale = (double)dspHeight / (double)themeHeight;

   int xDsp = x * xScale;
   int yDsp = y * yScale;
   int widthDsp = width * xScale;
   int heightDsp = height * yScale;

   tell(3, "Refresh area at %d/%d (%d/%d); scaled to %d/%d (%d/%d); scale %.2f/%.2f; dspSize (%d/%d)",
        x, y, width, height, xDsp, yDsp, widthDsp, heightDsp,
        xScale, yScale, dspWidth, dspHeight);
   
   XClearArea(disp, win, xDsp, yDsp, widthDsp, heightDsp, false);
}

void XRenderer::refreshPixmap()
{
   //  XWindowAttributes windowAttributes;
   //  XGetWindowAttributes(disp, win, &windowAttributes);
   //  int width = windowAttributes.width; 
   //  int height = windowAttributes.height;

   imlib_context_set_image(_cur_image);
   imlib_render_image_on_drawable_at_size(0, 0, dspWidth, dspHeight);

   XSetWindowBackgroundPixmap(disp, win, pix);
}

void XRenderer::clear()
{
   if (!initialized)
      return ;

   cMutexLock lock(&mutex);
	ImlibRenderer::clear();
}