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

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