Remove trailing whitespace from most files
[mplayer/glamo.git] / gui / mplayer / mw.c
blob4e64af3a5181640b2f275a0d72463ae683172b70
1 /*
2 * main window
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <inttypes.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <string.h>
28 #include "config.h"
29 #include "gmplayer.h"
30 #include "gui/app.h"
31 #include "gui/skin/font.h"
32 #include "gui/skin/skin.h"
33 #include "gui/wm/ws.h"
35 #include "help_mp.h"
36 #include "libvo/x11_common.h"
37 #include "libvo/fastmemcpy.h"
39 #include "stream/stream.h"
40 #include "stream/url.h"
41 #include "mixer.h"
42 #include "libvo/sub.h"
43 #include "access_mpcontext.h"
45 #include "libmpdemux/demuxer.h"
46 #include "libmpdemux/stheader.h"
47 #include "codec-cfg.h"
48 #include "m_option.h"
49 #include "m_property.h"
51 #define GUI_REDRAW_WAIT 375
53 #include "play.h"
54 #include "widgets.h"
56 unsigned int GetTimerMS( void );
58 unsigned char * mplDrawBuffer = NULL;
59 int mplMainRender = 1;
61 int mplMainAutoPlay = 0;
62 int mplMiddleMenu = 0;
64 int mainVisible = 1;
66 int boxMoved = 0;
67 int sx = 0,sy = 0;
68 int i,pot = 0;
70 #include "gui_common.h"
72 void mplMainDraw( void )
75 if ( appMPlayer.mainWindow.State == wsWindowClosed ) exit_player( MSGTR_Exit_quit );
77 if ( appMPlayer.mainWindow.Visible == wsWindowNotVisible ||
78 !mainVisible ) return;
79 // !appMPlayer.mainWindow.Mapped ) return;
81 if ( mplMainRender && appMPlayer.mainWindow.State == wsWindowExpose )
83 btnModify( evSetMoviePosition,guiIntfStruct.Position );
84 btnModify( evSetVolume,guiIntfStruct.Volume );
86 fast_memcpy( mplDrawBuffer,appMPlayer.main.Bitmap.Image,appMPlayer.main.Bitmap.ImageSize );
87 Render( &appMPlayer.mainWindow,appMPlayer.Items,appMPlayer.NumberOfItems,mplDrawBuffer,appMPlayer.main.Bitmap.ImageSize );
88 mplMainRender=0;
90 wsPutImage( &appMPlayer.mainWindow );
91 // XFlush( wsDisplay );
94 static unsigned last_redraw_time = 0;
96 void mplEventHandling( int msg,float param )
98 int iparam = (int)param;
99 mixer_t *mixer = mpctx_get_mixer(guiIntfStruct.mpcontext);
101 switch( msg )
103 // --- user events
104 case evExit:
105 exit_player( "Exit" );
106 break;
108 case evPlayNetwork:
109 gfree( (void **)&guiIntfStruct.Subtitlename );
110 gfree( (void **)&guiIntfStruct.AudioFile );
111 guiIntfStruct.StreamType=STREAMTYPE_STREAM;
112 goto play;
113 case evSetURL:
114 gtkShow( evPlayNetwork,NULL );
115 break;
117 case evSetAudio:
118 if ( !guiIntfStruct.demuxer || audio_id == iparam ) break;
119 audio_id=iparam;
120 goto play;
122 case evSetVideo:
123 if ( !guiIntfStruct.demuxer || video_id == iparam ) break;
124 video_id=iparam;
125 goto play;
127 case evSetSubtitle:
128 mp_property_do("sub",M_PROPERTY_SET,&iparam,guiIntfStruct.mpcontext);
129 break;
131 #ifdef CONFIG_VCD
132 case evSetVCDTrack:
133 guiIntfStruct.Track=iparam;
134 case evPlayVCD:
135 gtkSet( gtkClearStruct,0,(void *)guiALL );
136 guiIntfStruct.StreamType=STREAMTYPE_VCD;
137 goto play;
138 #endif
139 #ifdef CONFIG_DVDREAD
140 case evPlayDVD:
141 guiIntfStruct.DVD.current_title=1;
142 guiIntfStruct.DVD.current_chapter=1;
143 guiIntfStruct.DVD.current_angle=1;
144 play_dvd_2:
145 gtkSet( gtkClearStruct,0,(void *)(guiALL - guiDVD) );
146 guiIntfStruct.StreamType=STREAMTYPE_DVD;
147 goto play;
148 #endif
149 case evPlay:
150 case evPlaySwitchToPause:
151 play:
153 if ( ( msg == evPlaySwitchToPause )&&( guiIntfStruct.Playing == 2 ) ) goto NoPause;
155 if ( gtkSet( gtkGetCurrPlItem,0,NULL ) &&( guiIntfStruct.StreamType == STREAMTYPE_FILE ) )
157 plItem * next = gtkSet( gtkGetCurrPlItem,0,NULL );
158 plLastPlayed=next;
159 mplSetFileName( next->path,next->name,STREAMTYPE_FILE );
162 switch ( guiIntfStruct.StreamType )
164 case STREAMTYPE_STREAM:
165 case STREAMTYPE_FILE:
166 gtkSet( gtkClearStruct,0,(void *)(guiALL - guiFilenames) );
167 break;
168 #ifdef CONFIG_VCD
169 case STREAMTYPE_VCD:
170 gtkSet( gtkClearStruct,0,(void *)(guiALL - guiVCD - guiFilenames) );
171 if ( !cdrom_device ) cdrom_device=gstrdup( DEFAULT_CDROM_DEVICE );
172 mplSetFileName( NULL,cdrom_device,STREAMTYPE_VCD );
173 if ( guiIntfStruct.Playing != 2 )
175 if ( !guiIntfStruct.Track )
177 if ( guiIntfStruct.VCDTracks > 1 ) guiIntfStruct.Track=2;
178 else guiIntfStruct.Track=1;
180 guiIntfStruct.DiskChanged=1;
182 break;
183 #endif
184 #ifdef CONFIG_DVDREAD
185 case STREAMTYPE_DVD:
186 gtkSet( gtkClearStruct,0,(void *)(guiALL - guiDVD - guiFilenames) );
187 if ( !dvd_device ) dvd_device=gstrdup( DEFAULT_DVD_DEVICE );
188 mplSetFileName( NULL,dvd_device,STREAMTYPE_DVD );
189 if ( guiIntfStruct.Playing != 2 )
191 guiIntfStruct.Title=guiIntfStruct.DVD.current_title;
192 guiIntfStruct.Chapter=guiIntfStruct.DVD.current_chapter;
193 guiIntfStruct.Angle=guiIntfStruct.DVD.current_angle;
194 guiIntfStruct.DiskChanged=1;
196 break;
197 #endif
199 guiIntfStruct.NewPlay=1;
200 mplPlay();
201 break;
202 #ifdef CONFIG_DVDREAD
203 case evSetDVDSubtitle:
204 dvdsub_id=iparam;
205 goto play_dvd_2;
206 break;
207 case evSetDVDAudio:
208 audio_id=iparam;
209 goto play_dvd_2;
210 break;
211 case evSetDVDChapter:
212 guiIntfStruct.DVD.current_chapter=iparam;
213 goto play_dvd_2;
214 break;
215 case evSetDVDTitle:
216 guiIntfStruct.DVD.current_title=iparam;
217 guiIntfStruct.DVD.current_chapter=1;
218 guiIntfStruct.DVD.current_angle=1;
219 goto play_dvd_2;
220 break;
221 #endif
223 case evPause:
224 case evPauseSwitchToPlay:
225 NoPause:
226 mplPause();
227 break;
229 case evStop:
230 guiIntfStruct.Playing=guiSetStop;
231 mplState();
232 guiIntfStruct.NoWindow=False;
233 break;
235 case evLoadPlay:
236 mplMainAutoPlay=1;
237 // guiIntfStruct.StreamType=STREAMTYPE_FILE;
238 case evLoad:
239 gtkSet( gtkDelPl,0,NULL );
240 gtkShow( evLoad,NULL );
241 break;
242 case evLoadSubtitle: gtkShow( evLoadSubtitle,NULL ); break;
243 case evDropSubtitle:
244 gfree( (void **)&guiIntfStruct.Subtitlename );
245 guiLoadSubtitle( NULL );
246 break;
247 case evLoadAudioFile: gtkShow( evLoadAudioFile,NULL ); break;
248 case evPrev: mplPrev(); break;
249 case evNext: mplNext(); break;
251 case evPlayList: gtkShow( evPlayList,NULL ); break;
252 case evSkinBrowser: gtkShow( evSkinBrowser,skinName ); break;
253 case evAbout: gtkShow( evAbout,NULL ); break;
254 case evPreferences: gtkShow( evPreferences,NULL ); break;
255 case evEqualizer: gtkShow( evEqualizer,NULL ); break;
257 case evForward10min: mplRelSeek( 600 ); break;
258 case evBackward10min: mplRelSeek( -600 );break;
259 case evForward1min: mplRelSeek( 60 ); break;
260 case evBackward1min: mplRelSeek( -60 ); break;
261 case evForward10sec: mplRelSeek( 10 ); break;
262 case evBackward10sec: mplRelSeek( -10 ); break;
263 case evSetMoviePosition: mplAbsSeek( param ); break;
265 case evIncVolume: vo_x11_putkey( wsGrayMul ); break;
266 case evDecVolume: vo_x11_putkey( wsGrayDiv ); break;
267 case evMute: mixer_mute( mixer ); break;
269 case evSetVolume:
270 guiIntfStruct.Volume=param;
271 goto set_volume;
272 case evSetBalance:
273 guiIntfStruct.Balance=param;
274 set_volume:
276 float l = guiIntfStruct.Volume * ( ( 100.0 - guiIntfStruct.Balance ) / 50.0 );
277 float r = guiIntfStruct.Volume * ( ( guiIntfStruct.Balance ) / 50.0 );
278 if ( l > guiIntfStruct.Volume ) l=guiIntfStruct.Volume;
279 if ( r > guiIntfStruct.Volume ) r=guiIntfStruct.Volume;
280 // printf( "!!! v: %.2f b: %.2f -> %.2f x %.2f\n",guiIntfStruct.Volume,guiIntfStruct.Balance,l,r );
281 mixer_setvolume( mixer,l,r );
283 if ( osd_level )
285 osd_visible=(GetTimerMS() + 1000) | 1;
286 vo_osd_progbar_type=OSD_VOLUME;
287 vo_osd_progbar_value=( ( guiIntfStruct.Volume ) * 256.0 ) / 100.0;
288 vo_osd_changed( OSDTYPE_PROGBAR );
290 break;
293 case evIconify:
294 switch ( iparam )
296 case 0: wsIconify( appMPlayer.mainWindow ); break;
297 case 1: wsIconify( appMPlayer.subWindow ); break;
299 break;
300 case evHalfSize:
301 btnSet( evFullScreen,btnReleased );
302 if ( guiIntfStruct.Playing )
304 if ( appMPlayer.subWindow.isFullScreen )
306 mplFullScreen();
308 wsResizeWindow( &appMPlayer.subWindow, guiIntfStruct.MovieWidth / 2, guiIntfStruct.MovieHeight / 2 );
309 wsMoveWindow( &appMPlayer.subWindow, 0,
310 ( wsMaxX - guiIntfStruct.MovieWidth/2 )/2 + wsOrgX,
311 ( wsMaxY - guiIntfStruct.MovieHeight/2 )/2 + wsOrgY );
313 break;
314 case evDoubleSize:
315 btnSet( evFullScreen,btnReleased );
316 if ( guiIntfStruct.Playing )
318 if ( appMPlayer.subWindow.isFullScreen )
320 mplFullScreen();
322 wsResizeWindow( &appMPlayer.subWindow, guiIntfStruct.MovieWidth * 2, guiIntfStruct.MovieHeight * 2 );
323 wsMoveWindow( &appMPlayer.subWindow, 0,
324 ( wsMaxX - guiIntfStruct.MovieWidth*2 )/2 + wsOrgX,
325 ( wsMaxY - guiIntfStruct.MovieHeight*2 )/2 + wsOrgY );
327 break;
328 case evNormalSize:
329 btnSet( evFullScreen,btnReleased );
330 if ( guiIntfStruct.Playing )
332 if ( appMPlayer.subWindow.isFullScreen )
334 mplFullScreen();
336 wsResizeWindow( &appMPlayer.subWindow, guiIntfStruct.MovieWidth, guiIntfStruct.MovieHeight );
337 wsMoveWindow( &appMPlayer.subWindow, 0,
338 ( wsMaxX - guiIntfStruct.MovieWidth )/2 + wsOrgX,
339 ( wsMaxY - guiIntfStruct.MovieHeight )/2 + wsOrgY );
340 break;
341 } else if ( !appMPlayer.subWindow.isFullScreen ) break;
342 case evFullScreen:
343 if ( !guiIntfStruct.Playing && !gtkShowVideoWindow ) break;
344 mplFullScreen();
345 if ( appMPlayer.subWindow.isFullScreen ) btnSet( evFullScreen,btnPressed );
346 else btnSet( evFullScreen,btnReleased );
347 break;
349 case evSetAspect:
350 switch ( iparam )
352 case 2: movie_aspect=16.0f / 9.0f; break;
353 case 3: movie_aspect=4.0f / 3.0f; break;
354 case 4: movie_aspect=2.35; break;
355 case 1:
356 default: movie_aspect=-1;
358 wsClearWindow( appMPlayer.subWindow );
359 #ifdef CONFIG_DVDREAD
360 if ( guiIntfStruct.StreamType == STREAMTYPE_DVD || guiIntfStruct.StreamType == STREAMTYPE_VCD ) goto play_dvd_2;
361 else
362 #endif
363 guiIntfStruct.NewPlay=1;
364 break;
366 // --- timer events
367 case evRedraw:
369 unsigned now = GetTimerMS();
370 extern int mplPBFade;
371 if ((now > last_redraw_time) &&
372 (now < last_redraw_time + GUI_REDRAW_WAIT) &&
373 !mplPBFade)
374 break;
375 last_redraw_time = now;
377 mplMainRender=1;
378 wsPostRedisplay( &appMPlayer.mainWindow );
379 wsPostRedisplay( &appMPlayer.barWindow );
380 break;
381 // --- system events
382 #ifdef MP_DEBUG
383 case evNone:
384 mp_msg( MSGT_GPLAYER,MSGL_STATUS,"[mw] event none received.\n" );
385 break;
386 default:
387 mp_msg( MSGT_GPLAYER,MSGL_STATUS,"[mw] unknown event received ( %d,%.2f ).\n",msg,param );
388 break;
389 #endif
393 #define itPLMButton (itNULL - 1)
394 #define itPRMButton (itNULL - 2)
396 void mplMainMouseHandle( int Button,int X,int Y,int RX,int RY )
398 static int itemtype = 0;
399 int i;
400 wItem * item = NULL;
401 float value = 0.0f;
403 static int SelectedItem = -1;
404 int currentselected = -1;
406 for ( i=0;i < appMPlayer.NumberOfItems + 1;i++ )
407 if ( ( appMPlayer.Items[i].pressed != btnDisabled )&&
408 ( wgIsRect( X,Y,appMPlayer.Items[i].x,appMPlayer.Items[i].y,appMPlayer.Items[i].x+appMPlayer.Items[i].width,appMPlayer.Items[i].y+appMPlayer.Items[i].height ) ) )
409 { currentselected=i; break; }
411 switch ( Button )
413 case wsPMMouseButton:
414 gtkShow( evHidePopUpMenu,NULL );
415 mplShowMenu( RX,RY );
416 itemtype=itPRMButton;
417 break;
418 case wsRMMouseButton:
419 mplHideMenu( RX,RY,0 );
420 break;
422 case wsPLMouseButton:
423 gtkShow( evHidePopUpMenu,NULL );
424 sx=X; sy=Y; boxMoved=1; itemtype=itPLMButton;
425 SelectedItem=currentselected;
426 if ( SelectedItem == -1 ) break;
427 boxMoved=0;
428 item=&appMPlayer.Items[SelectedItem];
429 itemtype=item->type;
430 item->pressed=btnPressed;
431 switch( item->type )
433 case itButton:
434 if ( ( SelectedItem > -1 ) &&
435 ( ( ( item->msg == evPlaySwitchToPause && item->msg == evPauseSwitchToPlay ) ) ||
436 ( ( item->msg == evPauseSwitchToPlay && item->msg == evPlaySwitchToPause ) ) ) )
437 { item->pressed=btnDisabled; }
438 break;
440 break;
441 case wsRLMouseButton:
442 boxMoved=0;
443 item=&appMPlayer.Items[SelectedItem];
444 item->pressed=btnReleased;
445 SelectedItem=-1;
446 if ( currentselected == - 1 ) { itemtype=0; break; }
447 value=0;
448 switch( itemtype )
450 case itPotmeter:
451 case itHPotmeter:
452 btnModify( item->msg,(float)( X - item->x ) / item->width * 100.0f );
453 mplEventHandling( item->msg,item->value );
454 value=item->value;
455 break;
456 case itVPotmeter:
457 btnModify( item->msg, ( 1. - (float)( Y - item->y ) / item->height) * 100.0f );
458 mplEventHandling( item->msg,item->value );
459 value=item->value;
460 break;
462 mplEventHandling( item->msg,value );
463 itemtype=0;
464 break;
466 case wsRRMouseButton:
467 gtkShow( evShowPopUpMenu,NULL );
468 break;
470 // --- rolled mouse ... de szar :)))
471 case wsP5MouseButton: value=-2.5f; goto rollerhandled;
472 case wsP4MouseButton: value= 2.5f;
473 rollerhandled:
474 item=&appMPlayer.Items[currentselected];
475 if ( ( item->type == itHPotmeter )||( item->type == itVPotmeter )||( item->type == itPotmeter ) )
477 item->value+=value;
478 btnModify( item->msg,item->value );
479 mplEventHandling( item->msg,item->value );
481 break;
483 // --- moving
484 case wsMoveMouse:
485 item=&appMPlayer.Items[SelectedItem];
486 switch ( itemtype )
488 case itPLMButton:
489 wsMoveWindow( &appMPlayer.mainWindow,False,RX - abs( sx ),RY - abs( sy ) );
490 mplMainRender=0;
491 break;
492 case itPRMButton:
493 mplMenuMouseHandle( X,Y,RX,RY );
494 break;
495 case itPotmeter:
496 item->value=(float)( X - item->x ) / item->width * 100.0f;
497 goto potihandled;
498 case itVPotmeter:
499 item->value=(1. - (float)( Y - item->y ) / item->height) * 100.0f;
500 goto potihandled;
501 case itHPotmeter:
502 item->value=(float)( X - item->x ) / item->width * 100.0f;
503 potihandled:
504 if ( item->value > 100.0f ) item->value=100.0f;
505 if ( item->value < 0.0f ) item->value=0.0f;
506 mplEventHandling( item->msg,item->value );
507 break;
509 break;
513 int keyPressed = 0;
515 void mplMainKeyHandle( int KeyCode,int Type,int Key )
517 int msg = evNone;
519 if ( Type != wsKeyPressed ) return;
521 if ( !Key )
523 switch ( KeyCode )
525 case wsXFMMPrev: msg=evPrev; break;
526 case wsXFMMStop: msg=evStop; break;
527 case wsXFMMPlay: msg=evPlaySwitchToPause; break;
528 case wsXFMMNext: msg=evNext; break;
529 case wsXFMMVolUp: msg=evIncVolume; break;
530 case wsXFMMVolDown: msg=evDecVolume; break;
531 case wsXFMMMute: msg=evMute; break;
534 else
536 switch ( Key )
538 case wsEnter: msg=evPlay; break;
539 case wsXF86LowerVolume: msg=evDecVolume; break;
540 case wsXF86RaiseVolume: msg=evIncVolume; break;
541 case wsXF86Mute: msg=evMute; break;
542 case wsXF86Play: msg=evPlaySwitchToPause; break;
543 case wsXF86Stop: msg=evStop; break;
544 case wsXF86Prev: msg=evPrev; break;
545 case wsXF86Next: msg=evNext; break;
546 case wsXF86Media: msg=evLoad; break;
547 case wsEscape:
548 if ( appMPlayer.subWindow.isFullScreen )
550 if ( guiIntfStruct.event_struct ) ((XEvent *)guiIntfStruct.event_struct)->type=None;
551 mplEventHandling( evNormalSize,0 );
552 return;
554 default: vo_x11_putkey( Key ); return;
557 if ( msg != evNone ) mplEventHandling( msg,0 );
560 /* this will be used to handle Drag&Drop files */
561 void mplDandDHandler(int num,char** files)
563 struct stat buf;
564 int f = 0;
566 char* subtitles = NULL;
567 char* filename = NULL;
569 if (num <= 0)
570 return;
573 /* now fill it with new items */
574 for(f=0; f < num; f++){
575 char* str = strdup( files[f] );
576 plItem* item;
578 url_unescape_string(str, files[f]);
580 if(stat(str,&buf) == 0 && S_ISDIR(buf.st_mode) == 0) {
581 /* this is not a directory so try to play it */
582 mp_msg( MSGT_GPLAYER,MSGL_V,"Received D&D %s\n",str );
584 /* check if it is a subtitle file */
586 char* ext = strrchr(str,'.');
587 if (ext) {
588 static char supported[] = "utf/sub/srt/smi/rt//txt/ssa/aqt/";
589 char* type;
590 int len;
591 if((len=strlen(++ext)) && (type=strstr(supported,ext)) &&\
592 (type-supported)%4 == 0 && *(type+len) == '/'){
593 /* handle subtitle file */
594 gfree((void**)&subtitles);
595 subtitles = str;
596 continue;
601 /* clear playlist */
602 if (filename == NULL) {
603 filename = files[f];
604 gtkSet(gtkDelPl,0,NULL);
607 item = calloc(1,sizeof(plItem));
609 /* FIXME: decompose file name ? */
610 /* yes -- Pontscho */
611 if ( strrchr( str,'/' ) ) {
612 char * s = strrchr( str,'/' ); *s=0; s++;
613 item->name = gstrdup( s );
614 item->path = gstrdup( str );
615 } else {
616 item->name = strdup(str);
617 item->path = strdup("");
619 gtkSet(gtkAddPlItem,0,(void*)item);
620 } else {
621 mp_msg( MSGT_GPLAYER,MSGL_WARN,MSGTR_NotAFile,str );
623 free( str );
626 if (filename) {
627 mplSetFileName( NULL,filename,STREAMTYPE_FILE );
628 if ( guiIntfStruct.Playing == 1 ) mplEventHandling( evStop,0 );
629 mplEventHandling( evPlay,0 );
631 if (subtitles) {
632 gfree((void**)&guiIntfStruct.Subtitlename);
633 guiIntfStruct.Subtitlename = subtitles;
634 guiLoadSubtitle(guiIntfStruct.Subtitlename);