1 /* 2 * Copyright 2012, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef CLI_CONTEXT_H 6 #define CLI_CONTEXT_H 7 8 9 #include <sys/cdefs.h> 10 // Needed in histedit.h. 11 #include <histedit.h> 12 13 #include <Locker.h> 14 15 #include "Team.h" 16 17 18 class Team; 19 class UserInterfaceListener; 20 21 22 class CliContext : private Team::Listener { 23 public: 24 enum { 25 EVENT_QUIT = 0x01, 26 EVENT_USER_INTERRUPT = 0x02, 27 EVENT_THREAD_ADDED = 0x04, 28 EVENT_THREAD_REMOVED = 0x08, 29 EVENT_THREAD_STOPPED = 0x10, 30 }; 31 32 public: 33 CliContext(); 34 ~CliContext(); 35 36 status_t Init(Team* team, 37 UserInterfaceListener* listener); 38 void Cleanup(); 39 40 void Terminating(); 41 42 bool IsTerminating() const { return fTerminating; } 43 44 // service methods for the input loop thread follow 45 46 Team* GetTeam() const { return fTeam; } 47 UserInterfaceListener* GetUserInterfaceListener() const 48 { return fListener; } 49 50 Thread* CurrentThread() const { return fCurrentThread; } 51 thread_id CurrentThreadID() const; 52 void SetCurrentThread(Thread* thread); 53 void PrintCurrentThread(); 54 55 const char* PromptUser(const char* prompt); 56 void AddLineToInputHistory(const char* line); 57 58 void QuitSession(bool killTeam); 59 60 void WaitForThreadOrUser(); 61 void ProcessPendingEvents(); 62 63 private: 64 struct Event; 65 66 typedef DoublyLinkedList<Event> EventList; 67 68 private: 69 // Team::Listener 70 virtual void ThreadAdded(const Team::ThreadEvent& event); 71 virtual void ThreadRemoved(const Team::ThreadEvent& event); 72 73 virtual void ThreadStateChanged( 74 const Team::ThreadEvent& event); 75 76 private: 77 void _QueueEvent(Event* event); 78 79 void _PrepareToWaitForEvents(uint32 eventMask); 80 uint32 _WaitForEvents(); 81 void _SignalInputLoop(uint32 events); 82 83 static const char* _GetPrompt(EditLine* editLine); 84 85 private: 86 BLocker fLock; 87 Team* fTeam; 88 UserInterfaceListener* fListener; 89 EditLine* fEditLine; 90 History* fHistory; 91 const char* fPrompt; 92 sem_id fBlockingSemaphore; 93 uint32 fInputLoopWaitingForEvents; 94 uint32 fEventsOccurred; 95 bool fInputLoopWaiting; 96 volatile bool fTerminating; 97 98 Thread* fCurrentThread; 99 100 EventList fPendingEvents; 101 }; 102 103 104 #endif // CLI_CONTEXT_H 105