home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1161 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  23.3 KB

  1. From: pd1h+@andrew.cmu.edu (Philip H. Dye)
  2. Newsgroups: alt.sources
  3. Subject: [c.s.wanted] Re: mouse interface for Sun
  4. Message-ID: <11671@stag.math.lsa.umich.edu>
  5. Date: 10 Apr 90 06:01:13 GMT
  6.  
  7. Archive-name: sun_mouse/10-Apr-90
  8. Original-posting-by: pd1h+@andrew.cmu.edu (Philip H. Dye)
  9. Original-subject: Re: mouse interface for Sun
  10. Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)
  11.  
  12. [This is an experimental alt.sources re-posting from the newsgroup(s)
  13. comp.sources.wanted. Comments on this service to emv@math.lsa.umich.edu 
  14. (Edward Vielmetti).]
  15.  
  16.  
  17. Some example code for direct access of the sun mouse.  It works
  18. fine here under SunOS 3.5 on Sun 3/60's with three button mice.
  19.  
  20. files:
  21.  
  22.     readme          - message header (this file)
  23.     sun_mouse.txt   - dicussion of sun mouse native byte stream
  24.     Makefile        - makefile to build mreset, mstat, and mtest
  25.     mreset.c        - reset the mouse fo no owner and native format
  26.     mstat.c         - show the current owner and format of the mouse
  27.     mtest.c         - show changes in the mouse buttons or position
  28.     sun_mouse.h     - header file for sun_mouse.c
  29.     sun_mouse.c     - access routines see sun_mouse.h or the examples
  30.                       for usage information.
  31.  
  32. The examples should be easy to understand.  The programs mreset, mstat,
  33. and mtest all have the same calling format.
  34.  
  35.     % <program>             - act on the default mouse device '/dev/mouse'
  36.     % <program> mouse ...   - act on each of the listed devices
  37.  
  38. The code was intended to be a simple example in response to a question
  39. from a net user.  For a more further examples of direct sun mouse see the
  40. source code for Carnegie Mellon's Andrew Window Manager, Chris Newman's
  41. niftyterm-console, or MGR.  (The sun X11 server source code uses the virtual
  42. user interface mode for the mouse device.)
  43.  
  44. Please send any questions/comments/criticism to me at one of the following
  45. email addresses.
  46.  
  47. ------------------------------------------------------------------------------
  48. | Philip H. Dye                   | Internet:   pd1h@andrew.cmu.edu          |
  49. | Physics & Mathematics           |     UUCP:   psuvax1!andrew.cmu.edu!pd1h  |
  50. | Carnegie Mellon University      |   Bitnet:   pd1h%andrew.cmu.edu@cmccvb   |
  51. |---------------------------------|------------------------------------------|
  52. | P.O. Box 190, CMU               | Network support of remote authentication |
  53. | Pittsburgh, PA 15213            | using any form of transmitted key is     |
  54. | (412) 683-3728                  | insecure.         - Random DOD Document  |
  55. ------------------------------------------------------------------------------
  56.  
  57. ------------------------------ cut here ------------------------------
  58.  
  59. : This is a shar archive.  Extract with sh, not csh.
  60. echo x - Makefile
  61. sed -e 's/^X//' > Makefile << '!Funky!Stuff!'
  62. X#
  63. X# Makefile for sun mouse examples
  64. X#
  65. X
  66. XCFLAGS = -g
  67. X
  68. XALL             = mstat mtest
  69. X
  70. Xall:        ${ALL}
  71. X
  72. X#----------------------------------------------------------------------
  73. X
  74. Xmstat_obj    = mstat.o
  75. X
  76. Xmstat:        ${mstat_obj}
  77. X        cc ${CFLAGS} -o $@ ${mstat_obj}
  78. X
  79. X#----------------------------------------------------------------------
  80. X
  81. Xmtest_obj       = mtest.o sun_mouse.o
  82. X
  83. Xmtest:        ${mtest_obj}
  84. X        cc ${CFLAGS} -o $@ ${mtest_obj}
  85. X
  86. X#----------------------------------------------------------------------
  87. X
  88. Xmstat.o:    sun_mouse.h mstat.c
  89. Xmtest.o:    sun_mouse.h mtest.c
  90. Xsun_mouse.o:    sun_mouse.h sun_mouse.c
  91. X
  92. X#----------------------------------------------------------------------
  93. X
  94. Xclean:
  95. X        rm *.o ${ALL} *.CKP *.BAK *~ a.out
  96. X
  97. X#----------------------------------------------------------------------
  98. !Funky!Stuff!
  99. echo x - mreset.c
  100. sed -e 's/^X//' > mreset.c << '!Funky!Stuff!'
  101. X/* file:  mreset.c */
  102. X/*
  103. X    Sun 3 mouse reset (tested on a Sun 3/60)
  104. X
  105. X    Philip H. Dye, 03 APR 1990                    phdye@cs.cmu.edu
  106. X
  107. X    P.O. Box 190, CMU             | Internet:   pd1h@andrew.cmu.edu
  108. X    Pittsburgh, PA 15213          |     UUCP:   psuvax1!andrew.cmu.edu!pd1h
  109. X    (412) 683-3728                |   Bitnet:   pd1h%andrew.cmu.edu@cmccvb
  110. X
  111. X    If you use most or all of this code please include the this notice.
  112. X*/
  113. X
  114. X#include <stdio.h>
  115. X
  116. X#include "sun_mouse.h"
  117. X
  118. Xvoid reset_mouse();
  119. X
  120. Xmain(argc,argv)
  121. X    int     argc;
  122. X    char    **argv;
  123. X{
  124. X    if (argc < 2)
  125. X        reset_mouse(M_DEFAULT_DEVICE);
  126. X    else    
  127. X        while (*(++argv))
  128. X            reset_mouse(*argv);
  129. X}
  130. X
  131. Xvoid reset_mouse(path)
  132. X    char    *path;
  133. X{
  134. X    int     fd;
  135. X
  136. X    fd = open(path, 0);
  137. X
  138. X    if ( fd < 0)
  139. X    {
  140. X        fprintf(stderr,"error opening mouse device '%s'\n",path);
  141. X        perror("open()");
  142. X        exit(1);
  143. X    }
  144. X
  145. X    if (m_reset(fd) < 0)
  146. X        m_perror("m_reset()");
  147. X
  148. X    close(fd);
  149. X}
  150. X
  151. X/* end of file mreset.c */
  152. !Funky!Stuff!
  153. echo x - mstat.c
  154. sed -e 's/^X//' > mstat.c << '!Funky!Stuff!'
  155. X/* file:  mstat.c */
  156. X/*
  157. X    Sun 3 mouse status (tested on a Sun 3/60)
  158. X
  159. X    Philip H. Dye, 03 APR 1990                    phdye@cs.cmu.edu
  160. X
  161. X    P.O. Box 190, CMU             | Internet:   pd1h@andrew.cmu.edu
  162. X    Pittsburgh, PA 15213          |     UUCP:   psuvax1!andrew.cmu.edu!pd1h
  163. X    (412) 683-3728                |   Bitnet:   pd1h%andrew.cmu.edu@cmccvb
  164. X
  165. X    If you use most or all of this code please include the this notice.
  166. X*/
  167. X
  168. X#include <stdio.h>
  169. X#include <errno.h>
  170. X#include <sgtty.h>
  171. X#include <sys/fcntl.h>
  172. X#include <sys/ioctl.h>
  173. X#include <sys/types.h>
  174. X#include <sys/time.h>
  175. X#include <sundev/vuid_event.h>
  176. X
  177. Xtypedef struct sgttyb sgtty;
  178. X
  179. X#include "sun_mouse.h"
  180. X
  181. Xmain(argc,argv)
  182. X    int     argc;
  183. X    char    **argv;
  184. X{
  185. X    if (argc < 2)
  186. X        mouse_status(M_DEFAULT_DEVICE);
  187. X    else    
  188. X        while (*(++argv))
  189. X            mouse_status(*argv);
  190. X}
  191. X
  192. Xmouse_status(path)
  193. X    char    *path;
  194. X{
  195. X    int     fd;
  196. X    sgtty   sg;
  197. X
  198. X    fd = open(M_DEFAULT_DEVICE, 0);
  199. X
  200. X    if ( fd >= 0)
  201. X    {
  202. X        int temp;
  203. X
  204. X        if (ioctl(fd,VUIDGFORMAT,&temp) < 0)
  205. X        {
  206. X            fprintf(stderr,"error fetching mouse byte stream temp\n");
  207. X            perror("ioctl()");
  208. X        }
  209. X        else
  210. X        {
  211. X            fputs("stream format =  ",stdout);
  212. X            switch (temp)
  213. X            {
  214. X                case VUID_NATIVE :
  215. X                    fputs("VUID_NATIVE\n",stdout);
  216. X                    break;
  217. X                case VUID_FIRM_EVENT :
  218. X                    fputs("VUI_FIRM_EVENT\n",stdout);
  219. X                    break;
  220. X                default:
  221. X                    printf("UNKNOWN (0x%X)\n",temp);
  222. X                    break;
  223. X            }
  224. X        }
  225. X
  226. X        if ((temp = fcntl(fd,F_GETOWN,NULL)) == -1)
  227. X        {
  228. X            fprintf(stderr,"error fetching owner\n");
  229. X            perror("fcntl()");
  230. X        }
  231. X        else
  232. X        {
  233. X            if (temp < 0)
  234. X                printf("     group id =  %d\n", -temp);
  235. X            else
  236. X                printf("   process id =  %d\n",temp);
  237. X        }
  238. X    }
  239. X}
  240. X
  241. X/* end of file mstat.c */
  242. !Funky!Stuff!
  243. echo x - mtest.c
  244. sed -e 's/^X//' > mtest.c << '!Funky!Stuff!'
  245. X/* file:  mtest.c */
  246. X/*
  247. X    test the sun mouse initialization and sampling
  248. X
  249. X    Philip H. Dye, 03 APR 1990                    phdye@cs.cmu.edu
  250. X
  251. X    P.O. Box 190, CMU             | Internet:   pd1h@andrew.cmu.edu
  252. X    Pittsburgh, PA 15213          |     UUCP:   psuvax1!andrew.cmu.edu!pd1h
  253. X    (412) 683-3728                |   Bitnet:   pd1h%andrew.cmu.edu@cmccvb
  254. X
  255. X    if use most or all of this code please include the this notice.
  256. X*/
  257. X
  258. X#include <stdio.h>
  259. X#include <sgtty.h>
  260. X
  261. Xtypedef struct sgttyb   sgtty;
  262. X
  263. X#include <errno.h>
  264. X
  265. Xextern int errno;
  266. X
  267. X#include <sys/fcntl.h>
  268. X#include <sys/ioctl.h>
  269. X
  270. X#include "sun_mouse.h"
  271. X
  272. Xstatic int    state    = 0;
  273. X
  274. X#define PRINT(state,f,s)        \
  275. X{                               \
  276. X    if (state)                  \
  277. X        fputs(", ",stdout);     \
  278. X                                \
  279. X    state++;                    \
  280. X                                \
  281. X    printf(f,s);                \
  282. X}
  283. X
  284. Xmain(argc,argv)
  285. X    int     argc;
  286. X    char    **argv;
  287. X{
  288. X    sgtty   old, new;
  289. X
  290. X    /* make stdin non-blocking */
  291. X
  292. X    if (fcntl(0,F_SETFL,FNDELAY) < 0)
  293. X    {
  294. X        fprintf(stderr,"unable to make stdin non-blocking\n");
  295. X        exit(1);
  296. X    }
  297. X
  298. X    if (ioctl(0, TIOCGETP, &old) < 0)
  299. X    {
  300. X        fprintf(stderr,"unable to get stdin's sgtty mode\n");
  301. X        perror("ioctl()");
  302. X        exit(1);
  303. X    }
  304. X
  305. X    new             = old;
  306. X    old.sg_flags    = (RAW | ANYP);
  307. X
  308. X    if (ioctl(0, TIOCSETP, &new) < 0)
  309. X    {
  310. X        fprintf(stderr,"unable to set stdin's sgtty mode\n");
  311. X        perror("ioctl()");
  312. X        exit(1);
  313. X    }
  314. X
  315. X    if (argc < 2)
  316. X        m_test(M_DEFAULT_DEVICE);
  317. X    else
  318. X        while (*(++argv))
  319. X            m_test(*argv);    
  320. X
  321. X    /* restore stdin sgtty mode */
  322. X
  323. X    if (ioctl(0, TIOCSETP, &old) < 0)
  324. X    {
  325. X        fprintf(stderr,"unable to restore stdin's sgtty mode\n");
  326. X        perror("ioctl()");
  327. X    }
  328. X
  329. X    if (ioctl(0,TIOCFLUSH,0) < 0)
  330. X    {
  331. X        fprintf(stderr,"unable to flush i/o buffers\n");
  332. X        perror("ioctl()");
  333. X    }
  334. X}
  335. X
  336. Xm_test(path)
  337. X    char    *path;
  338. X{
  339. X    int     fd, buttons, x, y;
  340. X    int        change, test, state;
  341. X    char    trash;
  342. X
  343. X    fd = open(path,0);
  344. X    if (fd < 0)
  345. X    {
  346. X        fprintf(stderr,"Error:  unable to open sun mouse '%s'\n",path);
  347. X        return(-1);
  348. X    }
  349. X
  350. X    if (m_init(fd) < 0)
  351. X    {
  352. X        fprintf(stderr,"Error:  unable to initialize sun mouse '%s'\n",path);
  353. X        return(-1);
  354. X    }
  355. X
  356. X    fprintf(stderr,"[sampling sun mouse device '%s']\n",path);
  357. X    fprintf(stderr,"[press any key to stop this program]\n");
  358. X
  359. X    buttons = m_buttons;
  360. X
  361. X    while ((read(0,&trash,1)==-1)&&(errno==EWOULDBLOCK)&&(m_sample(fd)>=0))
  362. X    {
  363. X        change = CHANGED(buttons,m_buttons);
  364. X        if (change)
  365. X        {
  366. X            if (change & M_RIGHT)
  367. X                PRINT(state,"right  %s", STRANSITION(M_RIGHT,change,buttons));
  368. X
  369. X            if (change & M_MIDDLE)
  370. X                PRINT(state,"middle %s", STRANSITION(M_MIDDLE,change,buttons));
  371. X
  372. X            if (change & M_LEFT)
  373. X                PRINT(state,"left   %s", STRANSITION(M_LEFT,change,buttons));
  374. X
  375. X            buttons = m_buttons;
  376. X        }
  377. X        if (m_x != 0)
  378. X        {
  379. X            PRINT(state,"dx = %d", (char*) m_x);
  380. X            m_x = 0;
  381. X        }
  382. X        if (m_y != 0)
  383. X        {
  384. X            PRINT(state,"dy = %d", (char*) m_y);
  385. X            m_y = 0;
  386. X        }
  387. X        if (state)
  388. X        {
  389. X            putchar('\n');
  390. X            state = 0;
  391. X        }
  392. X    }
  393. X    m_reset(fd);
  394. X    close(fd);
  395. X}
  396. X
  397. X/* end of file mtest.c */
  398. !Funky!Stuff!
  399. echo x - readme
  400. sed -e 's/^X//' > readme << '!Funky!Stuff!'
  401. XSome example code for direct access of the sun mouse.  It works
  402. Xfine here under SunOS 3.5 on Sun 3/60's with three button mice.
  403. X
  404. Xfiles:
  405. X
  406. X    readme          - message header (this file)
  407. X    sun_mouse.txt   - dicussion of sun mouse native byte stream
  408. X    Makefile        - makefile to build mreset, mstat, and mtest
  409. X    mreset.c        - reset the mouse fo no owner and native format
  410. X    mstat.c         - show the current owner and format of the mouse
  411. X    mtest.c         - show changes in the mouse buttons or position
  412. X    sun_mouse.h     - header file for sun_mouse.c
  413. X    sun_mouse.c     - access routines see sun_mouse.h or the examples
  414. X                      for usage information.
  415. X
  416. XThe examples should be easy to understand.  The programs mreset, mstat,
  417. Xand mtest all have the same calling format.
  418. X
  419. X    % <program>             - act on the default mouse device '/dev/mouse'
  420. X    % <program> mouse ...   - act on each of the listed devices
  421. X
  422. XThe code was intended to be a simple example in response to a question
  423. Xfrom a net user.  For a more further examples of direct sun mouse see the
  424. Xsource code for Carnegie Mellon's Andrew Window Manager, Chris Newman's
  425. Xniftyterm-console, or MGR.  (The sun X11 server source code uses the virtual
  426. Xuser interface mode for the mouse device.)
  427. X
  428. XPlease send any questions/comments/criticism to me at one of the following
  429. Xemail addresses.
  430. X
  431. X------------------------------------------------------------------------------
  432. X| Philip H. Dye                   | Internet:   pd1h@andrew.cmu.edu          |
  433. X| Physics & Mathematics           |     UUCP:   psuvax1!andrew.cmu.edu!pd1h  |
  434. X| Carnegie Mellon University      |   Bitnet:   pd1h%andrew.cmu.edu@cmccvb   |
  435. X|---------------------------------|------------------------------------------|
  436. X| P.O. Box 190, CMU               | Network support of remote authentication |
  437. X| Pittsburgh, PA 15213            | using any form of transmitted key is     |
  438. X| (412) 683-3728                  | insecure.         - Random DOD Document  |
  439. X------------------------------------------------------------------------------
  440. X
  441. X------------------------------ cut here ------------------------------
  442. !Funky!Stuff!
  443. echo x - sun_mouse.c
  444. sed -e 's/^X//' > sun_mouse.c << '!Funky!Stuff!'
  445. X/* sun_mouse.c */
  446. X/*
  447. X    Sun 3 mouse input (tested on a Sun 3/60)
  448. X
  449. X    Philip H. Dye, 03 APR 1990                    phdye@cs.cmu.edu
  450. X
  451. X    P.O. Box 190, CMU             | Internet:   pd1h@andrew.cmu.edu
  452. X    Pittsburgh, PA 15213          |     UUCP:   psuvax1!andrew.cmu.edu!pd1h
  453. X    (412) 683-3728                |   Bitnet:   pd1h%andrew.cmu.edu@cmccvb
  454. X
  455. X    If you use most or all of this code please include the this notice.
  456. X*/
  457. X
  458. X/* notes:
  459. X
  460. X    - should be split up into individual files for each function
  461. X
  462. X    - only the sun 3 button mouse is supported
  463. X
  464. X    - see sun_mouse.txt for information on decoding the sun mouse
  465. X      device native byte stream
  466. X*/
  467. X
  468. X#include <stdio.h>
  469. X#include <errno.h>
  470. X
  471. Xextern int errno;
  472. X
  473. X#include <sgtty.h>
  474. X
  475. Xtypedef struct sgttyb sgtty;
  476. X
  477. X#include <sys/fcntl.h>
  478. X#include <sys/ioctl.h>
  479. X#include <sys/types.h>
  480. X#include <sys/time.h>
  481. X#include <sundev/vuid_event.h>
  482. X
  483. X#include "sun_mouse.h"
  484. X
  485. Xm_codes m_error     = M_ERROR_NONE;
  486. Xchar    m_buttons   = 0;
  487. Xchar    m_x         = 0;
  488. Xchar    m_y         = 0;
  489. X
  490. Xtypedef struct
  491. X{
  492. X    char    *string;
  493. X    char    *proc;
  494. X} s_error;
  495. X
  496. Xstatic s_error m_strings[] =
  497. X{
  498. X    {"no error",NULL},
  499. X    {"invalid file descriptor",NULL},
  500. X    {"unable to get mouse stream format","ioctl()"},
  501. X    {"unable to set mouse stream format","ioctl()"},
  502. X    {"mouse byte stream format is not VUID_NATIVE",NULL},
  503. X    {"M_ERROR_IOCTL_TIOCSETP","?()"},
  504. X    {"M_ERROR_FCNTL_GETOWN","?()"},
  505. X    {"M_ERROR_FCNTL_SETOWN","?()"},
  506. X    {"M_ERROR_OWNER","?()"},
  507. X    {"M_ERROR_FCNTL_SETFL","?()"},
  508. X    {"M_ERROR_READ","?()"},
  509. X    {"M_ERROR_INPUT","?()"}
  510. X};
  511. X
  512. X/*  value = m_busy()    - see if the mouse device is in use
  513. X
  514. X    return value        = -1    - an error occurred, m_error will be non-zero
  515. X                           0    - device available
  516. X                           1    - device busy
  517. X*/
  518. Xint m_perror()
  519. X{
  520. X    s_error *p;
  521. X
  522. X    fprintf(stderr,"MOUSE ERROR:  %s\n",m_strings[(int)m_error].string);
  523. X
  524. X    if (m_strings[(int)m_error].proc)
  525. X        perror(m_strings[(int)m_error].proc);
  526. X
  527. X    return (1);
  528. X}
  529. X
  530. X/*  value = m_busy()    - see if the mouse device is in use
  531. X
  532. X    return value        = -1    - an error occurred, m_error will be non-zero
  533. X                           0    - device available
  534. X                           1    - device busy
  535. X*/
  536. Xint m_busy(fd)
  537. X    int fd;
  538. X{
  539. X    int temp;
  540. X
  541. X    if ( fd < 0)
  542. X    {
  543. X        m_error = M_ERROR_INVALID_FD;
  544. X        return (-1);
  545. X    }
  546. X
  547. X    /*    if the mouse device is not currently in VUID_NATIVE mode
  548. X        it is in use by another process
  549. X    */
  550. X    if (ioctl(fd,VUIDGFORMAT,&temp) < 0)
  551. X    {
  552. X        m_error = M_ERROR_IOCTL_VUIDGFORMAT;
  553. X        return (-1);
  554. X    }
  555. X
  556. X    if (temp != VUID_NATIVE)
  557. X    {
  558. X        m_error = M_ERROR_FORMAT;
  559. X        return (-1);
  560. X    }
  561. X
  562. X    /*    check the mouse device is owned by another process */
  563. X
  564. X    if ((temp = fcntl(fd,F_GETOWN)) == -1)
  565. X    {
  566. X        m_error = M_ERROR_FCNTL_GETOWN;
  567. X        return (-1);
  568. X    }
  569. X
  570. X    if (temp != 0)
  571. X    {
  572. X        m_error = M_ERROR_OWNER;
  573. X        return (1);
  574. X    }
  575. X
  576. X    return (0);
  577. X}
  578. X
  579. X/*
  580. X    value = m_reset()   - reset the mouse device
  581. X
  582. X    return value        = -1    - an error occurred, m_error will be non-zero
  583. X                           0    - device reset
  584. X*/
  585. Xint m_reset(fd)
  586. X    int fd;
  587. X{
  588. X    int temp = VUID_NATIVE;
  589. X
  590. X    if (ioctl(fd,VUIDSFORMAT,&temp) < 0)
  591. X    {
  592. X        m_error = M_ERROR_IOCTL_VUIDSFORMAT;
  593. X        return (-1);
  594. X    }
  595. X
  596. X    if ((temp = fcntl(fd,F_SETOWN,0)) == -1)
  597. X    {
  598. X        m_error = M_ERROR_FCNTL_SETOWN;
  599. X        return (-1);
  600. X    }
  601. X
  602. X    return (0);
  603. X}
  604. X
  605. X/*  value = m_init()    - intialize the mouse device
  606. X
  607. X    return value        = -1    - an error occurred, m_error will be non-zero
  608. X                           0    - it was initialized properly
  609. X*/
  610. Xint m_init(fd)
  611. X    int fd;
  612. X{
  613. X    sgtty   sg;
  614. X
  615. X    if ( fd < 0)
  616. X    {
  617. X        m_error = M_ERROR_INVALID_FD;
  618. X        return (-1);
  619. X    }
  620. X
  621. X    /* assume that the mouse is not in use by another process */
  622. X
  623. X    if (fcntl(fd,F_SETOWN,getpid()) < 0)
  624. X    {
  625. X        m_error = M_ERROR_FCNTL_SETOWN;
  626. X        return (-1);
  627. X    }
  628. X
  629. X    if (fcntl(fd,F_SETFL,FNDELAY) < 0)
  630. X    {
  631. X        m_error = M_ERROR_FCNTL_SETFL;
  632. X        return (-1);
  633. X    }
  634. X
  635. X    /*    - this is the baud rate used locally, other
  636. X          baud rates may be available
  637. X        - see /usr/include/sundev/msio.h for information
  638. X          on determining the device's speed limit
  639. X    */
  640. X    sg.sg_ispeed = sg.sg_ospeed = 9;    /* 1200 Baud */
  641. X
  642. X    sg.sg_flags     = (RAW | ANYP);
  643. X
  644. X    if (ioctl(fd, TIOCSETP, &sg) < 0)
  645. X    {
  646. X        m_error = M_ERROR_IOCTL_TIOCSETP;
  647. X        return (-1);
  648. X    }
  649. X
  650. X    return (0);
  651. X}
  652. X
  653. X/*
  654. X    value = m_sample()      - sample a mouse device
  655. X
  656. X    returns value;          -1      -> error, m_Error will be non-zero
  657. X                             0      -> no packets available
  658. X                             1      -> packet was available and one read
  659. X*/
  660. Xint m_sample(fd)
  661. X    int fd;
  662. X{
  663. X    static char buffer[M_BUFFER_SIZE];
  664. X    static int  items = 0;
  665. X    static char *p;
  666. X
  667. X    if ( fd < 0)
  668. X    {
  669. X        m_error = M_ERROR_INVALID_FD;
  670. X        return (-1);
  671. X    }
  672. X
  673. X    if (items == 0)
  674. X    {
  675. X        items = read(fd, buffer, sizeof(buffer));
  676. X        if (items < 0)
  677. X        {
  678. X            if (errno == EWOULDBLOCK)
  679. X            {
  680. X                items = 0;
  681. X                return (0);
  682. X            }
  683. X
  684. X            m_error = M_ERROR_READ;
  685. X            return (-1);
  686. X        }
  687. X        /*
  688. X            - the i/o buffer should be flushed before more
  689. X              data is read as it may have partial packets
  690. X              remaining
  691. X        */
  692. X        if ( (items % M_PACKET_SIZE) != 0)
  693. X        {
  694. X            m_error = M_ERROR_INPUT;
  695. X            return (-1);
  696. X        }
  697. X        p = buffer;
  698. X    }
  699. X
  700. X    if (items > 0)
  701. X    {
  702. X        register incr;
  703. X
  704. X        m_buttons   = M_BUTTON(*(p++));
  705. X        m_x         = *(p++);
  706. X        m_y            = *(p++);
  707. X
  708. X        items  -= M_PACKET_SIZE;
  709. X
  710. X        return (1);
  711. X    }
  712. X
  713. X    return (0);
  714. X}
  715. X
  716. X/* end of file mouse.c */
  717. !Funky!Stuff!
  718. echo x - sun_mouse.h
  719. sed -e 's/^X//' > sun_mouse.h << '!Funky!Stuff!'
  720. X/* file:  sun_mouse.h */
  721. X/*
  722. X    Sun 3 mouse input (tested on a Sun 3/60)
  723. X
  724. X    Philip H. Dye, 03 APR 1990                    phdye@cs.cmu.edu
  725. X
  726. X    P.O. Box 190, CMU             | Internet:   pd1h@andrew.cmu.edu
  727. X    Pittsburgh, PA 15213          |     UUCP:   psuvax1!andrew.cmu.edu!pd1h
  728. X    (412) 683-3728                |   Bitnet:   pd1h%andrew.cmu.edu@cmccvb
  729. X
  730. X    If you use most or all of this code please include the this notice.
  731. X*/
  732. X
  733. X/* -------------------- standard defines -------------------- */
  734. X
  735. X#ifndef FALSE
  736. X#define FALSE    0
  737. X#define TRUE    1
  738. X#endif TRUE
  739. X
  740. X/* -------------------- miscellaneous -------------------- */
  741. X
  742. X#define M_DEFAULT_DEVICE    "/dev/mouse"
  743. X
  744. X/*
  745. X    The buffer size should be a factor of M_PACKET_SIZE since the mouse data
  746. X    is encoded in a M_PACKET_SIZE bytes
  747. X*/
  748. X
  749. X#define M_PACKET_SIZE        3
  750. X#define M_BUFFER_SIZE       (M_PACKET_SIZE * 20)
  751. X
  752. X/* -------------------- errors -------------------- */
  753. X
  754. Xtypedef enum
  755. X{
  756. X    M_ERROR_NONE,
  757. X    M_ERROR_INVALID_FD,
  758. X    M_ERROR_IOCTL_VUIDGFORMAT,
  759. X    M_ERROR_IOCTL_VUIDSFORMAT,
  760. X    M_ERROR_FORMAT,
  761. X    M_ERROR_IOCTL_TIOCSETP,
  762. X    M_ERROR_FCNTL_GETOWN,
  763. X    M_ERROR_FCNTL_SETOWN,
  764. X    M_ERROR_OWNER,
  765. X    M_ERROR_FCNTL_SETFL,
  766. X    M_ERROR_READ,
  767. X    M_ERROR_INPUT,
  768. X} m_codes;
  769. X
  770. Xextern m_codes m_error;            /* last error encountered */
  771. X
  772. X/* -------------------- change of position -------------------- */
  773. X
  774. Xextern char m_x;
  775. Xextern char m_y;
  776. X
  777. X/* -------------------- three button mouse -------------------- */
  778. X
  779. Xextern char m_buttons;                    /* buttons raised (bit 0 left, etc) */
  780. X
  781. X/* for a three button mouse:  invert & mask three bits */
  782. X
  783. X#define M_BUTTON(x)            (~(x) & 0x07)
  784. X#define M_LEFT                0x01
  785. X#define M_MIDDLE            0x02
  786. X#define M_RIGHT                0x04
  787. X#define CHANGED(old,new)                ( (old) ^ (new) )   /* -> change */
  788. X#define TRANSITION(button,change,old)    ( (change) & (buttons) & (old) )
  789. X#define STRANSITION(button,change,old)    ( TRANSITION(button,change,old)    ? \
  790. X                                            "up" : "down" )
  791. X
  792. X/* -------------------- FUNCTIONS -------------------- */
  793. X
  794. Xint m_busy();
  795. X/*
  796. X    arguements:     int fd;         - file descriptor for mouse open
  797. mouse device
  798. X
  799. X    return value:   int result;     error                   -> -1
  800. X                                    mouse device busy        -> TRUE
  801. X                                    mouse device free        -> FALSE
  802. X*/
  803. X
  804. Xint m_init();
  805. X/*
  806. X    arguements:     int fd;         - file descriptor for mouse open
  807. mouse device
  808. X
  809. X    return value:   int fd;         (fd >= 0)    -> file descriptor for mouse
  810. X                                    (fd == -1)    -> error
  811. X*/
  812. X
  813. Xint m_simple();
  814. X/*
  815. X    arguements:     int fd;         - file descriptor for mouse open
  816. mouse device
  817. X
  818. X    return value:   int result;     no events available        -> FALSE
  819. X                                    events found            -> TRUE
  820. X                                    error                    -> -1
  821. X*/
  822. X
  823. X/* end of file sun_mouse.h */
  824. !Funky!Stuff!
  825. echo x - sun_mouse.txt
  826. sed -e 's/^X//' > sun_mouse.txt << '!Funky!Stuff!'
  827. XThe following information is valid for SunOS 3.5 on Sun 3/50 and 3/60's.
  828. XIt may or may not be valid for other configurations.
  829. X(Though I believe that is should be valid.)
  830. X
  831. XPhilip H. Dye, 04 APR 1990
  832. X
  833. X----------------------------------------------------------------------
  834. X
  835. XThe SunOS Kernal Mouse driver has two byte stream formats:
  836. X
  837. X    Native :    - character stream
  838. X                - mouse information comes in three character packets
  839. X                  (to be safe, flush input buffer before begining i/o)
  840. X
  841. X        char 0    = position data (buttons:  0 = down, 1 = up)
  842. X
  843. X                bit      0   : right button
  844. X                         1   : middle button
  845. X                         2   : left button
  846. X                        3-6  : 0
  847. X                         7   : 1
  848. X
  849. X        char 1  : x position change (signed value, delta x)
  850. X
  851. X        char 2  : y position change (signed value, delta y)
  852. X
  853. X
  854. X        * The deltas are signed quantities, with the mouse pad in front of
  855. X          you, moving the mouse left or toward you on the mousepad generates
  856. X          a negetive value, moving right or away from you generates a
  857. X          positive value.          
  858. X
  859. X
  860. X        * Client software normally multplies a constant by the deltas
  861. X          to determine the resolution of the deltas.
  862. X
  863. X    Virtual User Interface mode:
  864. X
  865. X        Used by all Sun Systems Graphics Applications (I believe) including
  866. X        the X11 server.  This multiplexes all user i/o over a single stream.
  867. X
  868. X----------------------------------------------------------------------
  869. X
  870. XSoftware which accesses the mouse device directly should be well behaved
  871. Xand determine if the 
  872. X
  873. XWhen allocating the workstation mouse one should always save the current
  874. Xstate of the mouse and restore it when done.  A process may set an
  875. Xexeclusive lock on the mouse to prevent other processes from changing
  876. Xthe state of the mouse device.
  877. X
  878. XOne might also only use it if the it is in native mode and owned by root.
  879. XThen place an exclusive lock on it, change the owner, and remove the 
  880. Xexclusive lock.  
  881. X
  882. X    - mouse's owner
  883. X    - sgttyb structure
  884. X    - byte stream format    (See /usr/include/sundev/vuid_event.h)
  885. X
  886. X----------------------------------------------------------------------
  887. X
  888. XImplemenations:
  889. X
  890. X    suntools    - uses VUI calls of course
  891. X
  892. X    X11            - the X11 server written at Sun Systems uses VUI calls
  893. X
  894. X    mgr            - provides a very general sun mouse support sampler
  895. X                  allowing up to five buttons (native byte stream)
  896. X                - makes the current process the 
  897. X
  898. X    wm            - Carnegie Mellon University's Andrew Window Manager
  899. X                  uses the native byte stream
  900. X
  901. X    this        - this example uses the native byte stream
  902. X
  903. X----------------------------------------------------------------------
  904. !Funky!Stuff!
  905. exit
  906.