AtlatestRepositorysigil-wm

sigil-wm / tree / testnative-lifetime.c

1/* SPDX-License-Identifier: BSD-3-Clause
2 * Native ownership test controller; the example managers exercise Sigil policy. */
3#include <errno.h>
4#include <poll.h>
5#include <stdbool.h>
6#include <signal.h>
7#include <stdint.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <sys/wait.h>
12#include <unistd.h>
13#include "api.h"
14static pid_t child;
15static int completed;
16static int64_t initial_window;
17static bool x11,ready;
18static char x_display[64];
19static void require(bool value,const char *message) {
20 if(value) return;
21 fprintf(stderr,"native lifetime: %s\n",message);
22 if(child>0) kill(child,SIGKILL);
23 exit(1);
25static void command(int64_t kind,int64_t id,int64_t a,int64_t b) {
26 struct wm_message m={.word={kind,id,a,b}};
27 require(wm_send(&m)==1,"command refused");
29static void spawn_client(void) {
30 child=fork();
31 require(child>=0,"fork failed");
32 if(!child) {
33 if(x11) {
34 if(setenv("DISPLAY",x_display,1)!=0) _exit(126);
35 execl("build/x11-client","x11-client","normal",NULL);
36 } else execl("build/pixel-client","pixel-client","00cc44","Lifetime client",NULL);
37 _exit(127);
38 }
40int main(void) {
41 const char *transport=getenv("SIGIL_WM_NATIVE_TRANSPORT");
42 x11=transport && !strcmp(transport,"x11");
43 require(!transport || x11 || !strcmp(transport,"wayland"),"unknown transport");
44 require(wm_options(x11 ? 31 : 27,1000)==1,"options refused");
45 require(wm_start()==1,"owner did not start");
46 bool done=false;
47 int iterations=0;
48 while(!done && iterations++<10000) {
49 struct pollfd fd={.fd=wm_event_fd(),.events=POLLIN};
50 int result=poll(&fd,1,100);
51 require(result>=0 || errno==EINTR,"event poll failed");
52 struct wm_message e;
53 while(wm_next(&e)) {
54 int64_t *w=e.word;
55 switch(w[0]) {
56 case 1: {
57 int64_t device[]={1,25,600,2,'u','s'};
58 require(wm_submit(3,99,device,6)==1,"keymap refused");
59 command(14,3,0,0);
60 ready=true;
61 if(!x11 || x_display[0]) spawn_client();
62 break;
63 }
64 case 2: {
65 int64_t output[]={w[1],1,0,0,1000,0,w[4],640,480,60000};
66 require(wm_submit(2,98,output,10)==1,"output refused");
67 break;
68 }
69 case 9:
70 if(x11) {
71 /* Display names use the same copied text transport as
72 * other retained native string facts. */
73 require(w[3]==-2,"X11 display name transport");
74 int length=wm_event_text(x_display,sizeof(x_display)-1);
75 require(length>0 && length==w[2],"X11 display name length");
76 x_display[length]=0;
77 if(ready && !child) spawn_client();
78 }
79 break;
80 case 14:
81 if(!x11 && w[1]>initial_window) {
82 initial_window=w[1];
83 command(2,w[1],320,240);
84 }
85 break;
86 case 11: {
87 int64_t plan[]={1,1,w[1],200,w[1],0,0,640,480,1,1,0,15,1};
88 require(wm_submit(1,completed+1,plan,14)==1,"plan refused");
89 break;
90 }
91 case 22:
92 /* The sole mapped window is found from the last map event. */
93 break;
94 case 18:
95 if(w[4]==640 && w[5]==480) command(13,w[1],0,0);
96 break;
97 case 13:
98 require(waitpid(child,NULL,0)==child,"client not reaped");
99 child=0;
100 if(++completed==20) command(1,0,0,0);
101 else spawn_client();
102 break;
103 case 7:
104 require(w[2]==0,"native owner failed");
105 done=true;
106 break;
107 case 23: require(false,"plan rejected"); break;
108 case 98: require(false,"command rejected"); break;
109 }
110 }
111 }
112 require(done,"native ownership test timed out");
113 require(wm_join()==1,"join failed");
114 require(wm_stat(1)==0 && wm_stat(2)==0 && wm_stat(3)==0 && wm_stat(4)==0 && wm_stat(7)==0,"native objects, events or buffers remain");
115 printf("NATIVE_LIFETIME=PASS clients=%d objects=0 pixel-bytes=0 overflow=0 saved-trees=0 transport=%s\n",completed,x11 ? "x11" : "wayland");
116 return 0;