system-controller: enable audio for players only if they were visible
[profile/ivi/murphy.git] / packaging.in / murphy.lua
1 with_system_controller = false
2 with_amb = false
3 verbose = 0
4
5 m = murphy.get()
6
7 -- try loading the various logging plugins
8 m:try_load_plugin('systemd')
9 m:try_load_plugin('dlog')
10
11 -- load the console plugin
12 m:try_load_plugin('console')
13
14 m:try_load_plugin('console.disabled', 'webconsole', {
15                   address = 'wsck:127.0.0.1:3000/murphy',
16                   httpdir = '/usr/share/murphy/webconsole' });
17
18 -- load the dbus plugin
19 if m:plugin_exists('dbus') then
20     m:load_plugin('dbus')
21 end
22
23 -- load the native resource plugin
24 if m:plugin_exists('resource-native') then
25     m:load_plugin('resource-native')
26     m:info("native resource plugin loaded")
27 else
28     m:info("No native resource plugin found...")
29 end
30
31 -- load the dbus resource plugin
32 m:try_load_plugin('resource-dbus', {
33     dbus_bus = "system",
34     dbus_service = "org.Murphy",
35     dbus_track = true,
36     default_zone = "driver",
37     default_class = "implicit"
38 })
39
40 -- load the domain control plugin
41 if m:plugin_exists('domain-control') then
42     m:load_plugin('domain-control')
43 else
44     m:info("No domain-control plugin found...")
45 end
46
47 if m:plugin_exists('glib') then
48     m:load_plugin('glib')
49 else
50     m:info("No glib plugin found...")
51 end
52
53 -- load the AMB plugin
54 if m:plugin_exists('amb') then
55     m:try_load_plugin('amb')
56
57     if builtin.method.amb_initiate and
58        builtin.method.amb_update
59     then
60         with_amb = true
61     end
62 else
63     m:info("No amb plugin found...")
64 end
65
66 -- load the ASM resource plugin
67 if m:plugin_exists('resource-asm') then
68     m:try_load_plugin('resource-asm', {
69         zone = "driver",
70         share_mmplayer = "player:AVP,mandatory,exclusive,strict",
71         ignored_argv0 = "WebProcess"
72     })
73 else
74     m:info("No audio session manager plugin found...")
75 end
76
77 if m:plugin_exists('system-controller') then
78     with_system_controller = true
79 elseif m:plugin_exists('ivi-resource-manager') then
80     m:load_plugin('ivi-resource-manager')
81     with_system_controller = false
82 end
83
84 -- define application classes
85 application_class {
86     name     = "interrupt",
87     priority = 99,
88     modal    = true ,
89     share    = false,
90     order    = "fifo"
91 }
92
93 application_class {
94     name     = "emergency",
95     priority = 80,
96     modal    = false,
97     share    = false,
98     order    = "fifo"
99 }
100 application_class {
101     name     = "system",
102     priority = 52,
103     modal    = false,
104     share    = true,
105     order    = "lifo"
106 }
107 application_class {
108     name     = "alert",
109     priority = 51,
110     modal    = false,
111     share    = false,
112     order    = "fifo"
113 }
114
115 application_class {
116     name     = "navigator",
117     priority = 50,
118     modal    = false,
119     share    = true,
120     order    = "fifo"
121 }
122
123 application_class {
124     name     = "phone",
125     priority = 6 ,
126     modal    = false,
127     share    = true ,
128     order    = "lifo"
129 }
130 application_class {
131     name     = "camera",
132     priority = 5,
133     modal    = false,
134     share    = false,
135     order    = "lifo"
136 }
137
138 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
139 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
140 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
141 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
142 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
143 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
144
145 -- define zone attributes
146 zone.attributes {
147     type = {mdb.string, "common", "rw"},
148     location = {mdb.string, "anywhere", "rw"}
149 }
150
151 -- define zones
152 zone {
153      name = "driver",
154      attributes = {
155          type = "common",
156          location = "front-left"
157      }
158 }
159
160 zone {
161      name = "passanger1",
162      attributes = {
163          type = "private",
164          location = "front-right"
165      }
166 }
167
168 zone {
169      name = "passanger2",
170      attributes = {
171          type = "private",
172          location = "back-left"
173      }
174 }
175
176 zone {
177      name = "passanger3",
178      attributes = {
179          type = "private",
180          location = "back-right"
181      }
182 }
183
184 zone {
185      name = "passanger4",
186      attributes = {
187          type = "private",
188          location = "back-left"
189      }
190 }
191
192
193 -- define resource classes
194 if not m:plugin_exists('ivi-resource-manager') and
195    not with_system_controller
196 then
197    resource.class {
198         name = "audio_playback",
199         shareable = true,
200         attributes = {
201             role = { mdb.string, "music", "rw" },
202             pid = { mdb.string, "<unknown>", "rw" },
203             policy = { mdb.string, "relaxed", "rw" }
204         }
205    }
206 end
207
208 resource.class {
209      name = "audio_recording",
210      shareable = true,
211      attributes = {
212          role   = { mdb.string, "music"    , "rw" },
213          pid    = { mdb.string, "<unknown>", "rw" },
214          policy = { mdb.string, "relaxed"  , "rw" }
215      }
216 }
217
218 resource.class {
219      name = "video_playback",
220      shareable = false,
221 }
222
223 resource.class {
224      name = "video_recording",
225      shareable = false
226 }
227
228 resource.class {
229      name = "speech_recognition",
230      shareable = true
231 }
232
233 resource.class {
234      name = "speech_synthesis",
235      shareable = true
236 }
237
238 if not m:plugin_exists('ivi-resource-manager') and
239    not with_system_controller
240 then
241     resource.method.veto = {
242         function(zone, rset, grant, owners, req_set)
243             return true
244          end
245     }
246 end
247
248 -- test for creating selections
249 mdb.select {
250            name = "audio_owner",
251            table = "audio_playback_owner",
252            columns = {"application_class"},
253            condition = "zone_name = 'driver'"
254 }
255
256 mdb.select {
257            name = "vehicle_speed",
258            table = "amb_vehicle_speed",
259            columns = {"value"},
260            condition = "key = 'VehicleSpeed'"
261 }
262
263 element.lua {
264    name    = "speed2volume",
265    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
266    outputs = {  mdb.table { name = "speedvol",
267                             index = {"zone", "device"},
268                             columns = {{"zone", mdb.string, 16},
269                                        {"device", mdb.string, 16},
270                                        {"value", mdb.floating}},
271                             create = true
272                            }
273              },
274    oldvolume = 0.0,
275    update  = function(self)
276                 speed = self.inputs.speed.single_value
277                 if (speed) then
278                     volume = (speed - 144.0) / 7.0
279                 else
280                     volume = 0.0
281                 end
282                 diff = volume - self.oldvolume
283                 if (diff*diff > self.inputs.param) then
284                     print("*** element "..self.name.." update "..volume)
285                     self.oldvolume = volume
286                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
287                 end
288              end
289 }
290
291 mdb.select {
292     name = "amb_state",
293     table = "amb_state",
294     columns = { "state" },
295     condition = "id = 0"
296 }
297
298 -- Night mode processing chain
299
300 mdb.select {
301     name = "exterior_brightness",
302     table = "amb_exterior_brightness",
303     columns = { "value" },
304     condition = "key = 'ExteriorBrightness'"
305 }
306
307 element.lua {
308     name    = "nightmode",
309     inputs  = { brightness = mdb.select.exterior_brightness },
310     oldmode = -1;
311     outputs = {
312         mdb.table {
313             name = "amb_nightmode",
314             index = { "id" },
315             create = true,
316             columns = {
317                 { "id", mdb.unsigned },
318                 { "night_mode", mdb.unsigned }
319             }
320         }
321     },
322     update = function(self)
323         -- This is a trivial function to calculate night mode. Later, we will
324         -- need a better threshold value and hysteresis to prevent oscillation.
325
326         brightness = self.inputs.brightness.single_value
327
328         if not brightness then
329             return
330         end
331
332         print("*** element "..self.name.." update brightness: "..brightness)
333
334         if brightness > 300 then
335             mode = 0
336         else
337             mode = 1
338         end
339
340         print("*** resulting mode: ".. mode)
341
342         if not (mode == self.oldmode) then
343             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
344         end
345
346         self.oldmode = mode
347     end
348 }
349
350 mdb.select {
351     name = "select_night_mode",
352     table = "amb_nightmode",
353     columns = { "night_mode" },
354     condition = "id = 0"
355 }
356
357 if with_amb then
358     sink.lua {
359         name = "night_mode",
360         inputs = { NightMode = mdb.select.select_night_mode,
361                    amb_state = mdb.select.amb_state },
362         property = "NightMode",
363         type = "b",
364         initiate = builtin.method.amb_initiate,
365         update = builtin.method.amb_update
366     }
367 end
368
369 -- Night mode general handlers
370
371 sink.lua {
372     name = "nightmode_homescreen",
373     inputs = { owner = mdb.select.select_night_mode },
374     initiate = function(self)
375         -- data = mdb.select.select_night_mode.single_value
376         return true
377     end,
378     update = function(self)
379         data = mdb.select.select_night_mode.single_value
380
381         if verbose > 1 then
382             print("Night mode updated: " .. tostring(data))
383         end
384
385         if sc then
386             -- tell homescreen that night mode was updated
387             sc:send_message(homescreen, m:JSON({command=0x60001,arg=m:JSON({stateid=2,state=data})}))
388         end
389         return true
390     end
391 }
392
393 -- Driving mode processing chain
394
395 element.lua {
396     name    = "drivingmode",
397     inputs  = { speed = mdb.select.vehicle_speed },
398     oldmode = -1;
399     outputs = {
400         mdb.table {
401             name = "amb_drivingmode",
402             index = { "id" },
403             create = true,
404             columns = {
405                 { "id", mdb.unsigned },
406                 { "driving_mode", mdb.unsigned }
407             }
408         }
409     },
410     update = function(self)
411
412         speed = self.inputs.speed.single_value
413
414         if not speed then
415             return
416         end
417
418         if speed == 0 then
419             mode = 0
420         else
421             mode = 1
422         end
423
424         if not (mode == self.oldmode) then
425             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
426         end
427
428         self.oldmode = mode
429     end
430 }
431
432 mdb.select {
433     name = "select_driving_mode",
434     table = "amb_drivingmode",
435     columns = { "driving_mode" },
436     condition = "id = 0"
437 }
438
439 if with_amb then
440     sink.lua {
441         name = "driving_mode",
442         inputs = { DrivingMode = mdb.select.select_driving_mode,
443                    amb_state = mdb.select.amb_state },
444         property = "DrivingMode",
445         type = "u",
446         initiate = builtin.method.amb_initiate,
447         update = builtin.method.amb_update
448     }
449 end
450
451 -- turn signals (left, right)
452
453 mdb.select {
454     name = "winker",
455     table = "amb_turn_signal",
456     columns = { "value" },
457     condition = "key = 'TurnSignal'"
458 }
459
460 -- define three categories
461
462 mdb.select {
463     name = "undefined_applications",
464     table = "aul_applications",
465     columns = { "appid" },
466     condition = "category = '<undefined>'"
467 }
468
469 mdb.select {
470     name = "basic_applications",
471     table = "aul_applications",
472     columns = { "appid" },
473     condition = "category = 'basic'"
474 }
475
476 mdb.select {
477     name = "entertainment_applications",
478     table = "aul_applications",
479     columns = { "appid" },
480     condition = "category = 'entertainment'"
481 }
482
483 function ft(t)
484     -- filter the object garbage out of the tables
485     ret = {}
486
487     for k,v in pairs(t) do
488         if k ~= "userdata" and k ~= "new" then
489             ret[k] = v
490         end
491     end
492
493     return ret
494 end
495
496 function getApplication(appid)
497     local conf = nil
498
499     -- find the correct local application definition
500
501     for k,v in pairs(ft(application)) do
502         if appid == v.appid then
503             conf = v
504             break
505         end
506     end
507
508     return conf
509 end
510
511 function regulateApplications(t, regulation)
512     for k,v in pairs(ft(t)) do
513
514         -- iterate through the undefined and entertainment apps, see if
515         -- they have been overruled in local config
516
517         local conf = getApplication(v.appid)
518
519         if conf and conf.resource_class ~= "player" then
520             -- override, don't disable
521             resmgr:disable_screen_by_appid("*", "*", v.appid, false, false)
522         else
523             resmgr:disable_screen_by_appid("*", "*", v.appid, regulation == 1, false)
524         end
525     end
526     resource.method.recalc("driver")
527 end
528
529 -- regulation (on), use "select_driving_mode"
530
531 sink.lua {
532     name = "driving_regulation",
533     inputs = { owner = mdb.select.select_driving_mode },
534     initiate = function(self)
535         -- local data = mdb.select.select_driving_mode.single_value
536         return true
537     end,
538     update = function(self)
539         local data = mdb.select.select_driving_mode.single_value
540
541         if verbose > 1 then
542             print("Driving mode updated: " .. tostring(data))
543         end
544
545         if not sc then
546             return true
547         end
548
549         -- tell homescreen that driving mode was updated
550         sc:send_message(homescreen, m:JSON({command=0x60001,arg=m:JSON({stateid=1,state=data})}))
551
552         --[[
553         -- bulk handle the applications that need requisites
554         r = requisite { driving = true }
555         resmgr:disable_screen_by_requisite("*", "*", r, data == 1, true)
556         --]]
557
558         regulateApplications(ft(mdb.select.entertainment_applications), data)
559         regulateApplications(ft(mdb.select.undefined_applications), data)
560
561         return true
562     end
563 }
564 --[[
565 sink.lua {
566     name = "regulated_app_change",
567     inputs = { undef = mdb.select.undefined_applications,
568                entertainment = mdb.select.entertainment_applications },
569     initiate = function(self)
570         return true
571     end,
572     update = function(self)
573         local data = mdb.select.select_driving_mode.single_value
574
575         if not sc then
576             return
577         end
578
579         if verbose > 1 then
580             print("regulated application list was changed")
581         end
582
583         regulateApplications(ft(mdb.select.entertainment_applications), data)
584         regulateApplications(ft(mdb.select.undefined_applications), data)
585
586         return true
587     end
588 }
589 --]]
590
591 -- shift position (parking, reverse, other)
592
593 mdb.select {
594     name = "gear_position",
595     table = "amb_gear_position",
596     columns = { "value" },
597     condition = "key = 'GearPosition'"
598 }
599
600 -- cameras (back, front, left, right)
601
602 element.lua {
603     name    = "camera_state",
604     inputs  = { winker = mdb.select.winker, gear = mdb.select.gear_position },
605     oldmode = -1;
606     outputs = {
607         mdb.table {
608             name = "target_camera_state",
609             index = { "id" },
610             create = true,
611             columns = {
612                 { "id", mdb.unsigned },
613                 { "front_camera", mdb.unsigned },
614                 { "back_camera", mdb.unsigned },
615                 { "right_camera", mdb.unsigned },
616                 { "left_camera", mdb.unsigned }
617             }
618         }
619     },
620     update = function(self)
621
622         front_camera = 0
623         back_camera = 0
624         right_camera = 0
625         left_camera = 0
626
627         if self.inputs.gear == 128 then
628             back_camera = 1
629         elseif self.inputs.winker == 1 then
630             right_camera = 1
631         elseif self.inputs.winker == 2 then
632             left_camera = 1
633         end
634
635         mdb.table.target_camera_state:replace({ id = 0, front_camera = front_camera, back_camera = back_camera, right_camera = right_camera, left_camera = left_camera })
636
637     end
638 }
639
640 -- load the telephony plugin
641 m:try_load_plugin('telephony')
642
643
644 -- system controller test setup
645
646 if not with_system_controller then
647    return
648 end
649
650 m:load_plugin('system-controller')
651
652 window_manager_operation_names = {
653     [1] = "create",
654     [2] = "destroy"
655 }
656
657 function window_manager_operation_name(oper)
658     local name = window_manager_operation_names[oper]
659     if name then return name end
660     return "<unknown " .. tostring(oper) .. ">"
661 end
662
663 window_operation_names = {
664     [1] = "create",
665     [2] = "destroy",
666     [3] = "name_change",
667     [4] = "visible",
668     [5] = "configure",
669     [6] = "active"
670 }
671
672 function window_operation_name(oper)
673     local name = window_operation_names[oper]
674     if name then return name end
675     return "<unknown " .. tostring(oper) .. ">"
676 end
677
678 layer_operation_names = {
679     [1] = "create",
680     [2] = "destroy",
681     [3] = "visible"
682 }
683
684 function layer_operation_name(oper)
685     local name = layer_operation_names[oper]
686     if name then return name end
687     return "<unknown " .. tostring(oper) .. ">"
688 end
689
690 input_manager_operation_names = {
691     [1] = "create",
692     [2] = "destroy",
693     [3] = "ready"
694 }
695
696 function input_manager_operation_name(oper)
697     local name = input_manager_operation_names[oper]
698     if name then return name end
699     return "<unknown " .. tostring(oper) .. ">"
700 end
701
702 input_operation_names = {
703     [1] = "create",
704     [2] = "destroy",
705     [3] = "update"
706 }
707
708 function input_operation_name(oper)
709     local name = input_operation_names[oper]
710     if name then return name end
711     return "<unknown " .. tostring(oper) .. ">"
712 end
713
714 code_operation_names = {
715     [1] = "create",
716     [2] = "destroy",
717     [3] = "state_change"
718 }
719
720 function code_operation_name(oper)
721     local name = code_operation_names[oper]
722     if name then return name end
723     return "<unknown " .. tostring(oper) .. ">"
724 end
725
726 command_names = {
727     [0x00001] = "send_appid",
728     [0x10001] = "create",
729     [0x10002] = "destroy",
730     [0x10003] = "show",
731     [0x10004] = "hide",
732     [0x10005] = "move",
733     [0x10006] = "change_active",
734     [0x10007] = "change_layer",
735     [0x10008] = "change_attr",
736     [0x10009] = "name",
737     [0x10011] = "map_thumb",
738     [0x10012] = "unmap_thumb",
739     [0x10020] = "show layer",
740     [0x10021] = "hide_layer",
741     [0x10022] = "change_layer_attr",
742     [0x20001] = "add_input",
743     [0x20002] = "del_input",
744     [0x20003] = "send_input",
745     [0x40001] = "acquire_res",
746     [0x40002] = "release_res",
747     [0x40003] = "deprive_res",
748     [0x40004] = "waiting_res",
749     [0x40005] = "revert_res",
750     [0x40011] = "create_res",
751     [0x40012] = "destroy_res",
752     [0x50001] = "set_region",
753     [0x50002] = "unset_region",
754     [0x60001] = "change_state"
755 }
756
757 function command_name(command)
758     local name = command_names[command]
759     if name then return name end
760     return "<unknown " .. tostring(command) .. ">"
761 end
762
763 input_layer = {
764    [3] = true, -- input
765    [4] = true, -- touch
766    [5] = true  -- cursor
767 }
768
769 resmgr = resource_manager {
770   screen_event_handler = function(self, ev)
771                              local event = ev.event
772                              local surface = ev.surface
773
774                              if event == "init" then
775                                  if verbose > 0 then
776                                      print("*** init screen resource allocation -- disable all 'player'")
777                                  end
778                                  resmgr:disable_audio_by_appid("*", "player", "*", true, false)
779                              elseif event == "preallocate" then
780                                  if verbose > 0 then
781                                      print("*** preallocate screen resource "..
782                                            "for '" .. ev.appid .. "' -- enable 'player', if any")
783                                  end
784                                  resmgr:disable_audio_by_appid("*", "player", ev.appid, false, false)
785                              elseif event == "grant" then
786                                  if verbose > 0 then
787                                     print("*** make visible surface "..surface)
788                                  end
789                                  local a = animation({})
790                                  local r = m:JSON({surface = surface,
791                                                    visible = 1,
792                                                    raise   = 1})
793                                  wmgr:window_request(r,a,0)
794                              elseif event == "revoke" then
795                                  if verbose > 0 then
796                                     print("*** hide surface "..surface)
797                                  end
798                                  local a = animation({})
799                                  local r = m:JSON({surface = ev.surface,
800                                                    visible = 0})
801                                  wmgr:window_request(r,a,0)
802
803                              elseif event == "create" then
804
805                                 if verbose > 0 then
806                                     print("*** screen resource event: " ..
807                                           tostring(ev))
808                                 end
809
810                                 local regulation = mdb.select.select_driving_mode.single_value
811
812                                 if regulation == 1 then
813
814                                     local blacklisted = false
815
816                                     -- applications which have their category set to "entertainment"
817                                     -- or "undefined" are blacklisted, meaning they should be regulated
818
819                                     for i,v in pairs(ft(mdb.select.undefined_applications)) do
820                                         if v.appid == ev.appid then
821                                             if verbose > 0 then
822                                                 print(ev.appid .. " was blacklisted (undefined)")
823                                             end
824                                             blacklisted = true
825                                             break
826                                         end
827                                     end
828
829                                     if not blacklisted then
830                                         for i,v in pairs(ft(mdb.select.entertainment_applications)) do
831                                             if v.appid == ev.appid then
832                                                 if verbose > 0 then
833                                                     print(ev.appid .. " was blacklisted (entertainment)")
834                                                 end
835                                                 blacklisted = true
836                                                 break
837                                             end
838                                         end
839                                     end
840
841                                     -- our local application config, which takes precedence
842                                     local conf = getApplication(ev.appid)
843
844                                     -- disable only non-whitelisted applications
845                                     if not conf or conf.resource_class == "player" then
846                                         if blacklisted then
847                                             if verbose > 0 then
848                                                 print("disabling screen for " .. ev.appid)
849                                             end
850                                             resmgr:disable_screen_by_appid("*", "*", ev.appid, true, true)
851                                         end
852                                     end
853                                 end
854
855                              elseif event == "destroy" then
856                                if verbose > 0 then
857                                     print("*** screen resource event: " ..
858                                           tostring(ev))
859                                  end
860                              else
861                                  if verbose > 0 then
862                                     print("*** screen resource event: " ..
863                                           tostring(ev))
864                                  end
865                              end
866                          end,
867   audio_event_handler = function(self, ev)
868                              local event = ev.event
869                              local appid = ev.appid
870                              local audioid = ev.audioid
871
872                              if event == "grant" then
873                                  if verbose > 0 then
874                                     print("*** grant audio to "..appid..
875                                           " ("..audioid..") in '" ..
876                                           ev.zone .. "' zone")
877                                  end
878                              elseif event == "revoke" then
879                                  if verbose > 0 then
880                                     print("*** revoke audio from "..appid..
881                                           " ("..audioid..") in '" ..
882                                           ev.zone .. "' zone")
883                                  end
884                              else
885                                  if verbose > 0 then
886                                     print("*** audio resource event: " ..
887                                           tostring(ev))
888                                  end
889                              end
890                         end
891 }
892
893 resclnt = resource_client {}
894
895 wmgr = window_manager {
896   geometry = function(self, w,h, v)
897                   if type(v) == "function" then
898                       return v(w,h)
899                   end
900                   return v
901              end,
902
903   application = function(self, appid)
904                      if appid then
905                          local app = application_lookup(appid)
906                          if not app then
907                              app = application_lookup("default")
908                          end
909                          return app
910                      end
911                      return { privileges = {screen="none", audio="none"} }
912                 end,
913
914   output_order = { 1, 0 },
915
916   outputs = { { name  = "Mid",
917                 id    = 0,
918                 zone  = "driver",
919                 areas = { Full = {
920                               id     = 20,
921                               pos_x  = 0,
922                               pos_y  = 0,
923                               width  = function(w,h) return w end,
924                               height = function(w,h) return h end
925                           },
926                           Left = {
927                               id     = 21,
928                               pos_x  = 0,
929                               pos_y  = 0,
930                               width  = 320,
931                               height = function(w,h) return h end
932                           },
933                           Right = {
934                               id     = 22,
935                               pos_x  = function(w,h) return w-320 end,
936                               pos_y  = 0,
937                               width  = 320,
938                               height = function(w,h) return h end
939                           }
940                         }
941                },
942                { name  = "Center",
943                  id    = 1,
944                  zone  = "driver",
945                  areas = { Status = {
946                               id     = 0,
947                               pos_x  = 0,
948                               pos_y  = 0,
949                               width  = function(w,h) return w end,
950                               height = 64
951                            },
952                            Full = {
953                               id     = 1,
954                               pos_x  = 0,
955                               pos_y  = 64,
956                               width  = function(w,h) return w end,
957                               height = function(w,h) return h-64-128 end
958                            },
959                            Upper = {
960                               id     = 2,
961                               pos_x  = 0,
962                               pos_y  = 64,
963                               width  = function(w,h) return w end,
964                               height = function(w,h) return (h-64-128)/2 end
965                            },
966                            Lower = {
967                               id     = 3,
968                               pos_x  = 0,
969                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
970                               width  = function(w,h) return w end,
971                               height = function(w,h) return (h-64-128)/2 end
972                            },
973                            UpperLeft = {
974                               id     = 4,
975                               pos_x  = 0,
976                               pos_y  = 64,
977                               width  = function(w,h) return w/2 end,
978                               height = function(w,h) return (h-64-128)/2 end
979                            },
980                            UpperRight = {
981                               id     = 5,
982                               pos_x  = function(w,h) return w/2 end,
983                               pos_y  = 64,
984                               width  = function(w,h) return w/2 end,
985                               height = function(w,h) return (h-64-128)/2 end
986                            },
987                            LowerLeft = {
988                               id     = 6,
989                               pos_x  = 0,
990                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
991                               width  = function(w,h) return w/2 end,
992                               height = function(w,h) return (h-64-128)/2 end
993                            },
994                            LowerRight = {
995                               id     = 7,
996                               pos_x  = function(w,h) return w/2 end,
997                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
998                               width  = function(w,h) return w/2 end,
999                               height = function(w,h) return (h-64-128)/2 end
1000                            },
1001                            SysApp = {
1002                               id     = 8,
1003                               pos_x  = 0,
1004                               pos_y  = 64,
1005                               width  = function(w,h) return w end,
1006                               height = function(w,h) return h-64-128 end
1007                            },
1008                            ["SysApp.Left"] = {
1009                               id     = 9,
1010                               pos_x  = 0,
1011                               pos_y  = 64,
1012                               width  = function(w,h) return w/2-181 end,
1013                               height = function(w,h) return h-64-128 end
1014                            },
1015                            ["SysApp.Right"] = {
1016                               id     = 10,
1017                               pos_x  = function(w,h) return w/2+181 end,
1018                               pos_y  = 64,
1019                               width  = function(w,h) return w/2-181 end,
1020                               height = function(w,h) return h-64-128 end
1021                            },
1022                         }
1023               }
1024   },
1025   layers = { {      0, "Background"   , 1 },
1026              {      1, "Application"  , 2 },
1027              {      2, "Softkeyboard" , 4 },
1028              {      3, "HomeScreen"   , 2 },
1029              {      4, "ControlBar"   , 2 },
1030              {      5, "InterruptApp" , 2 },
1031              {      6, "OnScreen"     , 2 },
1032              {    101, "Input"        , 3 },
1033              {    102, "Cursor"       , 5 },
1034              {    103, "Startup"      , 6 },
1035              { 0x1000, "Background"   , 1 },
1036              { 0x2000, "Normal"       , 2 },
1037              { 0x3000, "Fullscreen"   , 2 },
1038              { 0x4000, "InputPanel"   , 3 },
1039              { 0xA000, "Touch"        , 4 },
1040              { 0xB000, "Cursor"       , 5 },
1041              { 0xC000, "Startup"      , 6 }
1042   },
1043
1044
1045   manager_update = function(self, oper)
1046                        if verbose > 0 then
1047                            print("### <== WINDOW MANAGER UPDATE:" ..
1048                                  window_manager_operation_name(oper))
1049                        end
1050                        if oper == 1 then
1051                            local umask = window_mask { raise   = true,
1052                                                        visible = true,
1053                                                        active  = true }
1054                            local rmask = window_mask { active  = true }
1055                            local req = m:JSON({
1056                                        passthrough_update  = umask:tointeger(),
1057                                        passthrough_request = rmask:tointeger()
1058                            })
1059                            self:manager_request(req)
1060                        end
1061                    end,
1062
1063   window_update = function(self, oper, win, mask)
1064                       if verbose > 0 then
1065                           print("### <== WINDOW UPDATE oper:" ..
1066                                 window_operation_name(oper) ..
1067                                 " mask: " .. tostring(mask))
1068                           if verbose > 1 then
1069                               print(win)
1070                           end
1071                       end
1072
1073                       local arg = m:JSON({ surface = win.surface,
1074                                            winname = win.name,
1075                       })
1076                       local command = 0
1077
1078                       if oper == 1 then -- create
1079                            local layertype = win.layertype
1080                            if layertype and input_layer[layertype] then
1081                                if verbose > 0 then
1082                                    print("ignoring input panel creation")
1083                                end
1084                                return
1085                            end
1086                            command     = 0x10001
1087                       elseif oper == 2 then -- destroy
1088                            command     = 0x10002
1089                       elseif oper == 3 then  -- namechange
1090                            command     = 0x10009
1091                       elseif oper == 4 or oper == 5 then --visible or configure
1092                            command     = 0x10008
1093                            arg.zone    = win.area
1094                            arg.node    = win.node
1095                            arg.layer   = win.layer
1096                            arg.pos_x   = win.pos_x
1097                            arg.pos_y   = win.pos_y
1098                            arg.width   = win.width
1099                            arg.height  = win.height
1100                            arg.raise   = win.raise
1101                            arg.visible = win.visible
1102                            arg.active  = win.active
1103                       elseif oper == 6 then -- active
1104                            command     = 0x10006
1105                            arg.active  = win.active
1106                       else
1107                            if verbose > 0 then
1108                                print("### nothing to do")
1109                            end
1110                            return
1111                       end
1112
1113                       local msg = m:JSON({ command = command,
1114                                            appid   = win.appid,
1115                                            pid     = win.pid,
1116                                            arg     = arg
1117                       })
1118                       if verbose > 0 then
1119                           print("### <== sending " ..
1120                                 command_name(msg.command) ..
1121                                 " window message to '" .. win.name .. "'")
1122                           if verbose > 1 then
1123                               print(msg)
1124                           end
1125                       end
1126                       sc:send_message(homescreen, msg)
1127
1128                       if oper == 1 then -- create
1129                           local i = input_layer[win.layertype]
1130                           local p = self:application(win.appid)
1131                           local s = p.privileges.screen
1132
1133                           if s == "system" then
1134                               local a = animation({})
1135                               local r = m:JSON({surface = win.surface,
1136                                                 visible = 1,
1137                                                 raise   = 1})
1138                               self:window_request(r,a,0)
1139                           else
1140                               if i then
1141                                   if verbose > 0 then
1142                                       print("do not make resource for " ..
1143                                             "input window")
1144                                   end
1145                               else
1146                                   resclnt:resource_set_create("screen",
1147                                                                "driver",
1148                                                                win.appid,
1149                                                                win.surface)
1150                               end
1151                           end
1152                       elseif oper == 2 then -- destroy
1153                           resclnt:resource_set_destroy("screen", win.surface)
1154                       elseif oper == 6 then -- active
1155                           if win.active then
1156                               local i = input_layer[win.layertype]
1157                               local p = self:application(win.appid)
1158                               local s = p.privileges.screen
1159                               local surface = win.surface
1160                               if not i and s ~= "system" then
1161                                  resclnt:resource_set_acquire("screen",surface)
1162                                  resmgr:window_raise(win.appid, surface, 1)
1163                               end
1164                           end
1165                       end
1166                   end,
1167
1168   layer_update = function(self, oper, layer, mask)
1169                       if verbose > 0 then
1170                           print("### LAYER UPDATE:" ..
1171                                 layer_operation_name(oper) ..
1172                                 " mask: " .. tostring(mask))
1173                           if verbose > 1 then
1174                               print(layer)
1175                           end
1176                       end
1177                       if oper == 3 then -- visible
1178                          local command = 0x10022
1179                          local msg = m:JSON({
1180                                          command = command,
1181                                          appid = "",
1182                                          arg = m:JSON({layer = layer.id,
1183                                                        visible = layer.visible
1184                                          })
1185                                 })
1186                          if verbose > 0 then
1187                             print("### <== sending "..command_name(command)..
1188                                   " layer message")
1189                             if verbose > 1 then
1190                                 print(msg)
1191                             end
1192                          end
1193                          sc:send_message(homescreen, msg)
1194                       else
1195                            if verbose > 0 then
1196                                print("### nothing to do")
1197                            end
1198                       end
1199                  end,
1200
1201   output_update = function(self, oper, out, mask)
1202                       local idx = out.index
1203                       local defidx = self.output_order[idx+1]
1204                       if verbose > 0 then
1205                           print("### OUTPUT UPDATE:" .. oper ..
1206                                 " mask: "..tostring(mask))
1207                       end
1208                       if not defidx then
1209                           return
1210                       end
1211                       print(out)
1212                       local outdef = self.outputs[defidx+1]
1213                       if (oper == 1) then -- create
1214                           if outdef then
1215                               self:output_request(m:JSON({index = idx,
1216                                                           id    = outdef.id,
1217                                                           name  = outdef.name
1218                                                           }))
1219                           end
1220                       elseif (oper == 5) then -- done
1221                           local ads = outdef.areas
1222                           local on = outdef.name
1223                           if ads then
1224                               for name,ad in pairs(ads) do
1225                                   local can = wmgr:canonical_name(on.."."..name)
1226                                   local a = m:JSON({name   = name,
1227                                                     output = out.index})
1228                                   for fld,val in pairs(ad) do
1229                                       a[fld] = self:geometry(out.width,
1230                                                              out.height,
1231                                                              val)
1232                                    end
1233                                    self:area_create(a)
1234                                    resmgr:area_create(area[can], outdef.zone)
1235                               end
1236                           end
1237                       end
1238                   end
1239 }
1240
1241
1242 imgr = input_manager {
1243   inputs = {{ name = "G27 Racing Wheel",
1244               id = 0,
1245               switch = { [2] = {appid="org.tizen.ico.app-soundsample"      },
1246                          [3] = {appid="org.tizen.ico.homescreen", keycode=1},
1247                          [4] = {appid="org.tizen.ico.app-soundsample"      },
1248                          [5] = {appid="org.tizen.ico.homescreen", keycode=2}
1249              }}
1250   },
1251
1252   manager_update = function(self, oper)
1253                        if verbose > 0 then
1254                            print("### <== INPUT MANAGER UPDATE:" ..
1255                                  input_manager_operation_name(oper))
1256                        end
1257                    end,
1258
1259   input_update = function(self, oper, inp, mask)
1260                      if verbose > 0 then
1261                          print("### INPUT UPDATE:" ..
1262                                 input_operation_name(oper) ..
1263                                 " mask: " .. tostring(mask))
1264                           if verbose > 1 then
1265                               print(inp)
1266                           end
1267                       end
1268                  end,
1269   code_update  = function(self, oper, code, mask)
1270                      if verbose > 0 then
1271                          print("### CODE UPDATE: mask: " .. tostring(mask))
1272                          if verbose > 1 then
1273                              print(code)
1274                          end
1275                      end
1276                      local msg = m:JSON({ command = 1,
1277                                           appid = "org.tizen.ico.homescreen",
1278                                           arg = m:JSON({ device = code.device,
1279                                                          time = code.time,
1280                                                          input = code.input,
1281                                                          code = code.id,
1282                                                          state = code.state
1283                                            })
1284                      })
1285                      if verbose > 0 then
1286                          print("### <== sending " ..
1287                                command_name(msg.command) ..
1288                                " input message")
1289                          if verbose > 1 then
1290                              print(msg)
1291                          end
1292                      end
1293                      sc:send_message(homescreen, msg)
1294                  end
1295 }
1296
1297 sc = m:get_system_controller()
1298
1299 -- resource sets
1300 sets = {}
1301
1302 -- user manager
1303 um = m:UserManager()
1304
1305 connected = false
1306 homescreen = ""
1307
1308 -- these shoud be before wmgr:connect() is called
1309 if verbose > 0 then
1310    print("====== creating applications ======")
1311 end
1312 application {
1313     appid          = "default",
1314     area           = "Center.Full",
1315     privileges     = { screen = "none", audio = "none" },
1316     resource_class = "player",
1317     screen_priority = 0
1318 }
1319
1320 application {
1321     appid           = "weston",
1322     area            = "Center.Full",
1323     privileges      = { screen = "system", audio = "none" },
1324     resource_class  = "implicit",
1325     screen_priority = 30
1326 }
1327
1328 application {
1329     appid           = "org.tizen.ico.homescreen",
1330     area            = "Center.Full",
1331     privileges      = { screen = "system", audio = "system" },
1332     resource_class  = "player",
1333     screen_priority = 20
1334 }
1335
1336 application {
1337     appid           = "org.tizen.ico.statusbar",
1338     area            = "Center.Status",
1339     privileges      = { screen = "system", audio = "none" },
1340     resource_class  = "player",
1341     screen_priority = 20
1342 }
1343
1344 application {
1345     appid           = "org.tizen.ico.login",
1346     area            = "Center.Full",
1347     privileges      = { screen = "system", audio = "system" },
1348     resource_class  = "player",
1349     screen_priority = 20
1350 }
1351
1352 application {
1353     appid           = "org.tizen.ico.camera_left",
1354     area            = "Center.SysApp.Left",
1355     privileges      = { screen = "system", audio = "none" },
1356     requisites      = { screen = "blinker_left", audio = "none" },
1357     resource_class  = "player",
1358     screen_priority = 30
1359 }
1360
1361 application {
1362     appid           = "org.tizen.ico.camera_right",
1363     area            = "Center.SysApp.Right",
1364     privileges      = { screen = "system", audio = "none" },
1365     requisites      = { screen = "blinker_right", audio = "none" },
1366     resource_class  = "player",
1367     screen_priority = 30
1368 }
1369
1370 application {
1371     appid           = "net.zmap.navi",
1372     area            = "Center.Full",
1373     privileges      = { screen = "none", audio = "none" },
1374     resource_class  = "navigator",
1375     screen_priority = 30
1376 }
1377
1378 application {
1379     appid           = "GV3ySIINq7.GhostCluster",
1380     area            = "Mid.Full",
1381     privileges      = { screen = "none", audio = "none" },
1382     resource_class  = "system",
1383     screen_priority = 30
1384 }
1385
1386 if sc then
1387     sc.client_handler = function (self, cid, msg)
1388         local command = msg.command
1389         if verbose > 0 then
1390             print('### ==> client handler:')
1391             if verbose > 1 then
1392                 print(msg)
1393             end
1394         end
1395
1396         -- known commands: 1 for SEND_APPID, synthetic command 0xFFFF for
1397         -- disconnection
1398
1399         if command == 0xFFFF then
1400             if verbose > 1 then
1401                 print('client ' .. cid .. ' disconnected')
1402             end
1403             if msg.appid == homescreen then
1404                 connected = false
1405                 homescreen = ""
1406             end
1407             return
1408         end
1409
1410         -- handle the connection
1411
1412         if not connected then
1413             print('Setting homescreen='..msg.appid)
1414             homescreen = msg.appid
1415             print('Trying to connect to wayland...')
1416             connected = wmgr:connect()
1417         end
1418         if connected and command then
1419             if command == 1 then -- send_appid
1420                 local driving_mode = mdb.select.select_driving_mode.single_value
1421                 local night_mode = mdb.select.select_night_mode.single_value
1422
1423                 if not driving_mode then driving_mode = 0 end
1424                 if not night_mode then night_mode = 0 end
1425
1426                 local reply = m:JSON({ command = 0x60001,
1427                                        arg     = m:JSON({ stateid = 1,
1428                                                           state   = driving_mode})
1429                                      })
1430                  if verbose > 0 then
1431                      print("### <== sending " ..
1432                            command_name(command) .. " message")
1433                      if verbose > 1 then
1434                          print(reply)
1435                      end
1436                  end
1437                  sc:send_message(homescreen, reply)
1438
1439                  reply = m:JSON({ command = 0x60001,
1440                                   arg     = m:JSON({ stateid = 2,
1441                                                      state   = night_mode})
1442                                 })
1443                  if verbose > 0 then
1444                      print("### <== sending " ..
1445                            command_name(command) .. " message")
1446                      if verbose > 1 then
1447                          print(reply)
1448                      end
1449                  end
1450                  sc:send_message(homescreen, reply)
1451             end
1452         end
1453     end
1454
1455     sc.generic_handler = function (self, cid, msg)
1456         if verbose > 0 then
1457             print('### ==> generic handler:')
1458             if verbose > 1 then
1459                print(msg)
1460             end
1461         end
1462     end
1463
1464     sc.window_handler = function (self, cid, msg)
1465         if verbose > 0 then
1466             print('### ==> received ' ..
1467                    command_name(msg.command) .. ' message')
1468             if verbose > 1 then
1469                 print(tostring(msg))
1470             end
1471         end
1472
1473         local a = animation({})
1474         local nores = false
1475         if msg.command == 0x10003 then       -- ico SHOW command
1476             local raise_mask = 0x01000000
1477             local lower_mask = 0x02000000
1478             local nores_mask = 0x40000000
1479             local time_mask  = 0x00ffffff
1480             msg.arg.visible = 1
1481             if msg.arg then
1482                 local time = 200
1483                 if  msg.arg.anim_time then
1484                     local t = msg.arg.anim_time
1485                     time = m:AND(t, time_mask)
1486                     nores = not m:AND(t, nores_mask)
1487                     if m:AND(t, raise_mask) then
1488                         msg.arg.raise = 1
1489                     elseif m:AND(t, lower_mask) then
1490                         msg.arg.raise = 0
1491                     end
1492                 end
1493                 if msg.arg.anim_name then
1494                     a.show = { msg.arg.anim_name, time }
1495                     print('time: ' .. tostring(a.show[2]))
1496                 end
1497             end
1498             if not nores then
1499                 local p = wmgr:application(msg.appid)
1500                 local s = p.privileges.screen
1501                 if s == "system" then
1502                     nores = true
1503                     if not msg.arg.raise then
1504                         msg.arg.raise = 1
1505                     end
1506                 end
1507             end
1508             if verbose > 2 then
1509                 print('### ==> SHOW')
1510                 print(tostring(msg.arg))
1511             end
1512             if nores then
1513                 wmgr:window_request(msg.arg, a, 0)
1514             else
1515                 local surface = msg.arg.surface
1516                 resclnt:resource_set_acquire("screen", surface)
1517                 resmgr:window_raise(msg.appid, surface, 1)
1518             end
1519         elseif msg.command == 0x10004 then   -- ico HIDE command
1520             local raise_mask = 0x01000000
1521             local lower_mask = 0x02000000
1522             local nores_mask = 0x40000000
1523             local time_mask  = 0x00ffffff
1524             msg.arg.visible = 0
1525             if msg.arg then
1526                 local time = 200
1527                 if msg.arg.anim_time then
1528                     local t = msg.arg.anim_time
1529                     time = m:AND(t, time_mask)
1530                     nores = not m:AND(t, nores_mask)
1531                 end
1532                 if msg.arg.anim_name then
1533                     a.hide = { msg.arg.anim_name, time }
1534                     print('hide animation time: ' .. tostring(a.hide[2]))
1535                 end
1536             end
1537             if not nores then
1538                 local p = wmgr:application(msg.appid)
1539                 local s = p.privileges.screen
1540                 if s == "system" then
1541                     nores = true
1542                     msg.arg.raise = 0
1543                 end
1544             end
1545             if verbose > 2 then
1546                 print('### ==> HIDE REQUEST')
1547                 print(tostring(msg.arg))
1548             end
1549             if nores then
1550                 wmgr:window_request(msg.arg, a, 0)
1551             else
1552                 resmgr:window_raise(msg.appid, msg.arg.surface, -1)
1553             end
1554         elseif msg.command == 0x10005 then   -- ico MOVE
1555             if verbose > 2 then
1556                 print('### ==> MOVE REQUEST')
1557                 print(tostring(msg.arg))
1558             end
1559             if msg.arg.zone then
1560                 msg.arg.area = msg.arg.zone
1561             end
1562             wmgr:window_request(msg.arg, a, 0)
1563             -- TODO: handle if area changed
1564         elseif msg.command == 0x10006 then   -- ico ACTIVE
1565             if not msg.arg.active then
1566                 msg.arg.active = 3 -- pointer + keyboard
1567             end
1568             if verbose > 2 then
1569                 print('### ==> ACTIVE REQUEST')
1570                 print(tostring(msg.arg))
1571             end
1572             wmgr:window_request(msg.arg, a, 0)
1573         elseif msg.command == 0x10007 then   -- ico CHANGE_LAYER
1574             if verbose > 2 then
1575                 print('### ==> CHANGE_LAYER REQUEST')
1576                 print(tostring(msg.arg))
1577             end
1578             --[[
1579             if msg.arg.layer ~= 4 or msg.arg.layer ~= 5 then
1580                 print("do not change layer for other than cursor or touch")
1581                 return
1582             end
1583             --]]
1584             wmgr:window_request(msg.arg, a, 0)
1585         elseif msg.command == 0x10011 then   -- ico MAP_THUMB
1586             local framerate = msg.arg.framerate
1587             if not framerate or framerate < 0 then
1588                 framerate = 0
1589             end
1590             msg.arg.map = 1
1591             if verbose > 2 then
1592                 print('### ==> MAP_THUMB REQUEST')
1593                 print(msg.arg)
1594                 print('framerate: '..framerate)
1595             end
1596             wmgr:window_request(msg.arg, a, framerate)
1597         elseif msg.command == 0x10012 then   -- ico UNMAP_THUMB
1598             msg.arg.map = 0
1599             if verbose > 2 then
1600                 print('### ==> UNMAP_THUMB REQUEST')
1601                 print(msg.arg)
1602             end
1603             wmgr:window_request(msg.arg, a, 0)
1604         elseif msg.command == 0x10020 then   -- ico SHOW_LAYER command
1605             msg.arg.visible = 1
1606             if verbose > 2 then
1607                 print('### ==> SHOW_LAYER REQUEST')
1608                 print(msg.arg)
1609             end
1610             wmgr:layer_request(msg.arg)
1611         elseif msg.command == 0x10021 then   -- ico HIDE_LAYER command
1612             msg.arg.visible = 0
1613             if verbose > 2 then
1614                 print('### ==> HIDE_LAYER REQUEST')
1615                 print(msg.arg)
1616             end
1617             wmgr:layer_request(msg.arg)
1618         end
1619     end
1620
1621     sc.input_handler = function (self, cid, msg)
1622         if verbose > 0 then
1623             print('### ==> input handler: ' .. command_name(msg.command))
1624             if verbose > 1 then
1625                 print(msg)
1626             end
1627         end
1628         if msg.command == 0x20001 then -- add_input
1629             msg.arg.appid = msg.appid
1630             if verbose > 2 then
1631                 print('### ==> ADD_INPUT REQUEST')
1632                 print(tostring(msg.arg))
1633             end
1634             imgr:input_request(msg.arg)
1635         elseif msg.command == 0x20002 then -- del_input
1636             msg.arg.appid = ''
1637             if verbose > 2 then
1638                 print('### ==> DEL_INPUT REQUEST')
1639                 print(tostring(msg.arg))
1640             end
1641             imgr:input_request(msg.arg)
1642         elseif msg.command == 0x20003 then -- send_input
1643         end
1644     end
1645
1646     sc.user_handler = function (self, cid, msg)
1647         if verbose > 0 then
1648             print('### ==> user handler: ' .. command_name(msg.command))
1649             if verbose > 1 then
1650                 print(msg)
1651             end
1652         end
1653
1654         if not um then
1655             print("User Manager not initialized")
1656             return
1657         end
1658
1659         if msg.command == 0x00030001 then -- MSG_CMD_CHANGE_USER
1660             print("command CHANGE_USER")
1661             if not msg.arg then
1662                 print("invalid message")
1663                 return
1664             end
1665
1666             username = msg.arg.user
1667             passwd = msg.arg.pass
1668
1669             if not username then
1670                 username = ""
1671             end
1672
1673             if not passwd then
1674                 passwd = ""
1675             end
1676
1677             success = um:changeUser(username, passwd)
1678
1679             if not success then
1680                 reply = m.JSON({
1681                     appid = msg.appid,
1682                     command = cmd
1683                 })
1684                 if sc:send_message(msg.appid, reply) then
1685                     print('*** sent authentication failed message')
1686                 else
1687                     print('*** failed to send authentication failed message')
1688                 end
1689             end
1690
1691         elseif msg.command == 0x00030002 then -- MSG_CMD_GET_USERLIST
1692             print("command GET_USERLIST")
1693             if not msg.appid then
1694                 print("invalid message")
1695                 return
1696             end
1697
1698             users, currentUser = um:getUserList()
1699
1700             if not users then
1701                 print("failed to get user list")
1702                 return
1703             end
1704
1705             nUsers = 0
1706
1707             for i,v in pairs(users) do
1708                 nUsers = nUsers + 1
1709             end
1710
1711             if not currentUser then
1712                 currentUser = ""
1713             end
1714
1715             if verbose > 1 then
1716                 print("current user: " .. currentUser)
1717                 print("user list:")
1718                 for i,v in pairs(users) do
1719                     print(v)
1720                 end
1721             end
1722
1723             reply = m.JSON({
1724                 appid = msg.appid,
1725                 command = cmd,
1726                 arg = m.JSON({
1727                     user_num = nUsers,
1728                     user_list = users,
1729                     user_login = currentUser
1730                 })
1731             })
1732
1733             if verbose > 1 then
1734                 print("### <== GetUserList reply: " .. tostring(reply))
1735             end
1736
1737             if sc:send_message(msg.appid, reply) then
1738                 print('*** reply OK')
1739             else
1740                 print('*** reply FAILED')
1741             end
1742
1743         elseif msg.command == 0x00030003 then -- MSG_CMD_GET_LASTINFO
1744             print("command GET_LASTINFO")
1745             if not msg.appid then
1746                 print("invalid message")
1747                 return
1748             end
1749
1750             lastInfo = um:getLastInfo(msg.appid)
1751
1752             if not lastInfo then
1753                 print("failed to get last info for app" .. msg.appid)
1754                 return
1755             end
1756
1757             reply = m.JSON({
1758                 appid = msg.appid,
1759                 command = cmd,
1760                 arg = m.JSON({
1761                     lastinfo = lastinfo
1762                 })
1763             })
1764
1765             if sc:send_message(msg.appid, reply) then
1766                 print('*** reply OK')
1767             else
1768                 print('*** reply FAILED')
1769             end
1770
1771         elseif msg.command == 0x00030004 then -- MSG_CMD_SET_LASTINFO
1772             print("command SET_LASTINFO")
1773             if not msg.arg or not msg.appid then
1774                 print("invalid message")
1775                 return
1776             end
1777
1778             lastInfo = um:setLastInfo(msg.appid, msg.arg.lastinfo)
1779         end
1780     end
1781
1782     sc.resource_handler = function (self, cid, msg)
1783         if verbose > 0 then
1784             print('### ==> resource handler: ' .. command_name(msg.command))
1785             if verbose > 1 then
1786                 print(msg)
1787             end
1788         end
1789
1790         createResourceSet = function (ctl, client, msg)
1791             cb = function(rset, data)
1792                 print("> resource callback")
1793
1794                 -- type is either basic (0) or interrupt (1)
1795                 requestType = 0
1796                 if msg.res.type then
1797                     requestType = msg.res.type
1798                 end
1799
1800                 if rset.acquired then
1801                     cmd = 0x00040001 -- acquire
1802                 else
1803                     cmd = 0x00040002 -- release
1804                 end
1805
1806                 reply = m.JSON({
1807                         appid = data.client,
1808                         command = cmd,
1809                         res = {
1810                             type = requestType
1811                         }
1812                     })
1813
1814                 if rset.resources.audio_playback then
1815                     reply.res.sound = {
1816                         zone = "driver",
1817                         name = msg.appid,
1818                         adjust = 0,
1819                         -- id = "0"
1820                     }
1821                 end
1822
1823                 if rset.resources.display then
1824                     reply.res.window = {
1825                         zone = "driver",
1826                         name = msg.appid,
1827                         -- id = "0"
1828                     }
1829                 end
1830
1831                 if rset.resources.input then
1832                     reply.res.input = {
1833                         name = msg.appid,
1834                         event = 0
1835                     }
1836                 end
1837                 print("sending message to client: " .. data.client)
1838
1839                 if sc:send_message(data.client, reply) then
1840                     print('*** reply OK')
1841                 else
1842                     print('*** reply FAILED')
1843                 end
1844             end
1845
1846             rset = m:ResourceSet({
1847                     application_class = "player",
1848                     zone = "driver", -- msg.zone ("full")
1849                     callback = cb
1850                 })
1851
1852             rset.data = {
1853                 cid = cid,
1854                 ctl = ctl
1855             }
1856
1857             if msg.res.sound then
1858                 rset:addResource({
1859                         resource_name = "audio_playback"
1860                     })
1861                 rset.resources.audio_playback.attributes.pid = tostring(msg.pid)
1862                 rset.resources.audio_playback.attributes.appid = msg.appid
1863                 print("sound name: " .. msg.res.sound.name)
1864                 print("sound zone:" .. msg.res.sound.zone)
1865                 print("sound adjust: " .. tostring(msg.res.sound.adjust))
1866                 if msg.res.sound.id then
1867                     print("sound id: " .. msg.res.sound.id)
1868                 end
1869             end
1870
1871             if msg.res.input then
1872                 rset:addResource({
1873                         resource_name = "input"
1874                     })
1875                 rset.resources.input.attributes.pid = tostring(msg.pid)
1876                 rset.resources.input.attributes.appid = msg.appid
1877                 print("input name: " .. msg.res.sound.name)
1878                 print("input event:" .. tostring(msg.res.input.event))
1879             end
1880
1881             if msg.res.window then
1882                 rset:addResource({
1883                         resource_name = "display"
1884                     })
1885                 rset.resources.display.attributes.pid = tostring(msg.pid)
1886                 rset.resources.display.attributes.appid = msg.appid
1887                 print("display name: " .. msg.res.display.name)
1888                 print("display zone:" .. msg.res.display.zone)
1889                 if msg.res.display.id then
1890                     print("display id: " .. msg.res.display.id)
1891                 end
1892             end
1893
1894             return rset
1895         end
1896
1897         -- parse the message
1898
1899         -- fields common to all messages:
1900         --      msg.command
1901         --      msg.appid
1902         --      msg.pid
1903
1904         if msg.command == 0x00040011 then -- MSG_CMD_CREATE_RES
1905             print("command CREATE")
1906
1907             if not sets.cid then
1908                 sets.cid = createResourceSet(self, cid, msg)
1909             end
1910
1911         elseif msg.command == 0x00040012 then -- MSG_CMD_DESTORY_RES
1912             print("command DESTROY")
1913
1914             if sets.cid then
1915                 sets.cid:release()
1916             end
1917
1918             sets.cid = nil -- garbage collecting
1919
1920         elseif msg.command == 0x00040001 then -- MSG_CMD_ACQUIRE_RES
1921             print("command ACQUIRE")
1922
1923             if not sets.cid then
1924                 sets.cid = createResourceSet(self, cid, msg)
1925             end
1926
1927             sets.cid:acquire()
1928
1929         elseif msg.command == 0x00040002 then -- MSG_CMD_RELEASE_RES
1930             print("command RELEASE")
1931
1932             if sets.cid then
1933                 sets.cid:release()
1934             end
1935
1936         elseif msg.command == 0x00040003 then -- MSG_CMD_DEPRIVE_RES
1937             print("command DEPRIVE")
1938
1939         elseif msg.command == 0x00040004 then -- MSG_CMD_WAITING_RES
1940             print("command WAITING")
1941
1942         elseif msg.command == 0x00040005 then -- MSG_CMD_REVERT_RES
1943             print("command REVERT")
1944         end
1945     end
1946
1947     sc.inputdev_handler = function (self, cid, msg)
1948         if verbose > 0 then
1949             print('*** inputdev handler: ' .. command_name(msg.command))
1950             if verbose > 1 then
1951                 print(msg)
1952             end
1953         end
1954     end
1955 end