Usermode/AxWin4 - Server implementation runnable
[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                 for( int i = 0; i < nfd; i ++ ) {
99                         if( FD_ISSET(i, &rfds) ) {
100                                 _SysDebug("FD%i", i);
101                         }
102                 }
103                 
104                 // TODO: Support _SysSendMessage IPC?
105                 int64_t timeout = Timing::GetTimeToNextEvent();
106                 int64_t *timeoutp;
107                 if( timeout >= 0 ) {
108                         ::_SysDebug("Calling select with timeout %lli", timeout);
109                         timeoutp = &timeout;
110                 }
111                 else {
112                         ::_SysDebug("Calling select with no timeout");
113                         timeoutp = 0;
114                 }
115                 int rv = ::_SysSelect(nfd, &rfds, NULL, NULL/*&efds*/, timeoutp, 0);
116                 
117                 for( int i = 0; i < nfd; i ++ ) {
118                         if( FD_ISSET(i, &rfds) ) {
119                                 _SysDebug("FD%i", i);
120                         }
121                 }
122                 _SysDebug("rv=%i, timeout=%lli", rv, timeout);
123                 
124                 Timing::CheckEvents();
125                 
126                 input->HandleSelect(rfds);
127                 IPC::HandleSelect(rfds);
128                 
129                 compositor->Redraw();
130         }
131         return 0;
132 }
133
134 namespace AxWin {
135
136 const char* InitFailure::what() const throw()
137 {
138         return m_what;
139 }
140
141
142 }
143

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