]> git.sesse.net Git - vlc/blob - src/interface/main.c
. changement cosm�tique
[vlc] / src / interface / main.c
1 /*****************************************************************************
2  * main.c: main vlc source
3  * Includes the main() function for vlc. Parses command line, start interface
4  * and spawn threads.
5  *****************************************************************************
6  * Copyright (C) 1998, 1999, 2000 VideoLAN
7  *
8  * Authors:
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "defs.h"
29
30 #include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
31 #include <getopt.h>                                              /* getopt() */
32 #include <stdio.h>                                              /* sprintf() */
33
34 #include <errno.h>                                                 /* ENOMEM */
35 #include <stdlib.h>                                  /* getenv(), strtol(),  */
36 #include <string.h>                                            /* strerror() */
37
38 #include "config.h"
39 #include "common.h"
40 #include "threads.h"
41 #include "mtime.h"
42 #include "plugins.h"
43 #include "playlist.h"
44 #include "input_vlan.h"
45 #include "input_ps.h"
46
47 #include "intf_msg.h"
48 #include "interface.h"
49
50 #include "audio_output.h"
51
52 #ifdef SYS_BEOS
53 #include "beos_specific.h"
54 #endif
55
56 #include "main.h"
57
58 /*****************************************************************************
59  * Command line options constants. If something is changed here, be sure that
60  * GetConfiguration and Usage are also changed.
61  *****************************************************************************/
62
63 /* Long options return values - note that values corresponding to short options
64  * chars, and in general any regular char, should be avoided */
65 #define OPT_NOAUDIO             150
66 #define OPT_AOUT                151
67 #define OPT_STEREO              152
68 #define OPT_MONO                153
69
70 #define OPT_NOVIDEO             160
71 #define OPT_VOUT                161
72 #define OPT_DISPLAY             162
73 #define OPT_WIDTH               163
74 #define OPT_HEIGHT              164
75 #define OPT_COLOR               165
76
77 #define OPT_NOVLANS             170
78 #define OPT_SERVER              171
79 #define OPT_PORT                172
80
81 /* Usage fashion */
82 #define USAGE                     0
83 #define SHORT_HELP                1
84 #define LONG_HELP                 2
85
86 /* Long options */
87 static const struct option longopts[] =
88 {
89     /*  name,               has_arg,    flag,   val */
90
91     /* General/common options */
92     {   "help",             0,          0,      'h' },
93     {   "longhelp",         0,          0,      'H' },
94     {   "version",          0,          0,      'v' },
95
96     /* Audio options */
97     {   "noaudio",          0,          0,      OPT_NOAUDIO },
98     {   "aout",             1,          0,      OPT_AOUT },
99     {   "stereo",           0,          0,      OPT_STEREO },
100     {   "mono",             0,          0,      OPT_MONO },
101
102     /* Video options */
103     {   "novideo",          0,          0,      OPT_NOVIDEO },
104     {   "vout",             1,          0,      OPT_VOUT },
105     {   "display",          1,          0,      OPT_DISPLAY },
106     {   "width",            1,          0,      OPT_WIDTH },
107     {   "height",           1,          0,      OPT_HEIGHT },
108     {   "grayscale",        0,          0,      'g' },
109     {   "color",            0,          0,      OPT_COLOR },
110
111     /* DVD options */
112     {   "dvdaudio",         1,          0,      'a' },
113     {   "dvdchannel",       1,          0,      'c' },
114     {   "dvdsubtitle",      1,          0,      's' },
115     
116     /* Input options */
117     {   "novlans",          0,          0,      OPT_NOVLANS },
118     {   "server",           1,          0,      OPT_SERVER },
119     {   "port",             1,          0,      OPT_PORT },
120
121     {   0,                  0,          0,      0 }
122 };
123
124 /* Short options */
125 static const char *psz_shortopts = "hHvga:s:c:";
126
127 /*****************************************************************************
128  * Global variable program_data - this is the one and only, see main.h
129  *****************************************************************************/
130 main_t *p_main;
131
132 /*****************************************************************************
133  * Local prototypes
134  *****************************************************************************/
135 static void SetDefaultConfiguration ( void );
136 static int  GetConfiguration        ( int i_argc, char *ppsz_argv[],
137                                       char *ppsz_env[] );
138 static void Usage                   ( int i_fashion );
139 static void Version                 ( void );
140
141 static void InitSignalHandler       ( void );
142 static void SignalHandler           ( int i_signal );
143 #ifdef HAVE_MMX
144 static int  TestMMX                 ( void );
145 #endif
146
147 /*****************************************************************************
148  * main: parse command line, start interface and spawn threads
149  *****************************************************************************
150  * Steps during program execution are:
151  *      -configuration parsing and messages interface initialization
152  *      -opening of audio output device and some global modules
153  *      -execution of interface, which exit on error or on user request
154  *      -closing of audio output device and some global modules
155  * On error, the spawned threads are canceled, and the open devices closed.
156  *****************************************************************************/
157 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
158 {
159     main_t  main_data;                      /* root of all data - see main.h */
160
161     p_main = &main_data;                       /* set up the global variable */
162
163 #ifdef SYS_BEOS
164     /*
165      * System specific initialization code
166      */
167     beos_Create();
168 #endif
169
170 #ifdef HAVE_MMX
171     /*
172      * Test if our code is likely to run on this CPU 
173      */
174     if( !TestMMX() )
175     {
176         fprintf( stderr, "Sorry, this program needs an MMX processor. "
177                          "Please run the non-MMX version.\n" );
178         return( 1 );
179     }
180 #endif
181
182     /*
183      * Initialize messages interface
184      */
185     p_main->p_msg = intf_MsgCreate();
186     if( !p_main->p_msg )                         /* start messages interface */
187     {
188         fprintf( stderr, "error: can't initialize messages interface (%s)\n",
189                 strerror(errno) );
190         return( errno );
191     }
192
193     /*
194      * Read configuration
195      */
196     if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) )  /* parse cmd line */
197     {
198         intf_MsgDestroy();
199         fprintf( stderr, "error: can't read configuration (%s)\n",
200                 strerror(errno) );
201         return( errno );
202     }
203
204     /*
205      * Initialize playlist and get commandline files
206      */
207     p_main->p_playlist = playlist_Create( );
208     if( !p_main->p_playlist )
209     {
210         intf_Msg( "Playlist initialization failed\n" );
211         intf_MsgDestroy();
212         return( errno );
213     }
214     playlist_Init( p_main->p_playlist, optind );
215
216     /*
217      * Initialize plugin bank
218      */
219     p_main->p_bank = bank_Create( );
220     if( !p_main->p_bank )
221     {
222         intf_Msg( "Plugin bank initialization failed\n" );
223         playlist_Destroy( p_main->p_playlist );
224         intf_MsgDestroy();
225         return( errno );
226     }
227     bank_Init( p_main->p_bank );
228
229     /*
230      * Initialize shared resources and libraries
231      */
232     if( p_main->b_vlans && input_VlanCreate() )
233     {
234         /* On error during vlans initialization, switch off vlans */
235         intf_Msg( "Virtual LANs initialization failed : "
236                   "vlans management is deactivated\n" );
237         p_main->b_vlans = 0;
238     }
239
240     /*
241      * Open audio device and start aout thread
242      */
243     if( p_main->b_audio )
244     {
245         p_main->p_aout = aout_CreateThread( NULL );
246         if( p_main->p_aout == NULL )
247         {
248             /* On error during audio initialization, switch off audio */
249             intf_Msg( "Audio initialization failed : audio is deactivated\n" );
250             p_main->b_audio = 0;
251         }
252     }
253
254     /*
255      * Run interface
256      */
257     p_main->p_intf = intf_Create();
258     if( p_main->p_intf != NULL )
259     {
260         InitSignalHandler();             /* prepare signals for interception */
261
262         intf_Run( p_main->p_intf );
263
264         intf_Destroy( p_main->p_intf );
265     }
266
267     /*
268      * Close audio device
269      */
270     if( p_main->b_audio )
271     {
272         aout_DestroyThread( p_main->p_aout, NULL );
273     }
274
275     /*
276      * Free shared resources and libraries
277      */
278     if( p_main->b_vlans )
279     {
280         input_VlanDestroy();
281     }
282
283     /*
284      * Free plugin bank
285      */
286     bank_Destroy( p_main->p_bank );
287
288     /*
289      * Free playlist
290      */
291     playlist_Destroy( p_main->p_playlist );
292
293 #ifdef SYS_BEOS
294     /*
295      * System specific cleaning code
296      */
297     beos_Destroy();
298 #endif
299
300     /*
301      * Terminate messages interface and program
302      */
303     intf_Msg( "Program terminated.\n" );
304     intf_MsgDestroy();
305
306     return( 0 );
307 }
308
309 /*****************************************************************************
310  * main_GetIntVariable: get the int value of an environment variable
311  *****************************************************************************
312  * This function is used to read some default parameters in modules.
313  *****************************************************************************/
314 int main_GetIntVariable( char *psz_name, int i_default )
315 {
316     char *      psz_env;                                /* environment value */
317     char *      psz_end;                             /* end of parsing index */
318     long int    i_value;                                            /* value */
319
320     psz_env = getenv( psz_name );
321     if( psz_env )
322     {
323         i_value = strtol( psz_env, &psz_end, 0 );
324         if( (*psz_env != '\0') && (*psz_end == '\0') )
325         {
326             return( i_value );
327         }
328     }
329     return( i_default );
330 }
331
332 /*****************************************************************************
333  * main_GetPszVariable: get the string value of an environment variable
334  *****************************************************************************
335  * This function is used to read some default parameters in modules.
336  *****************************************************************************/
337 char * main_GetPszVariable( char *psz_name, char *psz_default )
338 {
339     char *psz_env;
340
341     psz_env = getenv( psz_name );
342     if( psz_env )
343     {
344         return( psz_env );
345     }
346     return( psz_default );
347 }
348
349 /*****************************************************************************
350  * main_PutPszVariable: set the string value of an environment variable
351  *****************************************************************************
352  * This function is used to set some default parameters in modules. The use of
353  * this function will cause some memory leak: since some systems use the pointer
354  * passed to putenv to store the environment string, it can't be freed.
355  *****************************************************************************/
356 void main_PutPszVariable( char *psz_name, char *psz_value )
357 {
358     char *psz_env;
359
360     psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
361     if( psz_env == NULL )
362     {
363         intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
364     }
365     else
366     {
367         sprintf( psz_env, "%s=%s", psz_name, psz_value );
368         if( putenv( psz_env ) )
369         {
370             intf_ErrMsg( "error: %s\n", strerror(errno) );
371         }
372     }
373 }
374
375 /*****************************************************************************
376  * main_PutIntVariable: set the integer value of an environment variable
377  *****************************************************************************
378  * This function is used to set some default parameters in modules. The use of
379  * this function will cause some memory leak: since some systems use the pointer
380  * passed to putenv to store the environment string, it can't be freed.
381  *****************************************************************************/
382 void main_PutIntVariable( char *psz_name, int i_value )
383 {
384     char psz_value[ 256 ];                               /* buffer for value */
385
386     sprintf( psz_value, "%d", i_value );
387     main_PutPszVariable( psz_name, psz_value );
388 }
389
390 /* following functions are local */
391
392 /*****************************************************************************
393  * SetDefaultConfiguration: set default options
394  *****************************************************************************
395  * This function is called by GetConfiguration before command line is parsed.
396  * It sets all the default values required later by the program. At this stage,
397  * most structure are not yet allocated, so initialization must be done using
398  * environment.
399  *****************************************************************************/
400 static void SetDefaultConfiguration( void )
401 {
402     /*
403      * All features are activated by default
404      */
405     p_main->b_audio  = 1;
406     p_main->b_video  = 1;
407     p_main->b_vlans  = 1;
408 }
409
410 /*****************************************************************************
411  * GetConfiguration: parse command line
412  *****************************************************************************
413  * Parse command line and configuration file for configuration. If the inline
414  * help is requested, the function Usage() is called and the function returns
415  * -1 (causing main() to exit). The messages interface is initialized at this
416  * stage, but most structures are not allocated, so only environment should
417  * be used.
418  *****************************************************************************/
419 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
420 {
421     int c, i_opt;
422
423     /* Set default configuration and copy arguments */
424     p_main->i_argc    = i_argc;
425     p_main->ppsz_argv = ppsz_argv;
426     p_main->ppsz_env  = ppsz_env;
427     SetDefaultConfiguration();
428
429     intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
430
431     /* Parse command line options */
432     opterr = 0;
433     while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
434     {
435         switch( c )
436         {
437         /* General/common options */
438         case 'h':                                              /* -h, --help */
439             Usage( SHORT_HELP );
440             return( -1 );
441             break;
442         case 'H':                                          /* -H, --longhelp */
443             Usage( LONG_HELP );
444             return( -1 );
445             break;
446         case 'v':                                           /* -v, --version */
447             Version();
448             return( -1 );
449             break;
450
451         /* Audio options */
452         case OPT_NOAUDIO:                                       /* --noaudio */
453             p_main->b_audio = 0;
454             break;
455         case OPT_AOUT:                                             /* --aout */
456             main_PutPszVariable( AOUT_METHOD_VAR, optarg );
457             break;
458         case OPT_STEREO:                                         /* --stereo */
459             main_PutIntVariable( AOUT_STEREO_VAR, 1 );
460             break;
461         case OPT_MONO:                                             /* --mono */
462             main_PutIntVariable( AOUT_STEREO_VAR, 0 );
463             break;
464
465         /* Video options */
466         case OPT_NOVIDEO:                                       /* --novideo */
467             p_main->b_video = 0;
468             break;
469         case OPT_VOUT:                                             /* --vout */
470             main_PutPszVariable( VOUT_METHOD_VAR, optarg );
471             break;
472         case OPT_DISPLAY:                                       /* --display */
473             main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
474             break;
475         case OPT_WIDTH:                                           /* --width */
476             main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
477             break;
478         case OPT_HEIGHT:                                         /* --height */
479             main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
480             break;
481
482         case 'g':                                         /* -g, --grayscale */
483             main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
484             break;
485         case OPT_COLOR:                                           /* --color */
486             main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
487             break;
488
489         /* DVD options */
490         case 'a':
491             if ( ! strcmp(optarg, "mpeg") )
492                 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_MPEG );
493             else if ( ! strcmp(optarg, "lpcm") )
494                 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_LPCM );
495             else if ( ! strcmp(optarg, "off") )
496                 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_NOAUDIO );
497             else
498                 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_AC3 );
499             break;
500         case 'c':
501             main_PutIntVariable( INPUT_DVD_CHANNEL_VAR, atoi(optarg) );
502             break;
503         case 's':
504             main_PutIntVariable( INPUT_DVD_SUBTITLE_VAR, atoi(optarg) );
505             break;
506
507         /* Input options */
508         case OPT_NOVLANS:                                       /* --novlans */
509             p_main->b_vlans = 0;
510             break;
511         case OPT_SERVER:                                         /* --server */
512             main_PutPszVariable( INPUT_SERVER_VAR, optarg );
513             break;
514         case OPT_PORT:                                             /* --port */
515             main_PutPszVariable( INPUT_PORT_VAR, optarg );
516             break;
517
518         /* Internal error: unknown option */
519         case '?':
520         default:
521             intf_ErrMsg( "intf error: unknown option `%s'\n", ppsz_argv[optind - 1] );
522             Usage( USAGE );
523             return( EINVAL );
524             break;
525         }
526     }
527
528     /* Parse command line parameters - no check is made for these options */
529     for( i_opt = optind; i_opt < i_argc; i_opt++ )
530     {
531         putenv( ppsz_argv[ i_opt ] );
532     }
533     return( 0 );
534 }
535
536 /*****************************************************************************
537  * Usage: print program usage
538  *****************************************************************************
539  * Print a short inline help. Message interface is initialized at this stage.
540  *****************************************************************************/
541 static void Usage( int i_fashion )
542 {
543     /* Usage */
544     intf_Msg( "Usage: vlc [options] [parameters] [file]...\n" );
545
546     if( i_fashion == USAGE )
547     {
548         intf_Msg( "Try `vlc --help' for more information.\n" );
549         return;
550     }
551
552     /* Options */
553     intf_Msg( "\n"
554               "Options:\n"
555               "      --noaudio                  \tdisable audio\n"
556               "      --aout <plugin>            \taudio output method\n"
557               "      --stereo, --mono           \tstereo/mono audio\n"
558               "\n"
559               "      --novideo                  \tdisable video\n"
560               "      --vout <plugin>            \tvideo output method\n"
561               "      --display <display>        \tdisplay string\n"
562               "      --width <w>, --height <h>  \tdisplay dimensions\n"
563               "  -g, --grayscale                \tgrayscale output\n"
564               "      --color                    \tcolor output\n"
565               "\n"
566               "  -a, --dvdaudio                 \tchoose DVD audio type\n"
567               "  -c, --dvdchannel               \tchoose DVD audio channel\n"
568               "  -s, --dvdsubtitle              \tchoose DVD subtitle channel\n"
569               "\n"
570               "      --novlans                  \tdisable vlans\n"
571               "      --server <host>            \tvideo server address\n"
572               "      --port <port>              \tvideo server port\n"
573               "\n"
574               "  -h, --help                     \tprint help and exit\n"
575               "  -H, --longhelp                 \tprint long help and exit\n"
576               "  -v, --version                  \toutput version information and exit\n" );
577
578     if( i_fashion == SHORT_HELP )
579         return;
580
581     /* Interface parameters */
582     intf_Msg( "\n"
583               "Interface parameters:\n"
584               "  " INTF_INIT_SCRIPT_VAR "=<filename>             \tinitialization script\n"
585               "  " INTF_CHANNELS_VAR "=<filename>            \tchannels list\n" );
586
587     /* Audio parameters */
588     intf_Msg( "\n"
589               "Audio parameters:\n"
590               "  " AOUT_METHOD_VAR "=<method name>        \taudio method\n"
591               "  " AOUT_DSP_VAR "=<filename>              \tdsp device path\n"
592               "  " AOUT_STEREO_VAR "={1|0}                \tstereo or mono output\n"
593               "  " AOUT_RATE_VAR "=<rate>             \toutput rate\n" );
594
595     /* Video parameters */
596     intf_Msg( "\n"
597               "Video parameters:\n"
598               "  " VOUT_METHOD_VAR "=<method name>        \tdisplay method\n"
599               "  " VOUT_DISPLAY_VAR "=<display name>      \tdisplay used\n"
600               "  " VOUT_WIDTH_VAR "=<width>               \tdisplay width\n"
601               "  " VOUT_HEIGHT_VAR "=<height>             \tdislay height\n"
602               "  " VOUT_FB_DEV_VAR "=<filename>           \tframebuffer device path\n"
603               "  " VOUT_GRAYSCALE_VAR "={1|0}             \tgrayscale or color output\n" );
604
605     /* DVD parameters */
606     intf_Msg( "\n"
607               "DVD parameters:\n"
608               "  " INPUT_DVD_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type\n"
609               "  " INPUT_DVD_CHANNEL_VAR "=[0-15]            \taudio channel\n"
610               "  " INPUT_DVD_SUBTITLE_VAR "=[0-31]           \tsubtitle channel\n" );
611
612     /* Input parameters */
613     intf_Msg( "\n"
614               "Input parameters:\n"
615               "  " INPUT_SERVER_VAR "=<hostname>          \tvideo server\n"
616               "  " INPUT_PORT_VAR "=<port>            \tvideo server port\n"
617               "  " INPUT_IFACE_VAR "=<interface>          \tnetwork interface\n"
618               "  " INPUT_VLAN_SERVER_VAR "=<hostname>     \tvlan server\n"
619               "  " INPUT_VLAN_PORT_VAR "=<port>           \tvlan server port\n" );
620 }
621
622 /*****************************************************************************
623  * Version: print complete program version
624  *****************************************************************************
625  * Print complete program version and build number.
626  *****************************************************************************/
627 static void Version( void )
628 {
629     intf_Msg( VERSION_MESSAGE
630               "This program comes with NO WARRANTY, to the extent permitted by law.\n"
631               "You may redistribute it under the terms of the GNU General Public License;\n"
632               "see the file named COPYING for details.\n"
633               "Written by the VideoLAN team at Ecole Centrale, Paris.\n" );
634
635 }
636
637 /*****************************************************************************
638  * InitSignalHandler: system signal handler initialization
639  *****************************************************************************
640  * Set the signal handlers. SIGTERM is not intercepted, because we need at
641  * at least a method to kill the program when all other methods failed, and
642  * when we don't want to use SIGKILL.
643  *****************************************************************************/
644 static void InitSignalHandler( void )
645 {
646     /* Termination signals */
647     signal( SIGHUP,  SignalHandler );
648     signal( SIGINT,  SignalHandler );
649     signal( SIGQUIT, SignalHandler );
650 }
651
652 /*****************************************************************************
653  * SignalHandler: system signal handler
654  *****************************************************************************
655  * This function is called when a signal is received by the program. It tries to
656  * end the program in a clean way.
657  *****************************************************************************/
658 static void SignalHandler( int i_signal )
659 {
660     /* Once a signal has been trapped, the termination sequence will be armed and
661      * following signals will be ignored to avoid sending messages to an interface
662      * having been destroyed */
663     signal( SIGHUP,  SIG_IGN );
664     signal( SIGINT,  SIG_IGN );
665     signal( SIGQUIT, SIG_IGN );
666
667     /* Acknowledge the signal received */
668     intf_ErrMsgImm("intf: signal %d received\n", i_signal );
669
670     /* Try to terminate everything - this is done by requesting the end of the
671      * interface thread */
672     p_main->p_intf->b_die = 1;
673 }
674
675 #ifdef HAVE_MMX
676 /*****************************************************************************
677  * TestMMX: tests if the processor has MMX support.
678  *****************************************************************************
679  * This function is called if HAVE_MMX is enabled, to check whether the
680  * CPU really supports MMX.
681  *****************************************************************************/
682 static int TestMMX( void )
683 {
684 /* FIXME: under beos, gcc does not support the following inline assembly */ 
685 #ifdef SYS_BEOS
686     return( 1 );
687 #else
688
689     int i_reg, i_dummy = 0;
690
691     /* test for a 386 CPU */
692     asm volatile ( "pushfl
693                     popl %%eax
694                     movl %%eax, %%ecx
695                     xorl $0x40000, %%eax
696                     pushl %%eax
697                     popfl
698                     pushfl
699                     popl %%eax
700                     xorl %%ecx, %%eax
701                     andl $0x40000, %%eax"
702                  : "=a" ( i_reg ) );
703
704     if( !i_reg )
705         return( 0 );
706
707     /* test for a 486 CPU */
708     asm volatile ( "movl %%ecx, %%eax
709                     xorl $0x200000, %%eax
710                     pushl %%eax
711                     popfl
712                     pushfl
713                     popl %%eax
714                     xorl %%ecx, %%eax
715                     pushl %%ecx
716                     popfl
717                     andl $0x200000, %%eax"
718                  : "=a" ( i_reg ) );
719
720     if( !i_reg )
721         return( 0 );
722
723     /* the CPU supports the CPUID instruction - get its level */
724     asm volatile ( "cpuid"
725                  : "=a" ( i_reg ),
726                    "=b" ( i_dummy ),
727                    "=c" ( i_dummy ),
728                    "=d" ( i_dummy )
729                  : "a"  ( 0 ),       /* level 0 */
730                    "b"  ( i_dummy ) ); /* buggy compiler shouldn't complain */
731
732     /* this shouldn't happen on a normal CPU */
733     if( !i_reg )
734         return( 0 );
735
736     /* test for the MMX flag */
737     asm volatile ( "cpuid
738                     andl $0x00800000, %%edx" /* X86_FEATURE_MMX */
739                  : "=a" ( i_dummy ),
740                    "=b" ( i_dummy ),
741                    "=c" ( i_dummy ),
742                    "=d" ( i_reg )
743                  : "a"  ( 1 ),       /* level 1 */
744                    "b"  ( i_dummy ) ); /* buggy compiler shouldn't complain */
745
746     if( !i_reg )
747         return( 0 );
748
749     return( 1 );
750
751 #endif
752 }
753 #endif