Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to write code or a script to lock the PC when my C++ program receives a specific command from Ethernet.

Is there any way to show the Login screen from the command line? I want to lock the screen from a c++ code. Actually I want to do something like Ctrl + Alt + L programmatically (from a c++ code). Thanks

Edit: I found the way. For those who want to know, as Lubuntu uses LXDE desktop, it has some methods to do thing like it. The command for locking the screen is lxlock.

share|improve this question
    
Not really a complete answer, but I would look for a way to do this with DBus bindings. This is very elegant as it's almost universal for every desktop environment. And LXDE supports DBus very well, looking at the source code. Try to find the Lock command using a dbus exlorer like qdbusviewer and a way to call the functions programmatically in C++. –  gertvdijk Jan 19 '13 at 12:48
add comment

3 Answers 3

In accordance with what @gertvdijk said, you can use DBus:

qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock

I have used that in my lock and off project.

share|improve this answer
add comment

Your question as it is written now is a bit unclear. I'll try to guess what you want and give you a couple answers to try.

  1. Ctrl + Alt + L will lock the screen. Everything is still running, you are logged in, but the screen is locked.
  2. Ctrl + Alt + T opens the Terminal, then type gnome-session-quit

Note that if #2 is what you want, this is an exact duplicate of this question 'How can you log out via the terminal', which is where I copied the second answer from.

Please edit your question so it is clearer, or so we can close it as a duplicate.

share|improve this answer
2  
I want to do something like Ctrl + Alt + L via a c++ code. I want to write a code to lock the PC when my program receives a specific command from ethernet. –  oMatrix Jan 19 '13 at 11:54
    
@oMatrix I've tried editing your question for you to make it clearer. Please take a look and re-edit it if I missed anything or got it wrong. Also, this may be off-topic here, or at least a better fit on one of the programming-specific sites like stackoverflow. –  Tom Brossman Jan 19 '13 at 12:03
    
Thanks for your help. I thought may be there's something like gnome-session-quit for locking the screen and as I'm using Lubuntu, I thought it's the best place to ask this question. And actually my question is not a programming related question in my opinion. Thanks again. –  oMatrix Jan 19 '13 at 12:18
3  
This question is on-topic. Questions for Users and Developers of Ubuntu and official derivatives. –  Marco Ceppi Jan 19 '13 at 12:34
add comment

I found the way. For those who want to know. As Lubuntu uses LXDE desktop, it has some methods to do thing like it. The command for locking the screen is lxlock.

share|improve this answer
5  
Pardon me? You clearly stated in your question you were looking for a way to do this programmatically in C++ and now you answer your own question with a Shell script? (source of lxlock is here which lists the Dbus commands to send) And here is a tutorial on how to program with Dbus in C++. –  gertvdijk Jan 19 '13 at 17:29
2  
-1 for unclear answer. –  Olcay Ertaş Jan 19 '13 at 22:48
    
sorry? I think you should read the question with more attention. I wrote "I want to write code <<<<or a script>>>> to lock the PC .....". So I think you shouldn't give me -1. I just wanted to lock the desktop from my C++ code. so I can use script, Dbus or any other way that works. –  oMatrix Jan 20 '13 at 7:16
2  
@oMatrix see your earlier comment on the answer of Tom Brossman. You said you needed C++ code. Clearly. You can't expect other users to answer your question when you change the question on such an important part by yourself. This deserves really -1's, sorry. You could have deleted your question and asked another one. –  gertvdijk Jan 20 '13 at 19:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.