File: alutInit.c

package info (click to toggle)
freealut 1.1.0-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,144 kB
  • sloc: sh: 8,617; ansic: 2,738; makefile: 89
file content (158 lines) | stat: -rw-r--r-- 3,360 bytes parent folder | download | duplicates (5)
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
#include "alutInternal.h"

static enum
{
  Unintialized,                 /* ALUT has not been initialized yet or has been de-initialised */
  ALUTDeviceAndContext,         /* alutInit has been called successfully */
  ExternalDeviceAndContext      /* alutInitWithoutContext has been called */
} initialisationState = Unintialized;

/*
 * Note: alutContext contains something valid only when initialisationState
 * contains ALUTDeviceAndContext.
 */
static ALCcontext *alutContext;

ALboolean
_alutSanityCheck (void)
{
  ALCcontext *context;

  if (initialisationState == Unintialized)
    {
      _alutSetError (ALUT_ERROR_INVALID_OPERATION);
      return AL_FALSE;
    }

  context = alcGetCurrentContext ();
  if (context == NULL)
    {
      _alutSetError (ALUT_ERROR_NO_CURRENT_CONTEXT);
      return AL_FALSE;
    }

  if (alGetError () != AL_NO_ERROR)
    {
      _alutSetError (ALUT_ERROR_AL_ERROR_ON_ENTRY);
      return AL_FALSE;
    }

  if (alcGetError (alcGetContextsDevice (context)) != ALC_NO_ERROR)
    {
      _alutSetError (ALUT_ERROR_ALC_ERROR_ON_ENTRY);
      return AL_FALSE;
    }

  return AL_TRUE;
}

ALboolean
alutInit (int *argcp, char **argv)
{
  ALCdevice *device;
  ALCcontext *context;

  if (initialisationState != Unintialized)
    {
      _alutSetError (ALUT_ERROR_INVALID_OPERATION);
      return AL_FALSE;
    }

  if ((argcp == NULL) != (argv == NULL))
    {
      _alutSetError (ALUT_ERROR_INVALID_VALUE);
      return AL_FALSE;
    }

  device = alcOpenDevice (NULL);
  if (device == NULL)
    {
      _alutSetError (ALUT_ERROR_OPEN_DEVICE);
      return AL_FALSE;
    }

  context = alcCreateContext (device, NULL);
  if (context == NULL)
    {
      alcCloseDevice (device);
      _alutSetError (ALUT_ERROR_CREATE_CONTEXT);
      return AL_FALSE;
    }

  if (!alcMakeContextCurrent (context))
    {
      alcDestroyContext (context);
      alcCloseDevice (device);
      _alutSetError (ALUT_ERROR_MAKE_CONTEXT_CURRENT);
      return AL_FALSE;
    }

  initialisationState = ALUTDeviceAndContext;
  alutContext = context;
  return AL_TRUE;
}

ALboolean
alutInitWithoutContext (int *argcp, char **argv)
{
  if (initialisationState != Unintialized)
    {
      _alutSetError (ALUT_ERROR_INVALID_OPERATION);
      return AL_FALSE;
    }

  if ((argcp == NULL) != (argv == NULL))
    {
      _alutSetError (ALUT_ERROR_INVALID_VALUE);
      return AL_FALSE;
    }

  initialisationState = ExternalDeviceAndContext;
  return AL_TRUE;
}

ALboolean
alutExit (void)
{
  ALCdevice *device;

  if (initialisationState == Unintialized)
    {
      _alutSetError (ALUT_ERROR_INVALID_OPERATION);
      return AL_FALSE;
    }

  if (initialisationState == ExternalDeviceAndContext)
    {
      initialisationState = Unintialized;
      return AL_TRUE;
    }

  if (!_alutSanityCheck ())
    {
      return AL_FALSE;
    }

  if (!alcMakeContextCurrent (NULL))
    {
      _alutSetError (ALUT_ERROR_MAKE_CONTEXT_CURRENT);
      return AL_FALSE;
    }

  device = alcGetContextsDevice (alutContext);
  alcDestroyContext (alutContext);
  if (alcGetError (device) != ALC_NO_ERROR)
    {
      _alutSetError (ALUT_ERROR_DESTROY_CONTEXT);
      return AL_FALSE;
    }

  if (!alcCloseDevice (device))
    {
      _alutSetError (ALUT_ERROR_CLOSE_DEVICE);
      return AL_FALSE;
    }

  initialisationState = Unintialized;
  return AL_TRUE;
}