shithub: choc

Download patch

ref: 604e09624e22148458ddb12526f2646e600f29bd
parent: 0517fad3f4e89304dc7a4ad18c5785e02173ca6c
author: Simon Howard <fraggle@gmail.com>
date: Sun Sep 2 20:43:29 EDT 2007

Try to open /dev/speaker in the parent process, so that we can tell if
we don't have permission to open it before we fork.

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 966

--- a/pcsound/pcsound_bsd.c
+++ b/pcsound/pcsound_bsd.c
@@ -142,25 +142,11 @@
     sleep_adjust = actual_time - ms;
 }
 
-static void SoundServer(void)
+static void SoundServer(int speaker_handle)
 {
-    int speaker_handle;
     tone_t tone;
     int result;
 
-    // Try to open the speaker device
-
-    speaker_handle = open(SPEAKER_DEVICE, O_WRONLY);
-
-    if (speaker_handle == -1)
-    {
-        // Don't have permissions for the console device?
-
-	fprintf(stderr, "PCSound_BSD_Init: Failed to open '%s': %s\n",
-			SPEAKER_DEVICE, strerror(errno));
-        return;
-    }
-
     // Run in a loop, invoking the callback
 
     for (;;)
@@ -181,10 +167,6 @@
 
         AdjustedBeep(speaker_handle, tone.duration, tone.frequency);
     }
-
-    // Finished, close the handle
-
-    close(speaker_handle);
 }
 
 // Start up the sound server.  Returns non-zero if successful.
@@ -192,12 +174,27 @@
 static int StartSoundServer(void)
 {
     int result;
+    int speaker_handle;
 
+    // Try to open the speaker device
+
+    speaker_handle = open(SPEAKER_DEVICE, O_WRONLY);
+
+    if (speaker_handle == -1)
+    {
+        // Don't have permissions for the console device?
+
+	fprintf(stderr, "StartSoundServer: Failed to open '%s': %s\n",
+                        SPEAKER_DEVICE, strerror(errno));
+        return 0;
+    }
+
     // Create a pipe for communications
 
     if (socketpair(AF_UNIX, SOCK_STREAM, 0, sound_server_pipe) < 0)
     {
         perror("socketpair");
+        close(speaker_handle);
         return 0;
     }
 
@@ -210,6 +207,7 @@
     if (result < 0)
     {
         fprintf(stderr, "Failed to fork sound server!\n");
+        close(speaker_handle);
         return 0;
     }
     else if (result == 0)
@@ -216,7 +214,8 @@
     {
         // This is the child (sound server)
 
-        SoundServer();
+        SoundServer(speaker_handle);
+        close(speaker_handle);
 
         exit(0);
     }