2 /* This module, and the entire FvwmSaveDesktop program, and the concept for
3 * interfacing this module to the Window Manager, are all original work
4 * by Robert Nation and Mr. Per Persson <pp@solace.mh.se>
6 * Copyright 1994, Robert Nation and Mr. Per Persson.
7 * 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. */
12 /* This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include "libs/ftime.h"
37 #include <X11/Xutil.h>
38 #include <X11/Xproto.h>
39 #include <X11/Xatom.h>
40 #include <X11/Intrinsic.h>
42 #include "libs/Module.h"
46 static ModuleArgs
*module
;
49 struct list
*list_root
= NULL
;
51 Display
*dpy
; /* which display are we talking to */
52 int ScreenWidth
, ScreenHeight
;
60 * main - start of module
63 int main(int argc
, char **argv
)
65 char *display_name
= NULL
;
67 module
= ParseModuleArgs(argc
,argv
,0); /* don't accept an alias */
70 fprintf(stderr
,"FvwmSave Version %s should only be executed by fvwm!\n",
75 /* Open the X display */
76 if (!(dpy
= XOpenDisplay(display_name
)))
78 fprintf(stderr
,"%s: can't open display %s", module
->name
,
79 XDisplayName(display_name
));
82 screen
= DefaultScreen(dpy
);
83 ScreenHeight
= DisplayHeight(dpy
,screen
);
84 ScreenWidth
= DisplayWidth(dpy
,screen
);
86 /* We should exit if our fvwm pipes die */
87 signal (SIGPIPE
, DeadPipe
);
89 fd
[0] = module
->to_fvwm
;
90 fd
[1] = module
->from_fvwm
;
92 /* Create a list of all windows */
93 /* Request a list of all windows,
94 * wait for ConfigureWindow packets */
95 SendInfo(fd
,"Send_WindowList",0);
97 /* tell fvwm we're running */
98 SendFinishedStartupNotification(fd
);
108 * Loop - wait for data to process
114 FvwmPacket
* packet
= ReadFvwmPacket(fd
[1]);
115 if ( packet
== NULL
)
118 process_message( packet
->type
, packet
->body
);
126 * Process message - examines packet types, and takes appropriate action
129 void process_message(unsigned long type
,unsigned long *body
)
133 case M_CONFIGURE_WINDOW
:
134 if(!find_window(body
[0]))
135 add_window(body
[0],body
);
140 case M_END_WINDOWLIST
:
155 * find_window - find a window in the current window list
158 struct list
*find_window(unsigned long id
)
162 if(list_root
== NULL
)
165 for(l
= list_root
; l
!= NULL
; l
= l
->next
)
178 * add_window - add a new window in the current window list
181 void add_window(unsigned long new_win
, unsigned long *body
)
184 struct ConfigWinPacket
*cfgpacket
= (void *)body
;
189 t
= (struct list
*)safemalloc(sizeof(struct list
));
192 t
->frame_height
= cfgpacket
->frame_height
;
193 t
->frame_width
= cfgpacket
->frame_width
;
194 t
->base_width
= cfgpacket
->hints_base_width
;
195 t
->base_height
= cfgpacket
->hints_base_height
;
196 t
->width_inc
= cfgpacket
->hints_width_inc
;
197 t
->height_inc
= cfgpacket
->hints_height_inc
;
198 t
->frame_x
= cfgpacket
->frame_x
;
199 t
->frame_y
= cfgpacket
->frame_y
;
200 t
->title_height
= cfgpacket
->title_height
;
201 t
->boundary_width
= cfgpacket
->border_width
;
202 memcpy(&(t
->flags
), &(cfgpacket
->flags
), sizeof(t
->flags
));
203 t
->gravity
= cfgpacket
->hints_win_gravity
;
212 * list_new_page - capture new-page info
215 void list_new_page(unsigned long *body
)
223 * SIGPIPE handler - SIGPIPE means fvwm is dying
226 RETSIGTYPE
DeadPipe(int nonsense
)
236 * writes a command line argument to file "out"
237 * checks for qoutes and stuff
240 void write_string(FILE *out
, char *line
)
242 int len
,space
= 0, qoute
= 0,i
;
248 if(isspace((unsigned char)(line
[i
])))
256 fprintf(out
,"%s",line
);
264 fprintf(out
,"%c",line
[i
]);
278 * checks to see if we are supposed to take some action now,
279 * finds time for next action to be performed.
285 char tname
[200],loc
[30];
288 int dwidth
,dheight
,xtermline
= 0;
289 int x1
,x2
,y1
,y2
,i
,command_count
;
292 sprintf(tname
, "%s/new.xinitrc", getenv( "HOME" ) );
293 out
= fopen( tname
, "w+" );
294 for (t
= list_root
; t
!= NULL
; t
= t
->next
)
299 x2
= ScreenWidth
- x1
- t
->frame_width
- 2;
303 y2
= ScreenHeight
- y1
- t
->frame_height
- 2;
306 dheight
= t
->frame_height
- t
->title_height
- 2*t
->boundary_width
;
307 dwidth
= t
->frame_width
- 2*t
->boundary_width
;
308 dwidth
-= t
->base_width
;
309 dheight
-= t
->base_height
;
310 dwidth
/= t
->width_inc
;
311 dheight
/= t
->height_inc
;
313 if (IS_STICKY_ACROSS_PAGES(t
))
323 sprintf(tname
,"%dx%d",dwidth
,dheight
);
324 if ((t
->gravity
== EastGravity
) ||
325 (t
->gravity
== NorthEastGravity
) ||
326 (t
->gravity
== SouthEastGravity
))
327 sprintf(loc
,"-%d",x2
);
329 sprintf(loc
,"+%d",x1
+(int)tVx
);
332 if((t
->gravity
== SouthGravity
)||
333 (t
->gravity
== SouthEastGravity
)||
334 (t
->gravity
== SouthWestGravity
))
335 sprintf(loc
,"-%d",y2
);
337 sprintf(loc
,"+%d",y1
+(int)tVy
);
340 if ( XGetCommand( dpy
, t
->id
, &command_list
, &command_count
) )
342 for (i
=0; i
< command_count
; i
++)
344 if ( strncmp( "-geo", command_list
[i
], 4) == 0)
349 if ( strncmp( "-ic", command_list
[i
], 3) == 0)
351 if ( strncmp( "-display", command_list
[i
], 8) == 0)
356 write_string(out
,command_list
[i
]);
357 if(strstr(command_list
[i
], "xterm"))
359 fprintf( out
, "-geometry %s ", tname
);
361 fprintf(out
, "-ic ");
365 if ( command_count
> 0 )
367 if ( xtermline
== 0 )
370 fprintf(out
, "-ic ");
371 fprintf( out
, "-geometry %s &\n", tname
);
375 fprintf( out
, "&\n");
378 XFreeStringList( command_list
);
382 fprintf(out
, "fvwm\n");