App inst fixed on invalid image file; App zinstall for ZDE works - we've got GUI...
[ZeXOS.git] / apps / zde / zinstall / main.c
blobb2ceb98f47274a9c87268a9a1fff75d769abaa1d
1 /*
2 * ZeX/OS
3 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <appcl.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <sys/ioctl.h>
26 typedef struct {
27 unsigned step;
28 char drive[32];
29 int fd;
30 unsigned flen;
31 unsigned long s;
32 struct ioatarq_t rq;
33 appcl_t *appcl;
34 } zinstall_t;
35 zinstall_t zinst;
37 #define IMAGE_FILE "inst.img"
39 void zinstall_draw_step1 ()
41 zgui_puts (10, 40, "First step is focused on hard-drive ID,\nthat mean, you must select\n correct disk for installation.\n\n", 0);
43 zgui_input (10, 80, &zinst.drive, 31, 0);
46 void zinstall_draw_step2 ()
48 zgui_puts (10, 40, "Are you sure you want overwrite\nall data on selected drive ?", 0);
50 zgui_puts (20, 80, zinst.drive, 0);
53 int zinstall_open ()
55 zinst.fd = open (IMAGE_FILE, O_RDONLY);
57 if (zinst.fd < 0) {
58 printf ("ERROR -> image file '%s' not found\n", IMAGE_FILE);
59 return -1;
61 printf ("zinst.fd: %d\n", zinst.fd);
63 zinst.flen = 2048*1024; /* 2MB image */
65 printf ("Image size: %dkB\n", zinst.flen/1024);
67 unsigned l = strlen (zinst.drive);
68 memcpy (zinst.rq.dev, zinst.drive, l);
69 zinst.rq.dev[l] = '\0';
71 zinst.s = 0;
73 return 0;
76 void zinstall_close ()
78 close (zinst.fd);
81 int zinstall_draw_step3 ()
83 unsigned char percent = zinst.s/41+1;
85 char str[32];
86 sprintf (str, "Installing: %d %c", zinst.s/41+1, '%');
87 zgui_puts (10, 40, str, 0);
89 xrectfill (zinst.appcl->x+50, zinst.appcl->y+75, zinst.appcl->x+50+percent*2, zinst.appcl->y+87, 0xffa000);
90 xrectfill (zinst.appcl->x+50+percent*2, zinst.appcl->y+75, zinst.appcl->x+250, zinst.appcl->y+87, 0x0000ff);
92 if (zinst.s >= zinst.flen/512)
93 return 0;
95 zinst.rq.sector = zinst.s ++;
97 memset (zinst.rq.data, 0, 512);
99 int l = read (zinst.fd, zinst.rq.data, 512);
101 if (!l) {
102 printf ("ERROR -> read () == 0; image data are probably corrupted\n");
103 return -1;
106 int r = ioctl (IOATAWRITE, &zinst.rq, sizeof (struct ioatarq_t));
108 if (r == -1) {
109 printf ("WARNING -> ioctl (IOATAWRITE) == -1; repeating\n");
110 sleep (1);
111 r = ioctl (IOATAWRITE, &zinst.rq, sizeof (struct ioatarq_t));
112 if (r == -1) {
113 printf ("ERROR -> ioctl (IOATAWRITE) == -1; installation failed !\n");
114 return -1;
118 return 0;
121 void zinstall_draw_step4 ()
123 zgui_puts (10, 40, "Installation was finished\nCongratulation !", 0);
126 void zinstall_draw ()
128 zgui_puts (10, 10, "ZeX/OS Installer\n-=-=-=-=-=-=-=-=-", 0);
130 unsigned btn_next = zgui_button (150, 145, "Next");
132 if (!btn_next && zinst.step < 4) {
133 zinst.step ++;
135 if (zinst.step == 3) {
136 int r = zinstall_open ();
138 if (r == -1)
139 zinst.step = 5;
140 } else if (zinst.step == 4)
141 zinstall_close ();
144 switch (zinst.step) {
145 case 0:
146 zgui_puts (10, 40, "Welcome !\nYou are attempting to install ZeX/OS\nonto your hard-drive, please be sure\nabout your backup because of data loss.\n\nWarning: This version is in the alpha-stage,\nit is possible that some error may occur.\n\nPress button \"Next\" for continue\nin installation process", 0);
147 break;
148 case 1:
149 return zinstall_draw_step1 ();
150 case 2:
151 return zinstall_draw_step2 ();
152 case 3:
154 int r = zinstall_draw_step3 ();
156 if (r == -1)
157 zinst.step = 5;
158 break;
160 case 4:
161 return zinstall_draw_step4 ();
162 case 5:
163 zgui_puts (10, 40, "Installation failed !\nprobably installer files are missing", 0);
164 break;
168 int main (int argc, char **argv)
170 int exit = 0;
172 zinst.step = 0;
173 memcpy (zinst.drive, "/dev/hda", 8);
174 zinst.drive[8] = '\0';
176 if (zgui_init () == -1)
177 return -1;
179 zinst.appcl = zgui_window ();
181 while (!exit) {
182 unsigned state = zgui_event ();
184 if (state & APPCL_STATE_REDRAW)
185 zinstall_draw ();
187 if (state & APPCL_STATE_EXIT)
188 exit = 1;
191 return zgui_exit ();