OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [MW_0_8_9PRE7/] [mw/] [src/] [mwin/] [winmain.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 673 markom
/*
2
 * Copyright (c) 1999, 2000 Greg Haerr <greg@censoft.com>
3
 *
4
 * Main module of Microwindows
5
 */
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
#include <signal.h>
10
 
11
#ifndef __PACIFIC__
12
#include <errno.h>
13
#include <sys/types.h>
14
#endif
15
 
16
#if UNIX | DOS_DJGPP
17
#include <unistd.h>
18
#if _MINIX
19
#include <sys/times.h>
20
#else
21
#include <sys/time.h>
22
#endif
23
#endif
24
 
25
#if ELKS
26
#include <linuxmt/posix_types.h>
27
#include <linuxmt/time.h>
28
#endif
29
 
30
#include "windows.h"
31
#include "wintern.h"
32
#include "device.h"
33
/*
34
 * External definitions defined here.
35
 */
36
HWND            listwp;                 /* list of all windows */
37
HWND            rootwp;                 /* root window pointer */
38
HWND            focuswp;                /* focus window for keyboard */
39
HWND            mousewp;                /* window mouse is currently in */
40
HWND            capturewp;              /* capture window*/
41
HWND            dragwp;                 /* window user is dragging*/
42
HCURSOR         curcursor;              /* currently enabled cursor */
43
MWCOORD         cursorx;                /* current x position of cursor */
44
MWCOORD         cursory;                /* current y position of cursor */
45
DWORD           startTicks;             /* tickcount on startup */
46
int             keyb_fd;                /* the keyboard file descriptor */
47
int             mouse_fd;               /* the mouse file descriptor */
48
int             escape_quits = 1;       /* terminate when pressing ESC */
49
 
50
int
51
main(int ac,char **av)
52
{
53
        /* call user hook routine before anything*/
54
        if(MwUserInit(ac, av) < 0)
55
                exit(1);
56
 
57
        if(MwOpen() < 0)
58
                exit(1);
59
 
60
        /* call windows main program entry point*/
61
        WinMain(NULL, NULL, NULL, SW_SHOW);
62
 
63
        MwClose();
64
        return 0;
65
}
66
 
67
/*
68
 * Open a connection from a new client to the server.
69
 * Returns -1 on failure.
70
 */
71
int
72
MwOpen(void)
73
{
74
        /* Client calls this routine once.  We
75
         * init everything here
76
         */
77
        if(MwInitialize() < 0)
78
                return -1;
79
        return 1;
80
}
81
 
82
/*
83
 * Close the connection to the server.
84
 */
85
void
86
MwClose(void)
87
{
88
        MwTerminate();
89
}
90
 
91
#if (UNIX | DOS_DJGPP) && !_MINIX
92
/*
93
 * Support for more than one user fd.
94
 * Chris Johns (ccj@acm.org)
95
 *
96
 * Register the specified file descriptor to post
97
 * WM_FDINPUT/WM_FDOUTPUT/WM_FDEXCEPT to the passed hwnd
98
 * when input/output/except is ready.
99
 *
100
 * Allow for any fd to be selected on.
101
 *
102
 * The user fd's are listed and scanned helping keep the
103
 * overhead down for a large list of fd's being selected on.
104
 */
105
 
106
typedef struct {
107
        HWND read;
108
        HWND write;
109
        HWND except;
110
        int  next;
111
} WNDUSERFD;
112
 
113
static WNDUSERFD userregfd[FD_SETSIZE];
114
static int       userregfd_head;
115
 
116
void
117
MwRegisterFdInput(HWND hwnd, int fd)
118
{
119
        if (fd < FD_SETSIZE && fd != mouse_fd && fd != keyb_fd) {
120
                if (!userregfd[fd].read) {
121
                        userregfd[fd].read = hwnd;
122
                        if (userregfd[fd].next == -1 && !userregfd[fd].write && !userregfd[fd].except) {
123
                                userregfd[fd].next = userregfd_head;
124
                                userregfd_head = fd;
125
                        }
126
                }
127
        }
128
}
129
 
130
void
131
MwUnregisterFdInput(HWND hwnd, int fd)
132
{
133
        if (fd < FD_SETSIZE && fd != mouse_fd && fd != keyb_fd) {
134
                if (userregfd[fd].read == hwnd) {
135
                        userregfd[fd].read = NULL;
136
                        if (!userregfd[fd].write && !userregfd[fd].except) {
137
                                int *listfd = &userregfd_head;
138
                                while (*listfd != -1) {
139
                                        if (*listfd == fd) {
140
                                                *listfd = userregfd[fd].next;
141
                                                userregfd[fd].next = -1;
142
                                                return;
143
                                        }
144
                                        listfd = &userregfd[*listfd].next;
145
                                }
146
                                userregfd_head = fd;
147
                        }
148
                }
149
        }
150
}
151
 
152
void
153
MwRegisterFdOutput(HWND hwnd, int fd)
154
{
155
        if (fd < FD_SETSIZE && fd != mouse_fd && fd != keyb_fd) {
156
                if (!userregfd[fd].write) {
157
                        userregfd[fd].write = hwnd;
158
                        if (userregfd[fd].next == -1 && !userregfd[fd].read && !userregfd[fd].except) {
159
                                userregfd[fd].next = userregfd_head;
160
                                userregfd_head = fd;
161
                        }
162
                }
163
        }
164
}
165
 
166
void
167
MwUnregisterFdOutput(HWND hwnd, int fd)
168
{
169
        if (fd < FD_SETSIZE && fd != mouse_fd && fd != keyb_fd) {
170
                if (userregfd[fd].write == hwnd) {
171
                        userregfd[fd].write = NULL;
172
                        if (!userregfd[fd].read && !userregfd[fd].except) {
173
                                int *listfd = &userregfd_head;
174
                                while (*listfd != -1) {
175
                                        if (*listfd == fd) {
176
                                                *listfd = userregfd[fd].next;
177
                                                userregfd[fd].next = -1;
178
                                                return;
179
                                        }
180
                                        listfd = &userregfd[*listfd].next;
181
                                }
182
                                userregfd_head = fd;
183
                        }
184
                }
185
        }
186
}
187
 
188
void
189
MwRegisterFdExcept(HWND hwnd, int fd)
190
{
191
        if (fd < FD_SETSIZE && fd != mouse_fd && fd != keyb_fd) {
192
                if (!userregfd[fd].except) {
193
                        userregfd[fd].except = hwnd;
194
                        if (userregfd[fd].next == -1 && !userregfd[fd].read && !userregfd[fd].write) {
195
                                userregfd[fd].next = userregfd_head;
196
                                userregfd_head = fd;
197
                        }
198
                }
199
        }
200
}
201
 
202
void
203
MwUnregisterFdExcept(HWND hwnd, int fd)
204
{
205
        if (fd < FD_SETSIZE && fd != mouse_fd && fd != keyb_fd) {
206
                if (userregfd[fd].except == hwnd) {
207
                        userregfd[fd].except = NULL;
208
                        if (!userregfd[fd].read && !userregfd[fd].write) {
209
                                int *listfd = &userregfd_head;
210
                                while (*listfd != -1) {
211
                                        if (*listfd == fd) {
212
                                                *listfd = userregfd[fd].next;
213
                                                userregfd[fd].next = -1;
214
                                                return;
215
                                        }
216
                                        listfd = &userregfd[*listfd].next;
217
                                }
218
                                userregfd_head = fd;
219
                        }
220
                }
221
        }
222
}
223
 
224
#endif /* UNIX | DOS_DJGPP*/
225
 
226
#if MSDOS | _MINIX
227
void
228
MwSelect(void)
229
{
230
        /* If mouse data present, service it*/
231
        if(mousedev.Poll())
232
                while(MwCheckMouseEvent())
233
                        continue;
234
 
235
        /* If keyboard data present, service it*/
236
        if(kbddev.Poll())
237
                while(MwCheckKeyboardEvent())
238
                        continue;
239
 
240
        MwHandleTimers();
241
}
242
#endif
243
 
244
#if UNIX && defined(HAVESELECT)
245
#if ANIMATEPALETTE
246
static int fade = 0;
247
#endif
248
 
249
void
250
MwSelect(void)
251
{
252
        fd_set  rfds;
253
        fd_set  wfds;
254
        fd_set  efds;
255
        int     fd;
256
        int     e;
257
        int     setsize = 0;
258
        UINT    timeout;
259
        struct timeval to;
260
 
261
        /* perform pre-select duties, if any*/
262
        if(scrdev.PreSelect)
263
                scrdev.PreSelect(&scrdev);
264
 
265
        /* Set up the FDs for use in the main select(): */
266
        FD_ZERO(&rfds);
267
        FD_ZERO(&wfds);
268
        FD_ZERO(&efds);
269
 
270
        if(mouse_fd >= 0) {
271
                FD_SET(mouse_fd, &rfds);
272
                if(mouse_fd > setsize)
273
                        setsize = mouse_fd;
274
        }
275
        if(keyb_fd >= 0) {
276
                FD_SET(keyb_fd, &rfds);
277
                if(keyb_fd > setsize)
278
                        setsize = keyb_fd;
279
        }
280
 
281
        /* handle registered file descriptors */
282
        fd = userregfd_head;
283
        while (fd != -1) {
284
                if (userregfd[fd].read) FD_SET(fd, &rfds);
285
                if (userregfd[fd].write) FD_SET(fd, &wfds);
286
                if (userregfd[fd].except) FD_SET(fd, &efds);
287
                if(fd > setsize) setsize = fd;
288
                fd = userregfd[fd].next;
289
        }
290
 
291
        ++setsize;
292
 
293
        /* Set up the timeout for the main select().  If
294
         * the mouse is captured we're probably moving a window,
295
         * so poll quickly to allow other windows to repaint while
296
         * checking for more event input.
297
         */
298
        if(dragwp)
299
                timeout = to.tv_sec = to.tv_usec = 0L;
300
        else {
301
                timeout = MwGetNextTimeoutValue();      /* returns ms*/
302
#if ANIMATEPALETTE
303
                if(fade < 100)
304
                        timeout = 40;
305
#endif
306
if (!timeout) timeout = 10;     /* temp kluge required for mdemo to run ok*/
307
                GdGetNextTimeout(&to, timeout);
308
        }
309
 
310
        /* Wait for some input on any of the fds in the set or a timeout: */
311
        if((e = select(setsize, &rfds, &wfds, &efds, &to)) > 0) {
312
 
313
                /* If data is present on the mouse fd, service it: */
314
                if(mouse_fd >= 0 && FD_ISSET(mouse_fd, &rfds))
315
                        while(MwCheckMouseEvent())
316
                                continue;
317
 
318
                /* If data is present on the keyboard fd, service it: */
319
                if(keyb_fd >= 0 && FD_ISSET(keyb_fd, &rfds))
320
                        while(MwCheckKeyboardEvent())
321
                                continue;
322
 
323
                /* If registered descriptor, handle it */
324
                fd = userregfd_head;
325
                while (fd != -1) {
326
                        if (userregfd[fd].read && FD_ISSET(fd, &rfds))
327
                                PostMessage(userregfd[fd].read, WM_FDINPUT, fd, 0);
328
                        if (userregfd[fd].write && FD_ISSET(fd, &wfds))
329
                                PostMessage(userregfd[fd].write, WM_FDOUTPUT, fd, 0);
330
                        if (userregfd[fd].except && FD_ISSET(fd, &efds))
331
                                PostMessage(userregfd[fd].except, WM_FDEXCEPT, fd, 0);
332
                        fd = userregfd[fd].next;
333
                }
334
        }
335
        else if(e == 0) {
336
                /* timeout has occured*/
337
                if(GdTimeout() == FALSE)
338
                        return;
339
#if ANIMATEPALETTE
340
                if(fade <= 100) {
341
                        setfadelevel(&scrdev, fade);
342
                        fade += 5;
343
                }
344
#endif
345
                MwHandleTimers();
346
        } else
347
                if(errno != EINTR)
348
                        EPRINTF("Select() call in main failed\n");
349
}
350
#endif
351
 
352
#if VTSWITCH
353
static void
354
CheckVtChange(void *arg)
355
{
356
        if(MwCheckVtChange()) {
357
#if ANIMATEPALETTE
358
                fade = 0;
359
#endif
360
                MwRedrawScreen();
361
        }
362
        GdAddTimer(50, CheckVtChange, NULL);
363
}
364
#endif
365
 
366
/*
367
 * Initialize the graphics and mouse devices at startup.
368
 * Returns nonzero with a message printed if the initialization failed.
369
 */
370
int
371
MwInitialize(void)
372
{
373
        HWND            wp;             /* root window */
374
        PSD             psd;
375
        WNDCLASS        wc;
376
        int             fd;
377
        static MWCURSOR arrow = {       /* default arrow cursor*/
378
                16, 16,
379
                0,  0,
380
                RGB(255, 255, 255), RGB(0, 0, 0),
381
                { 0xe000, 0x9800, 0x8600, 0x4180,
382
                  0x4060, 0x2018, 0x2004, 0x107c,
383
                  0x1020, 0x0910, 0x0988, 0x0544,
384
                  0x0522, 0x0211, 0x000a, 0x0004 },
385
                { 0xe000, 0xf800, 0xfe00, 0x7f80,
386
                  0x7fe0, 0x3ff8, 0x3ffc, 0x1ffc,
387
                  0x1fe0, 0x0ff0, 0x0ff8, 0x077c,
388
                  0x073e, 0x021f, 0x000e, 0x0004 }
389
        };
390
 
391
        extern MWLISTHEAD mwClassHead;
392
 
393
#if (UNIX | DOS_DJGPP) && !_MINIX
394
        for (fd = 0; fd < FD_SETSIZE; fd++) {
395
                userregfd[fd].read = NULL;
396
                userregfd[fd].write = NULL;
397
                userregfd[fd].except = NULL;
398
                userregfd[fd].next = -1;
399
        }
400
        userregfd_head = -1;
401
#endif
402
        /* catch terminate signal to restore tty state*/
403
        signal(SIGTERM, (void *)MwTerminate);
404
 
405
        startTicks = GetTickCount();
406
 
407
        if ((keyb_fd = GdOpenKeyboard()) == -1) {
408
                EPRINTF("Cannot initialise keyboard\n");
409
                return -1;
410
        }
411
 
412
        if ((psd = GdOpenScreen()) == NULL) {
413
                EPRINTF("Cannot initialise screen\n");
414
                GdCloseKeyboard();
415
                return -1;
416
        }
417
 
418
        if ((mouse_fd = GdOpenMouse()) == -1) {
419
                EPRINTF("Cannot initialise mouse\n");
420
                GdCloseScreen(psd);
421
                GdCloseKeyboard();
422
                return -1;
423
        }
424
 
425
#if ANIMATEPALETTE
426
        setfadelevel(psd, 0);
427
#endif
428
        /*
429
         * Initialize the root window.
430
         */
431
        wc.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW;
432
        wc.lpfnWndProc = (WNDPROC)DefWindowProc;
433
        wc.cbClsExtra = 0;
434
        wc.cbWndExtra = 0;
435
        wc.hInstance = 0;
436
        wc.hIcon = 0; /*LoadIcon(GetHInstance(), MAKEINTRESOURCE( 1));*/
437
        wc.hCursor = 0; /*LoadCursor(NULL, IDC_ARROW);*/
438
        wc.hbrBackground = CreateSolidBrush(GetSysColor(COLOR_BACKGROUND));
439
        wc.lpszMenuName = NULL;
440
        wc.lpszClassName =  "DeskTop";
441
        RegisterClass( &wc);
442
 
443
        wp = GdItemNew(struct hwnd);
444
        if (!wp) {
445
                EPRINTF("No memory for root window\n");
446
                GdCloseMouse();
447
                GdCloseScreen(psd);
448
                GdCloseKeyboard();
449
                return -1;
450
        }
451
        /* remove the WS_CAPTION to have bare desktop window*/
452
        /*wp->style = WS_CLIPCHILDREN | WS_CAPTION | WS_VISIBLE;*/
453
        wp->style = WS_CLIPCHILDREN | WS_VISIBLE;
454
        wp->exstyle = 0;
455
        wp->pClass = (PWNDCLASS)mwClassHead.head;
456
        wp->parent = NULL;
457
        wp->children = NULL;
458
        wp->siblings = NULL;
459
        wp->next = NULL;
460
        SetRect(&wp->winrect, 0, 0, psd->xvirtres, psd->yvirtres);
461
        MwCalcClientRect(wp);
462
        wp->cursor = NULL;
463
        wp->unmapcount = 0;
464
        wp->id = 0;
465
        strcpy(wp->szTitle, "Microwindows");
466
        wp->gotPaintMsg = PAINT_PAINTED;
467
#if UPDATEREGIONS
468
        wp->update = GdAllocRegion();
469
#endif
470
 
471
        listwp = wp;
472
        rootwp = wp;
473
        focuswp = wp;
474
        mousewp = wp;
475
 
476
        /* schedule desktop window paint*/
477
        InvalidateRect(rootwp, NULL, TRUE);
478
 
479
#if VTSWITCH
480
        MwInitVt();
481
        /* Check for VT change every 50 ms: */
482
        GdAddTimer(50, CheckVtChange, NULL);
483
#endif
484
 
485
        /*
486
         * Initialize and position the default cursor.
487
         */
488
        curcursor = NULL;
489
        cursorx = -1;
490
        cursory = -1;
491
        GdShowCursor(psd);
492
        MwMoveCursor(psd->xvirtres / 2, psd->yvirtres / 2);
493
        MwSetCursor(rootwp, &arrow);
494
 
495
        /*
496
         * Finally tell the mouse driver some things.
497
         */
498
        GdRestrictMouse(0, 0, psd->xvirtres - 1, psd->yvirtres - 1);
499
        GdMoveMouse(psd->xvirtres / 2, psd->yvirtres / 2);
500
 
501
        return 0;
502
}
503
 
504
/*
505
 * Here to close down the server.
506
 */
507
void
508
MwTerminate(void)
509
{
510
        GdCloseScreen(&scrdev);
511
        GdCloseMouse();
512
        GdCloseKeyboard();
513
#if VTSWITCH
514
        MwRedrawVt(mwvterm);
515
        MwExitVt();
516
#endif
517
        exit(0);
518
}
519
 
520
/*
521
 * Return # milliseconds elapsed since start of Microwindows
522
 * Granularity is 25 msec
523
 */
524
DWORD WINAPI
525
GetTickCount(VOID)
526
{
527
#if MSDOS
528
#include <time.h>
529
        return (DWORD)(clock() * 1000 / CLOCKS_PER_SEC);
530
#else
531
#if _MINIX
532
        struct tms      t;
533
 
534
        return (DWORD)times(&t) * 16;
535
#else
536
#if UNIX
537
        struct timeval t;
538
 
539
        gettimeofday(&t, NULL);
540
        return ((t.tv_sec * 1000) + (t.tv_usec / 25000) * 25) - startTicks;
541
#else
542
        return 0L;
543
#endif
544
#endif
545
#endif
546
}
547
 
548
VOID WINAPI
549
Sleep(DWORD dwMilliseconds)
550
{
551
        int i, j, k;
552
        const int loops_per_ms = 20000;
553
 
554
        /* FIXME this is not calibrated */
555
        for(i=0; i < dwMilliseconds; i++)
556
                for(j=0; j < loops_per_ms; j++)
557
                        k = i * j;
558
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.