Release s3d 0.2.2
[s3d.git] / server / event.c
blobb36a5543552c5296b5e42663b54791d6fb8043a8
1 /*
2 * event.c
4 * Copyright (C) 2004-2011 Simon Wunderlich <dotslash@packetmixer.de>
6 * This file is part of s3d, a 3d network display server.
7 * See http://s3d.berlios.de/ for more updates.
9 * s3d is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * s3d is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with s3d; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "global.h"
25 #include "proto.h"
26 #include <stdio.h> /* sprintf() */
27 #ifdef WIN32
28 #include <winsock2.h>
29 #else
30 #include <netinet/in.h> /* htonl(),htons() */
31 #endif
32 #include <string.h> /* strlen(),strcpy() */
34 /* I don't plan to keep this until the end, but it can show us how */
35 /* to interact ... */
36 int event_obj_click(struct t_process *p, int32_t oid)
38 uint32_t moid = htonl(oid);
39 s3dprintf(MED, "telling client that oid %d got clicked", oid);
40 prot_com_out(p, S3D_P_S_CLICK, (uint8_t *) & moid, 4);
41 return 0;
44 /* this functions sends keystroke events to the focused program. */
45 /* maybe mcp-keystrokes should be catched here. */
46 /* state = 0 -> pressed, 1 -> released */
47 int event_key_pressed(uint16_t key, uint16_t uni, uint16_t mod, int state)
49 uint16_t k[4];
50 struct t_obj *o;
51 k[0] = htons(key);
52 k[1] = htons(uni);
53 k[2] = htons(mod);
54 k[3] = htons(state);
55 if (OBJ_VALID(get_proc_by_pid(MCP), focus_oid, o))
56 prot_com_out(get_proc_by_pid(o->virtual_pid), S3D_P_S_KEY, (uint8_t *) k, 8);
57 prot_com_out(get_proc_by_pid(MCP), S3D_P_S_KEY, (uint8_t *) k, 8); /* mcp always gets a copy */
58 return 0;
61 /* mouse button changes are sent to the client */
62 int event_mbutton_clicked(uint8_t button, uint8_t state)
64 struct t_obj *o;
65 uint8_t b[2];
66 b[0] = button;
67 b[1] = state;
68 if (OBJ_VALID(get_proc_by_pid(MCP), focus_oid, o))
69 prot_com_out(get_proc_by_pid(o->virtual_pid), S3D_P_S_MBUTTON, (uint8_t *) & b, 2);
70 prot_com_out(get_proc_by_pid(MCP), S3D_P_S_MBUTTON, (uint8_t *) & b, 2); /* mcp always gets a copy */
71 return 0;
74 /* tell the client something about us */
75 int event_init(struct t_process *p)
77 char s[S3D_NAME_MAX + 3];
78 sprintf(s, "%c%c%c%s", S3D_SERVER_MAJOR, S3D_SERVER_MINOR, S3D_SERVER_PATCH, S3D_SERVER_NAME); /* thanks award */
79 prot_com_out(p, S3D_P_S_INIT, (uint8_t *) s, strlen(S3D_SERVER_NAME) + 4);
80 return 0;
83 /* this lets a process quit gracefully ... */
84 int event_quit(struct t_process *p)
86 prot_com_out(p, S3D_P_S_QUIT, NULL, 0);
87 s3dprintf(HIGH, "sending pid %d QUIT signal", p->id);
88 process_del(p->id);
89 return 0;
92 /* the cam changed?! we should run and tell this the mcp/focused client! */
93 int event_cam_changed(void)
95 struct t_process *p;
96 struct t_obj *o;
97 p = get_proc_by_pid(MCP);
98 event_obj_info(p, 0);
99 if (OBJ_VALID(p, focus_oid, o))
100 event_obj_info(get_proc_by_pid(o->virtual_pid), 0);
101 return 0;
104 /* same for the mouse movement! */
105 int event_ptr_changed(void)
107 struct t_process *p;
108 struct t_obj *o;
109 p = get_proc_by_pid(MCP);
110 event_obj_info(p, get_pointer(p));
111 if (OBJ_VALID(p, focus_oid, o)) {
112 p = get_proc_by_pid(o->virtual_pid); /* focused program pointer */
113 event_obj_info(p, get_pointer(p));
115 return 0;
118 /* inform client about an available shm-segment for the texture */
119 int event_texshm(struct t_process *p, int32_t oid, int32_t tex)
121 struct t_obj *o;
122 struct {
123 int32_t oid, tex, shmid;
124 uint16_t tw, th, w, h;
125 } __attribute__ ((__packed__)) shmtex_packet;
126 if (OBJ_VALID(p, oid, o)) {
127 s3dprintf(LOW, "informing process about new texture on oid %d, texture %d, which is available under id %d\n", oid, tex, o->p_tex[tex].shmid);
128 shmtex_packet.oid = htonl(oid);
129 shmtex_packet.tex = htonl(tex);
130 shmtex_packet.shmid = htonl(o->p_tex[tex].shmid);
131 shmtex_packet.tw = htons(o->p_tex[tex].tw);
132 shmtex_packet.th = htons(o->p_tex[tex].th);
133 shmtex_packet.w = htons(o->p_tex[tex].w);
134 shmtex_packet.h = htons(o->p_tex[tex].h);
135 prot_com_out(p, S3D_P_S_SHMTEX, (uint8_t *) & shmtex_packet, sizeof(shmtex_packet));
137 return 0;
140 /* this should replace the mcp_rep_object() function later ... */
141 int event_obj_info(struct t_process *p, int32_t oid)
143 struct {
144 int32_t object;
145 uint32_t flags;
146 float trans_x, trans_y, trans_z;
147 float rot_x, rot_y, rot_z;
148 float scale;
149 float r;
150 char name[S3D_NAME_MAX];
151 } __attribute__ ((__packed__)) mo;
153 struct t_process *ap;
154 struct t_obj *o;
155 if (OBJ_VALID(p, oid, o)) {
156 mo.object = htonl(oid);
157 mo.trans_x = p->object[oid]->translate.x;
158 mo.trans_y = p->object[oid]->translate.y;
159 mo.trans_z = p->object[oid]->translate.z;
161 mo.rot_x = p->object[oid]->rotate.x;
162 mo.rot_y = p->object[oid]->rotate.y;
163 mo.rot_z = p->object[oid]->rotate.z;
165 mo.scale = p->object[oid]->scale;
167 mo.r = p->object[oid]->r;
169 memset(mo.name, 0, S3D_NAME_MAX);
170 switch (o->oflags & OF_TYPE) {
171 case OF_VIRTUAL:
172 ap = get_proc_by_pid(o->virtual_pid);
173 strncpy(mo.name, ap->name, S3D_NAME_MAX);
174 break;
175 case OF_CAM:
176 mo.scale = (float)((float)winw) / winh; /* give aspect ratio to program */
177 strncpy(mo.name, "sys_camera0", S3D_NAME_MAX);
178 break;
179 case OF_POINTER:
180 strncpy(mo.name, "sys_pointer0", S3D_NAME_MAX);
181 break;
184 htonfb(&mo.trans_x, 8);
185 prot_com_out(p, S3D_P_S_OINFO, (uint8_t *) & mo, sizeof(mo));
187 return 0;