1 /*
2  *  main for mdgclock
3  *
4  *  RCS:
5  *      $Revision$
6  *      $Date$
7  *
8  *  Security:
9  *      Unclassified
10  *
11  *  Description:
12  *      text
13  *
14  *  Input Parameters:
15  *      type    identifier  description
16  *
17  *      text
18  *
19  *  Output Parameters:
20  *      type    identifier  description
21  *
22  *      text
23  *
24  *  Return Values:
25  *      value   description
26  *
27  *  Side Effects:
28  *      text
29  *
30  *  Limitations and Comments:
31  *      text
32  *
33  *  Development History:
34  *      who                 when        why
35  *      muquit@semcor.com   31-Aug-95   first cut
36  */
37 
38 #include "xhead.h"
39 
40 #define __Main__
41 #include "mxkill.h"
42 
43 #include "mxkicon.xbm"
44 
45 typedef struct
46 {
47     String
48         psCommand;
49 
50     String
51         psdefOption;
52 
53     String
54         psAllOption;
55 
56     String
57         psAllbyUserOption;
58 
59     String
60         psAllbyPidOption;
61 
62 } mresources, *mresourcesP;
63 
64 
65 #define moffset(x)   XtOffset(mresourcesP,x)
66 
67 static XtResource resources[]=
68 {
69     {"psCommand","psCommand",XtRString,sizeof(String),
70         moffset(psCommand),XtRString,"/bin/ps"},
71     {"psdefOption","psdefOption",XtRString,sizeof(String),
72         moffset(psdefOption),XtRString,"x"},
73     {"psAllOption","psAllOption",XtRString,sizeof(String),
74         moffset(psAllOption),XtRString,"aux"},
75     {"psAllbyUserOption","psAllbyUserOption",XtRString,sizeof(String),
76         moffset(psAllbyUserOption),XtRString,"aux|sort +0 -1"},
77     {"psAllbyPidOption","psAllbyPidOption",XtRString,sizeof(String),
78         moffset(psAllbyPidOption),XtRString,"aux|sort +0 -1"},
79 };
80 
81 /*
82 ** fallback resources
83 */
84 static char *app_defaults[]=
85 {
86 #include "MXkill.ad.h"
87     NULL
88 };
main(argc,argv)89 void main(argc,argv)
90 int
91     argc;
92 char
93     *argv[];
94 {
95 
96     XtAppContext
97         app;
98 
99     int
100         screen;
101 
102     int
103         i;
104 
105     Pixmap
106         iconpix;
107 
108     int
109         stderr_pipe[2];
110 
111     /*
112     ** initialize globals-----starts------
113     */
114 
115     GtoplevelW=(Widget) NULL;
116     Gdisplay=(Display *) NULL;
117     Gcolormap=(Colormap) NULL;
118     Gdepth=8;
119     Grootwindow=(Window) NULL;
120     Gwhitepixel=0;
121     Gblackpixel=1;
122     GdisplayWidth=0;
123     GdisplayHeight=0;
124     Gsignal_no=9;
125     for(i=0; i < 6; i++)
126         Gtoggle_menuW[i]=(Widget)NULL;
127 
128     *GpsCommand='\0';
129     *GpsdefOption='\0';
130     *GpsAllOption='\0';
131     *GpsAllbyUserOption='\0';
132     *GpsAllbyPidOption='\0';
133     Giconic_state=False;
134 
135 
136     /*
137     ** initialize globals-----ends------
138     */
139 
140     /*
141     ** create toplevelW
142     */
143 
144     XtSetLanguageProc (NULL, NULL, NULL);
145     GtoplevelW=XtVaAppInitialize(&app,"MXkill",
146         NULL,0,&argc,argv,app_defaults,NULL);
147 
148 
149     /*
150     ** add the event handler for detecting iconify/deiconify states
151     ** this time I'm doing with StructureNofify mask.
152     */
153     XtAddEventHandler(GtoplevelW,
154         StructureNotifyMask,
155         False,
156         (XtEventHandler) HandleIconicStates,
157         NULL);
158 
159     XtVaGetValues(GtoplevelW,
160         XmNiconic, &Giconic_state,
161         NULL);
162 
163     /*
164     ** set the forgiving error handler
165     */
166     XSetErrorHandler(MXError);
167     SetResources(GtoplevelW);
168 
169     Gdisplay=XtDisplay(GtoplevelW);
170     screen=XDefaultScreen(Gdisplay);
171     Gcolormap=XDefaultColormap(Gdisplay,screen);
172     Ggc=XDefaultGC(Gdisplay,screen);
173     Gblackpixel=BlackPixel(Gdisplay,screen);
174     Gwhitepixel=WhitePixel(Gdisplay,screen);
175     Grootwindow=XDefaultRootWindow(Gdisplay);
176     Gdepth=XDefaultDepth(Gdisplay,screen);
177     GdisplayWidth=XDisplayWidth(Gdisplay,screen);
178     GdisplayHeight=XDisplayHeight(Gdisplay,screen);
179 
180     /*
181     ** create the icon pixmap
182     */
183 
184     iconpix=XCreatePixmapFromBitmapData(Gdisplay,
185         Grootwindow,
186         mxkicon_bits,
187         mxkicon_width,
188         mxkicon_height,
189         Gblackpixel,
190         Gwhitepixel,
191         Gdepth);
192 
193     if (iconpix != (Pixmap) NULL)
194         XtVaSetValues(GtoplevelW,
195             XmNiconPixmap, iconpix,
196             NULL);
197 
198 
199     /*
200     ** create the UI
201     */
202     CreateMainWindow(GtoplevelW);
203     CreateAboutD(GtoplevelW);
204 
205     XtRealizeWidget(GtoplevelW);
206 
207     /*
208     ** show the first ps output with default command
209     */
210 /*    if (Giconic_state == False)
211         PsCb((Widget)NULL,(XtPointer)NULL,(XtPointer)NULL);*/
212 
213     /*
214     ** catch stderr and display the message in a dialog
215     */
216 
217     pipe(stderr_pipe);
218     dup2(stderr_pipe[1],2);
219     XtAppAddInput(app,stderr_pipe[0],
220         (XtPointer) XtInputReadMask,
221         (XtInputCallbackProc) DoStderr,
222         (XtPointer) stderr_pipe[0]);
223 
224     XtAppMainLoop(app);
225 }
226 
227 /*
228 ** load resources
229 */
SetResources(widget)230 void SetResources(widget)
231 Widget
232     widget;
233 {
234     mresources
235         app_resources;
236 
237     XtGetApplicationResources(widget,(XtPointer) &app_resources,
238         resources,XtNumber(resources),NULL,0);
239 
240     (void) sprintf(GpsCommand,"%s",app_resources.psCommand);
241     (void) sprintf(GpsdefOption,"%s",app_resources.psdefOption);
242     (void) sprintf(GpsAllOption,"%s",app_resources.psAllOption);
243     (void) sprintf(GpsAllbyUserOption,"%s",app_resources.psAllbyUserOption);
244     (void) sprintf(GpsAllbyPidOption,"%s",app_resources.psAllbyPidOption);
245 }
246 
mystrdup(s)247 char *mystrdup(s)
248   char *s;
249 {
250   char *t;
251 
252   if ( s == (char*)NULL )
253     return (char*)NULL;
254 
255   t=(char *) malloc(strlen(s)+1);
256   if (t == (char *) NULL)
257     return (char*)NULL;
258 
259   (void) strcpy(t,s);
260   return t;
261 }
262 
263 /*
264 ** Handle iconic states
265 */
266 
HandleIconicStates(widget,client_data,event)267 void HandleIconicStates(widget,client_data,event)
268 Widget
269     widget;
270 XtPointer
271     client_data;
272 XEvent
273     *event;
274 {
275    switch(event->type)
276    {
277         case MapNotify:
278         {
279             PsCb((Widget)NULL,(XtPointer)NULL,(XtPointer)NULL);
280             break;
281         }
282 
283         default:
284             break;
285    }
286 }
287 
DoStderr(client_data,source,id)288 void DoStderr(client_data,source,id)
289 XtPointer
290     client_data;
291 int
292     source;
293 XtInputId
294     *id;
295 {
296     int
297         fd=(int) client_data;
298 
299     char
300         buf[512];
301 
302     int
303         n;
304 
305     Arg
306         args[10];
307 
308     static Widget
309         messagedW = (Widget) NULL;
310 
311     XmString
312         xmstr;
313 
314     n=read(fd,buf,512);
315     buf[n]='\0';
316 
317     if (!n)
318         return;
319 
320     XBell(Gdisplay,0);
321 
322     if (messagedW == (Widget) NULL)
323     {
324         n=0;
325         XtSetArg(args[n],XmNtitle,"mxkill Error!"); n++;
326         XtSetArg(args[n],XmNdialogStyle,
327             XmDIALOG_PRIMARY_APPLICATION_MODAL);    n++;
328         messagedW=XmCreateErrorDialog(GtoplevelW,"Error",args,n);
329     }
330     xmstr=XmStringCreateLtoR(buf,XmFONTLIST_DEFAULT_TAG);
331     XtVaSetValues(messagedW,
332         XmNmessageString,xmstr,
333         NULL);
334     XmStringFree(xmstr);
335     XtManageChild(messagedW);
336     XtPopup(XtParent(messagedW),XtGrabNone);
337 }
338