Thank you!
I was not precise enough in my earlier post:
I had already found the code to detect the connected headphones, the part that I was missing was how to turn off the speaker as soon as the headphones are detected (and if it was possible to turn it off independently from the speaker).

Thanks to your comment and some poking around on GitHub I realized that I overlooked the GPIO extension. There is a SPK_EN pin there (Pin 1 on the first extension) that connects to the onboard amplifier and, according to its datasheet, can be used to turn the amplifier off.

Since the headphones are not connected to the amplifier, this should mute the speaker without influencing the headphones - I haven't tried it yet, but that's my guess based on the schematics.