Skip to content

Commit 08de5ea

Browse files
committed
Sound: merges isMain and isActive to type
1 parent c7ecc33 commit 08de5ea

10 files changed

Lines changed: 65 additions & 50 deletions

File tree

src/detection/sound/sound.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ typedef struct FFSoundDevice {
1010
FFstrbuf name;
1111
FFstrbuf platformApi;
1212
uint8_t volume; // 0-100%
13-
bool main;
14-
bool active;
13+
FFSoundType type;
1514
} FFSoundDevice;
1615

1716
const char* ffDetectSound(FFSoundOptions* options, FFlist* devices /* List of FFSoundDevice */);

src/detection/sound/sound_apple.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ const char* ffDetectSound(FFSoundOptions* options, FFlist* devices /* List of FF
5555
}
5656

5757
FFSoundDevice* device = FF_LIST_ADD(FFSoundDevice, *devices);
58-
device->main = deviceId == mainDeviceId;
59-
device->active = !!active;
58+
device->type = (deviceId == mainDeviceId ? FF_SOUND_TYPE_MAIN : FF_SOUND_TYPE_NONE) |
59+
(active ? FF_SOUND_TYPE_ACTIVE : FF_SOUND_TYPE_NONE);
6060
device->volume = FF_SOUND_VOLUME_UNKNOWN;
6161
ffStrbufInit(&device->identifier);
6262
ffStrbufInit(&device->name);

src/detection/sound/sound_bsd.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ const char* ffDetectSound(FFSoundOptions* options, FFlist* devices) {
8383
mutemask & SOUND_MASK_VOLUME ? 0 :
8484
#endif
8585
((uint8_t) volume /*left*/ + (uint8_t) (volume >> 8) /*right*/) / 2;
86-
device->active = true;
87-
device->main = isMain;
86+
device->type = FF_SOUND_TYPE_ACTIVE | (isMain ? FF_SOUND_TYPE_MAIN : FF_SOUND_TYPE_NONE);
8887
}
8988

9089
return NULL;

src/detection/sound/sound_haiku.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ const char* ffDetectSound(FF_A_UNUSED FFSoundOptions* options, FFlist* devices /
3030
ffStrbufInitStatic(&device->platformApi, "MediaKit");
3131
// We'll check the Mixer actually
3232
device->volume = 0;
33-
device->active = true;
34-
device->main = true;
33+
device->type = (FFSoundType) (FF_SOUND_TYPE_ACTIVE | FF_SOUND_TYPE_MAIN);
3534

3635
roster->ReleaseNode(mediaNode);
3736

src/detection/sound/sound_linux.c

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ static void paSinkInfoCallback(FF_A_UNUSED pa_context* c, const pa_sink_info* i,
3636
ffStrbufTrimRightSpace(&device->name);
3737
ffStrbufTrimLeft(&device->name, ' ');
3838
device->volume = i->mute ? 0 : (uint8_t) ((i->volume.values[0] * 100 + PA_VOLUME_NORM / 2 /*round*/) / PA_VOLUME_NORM);
39-
device->active = isActive;
40-
device->main = isMain;
39+
device->type = (isMain ? FF_SOUND_TYPE_MAIN : FF_SOUND_TYPE_NONE) | (isActive ? FF_SOUND_TYPE_ACTIVE : FF_SOUND_TYPE_NONE);
4140
}
4241

4342
static void paServerInfoCallback(FF_A_UNUSED pa_context* c, const pa_server_info* i, void* userdata) {
@@ -70,6 +69,7 @@ static const char* detectSound(FFSoundOptions* options, FFlist* devices) {
7069
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(pulse, pa_context_get_sink_info_list)
7170
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(pulse, pa_context_get_server_info)
7271
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(pulse, pa_context_unref)
72+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(pulse, pa_operation_cancel)
7373
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(pulse, pa_operation_get_state)
7474
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(pulse, pa_operation_unref)
7575

@@ -90,56 +90,66 @@ static const char* detectSound(FFSoundOptions* options, FFlist* devices) {
9090
return "Failed to create pulseaudio context";
9191
}
9292

93+
struct DetectionInfoBundle bundle = {
94+
.serverName = ffStrbufCreate(),
95+
.defaultDeviceId = ffStrbufCreate(),
96+
.result = devices,
97+
.options = options,
98+
};
99+
const char* error = NULL;
100+
93101
if (ffpa_context_connect(context, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0) {
94-
ffpa_context_unref(context);
95-
ffpa_mainloop_free(mainloop);
96-
return "Failed to connect to pulseaudio context";
102+
error = "Failed to connect to pulseaudio context";
103+
goto exit;
97104
}
98105

99106
pa_context_state_t state;
100107
while ((state = ffpa_context_get_state(context)) != PA_CONTEXT_READY) {
101108
if (!PA_CONTEXT_IS_GOOD(state)) {
102-
ffpa_context_unref(context);
103-
ffpa_mainloop_free(mainloop);
104-
return "Failed to get pulseaudio context state";
109+
error = "Failed to get pulseaudio context state";
110+
goto exit;
105111
}
106112

107113
ffpa_mainloop_iterate(mainloop, 1, NULL);
108114
}
109115

110-
struct DetectionInfoBundle bundle = {
111-
.serverName = ffStrbufCreate(),
112-
.defaultDeviceId = ffStrbufCreate(),
113-
.result = devices,
114-
.options = options,
115-
};
116-
117116
{
118117
pa_operation* operation = ffpa_context_get_server_info(context, paServerInfoCallback, &bundle);
118+
if (!operation) {
119+
error = "Failed to get pulseaudio server info";
120+
goto exit;
121+
}
119122
while (ffpa_operation_get_state(operation) == PA_OPERATION_RUNNING) {
120123
ffpa_mainloop_iterate(mainloop, 1, NULL);
121124
}
122125

123126
ffpa_operation_unref(operation);
124127
}
125128

126-
pa_operation* operation = ffpa_context_get_sink_info_list(context, paSinkInfoCallback, &bundle);
127-
if (!operation) {
128-
ffpa_context_unref(context);
129-
ffpa_mainloop_free(mainloop);
130-
return "Failed to get pulseaudio sink info list";
131-
}
129+
{
130+
pa_operation* operation = ffpa_context_get_sink_info_list(context, paSinkInfoCallback, &bundle);
131+
if (!operation) {
132+
error = "Failed to get pulseaudio sink info list";
133+
goto exit;
134+
}
132135

133-
while (ffpa_operation_get_state(operation) == PA_OPERATION_RUNNING) {
134-
ffpa_mainloop_iterate(mainloop, 1, NULL);
135-
}
136+
while (ffpa_operation_get_state(operation) == PA_OPERATION_RUNNING) {
137+
if (options->soundType & FF_SOUND_TYPE_MAIN && devices->length > 0) {
138+
ffpa_operation_cancel(operation);
139+
}
136140

137-
ffpa_operation_unref(operation);
141+
ffpa_mainloop_iterate(mainloop, 1, NULL);
142+
}
138143

144+
ffpa_operation_unref(operation);
145+
}
139146

147+
exit:
148+
ffStrbufDestroy(&bundle.serverName);
149+
ffStrbufDestroy(&bundle.defaultDeviceId);
140150
ffpa_context_unref(context);
141151
ffpa_mainloop_free(mainloop);
142-
return NULL;
152+
return error;
143153
}
144154

145155
#endif // FF_HAVE_PULSE

src/detection/sound/sound_nbsd.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ const char* ffDetectSound(FFSoundOptions* options, FFlist* devices) {
5151
ffStrbufTrimRightSpace(&device->name);
5252
ffStrbufInitF(&device->platformApi, "%s", "SunAudio");
5353
device->volume = (uint8_t) ((ai.play.gain * 100 + AUDIO_MAX_GAIN / 2) / AUDIO_MAX_GAIN);
54-
device->active = true;
55-
device->main = isMain;
54+
device->type = FF_SOUND_TYPE_ACTIVE | (isMain ? FF_SOUND_TYPE_MAIN : FF_SOUND_TYPE_NONE);
5655
}
5756

5857
return NULL;

src/detection/sound/sound_obsd.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ const char* ffDetectSound(FFSoundOptions* options, FFlist* devices) {
7575
ffStrbufInitS(&device->name, bundle.name);
7676
ffStrbufInitS(&device->identifier, SIO_DEVANY);
7777
ffStrbufInitStatic(&device->platformApi, "sndio");
78-
device->active = true;
79-
device->main = true;
8078
device->volume = 0;
79+
device->type = FF_SOUND_TYPE_ACTIVE | FF_SOUND_TYPE_MAIN;
8180

8281
double totalLevel = 0;
8382
for (uint8_t i = 0; i < bundle.iLevel; ++i) {

src/detection/sound/sound_sunos.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ const char* ffDetectSound(FFSoundOptions* options, FFlist* devices) {
101101
ffStrbufTrimRightSpace(&device->name);
102102
ffStrbufInitF(&device->platformApi, "%s %s", info.product, info.version);
103103
device->volume = (uint8_t) volume;
104-
device->active = !!mi.enabled;
105-
device->main = isMain;
104+
device->type = (mi.enabled ? FF_SOUND_TYPE_ACTIVE : FF_SOUND_TYPE_NONE) |
105+
(isMain ? FF_SOUND_TYPE_MAIN : FF_SOUND_TYPE_NONE);
106106
}
107107

108108
return NULL;

src/detection/sound/sound_windows.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ static const char* detectSoundDevice(FFlist* devices /* List of FFSoundDevice */
3636
}
3737

3838
FFSoundDevice* device = FF_LIST_ADD(FFSoundDevice, *devices);
39-
device->main = !mainDeviceId || wcscmp(immDeviceId, mainDeviceId) == 0;
40-
device->active = !!(immState & DEVICE_STATE_ACTIVE);
39+
device->type = (FFSoundType) ((!mainDeviceId || wcscmp(immDeviceId, mainDeviceId) == 0 ? FF_SOUND_TYPE_MAIN : FF_SOUND_TYPE_NONE) |
40+
((immState & DEVICE_STATE_ACTIVE) ? FF_SOUND_TYPE_ACTIVE : FF_SOUND_TYPE_NONE));
4141
device->volume = FF_SOUND_VOLUME_UNKNOWN;
4242
ffStrbufInitWS(&device->identifier, immDeviceId);
4343
ffStrbufInit(&device->name);

src/modules/sound/sound.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static void printDevice(FFSoundOptions* options, const FFSoundDevice* device, ui
3434
}
3535

3636
if (!(percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT)) {
37-
if (device->main && index > 0) {
37+
if ((device->type & FF_SOUND_TYPE_MAIN) && index > 0) {
3838
ffStrbufAppendS(&str, " (*)");
3939
}
4040
}
@@ -52,8 +52,11 @@ static void printDevice(FFSoundOptions* options, const FFSoundDevice* device, ui
5252
}
5353
}
5454

55+
bool isMain = !!(device->type & FF_SOUND_TYPE_MAIN);
56+
bool isActive = !!(device->type & FF_SOUND_TYPE_ACTIVE);
5557
FF_PRINT_FORMAT_CHECKED(FF_SOUND_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
56-
FF_ARG(device->main, "is-main"),
58+
FF_ARG(isMain, "is-main"),
59+
FF_ARG(isActive, "is-active"),
5760
FF_ARG(device->name, "name"),
5861
FF_ARG(percentageNum, "volume-percentage"),
5962
FF_ARG(device->identifier, "identifier"),
@@ -155,18 +158,24 @@ bool ffGenerateSoundJsonResult(FFSoundOptions* options, yyjson_mut_doc* doc, yyj
155158
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
156159
FF_LIST_FOR_EACH (FFSoundDevice, item, result) {
157160
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
158-
yyjson_mut_obj_add_bool(doc, obj, "active", item->active);
159-
yyjson_mut_obj_add_bool(doc, obj, "main", item->main);
161+
162+
yyjson_mut_obj_add_strbuf(doc, obj, "name", &item->name);
163+
yyjson_mut_obj_add_strbuf(doc, obj, "identifier", &item->identifier);
164+
yyjson_mut_obj_add_strbuf(doc, obj, "platformApi", &item->platformApi);
165+
166+
yyjson_mut_val* type = yyjson_mut_obj_add_arr(doc, obj, "type");
167+
if (item->type & FF_SOUND_TYPE_MAIN) {
168+
yyjson_mut_arr_add_str(doc, type, "main");
169+
}
170+
if (item->type & FF_SOUND_TYPE_ACTIVE) {
171+
yyjson_mut_arr_add_str(doc, type, "active");
172+
}
160173

161174
if (item->volume != FF_SOUND_VOLUME_UNKNOWN) {
162175
yyjson_mut_obj_add_uint(doc, obj, "volume", item->volume);
163176
} else {
164177
yyjson_mut_obj_add_null(doc, obj, "volume");
165178
}
166-
167-
yyjson_mut_obj_add_strbuf(doc, obj, "name", &item->name);
168-
yyjson_mut_obj_add_strbuf(doc, obj, "identifier", &item->identifier);
169-
yyjson_mut_obj_add_strbuf(doc, obj, "platformApi", &item->platformApi);
170179
}
171180

172181
FF_LIST_FOR_EACH (FFSoundDevice, device, result) {
@@ -200,6 +209,7 @@ FFModuleBaseInfo ffSoundModuleInfo = {
200209
.generateJsonConfig = (void*) ffGenerateSoundJsonConfig,
201210
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
202211
{ "Is main sound device", "is-main" },
212+
{ "Is active sound device", "is-active" },
203213
{ "Device name", "name" },
204214
{ "Volume (in percentage num)", "volume-percentage" },
205215
{ "Identifier", "identifier" },

0 commit comments

Comments
 (0)