AcessNative - Fixed windows symlink derps
[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 #ifndef __WIN32__
42 #define P_NOWAIT        0
43 int spawnv(int flags, const char *execuable, char * const argv[])
44 {
45         int pid = fork();
46         if( pid != 0 )  return pid;
47
48         execv(execuable, argv);
49         perror("spawnv - execve");
50         for(;;);
51 }
52 #endif
53
54 int main(int argc, char *argv[])
55 {
56         char    **rootapp = NULL;
57          int    rootapp_argc, i;
58         // Parse command line settings
59         for( i = 1; i < argc; i ++ )
60         {
61                 if( strcmp(argv[i], "--distroot") == 0 ) {
62                         gsAcessDir = argv[++i];
63                 }
64                 else if( strcmp(argv[i], "--rootapp") == 0 ) {
65                         rootapp = &argv[++i];
66                         rootapp_argc = argc - i;
67                         break;
68                 }
69                 else {
70                         fprintf(stderr, "Unknown command line option '%s'\n", argv[i]);
71                         return -1;
72                 }
73         }       
74
75         // Kernel build information
76         printf("Acess2 Native v%s\n", gsKernelVersion);
77         printf(" Build %i, Git Hash %s\n", giBuildNumber, gsGitHash);
78
79         // Start UI subsystem
80         UI_Initialise(800, 480);
81         
82         // - Ignore SIGUSR1 (used to wake threads)
83         #ifdef SIGUSR1
84         signal(SIGUSR1, SIG_IGN);
85         #endif
86                 
87         // Initialise VFS
88         VFS_Init();
89         // - Start IO Drivers
90         if( Video_Install(NULL) ) {
91                 Log_Error("Init", "Unable to load NativeVideo");
92         }
93         if( NativeKeyboard_Install(NULL) ) {
94                 Log_Error("Init", "Unable to load NativeKeyboard");
95         }
96         NativeFS_Install(NULL);
97         // - Start VTerm
98         {
99                 char    *args[] = {
100                         "Video=NativeVideo",
101                         "Input=NativeKeyboard",
102                         NULL
103                         };
104                 VT_Install(args);
105         }
106
107         VFS_MkDir("/Acess");    
108         VFS_Mount(gsAcessDir, "/Acess", "nativefs", "");
109
110         Debug_SetKTerminal("/Devices/VTerm/8");
111         
112         // Start syscall server
113         SyscallServer();
114         
115         // Spawn root application
116         if( rootapp )
117         {
118                  int    pid;
119                  int    argcount = 0;
120                 const char      *args[7+rootapp_argc+1+1];
121
122                 #if VALGRIND_CLIENT
123                 args[argcount++] = "valgrind";
124                 #endif
125                 args[argcount++] = "./ld-acess";
126                 args[argcount++] = "--open";    args[argcount++] = "/Devices/VTerm/0";
127                 args[argcount++] = "--open";    args[argcount++] = "/Devices/VTerm/0";
128                 args[argcount++] = "--open";    args[argcount++] = "/Devices/VTerm/0";
129                 for( i = 0; i < rootapp_argc; i ++ )
130                         args[argcount+i] = rootapp[i];
131                 args[argcount+rootapp_argc] = NULL;
132                 pid = spawnv(P_NOWAIT, "./ld-acess", args);
133                 if(pid < 0) {
134                         perror("Starting root application [fork(2)]");
135                         return 1;
136                 }
137                 printf("Root application running as PID %i\n", pid);
138         }
139
140         UI_MainLoop();
141
142         return 0;
143 }
144
145 void AcessNative_Exit(void)
146 {
147         // TODO: Close client applications too
148         Server_Shutdown();
149         exit(0);
150 }
151
152 uint64_t DivMod64U(uint64_t Num, uint64_t Den, uint64_t *Rem)
153 {
154         if(Rem) *Rem = Num % Den;
155         return Num / Den;
156 }
157
158 int Module_EnsureLoaded(const char *Name)
159 {
160         return 0;
161 }
162

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