uploaded original spice-server-0.12.4 and celt-0.5.1.3
[sdk/emulator/libs/spice-server.git] / client / foreign_menu.h
1 /*
2    Copyright (C) 2009 Red Hat, Inc.
3
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifndef _H_FOREIGN_MENU
19 #define _H_FOREIGN_MENU
20
21 #include "named_pipe.h"
22 #include "menu.h"
23
24 class ForeignMenuConnection;
25 struct FrgMenuInit;
26 struct FrgMenuMsg;
27
28 class ForeignMenuInterface : public CommandTarget {
29 public:
30     virtual ~ForeignMenuInterface() {}
31
32     virtual int get_foreign_menu_item_id(int32_t opaque_conn_ref, uint32_t msg_id) = 0;
33     virtual void clear_menu_items(int32_t opaque_conn_ref) = 0;
34     virtual void remove_menu_item(int item_id) = 0;
35     virtual Menu* get_app_menu() = 0;
36     virtual void update_menu() = 0;
37 };
38
39 class ForeignMenu : public NamedPipe::ListenerInterface {
40 public:
41     ForeignMenu(ForeignMenuInterface *handler, bool active = false);
42     virtual ~ForeignMenu();
43
44     ForeignMenu* ref() { _refs++; return this;}
45     void unref() { if (!--_refs) delete this;}
46
47     virtual NamedPipe::ConnectionInterface &create();
48     void add_connection(NamedPipe::ConnectionRef conn_ref, ForeignMenuConnection *conn);
49     void remove_connection(NamedPipe::ConnectionRef conn_ref);
50     void add_sub_menus();
51     void on_command(NamedPipe::ConnectionRef conn_ref, int32_t id);
52     void on_activate();
53     void on_deactivate();
54
55 private:
56     void send_active_state(ForeignMenuConnection *conn, int32_t cmd);
57
58 private:
59     ForeignMenuInterface *_handler;
60     std::map<NamedPipe::ConnectionRef, ForeignMenuConnection*> _connections;
61     NamedPipe::ListenerRef _foreign_menu;
62     bool _active;
63     int _refs;
64 };
65
66 #define FOREIGN_MENU_BUF_SIZE 4096
67
68 class ForeignMenuConnection : public NamedPipe::ConnectionInterface {
69 public:
70     ForeignMenuConnection(ForeignMenuInterface *handler, ForeignMenu& parent);
71     virtual ~ForeignMenuConnection();
72
73     virtual void bind(NamedPipe::ConnectionRef conn_ref);
74     virtual void on_data();
75     bool write_msg(const void *buf, int len);
76     void reset_handler() { _handler = NULL;}
77     void add_sub_menu();
78
79 private:
80     bool read_msgs();
81     bool handle_init(FrgMenuInit *init);
82     bool handle_message(FrgMenuMsg *hdr);
83     int get_item_state(int item_type);
84
85 private:
86     ForeignMenuInterface *_handler;
87     ForeignMenu& _parent;
88     Menu* _sub_menu;
89     bool _initialized;
90     int _write_pending;
91     uint8_t *_write_pos;
92     uint8_t *_read_pos;
93     uint8_t _write_buf[FOREIGN_MENU_BUF_SIZE];
94     uint8_t _read_buf[FOREIGN_MENU_BUF_SIZE];
95     RecurciveMutex _write_lock;
96 };
97
98 #endif