Usermode/AxWin3 - Preparing for SDL build
[tpg/acess2.git] / Usermode / Applications / axwin3_src / WM / main.c
1 /*
2  * Acess2 GUI (AxWin) Version 3
3  * - By John Hodge (thePowersGang)
4  *
5  * main.c
6  * - Entrypoint
7  */
8 #include <common.h>
9 #include <acess/sys.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <axwin3/keysyms.h>
13 #include <string.h>
14 #include "include/lowlevel.h"
15
16 // === IMPORTS ===
17 extern void     Video_Setup(void);
18 extern void     WM_Initialise(void);
19 extern int      Renderer_Menu_Init(void);
20 extern int      Renderer_Widget_Init(void);
21 extern int      Renderer_Background_Init(void);
22 extern int      Renderer_Framebuffer_Init(void);
23 extern int      Renderer_RichText_Init(void);
24 extern void     WM_Update(void);
25 extern void     WM_Hotkey_Register(int nKeys, uint32_t *Keys, const char *ActionName);
26
27 // === PROTOTYPES ===
28 void    ParseCommandline(int argc, char **argv);
29
30 // === GLOBALS ===
31 const char      *gsTerminalDevice = NULL;
32 const char      *gsMouseDevice = NULL;
33
34  int    giScreenWidth = 640;
35  int    giScreenHeight = 480;
36
37  int    giTerminalFD = -1;
38  int    giTerminalFD_Input = 0;
39  int    giMouseFD = -1;
40
41 #define __INSTALL_ROOT  "/Acess/Apps/AxWin/3.0"
42
43 const char      *gsInstallRoot = __INSTALL_ROOT;
44 const char      *gsInterfaceApp = __INSTALL_ROOT"/AxWinUI";
45  int    gbNoSpawnUI = 0;
46
47 // === CODE ===
48 /**
49  * \brief Program Entrypoint
50  */
51 int main(int argc, char *argv[])
52 {
53         ParseCommandline(argc, argv);
54         
55         if( gsTerminalDevice == NULL ) {
56                 gsTerminalDevice = "/Devices/VTerm/6";
57         }
58         if( gsMouseDevice == NULL ) {
59                 gsMouseDevice = "/Devices/Mouse/system";
60         }
61         
62         Video_Setup();
63         IPC_Init();
64         Input_Init();
65         
66         Renderer_Menu_Init();
67         Renderer_Widget_Init();
68         Renderer_Background_Init();
69         Renderer_Framebuffer_Init();
70         Renderer_RichText_Init();
71         WM_Initialise();
72
73         // TODO: Move these to config
74         uint32_t        keys[4];
75         keys[0] = KEYSYM_LEFTGUI;       keys[1] = KEYSYM_r;
76         WM_Hotkey_Register(2, keys, "Interface>Run");
77         keys[0] = KEYSYM_LEFTGUI;       keys[1] = KEYSYM_t;
78         WM_Hotkey_Register(2, keys, "Interface>Terminal");
79         keys[0] = KEYSYM_LEFTGUI;       keys[1] = KEYSYM_e;
80         WM_Hotkey_Register(2, keys, "Interface>TextEdit");
81         
82         // Spawn interface root
83         if( !gbNoSpawnUI )
84         {
85                 int     server_tid = gettid();
86                 _SysDebug("server_tid = %i", server_tid);
87                 char    server_info[] = "AXWIN3_SERVER=00000";
88                 const char      *envp[] = {server_info, NULL};
89                 const char      *argv[] = {gsInterfaceApp, NULL};
90                 _SysDebug("server_tid = %i, &server_tid = %p", server_tid, &server_tid);
91                 sprintf(server_info, "AXWIN3_SERVER=%i", server_tid);
92                 // TODO: Does the client need FDs?
93                  int    rv = _SysSpawn(gsInterfaceApp, argv, envp, 0, NULL, NULL);
94                 if( rv < 0 ) {
95                         _SysDebug("_SysSpawn chucked a sad, rv=%i, errno=%i", rv, _errno);
96                 }
97         }
98
99         // Main Loop
100         for(;;)
101         {
102                 fd_set  fds;
103                  int    nfds = 0;
104                 FD_ZERO(&fds);
105         
106                 WM_Update();
107
108                 Input_FillSelect(&nfds, &fds);
109                 IPC_FillSelect(&nfds, &fds);
110                 
111                 nfds ++;
112                 if( _SysSelect(nfds, &fds, NULL, NULL, NULL, THREAD_EVENT_IPCMSG) == -1 ) {
113                         _SysDebug("ERROR: select() returned -1");
114                         return -1;
115                 }
116
117                 Input_HandleSelect(&fds);
118                 IPC_HandleSelect(&fds);
119         }
120         return 0;
121 }
122
123 void PrintUsage(void)
124 {
125         fprintf(stderr, "Arguments:\n");
126         fprintf(stderr, "  --no-ui  : Don't spawn the UI process\n");
127 }
128
129 void ParseCommandline(int argc, char **argv)
130 {
131         for( int i = 1; i < argc; i ++ )
132         {
133                 if( argv[i][0] != '-' ) {
134                         // Error?
135                         PrintUsage();
136                         exit(-1);
137                 }
138                 else if( argv[i][1] != '-' ) {
139                         // Short
140                         PrintUsage();
141                         exit(-1);
142                 }
143                 else {
144                         // Long
145                         if( strcmp(argv[i], "--no-ui") == 0 ) {
146                                 gbNoSpawnUI = 1;
147                         }
148                         else {
149                                 // Error.
150                                 PrintUsage();
151                                 exit(-1);
152                         }
153                 }
154         }
155 }
156

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