File: Auto.c

package info (click to toggle)
asclassic 1.1b-26
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,828 kB
  • ctags: 2,019
  • sloc: ansic: 21,403; makefile: 407; sh: 276
file content (172 lines) | stat: -rw-r--r-- 4,013 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* This module, and the entire Auto program, and the concept for
 * interfacing this module to the Window Manager, are all original work
 * by Robert Nation
 *
 * Converted for AfterStep by Frank Fejes
 *
 * Copyright 1994, Robert Nation. No guarantees or warantees or anything
 * are provided or implied in any way whatsoever. Use this program at your
 * own risk. Permission to use this program for any purpose is given,
 * as long as the copyright is kept intact. */

#define TRUE 1
#define FALSE 

#include "../../configure.h"
#ifdef ISC
#include <sys/bsdtypes.h> /* Saul */
#endif 

#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/time.h>
#if defined ___AIX || defined _AIX || defined __QNX__ || defined ___AIXV3 || defined AIXV3 || defined _SEQUENT_
#include <sys/select.h>
#endif
#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#include "../../afterstep/module.h"
#include "../../lib/aftersteplib.h"     
#include "../../version.h"

int fd_width;
int fd[2];
int timeout, t_secs, t_usecs;

/*************************************************************************
 *
 * Subroutine Prototypes
 * 
 *************************************************************************/
void Loop(int *fd);
void DeadPipe(int nonsense);

#ifdef BROKEN_SUN_HEADERS
#include "../../afterstep/sun_headers.h"
#endif

#ifdef NEEDS_ALPHA_HEADER
#include "../../afterstep/alpha_header.h"
#endif /* NEEDS_ALPHA_HEADER */

/***********************************************************************
 *
 *  Procedure:
 *	main - start of module
 *
 ***********************************************************************/
void main(int argc, char **argv)
{
  FILE *file;
  char mask_mesg[80];

  if(argc != 7)
    {
      fprintf(stderr,"Auto requires one argument.\n");
      exit(1);
    }

  /* Dead pipes mean afterstep died */
  signal (SIGPIPE, DeadPipe);  
  
  fd[0] = atoi(argv[1]);
  fd[1] = atoi(argv[2]);

  timeout = atoi(argv[6]);
  t_secs = timeout/1000;
  t_usecs = (timeout - t_secs*1000)*1000;

  fd_width = GetFdWidth();
  sprintf(mask_mesg,"SET_MASK %lu\n",(unsigned long)(M_FOCUS_CHANGE));
  SendInfo(fd,mask_mesg,0);
  Loop(fd);
}


/***********************************************************************
 *
 *  Procedure:
 *	Loop - wait for data to process
 *
 ***********************************************************************/
unsigned long focus_win = 0;

void Loop(int *fd)
{
  unsigned long header[HEADER_SIZE], *body;
  fd_set in_fdset;
  struct itimerval value;
  int retval;
  int Raised = 0;

  while(1)
    {
      FD_ZERO(&in_fdset);
      FD_SET(fd[1],&in_fdset);

      /* set up a time-out, in case no packets arrive before we have to
       * iconify something */
      value.it_value.tv_usec = t_usecs;
      value.it_value.tv_sec = t_secs;

#ifdef __hpux
      if((timeout > 0)&&(Raised == 0))
	retval=select(fd_width,(int *)&in_fdset, 0, 0, &value.it_value);
      else
	retval=select(fd_width,(int *)&in_fdset, 0, 0, NULL);  
#else      
      if((timeout > 0)&&(Raised == 0))
	retval=select(fd_width,&in_fdset, 0, 0, &value.it_value);
      else
	retval=select(fd_width,&in_fdset, 0, 0, NULL);  
#endif

      if(FD_ISSET(fd[1], &in_fdset))
	{
	  /* read a packet */
	  if(ReadASPacket(fd[1],header, &body) > 0)
	    {
	      if(header[1] == M_FOCUS_CHANGE)
		{
		  focus_win = body[0];
		  if(focus_win != 0)
		    Raised = 0;
		  if(timeout == 0)
		    {
		      if(focus_win != 0)
			SendInfo(fd,"Raise",focus_win);		    
		      Raised = 1;
		    }
		}
	      free(body);
	    }
	}
      else
	{
	  /* Raise the current focus window */
	  Raised = 1;
	  if(focus_win != 0)
	    {
	      SendInfo(fd,"Raise",focus_win);
	    }
	}
    }
}



/***********************************************************************
 *
 *  Procedure:
 *	SIGPIPE handler - SIGPIPE means afterstep is dying
 *
 ***********************************************************************/
void DeadPipe(int nonsense)
{
  exit(0);
}