| | 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 | // BeOS's C++ compiler does not support varargs in macros |
| | 42 | // So, define CAIRO_BOILERPLATE_LOG here |
| | 43 | #define CAIRO_BOILERPLATE_LOG cairo_beos_boilerplate_log |
| | 44 | |
| | 45 | extern "C" { |
| | 46 | #include "cairo-boilerplate.h" |
| | 47 | } |
| | 48 | #include "cairo-boilerplate-beos-private.h" |
| | 49 | |
| | 50 | #include <cairo-beos.h> |
| | 51 | |
| | 52 | // Part of this code was originally part of |
| | 53 | // xpfe/bootstrap/nsNativeAppSupportBeOS.cpp in the Mozilla source code. |
| | 54 | |
| | 55 | #include <Application.h> |
| | 56 | #include <Window.h> |
| | 57 | #include <View.h> |
| | 58 | #include <Bitmap.h> |
| | 59 | |
| | 60 | static int cairo_beos_boilerplate_log(const char* format, ...) { |
| | 61 | va_list args; |
| | 62 | int rv; |
| | 63 | va_start(args, format); |
| | 64 | rv = vfprintf(stderr, format, args); |
| | 65 | va_end(args); |
| | 66 | return rv; |
| | 67 | } |
| | 68 | |
| | 69 | class CairoTestWindow : public BWindow |
| | 70 | { |
| | 71 | public: |
| | 72 | CairoTestWindow(BRect frame, const char* title); |
| | 73 | virtual ~CairoTestWindow(); |
| | 74 | BView* View() const { return mView; } |
| | 75 | private: |
| | 76 | BView* mView; |
| | 77 | }; |
| | 78 | |
| | 79 | CairoTestWindow::CairoTestWindow(BRect frame, const char* title) |
| | 80 | : BWindow(frame, title, B_TITLED_WINDOW, |
| | 81 | B_NOT_RESIZABLE|B_NOT_ZOOMABLE) |
| | 82 | { |
| | 83 | mView = new BView(frame, "CairoWindowTestView", B_FOLLOW_ALL_SIDES, 0); |
| | 84 | AddChild(mView); |
| | 85 | Show(); |
| | 86 | |
| | 87 | // Make sure the window is actually on screen |
| | 88 | Lock(); |
| | 89 | Sync(); |
| | 90 | mView->SetViewColor(B_TRANSPARENT_COLOR); |
| | 91 | mView->Sync(); |
| | 92 | Unlock(); |
| | 93 | } |
| | 94 | |
| | 95 | CairoTestWindow::~CairoTestWindow() |
| | 96 | { |
| | 97 | RemoveChild(mView); |
| | 98 | delete mView; |
| | 99 | } |
| | 100 | |
| | 101 | |
| | 102 | class nsBeOSApp : public BApplication |
| | 103 | { |
| | 104 | public: |
| | 105 | nsBeOSApp(sem_id sem) : BApplication(GetAppSig()), init(sem) |
| | 106 | {} |
| | 107 | |
| | 108 | void ReadyToRun() |
| | 109 | { |
| | 110 | release_sem(init); |
| | 111 | } |
| | 112 | |
| | 113 | static int32 Main(void *args) |
| | 114 | { |
| | 115 | nsBeOSApp *app = new nsBeOSApp( (sem_id)args ); |
| | 116 | if(app == NULL) |
| | 117 | return B_ERROR; |
| | 118 | return app->Run(); |
| | 119 | } |
| | 120 | |
| | 121 | private: |
| | 122 | |
| | 123 | const char *GetAppSig() |
| | 124 | { |
| | 125 | return "application/x-vnd.cairo-test-app"; |
| | 126 | } |
| | 127 | |
| | 128 | sem_id init; |
| | 129 | }; //class nsBeOSApp |
| | 130 | |
| | 131 | class AppRunner |
| | 132 | { |
| | 133 | public: |
| | 134 | AppRunner(); |
| | 135 | ~AppRunner(); |
| | 136 | }; |
| | 137 | |
| | 138 | AppRunner::AppRunner() |
| | 139 | { |
| | 140 | if (be_app) |
| | 141 | return; |
| | 142 | |
| | 143 | sem_id initsem = create_sem(0, "Cairo BApplication init"); |
| | 144 | if (initsem < B_OK) { |
| | 145 | CAIRO_BOILERPLATE_LOG("Error creating BeOS initialization semaphore\n"); |
| | 146 | return; |
| | 147 | } |
| | 148 | |
| | 149 | thread_id tid = spawn_thread(nsBeOSApp::Main, "Cairo/BeOS test", B_NORMAL_PRIORITY, (void *)initsem); |
| | 150 | if (tid < B_OK || B_OK != resume_thread(tid)) { |
| | 151 | CAIRO_BOILERPLATE_LOG("Error spawning thread\n"); |
| | 152 | return; |
| | 153 | } |
| | 154 | |
| | 155 | if (B_OK != acquire_sem(initsem)) { |
| | 156 | CAIRO_BOILERPLATE_LOG("Error acquiring semaphore\n"); |
| | 157 | return; |
| | 158 | } |
| | 159 | |
| | 160 | delete_sem(initsem); |
| | 161 | return; |
| | 162 | } |
| | 163 | |
| | 164 | AppRunner::~AppRunner() |
| | 165 | { |
| | 166 | if (be_app) { |
| | 167 | if (be_app->Lock()) |
| | 168 | be_app->Quit(); |
| | 169 | delete be_app; |
| | 170 | be_app = NULL; |
| | 171 | } |
| | 172 | } |
| | 173 | |
| | 174 | // Make sure that the BApplication is initialized |
| | 175 | static AppRunner sAppRunner; |
| | 176 | |
| | 177 | struct beos_boilerplate_closure |
| | 178 | { |
| | 179 | BView* view; |
| | 180 | BBitmap* bitmap; |
| | 181 | BWindow* window; |
| | 182 | }; |
| | 183 | |
| | 184 | // Test a real window |
| | 185 | cairo_surface_t * |
| | 186 | _cairo_boilerplate_beos_create_surface (const char *name, |
| | 187 | cairo_content_t content, |
| | 188 | int width, |
| | 189 | int height, |
| | 190 | cairo_boilerplate_mode_t mode, |
| | 191 | void **closure) |
| | 192 | { |
| | 193 | float right = width ? width - 1 : 0; |
| | 194 | float bottom = height ? height - 1 : 0; |
| | 195 | BRect rect(0.0, 0.0, right, bottom); |
| | 196 | CairoTestWindow* wnd = new CairoTestWindow(rect, name); |
| | 197 | |
| | 198 | beos_boilerplate_closure* bclosure = new beos_boilerplate_closure; |
| | 199 | bclosure->view = wnd->View(); |
| | 200 | bclosure->bitmap = NULL; |
| | 201 | bclosure->window = wnd; |
| | 202 | |
| | 203 | *closure = bclosure; |
| | 204 | |
| | 205 | return cairo_beos_surface_create(wnd->View()); |
| | 206 | } |
| | 207 | |
| | 208 | void |
| | 209 | _cairo_boilerplate_beos_cleanup (void* closure) |
| | 210 | { |
| | 211 | beos_boilerplate_closure* bclosure = reinterpret_cast<beos_boilerplate_closure*>(closure); |
| | 212 | |
| | 213 | bclosure->window->Lock(); |
| | 214 | bclosure->window->Quit(); |
| | 215 | |
| | 216 | delete bclosure; |
| | 217 | } |
| | 218 | |
| | 219 | // Test a bitmap |
| | 220 | cairo_surface_t * |
| | 221 | _cairo_boilerplate_beos_create_surface_for_bitmap (const char *name, |
| | 222 | cairo_content_t content, |
| | 223 | int width, |
| | 224 | int height, |
| | 225 | cairo_boilerplate_mode_t mode, |
| | 226 | void **closure) |
| | 227 | { |
| | 228 | BRect rect(0.0, 0.0, width - 1, height - 1); |
| | 229 | color_space beosformat = (content == CAIRO_CONTENT_COLOR_ALPHA) ? B_RGBA32 |
| | 230 | : B_RGB32; |
| | 231 | BBitmap* bmp = new BBitmap(rect, beosformat, true); |
| | 232 | BView* view = new BView(rect, "Cairo test view", B_FOLLOW_ALL_SIDES, 0); |
| | 233 | bmp->AddChild(view); |
| | 234 | |
| | 235 | beos_boilerplate_closure* bclosure = new beos_boilerplate_closure; |
| | 236 | bclosure->view = view; |
| | 237 | bclosure->bitmap = bmp; |
| | 238 | bclosure->window = NULL; |
| | 239 | *closure = bclosure; |
| | 240 | |
| | 241 | return cairo_beos_surface_create_for_bitmap(view, bmp); |
| | 242 | } |
| | 243 | |
| | 244 | void |
| | 245 | _cairo_boilerplate_beos_cleanup_bitmap (void* closure) |
| | 246 | { |
| | 247 | beos_boilerplate_closure* bclosure = reinterpret_cast<beos_boilerplate_closure*>(closure); |
| | 248 | |
| | 249 | bclosure->bitmap->RemoveChild(bclosure->view); |
| | 250 | |
| | 251 | |
| | 252 | delete bclosure->view; |
| | 253 | delete bclosure->bitmap; |
| | 254 | |
| | 255 | delete bclosure; |
| | 256 | } |
| | 257 | |
| | 258 | |