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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [services/] [gfx/] [mw/] [v2_0/] [src/] [demos/] [test/] [test.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
/*
2
 * Copyright (c) 1999 Greg Haerr <greg@censoft.com>
3
 *
4
 * Test for select()
5
 */
6
#include <stdio.h>
7
#include <stdlib.h>
8
 
9
#ifndef __PACIFIC__
10
#include <errno.h>
11
#include <sys/types.h>
12
#endif
13
 
14
#if UNIX
15
#include <unistd.h>
16
#include <sys/time.h>
17
#endif
18
#if ELKS
19
#include <linuxmt/posix_types.h>
20
#include <linuxmt/time.h>
21
#endif
22
#include "windows.h"
23
#include "wintern.h"
24
 
25
int             keyb_fd;                /* the keyboard file descriptor */
26
int             mouse_fd;               /* the mouse file descriptor */
27
 
28
int
29
main(int ac,char **av)
30
{
31
        if ((keyb_fd = GdOpenKeyboard()) < 0) {
32
                PRINT_ERROR("Cannot initialise keyboard");
33
                return -1;
34
        }
35
 
36
        if ((mouse_fd = GdOpenMouse()) < 0) {
37
                PRINT_ERROR("Cannot initialise mouse");
38
                GdCloseKeyboard();
39
                return -1;
40
        }
41
 
42
        for(;;)
43
                GsSelect();
44
 
45
        return 0;
46
}
47
 
48
void
49
GsTerminate(void)
50
{
51
        GdCloseMouse();
52
        GdCloseKeyboard();
53
        exit(0);
54
}
55
 
56
#if MSDOS
57
void
58
GsSelect(void)
59
{
60
        /* If mouse data present, service it*/
61
        if(mousedev.Poll())
62
                GsCheckMouseEvent();
63
 
64
        /* If keyboard data present, service it*/
65
        if(kbddev.Poll())
66
                GsCheckKeyboardEvent();
67
 
68
}
69
#endif
70
 
71
#if UNIX && defined(HAVESELECT)
72
void
73
GsSelect(void)
74
{
75
        fd_set  rfds;
76
        int     e;
77
        struct timeval to;
78
        int     n;
79
 
80
        /* Set up the FDs for use in the main select(): */
81
        FD_ZERO(&rfds);
82
        FD_SET(mouse_fd, &rfds);
83
        n = mouse_fd;
84
        FD_SET(keyb_fd, &rfds);
85
        if(keyb_fd > n)
86
                n = keyb_fd;
87
 
88
        /* Set up the timeout for the main select().  If
89
         * the mouse is captured we're probably moving a window,
90
         * so poll quickly to allow other windows to repaint while
91
         * checking for more event input.
92
         */
93
        to.tv_sec = 0L;
94
to.tv_sec = 1L;
95
        to.tv_usec = 0L;
96
to.tv_usec = 10000L;
97
 
98
        /* Wait for some input on any of the fds in the set or a timeout: */
99
        if((e = select(n+1, &rfds, NULL, NULL, &to)) > 0) {
100
 
101
                /* If data is present on the mouse fd, service it: */
102
                if(FD_ISSET(mouse_fd, &rfds))
103
                        GsCheckMouseEvent();
104
 
105
                /* If data is present on the keyboard fd, service it: */
106
                if(FD_ISSET(keyb_fd, &rfds))
107
                        GsCheckKeyboardEvent();
108
 
109
        }
110
        else if(!e) {
111
                printf("select() timeout\n");
112
        } else
113
                if(errno != EINTR)
114
                        PRINT_ERROR("Select() call in main failed");
115
}
116
#endif
117
 
118
/*
119
 * Update mouse status and issue events on it if necessary.
120
 * This function doesn't block, but is normally only called when
121
 * there is known to be some data waiting to be read from the mouse.
122
 */
123
BOOL GsCheckMouseEvent(void)
124
{
125
        COORD           rootx;          /* latest mouse x position */
126
        COORD           rooty;          /* latest mouse y position */
127
        BUTTON          newbuttons;     /* latest buttons */
128
        int             mousestatus;    /* latest mouse status */
129
 
130
        /* Read the latest mouse status: */
131
        mousestatus = GdReadMouse(&rootx, &rooty, &newbuttons);
132
        if(mousestatus < 0) {
133
                printf("Mouse error\n");
134
                return FALSE;
135
        } else if(mousestatus) { /* Deliver events as appropriate: */
136
                printf("mouse %d,%d,%d\n", rootx, rooty, newbuttons);
137
      return TRUE;
138
   }
139
   return FALSE;
140
}
141
 
142
/*
143
 * Update keyboard status and issue events on it if necessary.
144
 * This function doesn't block, but is normally only called when
145
 * there is known to be some data waiting to be read from the keyboard.
146
 */
147
BOOL GsCheckKeyboardEvent(void)
148
{
149
        unsigned char   ch;             /* latest character */
150
        MODIFIER        modifiers;      /* latest modifiers */
151
        int             keystatus;      /* latest keyboard status */
152
 
153
        /* Read the latest keyboard status: */
154
        keystatus = GdReadKeyboard(&ch, &modifiers);
155
        if(keystatus < 0) {
156
                if(keystatus == -2)     /* special case for ESC pressed*/
157
                        GsTerminate();
158
                printf("Kbd error\n");
159
                return FALSE;
160
        } else if(keystatus) { /* Deliver events as appropriate: */
161
                printf("kbd '%c',%d\n", ch, modifiers);
162
      return TRUE;
163
   }
164
   return FALSE;
165
}

powered by: WebSVN 2.1.0

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