Main Page   File List   Globals  

examples/mouse_ex.c

00001 /* To create executable use: $(CC) cb.c -o cb -lFB */
00002 #include "FBlib.h"
00003 #include "FBpriv.h"
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <unistd.h>
00007 #include <sys/poll.h>
00008 
00009 #define max(a,b) ((a) > (b) ? (a) : (b))
00010 #define min(a,b) ((a) < (b) ? (a) : (b))
00011 #define CLAMP(a,b,c) min(max((a),(b)),(c))
00012 
00013 int size = 2;
00014 int mouse_fd;
00015 int mouse_left;
00016 int mouse_right;
00017 int mouse_x = 50;
00018 int mouse_y = 50;
00019 int mouse_pix_x = 10;
00020 int mouse_pix_y = 10;
00021 static signed char data[4];
00022 FB_pixel mouse_area[11][11];
00023  
00024 void update_mouse(void)
00025 {
00026         static int count = 0;
00027         int i, changed = 0;
00028 
00029         while (1) {
00030                 /* This part of the code is specific to each mouse depending on 
00031                    how many bytes each puts out and how to interpret them...
00032                    If you don't have a PS/2 mouse this code will probably not 
00033                    work and you will need to experiment and change the code.
00034                 */
00035                 i = read(mouse_fd, data + count, 4 - count);
00036                 if (i <= 0) {
00037                         break;
00038                 }
00039                 count += i;
00040                 if (count == 4) {
00041                         count = 0;
00042                         changed = 1;
00043                         mouse_left  = data[0] & 1;
00044                         mouse_right = (data[0] & 2) >> 1;
00045                         mouse_x += (int) data[1];
00046                         mouse_y -= (int) data[2];
00047                 }
00048         }
00049         if (changed) {
00050                 mouse_x = CLAMP (mouse_x, mouse_pix_x + 2 * size, work_var_info.xres - mouse_pix_x - 1);
00051                 mouse_y = CLAMP (mouse_y, mouse_pix_y + 2 * size, work_var_info.yres - mouse_pix_y - 1);
00052         }
00053 }
00054 
00055 void under_mouse ()
00056 {
00057         int i, j;
00058         
00059         for (i = 0; i < 11; i++) {
00060                 for (j = 0; j < 11; j++) {
00061                         mouse_area[i][j] = FB_getpixel (i + mouse_x, j + mouse_y);
00062                 }
00063         }
00064 }
00065 
00066 void over_mouse ()
00067 {
00068         int i, j;
00069         
00070         for (i = 0; i < 11; i++) {
00071                 for (j = 0; j < 11; j++) {
00072                         FB_putpixel (i + mouse_x, j + mouse_y, mouse_area[i][j]);
00073                 }
00074         }
00075 }
00076 
00077 int main (int argc, char ** argv)
00078 {
00079         struct pollfd p_mouse;
00080         FB_pixel bg, red, green, wierd;
00081         
00082         printf ("Move the mouse around and press left click to draw.\n");
00083         printf ("To exit press right click. Have fun!\n");
00084         getchar ();
00085 
00086         if (argc == 2) size = atoi (argv[1]) + 1;
00087         printf ("Initializing Mouse... ");
00088         mouse_fd = open ("/dev/mouse", O_RDONLY | O_NONBLOCK);
00089         if (mouse_fd < 0)
00090         {
00091                 perror ("/dev/mouse");
00092         }
00093         p_mouse.fd = mouse_fd;
00094         p_mouse.events = POLLIN;
00095         printf ("[done]\n");
00096                 
00097         printf ("initializing libFB... ");
00098         FB_initlib("/dev/fb0");
00099         bg = FB_makecol (0,0,0,0);
00100         red = FB_makecol (255,0,0,10);
00101         green = FB_makecol (0,255,0,0);
00102         wierd = FB_makecol (245, 0, 128, 0);
00103         printf ("[done]\n");
00104 
00105         FB_clear_screen(bg);
00106         under_mouse ();
00107 
00108         while (1)
00109         {
00110                 poll (&p_mouse, 1, 0);
00111                 if (p_mouse.revents & POLLIN)
00112                 {
00113                         over_mouse ();
00114                         update_mouse ();
00115                         if (mouse_left) 
00116                         {
00117                                 FB_hline (mouse_x - 2 * size, mouse_x - size, mouse_y, red);
00118                                 FB_vline (mouse_x - 2 * size, mouse_y - 2 * size, mouse_y, wierd);
00119                                 FB_hline (mouse_x - 2 * size, mouse_x - size, mouse_y, red);
00120                                 FB_vline (mouse_x - size, mouse_y - 2 * size, mouse_y, wierd);
00121                         }
00122                         under_mouse ();
00123                         FB_vline (mouse_x + 5, mouse_y, mouse_y + 10, green);
00124                         FB_hline (mouse_x, mouse_x + 10, mouse_y + 5, green);
00125                         if (mouse_right) goto exit;
00126                 }
00127         }
00128 exit:
00129         printf ("Exit.\n");
00130         close (mouse_fd);
00131         FB_exit();
00132         return 0;
00133 }

Generated on Sun Apr 14 12:59:01 2002 for LibFB by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002