Texture: got rid of the image resizing, since you can just pass null data to glTexIma...
[io/quag.git] / projects / symbian / symbianmain.cpp
blob73297f5c8c116ca505b7a9315a39130ad08d0540
1 #include <e32base.h>
2 #include <eikappui.h>
3 #include <eikapp.h>
4 #include <e32cons.h>
5 #include <eikconso.h>
6 #include <eikdoc.h>
7 #include <baclipb.h>
8 #include <s32std.h>
9 #include <string.h>
10 #include <txtetext.h>
11 #include <txtrich.h>
12 #include <charconv.h>
13 #include <coecobs.h>
14 #include <eikbtgpc.h>
15 #include <eikenv.h>
16 #include <io.rsg>
18 extern "C"
20 #include "IoState.h"
21 #include "IoNIL.h"
22 #include "IoMessage.h"
23 #include "IoNumber.h"
26 #include "SymbianSockets.h"
27 #include "SymbianMain.h"
29 class CConsoleControl;
31 void executeClipboard(IoState* state);
33 extern "C"
35 void* init_thread_globals();
37 struct thread_globals* get_thread_globals()
39 return (struct thread_globals*)Dll::Tls();
42 void Scheduler_current_(Scheduler* s)
44 Dll::SetTls(s);
47 Scheduler *Scheduler_current(void)
49 return (Scheduler*)Dll::Tls();
53 IoMessage *IoMessage_newFromText_label_(void *state, char *text, char *label);
54 int IoObject_activeCoroutineCount(void) ;
55 void IoObject_yield(IoObject *self);
57 void IoExceptionCatch_jump(IoExceptionCatch *self)
59 User::Leave(1);
62 IoValue *IoObject_catchException(IoObject *self, IoObject *locals, IoMessage *m)
64 IoValue *result;
65 IoMessage_assertArgCount_(m, 3);
67 IoString *exceptionName = (IoString *)IoMessage_locals_stringArgAt_(m, locals, 0);
68 IoExceptionCatch *eCatch = IoState_pushExceptionCatchWithName_((IoState*)self->tag->state,
69 CSTRING(exceptionName));
71 TInt r = 0;
72 TRAP(r, result = (IoValue*)IoMessage_locals_valueArgAt_(m, locals, 1);
73 IoState_popExceptionCatch_((IoState*)self->tag->state, eCatch););
75 if(r != 0)
77 IoObject_setSlot_to_((IoObject*)locals, USTRING("exceptionName"), eCatch->caughtName);
78 if (eCatch->caughtDescription)
80 IoObject_setSlot_to_(locals, USTRING("exceptionDescription"),
81 eCatch->caughtDescription);
83 else
85 IoObject_setSlot_to_(locals, USTRING("exceptionDescription"),
86 USTRING("<no description>"));
88 IoState_popExceptionCatch_((IoState*)self->tag->state, eCatch);
89 result = (IoValue*)IoMessage_locals_valueArgAt_(m, locals, 2);
92 if (!result) { return IONIL(self); }
93 return result;
96 IoValue *IoState_doCString_(IoState *self, char *s, int debug, char *label)
98 IoValue * volatile result = self->ioNil;
99 IoExceptionCatch * volatile eCatch = IoState_pushExceptionCatchWithName_(self, "");
100 TInt r = 0;
101 TRAP(r,
103 IoMessage *m = IoMessage_newFromText_label_(self, s, label);
104 IoState_stackRetain_(self, (IoValue *)m);
105 if (m)
108 IoBlock *block = IoBlock_new(self);
109 IoBlock_message_(block, m);
111 if (debug)
112 { IoState_print_(self, "parsed: "); IoMessage_print(m); IoState_print_(self, "\n"); }
113 result = (IoValue*)IoMessage_locals_performOn_(m, self->lobby, self->lobby);
114 /*result = IoBlock_target_locals_call_(block, self->lobby, self->lobby, m);*/
115 while (Scheduler_coroCount(self->scheduler) > 1)
116 { IoObject_yield(self->mainActor); }
120 if(r != 0)
121 { IoState_callErrorCallback(self, eCatch->caughtName, eCatch->caughtDescription); }
122 IoState_popExceptionCatch_(self, (IoExceptionCatch *)eCatch);
123 return (IoValue *)result;
126 void ProcessUIEvent()
128 TRequestStatus status;
129 RTimer timer;
130 timer.CreateLocal();
131 timer.After(status, 10000);
132 User::WaitForAnyRequest();
133 if(status == KRequestPending)
135 TInt error;
136 CActiveScheduler::RunIfReady(error, CActive::EPriorityIdle);
137 User::WaitForRequest(status);
139 timer.Close();
143 CConsoleControl::CConsoleControl() :
144 keyIndex(0),
145 clipboardText(0),
146 historyCount(0),
147 currentHistoryCount(0)
149 socketServer.Connect();
150 fileServer.Connect();
151 memset(historyBuffer, 0, sizeof(historyBuffer));
154 void CConsoleControl::SetFont(TFontSpec& font) const
156 iConsole->SetFontL(font);
159 void CIoUi::AddControl(CCoeControl* control)
161 iControlList.Append(control);
164 void CIoUi::RemoveControl(CCoeControl* control)
166 TInt f = iControlList.Find(control);
167 if(f != KErrNotFound)
169 iControlList.Remove(f);
173 CEikButtonGroupContainer* CIoUi::GetCba()
175 return CEikButtonGroupContainer::Current();
178 TUint16* stringToUint16(char const* s, int len = -1)
180 if(len == -1)
182 len = strlen(s);
184 TUint16* buffer = new TUint16[len];
185 for(int index = 0; index < len; ++index)
187 buffer[index] = s[index];
190 return buffer;
193 TPtr16 stringToPtr16(char const* s, int len)
195 if(len == -1)
197 len = strlen(s);
199 return TPtr16(stringToUint16(s, len), len, len);
202 void MyPrint(void* state, char* s)
204 IoState* pVM = (IoState*)state;
205 if(IoState_userData(pVM))
207 CConsoleControl* control = reinterpret_cast<CConsoleControl*>(IoState_userData(pVM));
208 TPtrC ptr(stringToUint16(s), strlen(s));
209 control->Print(ptr);
210 if(control->clipboardText)
212 control->clipboardText->InsertL(control->clipboardText->DocumentLength(), ptr);
214 control->Flush();
218 void MyError(void* pVM, char* s, char* s2)
220 MyPrint(pVM, s);
223 void MyExit(void* pVM)
227 CConsoleControl* CConsoleControl::NewL(CIoUi* ui, IoState* vm, TRect& rect)
229 CConsoleControl* self=new (ELeave) CConsoleControl;
230 CleanupStack::PushL(self);
231 self->ConstructL(ui, vm, rect);
232 self->SetFocus(true);
233 CleanupStack::Pop();
234 return self;
237 void CConsoleControl::ConstructL(CIoUi* ui, IoState* vm, TRect& rect)
239 pUI = ui;
240 pVM = vm;
241 IoState_userData_(pVM, this);
242 CreateWindowL();
243 SetRect(rect);
244 EnableDragEvents();
245 SetBlank();
247 iConsole=new(ELeave) CEikConsoleScreen;
248 _LIT(KFicl,"Io v20020910");
249 TPoint p1 = Position();
250 TPoint p2 = PositionRelativeToScreen();
251 TSize s = Size();
252 iConsole->ConstructL(KFicl, Position(), s, 0, EEikConsWinInPixels);
253 iConsole->SetHistorySizeL(100,100);
254 iConsole->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff, CEikScrollBarFrame::EOn);
256 TPtrC fptr(stringToUint16("Swiss"), strlen("Swiss"));
257 TFontSpec font(fptr, 120);
258 SetFont(font);
262 CConsoleControl::~CConsoleControl()
264 fileServer.Close();
265 IoState_userData_(pVM, 0);
266 delete iConsole;
267 pVM = 0;
270 void CConsoleControl::ActivateL()
272 CCoeControl::ActivateL();
273 iConsole->SetKeepCursorInSight(TRUE);
274 iConsole->DrawCursor();
275 iConsole->ConsoleControl()->SetFocus(ETrue, EDrawNow);
278 TKeyResponse CConsoleControl::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
280 if (aType!=EEventKey)
281 return(EKeyWasConsumed);
282 TInt aChar=aKeyEvent.iCode;
284 if(aChar == EKeyEnter)
286 Print(TPtrC(stringToUint16("\n"), strlen("\n")));
287 Flush();
288 keyBuffer[keyIndex] = 0;
289 if(strcmp(keyBuffer, "/ec") == 0)
291 clipboardText = CPlainText::NewL();
292 TInt r = 0;
293 TRAP(r, executeClipboard(pVM));
294 if(r)
296 char buffer[200];
297 sprintf(buffer, "Failed: Leave code %d\n", r);
298 MyPrint(pVM, buffer);
300 CClipboard* cb = CClipboard::NewForWritingLC(fileServer);
301 clipboardText->CopyToStoreL(cb->Store(), cb->StreamDictionary(), 0, clipboardText->DocumentLength());
302 delete clipboardText;
303 clipboardText = 0;
304 cb->CommitL();
305 CleanupStack::PopAndDestroy();
307 else
309 TInt r = 0;
310 IoState_cliInput(pVM, keyBuffer);
314 if(historyCount == 100)
316 delete[] historyBuffer[0];
318 for(int index = 1; index < 100; ++index)
320 historyBuffer[index - 1] = historyBuffer[index];
322 historyCount--;
325 historyBuffer[historyCount] = new char[strlen(keyBuffer) + 1];
326 strcpy(historyBuffer[historyCount], keyBuffer);
327 historyCount++;
328 currentHistoryCount = historyCount;
330 memset(keyBuffer, 0, sizeof(keyBuffer));
331 keyBuffer[0] = 0;
332 keyIndex = 0;
334 else if(aChar == EKeyBackspace)
336 if(keyIndex > 0)
338 iConsole->Left(1);
339 Print(TPtrC(stringToUint16(" "), strlen(" ")));
340 iConsole->Left(1);
341 --keyIndex;
344 else if(aChar == EKeyLeftArrow)
346 if(keyIndex > 0)
348 iConsole->Left(1);
349 --keyIndex;
352 else if(aChar == EKeyRightArrow)
354 iConsole->Right(1);
355 if(keyBuffer[keyIndex] == 0)
357 keyBuffer[keyIndex] = ' ';
359 keyIndex++;
361 else if(aChar == EKeyUpArrow)
363 if(currentHistoryCount > 0)
365 strcpy(keyBuffer, historyBuffer[--currentHistoryCount]);
366 TPoint pos = iConsole->CursorPos();
367 if(keyIndex > 0)
369 iConsole->SetCursorPosAbs(TPoint(pos.iX - keyIndex , pos.iY));
370 iConsole->ClearToEndOfLine();
373 iConsole->SetCursorPosAbs(TPoint(pos.iX - keyIndex , pos.iY));
374 Print(TPtrC(stringToUint16(keyBuffer)));
375 Flush();
376 keyIndex = strlen(keyBuffer) + 1;
379 else
381 char buffer[2];
382 buffer[0] = char(aChar);
383 buffer[1] = 0;
384 Print(TPtrC(stringToUint16(buffer), strlen(buffer)));
385 Flush();
386 keyBuffer[keyIndex++] = char(aChar);
389 return(EKeyWasConsumed);
392 void CConsoleControl::Print(const TDesC& aDes)
394 iConsole->Write(aDes);
397 void CConsoleControl::Flush()
399 iConsole->FlushChars();
403 CIoUi::CIoUi(IoState* vm) : pVM(vm), iConsoleControl(0)
407 void CIoUi::ConstructL()
409 BaseConstructL();
410 CreateConsoleL();
411 _LIT(KCommands,"\nio v20020925 Started\n\n");
413 iConsoleControl->Print(KCommands);
416 void CIoUi::CreateConsoleL()
418 TRect rect(ClientRect());
419 iConsoleControl=CConsoleControl::NewL(this, pVM, rect);
420 IoState_userData_(pVM, iConsoleControl);
421 AddToStackL(iConsoleControl);
422 iConsoleControl->ActivateL();
425 CIoUi::~CIoUi()
427 delete(iConsoleControl);
430 void CIoUi::HandleCommandL(TInt aCommand)
432 switch (aCommand)
434 case EEikCmdExit:
435 OnExitCommand();
436 break;
437 default:;
441 void CIoUi::DynInitMenuPaneL(TInt resourceID, CEikMenuPane* pane)
443 if(resourceID == R_CONS_EXTEND_MENU)
445 // What to do here
449 void CIoUi::MakeVisible(CCoeControl* control)
451 if(iConsoleControl->IsVisible())
453 if(iControlList.Count() > 0)
455 iConsoleControl->MakeVisible(false);
456 RemoveFromStack(iConsoleControl);
457 control->MakeVisible(true);
458 AddToStackL(control);
461 else
463 int count = iControlList.Count();
464 for(int i = 0; i < count; ++i)
466 if(iControlList[i]->IsVisible())
468 iControlList[i]->MakeVisible(false);
469 RemoveFromStack(iControlList[i]);
472 control->MakeVisible(true);
473 AddToStackL(control);
477 void CIoUi::OnExitCommand()
479 Exit();
482 class CIoDocument : public CEikDocument
484 public:
485 CIoDocument(CEikApplication& aApp);
486 static CIoDocument* NewL(CEikApplication& aApp);
487 ~CIoDocument();
488 void ConstructL();
489 private:
490 // Override CApaDocument
491 CEikAppUi* CreateAppUiL();
492 private:
493 IoState* pState;
496 CIoDocument::CIoDocument(CEikApplication& aApp)
497 : CEikDocument(aApp), pState(0)
501 CIoDocument* CIoDocument::NewL(CEikApplication& aApp)
503 CIoDocument* self=new (ELeave) CIoDocument(aApp);
504 CleanupStack::PushL(self);
505 self->ConstructL();
506 CleanupStack::Pop();
507 return self;
510 void CIoDocument::ConstructL()
512 pState = IoState_new();
513 IoState_userData_(pState, 0);
514 IoState_printCallback_(pState, MyPrint);
515 IoState_errorCallback_(pState, MyError);
516 IoState_exitCallback_(pState, MyExit);
517 IoState_pauseGarbageCollector(pState);
518 initSocketAddons(pState);
519 IoState_resumeGarbageCollector(pState);
523 CIoDocument::~CIoDocument()
525 IoState_free(pState);
528 CEikAppUi* CIoDocument::CreateAppUiL()
530 return(new(ELeave) CIoUi(pState));
533 class CIoApplication : public CEikApplication
535 private: // from CApaApplication
536 CApaDocument* CreateDocumentL();
537 TUid AppDllUid() const;
540 TUid CIoApplication::AppDllUid() const
542 #ifdef _UNICODE
543 const TUid KUidFiclApp = {0x10004854};
544 #else
545 const TUid KUidFiclApp = {0x10004850};
546 #endif
548 return(KUidFiclApp);
551 CApaDocument* CIoApplication::CreateDocumentL()
553 return CIoDocument::NewL(*this);
556 EXPORT_C CApaApplication* NewApplication()
558 return(new CIoApplication);
561 GLDEF_C TInt E32Dll(TDllReason)
563 return(KErrNone);
566 TPtr8* narrowTptr16(RFs& fs, TPtr16* p16, int delete16)
568 CCnvCharacterSetConverter* converter = CCnvCharacterSetConverter::NewL();
569 converter->PrepareToConvertToOrFromL(KCharacterSetIdentifierAscii, fs);
572 TPtrC16 originalText(*p16);
573 TInt length = originalText.Length() * 3;
574 TUint8* finalBuffer = new TUint8[length + 1];
575 memset(finalBuffer, 0, length + 1);
576 TInt currentIndex = 0;
578 TBuf8<100> tempBuffer;
580 TInt result = converter->ConvertFromUnicode(tempBuffer, originalText);
581 if(tempBuffer.Length() >= length)
583 TUint8* newBuffer = new TUint8[length * 2];
584 memset(newBuffer, 0, length * 2);
585 memcpy(newBuffer, finalBuffer, length);
586 length = length * 2;
587 delete[] finalBuffer;
588 finalBuffer = newBuffer;
591 strncpy((char*)finalBuffer + currentIndex, (char*)tempBuffer.Ptr(), tempBuffer.Length());
592 currentIndex += tempBuffer.Length();
594 while(result > 0)
596 originalText.Set(originalText.Right(result));
597 result = converter->ConvertFromUnicode(tempBuffer, originalText);
598 if(currentIndex + tempBuffer.Length() >= length)
600 TUint8* newBuffer = new TUint8[length * 2];
601 memset(newBuffer, 0, length * 2);
602 memcpy(newBuffer, finalBuffer, length);
603 length = length * 2;
604 delete[] finalBuffer;
605 finalBuffer = newBuffer;
608 strncpy((char*)finalBuffer + currentIndex, (char*)tempBuffer.Ptr(), tempBuffer.Length());
609 currentIndex += tempBuffer.Length();
612 if(delete16 != 0)
614 delete p16;
617 delete converter;
619 return new TPtr8(finalBuffer, currentIndex, length);
622 void executeClipboard(IoState* pVM)
624 RFs& fs = ((CConsoleControl*)IoState_userData(pVM))->fileServer;
625 CClipboard* cb = CClipboard::NewForReadingL(fs);
626 TStreamId id = (cb->StreamDictionary()).At(KClipboardUidTypePlainText);
627 if(id == KNullStreamId)
629 MyPrint(pVM, "Clipboard execution failed\n");
632 CPlainText* ptext = CPlainText::NewL();
633 ptext->PasteFromStoreL(cb->Store(), cb->StreamDictionary(), 0);
634 TUint16 *buffer = new TUint16[ptext->DocumentLength()];
635 TPtr16 *text = new TPtr16(buffer, ptext->DocumentLength());
636 ptext->Extract(*text);
638 TPtr8* ptr8 = narrowTptr16(fs, text, 1);
639 delete[] buffer;
641 char* ctext = new char[ptr8->Length() + 1];
642 for(int index = 0; index < ptr8->Length(); ++index)
644 if((*ptr8)[index] == '\t' ||
645 (*ptr8)[index] == 26)
646 ctext[index] = ' ';
647 else
648 ctext[index] = (*ptr8)[index];
650 ctext[ptr8->Length()] = 0;
651 IoState_cliInput(pVM, ctext);
653 delete cb;
654 delete[] ctext;
655 delete ptr8;