Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
Samples/LedIt/Sources/Main.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4
5#include "Stroika/Foundation/StroikaPreComp.h"
6
7#if qStroika_Foundation_Common_Platform_MacOS
8#include <new.h>
9
10#include <UDrawingState.h> // for class UQDGlobals
11#include <UMemoryMgr.h> // for InitializeHeap
12#elif qStroika_FeatureSupported_XWindows
13#include <gdk/gdkx.h>
14#include <gtk/gtk.h>
15#include <stdio.h>
16#elif defined(WIN32)
17#include <afxwin.h>
18#endif
19
21
22#include "LedItApplication.h"
23
24/*
25 * Config/Defines
26 */
27#if qStroika_Foundation_Common_Platform_MacOS
28#if __profile__
29#define qProfile 1
30#endif
31
32#ifndef qProfile
33#define qProfile 0
34#endif
35#elif qStroika_FeatureSupported_XWindows
36#define qSlowXDebugSyncMode 0
37//#define qSlowXDebugSyncMode qStroika_Foundation_Debug_AssertionsChecked
38#ifndef qUseMyXErrorHandlers
39#define qUseMyXErrorHandlers qStroika_Foundation_Debug_AssertionsChecked
40#endif
41#endif
42
43using namespace Stroika::Foundation;
44using namespace Stroika::Frameworks::Led;
45using namespace Stroika::Frameworks::Led::Platform;
46
47/*
48 * Profiling/Mac memory Management.
49 */
50#if qStroika_Foundation_Common_Platform_MacOS
51
52#if qProfile && defined(__MWERKS__)
53#include "profiler.h"
54#endif
55
56#if qUseMacTmpMemForAllocs
57inline char* DoSysAlloc (size_t n)
58{
59#if DEBUG_NEW
60 gDebugNewFlags &= ~dnCheckBlocksInApplZone;
61#endif
62 OSErr err; // ignored...
63 Handle h = ::TempNewHandle (n, &err); // try temp mem, and use our local mem if no temp mem left
64 if (h == NULL) {
65 h = ::NewHandle (n);
66 // Don't use up the last few K of heap-memory cuz mac toolbox doesn't like it! -LGP 961021
67 if (h != NULL) {
68 Handle x = ::NewHandle (4 * 1024);
69 if (x == NULL) {
70 ::DisposeHandle (h);
71 h = NULL;
72 }
73 else {
74 ::DisposeHandle (x);
75 }
76 }
77 }
78 if (h == NULL) {
79 return NULL;
80 }
81 ::HLock (h);
82 return *h;
83}
84inline void DoSysFree (void* p)
85{
86 Handle h = ::RecoverHandle (reinterpret_cast<Ptr> (p));
87 ::HUnlock (h);
88 ::DisposeHandle (h);
89}
90inline size_t DoGetPtrSize (void* p)
91{
92 Handle h = ::RecoverHandle (reinterpret_cast<Ptr> (p));
93 return ::GetHandleSize (h);
94}
95#endif
96
97#if qUseMacTmpMemForAllocs
98/*
99 * In CW5Pro and earlier - I hooked into the Metrowerks memory allocation system via #including "New.cpp" and
100 * doing #defines of NewPtr and DisposePtr () etc. But that no longer works with CWPro7 (I'm not sure about
101 * CWPro6). As of CWPro7 - there are several malloc implementations. This below code seems to work for all of
102 * them (from purusing the code in alloc.c, pool_alloc.c, pool_alloc.h, pool_alloc.mac.c). And empiricly it
103 * works fine for the default allocation algorithm -- LGP 2001-09-19
104 */
105#include "pool_alloc.h"
106extern "C" {
107void* __sys_alloc (std::size_t n)
108{
109 return DoSysAlloc (n);
110}
111void __sys_free (void* p)
112{
113 DoSysFree (p);
114}
115std::size_t __sys_pointer_size (void* p)
116{
117 return ::DoGetPtrSize (p);
118}
119}
120#endif
121
122#if DEBUG_NEW
123#define NEWMODE NEWMODE_MALLOC
124#include "DebugNew.cp"
125#endif
126
127#endif
128
129using namespace Stroika::Foundation;
130using namespace Stroika::Frameworks::Led;
131
132#if qStroika_FeatureSupported_XWindows && qUseMyXErrorHandlers
133static int MyXErrorHandler (Display* display, XErrorEvent* error)
134{
135 if (error->error_code) {
136 if (gdk_error_warnings) {
137 char buf[64];
138 XGetErrorText (display, error->error_code, buf, 63);
139 g_error ("%s\n serial %ld error_code %d request_code %d minor_code %d\n", buf, error->serial, error->error_code,
140 error->request_code, error->minor_code);
141 }
142 gdk_error_code = error->error_code;
143 }
144 return 0;
145}
146#endif
147
148#if qStroika_Foundation_Common_Platform_MacOS || qStroika_FeatureSupported_XWindows
149int main ([[maybe_unused]]int argc, [maybe_unused]]char** argv)
150{
151#if qStroika_Foundation_Common_Platform_MacOS
153 // Set Debugging options
154 SetDebugThrow_ (debugAction_Alert);
155 SetDebugSignal_ (debugAction_Alert);
156 }
157
158#if !TARGET_CARBON
159 const long kMinStack = 32l * 1024l; // reserve 32K for stack
160 ::SetApplLimit ((Ptr)((long)(::GetApplLimit ())-kMinStack));
161#endif
162
163 /*
164 * Initialize Memory Manager Parameter is number of Master Pointer blocks to allocate
165 */
166 ::InitializeHeap (3);
167
168 // Initialize standard Toolbox managers
169 UQDGlobals::InitializeToolbox ();
170
171#if qProfile && defined(__MWERKS__)
172 OSErr proErr = ::ProfilerInit (collectDetailed, bestTimeBase, 10000, 1000);
173#endif
174
175 LedItApplication theApp; // Create instance of your Application
176 theApp.Run (); // class and run it
177
178#if qProfile && defined(__MWERKS__)
179 ::ProfilerDump ("\pLedProfile.prof");
180 ::ProfilerTerm ();
181#endif
182
183#if DEBUG_NEW > 0 && DEBUG_NEW >= DEBUG_NEW_LEAKS
184 // Hmm. produces link error doing this on CW6Pro? But seems to work OK without... LGP 2001-07-17
185 ::DebugNewValidateAllBlocks ();
186#endif
187#elif qStroika_FeatureSupported_XWindows
188 gtk_set_locale ();
189 gtk_init (&argc, &argv);
190#if qUseMyXErrorHandlers
191 XSetErrorHandler (MyXErrorHandler);
192#endif
193
194#if qSlowXDebugSyncMode
195 (void)XSynchronize (GDK_DISPLAY (), true);
196#endif
197
198 LedItApplication app;
199 gtk_main ();
200#endif
201 return (0);
202}
203#endif
#define qStroika_Foundation_Debug_AssertionsChecked
The qStroika_Foundation_Debug_AssertionsChecked flag determines if assertions are checked and validat...
Definition Assertions.h:48