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