Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /* file: hrtview.c
  2  *
  3  * demonstration/test program for the hrt frame grabber driver
  4  * simplified
  5  *
  6  */
  7 
  8 #include <SDL/SDL.h>
  9 #include <SDL/SDL_keysym.h>
 10 #include <stdio.h>
 11 #include <fcntl.h>
 12 #include <unistd.h>
 13 #include <string.h>
 14 #include <sys/mman.h>
 15 #include <asm/page.h>
 16 #include <sys/time.h>
 17 #include "../hrt.h"
 18 
 19 #define HRT_DEVICE "/dev/hrt0"
 20 #define HRT_DEFAULT_X 0
 21 #define HRT_DEFAULT_Y 0
 22 #define HRT_DEFAULT_WIDTH 256
 23 #define HRT_DEFAULT_HEIGHT 240
 24 #define HRT_DEFAULT_DEPTH  8
 25 
 26 typedef struct _hrtview_t {
 27         char *hrt_device;               /* path name to the HRT device */
 28         int   fd;                       /* file descriptor to the open HRT device */
 29         int   width, height, depth;     /* sdl info */
 30         int   flags;                    /* for the display */
 31         SDL_Surface *surface;           /* the main window surface */
 32         int   frame_count;
 33         int   argc;
 34         char **argv;
 35         int   paused;
 36         int   interrupts;
 37         int   x, y;
 38         int   dragging;
 39         int   start_x, start_y;
 40         int   cur_x, cur_y;     /* mouse stuff */
 41 } hrtview_t;
 42 static hrtview_t hrtview = { 0 };
 43 
 44 const char usage[] = "usage: hrtview [devicefilename]";
 45 
 46 int init_params(int argc, char **argv)
 47 {
 48         hrtview.argc   = argc;
 49         hrtview.argv   = argv;
 50         hrtview.width  = HRT_DEFAULT_WIDTH;
 51         hrtview.height = HRT_DEFAULT_HEIGHT;
 52         hrtview.depth  = HRT_DEFAULT_DEPTH;
 53         hrtview.x      = HRT_DEFAULT_X;
 54         hrtview.y      = HRT_DEFAULT_Y;
 55         hrtview.flags  = SDL_SWSURFACE | SDL_DOUBLEBUF;
 56         hrtview.interrupts = 1;
 57         hrtview.dragging = 0;
 58         if (argc > 2) return -1;
 59         if (argc == 2) hrtview.hrt_device = argv[1];
 60         else hrtview.hrt_device = (char *)strdup(HRT_DEVICE);
 61         return 0;
 62 }
 63 
 64 int init_device()
 65 {
 66         hrtview.fd = -1;
 67         if ((hrtview.fd = open(hrtview.hrt_device, O_RDONLY)) < 0) {
 68                 printf("unable to open %s\n", hrtview.hrt_device);
 69                 hrtview.fd = -1;
 70                 return -1;
 71         }
 72         return 0;
 73 }
 74 
 75 int init_view()
 76 {
 77         SDL_Color colors[256];
 78         int i;
 79         if (SDL_Init(SDL_INIT_VIDEO)) {
 80                 printf("Unable to initialize SDL\n");
 81                 return -1;
 82         }
 83         if ((hrtview.surface = 
 84              SDL_SetVideoMode(hrtview.width, hrtview.height, hrtview.depth, hrtview.flags)) == NULL) {
 85                 printf("Unable to obtain video mode %dx%dx%d\n",
 86                         hrtview.width, hrtview.height, hrtview.depth);
 87                 return -1;
 88         }
 89         /* set the driver's window */
 90         ioctl(hrtview.fd, IOC_HRT_WIN_SET_X, &hrtview.y);
 91         ioctl(hrtview.fd, IOC_HRT_WIN_SET_Y, &hrtview.x);
 92         printf("setting width %d\n", hrtview.width);
 93         ioctl(hrtview.fd, IOC_HRT_WIN_SET_WIDTH, &hrtview.width);
 94         ioctl(hrtview.fd, IOC_HRT_WIN_SET_HEIGHT, &hrtview.height);
 95         /* Intialize the B&W palette */
 96         for (i = 0; i < 256; i++) {
 97                 colors[i].r = i;        
 98                 colors[i].g = i;        
 99                 colors[i].b = i;        
100         }
101         SDL_SetColors(hrtview.surface, colors, 0, 256);
102         SDL_WM_SetCaption("hrtview", "hrtview");
103 
104         return 0;
105 }
106 
107 void close_view()
108 {
109         if (hrtview.surface) SDL_FreeSurface(hrtview.surface);
110         hrtview.surface = (SDL_Surface *)NULL;
111         if (SDL_WasInit(SDL_INIT_VIDEO)) 
112                 SDL_Quit();
113 }
114 
115 void close_device()
116 {
117 
118         if (hrtview.fd >= 0) close(hrtview.fd);
119         hrtview.fd = -1;
120 }
121 
122 void draw_invert_rect()
123 {
124         int x, y;
125 
126         if (!hrtview.dragging) return;
127         
128         y = hrtview.start_y;
129         for (x = hrtview.start_x; x < hrtview.cur_x; x++) 
130                 ((unsigned char *)hrtview.surface->pixels)[y*(hrtview.width*(hrtview.depth/8))+x] =
131                         ~((unsigned char *)hrtview.surface->pixels)[y*(hrtview.width*(hrtview.depth/8))+x];
132         y = hrtview.cur_y;
133         for (x = hrtview.start_x; x < hrtview.cur_x; x++) 
134                 ((unsigned char *)hrtview.surface->pixels)[y*(hrtview.width*(hrtview.depth/8))+x] =
135                         ~((unsigned char *)hrtview.surface->pixels)[y*(hrtview.width*(hrtview.depth/8))+x];
136         x = hrtview.start_x;
137         for (y = hrtview.start_y; y < hrtview.cur_y; y++) 
138                 ((unsigned char *)hrtview.surface->pixels)[y*(hrtview.width*(hrtview.depth/8))+x] =
139                         ~((unsigned char *)hrtview.surface->pixels)[y*(hrtview.width*(hrtview.depth/8))+x];
140         x = hrtview.cur_x;
141         for (y = hrtview.start_y; y < hrtview.cur_y; y++) 
142                 ((unsigned char *)hrtview.surface->pixels)[y*(hrtview.width*(hrtview.depth/8))+x] =
143                         ~((unsigned char *)hrtview.surface->pixels)[y*(hrtview.width*(hrtview.depth/8))+x];
144 }
145 
146 int update_frame()
147 {
148         int result;
149         int y, x;
150 
151         hrtview.frame_count++;
152 
153         if (SDL_MUSTLOCK(hrtview.surface))
154                 SDL_LockSurface(hrtview.surface);
155 
156         if (hrtview.fd >= 0) {
157                 /* We expect read() to block waiting for next frame */
158                 printf ("reading\n");
159                 result = read(hrtview.fd, hrtview.surface->pixels, 
160                      hrtview.width * hrtview.height * (hrtview.depth / 8));
161                 printf ("read returned %d\n", result);
162                 if (result == -1) return -1;
163 
164         }
165         /*
166         for (y = 0; y < hrtview.height; y++) {
167                 for (x = 0; x < hrtview.width; x++) {   
168                         ((char *)hrtview.surface->pixels)[y*hrtview.width+x] = 
169                         (char)((hrtview.frame_count%101)*x+(y/hrtview.frame_count)*101)%256;
170                 }
171         }
172         */
173         if (hrtview.dragging) 
174                 draw_invert_rect();
175 
176         SDL_UnlockSurface(hrtview.surface);
177         SDL_Flip(hrtview.surface);      /* should wait for vsync */
178         return 0;
179 }
180 
181 void save_frame()
182 {
183         printf("save frame not done\n");
184 }
185 
186 void set_view_window(int x, int y, int w, int h)
187 {
188         if (x % 4) x = x - (x % 4);
189         if (y % 4) y = y - (y % 4);
190         if (w % 4) w += 4 - (w % 4);
191         if (h % 4) h += 4 - (h % 4);
192 
193         close_view();
194         close_device();
195 
196         hrtview.x      = x;
197         hrtview.y      = y;
198         hrtview.width  = w;
199         hrtview.height = h;
200 
201 
202         if (init_device()) {
203                 printf("cannot re-create device\n");
204                 exit(0);
205         }
206 
207         if (init_view()) {
208                 printf("cannot re-create view\n");
209                 close_device();
210                 exit(0);
211         }       
212 }
213 
214 void main_loop()
215 {
216      SDL_Event ev;
217      int done = 0;
218      int start_x, start_y;
219 
220      while (!done) {
221           while(SDL_PollEvent(&ev)) {
222                         switch(ev.type) {
223                         case SDL_QUIT: done = 1; break;
224                         case SDL_KEYDOWN:
225                                 switch(ev.key.keysym.sym) {
226                                 case SDLK_ESCAPE: done = 1;  break;
227                                 /* some ioctls */
228                                 case SDLK_s:
229                                         break;
230                                 case SDLK_g:
231                                         break;
232                                 case SDLK_f:
233                                         break;
234                                 case SDLK_b:
235                                 {
236                                         SDL_SaveBMP(hrtview.surface, "snapshot.bmp");
237                                         printf("saved to snapshot.bmp\n");
238                                         switch(fork()) {
239                                         case 0: /* child */
240                                                 execlp("/usr/bin/display", "display", "./snapshot.bmp", NULL);
241                                                 break;
242                                         case -1: 
243                                                 printf("cannot fork process\n");
244                                                 break;
245                                         default:
246                                                 printf("forked child\n");
247                                                 break;
248                                         }       
249                                         break;
250                                 }
251                                 case SDLK_SPACE:  break;
252                                 case SDLK_RETURN: save_frame(); break;
253                                 case SDLK_m: break;
254                                 }
255                                 break;
256                         case SDL_KEYUP:
257                                 break;
258                         case SDL_MOUSEMOTION:
259                                 if (hrtview.dragging) {
260                                         hrtview.cur_x = ev.motion.x;
261                                         hrtview.cur_y = ev.motion.y;
262                                 }
263                                 break;  
264                         case SDL_MOUSEBUTTONDOWN:
265                                 printf("%d,%d\n", ev.button.x, ev.button.y);
266                                 if (ev.button.button == 3) 
267                                         set_view_window(0, 0, 512, 480);
268                                 else if (ev.button.button == 1) {
269                                         hrtview.start_x = ev.button.x;
270                                         hrtview.start_y = ev.button.y;
271                                         hrtview.cur_x   = hrtview.start_x;
272                                         hrtview.cur_y   = hrtview.start_y;
273                                         hrtview.dragging = 1;
274                                 }
275                                 break;
276                         case SDL_MOUSEBUTTONUP:
277                                 printf("%d,%d\n", ev.button.x, ev.button.y);
278                                 if (hrtview.dragging && ev.button.button == 1) {
279                                         if (ev.button.x > hrtview.start_x && ev.button.y > hrtview.start_y) {
280                                                 int w = ev.button.x - hrtview.start_x;
281                                                 int h = ev.button.y - hrtview.start_y;
282                                                 set_view_window(hrtview.start_x, hrtview.start_y, w, h);
283                                                 hrtview.dragging = 0;
284                                         }
285                                 }
286                                 break;
287                         }
288                 }
289         
290                 if (update_frame()) break;
291                 SDL_Delay(15);
292         }       
293 }
294 
295 int main(int argc, char *argv[])
296 {
297         memset(&hrtview, 0, sizeof(hrtview_t));
298         if(init_params(argc, argv)) {
299                 return -1;
300         }
301         printf("init_params done\n");
302         if(init_device()) {
303                 return -1;
304         }
305         printf("init_device done\n");
306         if(init_view()) {
307                 close_device();
308                 return -1;
309         }               
310         printf("init_view done\n");
311 
312         /* only return from main loop if we're ready to exit */
313         main_loop();
314 
315         printf("total Frames: %lu\n", hrtview.frame_count);
316         close_view();
317         close_device();
318         if (hrtview.hrt_device) free(hrtview.hrt_device);
319         return 0;
320 }
321 
322 
  This page was automatically generated by the LXR engine.