⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 verify.c

📁 一个linux 下的触摸屏校验应用程序
💻 C
字号:


#include <assert.h>
#include <gtk/gtk.h>
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include <fcntl.h>
#include <error.h>
#ifdef LINUX_V
#include <asm/io.h>
#endif


typedef struct 
{
	unsigned short pressure;  // touch pressure
	short x;         // calibrated X
	short y;         // calibrated Y
	unsigned short millisecs; // timestamp of this event
} TS_EVENT;

typedef struct
{
	int xraw;
	int yraw;
	int pendown;
} TS_RAW;

typedef struct 
{
	int xscale;
	int yscale;
	int xrawmax;
	int xrawmin;
	int yrawmax;
	int yrawmin;
	int xyswap;
} TS_CAL;

typedef struct 
{
	int x;
	int y;
}POINT;

/* Use 'f' as magic number */
#define IOC_MAGIC  'f'


#define TS_GET_CAL	8
#define TS_SET_CAL	9
#define TS_CAL_START	10
#define TS_CAL_END	11
#define TS_SET_TRACE	12
#define TS_GET_PARAM	13
#define TS_GET_RAW	14
#define TS_GET_CPLD 18

// power driver 
typedef struct
{
	int vbat;
	int temp0;
	int temp1;
	int auxin;
} TS_PARAM;

typedef struct
{
	int xleft;
	int ytop;
	int xright;
	int ybottom;
	int btracesupport;
} TS_TRACE;


/* Use 'f' as magic number */
#define IOC_MAGIC  'f'

#define WIDTH 800
#define HEIGH 600

static GtkWidget* da;

int fdCalibration = -1;
void WriteCalibration(int fd,TS_CAL* c);
void StartCalibration();
void EndCalibration();
void OpenCalibration();
void CloseCalibration();
void ResetCalibrationStatus();
gboolean  gtk_check(gpointer data);
void SuccessCalibration();
void FailedCalibration();
gboolean AdjustCalibration();
gboolean SwapValidCalibration();
void StartTrace();
void _expose_(GtkWidget* widget,GdkEventExpose* event,gpointer data);
void _expose_text(GtkWidget* widget,int x,int y,const char* text,int pos);
int  calibration_retry = 0;
static GtkWidget* da;
int button_down_number = 0;
int predown = 0;
int timer_handle = 0;

POINT p[] = { {10,5},{10,15},{5,10},{15,10},\
{WIDTH-10,HEIGH-15},{WIDTH-10,HEIGH-5},{WIDTH-15,HEIGH-10},{WIDTH-5,HEIGH-10},\
{WIDTH-10,5},{WIDTH-10,15},{WIDTH-15,10},{WIDTH-5,10},
{10,HEIGH-15},{10,HEIGH-5},{5,HEIGH-10},{15,HEIGH-10}};
TS_CAL cal;

TS_RAW tss;
TS_TRACE ts_trace;

int cal_x = 0;
int cal_y = 0;

int cal_x_again = 0;
int cal_y_again = 0;

static GdkColor black = {0,0,0,0};
static GdkColor white = {0,0xffff,0xffff,0xffff};
static GdkColor ax = {0,0xDB24,0xD926,0xD42B};
static GdkColor r = {0,0xffff,0,0};
static GdkColor g = {0,0,0xffff,0};
static GdkColor b = {0,0,0,0xffff};
static GdkColor y = {0,0,0xffff,0xffff};

#define cursor1_width 16
#define cursor1_height 16

static unsigned char cursor1_bits[] = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
static unsigned char cursor1mask_bits[] = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

		
GdkCursor *cursor;
GdkPixmap *source, *mask;
GdkColor fg = { 0, 65535, 0, 0 }; /* Red. */
GdkColor bg = { 0, 0, 0, 65535 }; /* Blue. */


int main(int argc, char* argv[])
{
	int result = 0;
	GtkWidget* window;
	GtkWidget* fixed ;

	gtk_init(&argc,&argv);
	gdk_rgb_init();
	

	window = gtk_window_new(GTK_WINDOW_POPUP);
	gtk_widget_set_uposition(GTK_WIDGET(window),0,0);
	gtk_widget_set_usize(GTK_WIDGET(window),WIDTH,HEIGH);
	
	fixed=gtk_fixed_new();
	gtk_container_add(GTK_CONTAINER(window),fixed);	
	
	da = gtk_drawing_area_new();
	gtk_widget_set_usize(GTK_WIDGET(da),WIDTH, HEIGH);
	gtk_fixed_put(GTK_FIXED(fixed),da,0,0);
	g_signal_connect(G_OBJECT(da),"expose_event",GTK_SIGNAL_FUNC(_expose_),0);
	ResetCalibrationStatus();

	source = gdk_bitmap_create_from_data (NULL, cursor1_bits,cursor1_width, cursor1_height);
	mask = gdk_bitmap_create_from_data (NULL, cursor1mask_bits,cursor1_width, cursor1_height);
	cursor = gdk_cursor_new_from_pixmap (source, mask, &fg, &bg, 8, 8);
	gdk_pixmap_unref (source);
	gdk_pixmap_unref (mask);	

	gtk_widget_show_all(window);
	gdk_window_set_cursor (window->window, cursor);
	gtk_main();
	return 0;	
}


void FailedCalibration()
{
	EndCalibration();
	CloseCalibration();
	if(timer_handle)
		gtk_timeout_remove(timer_handle) ;
	timer_handle = 0;
	calibration_retry = 0;
	predown = 0;
	exit(0);
}

gboolean SwapValidCalibration()
{
	int sure = 0;
	int x = 0;
	int y = 0;
	int xs = 0;
	int ys = 0;
	if(cal.xrawmax == cal.xrawmin)
		return FALSE;
	if(cal.yrawmax == cal.yrawmin)
		return FALSE;

	x = (cal_x - cal.xrawmin) * (WIDTH-20 ) /(cal.xrawmax-cal.xrawmin);	
	y = (cal_y - cal.yrawmin) * (HEIGH-20 ) /(cal.yrawmax-cal.yrawmin);

	// swap.
	ys = (cal_x - cal.xrawmin ) * (HEIGH-20 ) /(cal.xrawmax - cal.xrawmin);
	xs = (cal_y - cal.yrawmin ) * (WIDTH-20 ) /(cal.yrawmax - cal.yrawmin);

	if(x <0) x = -x;
	if(y <0) y = -y;

	if(xs <0) xs = -xs;
	if(ys <0) ys = -ys;
	


	x -=(WIDTH-20 ); if(x < 0) x = -x;
	y -=0;  if(y < 0) y = -y;
	

	xs -=(WIDTH-20 ); if(xs < 0) xs = -xs;
	ys -=0;  if(ys < 0) ys = -ys;

	if(xs <= 100  &&  ys <=100)
	{
		cal.xyswap = 1;
		sure = 1;
	}
	else if(x <= 100  &&  y <= 100 )
	{
		cal.xyswap = 0;
		sure = 1;
	}
	else
	{
		return FALSE;
	}
	return TRUE;
}

gboolean AdjustCalibration()
{

	// start deal with ...

	int x0 = 0;
	int y0 = 0;
	int x1 = 0;
	int y1 = 0;
	int min_x = 0;
	int min_y = 0;
	int max_x = 0;
	int max_y = 0;
	if(cal.xyswap == 0)
	{
		min_x = ( cal.xrawmin + cal_x_again ) /2;
		min_y = ( cal.yrawmin + cal_y) /2;
		max_x = ( cal.xrawmax + cal_x) /2;
		max_y = ( cal.yrawmax + cal_y_again ) /2;
	

		x0 = min_x + (min_x - max_x)*10/(WIDTH-20);
		y0 = min_y + (min_y - max_y)*10/(HEIGH-20);
		x1 = max_x - (min_x - max_x)*10/(WIDTH-20);
		y1 = max_y - (min_y - max_y)*10/(HEIGH-20);

		cal.xrawmin = x0;
		cal.yrawmin = y0;

		cal.xrawmax = x1;
		cal.yrawmax = y1;
	} 
	else 
	{
		min_x = ( cal.xrawmin + cal_x ) /2;
		min_y = ( cal.yrawmin + cal_y_again) /2;
		max_x = ( cal.xrawmax + cal_x_again) /2;
		max_y = ( cal.yrawmax + cal_y) /2;
	

		x0 = min_x + (min_x - max_x)*10/(WIDTH-20);
		y0 = min_y + (min_y - max_y)*10/(HEIGH-20);
		x1 = max_x - (min_x - max_x)*10/(WIDTH-20);
		y1 = max_y - (min_y - max_y)*10/(HEIGH-20);

		cal.xrawmin = x0;
		cal.yrawmin = y0;

		cal.xrawmax = x1;
		cal.yrawmax = y1;

	}
	return TRUE;
}

void SuccessCalibration()
{
	FILE* fdconf;
	if(timer_handle)
		gtk_timeout_remove(timer_handle) ;
	timer_handle = 0;
	if(AdjustCalibration())
	{
		WriteCalibration(fdCalibration,&cal);
		EndCalibration();
		CloseCalibration();
		fdconf = fopen("/etc/PXA_X11.conf", "w");
		if(fdconf)
		{
			fprintf(fdconf, "%d,%d,%d,%d,%d,%d,%d", cal.xscale, cal.yscale, cal.xrawmin, cal.yrawmin,cal.xrawmax, cal.yrawmax, cal.xyswap);
		fclose(fdconf);
		}
	calibration_retry = 0;
	predown = 0;
	}
	exit(0);
	
}

void OpenCalibration()
{
#ifdef LINUX_V
	fdCalibration = open("/dev/pxa_ts",(O_RDONLY | O_NDELAY));
	if (fdCalibration <=0)
	{
	}
#endif
	
}
void StartCalibration()
{
#ifdef LINUX_V
	if(fdCalibration<=0)
		return;
	
	ioctl(fdCalibration, TS_CAL_START);
#endif
	
}
void EndCalibration()
{
#ifdef LINUX_V
	
	if(fdCalibration<=0)
		return;
	ioctl(fdCalibration, TS_CAL_END);
#endif
	
}
void WriteCalibration(int fd,TS_CAL* c)
{
#ifdef LINUX_V
	if(fd <=0)
		return;
	ioctl(fd, TS_SET_CAL, c);
#endif
}

void CloseCalibration()
{
#ifdef LINUX_V
	if(fdCalibration<=0)
		return;
	close(fdCalibration);
#endif
}

void ResetCalibrationStatus()
{
	OpenCalibration();
	
	
#ifdef LINUX_V	
	calibration_retry = 0;
	predown = 0;
	
	cal.xrawmin = cal.yrawmin = cal.xrawmax = cal.yrawmax = 0;
	cal.xyswap = 0;
	cal.xscale = WIDTH;
	cal.yscale = HEIGH;
	
	ts_trace.xleft = 0;
	ts_trace.xright = 0;
	ts_trace.ytop = 0;
	ts_trace.ybottom = 0;
	ts_trace.btracesupport = 0;		/// ????????
#endif
//	StartTrace();
	StartCalibration();

	if(fdCalibration<=0)
		return;

	timer_handle = gtk_timeout_add(50,(GtkFunction)gtk_check,NULL);


}


gboolean gtk_check(gpointer data)
{
	int ret = 0;
	if(fdCalibration <= 0)
	{
		FailedCalibration();
		return TRUE;
	}
#ifdef LINUX_V
	ret = ioctl(fdCalibration,TS_GET_RAW, &tss);
#endif


	if(tss.pendown ==1 && predown ==0)
	{
		calibration_retry = 0;
		predown =1;
	}
	if(predown == 1 && tss.pendown == 0)
	{
		if(0 == button_down_number)
		{
			cal.xrawmin = tss.xraw;
			cal.yrawmin = tss.yraw;
			predown = 0;
		}
		else if( 1 == button_down_number)
		{
			cal.xrawmax = tss.xraw;
			cal.yrawmax = tss.yraw;
			predown = 0;
		}
		else if(2 == button_down_number )
		{
			cal_x = tss.xraw ;
			cal_y = tss.yraw ;
			predown = 0;
			if(!SwapValidCalibration())
			{
				StartCalibration();
				return TRUE;
			}
		}
		else if(3 == button_down_number)
		{
			cal_x_again = tss.xraw;
			cal_y_again = tss.yraw;
			SuccessCalibration();
			return FALSE;
		}
		button_down_number++;
		gtk_widget_queue_draw (GTK_WIDGET(da));
		StartCalibration();
	
	}
	else
		calibration_retry++;
	
	if(calibration_retry>400)
	{
		FailedCalibration();
		return FALSE;
	}
	return 1;
	
}

void StartTrace()
{
	if(fdCalibration<=0)
		return;
#ifdef LINUX_V
	ioctl(fdCalibration, TS_SET_TRACE, &ts_trace);
#endif
}

void _expose_text(GtkWidget* widget,int x,int y,const char* text,int pos)
{
	PangoFontDescription * font;
    PangoContext *context;
    PangoLayout  *layout;
	GdkColormap* cmap;

	GdkGC* gc;
	char font_desc[24];
	int nwidth = 0;
	int nheight = 0;
	gc=gdk_gc_new(widget->window);
	cmap=gdk_colormap_get_system();	
	memset(font_desc,0,24);
	sprintf(font_desc,"Serif Bold %d",WIDTH/100+10);
	font =  pango_font_description_from_string (font_desc);
    context = gtk_widget_create_pango_context (widget);
    layout  = pango_layout_new (context);
    g_object_unref (context);
	if(layout == NULL)
	{
		pango_font_description_free(font);
		gdk_gc_unref(gc);
		return;
	}
	pango_layout_set_font_description (layout, font);
	pango_layout_set_text (layout,text,  -1);	
	pango_layout_get_pixel_size(layout,&nwidth,&nheight);
	
	
	
	
	
	if(pos ==0) 
	{
		gdk_color_alloc(cmap,&r);
		gdk_gc_set_foreground(gc,&r);
	}
	else if(pos ==1) 
	{
		gdk_color_alloc(cmap,&g);
		gdk_gc_set_foreground(gc,&g);
	}
	else if(pos ==2)
	{
		gdk_color_alloc(cmap,&b);
		gdk_gc_set_foreground(gc,&b);
	}
	else if(pos ==3)
	{
		gdk_color_alloc(cmap,&r);
		gdk_gc_set_foreground(gc,&r);
	}

	gdk_draw_layout(widget->window,gc,(WIDTH-nwidth)/2,(HEIGH-nheight)/2,layout);

	pango_font_description_free(font);
	g_object_unref (layout);
	gdk_gc_unref(gc);

}

void _expose_( GtkWidget *widget,
					 GdkEventExpose *event, 
                     gpointer   data ) {
	GdkGC* gc;
	GdkColormap* cmap;
	GdkGCValues values;

	gc=gdk_gc_new(GDK_DRAWABLE(widget->window ));		
	cmap=gdk_colormap_get_system();	

	gdk_gc_get_values	  (gc,&values);
	gdk_gc_set_line_attributes (gc,2,values.line_style,values.cap_style,values.join_style);

	gdk_color_alloc(cmap,&white);
	gdk_color_alloc(cmap,&black);
	gdk_color_alloc(cmap,&r);
	gdk_color_alloc(cmap,&g);
	gdk_color_alloc(cmap,&b);
	gdk_color_alloc(cmap,&y);
	gdk_gc_set_foreground(gc,&white );
	gdk_draw_rectangle (da->window,gc,1,event->area.x,event->area.y,event->area.width,event->area.height);

	if(button_down_number == 0)
	{
		
		gdk_gc_set_foreground(gc,&r);
		gdk_draw_line(da->window,gc,p[0].x,p[0].y,p[1].x,p[1].y);
		gdk_draw_line(da->window,gc,p[2].x,p[2].y,p[3].x,p[3].y);
		_expose_text(da,WIDTH/4,HEIGH/2,"Upper Left Touch+",0);
	}
	else if(button_down_number == 1)
	{
		gdk_gc_set_foreground(gc,&black);
		gdk_draw_line(da->window,gc,p[0].x,p[0].y,p[1].x,p[1].y);
		gdk_draw_line(da->window,gc,p[2].x,p[2].y,p[3].x,p[3].y);
		gdk_gc_set_foreground(gc,&g);
		gdk_draw_line(da->window,gc,p[4].x,p[4].y,p[5].x,p[5].y);
		gdk_draw_line(da->window,gc,p[6].x,p[6].y,p[7].x,p[7].y);
		_expose_text(da,WIDTH/4,HEIGH/2,"Lower Right Touch +",1);
	}
	else if(button_down_number ==2)
	{
		gdk_gc_set_foreground(gc,&black);
		gdk_draw_line(da->window,gc,p[0].x,p[0].y,p[1].x,p[1].y);
		gdk_draw_line(da->window,gc,p[2].x,p[2].y,p[3].x,p[3].y);
		gdk_draw_line(da->window,gc,p[4].x,p[4].y,p[5].x,p[5].y);
		gdk_draw_line(da->window,gc,p[6].x,p[6].y,p[7].x,p[7].y);
		gdk_gc_set_foreground(gc,&b );
		gdk_draw_line(da->window,gc,p[8].x,p[8].y,p[9].x,p[9].y);
		gdk_draw_line(da->window,gc,p[10].x,p[10].y,p[11].x,p[11].y);
		_expose_text(da,WIDTH/4,HEIGH/2,"Upper Right Touch+",2);
	}	
	else if(button_down_number ==3)
	{
		gdk_gc_set_foreground(gc,&black);
		gdk_draw_line(da->window,gc,p[0].x,p[0].y,p[1].x,p[1].y);
		gdk_draw_line(da->window,gc,p[2].x,p[2].y,p[3].x,p[3].y);
		gdk_draw_line(da->window,gc,p[4].x,p[4].y,p[5].x,p[5].y);
		gdk_draw_line(da->window,gc,p[6].x,p[6].y,p[7].x,p[7].y);		
		gdk_draw_line(da->window,gc,p[8].x,p[8].y,p[9].x,p[9].y);
		gdk_draw_line(da->window,gc,p[10].x,p[10].y,p[11].x,p[11].y);
		gdk_gc_set_foreground(gc,&r );
		gdk_draw_line(da->window,gc,p[12].x,p[12].y,p[13].x,p[13].y);
		gdk_draw_line(da->window,gc,p[14].x,p[14].y,p[15].x,p[15].y);
		_expose_text(da,WIDTH/4,HEIGH/2,"Lower Left Touch+",3);

	}
	gdk_gc_unref(gc);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -