multi-touch.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <GL/freeglut.h>
  31. #include <GL/gl.h>
  32. #define NUM_DEVICES 16
  33. #define NUM_CURSORS 64
  34. typedef struct cursor {
  35. char on;
  36. float x;
  37. float y;
  38. } *Cursor;
  39. struct cursor cursors[NUM_DEVICES][NUM_CURSORS];
  40. static float square[] = {
  41. -.5, -.5,
  42. .5, -.5,
  43. -.5, .5,
  44. .5, .5,
  45. };
  46. void onDisplay(void) {
  47. int d;
  48. glClearColor(0,0,0,1);
  49. glClear(GL_COLOR_BUFFER_BIT);
  50. glEnableClientState(GL_VERTEX_ARRAY);
  51. glVertexPointer(2, GL_FLOAT, 0, square);
  52. for (d = 0; d < NUM_DEVICES; d++) {
  53. int c;
  54. for (c = 0; d < NUM_DEVICES; d++) {
  55. Cursor C = &cursors[d][c];
  56. if (C->on) {
  57. glMatrixMode(GL_MODELVIEW);
  58. glLoadIdentity();
  59. glTranslatef(C->x, C->y, 0);
  60. glScalef(30, 30, 1);
  61. switch(c) {
  62. case 0:
  63. glColor4f(0,0,1,1);
  64. break;
  65. case 1:
  66. glColor4f(0,1,0,1);
  67. break;
  68. case 2:
  69. glColor4f(1,0,0,1);
  70. break;
  71. case 3:
  72. glColor4f(1,1,1,1);
  73. break;
  74. default:
  75. glColor4d(.5,.5,.5,1);
  76. break;
  77. }
  78. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  79. }
  80. }
  81. }
  82. glDisableClientState(GL_VERTEX_ARRAY);
  83. glutSwapBuffers();
  84. }
  85. void onMouse(int button, int state, int x, int y) {
  86. if (button == 0) {
  87. cursors[0][0].on = (state == GLUT_DOWN);
  88. cursors[0][0].x = (float)x;
  89. cursors[0][0].y = (float)y;
  90. printf("normal click\n");
  91. }
  92. }
  93. void onMotion(int x, int y) {
  94. cursors[0][0].x = (float)x;
  95. cursors[0][0].y = (float)y;
  96. }
  97. /* Using FG2.8 (reversed) prototype for now */
  98. /* void onMultiButton(int cursor_id, int button, int state, int x, int y) { */
  99. void onMultiButton(int cursor_id, int x, int y, int button, int state) {
  100. if (cursor_id > NUM_CURSORS) {
  101. fprintf(stderr, "cursor_id(%d) > NUM_CURSORS(%d)\n", cursor_id, NUM_CURSORS);
  102. return;
  103. }
  104. if (button == 0) {
  105. cursors[0][cursor_id].on = (state == GLUT_DOWN);
  106. cursors[0][cursor_id].x = (float)x;
  107. cursors[0][cursor_id].y = (float)y;
  108. printf("multi-touch %d click\n", cursor_id);
  109. }
  110. }
  111. void onMultiMotion(int cursor_id, int x, int y) {
  112. if (cursor_id > NUM_CURSORS) {
  113. fprintf(stderr, "cursor_id(%d) > NUM_CURSORS(%d)\n", cursor_id, NUM_CURSORS);
  114. return;
  115. }
  116. cursors[0][cursor_id].x = (float)x;
  117. cursors[0][cursor_id].y = (float)y;
  118. }
  119. void onReshape(int width, int height) {
  120. glViewport(0, 0, width, height);
  121. glMatrixMode(GL_PROJECTION);
  122. glOrtho(0, width, height, 0, -1, 1);
  123. }
  124. void onIdle(void) {
  125. glutPostRedisplay();
  126. }
  127. int main(int argc, char* argv[]) {
  128. memset(cursors, 0, sizeof(cursors));
  129. glutInit(&argc, argv);
  130. glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
  131. glutInitWindowSize(640, 480);
  132. glutCreateWindow("Multi-touch test");
  133. glutDisplayFunc(onDisplay);
  134. glutReshapeFunc(onReshape);
  135. glutIdleFunc(onIdle);
  136. glutMouseFunc(onMouse);
  137. glutMotionFunc(onMotion);
  138. glutMultiButtonFunc(onMultiButton);
  139. glutMultiMotionFunc(onMultiMotion);
  140. glutMainLoop();
  141. return EXIT_SUCCESS;
  142. }