22 #include "IoMessage.h"
26 #include "SymbianSockets.h"
27 #include "SymbianMain.h"
29 class CConsoleControl
;
31 void executeClipboard(IoState
* state
);
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
)
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
)
62 IoValue
*IoObject_catchException(IoObject
*self
, IoObject
*locals
, IoMessage
*m
)
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
));
72 TRAP(r
, result
= (IoValue
*)IoMessage_locals_valueArgAt_(m
, locals
, 1);
73 IoState_popExceptionCatch_((IoState
*)self
->tag
->state
, eCatch
););
77 IoObject_setSlot_to_((IoObject
*)locals
, USTRING("exceptionName"), eCatch
->caughtName
);
78 if (eCatch
->caughtDescription
)
80 IoObject_setSlot_to_(locals
, USTRING("exceptionDescription"),
81 eCatch
->caughtDescription
);
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
); }
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
, "");
103 IoMessage
*m
= IoMessage_newFromText_label_(self
, s
, label
);
104 IoState_stackRetain_(self
, (IoValue
*)m
);
108 IoBlock *block = IoBlock_new(self);
109 IoBlock_message_(block, m);
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
); }
121 { IoState_callErrorCallback(self
, eCatch
->caughtName
, eCatch
->caughtDescription
); }
122 IoState_popExceptionCatch_(self
, (IoExceptionCatch
*)eCatch
);
123 return (IoValue
*)result
;
126 void ProcessUIEvent()
128 TRequestStatus status
;
131 timer
.After(status
, 10000);
132 User::WaitForAnyRequest();
133 if(status
== KRequestPending
)
136 CActiveScheduler::RunIfReady(error
, CActive::EPriorityIdle
);
137 User::WaitForRequest(status
);
143 CConsoleControl::CConsoleControl() :
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)
184 TUint16
* buffer
= new TUint16
[len
];
185 for(int index
= 0; index
< len
; ++index
)
187 buffer
[index
] = s
[index
];
193 TPtr16
stringToPtr16(char const* s
, int len
)
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
));
210 if(control
->clipboardText
)
212 control
->clipboardText
->InsertL(control
->clipboardText
->DocumentLength(), ptr
);
218 void MyError(void* pVM
, char* s
, char* s2
)
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);
237 void CConsoleControl::ConstructL(CIoUi
* ui
, IoState
* vm
, TRect
& rect
)
241 IoState_userData_(pVM
, this);
247 iConsole
=new(ELeave
) CEikConsoleScreen
;
248 _LIT(KFicl
,"Io v20020910");
249 TPoint p1
= Position();
250 TPoint p2
= PositionRelativeToScreen();
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);
262 CConsoleControl::~CConsoleControl()
265 IoState_userData_(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")));
288 keyBuffer
[keyIndex
] = 0;
289 if(strcmp(keyBuffer
, "/ec") == 0)
291 clipboardText
= CPlainText::NewL();
293 TRAP(r
, executeClipboard(pVM
));
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
;
305 CleanupStack::PopAndDestroy();
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
];
325 historyBuffer
[historyCount
] = new char[strlen(keyBuffer
) + 1];
326 strcpy(historyBuffer
[historyCount
], keyBuffer
);
328 currentHistoryCount
= historyCount
;
330 memset(keyBuffer
, 0, sizeof(keyBuffer
));
334 else if(aChar
== EKeyBackspace
)
339 Print(TPtrC(stringToUint16(" "), strlen(" ")));
344 else if(aChar
== EKeyLeftArrow
)
352 else if(aChar
== EKeyRightArrow
)
355 if(keyBuffer
[keyIndex
] == 0)
357 keyBuffer
[keyIndex
] = ' ';
361 else if(aChar
== EKeyUpArrow
)
363 if(currentHistoryCount
> 0)
365 strcpy(keyBuffer
, historyBuffer
[--currentHistoryCount
]);
366 TPoint pos
= iConsole
->CursorPos();
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
)));
376 keyIndex
= strlen(keyBuffer
) + 1;
382 buffer
[0] = char(aChar
);
384 Print(TPtrC(stringToUint16(buffer
), strlen(buffer
)));
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()
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();
427 delete(iConsoleControl
);
430 void CIoUi::HandleCommandL(TInt aCommand
)
441 void CIoUi::DynInitMenuPaneL(TInt resourceID
, CEikMenuPane
* pane
)
443 if(resourceID
== R_CONS_EXTEND_MENU
)
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
);
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()
482 class CIoDocument
: public CEikDocument
485 CIoDocument(CEikApplication
& aApp
);
486 static CIoDocument
* NewL(CEikApplication
& aApp
);
490 // Override CApaDocument
491 CEikAppUi
* CreateAppUiL();
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
);
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
543 const TUid KUidFiclApp
= {0x10004854};
545 const TUid KUidFiclApp
= {0x10004850};
551 CApaDocument
* CIoApplication::CreateDocumentL()
553 return CIoDocument::NewL(*this);
556 EXPORT_C CApaApplication
* NewApplication()
558 return(new CIoApplication
);
561 GLDEF_C TInt
E32Dll(TDllReason
)
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
);
587 delete[] finalBuffer
;
588 finalBuffer
= newBuffer
;
591 strncpy((char*)finalBuffer
+ currentIndex
, (char*)tempBuffer
.Ptr(), tempBuffer
.Length());
592 currentIndex
+= tempBuffer
.Length();
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
);
604 delete[] finalBuffer
;
605 finalBuffer
= newBuffer
;
608 strncpy((char*)finalBuffer
+ currentIndex
, (char*)tempBuffer
.Ptr(), tempBuffer
.Length());
609 currentIndex
+= tempBuffer
.Length();
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);
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)
648 ctext
[index
] = (*ptr8
)[index
];
650 ctext
[ptr8
->Length()] = 0;
651 IoState_cliInput(pVM
, ctext
);