fix armel build issues
[drnoksnes] / platform / sdlvscalers.cpp
1 #include <stdio.h> 
2 #include <math.h>
3
4 #include <X11/Xlib.h>
5 #include <X11/Xutil.h>
6 #include <SDL.h>
7 #include <SDL_syswm.h>
8
9 #if CONF_XSP
10 #       include <X11/extensions/Xsp.h>
11 #endif
12 #if CONF_HD
13 #       include <X11/Xatom.h>
14 #       include <sys/ipc.h>
15 #       include <sys/shm.h>
16 #endif
17
18 #include "snes9x.h"
19 #include "display.h"
20 #include "platform.h"
21 #include "scaler.h"
22 #include "sdlv.h"
23
24 #define DIE(format, ...) do { \
25                 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
26                 fprintf(stderr, format "\n", ## __VA_ARGS__); \
27                 abort(); \
28         } while (0);
29
30 /* Helper functions */
31
32 static void centerRectangle(SDL_Rect& result, int areaW, int areaH, int w, int h)
33 {
34         result.x = areaW / 2 - w / 2;
35         result.w = w;
36         result.y = areaH / 2 - h / 2;
37         result.h = h;
38 }
39
40 /* Base scaler for stupid scalers */
41 /** Does nothing but center the image */
42 class DummyScaler : public Scaler
43 {
44         SDL_Surface * m_screen;
45         SDL_Rect m_area;
46
47 protected:
48         DummyScaler(SDL_Surface* screen, int w, int h)
49         : m_screen(screen)
50         {
51                 centerRectangle(m_area, GUI.Width, GUI.Height, w, h);
52         }
53
54 public:
55
56         ~DummyScaler()
57         {
58         };
59
60         class Factory : public ScalerFactory
61         {
62                 const char * getName() const
63                 {
64                         return "none";
65                 }
66
67                 bool canEnable(int bpp, int w, int h) const
68                 {
69                         return true;
70                 }
71
72                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
73                 {
74                         return new DummyScaler(screen, w, h);
75                 }
76         };
77
78         static const Factory factory;
79
80         virtual const char * getName() const
81         {
82                 return "no scaling";
83         }
84
85         virtual uint8* getDrawBuffer() const
86         {
87                 const int Bpp = screen->format->BitsPerPixel / 8;
88                 const int pitch = screen->pitch;
89                 return ((uint8*) screen->pixels)
90                         + (m_area.x * Bpp)
91                         + (m_area.y * pitch);
92         };
93
94         virtual unsigned int getDrawBufferPitch() const
95         {
96                 return screen->pitch;
97         };
98
99         virtual void getRenderedGUIArea(unsigned short & x, unsigned short & y,
100                                                         unsigned short & w, unsigned short & h) const
101         {
102                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
103         };
104
105         virtual int getRatio() const
106         {
107                 return 1;
108         };
109
110         virtual void prepare() { };
111
112         virtual void finish()
113         {
114                 SDL_UpdateRects(m_screen, 1, &m_area);
115         };
116
117         virtual void pause() { };
118         virtual void resume() { };
119 };
120 const DummyScaler::Factory DummyScaler::factory;
121
122 /* Basic and slow software scaler */
123
124 class SWScaler : public Scaler
125 {
126         SDL_Surface * m_screen;
127         SDL_Rect m_area;
128         uint8 * m_surface;
129         const int m_w, m_h, m_Bpp;
130
131 protected:
132         SWScaler(SDL_Surface* screen, int w, int h)
133         : m_screen(screen), m_w(w), m_h(h),
134          m_Bpp(m_screen->format->BitsPerPixel / 8)
135         {
136                 centerRectangle(m_area, GUI.Width, GUI.Height, w * 2, h * 2);
137                 m_surface = reinterpret_cast<uint8*>(malloc(w * h * m_Bpp));
138         }
139 public:
140         virtual ~SWScaler()
141         {
142                 free(m_surface);
143         };
144
145         class Factory : public ScalerFactory
146         {
147                 const char * getName() const
148                 {
149                         return "soft2x";
150                 }
151
152                 bool canEnable(int bpp, int w, int h) const
153                 {
154                         return w * 2 < GUI.Width && h * 2 < GUI.Height;
155                 }
156
157                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
158                 {
159                         return new SWScaler(screen, w, h);
160                 }
161         };
162
163         static const Factory factory;
164
165         virtual const char * getName() const
166         {
167                 return "software 2x scaling";
168         }
169
170         uint8* getDrawBuffer() const
171         {
172                 return m_surface;
173         };
174
175         unsigned int getDrawBufferPitch() const
176         {
177                 return m_w * m_Bpp;
178         };
179
180         void getRenderedGUIArea(unsigned short & x, unsigned short & y,
181                                                         unsigned short & w, unsigned short & h) const
182         {
183                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
184         };
185
186         int getRatio() const
187         {
188                 return 2;
189         };
190
191         void prepare() { };
192
193         void finish()
194         {
195                 uint16 * src = reinterpret_cast<uint16*>(m_surface);
196                 uint16 * dst = reinterpret_cast<uint16*>(
197                         ((uint8*) m_screen->pixels)
198                         + (m_area.x * m_Bpp)
199                         + (m_area.y * m_screen->pitch));
200                 const int src_pitch = m_w;
201                 const int dst_pitch = m_screen->pitch / m_Bpp;
202                 int x, y;
203
204                 for (y = 0; y < m_h*2; y++) {
205                         for (x = 0; x < m_w*2; x+=2) {
206                                 dst[x] = src[x/2];
207                                 dst[x + 1] = src[x/2];
208                         }
209                         dst += dst_pitch;
210                         if (y&1) src += src_pitch;
211                 }
212
213                 SDL_UpdateRects(m_screen, 1, &m_area);
214         };
215
216         void pause() { };
217         void resume() { };
218 };
219 const SWScaler::Factory SWScaler::factory;
220
221 /* Platform specific scalers */
222
223 #ifdef __arm__
224 class ARMScaler : public Scaler
225 {
226         SDL_Surface * m_screen;
227         SDL_Rect m_area;
228         uint8 * m_surface;
229         const int m_w, m_h, m_Bpp;
230
231 protected:
232         ARMScaler(SDL_Surface* screen, int w, int h)
233         : m_screen(screen), m_w(w), m_h(h),
234          m_Bpp(m_screen->format->BitsPerPixel / 8)
235         {
236                 centerRectangle(m_area, GUI.Width, GUI.Height, w * 2, h * 2);
237                 m_surface = reinterpret_cast<uint8*>(malloc(w * h * m_Bpp));
238         }
239 public:
240         virtual ~ARMScaler()
241         {
242                 free(m_surface);
243         };
244
245         class Factory : public ScalerFactory
246         {
247                 const char * getName() const
248                 {
249                         return "arm2x";
250                 }
251
252                 bool canEnable(int bpp, int w, int h) const
253                 {
254                         return bpp == 16 && w * 2 < GUI.Width && h * 2 < GUI.Height &&
255                                 w % 16 == 0 /* asm assumes w div by 16 */;
256                 }
257
258                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
259                 {
260                         return new ARMScaler(screen, w, h);
261                 }
262         };
263
264         static const Factory factory;
265
266         virtual const char * getName() const
267         {
268                 return "software ARM 2x scaling";
269         }
270
271         uint8* getDrawBuffer() const
272         {
273                 return m_surface;
274         };
275
276         unsigned int getDrawBufferPitch() const
277         {
278                 return m_w * m_Bpp;
279         };
280
281         void getRenderedGUIArea(unsigned short & x, unsigned short & y,
282                                                         unsigned short & w, unsigned short & h) const
283         {
284                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
285         };
286
287         int getRatio() const
288         {
289                 return 2;
290         };
291
292         void prepare() { };
293
294         void finish()
295         {
296                 uint16 * src = reinterpret_cast<uint16*>(m_surface);
297                 uint16 * dst = reinterpret_cast<uint16*>(
298                         ((uint8*) m_screen->pixels)
299                         + (m_area.x * m_Bpp)
300                         + (m_area.y * m_screen->pitch));
301                 const int src_pitch = m_w;
302                 const int dst_pitch = m_screen->pitch / m_Bpp;
303                 int y;
304
305                 for (y = 0; y < m_h*2; y++) {
306                         asm volatile
307                         (
308                                 "mov r0, %0; mov r1, %1; mov r2, %2;"
309                                 "stmdb r13!,{r4,r5,r6,r7,r8,r9,r10,r11,r12,r14};"
310                                 "1:     ldmia r1!,{r3,r4,r5,r6,r7,r8,r9,r10};"
311                                 "mov r14,r5,lsr #16;"
312                                 "mov r12,r5,lsl #16;"
313                                 "orr r14,r14,r14,lsl #16;"
314                                 "orr r12,r12,r12,lsr #16;"
315                                 "mov r11,r4,lsr #16;"
316                                 "mov r5,r4,lsl #16;"
317                                 "orr r11,r11,r11,lsl #16;"
318                                 "orr r5,r5,r5,lsr #16;"
319                                 "mov r4,r3,lsr #16;"
320                                 "mov r3,r3,lsl #16;"
321                                 "orr r4,r4,r4,lsl #16;"
322                                 "orr r3,r3,r3,lsr #16;"
323                                 "stmia r0!,{r3,r4,r5,r11,r12,r14};"
324                                 "mov r3,r6,lsl #16;"
325                                 "mov r4,r6,lsr #16;"
326                                 "orr r3,r3,r3,lsr #16;"
327                                 "orr r4,r4,r4,lsl #16;"
328                                 "mov r5,r7,lsl #16;"
329                                 "mov r6,r7,lsr #16;"
330                                 "orr r5,r5,r5,lsr #16;"
331                                 "orr r6,r6,r6,lsl #16;"
332                                 "mov r7,r8,lsl #16;"
333                                 "mov r8,r8,lsr #16;"
334                                 "orr r7,r7,r7,lsr #16;"
335                                 "orr r8,r8,r8,lsl #16;"
336                                 "mov r12,r10,lsr #16;"
337                                 "mov r11,r10,lsl #16;"
338                                 "orr r12,r12,r12,lsl #16;"
339                                 "orr r11,r11,r11,lsr #16;"
340                                 "mov r10,r9,lsr #16;"
341                                 "mov r9,r9,lsl #16;"
342                                 "orr r10,r10,r10,lsl #16;"
343                                 "orr r9,r9,r9,lsr #16;"
344                                 "stmia r0!,{r3,r4,r5,r6,r7,r8,r9,r10,r11,r12};"
345                                 "subs r2,r2,#16;"
346                                 "bhi 1b;"
347                                 "ldmia r13!,{r4,r5,r6,r7,r8,r9,r10,r11,r12,r14};"
348                         :
349                         : "r" (dst), "r" (src), "r" (m_w)
350                         : "r0", "r1", "r2", "r3"
351                         );
352                         dst += dst_pitch;
353                         if (y&1) src += src_pitch;
354                 }
355
356                 SDL_UpdateRects(m_screen, 1, &m_area);
357         };
358
359         void pause() { };
360         void resume() { };
361 };
362 const ARMScaler::Factory ARMScaler::factory;
363 #endif
364
365 #if CONF_HD
366
367 enum hdAtoms {
368         ATOM_HILDON_NON_COMPOSITED_WINDOW = 0,
369         ATOM_NET_WM_STATE,
370         ATOM_NET_WM_STATE_FULLSCREEN,
371         ATOM_NET_WM_WINDOW_TYPE,
372         ATOM_NET_WM_WINDOW_TYPE_NORMAL,
373         ATOM_NET_WM_WINDOW_TYPE_DIALOG,
374         ATOM_HILDON_WM_WINDOW_TYPE_ANIMATION_ACTOR,
375         ATOM_HILDON_ANIMATION_CLIENT_READY,
376         ATOM_HILDON_ANIMATION_CLIENT_MESSAGE_SHOW,
377         ATOM_HILDON_ANIMATION_CLIENT_MESSAGE_POSITION,
378         ATOM_HILDON_ANIMATION_CLIENT_MESSAGE_ROTATION,
379         ATOM_HILDON_ANIMATION_CLIENT_MESSAGE_SCALE,
380         ATOM_HILDON_ANIMATION_CLIENT_MESSAGE_ANCHOR,
381         ATOM_HILDON_ANIMATION_CLIENT_MESSAGE_PARENT,
382         ATOM_HILDON_WM_WINDOW_TYPE_REMOTE_TEXTURE,
383         ATOM_HILDON_TEXTURE_CLIENT_MESSAGE_SHM,
384         ATOM_HILDON_TEXTURE_CLIENT_MESSAGE_DAMAGE,
385         ATOM_HILDON_TEXTURE_CLIENT_MESSAGE_SHOW,
386         ATOM_HILDON_TEXTURE_CLIENT_MESSAGE_POSITION,
387         ATOM_HILDON_TEXTURE_CLIENT_MESSAGE_OFFSET,
388         ATOM_HILDON_TEXTURE_CLIENT_MESSAGE_SCALE,
389         ATOM_HILDON_TEXTURE_CLIENT_MESSAGE_PARENT,
390         ATOM_HILDON_TEXTURE_CLIENT_READY,
391         ATOM_COUNT
392 };
393
394 static const char * hdAtomNames[] = {
395         "_HILDON_NON_COMPOSITED_WINDOW",
396         "_NET_WM_STATE",
397         "_NET_WM_STATE_FULLSCREEN",
398         "_NET_WM_WINDOW_TYPE",
399         "_NET_WM_WINDOW_TYPE_NORMAL",
400         "_NET_WM_WINDOW_TYPE_DIALOG",
401         "_HILDON_WM_WINDOW_TYPE_ANIMATION_ACTOR",
402         "_HILDON_ANIMATION_CLIENT_READY",
403         "_HILDON_ANIMATION_CLIENT_MESSAGE_SHOW",        
404         "_HILDON_ANIMATION_CLIENT_MESSAGE_POSITION",
405         "_HILDON_ANIMATION_CLIENT_MESSAGE_ROTATION",
406         "_HILDON_ANIMATION_CLIENT_MESSAGE_SCALE",
407         "_HILDON_ANIMATION_CLIENT_MESSAGE_ANCHOR",
408         "_HILDON_ANIMATION_CLIENT_MESSAGE_PARENT",
409         "_HILDON_WM_WINDOW_TYPE_REMOTE_TEXTURE",
410         "_HILDON_TEXTURE_CLIENT_MESSAGE_SHM",
411         "_HILDON_TEXTURE_CLIENT_MESSAGE_DAMAGE",
412         "_HILDON_TEXTURE_CLIENT_MESSAGE_SHOW",
413         "_HILDON_TEXTURE_CLIENT_MESSAGE_POSITION",
414         "_HILDON_TEXTURE_CLIENT_MESSAGE_OFFSET",
415         "_HILDON_TEXTURE_CLIENT_MESSAGE_SCALE",
416         "_HILDON_TEXTURE_CLIENT_MESSAGE_PARENT",
417         "_HILDON_TEXTURE_CLIENT_READY",
418         ""
419 };
420
421 static Atom hdAtomsValues[ATOM_COUNT];
422 static bool hdAtomsLoaded = false;
423
424 #define HDATOM(X) hdAtomsValues[ ATOM ## X ]
425
426 static void hildon_load_atoms(Display* display)
427 {
428         if (hdAtomsLoaded) return;
429         
430         XInternAtoms(display, (char**)hdAtomNames, ATOM_COUNT, True, hdAtomsValues);
431         hdAtomsLoaded = true;
432         
433         if (HDATOM(_HILDON_NON_COMPOSITED_WINDOW) == None) {
434                 DIE("Hildon Desktop seems not be loaded, since %s is not defined",
435                         "_HILDON_NON_COMPOSITED_WINDOW");
436                 return;
437         }
438 }
439
440 /** Enables or disables the Hildon NonCompositedWindow property */
441 static void hildon_set_non_compositing(bool enable)
442 {
443         SDL_SysWMinfo wminfo;
444         Display *display;
445         Window xwindow;
446         XSetWindowAttributes xattr;
447         Atom atom;
448         int one = 1;
449         
450         SDL_VERSION(&wminfo.version);
451         if (!SDL_GetWMInfo(&wminfo)) return;
452         
453         wminfo.info.x11.lock_func();
454         display = wminfo.info.x11.display;
455         xwindow = wminfo.info.x11.fswindow;
456         hildon_load_atoms(display);
457
458         if (enable) {
459                 /* 
460                  * The basic idea behind this is to disable the override_redirect
461                  * window attribute, which SDL sets, and instead use _NET_WM_STATE
462                  * to tell hildon-desktop to fullscreen the app.
463                  * I am not really happy with this, which should ideally be fixed
464                  * at the libsdl level, but seems to work.
465                  * As soon as the window is managed by Hildon-Desktop again, set for it
466                  * not to be composited.
467                  */
468                 XUnmapWindow(display, xwindow);
469                 xattr.override_redirect = False;
470                 XChangeWindowAttributes(display, xwindow, CWOverrideRedirect, &xattr);
471
472                 atom = HDATOM(_NET_WM_STATE_FULLSCREEN);
473                 XChangeProperty(display, xwindow, HDATOM(_NET_WM_STATE),
474                         XA_ATOM, 32, PropModeReplace,
475                         (unsigned char *) &atom, 1);
476
477                 XChangeProperty(display, xwindow, HDATOM(_HILDON_NON_COMPOSITED_WINDOW),
478                         XA_INTEGER, 32, PropModeReplace,
479                         (unsigned char *) &one, 1);
480                 XMapWindow(display, xwindow);
481         } else {
482                 xattr.override_redirect = True;
483                 XDeleteProperty(display, xwindow,
484                         HDATOM(_HILDON_NON_COMPOSITED_WINDOW));
485                 XChangeWindowAttributes(display, xwindow, CWOverrideRedirect, &xattr);
486         }
487
488         wminfo.info.x11.unlock_func();
489 }
490
491 class HDScalerBase : public Scaler
492 {
493         SDL_Surface * m_screen;
494         SDL_Rect m_area;
495         const int m_w, m_h, m_Bpp;
496         const float ratio_x, ratio_y;
497
498         // SDL/X11 stuff we save for faster access.
499         SDL_SysWMinfo wminfo;
500         Display* display;
501         Window window;
502
503         // Shared memory segment info.
504         key_t shmkey;
505         int shmid;
506         void *shmaddr;
507
508 private:
509         /** Sends a message to hildon-desktop.
510           * This function comes mostly straight from libhildon.
511           */
512         void sendMessage(Atom message_type,
513                 uint32 l0, uint32 l1, uint32 l2, uint32 l3, uint32 l4)
514         {
515                 XEvent event = { 0 };
516
517                 event.xclient.type = ClientMessage;
518                 event.xclient.window = window;
519                 event.xclient.message_type = message_type;
520                 event.xclient.format = 32;
521                 event.xclient.data.l[0] = l0;
522                 event.xclient.data.l[1] = l1;
523                 event.xclient.data.l[2] = l2;
524                 event.xclient.data.l[3] = l3;
525                 event.xclient.data.l[4] = l4;
526
527                 XSendEvent (display, window, True,
528                             StructureNotifyMask,
529                             (XEvent *)&event);
530         }
531
532         /** Sends all configuration parameters for the remote texture. */
533         void reconfigure()
534         {
535                 SDL_VERSION(&wminfo.version);
536                 if (!SDL_GetWMInfo(&wminfo)) {
537                         DIE("Bad SDL version!");
538                 }
539
540                 Window parent;
541                 int yoffset = 0;
542                 if (Config.fullscreen) {
543                         parent = wminfo.info.x11.fswindow;
544                 } else {
545                         parent = wminfo.info.x11.wmwindow;
546                         yoffset = 60; // Hardcode the title bar size for now.
547                 }
548
549                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_SHM),
550                         (uint32) shmkey, m_w, m_h, m_Bpp, 0);
551                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_PARENT),
552                         (uint32) parent, 0, 0, 0, 0);
553                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_POSITION),
554                         m_area.x, yoffset + m_area.y, m_area.w, m_area.h, 0);
555                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_SCALE),
556                         ratio_x * (1 << 16), ratio_y * (1 << 16), 0, 0, 0);
557                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_SHOW),
558                         1, 255, 0, 0, 0);
559         }
560
561 protected:
562         HDScalerBase(SDL_Surface* screen, int w, int h, float r_x, float r_y)
563         : m_screen(screen), m_w(w), m_h(h),
564          m_Bpp(m_screen->format->BitsPerPixel / 8),
565          ratio_x(r_x), ratio_y(r_y)
566         {
567                 centerRectangle(m_area, GUI.Width, GUI.Height, w * r_x, h * r_y);
568
569                 // What we're going to do:
570                 //  - Create a new window that we're going to manage
571                 //  - Set up that window as a Hildon Remote Texture
572                 //  - Render to that new window, instead of the SDL window ("screen").
573                 // Yet another load of uglyness, but hey.
574
575                 // Barf if this is not a known SDL version.
576                 SDL_VERSION(&wminfo.version);
577                 if (!SDL_GetWMInfo(&wminfo)) {
578                         DIE("Bad SDL version!");
579                 }
580
581                 // Clear the SDL screen with black, just in case it gets drawn.
582                 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
583
584                 // Get the SDL gfxdisplay (this is where events end up).
585                 display = wminfo.info.x11.display;
586
587                 // The parent window needs to be mapped, so we sync it.
588                 XSync(display, True);
589
590                 // Ensure hildon atoms are synced, and that hildon-desktop is up.
591                 hildon_load_atoms(display);
592
593                 // Create our alternative window.
594                 const int blackColor = BlackPixel(display, DefaultScreen(display));
595                 window = XCreateSimpleWindow(display, DefaultRootWindow(display),
596                         0, 0, m_w, m_h, 0, blackColor, blackColor);
597                 XStoreName(display, window, "DrNokSnes Video output window");
598                 Atom atom = HDATOM(_HILDON_WM_WINDOW_TYPE_REMOTE_TEXTURE);
599                 XChangeProperty(display, window, HDATOM(_NET_WM_WINDOW_TYPE),
600                         XA_ATOM, 32, PropModeReplace,
601                         (unsigned char *) &atom, 1);
602                 XSelectInput(display, window, PropertyChangeMask | StructureNotifyMask);
603                 XMapWindow(display, window);
604
605                 // Wait for "ready" property, set up by hildon-desktop after a while
606                 // For now, loop here. In the future, merge with main event loop.
607                 bool ready = false;
608                 while (!ready) {
609                         XEvent e;
610                         XNextEvent(display, &e);
611                         switch(e.type) {
612                                 case PropertyNotify:
613                                         if (e.xproperty.atom ==
614                                           HDATOM(_HILDON_TEXTURE_CLIENT_READY)) {
615                                                 ready = true;
616                                         }
617                                         break;
618                                 default:
619                                         break;
620                         }
621                 }
622
623                 // Create a shared memory segment with hildon-desktop
624                 shmkey = ftok("/usr/bin/drnoksnes", 'd'); // TODO Put rom file here
625                 shmid = shmget(shmkey, m_w * m_h * m_Bpp, IPC_CREAT | 0777);
626                 if (shmid < 0) {
627                         DIE("Failed to create shared memory");
628                 }
629                 shmaddr = shmat(shmid, 0, 0);
630                 if (shmaddr == (void*)-1) {
631                         DIE("Failed to attach shared memory");
632                 }
633
634                 // Send all configuration events to hildon-desktop
635                 reconfigure();
636         }
637
638 public:
639         virtual ~HDScalerBase()
640         {
641                 // Hide, unparent and deattach the remote texture
642                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_SHOW),
643                         0, 255, 0, 0, 0);
644                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_PARENT),
645                         0, 0, 0, 0, 0);
646                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_SHM),
647                         0, 0, 0, 0, 0);
648                 XFlush(display);
649                 // Destroy our managed window and shared memory segment
650                 XDestroyWindow(display, window);
651                 XSync(display, True);
652                 shmdt(shmaddr);
653                 shmctl(shmid, IPC_RMID, 0);
654         };
655
656         virtual uint8* getDrawBuffer() const
657         {
658                 return reinterpret_cast<uint8*>(shmaddr);
659         };
660
661         virtual unsigned int getDrawBufferPitch() const
662         {
663                 return m_w * m_Bpp;
664         };
665
666         virtual void getRenderedGUIArea(unsigned short & x, unsigned short & y,
667                                                         unsigned short & w, unsigned short & h) const
668         {
669                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
670         };
671
672         virtual int getRatio() const
673         {
674                 return ratio_y; // TODO
675         };
676
677         virtual void prepare()
678         {
679
680         };
681
682         virtual void finish()
683         {
684                 // Send a damage event to hildon-desktop.
685                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_DAMAGE),
686                         0, 0, m_w, m_h, 0);
687                 XSync(display, False);
688         };
689
690         virtual void pause() { };
691         virtual void resume() { };
692 };
693
694 class HDFillScaler : public HDScalerBase
695 {
696         HDFillScaler(SDL_Surface* screen, int w, int h)
697         : HDScalerBase(screen, w, h,
698                 GUI.Width / (float)w, GUI.Height / (float)h)
699         {
700         }
701
702 public:
703         class Factory : public ScalerFactory
704         {
705                 const char * getName() const
706                 {
707                         return "hdfill";
708                 }
709
710                 bool canEnable(int bpp, int w, int h) const
711                 {
712                         return true;
713                 }
714
715                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
716                 {
717                         return new HDFillScaler(screen, w, h-20);
718                 }
719         };
720
721         static const Factory factory;
722
723         virtual const char * getName() const
724         {
725                 return "hildon-desktop fill screen scaling";
726         }
727 };
728 const HDFillScaler::Factory HDFillScaler::factory;
729
730 class HDSquareScaler : public HDScalerBase
731 {
732         HDSquareScaler(SDL_Surface* screen, int w, int h, float ratio)
733         : HDScalerBase(screen, w, h, ratio, ratio)
734         {
735         }
736
737 public:
738         class Factory : public ScalerFactory
739         {
740                 const char * getName() const
741                 {
742                         return "hdsq";
743                 }
744
745                 bool canEnable(int bpp, int w, int h) const
746                 {
747                         return true;
748                 }
749
750                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
751                 {
752                         return new HDSquareScaler(screen, w, h,
753                                 fminf(GUI.Width / (float)w, GUI.Height / (float)h));
754                 }
755         };
756
757         static const Factory factory;
758
759         virtual const char * getName() const
760         {
761                 return "hildon-desktop square screen scaling";
762         }
763 };
764 const HDSquareScaler::Factory HDSquareScaler::factory;
765
766 class HDDummy : public DummyScaler
767 {
768         HDDummy(SDL_Surface* screen, int w, int h)
769         : DummyScaler(screen, w, h)
770         {
771                 hildon_set_non_compositing(true);
772         }
773         
774 public:
775         ~HDDummy()
776         {
777                 hildon_set_non_compositing(false);
778         };
779
780         class Factory : public ScalerFactory
781         {
782                 const char * getName() const
783                 {
784                         return "hddummy";
785                 }
786
787                 bool canEnable(int bpp, int w, int h) const
788                 {
789                         return Config.fullscreen; // This makes sense only in fullscreen
790                 }
791
792                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
793                 {
794                         return new HDDummy(screen, w, h);
795                 }
796         };
797
798         static const Factory factory;
799
800         const char * getName() const
801         {
802                 return "compositor disabled and no scaling";
803         }
804 };
805 const HDDummy::Factory HDDummy::factory;
806
807 class HDSW : public SWScaler
808 {
809         HDSW(SDL_Surface* screen, int w, int h)
810         : SWScaler(screen, w, h)
811         {
812                 hildon_set_non_compositing(true);
813         }
814         
815 public:
816         ~HDSW()
817         {
818                 hildon_set_non_compositing(false);
819         };
820
821         class Factory : public ScalerFactory
822         {
823                 const char * getName() const
824                 {
825                         return "hdsoft2x";
826                 }
827
828                 bool canEnable(int bpp, int w, int h) const
829                 {
830                         return Config.fullscreen; // This makes sense only in fullscreen
831                 }
832
833                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
834                 {
835                         return new HDSW(screen, w, h);
836                 }
837         };
838
839         static const Factory factory;
840
841         const char * getName() const
842         {
843                 return "compositor disabled and software 2x scaling";
844         }
845 };
846 const HDSW::Factory HDSW::factory;
847
848 #ifdef __arm__
849 class HDARM : public ARMScaler
850 {
851         HDARM(SDL_Surface* screen, int w, int h)
852         : ARMScaler(screen, w, h)
853         {
854                 hildon_set_non_compositing(true);
855         }
856         
857 public:
858         ~HDARM()
859         {
860                 hildon_set_non_compositing(false);
861         };
862
863         class Factory : public ScalerFactory
864         {
865                 const char * getName() const
866                 {
867                         return "hdarm2x";
868                 }
869
870                 bool canEnable(int bpp, int w, int h) const
871                 {
872                         return Config.fullscreen; // This makes sense only in fullscreen
873                 }
874
875                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
876                 {
877                         return new HDARM(screen, w, h);
878                 }
879         };
880
881         static const Factory factory;
882
883         const char * getName() const
884         {
885                 return "compositor disabled and software ARM 2x scaling";
886         }
887 };
888 const HDARM::Factory HDARM::factory;
889 #endif /* __arm__ */
890 #endif /* CONF_HD */
891
892 #if CONF_XSP
893 class XSPScaler : public Scaler
894 {
895         SDL_Surface* m_screen;
896         SDL_Rect m_area;
897         SDL_Rect m_real_area;
898         bool m_should_enable, m_enabled; // Try to avoid flicker.
899
900         static void setDoubling(bool enable)
901         {
902                 SDL_SysWMinfo wminfo;
903                 SDL_VERSION(&wminfo.version);
904                 if ( SDL_GetWMInfo(&wminfo) ) {
905                         Display *dpy = wminfo.info.x11.display;
906                         XSPSetPixelDoubling(dpy, 0, enable ? 1 : 0);
907                         XFlush(dpy);
908                 }
909         }
910
911         XSPScaler(SDL_Surface* screen, int w, int h)
912         : m_screen(screen), m_should_enable(true), m_enabled(false)
913         {
914                 centerRectangle(m_area, GUI.Width, GUI.Height,
915                         w * 2, h * 2);
916
917                 m_real_area.x = m_area.x;
918                 m_real_area.y = m_area.y;
919                 m_real_area.w = m_area.w / 2;
920                 m_real_area.h = m_area.h / 2;
921         };
922 public:
923         ~XSPScaler()
924         {
925                 if (m_enabled) setDoubling(false);
926         };
927
928         class Factory : public ScalerFactory
929         {
930                 const char * getName() const
931                 {
932                         return "xsp";
933                 }
934
935                 bool canEnable(int bpp, int w, int h) const
936                 {
937                         return w * 2 < GUI.Width && h * 2 < GUI.Height;
938                 };
939
940                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
941                 {
942                         return new XSPScaler(screen, w, h);
943                 }
944         };
945
946         static const Factory factory;
947
948         const char * getName() const
949         {
950                 return "XSP pixel doubling";
951         }
952
953         uint8* getDrawBuffer() const
954         {
955                 const int Bpp = screen->format->BitsPerPixel / 8;
956                 const int pitch = screen->pitch;
957                 return ((uint8*) screen->pixels)
958                         + (m_area.x * Bpp)
959                         + (m_area.y * pitch);
960         };
961
962         unsigned int getDrawBufferPitch() const
963         {
964                 return screen->pitch;
965         };
966
967         void getRenderedGUIArea(unsigned short & x, unsigned short & y,
968                                                         unsigned short & w, unsigned short & h) const
969         {
970                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
971         };
972
973         int getRatio() const
974         {
975                 return 2;
976         };
977
978         void prepare() 
979         {
980                 if (m_should_enable && !m_enabled) {
981                         setDoubling(true);
982                         m_enabled = true;
983                 }
984         };
985
986         void finish()
987         {
988                 SDL_UpdateRects(m_screen, 1, &m_real_area);
989         };
990
991         void pause()
992         {
993                 m_should_enable = false;
994                 if (m_enabled) {
995                         setDoubling(false);
996                         m_enabled = false;
997                 }
998         };
999
1000         void resume()
1001         {
1002                 m_should_enable = true; // Will enable later
1003         };
1004 };
1005 const XSPScaler::Factory XSPScaler::factory;
1006 #endif
1007
1008 static const ScalerFactory* scalers[] = {
1009 /* More useful scalers come first */
1010 #if CONF_HD && defined(__arm__)
1011         &HDARM::factory,                                /* non-composited arm 2x scaling */
1012 #endif
1013 #if CONF_HD
1014         &HDSquareScaler::factory,               /* h-d assisted square scaling */
1015         &HDSW::factory,                                 /* non-composited soft 2x scaling */
1016 #endif
1017 #if CONF_XSP
1018         &XSPScaler::factory,                    /* n8x0 pixel doubling */
1019 #endif
1020 #ifdef __arm__
1021         &ARMScaler::factory,                    /* arm 2x scaling */
1022 #endif
1023         &SWScaler::factory,                             /* soft 2x scaling */
1024 #if CONF_HD
1025         &HDDummy::factory,                              /* non composited */
1026 #endif
1027         &DummyScaler::factory,                  /* failsafe */
1028 /* The following scalers will not be automatically enabled, no matter what */
1029 #if CONF_HD
1030         &HDFillScaler::factory,
1031 #endif
1032 };
1033
1034 /* Entry point functions */
1035
1036 const ScalerFactory* searchForScaler(int bpp, int w, int h)
1037 {
1038         const int n = sizeof(scalers) / sizeof(ScalerFactory*);
1039         int i;
1040
1041         if (Config.scaler && strcasecmp(Config.scaler, "help") == 0 ) {
1042                 // List scalers
1043                 printf("Scalers list:\n");
1044                 for (i = 0; i < n; i++) {
1045                         printf(" %s\n", scalers[i]->getName());
1046                 }
1047                 DIE("End of scalers list");
1048         } else if (Config.scaler && strcasecmp(Config.scaler, "auto") != 0 ) {
1049                 // We prefer a specific scaler
1050                 for (i = 0; i < n; i++) {
1051                         if (strcasecmp(scalers[i]->getName(), Config.scaler) == 0) {
1052                                 if (scalers[i]->canEnable(bpp, w, h)) {
1053                                         // Found the scaler selected by the user, and we can use it.
1054                                         return scalers[i];
1055                                 } else {
1056                                         fprintf(stderr,
1057                                                 "Selected scaler '%s' cannot be enabled in this mode\n",
1058                                                 Config.scaler);
1059                                         break; // Fallback to another scaler.
1060                                 }
1061                         }
1062                 }
1063                 if (i == n) {
1064                         fprintf(stderr, "Selected scaler '%s' does not exist\n",
1065                                 Config.scaler);
1066                 }
1067         }
1068
1069         // Just try them all now, in a buildtime set priority.
1070         for (i = 0; i < n; i++) {
1071                 if (scalers[i]->canEnable(bpp, w, h)) {
1072                         return scalers[i];
1073                 }
1074         }
1075
1076         DIE("Can't use any scaler; this shouldn't happen.");
1077 }
1078