Usermode/AxWin4 - Added text rendering (very hacky using VGA font)
[tpg/acess2.git] / Usermode / Applications / axwin4_src / Server / main.cpp
1 /*
2  * Acess2 GUI v4 (AxWin4)
3  * - By John Hodge (thePowesGang)
4  * 
5  * main.cpp
6  * - Program main
7  */
8 #include <CConfig.hpp>
9 #include <ipc.hpp>
10 #include <input.hpp>
11 #include <video.hpp>
12 #include <CCompositor.hpp>
13 #include <timing.hpp>
14 #include <exception>
15 #include <algorithm>
16 #include <common.hpp>
17
18 #include <system_error>
19 #include <cerrno>
20
21 extern "C" {
22 #include <stdio.h>
23 #include <stdint.h>
24 };
25
26 using namespace AxWin;
27
28 // === CODE ===
29 int main(int argc, char *argv[])
30 {
31         // - Load configuration (from file and argv)
32         CConfig config;
33         try {
34                 config.parseCommandline(argc, argv);
35         }
36         catch(const std::exception& e) {
37                 fprintf(stderr, "Exception: %s\n", e.what());
38                 _SysDebug("Config threw exception: %s", e.what());
39                 return 1;
40         }
41         
42         CVideo* vid = 0;
43         CCompositor*    compositor = 0;
44         CInput* input = 0;
45         
46         try {
47                 // - Open graphics
48                 vid = new CVideo(config.m_video);
49                 ::_SysDebug("vid = %p", vid);
50                 // - Initialise compositor structures
51                 compositor = new CCompositor(/*config.m_compositor,*/ *vid);
52                 ::_SysDebug("compositor = %p", compositor);
53                 // - Open input
54                 input = new CInput(config.m_input, *compositor);
55                 ::_SysDebug("input = %p", input);
56                 //  > Handles hotkeys?
57                 // - Bind IPC channels
58                 IPC::Initialise(config.m_ipc, *compositor);
59                 ::_SysDebug("IPC up");
60                 // - Start root child process (from config)
61                 // TODO: Spin up child process
62                 
63                 {
64                         const char *interface_app = "/Acess/Apps/AxWin/4.0/AxWinUI";
65                         const char *argv[] = {interface_app, NULL};
66                          int    rv = _SysSpawn(interface_app, argv, NULL, 0, NULL, NULL);
67                         if( rv < 0 ) {
68                                 _SysDebug("_SysSpawn chucked a sad, rv=%i, errno=%i", rv, _errno);
69                                 throw ::std::system_error(errno, ::std::system_category());
70                         }
71                 }
72         }
73         catch(const ::std::exception& e) {
74                 fprintf(stderr, "Startup threw exception: %s\n", e.what());
75                 _SysDebug("Startup threw exception: %s", e.what());
76                 delete input;
77                 delete compositor;
78                 delete vid;
79                 return 1;
80         }
81         
82         // - Event loop
83         for( ;; )
84         {
85                  int    nfd = 0;
86                 fd_set  rfds, efds;
87                 
88                 FD_ZERO(&rfds);
89                 FD_ZERO(&efds);
90         
91                 nfd = ::std::max(nfd, input->FillSelect(rfds));
92                 nfd = ::std::max(nfd, IPC::FillSelect(rfds));
93                 
94                 for( int i = 0; i < nfd; i ++ )
95                         if( FD_ISSET(i, &rfds) )
96                                 FD_SET(i, &efds);
97
98                 #if 0
99                 for( int i = 0; i < nfd; i ++ ) {
100                         if( FD_ISSET(i, &rfds) ) {
101                                 _SysDebug("FD%i", i);
102                         }
103                 }
104                 #endif
105                 
106                 // TODO: Support _SysSendMessage IPC?
107                 int64_t timeout = Timing::GetTimeToNextEvent();
108                 int64_t *timeoutp;
109                 if( timeout >= 0 ) {
110                         ::_SysDebug("Calling select with timeout %lli", timeout);
111                         timeoutp = &timeout;
112                 }
113                 else {
114                         //::_SysDebug("Calling select with no timeout");
115                         timeoutp = 0;
116                 }
117                 int rv = ::_SysSelect(nfd, &rfds, NULL, NULL/*&efds*/, timeoutp, 0);
118                 
119                 #if 0
120                 for( int i = 0; i < nfd; i ++ ) {
121                         if( FD_ISSET(i, &rfds) ) {
122                                 _SysDebug("FD%i", i);
123                         }
124                 }
125                 #endif
126                 //_SysDebug("rv=%i, timeout=%lli", rv, timeout);
127                 
128                 try {
129                         Timing::CheckEvents();
130                         
131                         input->HandleSelect(rfds);
132                         IPC::HandleSelect(rfds);
133                         
134                         compositor->Redraw();
135                 }
136                 catch(...) {
137                         ::_SysDebug("Exception during select handling");
138                 }
139         }
140         return 0;
141 }
142
143 namespace AxWin {
144
145 const char* InitFailure::what() const throw()
146 {
147         return m_what;
148 }
149
150
151 }
152

UCC git Repository :: git.ucc.asn.au