LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 853|回复: 3

compiz里头的smackpad误判太严重, 自己小改了一个.

[复制链接]
发表于 2009-3-15 20:02:36 | 显示全部楼层 |阅读模式

  1. /*
  2. *
  3. * Compiz smackpad plugin
  4. * smackpad.c
  5. * Copyright (c) 2007 Eugen Feller <eugen.feller@uni-duesseldorf.de>
  6. *
  7. * Client message to root window taken from mswitch.c:
  8. * Copyright : (c) 2007 Robert Carr
  9. * E-mail    : racarr@opencompositing.org
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. * GNU General Public License for more details.
  20. *
  21. */

  22. /*
  23. * Thanks to Michele Campeotto <micampe@micampe.it> and
  24. * Fernando Herrera <fherrera@onirica.com> for their ideas
  25. * which helped me to make this plugin.
  26. */

  27. /*
  28. * Almost totally rewrite by Yang Zhe <yangzhe1990@gmail.com>
  29. */

  30. #include <compiz-core.h>
  31. #include "smackpad_options.h"
  32. #include <X11/Xlib.h>
  33. #include <math.h>
  34. #include <limits.h>
  35. #include <time.h>
  36. #include <pthread.h>
  37. #include <stdlib.h>
  38. #include <stdio.h>

  39. #define POSITION_FILE "/sys/devices/platform/hdaps/position"
  40. #define CALIBRATE_FILE "/sys/devices/platform/hdaps/calibrate"
  41. #define INVERT_FILE "/sys/devices/platform/hdaps/invert"

  42. int N=400;

  43. typedef enum
  44. {
  45.         LEFT,
  46.         RIGHT,
  47.         UP,
  48.         DOWN
  49. }smackDirection;

  50. typedef enum
  51. {
  52.         CALIBRATE,
  53.         POSITION
  54. }smackpadMisc;

  55. typedef struct
  56. {
  57.         int x;
  58.         int y;
  59. }smackpadPosition;
  60. static int sensitivityRate=5;
  61. pthread_t readPositionThread;
  62. static int readPositionThreadStatus=-1;
  63. static Bool statusLoop=FALSE;
  64. static Bool statusPlugin=FALSE;
  65. static CompScreen *screen=NULL;

  66. /* prototype definitions */

  67. static void* smackpadGetPosition (void *data);
  68. static void smackpadRotate (int dX, int dY);
  69. static void smackpadUpdateSensitivityRate (CompDisplay *d, CompOption *opt, SmackpadDisplayOptions num);
  70. static Bool smackpadInitiate (CompDisplay *d, CompAction *ac, CompActionState state, CompOption *option, int nOption);
  71. static Bool smackpadReadHDAPSData (smackpadMisc misc, Bool invert, smackpadPosition *position);
  72. static Bool smackpadInitHDAPS (void);
  73. static CompBool smackpadInitObject (CompPlugin *p, CompObject *o);
  74. static Bool smackpadInitDisplay (CompPlugin *p, CompDisplay *d);
  75. static Bool smackpadInitScreen (CompPlugin *p, CompScreen *s);
  76. static void smackpadFini (CompPlugin *p);

  77. static Bool
  78. smackpadReadHDAPSData (smackpadMisc misc,
  79.                                            Bool invert,
  80.                                            smackpadPosition *position)
  81. {
  82.         char buf[255];
  83.         FILE *file=NULL;
  84.        
  85.         switch(misc)
  86.         {
  87.                 case CALIBRATE:
  88.                         file = fopen(CALIBRATE_FILE, "r");
  89.                         break;
  90.                 case POSITION:
  91.                         file = fopen(POSITION_FILE, "r");
  92.                         break;
  93.         }
  94.        
  95.         if (file == NULL)
  96.                 return FALSE;
  97.        
  98.         fread(buf, 255, 1, file);
  99.           fclose(file);
  100.        
  101. /*        if(invert)
  102.                 sscanf(buf, "(%d,%d)", &position->y, &position->x);
  103.         else
  104.                 sscanf(buf, "(%d,%d)", &position->x, &position->y);
  105. */
  106.         sscanf(buf, "(%d,%d)", &position->x, &position->y);
  107.        
  108.         return TRUE;
  109.        
  110. }

  111. static void
  112. smackpadRotate (int dX,
  113.                                 int dY)
  114. {
  115.         XEvent xev;
  116.         xev.xclient.type = ClientMessage;
  117.         xev.xclient.display = screen->display->display;
  118.         xev.xclient.format = 32;
  119.         xev.xclient.message_type = screen->display->desktopViewportAtom;
  120.         xev.xclient.window = screen->root;
  121.         xev.xclient.data.l[0] = (screen->x+dX)*screen->width;
  122.         xev.xclient.data.l[1] = (screen->y+dY)*screen->height;
  123.         xev.xclient.data.l[2] = 0;
  124.         xev.xclient.data.l[3] = 0;
  125.         xev.xclient.data.l[4] = 0;
  126.        
  127.         XSendEvent(screen->display->display, screen->root, FALSE,
  128.                    SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  129. }

  130. static void*
  131. smackpadGetPosition (void *misc)
  132. {       
  133.         struct timespec sleepTimer;
  134.         smackpadPosition *positions;
  135.         int i, w, x, *maxstack, *maxidx, maxbottom, maxtop, maxcnt, *minstack, *minidx, minbottom, mintop, mincnt;
  136.         int direction = 0;
  137.         int tot = 0;
  138. #ifdef DEBUG2
  139.         int j;
  140. #endif
  141.         float avg;

  142.         sleepTimer.tv_sec=0;
  143.         sleepTimer.tv_nsec=1000000; /* 1ms */
  144.        
  145.         positions = malloc( N*sizeof(smackpadPosition) );
  146.         maxstack = malloc( N*sizeof(int) ); maxidx = malloc( N*sizeof(int) ); maxbottom = 0; maxtop = -1; maxcnt = 0;
  147.         minstack = malloc( N*sizeof(int) ); minidx = malloc( N*sizeof(int) ); minbottom = 0; mintop = -1; mincnt = 0;
  148.         if (positions == NULL || maxstack == NULL || maxidx == NULL || minstack == NULL || minidx == NULL) return NULL;

  149.         for (i = 0; i < N; ++i) {
  150.                 if(!smackpadReadHDAPSData(1,TRUE,positions+i)) return NULL;
  151.                 x = (positions+i)->x;
  152.                 tot += x;

  153.                 while (maxcnt > 0 && x >= maxstack[maxtop]) {
  154.                         maxtop = (maxtop + N - 1) % N;
  155.                         --maxcnt;
  156.                 }
  157.                 maxtop = (maxtop + 1) % N;
  158.                 maxstack[maxtop] = x;
  159.                 maxidx[maxtop] = i;
  160.                 ++maxcnt;
  161.                 while (mincnt > 0 && x <= minstack[mintop]) {
  162.                         mintop = (mintop + N - 1) % N;
  163.                         --mincnt;
  164.                 }
  165.                 mintop = (mintop + 1) % N;
  166.                 minstack[mintop] = x;
  167.                 minidx[mintop] = i;
  168.                 ++mincnt;

  169.         }

  170.         i = 0;
  171.         while (statusLoop)
  172.         {
  173.                 avg = (float)tot / N;
  174.                 w = maxstack[maxbottom] - minstack[minbottom];

  175.                 tot -= (positions+i)->x;
  176.                 if(!smackpadReadHDAPSData(1,TRUE,positions+i)) return NULL;
  177.                 x = (positions+i)->x;
  178.                 tot += x;

  179.                 if (minidx[minbottom] == i) {
  180.                         minbottom = (minbottom + 1) % N;
  181.                         --mincnt;
  182.                 }
  183.                 if (maxidx[maxbottom] == i) {
  184.                         maxbottom = (maxbottom + 1) % N;
  185.                         --maxcnt;
  186.                 }
  187.                 while (maxcnt > 0 && x >= maxstack[maxtop]) {
  188.                         maxtop = (maxtop + N - 1) % N;
  189.                         --maxcnt;
  190.                 }
  191.                 maxtop = (maxtop + 1) % N;
  192.                 maxstack[maxtop] = x;
  193.                 maxidx[maxtop] = i;
  194.                 ++maxcnt;
  195.                 while (mincnt > 0 && x <= minstack[mintop]) {
  196.                         mintop = (mintop + N - 1) % N;
  197.                         --mincnt;
  198.                 }
  199.                 mintop = (mintop + 1) % N;
  200.                 minstack[mintop] = x;
  201.                 minidx[mintop] = i;
  202.                 ++mincnt;

  203. #ifdef DEBUG2
  204.                 printf("%d\n", w);
  205. #endif
  206.                 if (w <= 2) {
  207.                         if (fabs(x - avg) > 3)
  208.                                 direction = (x > avg) ? 1 : -1;
  209.                         else
  210.                                 direction = 0;
  211.                         if (fabs(x - avg) > sensitivityRate) {
  212.                                 if (direction == 0)
  213.                                         direction = (x > avg) ? 1 : -1;
  214. #ifdef DEBUG2
  215.                                         printf("avg: %2f, x: %d\n", avg, x);
  216.                                         for (j = (i+490)%N; j != i; j = (j+1)%N)
  217.                                                 printf("(%d, %d), ", (positions+j)->x, j);
  218.                                         printf("(%d, %d)\n", (positions+i)->x, i);
  219. #endif                               
  220.                                 smackpadRotate(direction, 0);
  221.                         }
  222.                 }
  223.                 else
  224.                         direction = 0;

  225.                 i = (i+1) % N;
  226.                 nanosleep(&sleepTimer,NULL);
  227.         }
  228.        
  229.         free(maxstack); free(maxidx);
  230.         free(minstack); free(minidx);
  231.         return NULL;
  232. }

  233. static void
  234. smackpadUpdateSensitivityRate (CompDisplay *d,
  235.                                                            CompOption *opt,
  236.                                                            SmackpadDisplayOptions num)
  237. {
  238.         sensitivityRate=smackpadGetSensitivityRate(d);
  239. }

  240. static Bool
  241. smackpadInitHDAPS (void)
  242. {
  243.         FILE *file=NULL;
  244.         file=fopen(POSITION_FILE,"r");
  245.        
  246.         if(file!=NULL)
  247.         {
  248.                 fclose(file);
  249.                 return TRUE;
  250.         }
  251.        
  252.         return FALSE;
  253. }

  254. static Bool
  255. smackpadInitiate (CompDisplay *d,
  256.                                   CompAction *ac,
  257.                                   CompActionState state,
  258.                                   CompOption *option,
  259.                                   int nOption)
  260. {       
  261.         Bool testHdapsStatus=smackpadInitHDAPS();
  262.        
  263.         if(testHdapsStatus && !statusPlugin)
  264.         {
  265.                 statusLoop=TRUE;
  266.                 statusPlugin=TRUE;
  267.                 readPositionThreadStatus=pthread_create(&readPositionThread,NULL,smackpadGetPosition,NULL);
  268.         }
  269.         else if(testHdapsStatus && screen)
  270.         {
  271.                 statusLoop=FALSE;
  272.                 statusPlugin=FALSE;
  273.                 pthread_join(readPositionThread,NULL);
  274.         }
  275.        
  276.         return TRUE;
  277. }

  278. static Bool
  279. smackpadInitScreen (CompPlugin *p,
  280.                                         CompScreen *s)
  281. {       
  282.         screen=s;
  283.        
  284.         return TRUE;
  285. }

  286. static Bool
  287. smackpadInitDisplay (CompPlugin *p,
  288.                                          CompDisplay *d)
  289. {       
  290.         smackpadSetSensitivityRateNotify(d,smackpadUpdateSensitivityRate);
  291.         smackpadSetInitiateKeyInitiate(d, smackpadInitiate);
  292.        
  293.         return TRUE;
  294. }

  295. static CompBool
  296. smackpadInitObject (CompPlugin *p,
  297.                                         CompObject *o)
  298. {
  299.         static InitPluginObjectProc dispTab[] = {
  300.                 (InitPluginObjectProc) 0, /* InitCore */
  301.                 (InitPluginObjectProc) smackpadInitDisplay,
  302.                 (InitPluginObjectProc) smackpadInitScreen,
  303.     };

  304.     RETURN_DISPATCH (o, dispTab, ARRAY_SIZE (dispTab), TRUE, (p, o));
  305. }

  306. static void
  307. smackpadFini (CompPlugin *p)
  308. {
  309.         if(readPositionThreadStatus==0)
  310.         {
  311.                 statusLoop=FALSE;
  312.                 pthread_join(readPositionThread,NULL);
  313.         }
  314. }


  315. CompPluginVTable smackpadVTable = {
  316.         "smackpad",
  317.         0,
  318.         0,
  319.         smackpadFini,
  320.         smackpadInitObject,
  321.         0,
  322.         0,
  323.         0
  324. };

  325. CompPluginVTable *
  326. getCompPluginInfo (void)
  327. {
  328.         return &smackpadVTable;
  329. }
复制代码
发表于 2009-3-15 22:54:51 | 显示全部楼层
太有水平,偶看不懂什么意思,这个检测什么东西的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-3-16 04:12:51 | 显示全部楼层
Post by jarryson;1961396
太有水平,偶看不懂什么意思,这个检测什么东西的


对于支持hdaps的笔记本... 从右边拍, 屏幕左转; 从左边拍, 屏幕右转..
回复 支持 反对

使用道具 举报

发表于 2009-3-16 04:21:46 | 显示全部楼层
员工正在看小说,看到老板过来,马上切换了一个桌面

老板到跟前,一拍,说:东西做好没有!

然后桌面一翻转,之前看的电影窗口显示出来

老板、员工一起抱着膀子说:谁用谁知道!

315过去了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表