mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 17:52:45 +00:00 
			
		
		
		
	WindowServer+LibGUI: Add a server-side clipboard.
On the client side, use GClipboard's data() and set_data(String) to access the global clipboard. :^)
This commit is contained in:
		
							parent
							
								
									eda0866992
								
							
						
					
					
						commit
						6820f9e14f
					
				
					 12 changed files with 250 additions and 2 deletions
				
			
		
							
								
								
									
										51
									
								
								LibGUI/GClipboard.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								LibGUI/GClipboard.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,51 @@ | |||
| #include <LibGUI/GClipboard.h> | ||||
| #include <LibGUI/GEventLoop.h> | ||||
| #include <WindowServer/WSAPITypes.h> | ||||
| #include <LibC/SharedBuffer.h> | ||||
| 
 | ||||
| GClipboard& GClipboard::the() | ||||
| { | ||||
|     static GClipboard* s_the; | ||||
|     if (!s_the) | ||||
|         s_the = new GClipboard; | ||||
|     return *s_the; | ||||
| } | ||||
| 
 | ||||
| GClipboard::GClipboard() | ||||
| { | ||||
| } | ||||
| 
 | ||||
| String GClipboard::data() const | ||||
| { | ||||
|     WSAPI_ClientMessage request; | ||||
|     request.type = WSAPI_ClientMessage::Type::GetClipboardContents; | ||||
|     auto response = GEventLoop::main().sync_request(request, WSAPI_ServerMessage::Type::DidGetClipboardContents); | ||||
|     if (response.clipboard.shared_buffer_id < 0) | ||||
|         return { }; | ||||
|     auto shared_buffer = SharedBuffer::create_from_shared_buffer_id(response.clipboard.shared_buffer_id); | ||||
|     if (!shared_buffer) { | ||||
|         dbgprintf("GClipboard::data() failed to attach to the shared buffer\n"); | ||||
|         return { }; | ||||
|     } | ||||
|     if (response.clipboard.contents_size > shared_buffer->size()) { | ||||
|         dbgprintf("GClipboard::data() clipping contents size is greater than shared buffer size\n"); | ||||
|         return { }; | ||||
|     } | ||||
|     return String((const char*)shared_buffer->data(), response.clipboard.contents_size); | ||||
| } | ||||
| 
 | ||||
| void GClipboard::set_data(const String& data) | ||||
| { | ||||
|     WSAPI_ClientMessage request; | ||||
|     request.type = WSAPI_ClientMessage::Type::SetClipboardContents; | ||||
|     auto shared_buffer = SharedBuffer::create(GEventLoop::main().server_pid(), data.length() + 1); | ||||
|     if (!shared_buffer) { | ||||
|         dbgprintf("GClipboard::set_data() failed to create a shared buffer\n"); | ||||
|         return; | ||||
|     } | ||||
|     memcpy(shared_buffer->data(), data.characters(), data.length() + 1); | ||||
|     request.clipboard.shared_buffer_id = shared_buffer->shared_buffer_id(); | ||||
|     request.clipboard.contents_size = data.length(); | ||||
|     auto response = GEventLoop::main().sync_request(request, WSAPI_ServerMessage::Type::DidSetClipboardContents); | ||||
|     ASSERT(response.clipboard.shared_buffer_id == shared_buffer->shared_buffer_id()); | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling