Main Page   File List   Globals  

examples/mouse.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 data_size;
00016 int mouse_left;
00017 int mouse_right;
00018 int mouse_x = 50;
00019 int mouse_y = 50;
00020 int mouse_pix_x = 10;
00021 int mouse_pix_y = 10;
00022 static signed char data[4];
00023 FB_pixel mouse_area[11][11];
00024  
00025 void update_mouse(void)
00026 {
00027         static int count = 0;
00028         int i, changed = 0;
00029 
00030         while (1) {
00031                 i = read(mouse_fd, data + count, data_size - count);
00032                 if (i <= 0) {
00033                         break;
00034                 }
00035                 count += i;
00036                 if (count == data_size) {
00037                         count = 0;
00038                         changed = 1;
00039                         mouse_left  = data[0] & 1;
00040                         mouse_right = (data[0] & 2) >> 1;
00041                         mouse_x += (int) data[1];
00042                         mouse_y -= (int) data[2];
00043                 }
00044         }
00045         if (changed) {
00046                 mouse_x = CLAMP (mouse_x, mouse_pix_x + 2 * size, work_var_info.xres - mouse_pix_x - 1);
00047                 mouse_y = CLAMP (mouse_y, mouse_pix_y + 2 * size, work_var_info.yres - mouse_pix_y - 1);
00048         }
00049 }
00050 
00051 void under_mouse ()
00052 {
00053         int i, j;
00054         
00055         for (i = 0; i < 11; i++) {
00056                 for (j = 0; j < 11; j++) {
00057                         mouse_area[i][j] = FB_getpixel (i + mouse_x, j + mouse_y);
00058                 }
00059         }
00060 }
00061 
00062 void over_mouse ()
00063 {
00064         int i, j;
00065         
00066         for (i = 0; i < 11; i++) {
00067                 for (j = 0; j < 11; j++) {
00068                         FB_putpixel (i + mouse_x, j + mouse_y, mouse_area[i][j]);
00069                 }
00070         }
00071 }
00072 
00073 int main (int argc, char ** argv)
00074 {
00075         struct pollfd p_mouse;
00076         FB_pixel bg, red, green, wierd;
00077         
00078         if (argc != 3) {
00079                 printf ("Usage: %s -(type) size\n", argv[0]);
00080                 printf ("Where (type) can so far be one of the following:\n");
00081                 printf ("imps2 - for imps/2 mouse.\n");
00082                 printf ("ps2 - for ps/2 mouse.\n");
00083                 exit (0);
00084         }
00085 
00086         /*  This is so it will work for either IMPS/2 or PS/2. The only difference is in
00087          *  the size of the data returned by the mouse. Use -i for imps/2, -p for ps/2.
00088          *  Hopefully in the future I will be able to make it work for more kind of mice.
00089          */
00090 
00091         if (!strcmp ("-imps2", argv[1])) data_size = 4;
00092         else if (!strcmp ("-ps2", argv[1])) data_size = 3;
00093         else {
00094                 printf ("Unknown mouse type %s.\n", argv[1]);
00095                 exit (0);
00096         };
00097 
00098         size = atoi (argv[2]) + 1;
00099         
00100         printf ("Move the mouse around and press left click to draw.\n");
00101         printf ("To exit press right click. Have fun!\n");
00102         getchar ();
00103 
00104         printf ("Initializing Mouse... ");
00105         mouse_fd = open ("/dev/mouse", O_RDONLY | O_NONBLOCK);
00106         if (mouse_fd < 0)
00107         {
00108                 perror ("/dev/mouse");
00109         }
00110         p_mouse.fd = mouse_fd;
00111         p_mouse.events = POLLIN;
00112         printf ("[done]\n");
00113                 
00114         printf ("initializing libFB... ");
00115         FB_initlib("/dev/fb0");
00116         bg = FB_makecol (0,0,0,0);
00117         red = FB_makecol (255,0,0,10);
00118         green = FB_makecol (0,255,0,0);
00119         wierd = FB_makecol (245, 0, 128, 0);
00120 
00121         FB_clear_screen (bg);
00122         under_mouse ();
00123 
00124         while (1)
00125         {
00126                 poll (&p_mouse, 1, 0);
00127                 if (p_mouse.revents & POLLIN)
00128                 {
00129                         over_mouse ();
00130                         update_mouse ();
00131                         if (mouse_left) 
00132                         {
00133                                 FB_hline (mouse_x - 2 * size, mouse_x - size, mouse_y, red);
00134                                 FB_vline (mouse_x - 2 * size, mouse_y - 2 * size, mouse_y, wierd);
00135                                 FB_hline (mouse_x - 2 * size, mouse_x - size, mouse_y, red);
00136                                 FB_vline (mouse_x - size, mouse_y - 2 * size, mouse_y, wierd);
00137                         }
00138                         under_mouse ();
00139                         FB_vline (mouse_x + 5, mouse_y, mouse_y + 10, green);
00140                         FB_hline (mouse_x, mouse_x + 10, mouse_y + 5, green);
00141                         if (mouse_right) {
00142                                 FB_clear_screen (bg);
00143                                 goto exit;
00144                         }
00145                 }
00146         }
00147 exit:
00148         close (mouse_fd);
00149         FB_exit();
00150         printf ("Exit.\n");
00151         return 0;
00152 }

Generated on Sat Jul 5 09:04:51 2003 for LibFB by doxygen1.2.18