File: ascdc.c

package info (click to toggle)
ascdc 0.3-17
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 172 kB
  • sloc: makefile: 444; ansic: 350
file content (486 lines) | stat: -rw-r--r-- 12,746 bytes parent folder | download | duplicates (3)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*****************************************************************************/
/*	ascdc - AfterStep CD Changer				             */
/*	Version .3						             */
/*	By Rob Malda							     */		
/*	malda@cs.hope.edu						     */
/*	http://www.cs.hope.edu/~malda/					     */
/*									     */
/*	This is an 'AfterStep Look & Feel' Wharf style applet that can be    */
/*	used to control an IDE CD Changer.				     */
/*									     */
/*	It works wonderfully with my NEC 4x4 Changer			     */
/*									     */
/*	Things to do:							     */
/*	- Detect what CD is currently active.				     */
/*	- Mounting & Unmounting. (Sorta Works)				     */
/*	- Autolaunch CD Player	 (Sorta Works)				     */
/*****************************************************************************/

#include <stdio.h>
#include <stdlib.h>       
#include <string.h>

#include <X11/Xlib.h>
#include <X11/xpm.h>
#include <X11/extensions/shape.h>

#include <errno.h>
#include <unistd.h>
#include <linux/cdrom.h>
/* #include <linux/fs.h>   */                         

#include <sys/mount.h>


  
/* XPM struct and icons ******************************************************/
typedef struct _XpmIcon {
	Pixmap pixmap;
	Pixmap mask;
	XpmAttributes attributes;
} XpmIcon;

XpmIcon cd0XPM, cd1XPM, cd2XPM, cd3XPM, CurrentXPM;
Pixmap	CDMask;


#include "XPM/cd01.xpm"
#include "XPM/cd02.xpm"
#include "XPM/cd03.xpm"
#include "XPM/cd04.xpm"
#include "XPM/cdmask.xbm"

/* Functions *****************************************************************/
void	Help(void);
void	CreateWindow(void);
void	ParseCmdLine(int argc, char *argv[]);
void	MainLoop();
void	GetXPM(void);
int	FlushExpose(Window w);
void	RedrawWindow( XpmIcon *v);
Pixel	GetColor(char *name);
void	swapcd(int newslot);



/* Global stuff **************************************************************/
#define TRUE 1;
#define FALSE 0;
#define MAXSLOT 3;
#define DEFAULTCD 3;
#define DEFAULTDEVICE "/dev/hdd";


Display	*Disp;	 
Window	Root;
Window	Iconwin;
Window	Win;
char	*Geometry = 0;
char    device[128]=DEFAULTDEVICE;

char	cdplayer[128]="/usr/X11R6/bin/xmcd";
int	autoplay=FALSE

char    mountdir[128]="";
int	mounton=FALSE;

int	slot=0;
int	maxslot=MAXSLOT;
int     withdrawn=FALSE;
GC	WinGC;
int     CarrierOn = FALSE;

/*****************************************************************************/
int main(int argc,char *argv[])
{       	
	ParseCmdLine(argc, argv);      
	CreateWindow();
        XSetCommand(Disp,Win,argv,argc); 
	MainLoop();
	return 0;
}







void swapcd(int newslot)
{
        int fd;           /* file descriptor for CD-ROM device */
        int status=0;       /* return status for system calls */
        


	if(newslot == slot)
		return;
	slot=newslot;

        /* open device */
        fd = open (device, 0);
     
        if(mounton) umount(device);

        if (fd < 0) {
                fprintf (stderr, "ascdc: open failed for `%s': %s\n",
                         device, strerror (errno));
        }                       

       /* load */

	if (ioctl (fd, CDROM_CHANGER_NSLOTS) < 0)
	  status += ioctl (fd, CDROM_SELECT_DISC, slot);
	else
	  status += ioctl (fd, CDROM_SELECT_DISC, slot) < 0;

        if (status != 0) {
                fprintf (stderr,
                         "%s: CDROM_SELECT_DISC ioctl failed for ascdc: %s\n",
                         device, strerror (errno));
        }

        /* close device */
        status += close (fd);
        if (status != 0) {
                fprintf (stderr, "ascdc: close failed for `%s': %s\n",
                         device, strerror (errno));
			 }

	if(status==0)
	{
		if(slot==0) CurrentXPM=cd0XPM;
        	if(slot==1) CurrentXPM=cd1XPM;	
        	if(slot==2) CurrentXPM=cd2XPM;
		if(slot==3) CurrentXPM=cd3XPM;
                RedrawWindow(&CurrentXPM); 	
		
		if(mounton) 
                  {  status=mount(device,mountdir,"iso9660", 0xC0ED0001,0); 
                     fprintf(stderr, "errno %i Mounter %s\n",errno, mountdir);
		     if(status!=0) 
			{  
 			   if(autoplay)
			   system(cdplayer);
			}
		      else fprintf(stderr, "errno %i Mounter %s\n",errno, mountdir);
		  } 

	}




}                          







/*****************************************************************************/
void Help()
{       
	fprintf(stderr,"ascdc - Version 0.3\n");
	fprintf(stderr,"by Rob Malda - malda@cs.hope.edu\n");
	fprintf(stderr,"http://www.cs.hope.edu/~malda/\n");
	fprintf(stderr,"usage:  ascdc [-options ...] \n");
	fprintf(stderr,"options:\n");
	fprintf(stderr,"\n");       
	fprintf(stderr,"  Command                 Default   Why\n");
	fprintf(stderr,"g Geometry                none      Standard X Location\n");	
	fprintf(stderr,"d Device                  /dev/hdd  IDE Device of the Changer\n");
	fprintf(stderr,"c cdplayer                none	    Location of program to launch for audio CDs.\n");
	fprintf(stderr,"n Number to set now	  none	    If set, switches to slot 0 on launch.\n");
	fprintf(stderr,"m Mount/Umount Directory  none      If desired umounts & mounts\n");
	fprintf(stderr,"                                     eg /cdrom or /mnt\n");
        fprintf(stderr,"w Withdrawn               none      Starts window in Withdrawn State (for WindowMaker)");
	fprintf(stderr,"\n\n");
	exit(1); 
}

/****************************************************************************/
void CreateWindow(void)
{
	int i;
	unsigned int borderwidth ;
	char *display_name = NULL; 
	char *wname = "ascdc";
	XGCValues gcv;
	unsigned long gcm;
	XTextProperty name;
	Pixel back_pix, fore_pix;
	Pixmap pixmask;
	int screen;	
	int x_fd;
	int d_depth;
	int ScreenWidth, ScreenHeight;
	XSizeHints SizeHints;
	XWMHints WmHints;
        XClassHint classHint; 
	
	/* Open display */
	if (!(Disp = XOpenDisplay(display_name)))  
	{ 
		fprintf(stderr,"ascdc: can't open display %s\n", XDisplayName(display_name));    
		exit (1); 
	} 
	
	screen = DefaultScreen(Disp);
	Root = RootWindow(Disp, screen);
	d_depth = DefaultDepth(Disp, screen);
	x_fd = XConnectionNumber(Disp);	
	ScreenHeight = DisplayHeight(Disp,screen);
	ScreenWidth = DisplayWidth(Disp,screen);
	       
	GetXPM();
		
	SizeHints.flags= USSize|USPosition;
	SizeHints.x = 0;
	SizeHints.y = 0;	
	back_pix = GetColor("white");
	fore_pix = GetColor("black");
	
	XWMGeometry(Disp, screen, Geometry, NULL, (borderwidth =1), &SizeHints,
		    &SizeHints.x,&SizeHints.y,&SizeHints.width,
		    &SizeHints.height, &i); 	
	
	SizeHints.width = cd0XPM.attributes.width;
	SizeHints.height= cd0XPM.attributes.height;	
	Win = XCreateSimpleWindow(Disp,Root,SizeHints.x,SizeHints.y,
				  SizeHints.width,SizeHints.height,
				  borderwidth,fore_pix,back_pix);
	Iconwin = XCreateSimpleWindow(Disp,Win,SizeHints.x,SizeHints.y,
				      SizeHints.width,SizeHints.height,
				      borderwidth,fore_pix,back_pix);	     


        classHint.res_name =  "ascdc";
        classHint.res_class = "ASCdc";
        XSetClassHint(Disp, Win, &classHint);    




	XSetWMNormalHints(Disp, Win, &SizeHints);
	XSelectInput(Disp, Win, (ExposureMask | ButtonPressMask | 
				 StructureNotifyMask));
	XSelectInput(Disp, Iconwin, (ExposureMask | ButtonPressMask | 
				     StructureNotifyMask));
	
	if (XStringListToTextProperty(&wname, 1, &name) ==0) 
	{
		fprintf(stderr, "ascdc: can't allocate window name\n");
		exit(-1);
	}
	XSetWMName(Disp, Win, &name);
	
	/* Create WinGC */
	gcm = GCForeground|GCBackground|GCGraphicsExposures;
	gcv.foreground = fore_pix;
	gcv.background = back_pix;
	gcv.graphics_exposures = False;
	WinGC = XCreateGC(Disp, Root, gcm, &gcv);  

	 
	CDMask = XCreateBitmapFromData(Disp, Win, 
						cdmask_bits, 
						cdmask_width, 
						cdmask_height);		
		
		XShapeCombineMask(Disp, Win, ShapeBounding, 0, 0, 
				  CDMask, ShapeSet);
		XShapeCombineMask(Disp, Iconwin, ShapeBounding, 0, 0, 
				  CDMask, ShapeSet);
		
	WmHints.initial_state = withdrawn?WithdrawnState:NormalState;
	WmHints.icon_window = Iconwin;
	WmHints.icon_x = SizeHints.x;
	WmHints.icon_y = SizeHints.y;
	WmHints.flags = StateHint | IconWindowHint | IconPositionHint  | WindowGroupHint; ;
        WmHints.window_group = Win; 
	XSetWMHints(Disp, Win, &WmHints); 	
	XMapWindow(Disp,Win);
	RedrawWindow(&CurrentXPM);
}

/****************************************************************************/
void ParseCmdLine(int argc, char *argv[])
{

	char *Argument;
	int i;       
	
	for(i = 1; i < argc; i++) 
	{
		Argument = argv[i];
		
		if (Argument[0] == '-') 
		{
			switch(Argument[1]) 
			{
                         case 'w':
                                withdrawn=TRUE;
                                continue;       
			 case 'g': /* Geometry */
				if(++i >= argc) Help();
				Geometry = argv[i];
				continue;
			 case 'd': /* Device */
				if(++i >= argc) Help();
				strcpy(&device[0], argv[i]);
				continue;

			 case 'm' :
				if(++i >= argc) Help();
				strcpy(mountdir, argv[i]);
				mounton=TRUE;
				continue;
			 case 'c' :
				if(++i >=argc) Help();
				strcpy(cdplayer, argv[i]);
				strcat(cdplayer, " &");
				fprintf(stdout, "cdplayer: %s\n,", cdplayer);
				autoplay=TRUE;
				continue;
			 case 'n' :
				if(++i >=argc) Help();
				swapcd(atoi(argv[i]));
				continue;
			case 'h':  /* Help */
				if(++i >= argc) Help();
				continue;
			
			 default:
				Help();
			}
		}
		else
			Help();
	}

}

/****************************************************************************/
void MainLoop()
{
	XEvent Event;            
			      
	/* Main loop */
	while(1)
	{		
		/* Check events */
		while (XPending(Disp))
		{
			XNextEvent(Disp,&Event);
			switch(Event.type)
			{		     
			 case Expose:		/* Redraw window */
				if(Event.xexpose.count == 0)
					RedrawWindow(&CurrentXPM); 
				break;		     
			 case ButtonPress:	/* Mouseclick */
				
				if(Event.xbutton.y < 24)
                                    {
				      if(Event.xbutton.x < 24) swapcd(0);
				      else swapcd(1);		
				    }
				else
				    { if(Event.xbutton.x < 24) swapcd(3);
				      else swapcd(2);
                                    }
				break;			 
			 case DestroyNotify:	/* Destroy window */
				XFreeGC(Disp, WinGC);
				XDestroyWindow(Disp, Win);
				XDestroyWindow(Disp, Iconwin);
				XCloseDisplay(Disp);				
				exit(0); 
				break;			
			}
		}
		XFlush(Disp);
		usleep(5000L);
	}
}

/****************************************************************************/
void GetXPM(void)
{
	XWindowAttributes Attributes;
	int Ret;

	
	XGetWindowAttributes(Disp,Root,&Attributes);		
	cd0XPM.attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);

	Ret = XpmCreatePixmapFromData(Disp, Root, cd01, &cd0XPM.pixmap, 
				      &cd0XPM.mask, &cd0XPM.attributes);

	cd1XPM.attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
	Ret = XpmCreatePixmapFromData(Disp, Root, cd02, &cd1XPM.pixmap, 
				      &cd1XPM.mask, &cd1XPM.attributes);

        cd2XPM.attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
        Ret = XpmCreatePixmapFromData(Disp, Root, cd03, &cd2XPM.pixmap,
                                      &cd2XPM.mask, &cd2XPM.attributes);         

        cd3XPM.attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
        Ret = XpmCreatePixmapFromData(Disp, Root, cd04, &cd3XPM.pixmap,
                                      &cd3XPM.mask, &cd3XPM.attributes);         



        CurrentXPM=cd0XPM;	
	if(Ret != XpmSuccess)
        {
                fprintf(stderr, "ascdc: not enough free color cells\n");
                exit(1);
        }          
}

/****************************************************************************/
int FlushExpose(Window w)
{
	XEvent dummy;
	int i=0;
	
	while (XCheckTypedWindowEvent (Disp, w, Expose, &dummy))i++;
	return i;
}

/****************************************************************************/
void RedrawWindow(XpmIcon *Icon)
{		
	FlushExpose(Win);
	FlushExpose(Iconwin);
	
		XShapeCombineMask(Disp, Win, ShapeBounding, 0, 0, 
				  CDMask, ShapeSet);
		XShapeCombineMask(Disp, Iconwin, ShapeBounding, 0, 0, 
				  CDMask, ShapeSet);
	
	XCopyArea(Disp,Icon->pixmap,Win,WinGC,
		  0,0,Icon->attributes.width, Icon->attributes.height,0,0);	
	XCopyArea(Disp,Icon->pixmap,Iconwin,WinGC,
		  0,0,Icon->attributes.width, Icon->attributes.height,0,0);
}

/****************************************************************************/
Pixel GetColor(char *ColorName)
{
	XColor Color;
	XWindowAttributes Attributes;
	
	XGetWindowAttributes(Disp,Root,&Attributes);
	Color.pixel = 0;
	
	if (!XParseColor (Disp, Attributes.colormap, ColorName, &Color)) 
		fprintf(stderr,"ascdc: can't parse %s\n", ColorName);
	else if(!XAllocColor (Disp, Attributes.colormap, &Color)) 
		fprintf(stderr,"ascdc: can't allocate %s\n", ColorName);       
	
	return Color.pixel;
}