Update change log.
[platform/upstream/cairo.git] / boilerplate / cairo-boilerplate-beos.cpp
1 /* vim:set ts=8 sw=4 noet cin: */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is Mozilla Communicator client code.
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communications Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 1998
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *   Takashi Toyoshima <toyoshim@be-in.org>
24  *   Fredrik Holmqvist <thesuckiestemail@yahoo.se>
25  *   Christian Biesinger <cbiesinger@web.de>
26  *
27  * Alternatively, the contents of this file may be used under the terms of
28  * either of the GNU General Public License Version 2 or later (the "GPL"),
29  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30  * in which case the provisions of the GPL or the LGPL are applicable instead
31  * of those above. If you wish to allow use of your version of this file only
32  * under the terms of either the GPL or the LGPL, and not to allow others to
33  * use your version of this file under the terms of the MPL, indicate your
34  * decision by deleting the provisions above and replace them with the notice
35  * and other provisions required by the GPL or the LGPL. If you do not delete
36  * the provisions above, a recipient may use your version of this file under
37  * the terms of any one of the MPL, the GPL or the LGPL.
38  *
39  * ***** END LICENSE BLOCK ***** */
40
41 #include "cairo-boilerplate.h"
42 #include <cairo-beos.h>
43
44 // Part of this code was originally part of
45 // xpfe/bootstrap/nsNativeAppSupportBeOS.cpp in the Mozilla source code.
46
47 #include <Application.h>
48 #include <Window.h>
49 #include <View.h>
50 #include <Bitmap.h>
51
52 class CairoTestWindow : public BWindow
53 {
54 public:
55     CairoTestWindow(BRect frame, const char* title);
56     virtual ~CairoTestWindow();
57     BView* View() const { return mView; }
58 private:
59     BView* mView;
60 };
61
62 CairoTestWindow::CairoTestWindow(BRect frame, const char* title)
63     : BWindow(frame, title, B_TITLED_WINDOW,
64               B_NOT_RESIZABLE|B_NOT_ZOOMABLE)
65 {
66     mView = new BView(frame, "CairoWindowTestView", B_FOLLOW_ALL_SIDES, 0);
67     AddChild(mView);
68     Show();
69
70     // Make sure the window is actually on screen
71     Lock();
72     Sync();
73     mView->SetViewColor(B_TRANSPARENT_COLOR);
74     mView->Sync();
75     Unlock();
76 }
77
78 CairoTestWindow::~CairoTestWindow()
79 {
80     RemoveChild(mView);
81     delete mView;
82 }
83
84
85 class nsBeOSApp : public BApplication
86 {
87 public:
88     nsBeOSApp(sem_id sem) : BApplication(GetAppSig()), init(sem)
89     {}
90
91     void ReadyToRun()
92     {
93         release_sem(init);
94     }
95
96     static int32 Main(void *args)
97     {
98         nsBeOSApp *app = new nsBeOSApp( (sem_id)args );
99         if(app == NULL)
100             return B_ERROR;
101         return app->Run();
102     }
103
104 private:
105
106     const char *GetAppSig()
107     {
108         return "application/x-vnd.cairo-test-app";
109     }
110
111     sem_id init;
112 }; //class nsBeOSApp
113
114 class AppRunner
115 {
116     public:
117         AppRunner();
118         ~AppRunner();
119 };
120
121 AppRunner::AppRunner()
122 {
123     if (be_app)
124         return;
125
126     sem_id initsem = create_sem(0, "Cairo BApplication init");
127     if (initsem < B_OK) {
128         fprintf (stderr, "Error creating BeOS initialization semaphore\n");
129         return;
130     }
131
132     thread_id tid = spawn_thread(nsBeOSApp::Main, "Cairo/BeOS test", B_NORMAL_PRIORITY, (void *)initsem);
133     if (tid < B_OK || B_OK != resume_thread(tid)) {
134         fprintf (stderr, "Error spawning thread\n");
135         return;
136     }
137
138     if (B_OK != acquire_sem(initsem)) {
139         fprintf (stderr, "Error acquiring semaphore\n");
140         return;
141     }
142
143     delete_sem(initsem);
144     return;
145 }
146
147 AppRunner::~AppRunner()
148 {
149     if (be_app) {
150         if (be_app->Lock())
151             be_app->Quit();
152         delete be_app;
153         be_app = NULL;
154     }
155 }
156
157 // Make sure that the BApplication is initialized
158 static AppRunner sAppRunner;
159
160 struct beos_boilerplate_closure {
161     BView* view;
162     BBitmap* bitmap;
163     BWindow* window;
164 };
165
166 // Test a real window
167 static cairo_surface_t *
168 _cairo_boilerplate_beos_create_surface (const char                *name,
169                                         cairo_content_t            content,
170                                         double                     width,
171                                         double                     height,
172                                         cairo_boilerplate_mode_t   mode,
173                                         void                     **closure)
174 {
175     float right = width ? width - 1 : 0;
176     float bottom = height ? height - 1 : 0;
177     BRect rect(0.0, 0.0, right, bottom);
178     CairoTestWindow* wnd = new CairoTestWindow(rect, name);
179
180     beos_boilerplate_closure* bclosure = new beos_boilerplate_closure;
181     bclosure->view = wnd->View();
182     bclosure->bitmap = NULL;
183     bclosure->window = wnd;
184
185     *closure = bclosure;
186
187     return cairo_beos_surface_create(wnd->View());
188 }
189
190 static void
191 _cairo_boilerplate_beos_cleanup (void *closure)
192 {
193     beos_boilerplate_closure* bclosure = reinterpret_cast<beos_boilerplate_closure*>(closure);
194
195     bclosure->window->Lock();
196     bclosure->window->Quit();
197
198     delete bclosure;
199 }
200
201 // Test a bitmap
202 static cairo_surface_t *
203 _cairo_boilerplate_beos_create_surface_for_bitmap (const char                *name,
204                                                    cairo_content_t            content,
205                                                    double                     width,
206                                                    double                     height,
207                                                    cairo_boilerplate_mode_t   mode,
208                                                    void                     **closure)
209 {
210     BRect rect(0.0, 0.0, width - 1, height - 1);
211     color_space beosformat = (content == CAIRO_CONTENT_COLOR_ALPHA) ? B_RGBA32
212                                                                     : B_RGB32;
213     BBitmap* bmp = new BBitmap(rect, beosformat, true);
214     BView* view = new BView(rect, "Cairo test view", B_FOLLOW_ALL_SIDES, 0);
215     bmp->AddChild(view);
216
217     beos_boilerplate_closure* bclosure = new beos_boilerplate_closure;
218     bclosure->view = view;
219     bclosure->bitmap = bmp;
220     bclosure->window = NULL;
221     *closure = bclosure;
222
223     return cairo_beos_surface_create_for_bitmap(view, bmp);
224 }
225
226 static void
227 _cairo_boilerplate_beos_cleanup_bitmap (void *closure)
228 {
229     beos_boilerplate_closure* bclosure = reinterpret_cast<beos_boilerplate_closure*>(closure);
230
231     bclosure->bitmap->RemoveChild(bclosure->view);
232
233
234     delete bclosure->view;
235     delete bclosure->bitmap;
236
237     delete bclosure;
238 }
239
240 static const cairo_boilerplate_target_t targets[] = {
241     /* BeOS sometimes produces a slightly different image. Perhaps this
242      * is related to the fact that it doesn't use premultiplied alpha...
243      * Just ignore the small difference. */
244     {
245         "beos", "beos", NULL, NULL,
246         CAIRO_SURFACE_TYPE_BEOS, CAIRO_CONTENT_COLOR, 1,
247         _cairo_boilerplate_beos_create_surface,
248         NULL, NULL,
249         _cairo_boilerplate_get_image_surface,
250         cairo_surface_write_to_png,
251         _cairo_boilerplate_beos_cleanup
252     },
253     {
254         "beos-bitmap", "beos", NULL, NULL,
255         CAIRO_SURFACE_TYPE_BEOS, CAIRO_CONTENT_COLOR, 1,
256         _cairo_boilerplate_beos_create_surface_for_bitmap,
257         NULL, NULL,
258         _cairo_boilerplate_get_image_surface,
259         cairo_surface_write_to_png,
260         _cairo_boilerplate_beos_cleanup_bitmap
261     },
262     {
263         "beos-bitmap", "beos", NULL, NULL,
264         CAIRO_SURFACE_TYPE_BEOS, CAIRO_CONTENT_COLOR_ALPHA, 1,
265         _cairo_boilerplate_beos_create_surface_for_bitmap,
266         NULL, NULL,
267         _cairo_boilerplate_get_image_surface,
268         cairo_surface_write_to_png,
269         _cairo_boilerplate_beos_cleanup_bitmap
270     },
271 };
272 CAIRO_BOILERPLATE (beos, targets)
273