AcessNative - Bugfixes 'r' us, GUI can start and render (partially)
[tpg/acess2.git] / AcessNative / acesskernel_src / main.c
1 /*
2  * Acess2 Native Kernel
3  * - Acess kernel emulation on another OS using SDL and UDP
4  *
5  * Kernel Main
6  */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <signal.h>
10 #include <stdint.h>
11 #ifdef __LINUX__
12 #include <sys/prctl.h>
13 #endif
14 #include <unistd.h>
15 #include <string.h>
16
17 #define VALGRIND_CLIENT 0
18
19 // === IMPORTS ===
20 extern int      UI_Initialise(int Width, int Height);
21 extern void     UI_MainLoop(void);
22 extern int      VFS_Init(void);
23 extern int      Video_Install(char **Arguments);
24 extern int      NativeKeyboard_Install(char **Arguments);
25 extern int      NativeFS_Install(char **Arguments);
26 extern void     Debug_SetKTerminal(char *Path);
27 extern int      VT_Install(char **Arguments);
28 extern int      VFS_Mount(const char *Device, const char *MountPoint, const char *Filesystem, const char *Options);
29 extern int      VFS_MkDir(const char *Path);
30 extern int      SyscallServer(void);
31 extern int      Server_Shutdown(void);
32 extern const char       gsKernelVersion[];
33 extern const char       gsGitHash[];
34 extern int      giBuildNumber;
35
36 // === GLOBALS ===
37 const char      *gsAcessDir = "../Usermode/Output/x86_64";
38
39 // === CODE ===
40 int main(int argc, char *argv[])
41 {
42         char    **rootapp = NULL;
43          int    rootapp_argc, i;
44         // Parse command line settings
45         for( i = 1; i < argc; i ++ )
46         {
47                 if( strcmp(argv[i], "--distroot") == 0 ) {
48                         gsAcessDir = argv[++i];
49                 }
50                 else if( strcmp(argv[i], "--rootapp") == 0 ) {
51                         rootapp = &argv[++i];
52                         rootapp_argc = argc - i;
53                         break;
54                 }
55                 else {
56                         fprintf(stderr, "Unknown command line option '%s'\n", argv[i]);
57                         return -1;
58                 }
59         }       
60
61         // Kernel build information
62         printf("Acess2 Native v%s\n", gsKernelVersion);
63         printf(" Build %i, Git Hash %s\n", giBuildNumber, gsGitHash);
64
65         // Start UI subsystem
66         UI_Initialise(800, 480);
67         
68         // - Ignore SIGUSR1 (used to wake threads)
69         signal(SIGUSR1, SIG_IGN);
70                 
71         // Initialise VFS
72         VFS_Init();
73         // - Start IO Drivers
74         if( Video_Install(NULL) ) {
75                 Log_Error("Init", "Unable to load NativeVideo");
76         }
77         if( NativeKeyboard_Install(NULL) ) {
78                 Log_Error("Init", "Unable to load NativeKeyboard");
79         }
80         NativeFS_Install(NULL);
81         // - Start VTerm
82         {
83                 char    *args[] = {
84                         "Video=NativeVideo",
85                         "Input=NativeKeyboard",
86                         NULL
87                         };
88                 VT_Install(args);
89         }
90
91         VFS_MkDir("/Acess");    
92         VFS_Mount(gsAcessDir, "/Acess", "nativefs", "");
93
94         Debug_SetKTerminal("/Devices/VTerm/8");
95         
96         // Start syscall server
97         SyscallServer();
98         
99         // Spawn root application
100         if( rootapp )
101         {
102                  int    pid;
103                  int    argcount = 0;
104                 char    *args[7+rootapp_argc+1+1];
105
106                 #if VALGRIND_CLIENT
107                 args[argcount++] = "valgrind";
108                 #endif
109                 args[argcount++] = "./ld-acess";
110                 args[argcount++] = "--open";    args[argcount++] = "/Devices/VTerm/0";
111                 args[argcount++] = "--open";    args[argcount++] = "/Devices/VTerm/0";
112                 args[argcount++] = "--open";    args[argcount++] = "/Devices/VTerm/0";
113                 for( i = 0; i < rootapp_argc; i ++ )
114                         args[argcount+i] = rootapp[i];
115                 args[argcount+rootapp_argc] = NULL;
116                 
117                 pid = fork();
118                 if(pid < 0) {
119                         perror("Starting root application [fork(2)]");
120                         return 1;
121                 }
122                 if(pid == 0)
123                 {
124                         #ifdef __LINUX__
125                         prctl(PR_SET_PDEATHSIG, SIGHUP);        // LINUX ONLY!
126                         #endif
127                         #if VALGRIND_CLIENT
128                         execv("valgrind", args);
129                         #else
130                         execv("./ld-acess", args);
131                         #endif
132                 }
133                 printf("Root application running as PID %i\n", pid);
134         }
135
136         UI_MainLoop();
137
138         return 0;
139 }
140
141 void AcessNative_Exit(void)
142 {
143         // TODO: Close client applications too
144         Server_Shutdown();
145         exit(0);
146 }
147
148 uint64_t DivMod64U(uint64_t Num, uint64_t Den, uint64_t *Rem)
149 {
150         if(Rem) *Rem = Num % Den;
151         return Num / Den;
152 }
153
154 int Module_EnsureLoaded(const char *Name)
155 {
156         return 0;
157 }
158

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