]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/services/gfx/mw/v2_0/src/ecos/ecos_init.c
Initial revision
[karo-tx-redboot.git] / packages / services / gfx / mw / v2_0 / src / ecos / ecos_init.c
1 //
2 // Micro-Windows/Nano-X additional setup fo eCos
3 //
4
5 #include <pkgconf/system.h>
6 #include <pkgconf/kernel.h>
7 #include <pkgconf/hal.h>
8 #include <pkgconf/microwindows.h>
9 #include <cyg/kernel/kapi.h>
10 #include <cyg/infra/diag.h>
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <time.h>
17 #include <ctype.h>
18 #include <errno.h>
19 #include <sys/time.h>
20 #include <sys/stat.h>
21
22 #define MWINCLUDECOLORS
23 #include <microwin/nano-X.h>
24 #if 0
25 #include <nwidgets.h>
26 #endif
27
28 #if 0
29 static bool closed = false;
30
31 static void 
32 do_close(NBUTTON * w, int b)
33 {
34    printf("Button %d was clicked in widget %p\n",b,w);
35    closed = true;
36 }
37 #endif
38
39 // Display a number of ticks as microseconds
40 // Note: for improved calculation significance, values are kept in ticks*1000
41 static void
42 show_ns(long long ns)
43 {
44     diag_printf("%5d.%02d", (int)(ns/1000), (int)((ns%1000)/10));
45 }
46
47 static long rtc_resolution[] = CYGNUM_KERNEL_COUNTERS_RTC_RESOLUTION;
48 static long ns_per_system_clock;
49
50 static long long
51 ns_time(void)
52 {
53     cyg_uint32 off;
54     long long ns, clocks;
55
56     ns_per_system_clock = 1000000/rtc_resolution[1];
57     HAL_CLOCK_READ(&off);
58     ns = (ns_per_system_clock * (long long)off) / CYGNUM_KERNEL_COUNTERS_RTC_PERIOD;
59     ns += 5;  // for rounding to .01us
60     clocks = (cyg_current_time() * 10000000) + ns;
61     return clocks;
62 }
63
64 static void
65 test_file_io(void)
66 {
67     long long start_time, end_time;
68     FILE *fp;
69     unsigned char buf[256];
70     int len, n, fd;
71
72     start_time = ns_time();
73     len = 0;
74     if ((fp = fopen("/redhat.logo", "r")) != (FILE *)NULL) {
75         while (fgets(buf, sizeof(buf), fp)) {
76             len += strlen(buf);
77         }
78         fclose(fp);
79     }
80     end_time = ns_time();
81     diag_printf("'fgets': %d bytes in ", len); show_ns(end_time-start_time);  diag_printf("ns\n");
82
83     start_time = ns_time();
84     len = 0;
85     if ((fp = fopen("/redhat.logo", "r")) != (FILE *)NULL) {
86         while ((n = fread(buf, 1, sizeof(buf), fp))) {
87             len += n;
88         }
89         fclose(fp);
90     }
91     end_time = ns_time();
92     diag_printf("'fread': %d bytes in ", len); show_ns(end_time-start_time);  diag_printf("ns\n");
93
94     start_time = ns_time();
95     len = 0;
96     if ((fd = open("/redhat.logo", O_RDONLY)) >= 0) {
97         while ((n = read(fd, buf, sizeof(buf)))) {
98             len += n;
99         }
100         close(fd);
101     }
102     end_time = ns_time();
103     diag_printf("'read': %d bytes in ", len); show_ns(end_time-start_time);  diag_printf("ns\n");
104 }
105
106 void
107 ecos_nx_init(CYG_ADDRWORD data)
108 {
109     GR_SCREEN_INFO      si;             /* window information */
110     GR_FONT_INFO        fi;             /* font information */
111     GR_WINDOW_ID        mainwid;        /* main window id */
112     GR_WM_PROPERTIES    props;
113     GR_GC_ID            gct = 0;
114 #if 0
115     NWIDGET             *w;
116     NBUTTON             *b;
117     NTEXTFIELD          *t;
118 #endif
119
120     cyg_thread_delay(50);
121     INIT_PER_THREAD_DATA();
122
123     test_file_io();
124
125     if(GrOpen() < 0) {
126         fprintf(stderr, "Couldn't connect to Nano-X server\n");
127         exit(1);
128     }
129
130 #if (defined CYGPKG_HAL_ARM_SA11X0_IPAQ) && (!defined CYGBLD_MICROWINDOWS_VNC_DRIVERS)
131     GrSetPortraitMode(MWPORTRAIT_RIGHT);
132 #endif
133     
134     GrGetScreenInfo(&si);
135     GrGetFontInfo(0, &fi);
136
137 #if 1
138     mainwid = GrNewWindow(GR_ROOT_WINDOW_ID, 0, 0, si.cols, si.rows,
139                           0, RED, WHITE);
140
141     props.flags = GR_WM_FLAGS_PROPS;
142     props.props = GR_WM_PROPS_BORDER;
143     GrSetWMProperties(mainwid, &props);
144
145     GrMapWindow(mainwid);
146     GrFlush();
147     cyg_thread_delay(50);
148
149     gct = GrNewGC();
150     GrSetGCForeground(gct, WHITE);
151
152 #ifdef CYGPKG_IO_FILEIO
153     {
154         struct stat         stat_data;
155         if (0 == stat("/redhat.logo", &stat_data)) {
156             GrDrawImageFromFile(mainwid, gct, 0, 0, si.cols, si.rows, "/redhat.logo", 0);
157         }
158     }
159 #endif
160
161 #if (defined CYGPKG_HAL_ARM) && (!defined CYGBLD_MICROWINDOWS_VNC_DRIVERS)
162     // Touch sensitive screen calibration, only relevant on some
163     // platforms.
164     GrSetGCFont(gct, GrCreateFont(GR_FONT_GUI_VAR, 0, NULL));
165     GrText(mainwid, gct, 80, 350, "Tap all 4 corners", 17, GR_TFTOP);
166     GrFlush();
167     printf("Tap all four corners\n");
168     cyg_thread_delay(10*100);
169 #endif    
170
171 #else
172     n_init_button_class();
173     n_init_textfield_class();
174
175     w = NEW_NOBJECT(widget);
176     n_widget_init(w, 0);    
177     n_widget_resize(w, si.cols - 10, si.rows - 30);
178     n_widget_background(w, "/redhat.logo");
179     n_widget_show(w);
180
181     b = NEW_NOBJECT(button);
182     n_button_init(b, w, "Close");
183     n_button_onclick(b, do_close);
184     n_widget_resize(b, 40, 20);
185     n_widget_move(b,180,260);
186     n_widget_show(b);
187
188     t = NEW_NOBJECT(textfield);
189     n_textfield_init(t,w,"Tap all 4 corners");
190     n_widget_move(t,45,220);
191     n_widget_resize(t,120,20);
192     n_widget_show(t);
193
194     t = NEW_NOBJECT(textfield);
195     n_textfield_init(t,w,"Then press close");
196     n_widget_move(t,45,250);
197     n_widget_resize(t,120,20);
198     n_widget_show(t);
199
200     while (!closed) {
201         n_handle_event();
202     }
203
204     n_widget_hide(w);
205     n_object_cleanup(w);
206
207 #endif
208
209     GrClose();
210 }