42842de0959e2a00b23524ac023fad92b44d5bdb
[freeglut] / progs / demos / multi-touch / multi-touch.c
1 /**
2  * Sample multi-touch program that displays a square where a cursor
3  * clicks, with a different color for each cursor.
4  *
5  * Copyright (C) 2012  Sylvain Beucler
6  * 
7  * Permission is hereby granted, free of charge, to any person
8  * obtaining a copy of this software and associated documentation
9  * files (the "Software"), to deal in the Software without
10  * restriction, including without limitation the rights to use, copy,
11  * modify, merge, publish, distribute, sublicense, and/or sell copies
12  * of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <GL/freeglut.h>
33 #include <GL/gl.h>
34
35 #define NUM_DEVICES 16
36 #define NUM_CURSORS 64
37 typedef struct cursor {
38     char on;
39     float x;
40     float y;
41 } *Cursor;
42 struct cursor cursors[NUM_DEVICES][NUM_CURSORS];
43
44 void onDisplay() {
45     glClearColor(0,0,0,1);
46     glClear(GL_COLOR_BUFFER_BIT);
47
48     float square[] = {
49         -.5, -.5,
50          .5, -.5,
51         -.5,  .5,
52          .5,  .5,
53     };
54
55     glEnableClientState(GL_VERTEX_ARRAY);
56     glVertexPointer(2, GL_FLOAT, 0, square);
57     int d;
58     for (d = 0; d < NUM_DEVICES; d++) {
59         int c;
60         for (c = 0; d < NUM_DEVICES; d++) {
61             Cursor C = &cursors[d][c];
62             if (C->on) {
63                 glMatrixMode(GL_MODELVIEW);
64                 glLoadIdentity();
65                 glTranslatef(C->x, C->y, 0);
66                 glScalef(30, 30, 1);
67                 
68                 switch(c) {
69                 case 0:
70                     glColor4f(0,0,1,1);
71                     break;
72                 case 1:
73                     glColor4f(0,1,0,1);
74                     break;
75                 case 2:
76                     glColor4f(1,0,0,1);
77                     break;
78                 case 3:
79                     glColor4f(1,1,1,1);
80                     break;
81                 default:
82                     glColor4d(.5,.5,.5,1);
83                     break;
84                 }
85                 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
86             }
87         }
88     }
89     glDisableClientState(GL_VERTEX_ARRAY);
90
91     glutSwapBuffers();
92 }
93
94 void onMouse(int button, int state, int x, int y) {
95     if (button == 0) {
96         cursors[0][0].on = (state == GLUT_DOWN);
97         cursors[0][0].x = x;
98         cursors[0][0].y = y;
99         printf("normal click\n");
100     }
101 }
102
103 void onMotion(int x, int y) {
104     cursors[0][0].x = x;
105     cursors[0][0].y = y;
106 }
107
108 /* Using FG2.8 (reversed) prototype for now */
109 /* void onMultiButton(int cursor_id, int button, int state, int x, int y) { */
110 void onMultiButton(int cursor_id, int x, int y, int button, int state) {
111     if (cursor_id > NUM_CURSORS) {
112         fprintf(stderr, "cursor_id(%d) > NUM_CURSORS(%d)\n", cursor_id, NUM_CURSORS);
113         return;
114     }
115     if (button == 0) {
116         cursors[0][cursor_id].on = (state == GLUT_DOWN);
117         cursors[0][cursor_id].x = x;
118         cursors[0][cursor_id].y = y;
119         printf("multi-touch %d click\n", cursor_id);
120     }
121 }
122
123 void onMultiMotion(int cursor_id, int x, int y) {
124     if (cursor_id > NUM_CURSORS) {
125         fprintf(stderr, "cursor_id(%d) > NUM_CURSORS(%d)\n", cursor_id, NUM_CURSORS);
126         return;
127     }
128     cursors[0][cursor_id].x = x;
129     cursors[0][cursor_id].y = y;
130 }
131
132 void onReshape(int width, int height) {
133     glViewport(0, 0, width, height);
134     
135     glMatrixMode(GL_PROJECTION);
136     glOrtho(0, width, height, 0, -1, 1);
137 }
138
139 void onIdle() {
140     glutPostRedisplay();
141 }
142
143 int main(int argc, char* argv[]) {
144     memset(cursors, 0, sizeof(cursors));
145
146     glutInit(&argc, argv);
147     glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
148     glutInitWindowSize(640, 480);
149     glutCreateWindow("Multi-touch test");
150
151     glutDisplayFunc(onDisplay);
152     glutReshapeFunc(onReshape);
153     glutIdleFunc(onIdle);
154     glutMouseFunc(onMouse);
155     glutMotionFunc(onMotion);
156     glutMultiButtonFunc(onMultiButton);
157     glutMultiMotionFunc(onMultiMotion);
158
159     glutMainLoop();
160
161     return EXIT_SUCCESS;
162 }