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

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