driver.c

Go to the documentation of this file.
00001 /*
00002  * File Name: driver.c
00003  */
00004 
00005 /*
00006  * This file is part of liberscribble.
00007  *
00008  * liberscribble is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * liberscribble is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 //----------------------------------------------------------------------------
00028 // Include Files
00029 //----------------------------------------------------------------------------
00030 
00031 // system include files, between < >
00032 #include <fcntl.h>
00033 #include <unistd.h>
00034 #include <errno.h>
00035 #include <string.h>
00036 #include <sys/types.h>
00037 #include <sys/socket.h>
00038 #include <arpa/inet.h>
00039 
00040 #ifdef ENABLE_DRIVER
00041 #include <sys/ioctl.h>
00042 #include "delta.h"
00043 #endif
00044 
00045 // ereader include files, between < >
00046 
00047 // local include files, between " "
00048 #include "driver.h"
00049 #include "scblog.h"
00050 
00051 
00052 //----------------------------------------------------------------------------
00053 // Type Declarations
00054 //----------------------------------------------------------------------------
00055 
00056 
00057 //----------------------------------------------------------------------------
00058 // Global Constants
00059 //----------------------------------------------------------------------------
00060 
00061 #define DMPORT      50555
00062 #define DMSERVER    "127.0.0.1"
00063 
00064 #define FRAMEBUFFER_DEVICE "/dev/fb0"
00065 
00066 
00067 //----------------------------------------------------------------------------
00068 // Static Variables
00069 //----------------------------------------------------------------------------
00070 
00071 #ifdef ENABLE_DRIVER
00072 static int fbdev = 0;
00073 #endif
00074 static int fd = 0;
00075 static FastDrawContext s_driver_draw_ctx;
00076 static struct sockaddr_in server_addr;
00077 
00078 
00079 //============================================================================
00080 // Local Function Definitions
00081 //============================================================================
00082 
00083 
00084 //============================================================================
00085 // Functions Implementation
00086 //============================================================================
00087 
00088 void erscribble_driver_init()
00089 {
00090     // create and bind display manager socket
00091     fd = socket(PF_INET, SOCK_DGRAM, 0);
00092     if (-1 == fd)
00093     {
00094         ERRORPRINTF("Could not open port %d at %s, errno %d (%s)", DMPORT, DMSERVER, errno, strerror(errno));
00095         fd = 0;
00096         return;
00097     }
00098     memset(&server_addr, '\0', sizeof(struct sockaddr_in));
00099     server_addr.sin_family = AF_INET;
00100     server_addr.sin_port = htons(DMPORT);
00101     server_addr.sin_addr.s_addr = inet_addr(DMSERVER);
00102     
00103 #ifdef ENABLE_DRIVER
00104     if (fbdev != 0)
00105     {
00106         ERRORPRINTF(FRAMEBUFFER_DEVICE " already open!");
00107         return;
00108     }
00109 
00110     fbdev = open(FRAMEBUFFER_DEVICE, O_RDWR);
00111     if (-1 == fbdev)
00112     {
00113         ERRORPRINTF("Could not open " FRAMEBUFFER_DEVICE "!");
00114         fbdev = 0;
00115         return;
00116     }
00117 #endif
00118 }
00119 
00120 
00121 void erscribble_driver_close()
00122 {
00123     // close display manager socket
00124     close(fd);
00125     fd = 0;
00126 
00127 #ifdef ENABLE_DRIVER
00128     // close frame buffer
00129     close(fbdev);
00130     fbdev = 0;
00131 #endif
00132 }
00133 
00134 
00135 void erscribble_driver_draw_points(DrvPointsBufPtr ptr)
00136 {
00137 #ifdef ENABLE_DRIVER
00138     if (ptr)
00139     {   
00140         if (ioctl (fbdev, FBIO_DELTA_DRAWMODE_PIXELS, ptr) != 0)
00141         {
00142             ERRORPRINTF("ioctl request error!");
00143         }
00144     }
00145 #endif
00146 }
00147 
00148 
00149 
00150 void erscribble_driver_draw_reset_context()
00151 {
00152 #ifdef ENABLE_DRIVER
00153     s_driver_draw_ctx.points.count = 0;
00154     gettimeofday(&s_driver_draw_ctx.t1, NULL);
00155     s_driver_draw_ctx.t2.tv_sec   = s_driver_draw_ctx.t1.tv_sec;
00156     s_driver_draw_ctx.t2.tv_usec  = s_driver_draw_ctx.t1.tv_usec;
00157 #endif
00158 }
00159 
00160 
00161 gboolean erscribble_driver_draw_now()
00162 {
00163 #ifdef ENABLE_DRIVER
00164     // check points 
00165     if (s_driver_draw_ctx.points.count > ERSCRIBBLE_DEF_DRIVER_DRAW_COUNT)
00166     {
00167         //LOGPRINTF("Should draw now points!");
00168         return TRUE;
00169     }
00170 
00171     // check time
00172     static const long usec = 1000 * 1000;
00173     long elapsed = ( s_driver_draw_ctx.t2.tv_sec - s_driver_draw_ctx.t1.tv_sec ) * usec;
00174     elapsed += s_driver_draw_ctx.t2.tv_usec - s_driver_draw_ctx.t1.tv_usec;
00175 
00176     if (elapsed >= ERSCRIBBLE_DEF_DRIVER_DRAW_TIME)
00177     {
00178         LOGPRINTF("Should draw now time!");
00179         return TRUE;
00180     }
00181     
00182     LOGPRINTF("No, not yet. Waiting for...");
00183     return FALSE;
00184 
00185 #else
00186 
00187     return TRUE;
00188 
00189 #endif
00190 }
00191 
00192 
00193 void erscribble_driver_draw_record(const ScbDevPointPtr ptr, 
00194                           const unsigned char size, 
00195                           const unsigned char color,
00196                           const unsigned char pen_down)
00197 {
00198 #ifdef ENABLE_DRIVER
00199     // make a check
00200     if (s_driver_draw_ctx.points.count <  ERSCRIBBLE_DEF_DRIVER_DRAW_BUF_LEN && ptr)
00201     {
00202         s_driver_draw_ctx.points.points[s_driver_draw_ctx.points.count].x = ptr->point.x;
00203         s_driver_draw_ctx.points.points[s_driver_draw_ctx.points.count].y = ptr->point.y;
00204         s_driver_draw_ctx.points.points[s_driver_draw_ctx.points.count].size = size;
00205         s_driver_draw_ctx.points.points[s_driver_draw_ctx.points.count].color = color;
00206         s_driver_draw_ctx.points.points[s_driver_draw_ctx.points.count].pen_down = pen_down;
00207         ++s_driver_draw_ctx.points.count;
00208         gettimeofday(&s_driver_draw_ctx.t2, NULL);
00209     }
00210 #endif
00211 }
00212 
00213 
00214 void erscribble_driver_draw()
00215 {
00216     erscribble_driver_draw_points(&s_driver_draw_ctx.points);
00217 }
00218 
00219 
00220 void erscribble_driver_update_display()
00221 {
00222     // Send update message to display manager
00223     //
00224     //   Syntax: !I,cmd,pid,x,y,w,h,ignore,window,type,widget,focus
00225     //   - paint full screen (w=768,h=1024)
00226     //   - skip the normal rules (ignore=1)
00227     //   - special widget (widget=erscribble)
00228     //   - rest unused (0)
00229     const char *msg = "!I,0,0,0,768,1024,1,0,0,erscribble,0\n";
00230     sendto(fd, msg, strlen(msg), 0, (struct sockaddr*) &server_addr, sizeof(server_addr));
00231     LOGPRINTF("Sent message `%s` to display manager", msg);
00232 }
Generated by  doxygen 1.6.2-20100208