1 /* This module, and the entire Auto program, and the concept for
2  * interfacing this module to the Window Manager, are all original work
3  * by Robert Nation
4  *
5  * Converted for AfterStep by Frank Fejes
6  *
7  * Copyright 1994, Robert Nation. No guarantees or warantees or anything
8  * are provided or implied in any way whatsoever. Use this program at your
9  * own risk. Permission to use this program for any purpose is given,
10  * as long as the copyright is kept intact. */
11 
12 #define TRUE 1
13 #define FALSE
14 
15 #include "../../configure.h"
16 #ifdef ISC
17 #include <sys/bsdtypes.h> /* Saul */
18 #endif
19 
20 #include <stdio.h>
21 #include <signal.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <sys/wait.h>
25 #include <sys/time.h>
26 #if defined ___AIX || defined _AIX || defined __QNX__ || defined ___AIXV3 || defined AIXV3 || defined _SEQUENT_
27 #include <sys/select.h>
28 #endif
29 #include <unistd.h>
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include "../../afterstep/module.h"
33 #include "../../lib/aftersteplib.h"
34 #include "../../version.h"
35 
36 int fd_width;
37 int fd[2];
38 int timeout, t_secs, t_usecs;
39 
40 /*************************************************************************
41  *
42  * Subroutine Prototypes
43  *
44  *************************************************************************/
45 void Loop(int *fd);
46 void DeadPipe(int nonsense);
47 
48 #ifdef BROKEN_SUN_HEADERS
49 #include "../../afterstep/sun_headers.h"
50 #endif
51 
52 #ifdef NEEDS_ALPHA_HEADER
53 #include "../../afterstep/alpha_header.h"
54 #endif /* NEEDS_ALPHA_HEADER */
55 
56 /***********************************************************************
57  *
58  *  Procedure:
59  *	main - start of module
60  *
61  ***********************************************************************/
main(int argc,char ** argv)62 int main(int argc, char **argv)
63 {
64   FILE *file;
65   char mask_mesg[80];
66 
67   if(argc != 7)
68     {
69       fprintf(stderr,"Auto requires one argument.\n");
70       exit(1);
71     }
72 
73   /* Dead pipes mean afterstep died */
74   signal (SIGPIPE, DeadPipe);
75 
76   fd[0] = atoi(argv[1]);
77   fd[1] = atoi(argv[2]);
78 
79   timeout = atoi(argv[6]);
80   t_secs = timeout/1000;
81   t_usecs = (timeout - t_secs*1000)*1000;
82 
83   fd_width = GetFdWidth();
84   sprintf(mask_mesg,"SET_MASK %lu\n",(unsigned long)(M_FOCUS_CHANGE));
85   SendInfo(fd,mask_mesg,0);
86   Loop(fd);
87 }
88 
89 
90 /***********************************************************************
91  *
92  *  Procedure:
93  *	Loop - wait for data to process
94  *
95  ***********************************************************************/
96 unsigned long focus_win = 0;
97 
Loop(int * fd)98 void Loop(int *fd)
99 {
100   unsigned long header[HEADER_SIZE], *body;
101   fd_set in_fdset;
102   struct itimerval value;
103   int retval;
104   int Raised = 0;
105 
106   while(1)
107     {
108       FD_ZERO(&in_fdset);
109       FD_SET(fd[1],&in_fdset);
110 
111       /* set up a time-out, in case no packets arrive before we have to
112        * iconify something */
113       value.it_value.tv_usec = t_usecs;
114       value.it_value.tv_sec = t_secs;
115 
116 #ifdef __hpux
117       if((timeout > 0)&&(Raised == 0))
118 	retval=select(fd_width,(int *)&in_fdset, 0, 0, &value.it_value);
119       else
120 	retval=select(fd_width,(int *)&in_fdset, 0, 0, NULL);
121 #else
122       if((timeout > 0)&&(Raised == 0))
123 	retval=select(fd_width,&in_fdset, 0, 0, &value.it_value);
124       else
125 	retval=select(fd_width,&in_fdset, 0, 0, NULL);
126 #endif
127 
128       if(FD_ISSET(fd[1], &in_fdset))
129 	{
130 	  /* read a packet */
131 	  if(ReadASPacket(fd[1],header, &body) > 0)
132 	    {
133 	      if(header[1] == M_FOCUS_CHANGE)
134 		{
135 		  focus_win = body[0];
136 		  if(focus_win != 0)
137 		    Raised = 0;
138 		  if(timeout == 0)
139 		    {
140 		      if(focus_win != 0)
141 			SendInfo(fd,"Raise",focus_win);
142 		      Raised = 1;
143 		    }
144 		}
145 	      free(body);
146 	    }
147 	}
148       else
149 	{
150 	  /* Raise the current focus window */
151 	  Raised = 1;
152 	  if(focus_win != 0)
153 	    {
154 	      SendInfo(fd,"Raise",focus_win);
155 	    }
156 	}
157     }
158 }
159 
160 
161 
162 /***********************************************************************
163  *
164  *  Procedure:
165  *	SIGPIPE handler - SIGPIPE means afterstep is dying
166  *
167  ***********************************************************************/
DeadPipe(int nonsense)168 void DeadPipe(int nonsense)
169 {
170   exit(0);
171 }
172 
173