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

📄 main.c

📁 在运行嵌入式Linux系统的、支持屏幕点击功能的手机上
💻 C
📖 第 1 页 / 共 2 页
字号:
	    sx = (sample[middle-1].x + sample[middle].x) / 2;
	  qsort (sample, samples, sizeof(struct point), sort_by_y);
	  if (samples & 1)
	    sy = sample[middle].y;
	  else
	    sy = (sample[middle-1].y + sample[middle].y) / 2;
	  
	  cal.x[event] = sx;
	  cal.y[event] = sy;
	  
	  if (flag_debug)
	    fprintf (stderr, "point %d: [%d %d]\n", event, cal.x[event], cal.y[event]);
	  
	  event++;
	  
	  if (event < NR_POINTS)
	    {
	      samples = 0;
	      next_event ();
	    }
	  else
	    {
	      if (perform_calibration (&cal))
		{
		  set_calibration (&cal);
		  //XCalibrateSetRawMode (dpy, False);
		  exit (0);
		}
	      else
		{
		  samples = 0;
		  event = 0;
		  next_event ();
		}
	    }
	}
    }
}

void
read_ts (void)
{
  TS_EVENT ts_ev;
  int r;

  r = read (ts_fd, &ts_ev, sizeof (ts_ev));
  if (r == sizeof (ts_ev)) {
    handle_ts_event (ts_ev.x, ts_ev.y, ts_ev.pressure);
    if (flag_debug)
       printf("read_ts: x=%d, y=%d, press=%d\n", ts_ev.x, ts_ev.y, ts_ev.pressure);
  }
}

void
priv_do_cal(TS_CAL * tc)
{
  if (flag_debug)
    fprintf (stderr, "setting: %d %d %d %d %d\n", 
	     tc->xtrans, tc->ytrans, tc->xscale, tc->yscale, tc->xyswap);
    
  if (ioctl (ts_fd, TS_SET_CAL, (void *)tc) != 0)
    {
      perror ("TS_SET_CAL");
      exit (1);
    }
  
  exit (0);
}

void
do_cal (char **args)
{
  TS_CAL tc;

  tc.xscale = atoi (args[0]);
  tc.xtrans = atoi (args[1]);
  tc.yscale = atoi (args[2]);
  tc.ytrans = atoi (args[3]);
  tc.xyswap = atoi (args[4]);
  priv_do_cal(&tc);
}

do_cal_file (char **args)
{
  FILE * fp;
  TS_CAL tc;

  fp = fopen(args[0], "r");
  if(!fp) {
     fprintf(stderr, "fail to open %s\n", args[0]);
     //to normal calibrate process
     return;
  }  
  fscanf(fp,"%d %d %d %d %d",&tc.xscale, &tc.xtrans, &tc.yscale, &tc.ytrans, &tc.xyswap);
  fclose(fp);
  priv_do_cal(&tc);
}

void
show_cal (void)
{
  TS_CAL tc;

  if (ioctl (ts_fd, TS_GET_CAL, (void *)&tc) != 0)
    {
      perror ("TS_GET_CAL");
      exit (1);
    }

  printf ("%d %d %d %d %d\n", tc.xscale, tc.xtrans, tc.yscale, tc.ytrans, tc.xyswap);
}

void
usage (const char *name)
{
  fprintf (stderr, "usage: %s -view\n", name);
  fprintf (stderr, "       %s [-rotate <0 | 90 | 180 | 270>]\n", name);
  fprintf (stderr, "       %s -cal <xs> <xt> <ys> <yt> <xyswap>\n", name);
  fprintf (stderr, "       %s -cal datafile\n", name);

  exit (1);
}

int
xrr_supported (void)
{
  int xrr_event_base, xrr_error_base;
  int xrr_major, xrr_minor;

  if (XRRQueryExtension (dpy, &xrr_event_base, &xrr_error_base) == False
      || XRRQueryVersion (dpy, &xrr_major, &xrr_minor) == 0
      || xrr_major != 1
      || xrr_minor < 1)
    return 0;

  return 1;
}

int
main (int argc, char *argv[])
{
  XSetWindowAttributes attributes;
  int xfd;
  XRenderColor colortmp;
  int max_fd;
  GC bg_gc;
  int i;
  int have_xrandr;

  if (!using_xcalibrate)
    {
      ts_fd = open (RAW_DEVICE, O_RDONLY);
      if (ts_fd < 0)
	{
	  perror (RAW_DEVICE);
	  exit (1);
	}
    }

  for (i = 1; i < argc; i++)
    {
      if (!strcmp (argv[i], "-view"))
	{
	  show_cal ();
	  exit (0);
	}
      else if (!strcmp (argv[i], "-debug"))
	flag_debug = 1;
      else if (!strcmp (argv[i], "-cal"))
	{
	  if (argc > (i + 5))
	    do_cal (argv + i + 1);
          else if(argc == (i+2)) {
            do_cal_file (argv + i + 1);
            //if cal from data file fail, do normal
            break;
	  } else
	    usage (argv[0]);
	}
      else if (!strcmp (argv[i], "-rotate"))
	{
	  if (argc > (i + 1))
	    rotation = atoi (argv[++i]);
	  else
	    usage (argv[0]);
	}
      else
	usage (argv[0]);
    }
  
  dpy = XOpenDisplay (NULL);
  if (dpy == NULL)
    {
      fprintf (stderr, "Couldn't open display\n");
      exit (1);
    }

  screen = DefaultScreen (dpy);

  have_xrandr = xrr_supported ();
  if (have_xrandr)
    {
      XRRScreenConfiguration *rr_screen;
      Rotation current_rotation;

      if (flag_debug)
	fprintf (stderr, "XRANDR is supported\n");

      rr_screen = XRRGetScreenInfo (dpy, RootWindow (dpy, screen));

      XRRRotations (dpy, screen, &current_rotation);

      XRRFreeScreenConfigInfo (rr_screen);

      if (flag_debug)
	fprintf (stderr, "Current RANDR rotation is %d\n", current_rotation);

      switch (current_rotation)
	{
	case RR_Rotate_270:
	  rotation += 90;
	case RR_Rotate_180:
	  rotation += 90;
	case RR_Rotate_90:
	  rotation += 90;
	  rotation %= 360;
	case RR_Rotate_0:
	  break;
	default:
	  fprintf (stderr, "Unknown RANDR rotation: %d\n", current_rotation);
	  break;
	}
    }
  else
    {
      if (flag_debug)
	fprintf (stderr, "XRANDR not supported\n");
    }

  attributes.override_redirect = flag_debug ? False : True;
  attributes.background_pixel = WhitePixel (dpy, screen);
    
  w = XCreateWindow (dpy, DefaultRootWindow(dpy), 0 , 0,
		     DisplayWidth(dpy, 0), DisplayHeight(dpy, 0),
		     0,
		     DefaultDepth(dpy, 0),
		     InputOutput,
		     DefaultVisual(dpy, 0),
		     CWOverrideRedirect | CWBackPixel,
		     &attributes );

  screen_x = DisplayWidth (dpy, screen);
  screen_y = DisplayHeight (dpy, screen);

  hide_cursor (dpy, w);

  crosshair_x = OFFSET;
  crosshair_y = OFFSET;

  crosshair_w = XCreateSimpleWindow (dpy, w, crosshair_x, crosshair_y,
				     CROSSHAIR_SIZE, CROSSHAIR_SIZE,
				     0, None, WhitePixel (dpy, screen));

  draw_crosshair ();

  xftfont = XftFontOpenName (dpy, screen, FONTNAME);
  
  if (xftfont == NULL)
    { 
      fprintf (stderr, "Can't open XFT font\n"); 
      exit(1); 
    }

  colortmp.red   = 0;
  colortmp.green = 0;
  colortmp.blue  = 0;
  colortmp.alpha = 0xffff;
  XftColorAllocValue (dpy,
		     DefaultVisual (dpy, screen),
		     DefaultColormap (dpy, screen),
		     &colortmp,
		     &xftcol);

  bg_pixmap = XCreatePixmap (dpy, w, screen_x, screen_y, DefaultDepth (dpy, screen));
  bg_gc = XCreateGC (dpy, bg_pixmap, 0, NULL);
  XSetForeground (dpy, bg_gc, WhitePixel (dpy, screen));

  XFillRectangle (dpy, bg_pixmap, bg_gc, 0, 0, screen_x, screen_y);

  xftdraw = XftDrawCreate (dpy, bg_pixmap,
			   DefaultVisual (dpy, screen),
			   DefaultColormap (dpy, screen));

  draw_background ();

  XSetWindowBackgroundPixmap (dpy, w, bg_pixmap);

  XSelectInput (dpy, w, StructureNotifyMask | ButtonPressMask | KeyPressMask);
  XSelectInput (dpy, crosshair_w, ExposureMask);
  XMapWindow (dpy, w);
  XMapRaised (dpy, crosshair_w);
  XGrabPointer (dpy, w, True, Button1Mask, GrabModeAsync, GrabModeAsync,
		None, None, CurrentTime);
  XGrabKeyboard (dpy, w, True, GrabModeAsync, GrabModeAsync, CurrentTime);

  xfd = ConnectionNumber (dpy);

#if NR_POINTS == 5
  cal.xscr[0] = OFFSET;
  cal.yscr[0] = OFFSET;

  cal.xscr[1] = screen_x - OFFSET;
  cal.yscr[1] = OFFSET;

  cal.xscr[2] = screen_x - OFFSET;
  cal.yscr[2] = screen_y - OFFSET;

  cal.xscr[3] = OFFSET;
  cal.yscr[3] = screen_y - OFFSET;

  cal.xscr[4] = (screen_x / 2);
  cal.yscr[4] = (screen_y / 2);
#else

#error Unsupported number of calibration points

#endif

  for (i = 0; i < NR_POINTS; i++)
    {
      switch (rotation)
	{
	case 0:
	  cal.xfb[i] = cal.xscr[i];
	  cal.yfb[i] = cal.yscr[i];
	  break;
	case 90:
	  cal.xfb[i] = cal.yscr[i];
	  cal.yfb[i] = screen_x - cal.xscr[i];
	  break;
	case 180:
	  cal.xfb[i] = screen_x - cal.xscr[i];
	  cal.yfb[i] = screen_y - cal.yscr[i];
	  break;
	case 270:
	  cal.xfb[i] = screen_y - cal.yscr[i];
	  cal.yfb[i] = cal.xscr[i];
	  break;
	}

      if (flag_debug)
	printf ("rotation %d: (%d,%d) -> (%d,%d)\n", rotation, 
		cal.xscr[i], cal.yscr[i], cal.xfb[i], cal.yfb[i]);
    }

  next_event ();

  max_fd = (xfd > ts_fd) ? xfd : ts_fd;

  for (;;)
    {
      fd_set fds;

      handle_events ();

      FD_ZERO (&fds);
      FD_SET (xfd, &fds);
      if (ts_fd != -1)
	FD_SET (ts_fd, &fds);

      select (max_fd + 1, &fds, NULL, NULL, NULL);
      
      if (ts_fd != -1 && FD_ISSET (ts_fd, &fds))
	read_ts ();
    }
}

⌨️ 快捷键说明

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