# Automatic frame titles subtitles, with condition

I'm trying to set up automatic frame titles and subtitles as follows:

• If a frame has a title or subtitle set (even to {}, it is used).
• If subsection title is not empty, use section title as a frame subtitle, and subsection title as a frame title.
• Otherwise, use section title as a frame title, frame does not have a subtitle.

Here is a code that I tried (does not follow the first rule above, taken from Automatic frame titles and subtitles). It does not work as expected — with \subsection*{} frame title is empty and frame subtitle is set to section title.

\ifx\insertsubsection\empty
\else
\fi
}{}

\ifx\insertsubsection\empty
\else
\fi
}{}

\makeatletter
\CheckCommand*\beamer@checkframetitle{%
\@ifnextchar\bgroup\beamer@inlineframetitle{}}
\renewcommand*\beamer@checkframetitle{%
\global\let\beamer@frametitle\relax\@ifnextchar%
\bgroup\beamer@inlineframetitle{}}
\makeatother

Any hints?

Consider the following code:

\documentclass{beamer}

\makeatletter
\CheckCommand*\beamer@checkframetitle{%
\@ifnextchar\bgroup\beamer@inlineframetitle{}}
\renewcommand*\beamer@checkframetitle{%
\global\let\beamer@frametitle\relax\@ifnextchar%
\bgroup\beamer@inlineframetitle{}}
\makeatother

\ifx\insertframetitle\empty
\ifx\insertframesubtitle\empty
\ifx\insertsubsection\empty
\else
\fi
\else
\fi
\else
\fi
}{}

\begin{document}

\section{section}
\subsection{subsection}
\begin{frame}
\frametitle{title}
\framesubtitle{subtitle}
Frame with frametitle and framesubtitle set (section and subsection are ignored)
\end{frame}

\begin{frame}
%\frametitle{title}
\framesubtitle{subtitle}
Frame with framesubtitle only (section and subsection are ignored).
\end{frame}

\begin{frame}
%\frametitle{title}
%\framesubtitle{subtitle}
Frame with no frametitle, no framesubtitle, part of a subsection (subsection title is used as frametitle and section title as framesubtitle)
\end{frame}

\section{section}
\begin{frame}
%\subsection{subsection}
%\frametitle{title}
%\framesubtitle{subtitle}
Frame with no frametitle, no framesubtitle and not part of a subsection. Section title is used as frametitle.
\end{frame}
\end{document}

It also works if you have a \subsection*{} (i.e. in such a case it still shows section name as frame subtitle if no title or subtitle are present. It does not work if you have both \frametitle{} and \framesubtitle{}, i.e. in such a case it will show section or subsection name as frame title. But maybe this can be workarounded with no harm replacing \frametitle{} with \frametitle{~}.