How to calculate photons/light trajectory under gravity I'm aware many questions are out there asking similar questions about photons and gravity and I got the basic concept by searching through them. I will just call it light although it may not be the most accurate term for it, but I'm sure what I'm asking is apparent. (feel free to answer with photons, lightlike curves, light, radiation or anything you think is accurate)
My question is as follows: 
Consider a light beam, initially having a straight line trajectory. If I put a mass $M$ at distance $r$, trajectory will bend slightly towards the objects side. If I put the mass even closer, light might even hit the mass with following a 'death spiral' path. At one specific mass and distance, one can even make light orbit the mass forever if no other interaction exists (or am I mistaken about that?). How do we anticipate which happens?
To discuss on more specific terms:
Say there is a wall at a distance $2d$ from the source, at the middle point $d$ there is an object of mass $M$. We send light with frequency $f$, and the straight line photons would follow without gravity has closest distance of $r$ to the object. How much would light would deviate from its final destination, due to gravity?

 A: Yes, you can calculate light trajectories in gravitational fields. The equation is
$$ \left( \frac{1}{r^2} \frac{\mathrm{d}r}{\mathrm{d}\theta} \right)^2 = \frac{1}{b^2} - \left(1- \frac{2M}{r}\right)\frac{1}{r^2}$$
Here, $r$ is the distance between the light beam and the massive object, $\theta$ is the angle of the light beam,  $M$ is the mass of the object, and $b$ is the impact parameter (the minimum distance between the light beam and mass if there was no bending). See the image below.
Integrating over this equation gives the total deflection of the light during its passage near the massive object. As given in a comment, it is:
$$\Delta \theta = \frac{4MG}{Rc^2}.$$
Deriving these equations requires some very basic General Relativity. You can find a derivation on this lovely website by Christian Magnan, who cites the book "Exploring Black Holes, Introduction to General Relativity" by Edwin F. Taylor and John Archibald Wheeler.

A: I forget who said it, but it's a good rule of thumb "Mass-energy tells space how to bend, space tells mass-energy how to move."
Space-time has a certain shape. Just as the shortest distance between two points is not a straight line on the surface of a globe is not a straight line, the shortest distance between two points in space-time is also not a straight line under the influence of gravity .
The shortest distance path on a surface is called a geodesic, whatever the shape of the surface. So a great circle on a sphere is a geodesic, so is a straight line on the euclidean plane. One is not the same as the other, but they both are the paths an object would take if it is not under the influence of an external force. 
Consider the graph of the function $y=x^2$. And suppose you travel along that graph at a constant speed. 
$$y=x^2$$
$$\dot{x}^2+\dot{y}^2=u^2.$$
From this it follows that:
$$\dot{x}=\frac{u}{\sqrt{1+4x^2}}$$
$$\dot{y}=\frac{2xu}{\sqrt{1+4x^2}}$$
So, 
$$\ddot{x}=\frac{4xu}{(1+4x^2)^\frac{3}{2}}$$
$$\ddot{y}=\frac{-2u}{(1+4x^2)^\frac{3}{2}}$$
It's clear that despite maintaining a constant speed, there is a changing velocity, an acceleration since the second derivatives with respect to time are non-zero. But acceleration in the absence of an applied net force is a pseudo-force. 
Now the curvature of a curve is $k=\frac{\frac{d^2y}{dx^2}}{(1+(\frac{dy}{dx})^2)^\frac{3}{2}}=\frac{2}{(1+4x^2)^\frac{3}{2}}$ in the case of our curve here.
Notice that $k$ divides both acceleration terms. This is no coincidence. 
Let $\frac{d\vec{s}}{dt}$ be velocity along the path. By definition, curvature is : 
$$k=\frac{d\theta}{ds}$$ where $\theta$ is $tan^-1(dy/dx)$.
Another definition of curvature is $\frac{d\vec{T}}{dt}$, where $\vec{T}$ is the unit tangent vector to the curve, i.e. $\vec{T}=\frac{d\vec{s}}{ds}=\frac{d\vec{s}/dt}{ds/dt}$
Now $ds/dt=u$ and we are holding $u$ constant, so $du/dt=0$. 
So $\frac{d}{dt}(\frac{d\vec{s}/dt}{ds/dt})=\frac{1}{u}\frac{d^2\vec{s}}{dt^2}=\vec{k}$
So we expect the acceleration to be the product of the constant velocity and the curvature. 
Notice from this, if we have no curvature, we have no acceleration. Zero curvature implies a straight line and no pseudo-force. 
Curvature has a broader definition in 4d space-time. 
The equation of geodesics becomes:
$$\frac{d^2x^a}{dt^2}=\sum_{\mu=0}^3\sum_{\nu=0}^3-\Gamma_{\mu\nu}^a\frac{dx^\mu}{dt}\frac{dx^\nu}{dt}$$
Here $x^a$ means the $a_{th}$ component of path $\vec{s}$. 
$\Gamma^a_{\mu\nu}$ is the Christofel Symbol of the Second Kind. 
Notice again we have a second time derivative being the product of some first time derivative. As it happens the Christofel Symbol has a close relation to curvature. 
The Stress-Energy Tensor, roughly equivalent to the mass distribution of Newtonian Gravity, dictates curvature of your region of space which can be expressed in terms of the Christofel Symbols. 
If we know the Christofel Symobols, then the geodesic equation will tell us the path particles will take in the absence of an applied force. It will tell us how photons move. 
Notice the geodesic equation makes no explicit reference to some mass energy distribution. The Christofel symbols dictate our geometry. All we need to know is the geometry to find our paths. 
So that quote above actually breaks up the problem of General Relativity. Understanding geometry of space time, and understanding how that is manipulated by mass energy. For the sake of theoretical work, they can be given arbitrarily to see what paths result. 
So curvature determines which paths exist in the first place and and light "selects" the Least Time Path according to Fermat's Principle. 
