A Ugandan court has ordered the military and police to leave the home of opposition politician Bobi Wine.
He has not left his house in the outskirts of the capital Kampala since voting in the presidential election 11 days ago, in which he was declared runner-up to President Yoweri Museveni.
Monday's ruling by the High Court in Kampala comes after a petition by Bobi Wine's lawyers.
The state argued it needed to prevent him organising protests.
But the High Court has now ruled that Bobi Wine's detention was unlawful, with Justice Micheal Elubu telling the court it was also an "an infringement on his personal liberties".
The court said if Bobi Wine had committed any offence, he should be taken to an authorised detention centre. The army said it would respect the ruling, but has yet to withdraw its troops or nearby road blocks.
Dozens of people were killed during campaigning - many were shot by security forces.
Bobi Wine alleges that the election, in which he won 35% of the vote as National Unity Platform leader, was rigged.
Uganda's Electoral Commission said the vote was peaceful, but the EU, United Nations and several rights groups have raised concerns. Aside from an African Union mission, no major international group monitored the vote.
In a phone interview with the BBC World Service days after the poll, Bobi Wine, a popular singer, 38, whose real name is Robert Kyagulanyi, said he was "worried about my life and the life of my wife".
The BBC's Patience Atuhaire in Kampala says armed police and soldiers have been stationed outside the musician-turned-politician's residence, not allowing anyone in or out.
Several more have been patrolling the neighbourhood.
What next for Bobi Wine?
By Patience Atuhaire, BBC News, Kampala
If the security forces withdraw immediately, Bobi Wine still has at least four days to file a court petition challenging the result of the vote.
If he takes that route, the court must hear and rule on the petition within 45 days.
National Unity Platform officials had told the BBC that a way forward had not been mapped, as they were unable to access their party president.
In a Facebook Live address on Friday, Bobi Wine said that Ugandans had a right to protest if they so wished, though he did not outright call on his supporters to take to the streets.
He is not the first Ugandan opposition leader to be kept under home confinement. Kizza Besigye was kept under house arrest for more than two months after the 2016 election.
Following the 2011 election, Dr Besigye launched a series of walk-to-work protests, rather than challenge the poll result in court. They were all violently broken up by the security forces.