driver.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
00046
00047
00048 #include "driver.h"
00049 #include "scblog.h"
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #define DMPORT 50555
00062 #define DMSERVER "127.0.0.1"
00063
00064 #define FRAMEBUFFER_DEVICE "/dev/fb0"
00065
00066
00067
00068
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
00081
00082
00083
00084
00085
00086
00087
00088 void erscribble_driver_init()
00089 {
00090
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
00124 close(fd);
00125 fd = 0;
00126
00127 #ifdef ENABLE_DRIVER
00128
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
00165 if (s_driver_draw_ctx.points.count > ERSCRIBBLE_DEF_DRIVER_DRAW_COUNT)
00166 {
00167
00168 return TRUE;
00169 }
00170
00171
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
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
00223
00224
00225
00226
00227
00228
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 }